-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathci_resume.test.js
137 lines (115 loc) · 4.43 KB
/
ci_resume.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import assert from 'assert';
import sinon from 'sinon';
import FormData from 'form-data';
import {
RunPRJob,
CI_CRUMB_URL,
CI_PR_NAME,
CI_PR_RESUME_URL
} from '../../lib/ci/run_ci.js';
import { CI_DOMAIN } from '../../lib/ci/ci_type_parser.js';
import TestCLI from '../fixtures/test_cli.js';
import { jobCache } from '../../lib/ci/build-types/job.js';
describe('Jenkins resume', () => {
const owner = 'nodejs';
const repo = 'node-auto-test';
const prid = 123456;
const jobid = 654321;
const crumb = 'asdf1234';
before(() => {
jobCache.disable();
sinon.stub(FormData.prototype, 'append').callsFake(function(key, value) {
assert.strictEqual(key, 'json');
const { parameter } = JSON.parse(value);
const expectedParameters = {
CERTIFY_SAFE: 'on',
TARGET_GITHUB_ORG: owner,
TARGET_REPO_NAME: repo,
PR_ID: prid,
REBASE_ONTO: '<pr base branch>',
DESCRIPTION_SETTER_DESCRIPTION: ''
};
for (const { name, value } of parameter) {
assert.strictEqual(value, expectedParameters[name]);
delete expectedParameters[name];
}
assert.strictEqual(Object.keys(expectedParameters).length, 0);
this._validated = true;
return FormData.prototype.append.wrappedMethod.bind(this)(key, value);
});
});
after(() => {
sinon.restore();
});
it('should return false if crumb fails', async() => {
const cli = new TestCLI();
const request = {
json: sinon.stub().throws()
};
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, jobid);
assert.strictEqual(await jobRunner.resume(), false);
});
it('should return false if run status not FAILURE', async() => {
const cli = new TestCLI();
const request = {
json: sinon.stub()
};
request.json.withArgs(CI_CRUMB_URL)
.returns(Promise.resolve({ crumb }));
request.json.withArgs(`https://${CI_DOMAIN}/job/${CI_PR_NAME}/${jobid}/api/json?tree=result%2Curl%2Cnumber`)
.returns(Promise.resolve({ result: null }));
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, jobid);
assert.strictEqual(await jobRunner.resume(), false);
});
it('should resume node-pull-request job', async() => {
const cli = new TestCLI();
const request = {
fetch: sinon.stub()
.callsFake((url, { method, headers }) => {
assert.strictEqual(url, `${CI_PR_RESUME_URL}${jobid}/resume`);
assert.strictEqual(method, 'POST');
assert.deepStrictEqual(headers, { 'Jenkins-Crumb': crumb });
return Promise.resolve({ status: 200 });
}),
json: sinon.stub()
};
request.json.withArgs(CI_CRUMB_URL)
.returns(Promise.resolve({ crumb }));
request.json.withArgs(`https://${CI_DOMAIN}/job/${CI_PR_NAME}/${jobid}/api/json?tree=result%2Curl%2Cnumber`)
.returns(Promise.resolve({ result: 'FAILURE' }));
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, jobid);
assert.ok(await jobRunner.resume());
});
it('should fail if resuming node-pull-request throws', async() => {
const cli = new TestCLI();
const request = {
fetch: sinon.stub().throws(),
json: sinon.stub()
};
request.json.withArgs(CI_CRUMB_URL)
.returns(Promise.resolve({ crumb }));
request.json.withArgs(`https://${CI_DOMAIN}/job/${CI_PR_NAME}/${jobid}/api/json?tree=result%2Curl%2Cnumber`)
.returns(Promise.resolve({ result: 'FAILURE' }));
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, jobid);
assert.strictEqual(await jobRunner.resume(), false);
});
it('should return false if node-pull-request not resumed', async() => {
const cli = new TestCLI();
const request = {
fetch: sinon.stub()
.callsFake((url, { method, headers }) => {
assert.strictEqual(url, `${CI_PR_RESUME_URL}${jobid}/resume`);
assert.strictEqual(method, 'POST');
assert.deepStrictEqual(headers, { 'Jenkins-Crumb': crumb });
return Promise.resolve({ status: 401 });
}),
json: sinon.stub()
};
request.json.withArgs(CI_CRUMB_URL)
.returns(Promise.resolve({ crumb }));
request.json.withArgs(`https://${CI_DOMAIN}/job/${CI_PR_NAME}/${jobid}/api/json?tree=result%2Curl%2Cnumber`)
.returns(Promise.resolve({ result: 'FAILURE' }));
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, jobid);
assert.strictEqual(await jobRunner.resume(), false);
});
});