2020-07-21 15:25:47 +00:00
|
|
|
process.env.DOCKER_HOST = 'unix:///your/dockerode/mocks/are/not/working';
|
|
|
|
|
2020-07-22 09:53:50 +00:00
|
|
|
import * as dockerode from 'dockerode';
|
2020-08-13 12:25:39 +00:00
|
|
|
import { Stream } from 'stream';
|
|
|
|
import _ = require('lodash');
|
|
|
|
import { TypedError } from 'typed-error';
|
|
|
|
|
|
|
|
export class NotFoundError extends TypedError {
|
|
|
|
public statusCode: number;
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.statusCode = 404;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const overrides: Dictionary<(...args: any[]) => Resolvable<any>> = {};
|
|
|
|
|
2020-10-30 21:38:19 +00:00
|
|
|
interface Action {
|
|
|
|
name: string;
|
|
|
|
parameters: Dictionary<any>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export let actions: Action[] = [];
|
|
|
|
|
|
|
|
export function resetHistory() {
|
|
|
|
actions = [];
|
|
|
|
}
|
|
|
|
|
2021-03-03 20:30:14 +00:00
|
|
|
/**
|
|
|
|
* Tracks actions performed on a mocked dockerode instance
|
|
|
|
* @param name action called
|
|
|
|
* @param parameters data passed
|
|
|
|
*/
|
|
|
|
function addAction(name: string, parameters: Dictionary<any> = {}) {
|
2020-10-30 21:38:19 +00:00
|
|
|
actions.push({
|
|
|
|
name,
|
|
|
|
parameters,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-13 12:25:39 +00:00
|
|
|
type DockerodeFunction = keyof dockerode;
|
|
|
|
for (const fn of Object.getOwnPropertyNames(dockerode.prototype)) {
|
|
|
|
if (
|
|
|
|
fn !== 'constructor' &&
|
|
|
|
typeof (dockerode.prototype as any)[fn] === 'function'
|
|
|
|
) {
|
|
|
|
(dockerode.prototype as any)[fn] = async function (...args: any[]) {
|
|
|
|
console.log(`🐳 Calling ${fn}...`);
|
|
|
|
if (overrides[fn] != null) {
|
|
|
|
return overrides[fn](args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return promise */
|
|
|
|
return Promise.resolve([]);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// default overrides needed to startup...
|
|
|
|
registerOverride(
|
|
|
|
'getEvents',
|
|
|
|
async () =>
|
|
|
|
new Stream.Readable({
|
|
|
|
read: () => {
|
|
|
|
return _.noop();
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2021-03-03 20:30:14 +00:00
|
|
|
/**
|
|
|
|
* Used to add or modifying functions on the mocked dockerode
|
|
|
|
* @param name function name to override
|
|
|
|
* @param fn function to execute
|
|
|
|
*/
|
2020-08-13 12:25:39 +00:00
|
|
|
export function registerOverride<
|
|
|
|
T extends DockerodeFunction,
|
|
|
|
P extends Parameters<dockerode[T]>,
|
|
|
|
R extends ReturnType<dockerode[T]>
|
|
|
|
>(name: T, fn: (...args: P) => R) {
|
|
|
|
console.log(`Overriding ${name}...`);
|
|
|
|
overrides[name] = fn;
|
|
|
|
}
|
2020-07-22 09:53:50 +00:00
|
|
|
|
|
|
|
export interface TestData {
|
|
|
|
networks: Dictionary<any>;
|
|
|
|
images: Dictionary<any>;
|
2020-11-19 01:37:34 +00:00
|
|
|
containers: Dictionary<any>;
|
2021-03-03 20:30:14 +00:00
|
|
|
volumes: Dictionary<any>;
|
2020-07-22 09:53:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function createMockedDockerode(data: TestData) {
|
|
|
|
const mockedDockerode = dockerode.prototype;
|
2021-03-03 20:30:14 +00:00
|
|
|
|
|
|
|
mockedDockerode.listImages = async () => [];
|
|
|
|
|
|
|
|
mockedDockerode.listVolumes = async () => {
|
|
|
|
addAction('listVolumes');
|
|
|
|
return {
|
|
|
|
Volumes: data.volumes as dockerode.VolumeInspectInfo[],
|
|
|
|
Warnings: [],
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
mockedDockerode.getVolume = (name: string) => {
|
|
|
|
addAction('getVolume');
|
|
|
|
const picked = data.volumes.filter((v: Dictionary<any>) => v.Name === name);
|
|
|
|
if (picked.length !== 1) {
|
|
|
|
throw new NotFoundError();
|
|
|
|
}
|
|
|
|
const volume = picked[0];
|
|
|
|
return {
|
|
|
|
...volume,
|
|
|
|
inspect: async () => {
|
|
|
|
addAction('inspect');
|
|
|
|
// TODO fully implement volume inspect.
|
|
|
|
// This should return VolumeInspectInfo not Volume
|
|
|
|
return volume;
|
|
|
|
},
|
|
|
|
remove: async (options?: {}) => {
|
|
|
|
addAction('remove', options);
|
|
|
|
data.volumes = _.reject(data.volumes, { name: volume.name });
|
|
|
|
},
|
|
|
|
name: volume.name,
|
|
|
|
modem: {},
|
|
|
|
} as dockerode.Volume;
|
|
|
|
};
|
|
|
|
|
2020-11-26 06:11:47 +00:00
|
|
|
mockedDockerode.createContainer = async (
|
|
|
|
options: dockerode.ContainerCreateOptions,
|
|
|
|
) => {
|
|
|
|
addAction('createContainer', { options });
|
|
|
|
return {
|
|
|
|
start: async () => {
|
2021-03-03 20:30:14 +00:00
|
|
|
addAction('start');
|
2020-11-26 06:11:47 +00:00
|
|
|
},
|
|
|
|
} as dockerode.Container;
|
|
|
|
};
|
2021-03-03 20:30:14 +00:00
|
|
|
|
2020-11-19 01:37:34 +00:00
|
|
|
mockedDockerode.getContainer = (id: string) => {
|
|
|
|
addAction('getContainer', { id });
|
|
|
|
return {
|
2021-03-03 20:30:14 +00:00
|
|
|
inspect: async () => {
|
|
|
|
return data.containers.filter((c: Dictionary<any>) => c.id === id);
|
|
|
|
},
|
2020-11-19 01:37:34 +00:00
|
|
|
start: async () => {
|
2021-03-03 20:30:14 +00:00
|
|
|
addAction('start');
|
2020-11-19 01:37:34 +00:00
|
|
|
data.containers = data.containers.map((c: any) => {
|
|
|
|
if (c.containerId === id) {
|
|
|
|
c.status = 'Installing';
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
stop: async () => {
|
2021-03-03 20:30:14 +00:00
|
|
|
addAction('stop');
|
2020-11-19 01:37:34 +00:00
|
|
|
data.containers = data.containers.map((c: any) => {
|
|
|
|
if (c.containerId === id) {
|
|
|
|
c.status = 'Stopping';
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
remove: async () => {
|
2021-03-03 20:30:14 +00:00
|
|
|
addAction('remove');
|
2020-11-19 01:37:34 +00:00
|
|
|
data.containers = data.containers.map((c: any) => {
|
|
|
|
if (c.containerId === id) {
|
|
|
|
c.status = 'removing';
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
} as dockerode.Container;
|
|
|
|
};
|
2021-03-03 20:30:14 +00:00
|
|
|
|
2020-07-22 09:53:50 +00:00
|
|
|
mockedDockerode.getNetwork = (id: string) => {
|
2020-10-30 21:38:19 +00:00
|
|
|
addAction('getNetwork', { id });
|
2020-07-22 09:53:50 +00:00
|
|
|
return {
|
|
|
|
inspect: async () => {
|
2021-03-03 20:30:14 +00:00
|
|
|
addAction('inspect');
|
2020-07-22 09:53:50 +00:00
|
|
|
return data.networks[id];
|
|
|
|
},
|
|
|
|
} as dockerode.Network;
|
|
|
|
};
|
|
|
|
|
|
|
|
mockedDockerode.getImage = (name: string) => {
|
2020-10-30 21:38:19 +00:00
|
|
|
addAction('getImage', { name });
|
2020-07-22 09:53:50 +00:00
|
|
|
return {
|
|
|
|
inspect: async () => {
|
2021-03-03 20:30:14 +00:00
|
|
|
addAction('inspect');
|
2020-07-22 09:53:50 +00:00
|
|
|
return data.images[name];
|
|
|
|
},
|
2020-10-30 21:38:19 +00:00
|
|
|
remove: async () => {
|
2021-03-03 20:30:14 +00:00
|
|
|
addAction('remove');
|
2020-10-30 21:38:19 +00:00
|
|
|
data.images = _.reject(data.images, {
|
|
|
|
name,
|
|
|
|
});
|
|
|
|
},
|
2020-07-22 09:53:50 +00:00
|
|
|
} as dockerode.Image;
|
|
|
|
};
|
|
|
|
|
|
|
|
return mockedDockerode;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function testWithData(
|
|
|
|
data: Partial<TestData>,
|
|
|
|
test: () => Promise<any>,
|
|
|
|
) {
|
|
|
|
const mockedData: TestData = {
|
|
|
|
...{
|
2021-03-03 20:30:14 +00:00
|
|
|
networks: [],
|
|
|
|
images: [],
|
|
|
|
containers: [],
|
|
|
|
volumes: [],
|
2020-07-22 09:53:50 +00:00
|
|
|
},
|
|
|
|
...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;
|
|
|
|
}
|
|
|
|
}
|