mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-01-31 08:25:36 +00:00
Migrate docker-util tests
Merged docker-utils and delta tests into a single test suite. They are now ran as part of the integration tests using the real engine. Change-type: patch
This commit is contained in:
parent
93ca9c05f3
commit
aa3002f909
173
test/integration/lib/docker-utils.spec.ts
Normal file
173
test/integration/lib/docker-utils.spec.ts
Normal file
@ -0,0 +1,173 @@
|
||||
import { expect } from 'chai';
|
||||
import { stub } from 'sinon';
|
||||
|
||||
import * as dockerUtils from '~/lib/docker-utils';
|
||||
import { createDockerImage } from '~/test-lib/docker-helper';
|
||||
import * as Docker from 'dockerode';
|
||||
|
||||
describe('lib/docker-utils', () => {
|
||||
const docker = new Docker();
|
||||
|
||||
describe('getNetworkGateway', async () => {
|
||||
before(async () => {
|
||||
await docker.createNetwork({
|
||||
Name: 'supervisor0',
|
||||
Options: {
|
||||
'com.docker.network.bridge.name': 'supervisor0',
|
||||
},
|
||||
IPAM: {
|
||||
Driver: 'default',
|
||||
Config: [
|
||||
{
|
||||
Gateway: '10.0.105.1',
|
||||
Subnet: '10.0.105.0/16',
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
const allNetworks = await docker.listNetworks();
|
||||
|
||||
// Delete any remaining networks
|
||||
await Promise.all(
|
||||
allNetworks
|
||||
.filter(({ Name }) => !['bridge', 'host', 'none'].includes(Name)) // exclude docker default network from the cleanup
|
||||
.map(({ Name }) => docker.getNetwork(Name).remove()),
|
||||
);
|
||||
});
|
||||
|
||||
// test using existing data...
|
||||
it('should return the correct gateway address for supervisor0', async () => {
|
||||
const gateway = await dockerUtils.getNetworkGateway('supervisor0');
|
||||
expect(gateway).to.equal('10.0.105.1');
|
||||
});
|
||||
|
||||
it('should return the correct gateway address for host', async () => {
|
||||
const gateway = await dockerUtils.getNetworkGateway('host');
|
||||
expect(gateway).to.equal('127.0.0.1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getImageEnv', () => {
|
||||
before(async () => {
|
||||
await createDockerImage('test-image', ['io.balena.testing=1'], docker, [
|
||||
'ENV TEST_VAR_1=1234',
|
||||
'ENV TEST_VAR_2=5678',
|
||||
]);
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await docker.pruneImages({ filters: { dangling: { false: true } } });
|
||||
});
|
||||
|
||||
// test using existing data...
|
||||
it('should return the correct image environment', async () => {
|
||||
const obj = await dockerUtils.getImageEnv('test-image');
|
||||
expect(obj).to.have.property('TEST_VAR_1').equal('1234');
|
||||
expect(obj).to.have.property('TEST_VAR_2').equal('5678');
|
||||
});
|
||||
});
|
||||
|
||||
describe('isV2DeltaImage', () => {
|
||||
it('should correctly detect a V2 delta', async () => {
|
||||
// INFO: we still use the stub here. V2 deltas should eventually go away and there is no
|
||||
// really easy way to create a real image that simulates a v2 delta.
|
||||
const imageStub = stub(dockerUtils.docker, 'getImage').returns({
|
||||
inspect: () => {
|
||||
return Promise.resolve({
|
||||
Id: 'sha256:34ec91fe6e08cb0f867bbc069c5f499d39297eb8e874bb8ce9707537d983bcbc',
|
||||
RepoTags: [],
|
||||
RepoDigests: [],
|
||||
Parent: '',
|
||||
Comment: '',
|
||||
Created: '2019-12-05T10:20:51.516Z',
|
||||
Container: '',
|
||||
ContainerConfig: {
|
||||
Hostname: '',
|
||||
Domainname: '',
|
||||
User: '',
|
||||
AttachStdin: false,
|
||||
AttachStdout: false,
|
||||
AttachStderr: false,
|
||||
Tty: false,
|
||||
OpenStdin: false,
|
||||
StdinOnce: false,
|
||||
Env: null,
|
||||
Cmd: null,
|
||||
Image: '',
|
||||
Volumes: null,
|
||||
WorkingDir: '',
|
||||
Entrypoint: null,
|
||||
OnBuild: null,
|
||||
Labels: null,
|
||||
},
|
||||
DockerVersion: '',
|
||||
Author: '',
|
||||
Config: {
|
||||
Hostname: '7675a23f4fdc',
|
||||
Domainname: '',
|
||||
User: '',
|
||||
AttachStdin: false,
|
||||
AttachStdout: false,
|
||||
AttachStderr: false,
|
||||
Tty: false,
|
||||
OpenStdin: false,
|
||||
StdinOnce: false,
|
||||
Env: [
|
||||
'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
|
||||
'TINI_VERSION=0.14.0',
|
||||
'LC_ALL=C.UTF-8',
|
||||
'DEBIAN_FRONTEND=noninteractive',
|
||||
'UDEV=on',
|
||||
'container=docker',
|
||||
'test=123',
|
||||
],
|
||||
Cmd: [
|
||||
'/bin/sh',
|
||||
'-c',
|
||||
"while true; do echo 'hello'; sleep 10; done;",
|
||||
],
|
||||
ArgsEscaped: true,
|
||||
Image:
|
||||
'sha256:b24946093df7157727b20934d11a7287359d8de42d8a80030f51f46a73d645ec',
|
||||
Volumes: {
|
||||
'/sys/fs/cgroup': {},
|
||||
},
|
||||
WorkingDir: '',
|
||||
Entrypoint: ['/usr/bin/entry.sh'],
|
||||
OnBuild: [],
|
||||
Labels: {
|
||||
'io.resin.architecture': 'amd64',
|
||||
'io.resin.device-type': 'intel-nuc',
|
||||
},
|
||||
StopSignal: '37',
|
||||
},
|
||||
Architecture: '',
|
||||
Os: 'linux',
|
||||
Size: 17,
|
||||
VirtualSize: 17,
|
||||
GraphDriver: {
|
||||
Data: null,
|
||||
Name: 'aufs',
|
||||
},
|
||||
RootFS: {
|
||||
Type: 'layers',
|
||||
Layers: [
|
||||
'sha256:c6e6cd4f95ef00e62f5c9df5798393470c991ca0148cb1e434b28101ed4219d3',
|
||||
],
|
||||
},
|
||||
Metadata: {
|
||||
LastTagTime: '0001-01-01T00:00:00Z',
|
||||
},
|
||||
});
|
||||
},
|
||||
} as any);
|
||||
|
||||
expect(await dockerUtils.isV2DeltaImage('test')).to.be.true;
|
||||
expect(imageStub.callCount).to.equal(1);
|
||||
imageStub.restore();
|
||||
});
|
||||
});
|
||||
});
|
@ -1,103 +0,0 @@
|
||||
import { expect } from 'chai';
|
||||
import { stub } from 'sinon';
|
||||
|
||||
import * as dockerUtils from '~/lib/docker-utils';
|
||||
|
||||
describe('Deltas', () => {
|
||||
it('should correctly detect a V2 delta', async () => {
|
||||
const imageStub = stub(dockerUtils.docker, 'getImage').returns({
|
||||
inspect: () => {
|
||||
return Promise.resolve({
|
||||
Id: 'sha256:34ec91fe6e08cb0f867bbc069c5f499d39297eb8e874bb8ce9707537d983bcbc',
|
||||
RepoTags: [],
|
||||
RepoDigests: [],
|
||||
Parent: '',
|
||||
Comment: '',
|
||||
Created: '2019-12-05T10:20:51.516Z',
|
||||
Container: '',
|
||||
ContainerConfig: {
|
||||
Hostname: '',
|
||||
Domainname: '',
|
||||
User: '',
|
||||
AttachStdin: false,
|
||||
AttachStdout: false,
|
||||
AttachStderr: false,
|
||||
Tty: false,
|
||||
OpenStdin: false,
|
||||
StdinOnce: false,
|
||||
Env: null,
|
||||
Cmd: null,
|
||||
Image: '',
|
||||
Volumes: null,
|
||||
WorkingDir: '',
|
||||
Entrypoint: null,
|
||||
OnBuild: null,
|
||||
Labels: null,
|
||||
},
|
||||
DockerVersion: '',
|
||||
Author: '',
|
||||
Config: {
|
||||
Hostname: '7675a23f4fdc',
|
||||
Domainname: '',
|
||||
User: '',
|
||||
AttachStdin: false,
|
||||
AttachStdout: false,
|
||||
AttachStderr: false,
|
||||
Tty: false,
|
||||
OpenStdin: false,
|
||||
StdinOnce: false,
|
||||
Env: [
|
||||
'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
|
||||
'TINI_VERSION=0.14.0',
|
||||
'LC_ALL=C.UTF-8',
|
||||
'DEBIAN_FRONTEND=noninteractive',
|
||||
'UDEV=on',
|
||||
'container=docker',
|
||||
'test=123',
|
||||
],
|
||||
Cmd: [
|
||||
'/bin/sh',
|
||||
'-c',
|
||||
"while true; do echo 'hello'; sleep 10; done;",
|
||||
],
|
||||
ArgsEscaped: true,
|
||||
Image:
|
||||
'sha256:b24946093df7157727b20934d11a7287359d8de42d8a80030f51f46a73d645ec',
|
||||
Volumes: {
|
||||
'/sys/fs/cgroup': {},
|
||||
},
|
||||
WorkingDir: '',
|
||||
Entrypoint: ['/usr/bin/entry.sh'],
|
||||
OnBuild: [],
|
||||
Labels: {
|
||||
'io.resin.architecture': 'amd64',
|
||||
'io.resin.device-type': 'intel-nuc',
|
||||
},
|
||||
StopSignal: '37',
|
||||
},
|
||||
Architecture: '',
|
||||
Os: 'linux',
|
||||
Size: 17,
|
||||
VirtualSize: 17,
|
||||
GraphDriver: {
|
||||
Data: null,
|
||||
Name: 'aufs',
|
||||
},
|
||||
RootFS: {
|
||||
Type: 'layers',
|
||||
Layers: [
|
||||
'sha256:c6e6cd4f95ef00e62f5c9df5798393470c991ca0148cb1e434b28101ed4219d3',
|
||||
],
|
||||
},
|
||||
Metadata: {
|
||||
LastTagTime: '0001-01-01T00:00:00Z',
|
||||
},
|
||||
});
|
||||
},
|
||||
} as any);
|
||||
|
||||
expect(await dockerUtils.isV2DeltaImage('test')).to.be.true;
|
||||
expect(imageStub.callCount).to.equal(1);
|
||||
imageStub.restore();
|
||||
});
|
||||
});
|
@ -1,56 +0,0 @@
|
||||
import { expect } from 'chai';
|
||||
import { testWithData } from '~/test-lib/mocked-dockerode';
|
||||
|
||||
import * as dockerUtils from '~/lib/docker-utils';
|
||||
|
||||
describe('Docker Utils', () => {
|
||||
describe('Supervisor Address', () => {
|
||||
// setup some fake data...
|
||||
const networks = {
|
||||
supervisor0: {
|
||||
IPAM: {
|
||||
Config: [
|
||||
{
|
||||
Gateway: '10.0.105.1',
|
||||
Subnet: '10.0.105.0/16',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
// test using existing data...
|
||||
it('should return the correct gateway address for supervisor0', async () => {
|
||||
await testWithData({ networks }, async () => {
|
||||
const gateway = await dockerUtils.getNetworkGateway('supervisor0');
|
||||
expect(gateway).to.equal('10.0.105.1');
|
||||
});
|
||||
});
|
||||
|
||||
it('should return the correct gateway address for host', async () => {
|
||||
await testWithData({ networks }, async () => {
|
||||
const gateway = await dockerUtils.getNetworkGateway('host');
|
||||
expect(gateway).to.equal('127.0.0.1');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Image Environment', () => {
|
||||
const images = {
|
||||
['test-image']: {
|
||||
Config: {
|
||||
Env: ['TEST_VAR_1=1234', 'TEST_VAR_2=5678'],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
// test using existing data...
|
||||
it('should return the correct image environment', async () => {
|
||||
await testWithData({ images }, async () => {
|
||||
const obj = await dockerUtils.getImageEnv('test-image');
|
||||
expect(obj).to.have.property('TEST_VAR_1').equal('1234');
|
||||
expect(obj).to.have.property('TEST_VAR_2').equal('5678');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
@ -8,11 +8,15 @@ export async function createDockerImage(
|
||||
name: string,
|
||||
labels: [string, ...string[]],
|
||||
docker = new Docker(),
|
||||
extra = [] as string[], // Additional instructions to add to the dockerfile
|
||||
): Promise<string> {
|
||||
const pack = tar.pack(); // pack is a streams2 stream
|
||||
pack.entry(
|
||||
{ name: 'Dockerfile' },
|
||||
['FROM scratch'].concat(labels.map((l) => `LABEL ${l}`)).join('\n'),
|
||||
['FROM scratch']
|
||||
.concat(labels.map((l) => `LABEL ${l}`))
|
||||
.concat(extra)
|
||||
.join('\n'),
|
||||
(err) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
|
@ -6,7 +6,7 @@ import { NetworkInspectInfo } from 'dockerode';
|
||||
|
||||
import { log } from '~/lib/supervisor-console';
|
||||
|
||||
describe('compose/network: unit tests', () => {
|
||||
describe('compose/network', () => {
|
||||
describe('creating a network from a compose object', () => {
|
||||
it('creates a default network configuration if no config is given', () => {
|
||||
const network = Network.fromComposeObject(
|
||||
|
Loading…
x
Reference in New Issue
Block a user