Skip to content

Commit b5904f9

Browse files
authored
Improve integration tests for pushing Jenkins statuses (#222)
* jenkins: refactor push-jenkins tests to prove they actually fail The previous approach of testing if network requests had been sent from the bot, via nock scopes, didn't test more then one network request. That's because we tested all scoped in a one-liner like this; ```js prCommitsScope.done() && scope.done() ``` It turns out that the above will not invoke the second `scope.done()`, meaning only the first nock scope (read: outgoing network request) was asserted. By going away from that one-liner and invoking one by one on each line, tests starts to explode. A fix to the bot's source will come in the next commit. * jenkins: wait until status has been pushed to GitHub before responding These changes are primarily done so we can get more reliable integration tests. Previously the incoming requests was responded to, before we knew whether or not the Jenkins status has been successfully pushed to GitHub. That makes it challenging to know when we can assert what we want to verify in integration tests, as we don't know when the entire operation has finished. Although the previous approach did what it should in practise when run in production, it should be just as important to have reliable tests that we can rely on when doing changes going forward. * jenkins: move HTTP assertions from .tearDown() alongside response test As it's way more intuitive to have those outgoing HTTP request assertions alongside other assertions, than as part of the test tear down process.
1 parent a0b8404 commit b5904f9

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

lib/push-jenkins-update.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const url = require('url')
44

55
const githubClient = require('./github-client')
66

7-
function pushStarted (options, build) {
7+
function pushStarted (options, build, cb) {
88
const pr = findPrInRef(build.ref)
99

1010
// create unique logger which is easily traceable for every subsequent log statement
@@ -15,7 +15,9 @@ function pushStarted (options, build) {
1515

1616
findLatestCommitInPr(optsWithPr, (err, latestCommit) => {
1717
if (err) {
18-
return logger.error(err, 'Got error when retrieving GitHub commits for PR')
18+
logger.error(err, 'Got error when retrieving GitHub commits for PR')
19+
cb(err)
20+
return
1921
}
2022

2123
const statusOpts = Object.assign({
@@ -26,11 +28,11 @@ function pushStarted (options, build) {
2628
message: build.message || 'running tests'
2729
}, options)
2830

29-
createGhStatus(statusOpts, logger)
31+
createGhStatus(statusOpts, logger, cb)
3032
})
3133
}
3234

33-
function pushEnded (options, build) {
35+
function pushEnded (options, build, cb) {
3436
const pr = findPrInRef(build.ref)
3537

3638
// create unique logger which is easily traceable for every subsequent log statement
@@ -41,7 +43,9 @@ function pushEnded (options, build) {
4143

4244
findLatestCommitInPr(optsWithPr, (err, latestCommit) => {
4345
if (err) {
44-
return logger.error(err, 'Got error when retrieving GitHub commits for PR')
46+
logger.error(err, 'Got error when retrieving GitHub commits for PR')
47+
cb(err)
48+
return
4549
}
4650

4751
const statusOpts = Object.assign({
@@ -52,7 +56,7 @@ function pushEnded (options, build) {
5256
message: build.message || 'all tests passed'
5357
}, options)
5458

55-
createGhStatus(statusOpts, logger)
59+
createGhStatus(statusOpts, logger, cb)
5660
})
5761
}
5862

@@ -89,7 +93,7 @@ function findLatestCommitInPr (options, cb, pageNumber = 1) {
8993
})
9094
}
9195

92-
function createGhStatus (options, logger) {
96+
function createGhStatus (options, logger, cb) {
9397
githubClient.repos.createStatus({
9498
owner: options.owner,
9599
repo: options.repo,
@@ -100,9 +104,12 @@ function createGhStatus (options, logger) {
100104
description: options.message
101105
}, (err, res) => {
102106
if (err) {
103-
return logger.error(err, 'Error while updating Jenkins / GitHub PR status')
107+
logger.error(err, 'Error while updating Jenkins / GitHub PR status')
108+
cb(err)
109+
return
104110
}
105111
logger.info('Jenkins / Github PR status updated')
112+
cb(null)
106113
})
107114
}
108115

scripts/jenkins-status.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ module.exports = function (app) {
4646
owner: 'nodejs',
4747
repo,
4848
logger: req.log
49-
}, req.body)
50-
51-
res.status(201).end()
49+
}, req.body, (err) => {
50+
const statusCode = err !== null ? 500 : 201
51+
res.status(statusCode).end()
52+
})
5253
})
5354

5455
app.post('/:repo/jenkins/end', (req, res) => {
@@ -75,8 +76,9 @@ module.exports = function (app) {
7576
owner: 'nodejs',
7677
repo,
7778
logger: req.log
78-
}, req.body)
79-
80-
res.status(201).end()
79+
}, req.body, (err) => {
80+
const statusCode = err !== null ? 500 : 201
81+
res.status(statusCode).end()
82+
})
8183
})
8284
}

test/integration/push-jenkins-update.test.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ tap.test('Sends POST requests to https://api.github.com/repos/nodejs/node/status
1919
.reply(201)
2020

2121
t.plan(1)
22-
t.tearDown(() => prCommitsScope.done() && scope.done())
2322

2423
supertest(app)
2524
.post('/node/jenkins/start')
2625
.send(jenkinsPayload)
2726
.expect(201)
2827
.end((err, res) => {
28+
prCommitsScope.done()
29+
scope.done()
2930
t.equal(err, null)
3031
})
3132
})
@@ -40,13 +41,14 @@ tap.test('Allows repository name to be provided with URL parameter when pushing
4041
.reply(201)
4142

4243
t.plan(1)
43-
t.tearDown(() => prCommitsScope.done() && scope.done())
4444

4545
supertest(app)
4646
.post('/citgm/jenkins/start')
4747
.send(jenkinsPayload)
4848
.expect(201)
4949
.end((err, res) => {
50+
prCommitsScope.done()
51+
scope.done()
5052
t.equal(err, null)
5153
})
5254
})
@@ -61,13 +63,14 @@ tap.test('Allows repository name to be provided with URL parameter when pushing
6163
.reply(201)
6264

6365
t.plan(1)
64-
t.tearDown(() => prCommitsScope.done() && scope.done())
6566

6667
supertest(app)
6768
.post('/citgm/jenkins/end')
6869
.send(jenkinsPayload)
6970
.expect(201)
7071
.end((err, res) => {
72+
prCommitsScope.done()
73+
scope.done()
7174
t.equal(err, null)
7275
})
7376
})
@@ -87,13 +90,14 @@ tap.test('Forwards payload provided in incoming POST to GitHub status API', (t)
8790
.reply(201)
8891

8992
t.plan(1)
90-
t.tearDown(() => prCommitsScope.done() && scope.done())
9193

9294
supertest(app)
9395
.post('/node/jenkins/start')
9496
.send(fixture)
9597
.expect(201)
9698
.end((err, res) => {
99+
prCommitsScope.done()
100+
scope.done()
97101
t.equal(err, null)
98102
})
99103
})

0 commit comments

Comments
 (0)