Skip to content

Commit

Permalink
Merge pull request #37 from dependabot/jurre/extract-updater-image-bu…
Browse files Browse the repository at this point in the history
…ilding

Extract updater image building for easier testing
  • Loading branch information
Jurre authored and GitHub committed Oct 26, 2021
2 parents a4826a3 + e739a7a commit 5968d26
Show file tree
Hide file tree
Showing 8 changed files with 318 additions and 151 deletions.
4 changes: 4 additions & 0 deletions __tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,7 @@ export const eventFixturePath = (fixtureName: string): string => {
`${fixtureName}.json`
)
}

export const integration = process.env.SKIP_INTEGRATION_TESTS
? describe.skip
: describe
17 changes: 2 additions & 15 deletions __tests__/proxy-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import {Credential} 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'
import {integration, removeDanglingUpdaterContainers} from './helpers'
import {spawnSync} from 'child_process'
import fs from 'fs'
import path from 'path'

describe('ProxyBuilder', () => {
integration('ProxyBuilder', () => {
const docker = new Docker()
const jobId = 1
const credentials: Credential[] = [
Expand All @@ -23,10 +23,6 @@ describe('ProxyBuilder', () => {
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)
})

Expand All @@ -36,11 +32,6 @@ describe('ProxyBuilder', () => {

jest.setTimeout(20000)
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(jobId, credentials)
await proxy.container.start()

Expand Down Expand Up @@ -77,10 +68,6 @@ describe('ProxyBuilder', () => {

jest.setTimeout(20000)
it('copies in a custom root CA if configured', async () => {
if (process.env.SKIP_INTEGRATION_TESTS) {
return
}

// make a tmp dir at the repo root unless it already exists
const tmpDir = path.join(__dirname, '../tmp')
if (!fs.existsSync(tmpDir)) {
Expand Down
90 changes: 90 additions & 0 deletions __tests__/updater-builder-integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import {PROXY_IMAGE_NAME, UPDATER_IMAGE_NAME} from '../src/main'
import {ImageService} from '../src/image-service'
import {removeDanglingUpdaterContainers, integration} from './helpers'
import Docker from 'dockerode'
import {Credential, JobDetails} from '../src/api-client'
import {ProxyBuilder} from '../src/proxy'
import path from 'path'
import fs from 'fs'
import {JobParameters} from '../src/inputs'
import {UpdaterBuilder} from '../src/updater-builder'

integration('UpdaterBuilder', () => {
const docker = new Docker()
const credentials: Credential[] = [
{
type: 'git_source',
host: 'github.com',
username: 'x-access-token',
password: 'ghp_some_token'
}
]

const details: JobDetails = {
'allowed-updates': [],
id: '1',
'package-manager': 'npm_and_yarn'
}

const workingDirectory = path.join(
__dirname,
'..',
'tmp',
'./integration_working_directory'
)

beforeAll(async () => {
await ImageService.pull(PROXY_IMAGE_NAME)
await ImageService.pull(UPDATER_IMAGE_NAME)

fs.mkdirSync(workingDirectory)
})

afterEach(async () => {
await removeDanglingUpdaterContainers()
fs.rmdirSync(workingDirectory, {recursive: true})
})

it('createUpdaterContainer returns a container only connected to the internal network', async () => {
const outputPath = path.join(workingDirectory, 'output')
const repoPath = path.join(workingDirectory, 'repo')
fs.mkdirSync(outputPath)
fs.mkdirSync(repoPath)

const proxy = await new ProxyBuilder(docker, PROXY_IMAGE_NAME).run(
1,
credentials
)
await proxy.container.start()
const input = {job: details}
const params = new JobParameters(
1,
'job-token',
'cred-token',
'https://example.com',
'172.17.0.1',
workingDirectory
)
const container = await new UpdaterBuilder(
docker,
params,
input,
outputPath,
proxy,
repoPath,
UPDATER_IMAGE_NAME
).run('updater-image-test', 'fetch_files')

const containerInfo = await container.inspect()

const networkNames = Object.keys(containerInfo.NetworkSettings.Networks)
expect(networkNames).toEqual(['dependabot-job-1-internal-network'])

const network = docker.getNetwork(networkNames[0])
const networkInfo = await network.inspect()
expect(networkInfo.Internal).toBe(true)

await proxy.shutdown()
await container.remove()
})
})
23 changes: 6 additions & 17 deletions __tests__/updater-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import {JobParameters} from '../src/inputs'
import {UPDATER_IMAGE_NAME, PROXY_IMAGE_NAME} from '../src/main'
import {Updater} from '../src/updater'

import {removeDanglingUpdaterContainers, runFakeDependabotApi} from './helpers'
import {
integration,
removeDanglingUpdaterContainers,
runFakeDependabotApi
} from './helpers'

const FAKE_SERVER_PORT = 9000

describe('Updater', () => {
integration('Updater', () => {
let server: any

// Used from this action to get job details and credentials
Expand Down Expand Up @@ -40,11 +44,6 @@ describe('Updater', () => {
const apiClient = new ApiClient(client, params)

beforeAll(async () => {
// Skip the test when we haven't preloaded the updater image
if (process.env.SKIP_INTEGRATION_TESTS) {
return
}

await ImageService.pull(UPDATER_IMAGE_NAME)
await ImageService.pull(PROXY_IMAGE_NAME)

Expand All @@ -54,23 +53,13 @@ describe('Updater', () => {
})

afterEach(async () => {
// Skip the test when we haven't preloaded the updater image
if (process.env.SKIP_INTEGRATION_TESTS) {
return
}

server && server() // teardown server process
await removeDanglingUpdaterContainers()
fs.rmdirSync(workingDirectory, {recursive: true})
})

jest.setTimeout(120000)
it('should run the updater and create a pull request', async () => {
// Skip the test when we haven't preloaded the updater image
if (process.env.SKIP_INTEGRATION_TESTS) {
return
}

const details = await apiClient.getJobDetails()
const credentials = await apiClient.getCredentials()

Expand Down
156 changes: 108 additions & 48 deletions dist/main/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/main/index.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit 5968d26

Please sign in to comment.