-
Notifications
You must be signed in to change notification settings - Fork 924
/
Copy pathdiscovery.test.ts
337 lines (282 loc) · 10.8 KB
/
discovery.test.ts
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
* @license
* Copyright 2021 Google LLC
*
* 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';
import * as sinon from 'sinon';
import {
discoverEmulators,
EMULATOR_HOST_ENV_VARS,
getEmulatorHostAndPort
} from '../../src/impl/discovery';
import { HostAndPort } from '../../src/public_types';
import { restoreEnvVars, stashEnvVars } from '../test_utils';
describe('discoverEmulators()', () => {
it('finds all running emulators', async () => {
const emulators = await discoverEmulators(getEmulatorHostAndPort('hub')!);
expect(emulators).to.deep.equal({
database: {
host: '127.0.0.1',
port: 9002
},
firestore: {
host: '127.0.0.1',
port: 9003
},
storage: {
host: '127.0.0.1',
port: 9199
},
hub: {
host: '127.0.0.1',
port: 4400
}
});
});
it('connect to IPv6 addresses correctly', async () => {
const fetch = sinon.fake(async () => {
return {
ok: true,
async json() {
return {};
}
};
});
const emulators = await discoverEmulators(
{ host: '::1', port: 1111 },
fetch as any
);
expect(fetch).to.be.calledOnceWith(new URL('http://[::1]:1111/emulators')); // bracketed
expect(emulators).to.deep.equal({});
});
it('throws error if emulator hub is unreachable', async () => {
// Connect to port:0. Should always fail (although error codes may differ among OSes).
await expect(
discoverEmulators({ host: '127.0.0.1', port: 0 })
).to.be.rejectedWith(/EADDRNOTAVAIL|ECONNREFUSED|fetch failed/);
});
it('throws if response status is not 2xx', async () => {
const fetch = sinon.fake(async () => {
return {
ok: false,
status: 666,
async json() {
return {};
}
};
});
await expect(
discoverEmulators({ host: '127.0.0.1', port: 4444 }, fetch as any)
).to.be.rejectedWith(/HTTP Error 666/);
});
});
describe('getEmulatorHostAndPort()', () => {
context('without env vars', () => {
beforeEach(() => {
stashEnvVars();
});
afterEach(() => {
restoreEnvVars();
});
it('returns undefined if config option is not set', async () => {
const result = getEmulatorHostAndPort('hub');
expect(result).to.be.undefined;
});
it('returns undefined if config option does not contain host/port', async () => {
const result = getEmulatorHostAndPort('hub', {
rules: '/* security rules only, no host/port */'
});
expect(result).to.be.undefined;
});
it('removes brackets from IPv6 hosts', async () => {
const result = getEmulatorHostAndPort('hub', {
host: '[::1]',
port: 1111
});
expect(result?.host).to.equal('::1');
});
it('throws if only host is present', async () => {
expect(() =>
getEmulatorHostAndPort('hub', {
host: '[::1]'
} as HostAndPort)
).to.throw(/hub.port=undefined/);
});
it('throws if only port is present', async () => {
expect(() =>
getEmulatorHostAndPort('database', {
port: 1234
} as HostAndPort)
).to.throw(/Invalid configuration database.host=undefined/);
});
it('connect to 127.0.0.1 if host is wildcard 0.0.0.0', async () => {
const result = getEmulatorHostAndPort('hub', {
host: '0.0.0.0',
port: 1111
});
// Do not connect to 0.0.0.0 which is invalid and won't work on some OSes.
expect(result?.host).to.equal('127.0.0.1');
});
it('connect to [::1] if host is wildcard [::]', async () => {
const result = getEmulatorHostAndPort('hub', { host: '::1', port: 1111 });
// Do not connect to :: which is invalid and won't work on some OSes.
expect(result?.host).to.equal('::1');
});
it('uses discovered host/port if both config and env var are unset', async () => {
const result = getEmulatorHostAndPort('hub', undefined, {
hub: { host: '::1', port: 3333 }
});
expect(result?.host).to.equal('::1');
expect(result?.port).to.equal(3333);
});
it('returns undefined if none of config, env var, discovered contains emulator', async () => {
const result = getEmulatorHostAndPort('database', undefined, {
hub: { host: '::1', port: 3333 } /* only hub, no database */
});
expect(result).to.be.undefined;
});
it('uses hub host as fallback if discovered host is wildcard 0.0.0.0/[::]', async () => {
const result = getEmulatorHostAndPort('database', undefined, {
database: { host: '0.0.0.0', port: 1111 },
hub: { host: '10.0.0.1', port: 3333 }
});
// If we can reach hub via 10.0.0.1 but database has host 0.0.0.0, it is very likely that
// database is also running on 10.0.0.1 and listening on all IPv4 addresses.
expect(result?.host).to.equal('10.0.0.1');
expect(result?.port).to.equal(1111);
const result2 = getEmulatorHostAndPort('database', undefined, {
database: { host: '::', port: 2222 },
hub: { host: '10.0.0.1', port: 3333 }
});
// The situation is less ideal when database listens on all IPv6 addresses, but we'll still
// try the same host as hub, hoping that the OS running database forwards v6 to v4.
expect(result2?.host).to.equal('10.0.0.1');
expect(result2?.port).to.equal(2222);
});
it('uses hub host as fallback if config host is wildcard 0.0.0.0/[::]', async () => {
// We apply the same logic to manually specified {host: '0.0.0.0'}, although it is a bit
// unclear what the developer actually means in that case. If we get this wrong though, the
// developer can always manually specify a non-wildcard address instead.
const discovered = {
hub: { host: '10.0.0.1', port: 3333 }
};
const result = getEmulatorHostAndPort(
'database',
{
host: '0.0.0.0',
port: 1111
},
discovered
);
expect(result?.host).to.equal('10.0.0.1');
expect(result?.port).to.equal(1111);
const result2 = getEmulatorHostAndPort(
'database',
{
host: '[::]',
port: 2222
},
discovered
);
expect(result2?.host).to.equal('10.0.0.1');
expect(result2?.port).to.equal(2222);
});
});
context('with env vars', () => {
beforeEach(() => {
stashEnvVars();
});
afterEach(() => {
restoreEnvVars();
});
it('parses IPv4 host + port correctly from env var', async () => {
process.env[EMULATOR_HOST_ENV_VARS.hub] = '127.0.0.1:3445';
const result = getEmulatorHostAndPort('hub');
expect(result?.host).to.equal('127.0.0.1');
expect(result?.port).to.equal(3445);
});
it('throws if port is not a number', async () => {
process.env[EMULATOR_HOST_ENV_VARS.hub] = '127.0.0.1:hhh';
expect(() => getEmulatorHostAndPort('hub')).to.throw(
/Invalid format in environment variable FIREBASE_EMULATOR_HUB/
);
});
it('parses IPv6 host + port correctly from env var and removes brackets', async () => {
process.env[EMULATOR_HOST_ENV_VARS.hub] = '[::1]:3445';
const result = getEmulatorHostAndPort('hub');
expect(result?.host).to.equal('::1');
expect(result?.port).to.equal(3445);
});
it('parses env var with IPv6 host but no port correctly', async () => {
process.env[EMULATOR_HOST_ENV_VARS.hub] = '[::1]';
const result = getEmulatorHostAndPort('hub');
expect(result?.host).to.equal('::1');
expect(result?.port).to.equal(80); // default port
});
it('parses env var with host but no port correctly', async () => {
process.env[EMULATOR_HOST_ENV_VARS.hub] = 'myhub.example.com';
const result = getEmulatorHostAndPort('hub');
expect(result?.host).to.equal('myhub.example.com');
expect(result?.port).to.equal(80); // default port
});
it('connect to 127.0.0.1 if host is wildcard 0.0.0.0', async () => {
process.env[EMULATOR_HOST_ENV_VARS.hub] = '0.0.0.0:3445';
const result = getEmulatorHostAndPort('hub');
// Do not connect to 0.0.0.0 which is invalid and won't work on some OSes.
expect(result?.host).to.equal('127.0.0.1');
});
it('connect to [::1] if host is wildcard [::]', async () => {
process.env[EMULATOR_HOST_ENV_VARS.hub] = '[::]:3445';
const result = getEmulatorHostAndPort('hub');
// Do not connect to :: which is invalid and won't work on some OSes.
expect(result?.host).to.equal('::1');
});
it('prefers config value over env var', async () => {
process.env[EMULATOR_HOST_ENV_VARS.hub] = '127.0.0.1:3445'; // ignored
const result = getEmulatorHostAndPort('hub', {
host: 'localhost',
port: 1234
});
expect(result?.host).to.equal('localhost');
expect(result?.port).to.equal(1234);
});
it('takes host and port from env var if config has no host/port', async () => {
process.env[EMULATOR_HOST_ENV_VARS.hub] = '127.0.0.1:3445';
const result = getEmulatorHostAndPort('hub', {
rules: '/* security rules only, no host/port */'
});
expect(result?.host).to.equal('127.0.0.1');
expect(result?.port).to.equal(3445);
});
it('uses hub host as fallback if host from env var is wildcard 0.0.0.0/[::]', async () => {
process.env[EMULATOR_HOST_ENV_VARS.database] = '0.0.0.0:1111';
const result = getEmulatorHostAndPort('database', undefined, {
hub: { host: '10.0.0.1', port: 3333 }
});
// If we can reach hub via 10.0.0.1 but database has host 0.0.0.0, it is very likely that
// database is also running on 10.0.0.1 and listening on all IPv4 addresses.
expect(result?.host).to.equal('10.0.0.1');
expect(result?.port).to.equal(1111);
process.env[EMULATOR_HOST_ENV_VARS.database] = '[::]:2222';
const result2 = getEmulatorHostAndPort('database', undefined, {
hub: { host: '10.0.0.1', port: 3333 }
});
// The situation is less ideal when database listens on all IPv6 addresses, but we'll still
// try the same host as hub, hoping that the OS running database forwards v6 to v4.
expect(result2?.host).to.equal('10.0.0.1');
expect(result2?.port).to.equal(2222);
});
});
});