Merge pull request #2038 from balena-os/legacy-unit-tests

Migrate simple legacy tests to test/unit and test/integration
This commit is contained in:
bulldozer-balena[bot] 2022-10-19 13:34:07 +00:00 committed by GitHub
commit 0a423811a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 90 additions and 70 deletions

View File

@ -0,0 +1,45 @@
import { expect } from 'chai';
import { testfs, TestFs } from 'mocha-pod';
import * as osRelease from '~/lib/os-release';
describe('lib/os-release', () => {
let tfs: TestFs.Enabled;
before(async () => {
tfs = await testfs({
'/etc/os-release': testfs.from('test/data/etc/os-release-tx2'),
}).enable();
});
after(async () => {
await tfs.restore();
});
it('gets pretty name', async () => {
// Try to get PRETTY_NAME
await expect(osRelease.getOSVersion('/etc/os-release')).to.eventually.equal(
'balenaOS 2.45.1+rev3',
);
});
it('gets variant', async () => {
// Try to get VARIANT_ID
await expect(osRelease.getOSVariant('/etc/os-release')).to.eventually.equal(
'prod',
);
});
it('gets version', async () => {
// Try to get VERSION
await expect(osRelease.getOSSemver('/etc/os-release')).to.eventually.equal(
'2.45.1+rev3',
);
});
it('gets meta release version', async () => {
// Try to get META_BALENA_VERSIONS
await expect(
osRelease.getMetaOSRelease('/etc/os-release'),
).to.eventually.equal('2.45.1');
});
});

View File

@ -0,0 +1,36 @@
import * as path from 'path';
import { promises as fs } from 'fs';
import { SinonStub } from 'sinon';
import { testfs } from 'mocha-pod';
import { expect } from 'chai';
import Log from '~/lib/supervisor-console';
import * as network from '~/src/network';
import * as constants from '~/src/lib/constants';
describe('network', () => {
it('checks VPN connection status', async () => {
const vpnStatusPath = path.join(constants.vpnStatusPath, 'active');
// Logstub already exists as part of the test hooks
const logStub = Log.info as SinonStub;
// When VPN is inactive vpnStatusPath does not exist
await expect(
fs.access(vpnStatusPath),
'VPN active file does not exist before testing',
).to.be.rejected;
await expect(network.isVPNActive()).to.eventually.equal(false);
expect(logStub.lastCall?.lastArg).to.equal(`VPN connection is not active.`);
// Test when VPN is active
const testFs = await testfs({
[vpnStatusPath]: '',
}).enable();
await expect(network.isVPNActive()).to.eventually.equal(true);
expect(logStub.lastCall?.lastArg).to.equal(`VPN connection is active.`);
// Restore file system
await testFs.restore();
});
});

View File

@ -1,9 +0,0 @@
import { expect } from 'chai';
import * as constants from '~/lib/constants';
describe('constants', function () {
it('has the correct configJsonPathOnHost', () =>
expect(constants.configJsonPathOnHost).to.equal('/config.json'));
it('has the correct rootMountPoint', () =>
expect(constants.rootMountPoint).to.equal('./test/data'));
});

View File

@ -9,6 +9,8 @@ import * as constants from '~/src/lib/constants';
import { docker } from '~/lib/docker-utils';
import { Supervisor } from '~/src/supervisor';
// TODO: remove this when we can test proper supervisor startup during
// integration tests
describe('Startup', () => {
let startStub: SinonStub;
let vpnStatusPathStub: SinonStub;

View File

@ -1,35 +0,0 @@
import { expect } from 'chai';
import * as osRelease from '~/lib/os-release';
const OS_RELEASE_PATH = 'test/data/etc/os-release-tx2';
describe('OS Release Information', () => {
it('gets pretty name', async () => {
// Try to get PRETTY_NAME
await expect(osRelease.getOSVersion(OS_RELEASE_PATH)).to.eventually.equal(
'balenaOS 2.45.1+rev3',
);
});
it('gets variant', async () => {
// Try to get VARIANT_ID
await expect(osRelease.getOSVariant(OS_RELEASE_PATH)).to.eventually.equal(
'prod',
);
});
it('gets version', async () => {
// Try to get VERSION
await expect(osRelease.getOSSemver(OS_RELEASE_PATH)).to.eventually.equal(
'2.45.1+rev3',
);
});
it('gets meta release version', async () => {
// Try to get META_BALENA_VERSIONS
await expect(
osRelease.getMetaOSRelease(OS_RELEASE_PATH),
).to.eventually.equal('2.45.1');
});
});

View File

@ -6,7 +6,7 @@ const PortMapPublic = PortMap as any as new (
portStrOrObj: string | PortRange,
) => PortMap;
describe('Ports', function () {
describe('compose/ports', function () {
describe('Port string parsing', function () {
it('should correctly parse a port string without a range', function () {
expect(new PortMapPublic('80')).to.deep.equal(

View File

@ -1,7 +1,7 @@
import { expect } from 'chai';
import * as ComposeUtils from '~/src/compose/utils';
describe('Composition utilities', () =>
describe('compose/utils', () =>
it('Should correctly camel case the configuration', function () {
const config = {
networks: ['test', 'test2'],

View File

@ -1,7 +1,7 @@
import { expect } from 'chai';
import * as conversion from '~/lib/conversions';
describe('conversions', function () {
describe('lib/conversions', function () {
describe('envArrayToObject', function () {
it('should convert an env array to an object', () =>
expect(

View File

@ -4,7 +4,7 @@ import { expect } from 'chai';
import constants = require('~/lib/constants');
import { spawnJournalctl } from '~/lib/journald';
describe('journald', () => {
describe('lib/journald', () => {
let spawn: SinonStub;
beforeEach((done) => {
@ -17,6 +17,8 @@ describe('journald', () => {
done();
});
// TODO: this test is not really that useful as it basically is just testing
// the internal implementation of the method
it('spawnJournalctl calls spawn child process with expected args', () => {
spawnJournalctl({
all: true,

View File

@ -1,9 +1,7 @@
import { promises as fs } from 'fs';
import * as os from 'os';
import { stub, spy } from 'sinon';
import { stub } from 'sinon';
import { expect } from 'chai';
import Log from '~/lib/supervisor-console';
import * as network from '~/src/network';
describe('network', () => {
@ -92,23 +90,4 @@ describe('network', () => {
'2605:9080:1103:3011:2dbe:35e3:1b5a:b99',
]));
});
it('checks VPN connection status', async () => {
const statStub = stub(fs, 'lstat');
const logStub = spy(Log, 'info');
// Test when VPN is inactive
statStub.rejects(); // Reject so we can't stat the vpn active file
await expect(network.isVPNActive()).to.eventually.equal(false);
expect(logStub.lastCall?.lastArg).to.equal(`VPN connection is not active.`);
// Test when VPN is active
statStub.resolves(); // Resolve so we can stat the vpn active file
await expect(network.isVPNActive()).to.eventually.equal(true);
expect(logStub.lastCall?.lastArg).to.equal(`VPN connection is active.`);
// Restore stubs
statStub.restore();
logStub.restore();
});
});