Repository navigation
Expand file tree
/
Copy pathpost.tests.ts
More file actions
129 lines (104 loc) · 3.92 KB
/
Copy pathpost.tests.ts
File metadata and controls
129 lines (104 loc) · 3.92 KB
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
import assert from 'assert';
import https from 'https';
import sinon from 'sinon';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import { run, sendReport, _sendReport } from '../lib/post';
describe('postjob run', function() {
let execStub: sinon.SinonStub;
let sendReportStub: sinon.SinonStub;
beforeEach(() => {
execStub = sinon.stub(exec, 'exec');
sendReportStub = sinon.stub(sendReport);
});
afterEach(() => {
execStub.restore();
sendReport.restore();
});
it('should run three docker commands and send the report', async () => {
await run();
sinon.assert.callCount(execStub, 3);
sinon.assert.calledWith(execStub, 'docker --version');
sinon.assert.calledWith(execStub, 'docker images --format CreatedAt={{.CreatedAt}}::Repo={{.Repository}}::Tag={{.Tag}}::Digest={{.Digest}}');
sinon.assert.calledOnce(sendReport);
});
});
describe('postjob sendReport', function() {
let _sendReportStub: sinon.SinonStub;
let data: Object;
beforeEach(() => {
_sendReportStub = sinon.stub(_sendReport);
data = {
"key.fake": "value.fake"
};
});
afterEach(() => {
_sendReportStub.restore();
});
it('should still call _sendReport once if retryCount < 1', async () => {
await sendReport(data, -1);
sinon.assert.calledOnce(_sendReport);
});
it('should succeed if _sendReport succeeds', async () => {
_sendReportStub.throws(new Error('_sendReport().Error'));
await sendReport(data, 0);
sinon.assert.calledOnce(_sendReport);
});
it('should succeed if _sendReport succeeds', async () => {
await sendReport(data, 0);
sinon.assert.calledOnce(_sendReport);
});
// should still call _sendReport once if retryCount < 1
// should succeed if _sendReport succeeds
// should fail if _sendReport fails and retryCount == 0
// should succeed if _sendReport fails the first time and succeeds the second if retryCount > 0
// should fail if _sendReport fails for all retries
});
describe('postjob _sendReport', function() {
let core_getIDTokenStub: sinon.SinonStub;
let https_requestStub: sinon.SinonStub;
let clientRequestStub;
let data: Object;
const expectedUrl = 'https://dfdinfra-afdendpoint2-dogfood-edb5h5g7gyg7h3hq.z01.azurefd.net/github/v1/container-mappings';
beforeEach(() => {
core_getIDTokenStub = sinon.stub(core, 'getIDToken');
https_requestStub = sinon.stub(https, 'request');
clientRequestStub = sinon.stub();
clientRequestStub.end = sinon.stub();
core_getIDTokenStub.resolves('bearerToken.mock');
https_requestStub
.callsArgWith(2, {
on: (event, callback) => {
if (event === 'data') {
callback();
} else if (event === 'end') {
callback();
}
},
end: () => {}
})
.returns(clientRequestStub);
data = {
"key.fake": "value.fake"
};
});
afterEach(() => {
core_getIDTokenStub.restore();
https_requestStub.restore();
clientRequestStub.restore();
});
it('should still call _sendReport once if retryCount < 1', async () => {
await _sendReport(data, -1);
sinon.assert.calledOnce(core_getIDTokenStub);
sinon.assert.calledOnce(https_requestStub);
// {
// method: 'POST',
// timeout: 2500,
// headers: {
// 'Content-Type': 'application/json',
// 'Authorization': 'Bearer bearerToken.mock'
// },
// data: data
// };
});
});