/
docker-info.tap.js
82 lines (70 loc) · 2.25 KB
/
docker-info.tap.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
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
'use strict'
const test = require('tap').test
const fs = require('fs/promises')
const common = require('../../../lib/utilization/common')
const dockerInfo = require('../../../lib/utilization/docker-info')
const helper = require('../../lib/agent_helper')
const path = require('path')
const sinon = require('sinon')
const TEST_DIRECTORY = path.resolve(__dirname, '../../lib/cross_agent_tests/docker_container_id/')
const TEST_DIRECTORY_V2 = path.resolve(
__dirname,
'../../lib/cross_agent_tests/docker_container_id_v2/'
)
const tests = [
{ name: 'v1', testsDir: TEST_DIRECTORY },
{ name: 'v2', testsDir: TEST_DIRECTORY_V2 }
]
tests.forEach(({ name, testsDir }) => {
test(`pricing docker info ${name}`, async function (t) {
const os = require('os')
t.teardown(function () {
os.platform.restore()
})
sinon.stub(os, 'platform')
os.platform.returns('linux')
const data = await fs.readFile(`${testsDir}/cases.json`)
const cases = JSON.parse(data)
cases.forEach((testCase) => {
const testFile = path.join(testsDir, testCase.filename)
t.test(testCase.filename, makeTest(testCase, testFile, name === 'v2'))
})
t.end()
})
})
function makeTest(testCase, testFile, v2) {
return async function (t) {
const agent = helper.loadMockedAgent()
sinon.stub(common, 'readProc')
const file = await fs.readFile(testFile, { encoding: 'utf8' })
mockProcRead(file, v2)
t.teardown(function () {
helper.unloadAgent(agent)
dockerInfo.clearVendorCache()
common.readProc.restore()
})
await new Promise((resolve) => {
dockerInfo.getVendorInfo(agent, function (err, info) {
if (testCase.containerId) {
t.error(err, 'should not have failed')
t.same(info, { id: testCase.containerId }, 'should have expected container id')
} else {
t.notOk(info, 'should not have found container id')
}
resolve()
})
})
}
}
function mockProcRead(data, v2) {
if (!v2) {
common.readProc.onCall(0).yields(null, null)
common.readProc.onCall(1).yields(null, data)
} else {
common.readProc.onCall(0).yields(null, data)
}
}