-
-
Notifications
You must be signed in to change notification settings - Fork 359
/
index_test.js
196 lines (172 loc) · 5.64 KB
/
index_test.js
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var proxyquire = require('proxyquire').noPreserveCache();
var sinon = require('sinon');
var Code = require('@hapi/code');
var Lab = require('@hapi/lab');
var lab = (exports.lab = Lab.script());
var validDbConfigWithTunnel = {
driver: 'mysql',
host: 'dbHost',
port: 'dbPort',
tunnel: {
localPort: 'localPort',
host: 'sshHost',
port: 'sshPort'
}
};
var indexConnectCallback = function (tunnelStub, driverSpy, callback) {
return function (err, db) {
if (err) {
callback(err, db, tunnelStub, driverSpy);
return;
}
callback(err, db, tunnelStub, driverSpy);
};
};
lab.experiment('index', function () {
lab.test(
'a successful connection with ssh tunnel with expected parameters',
function (flags) {
// Ensure that require gets a new copy of the module for each test
delete require.cache[require.resolve('db-migrate-mysql')];
var driver = require('db-migrate-mysql');
// Set up stubs/spies to verify correct flow
var driverSpy = sinon.stub(driver, 'connect').yields(null, {});
var tunnelStub = sinon.stub().callsArg(1);
var index = proxyquire('../../lib/driver/index', {
'tunnel-ssh': tunnelStub,
'./mysql': driver
});
// register clean up
flags.onCleanup = () => {
driverSpy.restore();
delete require.cache[require.resolve('tunnel-ssh')];
delete require.cache[require.resolve('db-migrate-mysql')];
};
index.connect(
validDbConfigWithTunnel,
{},
indexConnectCallback(tunnelStub, driverSpy, validate)
);
function validate (err, db, tunnelStub, driverSpy) {
var expectedTunnelConfig = {
localPort: 'localPort',
host: 'sshHost',
port: 'sshPort',
dstHost: 'dbHost',
dstPort: 'dbPort'
};
var expectedDbConfig = {
driver: 'mysql',
host: '127.0.0.1',
port: 'localPort',
tunnel: {
localPort: 'localPort',
host: 'sshHost',
port: 'sshPort'
}
};
Code.expect(err).to.be.null();
Code.expect(db).to.not.be.null();
Code.expect(
tunnelStub.withArgs(expectedTunnelConfig).calledOnce
).to.be.true();
Code.expect(
driverSpy.withArgs(expectedDbConfig).calledOnce
).to.be.true();
}
}
);
lab.test('a failed connection with ssh tunnel', flags => {
// Ensure that require gets a new copy of the module for each test
delete require.cache[require.resolve('db-migrate-mysql')];
var driver = require('db-migrate-mysql');
// Set up stubs/spies to verify correct flow
var tunnelStub = sinon.stub().callsArgWith(1, new Error('error'));
var driverSpy = sinon.stub(driver, 'connect').yields(null, {});
var index = proxyquire('../../lib/driver/index', {
'tunnel-ssh': tunnelStub,
'./mysql': driver
});
index.connect(
validDbConfigWithTunnel,
{},
indexConnectCallback(tunnelStub, driverSpy, validate)
);
// register clean up
flags.onCleanup = () => {
driverSpy.restore();
delete require.cache[require.resolve('tunnel-ssh')];
delete require.cache[require.resolve('db-migrate-mysql')];
};
function validate (err, db, tunnelStub, driverSpy) {
Code.expect(err, 'err should be non-null').to.exists();
Code.expect(db, 'driver should be null or undefined').to.not.exists();
Code.expect(
tunnelStub.calledOnce,
'tunnel should be called once'
).to.be.true();
Code.expect(
driverSpy.notCalled,
'driver.connect should not be called'
).to.be.true();
}
});
lab.test('privateKey gets set as expected', function (flags) {
// Ensure that require gets a new copy of the module for each test
delete require.cache[require.resolve('db-migrate-mysql')];
var driver = require('db-migrate-mysql');
var fs = { readFileSync: sinon.stub().returns('sshkey') };
// Set up stubs/spies to verify correct flow
var driverSpy = sinon.stub(driver, 'connect').yields(null, {});
var tunnelStub = sinon.stub().callsArg(1);
var index = proxyquire('../../lib/driver/index', {
'tunnel-ssh': tunnelStub,
fs: fs,
'./mysql': driver
});
// register clean up
flags.onCleanup = () => {
driverSpy.restore();
delete require.cache[require.resolve('tunnel-ssh')];
delete require.cache[require.resolve('db-migrate-mysql')];
};
validDbConfigWithTunnel.tunnel.privateKeyPath = '/test/key';
index.connect(
validDbConfigWithTunnel,
{},
indexConnectCallback(tunnelStub, driverSpy, validate)
);
function validate (err, db, tunnelStub, driverSpy) {
var expectedTunnelConfig = {
localPort: 'localPort',
host: 'sshHost',
port: 'sshPort',
privateKeyPath: '/test/key',
dstHost: '127.0.0.1',
dstPort: 'localPort',
privateKey: 'sshkey'
};
var expectedDbConfig = {
driver: 'mysql',
host: '127.0.0.1',
port: 'localPort',
tunnel: {
localPort: 'localPort',
host: 'sshHost',
port: 'sshPort',
privateKeyPath: '/test/key'
}
};
Code.expect(err).to.be.null();
Code.expect(db).to.not.be.null();
Code.expect(
fs.readFileSync.withArgs(validDbConfigWithTunnel.tunnel.privateKeyPath)
.calledOnce
).to.be.true();
Code.expect(
tunnelStub.withArgs(expectedTunnelConfig).calledOnce
).to.be.true();
Code.expect(driverSpy.withArgs(expectedDbConfig).calledOnce).to.be.true();
}
});
});