diff --git a/__tests__/helpers.ts b/__tests__/helpers.ts index 4186e0e..6cbc29e 100644 --- a/__tests__/helpers.ts +++ b/__tests__/helpers.ts @@ -20,6 +20,9 @@ export const removeDanglingUpdaterContainers = async (): Promise => { } } } + + await docker.pruneNetworks() + await docker.pruneContainers() } export const runFakeDependabotApi = async (port: number): Promise => { diff --git a/__tests__/proxy-integration.test.ts b/__tests__/proxy-integration.test.ts new file mode 100644 index 0000000..3c673ae --- /dev/null +++ b/__tests__/proxy-integration.test.ts @@ -0,0 +1,63 @@ +import Docker from 'dockerode' +import {Credential, JobDetails, PackageManager} from '../src/api-client' +import {ImageService} from '../src/image-service' +import {PROXY_IMAGE_NAME} from '../src/main' +import {ProxyBuilder} from '../src/proxy' +import {removeDanglingUpdaterContainers} from './helpers' + +describe('ProxyBuilder', () => { + const docker = new Docker() + const details: JobDetails = { + id: '1', + 'allowed-updates': [ + { + 'dependency-type': 'all' + } + ], + 'package-manager': PackageManager.NpmAndYarn + } + const credentials: Credential[] = [ + { + type: 'git_source', + host: 'github.com', + username: 'x-access-token', + password: 'ghp_some_token' + } + ] + + const builder = new ProxyBuilder(docker, PROXY_IMAGE_NAME) + + beforeAll(async () => { + // Skip the test when we haven't preloaded the updater image + if (process.env.SKIP_INTEGRATION_TESTS) { + return + } + await ImageService.pull(PROXY_IMAGE_NAME) + }) + + afterEach(async () => { + await removeDanglingUpdaterContainers() + }) + + it('should create a proxy container with the right details', async () => { + // Skip the test when we haven't preloaded the updater image + if (process.env.SKIP_INTEGRATION_TESTS) { + return + } + + const proxy = await builder.run(details, credentials) + + expect(proxy.networkName).toBe('job-1-network') + expect(proxy.url).toMatch(/^http:\/\/1:.+job-1-proxy:1080$/) + + const containerInfo = await proxy.container.inspect() + expect(containerInfo.Name).toBe('/job-1-proxy') + expect(containerInfo.HostConfig.NetworkMode).toBe('job-1-network') + + const networkInfo = await proxy.network.inspect() + expect(networkInfo.Name).toBe('job-1-network') + expect(networkInfo.Internal).toBe(false) + + await proxy.shutdown() + }) +}) diff --git a/src/proxy.ts b/src/proxy.ts index b599bc0..bbf1ab7 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -48,6 +48,7 @@ export type Proxy = { networkName: string url: string cert: string + shutdown: () => Promise } export class ProxyBuilder { @@ -91,7 +92,12 @@ export class ProxyBuilder { network, networkName, url, - cert + cert, + shutdown: async () => { + await container.stop() + await container.remove() + await network.remove() + } } } diff --git a/src/updater.ts b/src/updater.ts index 1fe91b0..a7131a1 100644 --- a/src/updater.ts +++ b/src/updater.ts @@ -52,9 +52,8 @@ export class Updater { // TODO: report job runner_error? core.error(`Error ${e}`) } finally { - await proxy.container.stop() - await proxy.container.remove() - await proxy.network.remove() + await proxy.shutdown() + await this.docker.pruneNetworks() } } catch (e) { // TODO: report job runner_error?