-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathupdater-builder.test.ts
More file actions
159 lines (131 loc) · 3.84 KB
/
Copy pathupdater-builder.test.ts
File metadata and controls
159 lines (131 loc) · 3.84 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import {UpdaterBuilder} from '../src/updater-builder'
import {extractUpdaterSha} from '../src/utils'
import Docker from 'dockerode'
import {JobParameters} from '../src/inputs'
// Mock the extractUpdaterSha function to test the logic
jest.mock('../src/utils', () => ({
extractUpdaterSha: jest.fn()
}))
// Mock the ContainerService
jest.mock('../src/container-service', () => ({
ContainerService: {
storeCert: jest.fn().mockResolvedValue(undefined),
storeInput: jest.fn().mockResolvedValue(undefined)
}
}))
const mockExtractUpdaterSha = extractUpdaterSha as jest.MockedFunction<
typeof extractUpdaterSha
>
describe('UpdaterBuilder', () => {
let mockDocker: Docker
let mockCreateContainer: jest.Mock
let mockContainer: any
let mockProxy: any
let jobParams: JobParameters
let input: any
beforeEach(() => {
// Mock Docker container creation
mockContainer = {
id: 'test-container-id'
}
mockCreateContainer = jest.fn().mockResolvedValue(mockContainer)
mockDocker = {
createContainer: mockCreateContainer
} as any
// Mock proxy
mockProxy = {
url: jest.fn().mockResolvedValue('http://proxy:1080'),
networkName: 'test-network',
cert: 'test-cert'
}
// Mock job parameters
jobParams = new JobParameters(
1,
'job-token',
'cred-token',
'https://example.com',
'172.17.0.1',
'test-image'
)
// Mock input
input = {job: {id: 1}}
})
afterEach(() => {
jest.clearAllMocks()
})
it('should add DEPENDABOT_UPDATER_SHA to environment when SHA is present', async () => {
const testSha = '04aab0a156d33033b6082c7deb5feb6a212e4174'
const imageWithSha = `ghcr.io/dependabot/dependabot-updater-gomod:${testSha}`
mockExtractUpdaterSha.mockReturnValue(testSha)
const updaterBuilder = new UpdaterBuilder(
mockDocker,
jobParams,
input,
mockProxy,
imageWithSha
)
await updaterBuilder.run('test-container')
expect(mockCreateContainer).toHaveBeenCalledWith(
expect.objectContaining({
Env: expect.arrayContaining([`DEPENDABOT_UPDATER_SHA=${testSha}`])
})
)
})
it('should not add DEPENDABOT_UPDATER_SHA to environment when SHA is not present', async () => {
const imageWithoutSha = 'ghcr.io/dependabot/dependabot-updater-gomod'
mockExtractUpdaterSha.mockReturnValue(null)
const updaterBuilder = new UpdaterBuilder(
mockDocker,
jobParams,
input,
mockProxy,
imageWithoutSha
)
await updaterBuilder.run('test-container')
expect(mockCreateContainer).toHaveBeenCalledWith(
expect.objectContaining({
Env: expect.not.arrayContaining([
expect.stringMatching(/DEPENDABOT_UPDATER_SHA=/)
])
})
)
})
it('sets UPDATER_ONE_CONTAINER when both phases share a container', async () => {
mockExtractUpdaterSha.mockReturnValue(null)
const updaterBuilder = new UpdaterBuilder(
mockDocker,
jobParams,
input,
mockProxy,
'test-image'
)
await updaterBuilder.run('test-container')
expect(mockCreateContainer).toHaveBeenCalledWith(
expect.objectContaining({
Env: expect.arrayContaining(['UPDATER_ONE_CONTAINER=1'])
})
)
})
it.each(['fetch', 'update'] as const)(
'does not set UPDATER_ONE_CONTAINER for the %s phase',
async phase => {
mockExtractUpdaterSha.mockReturnValue(null)
const updaterBuilder = new UpdaterBuilder(
mockDocker,
jobParams,
input,
mockProxy,
'test-image',
phase
)
await updaterBuilder.run('test-container')
expect(mockCreateContainer).toHaveBeenCalledWith(
expect.objectContaining({
Env: expect.not.arrayContaining([
expect.stringMatching(/UPDATER_ONE_CONTAINER/)
])
})
)
}
)
})