Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests #2

Merged
merged 6 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 10 additions & 19 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,16 @@ jobs:
id: npm-lint
run: npm run lint

- name: Run test server
run: |
docker compose build
docker compose up &

- name: Add SSH identity of test server to known hosts
run: |
mkdir -p ~/.ssh
ssh-keyscan -t rsa -p 2222 127.0.0.1 >> ~/.ssh/known_hosts

- name: Test
id: npm-ci-test
run: npm run ci-test

test-action:
name: GitHub Actions Test
runs-on: ubuntu-latest

steps:
- name: Checkout
id: checkout
uses: actions/checkout@v4

- name: Test Local Action
id: test-action
uses: ./
with:
milliseconds: 2000

- name: Print Output
id: output
run: echo "${{ steps.test-action.outputs.time }}"
45 changes: 0 additions & 45 deletions .github/workflows/linter.yml

This file was deleted.

62 changes: 60 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,10 @@ describe('action', () => {

await main.run()
expect(runMock).toHaveReturned()
})
expect(setFailedMock).not.toHaveBeenCalled()
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.
59 changes: 52 additions & 7 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
Loading