Skip to content

Commit 2cde44a

Browse files
committed
Add couple more tests
1 parent 6fcd4ef commit 2cde44a

9 files changed

+219
-9
lines changed

__tests__/main.test.ts

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,74 @@
88

99
import * as core from '@actions/core'
1010
import * as main from '../src/main'
11+
import axios from 'axios'
1112

1213
// Mock the action's main function
1314
const runMock = jest.spyOn(main, 'run')
1415

1516
// Mock the GitHub Actions core library
17+
let infoMock: jest.SpyInstance
1618
let getInputMock: jest.SpyInstance
19+
let setFailedMock: jest.SpyInstance
1720

1821
describe('action', () => {
1922
beforeEach(() => {
2023
jest.clearAllMocks()
2124

25+
infoMock = jest.spyOn(core, 'info').mockImplementation()
2226
getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
27+
setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation()
2328
})
2429

25-
it('deploys Hashicorp', async () => {
30+
afterEach(() => {
31+
main.cleanup()
32+
})
33+
34+
it('fails if compose file does not exist', async () => {
35+
getInputMock.mockImplementation((name: string): string => {
36+
switch (name) {
37+
case 'compose-file':
38+
return 'nada.yml'
39+
case 'stack-name':
40+
return 'david'
41+
case 'ssh-user-at-host':
42+
return 'david@notexisting'
43+
case 'ssh-port':
44+
return '22'
45+
default:
46+
return ''
47+
}
48+
})
49+
50+
await main.run()
51+
expect(runMock).toHaveReturned()
52+
expect(setFailedMock).toHaveBeenCalledWith(
53+
'Compose file nada.yml does not exist'
54+
)
55+
})
56+
57+
it('fails if SSH is not reachable', async () => {
58+
getInputMock.mockImplementation((name: string): string => {
59+
switch (name) {
60+
case 'compose-file':
61+
return 'docker-compose.test.yml'
62+
case 'stack-name':
63+
return 'david'
64+
case 'ssh-user-at-host':
65+
return 'david@notexisting'
66+
case 'ssh-port':
67+
return '22'
68+
default:
69+
return ''
70+
}
71+
})
72+
73+
await main.run()
74+
expect(runMock).toHaveReturned()
75+
expect(setFailedMock).toHaveBeenCalled()
76+
})
77+
78+
it('deploys Hashicorp Echo HTTP server', async () => {
2679
// Set the action's inputs as return values from core.getInput()
2780
getInputMock.mockImplementation((name: string): string => {
2881
switch (name) {
@@ -41,5 +94,9 @@ describe('action', () => {
4194

4295
await main.run()
4396
expect(runMock).toHaveReturned()
44-
})
97+
expect(infoMock).toHaveBeenCalledWith('Cleaning up ...')
98+
99+
const response = await axios.get('http://127.0.0.1:8888')
100+
expect(response.data).toMatch(/Hello World/)
101+
}, 30000)
45102
})

action.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ inputs:
2929
runs:
3030
using: node20
3131
main: dist/index.js
32+
post: dist/index.js

badges/coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

dist/index.js

Lines changed: 28 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker-compose.test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ version: "3.9"
33
services:
44
web:
55
image: "hashicorp/http-echo"
6-
command: ["-text", "Hello World"]
6+
command: ["-listen", ":8080", "-text", "Hello World"]
77
ports:
88
- 8080:8080

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ services:
99
cgroup: host
1010
ports:
1111
- 127.0.0.1:2222:22
12+
- 127.0.0.1:8888:8080
1213
volumes:
1314
- /sys/fs/cgroup:/sys/fs/cgroup:rw

package-lock.json

Lines changed: 100 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"@typescript-eslint/eslint-plugin": "^6.13.1",
7575
"@typescript-eslint/parser": "^6.13.1",
7676
"@vercel/ncc": "^0.38.1",
77+
"axios": "^1.6.2",
7778
"eslint": "^8.55.0",
7879
"eslint-plugin-github": "^4.10.1",
7980
"eslint-plugin-jest": "^27.6.0",

0 commit comments

Comments
 (0)