Merge pull request #1403 from balena-io/fix-this-reference-docker-util

Fix docker-util using incorrect reference for function
This commit is contained in:
bulldozer-balena[bot] 2020-07-22 13:09:54 +00:00 committed by GitHub
commit 7fcf26e161
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 115 additions and 3 deletions

View File

@ -188,7 +188,7 @@ export async function fetchDeltaWithProgress(
} catch (e) {
if (e instanceof OutOfSyncError) {
logFn('Falling back to regular pull due to delta out of sync error');
return await this.fetchImageWithProgress(imgDest, deltaOpts, onProgress);
return await fetchImageWithProgress(imgDest, deltaOpts, onProgress);
} else {
logFn(`Delta failed with ${e}`);
throw e;
@ -219,7 +219,7 @@ export async function fetchImageWithProgress(
}
export async function getImageEnv(id: string): Promise<EnvVarObject> {
const inspect = await this.getImage(id).inspect();
const inspect = await docker.getImage(id).inspect();
try {
return envArrayToObject(_.get(inspect, ['Config', 'Env'], []));
@ -234,7 +234,7 @@ export async function getNetworkGateway(networkName: string): Promise<string> {
return '127.0.0.1';
}
const network = await this.getNetwork(networkName).inspect();
const network = await docker.getNetwork(networkName).inspect();
const config = _.get(network, ['IPAM', 'Config', '0']);
if (config != null) {
if (config.Gateway != null) {

View File

@ -0,0 +1,56 @@
import { expect } from 'chai';
import { testWithData } from './lib/mocked-dockerode';
import * as dockerUtils from '../src/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');
});
});
});
});

View File

@ -0,0 +1,56 @@
import * as dockerode from 'dockerode';
export interface TestData {
networks: Dictionary<any>;
images: Dictionary<any>;
}
function createMockedDockerode(data: TestData) {
const mockedDockerode = dockerode.prototype;
mockedDockerode.getNetwork = (id: string) => {
return {
inspect: async () => {
return data.networks[id];
},
} as dockerode.Network;
};
mockedDockerode.getImage = (name: string) => {
return {
inspect: async () => {
return data.images[name];
},
} as dockerode.Image;
};
return mockedDockerode;
}
export async function testWithData(
data: Partial<TestData>,
test: () => Promise<any>,
) {
const mockedData: TestData = {
...{
networks: {},
images: {},
containers: {},
},
...data,
};
// grab the original prototype...
const basePrototype = dockerode.prototype;
// @ts-expect-error setting a RO property
dockerode.prototype = createMockedDockerode(mockedData);
try {
// run the test...
await test();
} finally {
// reset the original prototype...
// @ts-expect-error setting a RO property
dockerode.prototype = basePrototype;
}
}