This repository has been archived by the owner on May 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
85 lines (72 loc) · 2.15 KB
/
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
const ec2Info = require('./index')
const { expect } = require('chai')
const http = require('http')
const { parse } = require('url')
const SOURCE_INFO = {
'/made-up': 'bar',
'/meta-data/ami-id': 'foo',
'/meta-data/public-ipv4': '1.2.3.4',
'/meta-data/instance-id': '123'
}
const port = 3003
const dataURL = 'http://localhost:' + port
const testOptions = { dataURL, testIsEc2Machine: false }
describe('ec2-info', () => {
let server
it('fetches properties', (done) => {
let properties = ['meta-data/ami-id', '/meta-data/instance-id', 'made-up']
ec2Info(properties, testOptions, (err, info) => {
if (err) return done(err)
expect(info.size).to.equal(3)
expect(info.get('meta-data/ami-id')).to.equal('foo')
expect(info.get('/meta-data/instance-id')).to.equal('123')
expect(info.get('made-up')).to.equal('bar')
done()
})
})
it('on a machine that is not an ec2 machine, dont fail and dont fetch anything', () => {
let properties = ['meta-data/ami-id', 'meta-data/instance-id']
ec2Info(properties, (err, info) => {
if (err) return done(err)
expect(info.size).to.equal(2)
for (let [property, value] of info) {
expect(value).to.equal('not an ec2 machine')
}
})
})
beforeEach((done) => {
server = startHttpTestServer(SOURCE_INFO, port, done)
})
afterEach((done) => {
if (server) {
return server.close(done)
}
done()
})
})
describe.skip('integration test', () => {
it('fetches properties on a real ec2-machine', (done) => {
let properties = ['meta-data/services/partition', '/meta-data/services/domain']
let options = { dataURL: 'http://localhost:8080/latest/' }
ec2Info(properties, options, (err, info) => {
if (err) return done(err)
expect(info.size).to.equal(2)
expect(info.get('meta-data/services/partition')).to.equal('aws')
expect(info.get('/meta-data/services/domain')).to.equal('amazonaws.com')
done()
})
})
})
function startHttpTestServer(info, port, callback) {
let server = http.createServer((req, res) => {
let url = parse(req.url)
let value = info[url.path]
if (!value) {
res.statusCode = 404
value = 'error'
}
res.end(value)
})
server.listen(port, callback)
return server
}