forked from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker.test.js
More file actions
113 lines (101 loc) · 3.51 KB
/
Copy pathdocker.test.js
File metadata and controls
113 lines (101 loc) · 3.51 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
const { execSync } = require('child_process')
const core = require('@actions/core')
const logger = require('../../src/util/log')
const {
runDockerImage,
createDockerNetwork,
removeContainer,
waitForContainerToFinish,
isContainerFinished
} = require('../../src/util/docker')
jest.mock('child_process')
jest.mock('@actions/core')
jest.mock('../../src/util/log')
describe('runDockerImage', () => {
it('should execute docker command successfully', () => {
execSync.mockReturnValueOnce('success')
runDockerImage('docker run my-image', 1)
expect(execSync).toHaveBeenCalledWith('docker run my-image')
})
it('should log error and set failed status on command failure', () => {
execSync.mockImplementationOnce(() => {
throw new Error('command failed')
})
expect(() => runDockerImage('docker run my-image', 1)).toThrow(
'command failed'
)
expect(logger.logError).toHaveBeenCalledWith(
'[Docker]',
'Failed to run data-caterer container',
expect.any(Error)
)
expect(core.setFailed).toHaveBeenCalledWith(new Error('command failed'))
})
})
describe('removeContainer', () => {
it('should remove existing container', () => {
execSync.mockReturnValueOnce('container-id')
removeContainer('my-container')
expect(execSync).toHaveBeenCalledWith('docker rm my-container')
})
it('should not attempt to remove non-existing container', () => {
execSync.mockReturnValueOnce('')
removeContainer('my-container')
expect(execSync).not.toHaveBeenCalledWith('docker rm my-container')
})
})
describe('createDockerNetwork', () => {
it('should create network if not exists', () => {
execSync.mockReturnValueOnce('network details')
execSync.mockReturnValueOnce('')
createDockerNetwork()
expect(execSync).toHaveBeenCalledWith(
'docker network create insta-infra_default'
)
})
it('should not create network if already exists', () => {
execSync.mockReturnValueOnce('insta-infra_default')
createDockerNetwork()
expect(execSync).not.toHaveBeenCalledWith(
'docker network create insta-infra_default'
)
})
})
describe('isContainerFinished', () => {
it('should return true if container finished successfully', () => {
execSync.mockReturnValueOnce('container-id')
execSync.mockReturnValueOnce('container-id')
const result = isContainerFinished('my-container')
expect(result).toBe(true)
})
it('should return false if container is still running', () => {
execSync.mockReturnValueOnce('')
const result = isContainerFinished('my-container')
expect(result).toBe(false)
})
it('should throw error if container finished with non-zero exit code', () => {
execSync.mockReturnValueOnce('container-id')
execSync.mockReturnValueOnce('')
expect(() => isContainerFinished('my-container')).toThrow(
'my-container docker container failed'
)
})
})
describe('waitForContainerToFinish', () => {
it('should resolve when container finishes', async () => {
execSync.mockReturnValueOnce('container-id')
execSync.mockReturnValueOnce('container-id')
await waitForContainerToFinish('my-container')
expect(execSync).toHaveBeenCalledWith(
'docker ps -q -f name=my-container -f status=exited'
)
})
it('should reject when isContainerFinished throws an error', async () => {
execSync.mockImplementation(() => {
throw new Error('Docker command failed')
})
await expect(waitForContainerToFinish('my-container')).rejects.toThrow(
'Docker command failed'
)
})
})