-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.mjs
More file actions
143 lines (135 loc) · 4.94 KB
/
Copy pathindex.test.mjs
File metadata and controls
143 lines (135 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { sshExec } from './index.mjs';
import { jest, test, expect } from '@jest/globals';
test('throws if host is missing', async () => {
await expect(sshExec({ commands: ['echo hi'] })).rejects.toThrow('host and commands[] are required');
});
test('throws if commands is missing', async () => {
await expect(sshExec({ host: 'localhost' })).rejects.toThrow('host and commands[] are required');
});
test('throws if no private key is found', async () => {
// Inject fsLib and homedirFn to simulate missing private key
const mockFs = { readFile: jest.fn().mockRejectedValue(new Error('not found')) };
const mockHomedir = () => '/mockhome';
class DummyClient {
on() { return this; }
connect() { return this; }
end() { return this; }
}
await expect(sshExec({
host: 'localhost',
commands: ['echo hi'],
ClientClass: DummyClient,
fsLib: mockFs,
homedirFn: mockHomedir,
})).rejects.toThrow('No private key found in ~/.ssh/');
});
test('successful command execution (mocked)', async () => {
const mockFs = { readFile: jest.fn().mockResolvedValue('PRIVATEKEY') };
const mockHomedir = () => '/mockhome';
const events = {};
class DummyStream {
constructor() { this.handlers = {}; }
on(event, cb) { this.handlers[event] = cb; return this; }
stderr = { on: (event, cb) => { this.handlers['stderr_' + event] = cb; return this; } };
trigger(event, ...args) {
if (event === 'close') {
this.handlers['close'] && this.handlers['close'](...args);
} else if (event === 'data') {
this.handlers['data'] && this.handlers['data'](...args);
} else if (event === 'stderr_data') {
this.handlers['stderr_data'] && this.handlers['stderr_data'](...args);
}
}
}
class DummyClient {
on(event, cb) { events[event] = cb; return this; }
connect() { setTimeout(() => events['ready'](), 0); return this; }
end() { return this; }
exec(cmd, cb) {
const stream = new DummyStream();
setTimeout(() => {
stream.trigger('data', `out:${cmd}`);
stream.trigger('stderr_data', `err:${cmd}`);
stream.trigger('close', 0);
}, 0);
cb(null, stream);
return this;
}
}
const result = await sshExec({
host: 'localhost',
commands: ['foo', 'bar'],
ClientClass: DummyClient,
fsLib: mockFs,
homedirFn: mockHomedir,
});
expect(result).toEqual([
{ result: 'out:fooerr:foo', code: 0 },
{ result: 'out:barerr:bar', code: 0 },
]);
});
test('ssh exec error', async () => {
const mockFs = { readFile: jest.fn().mockResolvedValue('PRIVATEKEY') };
const mockHomedir = () => '/mockhome';
class DummyClient {
on(event, cb) { if (event === 'ready') this._ready = cb; return this; }
connect() { setTimeout(() => this._ready(), 0); return this; }
end() { return this; }
exec(cmd, cb) { cb(new Error('execfail')); }
}
await expect(sshExec({
host: 'localhost',
commands: ['fail'],
ClientClass: DummyClient,
fsLib: mockFs,
homedirFn: mockHomedir,
})).rejects.toThrow('SSH exec error: execfail');
});
test('ssh connection error', async () => {
const mockFs = { readFile: jest.fn().mockResolvedValue('PRIVATEKEY') };
const mockHomedir = () => '/mockhome';
class DummyClient {
on(event, cb) { if (event === 'error') this._error = cb; if (event === 'ready') this._ready = cb; return this; }
connect() { setTimeout(() => this._error(new Error('netfail')), 0); return this; }
end() { return this; }
}
await expect(sshExec({
host: 'localhost',
commands: ['foo'],
ClientClass: DummyClient,
fsLib: mockFs,
homedirFn: mockHomedir,
})).rejects.toThrow('SSH connection error: netfail');
});
test('ssh authentication error', async () => {
const mockFs = { readFile: jest.fn().mockResolvedValue('PRIVATEKEY') };
const mockHomedir = () => '/mockhome';
class DummyClient {
on(event, cb) { if (event === 'error') this._error = cb; if (event === 'ready') this._ready = cb; return this; }
connect() { setTimeout(() => this._error({ message: 'bad auth', level: 'client-authentication' }), 0); return this; }
end() { return this; }
}
await expect(sshExec({
host: 'localhost',
commands: ['foo'],
ClientClass: DummyClient,
fsLib: mockFs,
homedirFn: mockHomedir,
})).rejects.toThrow('SSH authentication failed: bad auth');
});
test('ssh timeout error', async () => {
const mockFs = { readFile: jest.fn().mockResolvedValue('PRIVATEKEY') };
const mockHomedir = () => '/mockhome';
class DummyClient {
on(event, cb) { if (event === 'error') this._error = cb; if (event === 'ready') this._ready = cb; return this; }
connect() { setTimeout(() => this._error({ message: 'timeout', level: 'client-timeout' }), 0); return this; }
end() { return this; }
}
await expect(sshExec({
host: 'localhost',
commands: ['foo'],
ClientClass: DummyClient,
fsLib: mockFs,
homedirFn: mockHomedir,
})).rejects.toThrow('SSH connection timed out: timeout');
});