Skip to content

Commit

Permalink
Merge pull request #690 from ministryofjustice/check-master-deployed-…
Browse files Browse the repository at this point in the history
…before-running-e2e

build: Wait for master to be deployed before running e2e tests
  • Loading branch information
solidgoldpig committed Jul 23, 2020
2 parents ba14c1c + 7192ee7 commit d722f46
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 32 deletions.
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,9 @@ jobs:
--label build.git.branch=${CIRCLE_BRANCH} \
--label build.date=${BUILD_DATE} \
--build-arg APP_BUILD_DATE=${BUILD_DATE} \
--build-arg APP_BUILD_TAG=${CIRCLE_BRANCH} \
--build-arg APP_BUILD_TAG=${CIRCLE_BUILD_NUM} \
--build-arg APP_GIT_COMMIT=${CIRCLE_SHA1} \
--build-arg APP_BUILD_BRANCH=${CIRCLE_BRANCH} \
-t app .
- run:
name: Login to ECR
Expand Down
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ ENV APP_BUILD_TAG ${APP_BUILD_TAG}
ARG APP_GIT_COMMIT
ENV APP_GIT_COMMIT ${APP_GIT_COMMIT}

ARG APP_BUILD_BRANCH
ENV APP_BUILD_BRANCH ${APP_BUILD_BRANCH}

COPY --chown=node:node start.js .
COPY --chown=node:node server.js .
COPY --chown=node:node locales locales
Expand Down
25 changes: 19 additions & 6 deletions app/healthcheck/controllers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
const { some } = require('lodash')

const { BUILD_DATE, BUILD_BRANCH, GIT_SHA } = require('../../config')
const {
API,
APP_BUILD_DATE,
APP_BUILD_TAG,
APP_BUILD_BRANCH,
APP_GIT_SHA,
} = require('../../config')
const { version } = require('../../package.json')

// https://github.com/ministryofjustice/ping.json
const buildDetails = {
build_date: APP_BUILD_DATE,
build_tag: APP_BUILD_TAG,
commit_id: APP_GIT_SHA,
version_number: version,
branch: APP_BUILD_BRANCH,
api_version: API.VERSION,
}

