Skip to content

Commit

Permalink
test: Migrated test/integration/utilization tests to node node:test
Browse files Browse the repository at this point in the history
  • Loading branch information
bizob2828 committed Nov 25, 2024
1 parent 03b01a3 commit 1d01545
Show file tree
Hide file tree
Showing 14 changed files with 260 additions and 264 deletions.
12 changes: 0 additions & 12 deletions test/integration/utilization/aws-info.tap.js

This file was deleted.

13 changes: 13 additions & 0 deletions test/integration/utilization/aws-info.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'

const test = require('node:test')
const vendorTests = require('./vendor-info-tests')

test('pricing aws info', async function (t) {
await vendorTests(t, 'aws')
})
12 changes: 0 additions & 12 deletions test/integration/utilization/azure-info.tap.js

This file was deleted.

12 changes: 12 additions & 0 deletions test/integration/utilization/azure-info.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'
const test = require('node:test')
const vendorTests = require('./vendor-info-tests')

test('pricing azure info', async function (t) {
await vendorTests(t, 'azure')
})
55 changes: 55 additions & 0 deletions test/integration/utilization/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'
const assert = require('node:assert')
const fs = require('fs/promises')
const glob = require('glob')
const JSONbig = require('json-bigint')({ useNativeBigInt: true })
const path = require('path')

function checkMetrics(agent, expectedMetrics) {
if (!expectedMetrics) {
assert.equal(agent.metrics._metrics.toJSON().length, 0, 'should not have any metrics')
return
}

Object.keys(expectedMetrics).forEach(function (expectedMetric) {
const metric = agent.metrics.getOrCreateMetric(expectedMetric)
assert.equal(
metric.callCount,
expectedMetrics[expectedMetric].call_count,
'should have correct metric call count (' + expectedMetric + ')'
)
})
}

async function getTestCases(vendor) {
const testFile = path.resolve(
__dirname,
'../../lib/cross_agent_tests/utilization_vendor_specific',
vendor + '.json'
)
const data = await fs.readFile(testFile)
return JSONbig.parse(data)
}

async function getProcTests(type) {
const testDir = path.resolve(__dirname, '../../lib/cross_agent_tests', type)
return new Promise((resolve, reject) => {
glob(path.join(testDir, '*.txt'), function (err, fileList) {
if (err) {
return reject(err)
}
return resolve(fileList)
})
})
}

module.exports = {
checkMetrics,
getTestCases,
getProcTests
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

'use strict'

const test = require('tap').test
const test = require('node:test')
const assert = require('node:assert')
const fs = require('fs/promises')
const common = require('../../../lib/utilization/common')
const dockerInfo = require('../../../lib/utilization/docker-info')
Expand All @@ -27,7 +28,7 @@ const tests = [
tests.forEach(({ name, testsDir }) => {
test(`pricing docker info ${name}`, async function (t) {
const os = require('os')
t.teardown(function () {
t.after(function () {
os.platform.restore()
})

Expand All @@ -36,34 +37,38 @@ tests.forEach(({ name, testsDir }) => {
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.beforeEach((ctx) => {
const agent = helper.loadMockedAgent()
sinon.stub(common, 'readProc')
ctx.nr = { agent }
})

t.afterEach((ctx) => {
helper.unloadAgent(ctx.nr.agent)
dockerInfo.clearVendorCache()
common.readProc.restore()
})
t.end()

for (const testCase of cases) {
const testFile = path.join(testsDir, testCase.filename)
await t.test(testCase.filename, makeTest(testCase, testFile, name === 'v2'))
}
})
})

function makeTest(testCase, testFile, v2) {
return async function (t) {
const agent = helper.loadMockedAgent()
sinon.stub(common, 'readProc')
const { agent } = t.nr
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')
assert.ok(!err, 'should not have failed')
assert.deepEqual(info, { id: testCase.containerId }, 'should have expected container id')
} else {
t.notOk(info, 'should not have found container id')
assert.ok(!info, 'should not have found container id')
}

resolve()
Expand Down
12 changes: 0 additions & 12 deletions test/integration/utilization/gcp-info.tap.js

This file was deleted.

12 changes: 12 additions & 0 deletions test/integration/utilization/gcp-info.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'
const test = require('node:test')
const vendorTests = require('./vendor-info-tests')

test('pricing gcp info', async function (t) {
await vendorTests(t, 'gcp')
})
82 changes: 0 additions & 82 deletions test/integration/utilization/pcf-info.tap.js

This file was deleted.

57 changes: 57 additions & 0 deletions test/integration/utilization/pcf-info.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'
const helper = require('../../lib/agent_helper')
const test = require('node:test')
const assert = require('node:assert')
const { checkMetrics, getTestCases } = require('./common')

test('pricing pcf info', async function (t) {
const cases = await getTestCases('pcf')
assert.ok(cases.length > 0, 'should have tests to run')
const getInfo = require('../../../lib/utilization/pcf-info')

t.beforeEach((ctx) => {
const agent = helper.loadMockedAgent()
ctx.nr = { agent }
})

t.afterEach((ctx) => {
helper.unloadAgent(ctx.nr.agent)
})

for (const testCase of cases) {
await t.test(testCase.testname, makeTest(testCase, getInfo))
}
})

function makeTest(testCase, getInfo) {
return function (t, end) {
const { agent } = t.nr
Object.keys(testCase.env_vars).forEach(function (key) {
const value = testCase.env_vars[key].response
if (value == null) {
delete process.env[key]
} else {
process.env[key] = value
}
})

getInfo(agent, function (err, info) {
if (testCase.expected_vendors_hash) {
const expected = testCase.expected_vendors_hash.pcf
assert.ok(!err, 'should not error getting data')
assert.deepEqual(info, expected, 'should have expected info')
} else {
assert.ok(!info, 'should not have received vendor info')
}

checkMetrics(agent, testCase.expected_metrics)

end()
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,26 @@

'use strict'

const test = require('tap').test
const glob = require('glob')
const test = require('node:test')
const { tspl } = require('@matteo.collina/tspl')
const fs = require('fs/promises')
const parseProcCpuInfo = require('../../../lib/parse-proc-cpuinfo')
const path = require('path')
const { getProcTests } = require('./common')

test('pricing proc_cpuinfo', async function (t) {
const testDir = path.resolve(__dirname, '../../lib/cross_agent_tests/proc_cpuinfo')
const data = await new Promise((resolve, reject) => {
glob(path.join(testDir, '*.txt'), function globCallback(err, fileList) {
if (err) {
return reject(err)
}
resolve(fileList)
})
})
t.ok(data.length > 0, 'should have tests to run')
const data = await getProcTests('proc_cpuinfo')
const plan = tspl(t, { plan: data.length + 1 })

plan.ok(data.length > 0, 'should have tests to run')
for (const name of data) {
const buffer = await fs.readFile(name)
const file = buffer.toString()
const expected = parseName(name)
const info = parseProcCpuInfo(file)
t.same(info, expected, 'should have expected info for ' + name)
plan.deepEqual(info, expected, 'should have expected info for ' + name)
}
t.end()

await plan.completed

function parseName(name) {
const pattern = /^((\d+|X)pack_(\d+|X)core_(\d+|X)logical).txt$/
Expand Down
Loading

0 comments on commit 1d01545

Please sign in to comment.