Skip to content

Commit

Permalink
Add basic test coverage for proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Jurre Stender committed Aug 12, 2021
1 parent a70f3f2 commit 8959322
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
3 changes: 3 additions & 0 deletions __tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export const removeDanglingUpdaterContainers = async (): Promise<void> => {
}
}
}

await docker.pruneNetworks()
await docker.pruneContainers()
}

export const runFakeDependabotApi = async (port: number): Promise<Function> => {
Expand Down
63 changes: 63 additions & 0 deletions __tests__/proxy-integration.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})
})
8 changes: 7 additions & 1 deletion src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type Proxy = {
networkName: string
url: string
cert: string
shutdown: () => Promise<void>
}

export class ProxyBuilder {
Expand Down Expand Up @@ -91,7 +92,12 @@ export class ProxyBuilder {
network,
networkName,
url,
cert
cert,
shutdown: async () => {
await container.stop()
await container.remove()
await network.remove()
}
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down

0 comments on commit 8959322

Please sign in to comment.