module.exports = {
get: (req, res) => {
const { dependencies } = res
Expand All @@ -12,15 +28,12 @@ module.exports = {
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate')
res.status(errors ? 503 : 200).json({
status,
version,
dependencies,
buildDate: BUILD_DATE,
buildTag: BUILD_BRANCH,
gitSha: GIT_SHA,
...buildDetails,
})
},
ping: (req, res) => {
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate')
res.status(200).send('OK')
res.status(200).json(buildDetails)
},
}
43 changes: 29 additions & 14 deletions app/healthcheck/controllers.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const proxyquire = require('proxyquire').noCallThru()

const mockConfig = {
BUILD_DATE: '2019-10-10',
BUILD_BRANCH: 'master',
GIT_SHA: 'gbe34155ae5edfade5107bd5629d0c159dc37d19',
API: {
VERSION: 7,
},
APP_BUILD_DATE: '2019-10-10',
APP_BUILD_TAG: '5226',
APP_BUILD_BRANCH: 'master',
APP_GIT_SHA: 'gbe34155ae5edfade5107bd5629d0c159dc37d19',
}
const mockManifest = {
version: '1.0.0',
version_number: '1.0.0',
}
const controllers = proxyquire('./controllers', {
'../../config': mockConfig,
Expand Down Expand Up @@ -60,10 +64,12 @@ describe('Healthcheck controllers', function () {
it('should render JSON', function () {
expect(res.json.args[0][0]).to.deep.equal({
status: 'OK',
version: mockManifest.version,
buildDate: mockConfig.BUILD_DATE,
buildTag: mockConfig.BUILD_BRANCH,
gitSha: mockConfig.GIT_SHA,
api_version: 7,
version_number: mockManifest.version,
build_date: mockConfig.APP_BUILD_DATE,
build_tag: mockConfig.APP_BUILD_TAG,
commit_id: mockConfig.APP_GIT_SHA,
branch: mockConfig.APP_BUILD_BRANCH,
dependencies: res.dependencies,
})
})
Expand Down Expand Up @@ -96,10 +102,12 @@ describe('Healthcheck controllers', function () {
it('should render JSON', function () {
expect(res.json.args[0][0]).to.deep.equal({
status: 'Service unavailable',
version: mockManifest.version,
buildDate: mockConfig.BUILD_DATE,
buildTag: mockConfig.BUILD_BRANCH,
gitSha: mockConfig.GIT_SHA,
api_version: 7,
version_number: mockManifest.version,
build_date: mockConfig.APP_BUILD_DATE,
build_tag: mockConfig.APP_BUILD_TAG,
commit_id: mockConfig.APP_GIT_SHA,
branch: mockConfig.APP_BUILD_BRANCH,
dependencies: res.dependencies,
})
})
Expand All @@ -113,7 +121,7 @@ describe('Healthcheck controllers', function () {
res = {
setHeader: sinon.stub(),
status: sinon.stub().returnsThis(),
send: sinon.stub(),
json: sinon.stub(),
}
controllers.ping({}, res)
})
Expand All @@ -130,7 +138,14 @@ describe('Healthcheck controllers', function () {
})

it('should render JSON', function () {
expect(res.send).to.be.calledOnceWithExactly('OK')
expect(res.json).to.be.calledOnceWithExactly({
api_version: 7,
version_number: mockManifest.version,
build_date: mockConfig.APP_BUILD_DATE,
build_tag: mockConfig.APP_BUILD_TAG,
commit_id: mockConfig.APP_GIT_SHA,
branch: mockConfig.APP_BUILD_BRANCH,
})
})
})
})
7 changes: 4 additions & 3 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ module.exports = {
NO_CACHE: process.env.CACHE_ASSETS ? false : IS_DEV,
FEEDBACK_URL: process.env.FEEDBACK_URL,
SUPPORT_EMAIL: process.env.SUPPORT_EMAIL,
BUILD_DATE: process.env.APP_BUILD_DATE,
BUILD_BRANCH: process.env.APP_BUILD_TAG,
GIT_SHA: process.env.APP_GIT_COMMIT,
APP_BUILD_DATE: process.env.APP_BUILD_DATE,
APP_BUILD_BRANCH: process.env.APP_BUILD_BRANCH,
APP_BUILD_TAG: process.env.APP_BUILD_TAG,
APP_GIT_SHA: process.env.APP_GIT_COMMIT,
API: {
VERSION: Number(API_VERSION),
BASE_URL: API_BASE_URL + process.env.API_PATH,
Expand Down
56 changes: 48 additions & 8 deletions test/e2e/parallel-runner.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
/* eslint-disable no-template-curly-in-string */
/* eslint-disable no-process-env */
const axios = require('axios')
const concurrently = require('concurrently')
const glob = require('glob')
const yargs = require('yargs')

const { E2E_MAX_PROCESSES, E2E_SKIP, E2E_VIDEO, E2E_FAIL_FAST } = process.env
const {
E2E_MAX_PROCESSES,
E2E_SKIP,
E2E_VIDEO,
E2E_FAIL_FAST,
E2E_BASE_URL,
} = process.env

const args = yargs
.usage(
Expand Down Expand Up @@ -189,12 +196,45 @@ if (args.n) {
process.exit()
}

concurrently(testcafeRuns, {
maxProcesses,
killOthers,
}).then(
() => {},
() => {
const checkCircleSha = async () => {
const { CIRCLECI, CIRCLE_BRANCH, CIRCLE_SHA1 } = process.env

if (E2E_BASE_URL && CIRCLECI && CIRCLE_BRANCH === 'master') {
const sleep = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms))

let checkPing = true
const pingUrl = `${E2E_BASE_URL}/healthcheck/ping`
process.stdout.write(
`Checking ${pingUrl} for correct commit sha ${CIRCLE_SHA1}`
)

while (checkPing) {
try {
const { data } = await axios.get(pingUrl)

if (data.commit_id === CIRCLE_SHA1) {
checkPing = false
process.stdout.write(`${E2E_BASE_URL} returned correct commit sha`)
}
} catch (error) {
process.stdout.write('.')
await sleep(2000)
}
}
}
}

const runTests = async () => {
await checkCircleSha()

try {
await concurrently(testcafeRuns, {
maxProcesses,
killOthers,
})
} catch (e) {
process.exit(1)
}
)
}

runTests()

0 comments on commit d722f46

Please sign in to comment.