2020-07-02 10:11:23 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2020 Balena Ltd.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { expect } from 'chai';
|
2024-09-04 17:33:52 +00:00
|
|
|
import * as mock from 'mock-require';
|
2024-03-14 12:11:53 +00:00
|
|
|
import type { Server } from 'net';
|
|
|
|
import { createServer } from 'net';
|
2020-07-02 10:11:23 +00:00
|
|
|
|
2024-09-26 15:26:19 +00:00
|
|
|
import { BalenaAPIMock } from '../../nock/balena-api-mock';
|
|
|
|
import { cleanOutput, runCommand } from '../../helpers';
|
2020-07-02 10:11:23 +00:00
|
|
|
|
|
|
|
// "itSS" means "it() Skip Standalone"
|
|
|
|
const itSS = process.env.BALENA_CLI_TEST_TYPE === 'standalone' ? it.skip : it;
|
|
|
|
|
2024-09-26 15:26:19 +00:00
|
|
|
describe('balena device ssh', function () {
|
2020-07-02 10:11:23 +00:00
|
|
|
let api: BalenaAPIMock;
|
|
|
|
let sshServer: Server | undefined;
|
|
|
|
let sshServerPort: number;
|
|
|
|
let hasSshExecutable = false;
|
|
|
|
let mockedExitCode = 0;
|
|
|
|
|
2022-02-11 17:18:42 +00:00
|
|
|
async function mockSpawn({ revert = false } = {}) {
|
2022-02-24 22:37:21 +00:00
|
|
|
const childProcessPath = 'child_process';
|
2022-02-11 17:18:42 +00:00
|
|
|
if (revert) {
|
2022-02-24 22:37:21 +00:00
|
|
|
mock.stop(childProcessPath);
|
2024-09-26 15:26:19 +00:00
|
|
|
mock.reRequire('../../../build/utils/ssh');
|
|
|
|
mock.reRequire('../../../build/utils/device/ssh');
|
2022-02-11 17:18:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const { EventEmitter } = await import('stream');
|
2022-02-24 22:37:21 +00:00
|
|
|
const childProcessMod = await import(childProcessPath);
|
2022-02-11 17:18:42 +00:00
|
|
|
const originalSpawn = childProcessMod.spawn;
|
2022-02-24 22:37:21 +00:00
|
|
|
mock(childProcessPath, {
|
2022-02-11 17:18:42 +00:00
|
|
|
...childProcessMod,
|
|
|
|
spawn: (program: string, ...args: any[]) => {
|
|
|
|
if (program.includes('ssh')) {
|
|
|
|
const emitter = new EventEmitter();
|
|
|
|
setTimeout(() => emitter.emit('close', mockedExitCode), 1);
|
|
|
|
return emitter;
|
|
|
|
}
|
|
|
|
return originalSpawn(program, ...args);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-02 10:11:23 +00:00
|
|
|
this.beforeAll(async function () {
|
|
|
|
hasSshExecutable = await checkSsh();
|
2022-02-11 17:18:42 +00:00
|
|
|
if (!hasSshExecutable) {
|
|
|
|
this.skip();
|
2020-07-02 10:11:23 +00:00
|
|
|
}
|
2022-02-11 17:18:42 +00:00
|
|
|
[sshServer, sshServerPort] = await startMockSshServer();
|
|
|
|
await mockSpawn();
|
2020-07-02 10:11:23 +00:00
|
|
|
});
|
|
|
|
|
2022-02-11 17:18:42 +00:00
|
|
|
this.afterAll(async function () {
|
2020-07-02 10:11:23 +00:00
|
|
|
if (sshServer) {
|
|
|
|
sshServer.close();
|
|
|
|
sshServer = undefined;
|
|
|
|
}
|
2022-02-11 17:18:42 +00:00
|
|
|
await mockSpawn({ revert: true });
|
2020-07-02 10:11:23 +00:00
|
|
|
});
|
|
|
|
|
2022-02-11 17:18:42 +00:00
|
|
|
this.beforeEach(function () {
|
2020-07-02 10:11:23 +00:00
|
|
|
api = new BalenaAPIMock();
|
|
|
|
});
|
|
|
|
|
2022-02-11 17:18:42 +00:00
|
|
|
this.afterEach(function () {
|
2020-07-02 10:11:23 +00:00
|
|
|
// Check all expected api calls have been made and clean up.
|
|
|
|
api.done();
|
|
|
|
});
|
|
|
|
|
|
|
|
itSS('should succeed (mocked, device UUID)', async () => {
|
|
|
|
const deviceUUID = 'abc1234';
|
|
|
|
api.expectGetWhoAmI({ optional: true, persist: true });
|
2020-12-16 15:57:25 +00:00
|
|
|
api.expectGetDevice({ fullUUID: deviceUUID, isOnline: true });
|
2020-07-02 10:11:23 +00:00
|
|
|
mockedExitCode = 0;
|
|
|
|
|
2024-09-26 15:26:19 +00:00
|
|
|
const { err, out } = await runCommand(`device ssh ${deviceUUID}`);
|
2020-12-16 15:57:25 +00:00
|
|
|
|
2020-07-02 10:11:23 +00:00
|
|
|
expect(err).to.be.empty;
|
|
|
|
expect(out).to.be.empty;
|
|
|
|
});
|
|
|
|
|
|
|
|
itSS('should succeed (mocked, device IP address)', async () => {
|
|
|
|
mockedExitCode = 0;
|
2024-09-26 15:26:19 +00:00
|
|
|
const { err, out } = await runCommand(`device ssh 1.2.3.4`);
|
2020-07-02 10:11:23 +00:00
|
|
|
expect(err).to.be.empty;
|
|
|
|
expect(out).to.be.empty;
|
|
|
|
});
|
|
|
|
|
|
|
|
itSS(
|
|
|
|
'should produce the expected error message (mocked, device UUID)',
|
|
|
|
async () => {
|
|
|
|
const deviceUUID = 'abc1234';
|
|
|
|
const expectedErrLines = [
|
2022-02-11 17:18:42 +00:00
|
|
|
'SSH: Remote command "host abc1234" exited with non-zero status code "255"',
|
2020-07-02 10:11:23 +00:00
|
|
|
];
|
|
|
|
api.expectGetWhoAmI({ optional: true, persist: true });
|
2020-12-16 15:57:25 +00:00
|
|
|
api.expectGetDevice({ fullUUID: deviceUUID, isOnline: true });
|
2020-07-02 10:11:23 +00:00
|
|
|
mockedExitCode = 255;
|
|
|
|
|
2024-09-26 15:26:19 +00:00
|
|
|
const { err, out } = await runCommand(`device ssh ${deviceUUID}`);
|
2020-07-02 10:11:23 +00:00
|
|
|
expect(cleanOutput(err, true)).to.include.members(expectedErrLines);
|
|
|
|
expect(out).to.be.empty;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-02-24 22:37:21 +00:00
|
|
|
itSS('should fail if device not online (mocked, device UUID)', async () => {
|
2020-12-16 15:57:25 +00:00
|
|
|
const deviceUUID = 'abc1234';
|
2024-09-17 12:10:02 +00:00
|
|
|
const expectedErrLines = ['Device with UUID abc1234 is disconnected'];
|
2020-12-16 15:57:25 +00:00
|
|
|
api.expectGetWhoAmI({ optional: true, persist: true });
|
|
|
|
api.expectGetDevice({ fullUUID: deviceUUID, isOnline: false });
|
|
|
|
mockedExitCode = 0;
|
|
|
|
|
2024-09-26 15:26:19 +00:00
|
|
|
const { err, out } = await runCommand(`device ssh ${deviceUUID}`);
|
2020-12-16 15:57:25 +00:00
|
|
|
|
|
|
|
expect(cleanOutput(err, true)).to.include.members(expectedErrLines);
|
|
|
|
expect(out).to.be.empty;
|
|
|
|
});
|
2022-02-11 17:18:42 +00:00
|
|
|
|
|
|
|
it('should produce the expected error message (real ssh, device IP address)', async function () {
|
|
|
|
await mockSpawn({ revert: true });
|
2022-02-24 22:37:21 +00:00
|
|
|
api.expectGetWhoAmI({ optional: true, persist: true });
|
2022-02-11 17:18:42 +00:00
|
|
|
const expectedErrLines = [
|
|
|
|
'SSH: Process exited with non-zero status code "255"',
|
|
|
|
];
|
|
|
|
const { err, out } = await runCommand(
|
2024-09-26 15:26:19 +00:00
|
|
|
`device ssh 127.0.0.1 -p ${sshServerPort} --noproxy`,
|
2022-02-11 17:18:42 +00:00
|
|
|
);
|
|
|
|
expect(cleanOutput(err, true)).to.include.members(expectedErrLines);
|
|
|
|
expect(out).to.be.empty;
|
|
|
|
});
|
2020-07-02 10:11:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/** Check whether the 'ssh' tool (executable) exists in the PATH */
|
|
|
|
async function checkSsh(): Promise<boolean> {
|
2024-09-26 15:26:19 +00:00
|
|
|
const { which } = await import('../../../build/utils/which');
|
2020-07-02 10:11:23 +00:00
|
|
|
const sshPath = await which('ssh', false);
|
2020-11-30 22:58:46 +00:00
|
|
|
if ((sshPath || '').includes('\\Windows\\System32\\OpenSSH\\ssh')) {
|
|
|
|
// don't use Windows' built-in ssh tool for these test cases
|
|
|
|
// because it messes up with the terminal window such that
|
|
|
|
// "line breaks stop working" (and not even '\033c' fixes it)
|
|
|
|
// and all mocha output gets printed on a single very long line...
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-02 10:11:23 +00:00
|
|
|
return !!sshPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Start a TCP server (listening socket), used as a mock ssh server */
|
|
|
|
async function startMockSshServer(): Promise<[Server, number]> {
|
|
|
|
const server = createServer((c) => {
|
|
|
|
// 'connection' listener
|
2020-11-30 22:58:46 +00:00
|
|
|
const handler = (msg: string) => {
|
2020-07-02 10:11:23 +00:00
|
|
|
if (process.env.DEBUG) {
|
2020-11-30 22:58:46 +00:00
|
|
|
console.error(`[debug] mock ssh server: ${msg}`);
|
2020-07-02 10:11:23 +00:00
|
|
|
}
|
2020-11-30 22:58:46 +00:00
|
|
|
};
|
|
|
|
c.on('error', (err) => handler(err.message));
|
|
|
|
c.on('end', () => handler('client disconnected'));
|
2020-07-02 10:11:23 +00:00
|
|
|
c.end();
|
|
|
|
});
|
|
|
|
server.on('error', (err) => {
|
|
|
|
console.error(`mock ssh server error:\n${err}`);
|
|
|
|
});
|
|
|
|
|
2022-02-24 22:37:21 +00:00
|
|
|
return await new Promise<[Server, number]>((resolve, reject) => {
|
2023-05-02 15:46:32 +00:00
|
|
|
// TODO: remove 'as any' below. According to @types/node v16.18.25, the
|
2022-01-19 21:40:53 +00:00
|
|
|
// callback type is `() => void`, but our code assumes `(err: Error) => void`
|
|
|
|
const listener = (server.listen as any)(0, '127.0.0.1', (err: Error) => {
|
2020-07-02 10:11:23 +00:00
|
|
|
// this callback is called for the 'listening' event
|
|
|
|
if (err) {
|
|
|
|
console.error(`Error starting mock ssh server:\n${err}`);
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
const info: any = listener.address();
|
|
|
|
console.error(
|
|
|
|
`[Info] Mock ssh server listening on ${info.address}:${info.port}`,
|
|
|
|
);
|
|
|
|
resolve([server, info.port]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|