Skip to content

Commit

Permalink
Add couple more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andyundso committed Dec 7, 2023
1 parent 6fcd4ef commit 2cde44a
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 9 deletions.
61 changes: 59 additions & 2 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,74 @@

import * as core from '@actions/core'
import * as main from '../src/main'
import axios from 'axios'

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

// Mock the GitHub Actions core library
let infoMock: jest.SpyInstance
let getInputMock: jest.SpyInstance
let setFailedMock: jest.SpyInstance

describe('action', () => {
beforeEach(() => {
jest.clearAllMocks()

infoMock = jest.spyOn(core, 'info').mockImplementation()
getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation()
})

it('deploys Hashicorp', async () => {
afterEach(() => {
main.cleanup()
})

it('fails if compose file does not exist', async () => {
getInputMock.mockImplementation((name: string): string => {
switch (name) {
case 'compose-file':
return 'nada.yml'
case 'stack-name':
return 'david'
case 'ssh-user-at-host':
return 'david@notexisting'
case 'ssh-port':
return '22'
default:
return ''
}
})

await main.run()
expect(runMock).toHaveReturned()
expect(setFailedMock).toHaveBeenCalledWith(
'Compose file nada.yml does not exist'
)
})

it('fails if SSH is not reachable', async () => {
getInputMock.mockImplementation((name: string): string => {
switch (name) {
case 'compose-file':
return 'docker-compose.test.yml'
case 'stack-name':
return 'david'
case 'ssh-user-at-host':
return 'david@notexisting'
case 'ssh-port':
return '22'
default:
return ''
}
})

await main.run()
expect(runMock).toHaveReturned()
expect(setFailedMock).toHaveBeenCalled()
})

it('deploys Hashicorp Echo HTTP server', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation((name: string): string => {
switch (name) {
Expand All @@ -41,5 +94,9 @@ describe('action', () => {

await main.run()
expect(runMock).toHaveReturned()
})
expect(infoMock).toHaveBeenCalledWith('Cleaning up ...')

const response = await axios.get('http://127.0.0.1:8888')
expect(response.data).toMatch(/Hello World/)
}, 30000)
})
1 change: 1 addition & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ inputs:
runs:
using: node20
main: dist/index.js
post: dist/index.js
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 28 additions & 3 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ version: "3.9"
services:
web:
image: "hashicorp/http-echo"
command: ["-text", "Hello World"]
command: ["-listen", ":8080", "-text", "Hello World"]
ports:
- 8080:8080
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ services:
cgroup: host
ports:
- 127.0.0.1:2222:22
- 127.0.0.1:8888:8080
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
100 changes: 100 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"@typescript-eslint/eslint-plugin": "^6.13.1",
"@typescript-eslint/parser": "^6.13.1",
"@vercel/ncc": "^0.38.1",
"axios": "^1.6.2",
"eslint": "^8.55.0",
"eslint-plugin-github": "^4.10.1",
"eslint-plugin-jest": "^27.6.0",
Expand Down
Loading

0 comments on commit 2cde44a

Please sign in to comment.