Skip to content

Commit

Permalink
Merge pull request #59 from github/feelepxyz/extract-image-service
Browse files Browse the repository at this point in the history
Extract image pull to a separate service class
  • Loading branch information
Philip Harrison authored and GitHub committed Jul 26, 2021
2 parents e3da62e + f6182de commit 56396c5
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 130 deletions.
7 changes: 7 additions & 0 deletions __tests__/image-service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {ImageService} from '../src/image-service'

describe('ImageService', () => {
test('pulls the image from docker hub', async () => {
await ImageService.pull('hello-world')
})
})
40 changes: 24 additions & 16 deletions __tests__/updater-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import Docker from 'dockerode'
import fs from 'fs'
import path from 'path'
import {Updater} from '../src/updater'
import {UPDATER_IMAGE_NAME} from '../src/main'
import {ImageService} from '../src/image-service'

describe('Updater', () => {
const docker = new Docker()
// To run the js-code itself against API:
// const params = {
// jobID: 1,
Expand All @@ -14,7 +15,7 @@ describe('Updater', () => {
// }
// const client = axios.create({baseURL: params.dependabotAPI})
// const api = new DependabotAPI(client, params)
// const updater = new Updater(docker, api)
// const updater = new Updater(UPDATER_IMAGE_NAME, api)

// This stubs out API calls from JS, but will run the updater against an API
// running on the specified API endpoint.
Expand All @@ -28,26 +29,33 @@ describe('Updater', () => {
dependabotAPIURL: 'http://host.docker.internal:3001'
}
}
const updater = new Updater(docker, mockAPIClient)
const updater = new Updater(UPDATER_IMAGE_NAME, mockAPIClient)

beforeAll(() => {
updater.pullImage()
beforeAll(async () => {
if (process.env.CI) {
// Skip this test on CI, as it takes too long to download the image
return
}
await ImageService.pull(UPDATER_IMAGE_NAME)
})

afterEach(() => {
docker.listContainers(function (err, containers) {
if (!containers) return
afterEach(async () => {
const docker = new Docker()
const containers = (await docker.listContainers()) || []

for (const container of containers) {
if (
container.Image.includes(
'docker.pkg.github.com/dependabot/dependabot-updater'
)
) {
docker.getContainer(container.Id).remove()
for (const container of containers) {
if (
container.Image.includes(
'docker.pkg.github.com/dependabot/dependabot-updater'
)
) {
try {
await docker.getContainer(container.Id).remove({v: true, force: true})
} catch (e) {
// ignore
}
}
})
}
})

jest.setTimeout(20000)
Expand Down
5 changes: 2 additions & 3 deletions __tests__/updater.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Docker from 'dockerode'
import {UPDATER_IMAGE_NAME} from '../src/main'
import {Updater} from '../src/updater'

describe('Updater', () => {
const docker = new Docker()
const mockAPIClient: any = {
getJobDetails: jest.fn(),
getCredentials: jest.fn(),
Expand All @@ -13,7 +12,7 @@ describe('Updater', () => {
dependabotAPIURL: 'http://host.docker.internal:3001'
}
}
const updater = new Updater(docker, mockAPIClient)
const updater = new Updater(UPDATER_IMAGE_NAME, mockAPIClient)

it('should fetch job details', async () => {
mockAPIClient.getJobDetails.mockImplementation(() => {
Expand Down
152 changes: 94 additions & 58 deletions dist/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/index.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit 56396c5

Please sign in to comment.