Skip to content

Commit

Permalink
Merge pull request #17 from cronitorio/automated-tests
Browse files Browse the repository at this point in the history
Tests on Actions
  • Loading branch information
shaneharter authored Jun 14, 2022
2 parents e0ef820 + 4f58a8b commit 1b4c7d8
Show file tree
Hide file tree
Showing 20 changed files with 431 additions and 548 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# This is a basic workflow to help you get started with Actions

name: "CronitorCLI Release"

on:
Expand Down
32 changes: 32 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: "Run CronitorCLI tests"

on:
- push

jobs:
tests:
name: Run tests
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- uses: actions/setup-go@v3

- name: Setup BATS
uses: mig4/setup-bats@v1
with:
bats-version: 1.7.0

- name: Build binary
run: go build -o cronitor main.go

- name: Run tests
working-directory: tests
env:
CRONITOR_API_KEY: "${{ secrets.CRONITOR_API_KEY }}"
run: |
echo "::add-mask::cb54ac4fd16142469f2d84fc1bbebd84"
echo "::add-mask::$CRONITOR_API_KEY"
export BATS_PATH="$(which bats)"
sudo -E -n -- $BATS_PATH *.bats
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,6 @@ cronitor
ctab
cronitor-cli

local.md
local.md
TODO.md
*.exe
3 changes: 2 additions & 1 deletion fixtures/crontab.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
0,5,10,15,20,25,30,35,40,45,50,55 * * * * true
0,15,30,45 * * * * /usr/bin/true
0 * * * * echo "this is a longer command"
0 * * * * echo "this is a longer command"
0 * * * * bash slave_status.sh
6 changes: 5 additions & 1 deletion tests/setup.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
CLI_LOGFILE="/tmp/test-build.log"
CLI_LOGFILE_ALTERNATE="/tmp/test-build-alternate.log"
CLI_CONFIGFILE="/etc/cronitor/cronitor.json"
#CLI_CONFIGFILE="/tmp/cronitor.json"
CLI_CONFIGFILE_ALTERNATE="/tmp/test-build-config.json"
CLI_ACTUAL_API_KEY="cb54ac4fd16142469f2d84fc1bbebd84"
#CLI_ACTUAL_API_KEY="cb54ac4fd16142469f2d84fc1bbebd84"
CLI_ACTUAL_API_KEY="$CRONITOR_API_KEY"
CLI_CRONTAB_TEMP="/tmp/crontab"
CLI_USERNAME=`whoami`

Expand All @@ -14,3 +16,5 @@ if [ "$1" = "--use-dev" ]
CRONITOR_ARGS=""
HOSTNAME="https://cronitor.link"
fi

sudo ../cronitor configure -k "$CLI_ACTUAL_API_KEY" >/dev/null 2>/dev/null
25 changes: 25 additions & 0 deletions tests/test-activity.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bats

setup() {
SCRIPT_DIR="$(dirname $BATS_TEST_FILENAME)"
cd $SCRIPT_DIR

source $SCRIPT_DIR/setup.sh
rm -f $CLI_LOGFILE
}

#################
# ACTIVITY TESTS
#################

@test "Activity integration test without filter" {
../cronitor $CRONITOR_ARGS activity 44oI2n --log $CLI_LOGFILE | grep -q "monitor_name"
}

@test "Activity integration test with only pings filter" {
../cronitor $CRONITOR_ARGS activity 44oI2n --only pings --log $CLI_LOGFILE | grep -q "monitor_name"
}

@test "Activity integration test with only alerts filter" {
../cronitor $CRONITOR_ARGS activity 44oI2n --only alerts --log $CLI_LOGFILE | grep -q -v "\"description\": \"ping\""
}
33 changes: 0 additions & 33 deletions tests/test-activity.sh

This file was deleted.

2 changes: 2 additions & 0 deletions tests/test-all.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash

set -euo pipefail

SCRIPT_DIR=$( cd $(dirname $0) ; pwd -P )
cd $SCRIPT_DIR

Expand Down
85 changes: 85 additions & 0 deletions tests/test-configure.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env bats

setup() {
SCRIPT_DIR="$(dirname $BATS_TEST_FILENAME)"
cd $SCRIPT_DIR

source $SCRIPT_DIR/setup.sh
# CLI_CONFIGFILE="$BATS_TMPDIR/cronitor.json"
MSG=`date`
}

teardown() {
rm -f $CLI_LOGFILE
rm -f $CLI_CONFIGFILE
rm -f $CLI_LOGFILE_ALTERNATE
}

#################
# CONFIGURE TESTS
#################

@test "Configure uses log file from env var" {
CRONITOR_LOG=$CLI_LOGFILE ../cronitor $CRONITOR_ARGS ping d3x0c1 --run -k $CRONITOR_API_KEY
grep "d3x0c1" $CLI_LOGFILE | grep -q 'state=run'
}

@test "Configure uses hostname from env var" {
CRONITOR_HOSTNAME=myHost ../cronitor $CRONITOR_ARGS ping d3x0c1 --run --log $CLI_LOGFILE
grep -q "&host=myHost" $CLI_LOGFILE
}

@test "Configure uses hostname from arg not env var" {
CRONITOR_HOSTNAME=myHost ../cronitor $CRONITOR_ARGS ping d3x0c1 --run --log $CLI_LOGFILE --hostname otherHost
grep -q "&host=otherHost" $CLI_LOGFILE
}

@test "Configure uses ping api key from env var" {
CRONITOR_PING_API_KEY=123 ../cronitor $CRONITOR_ARGS ping d3x0c1 --run --log $CLI_LOGFILE -k $CRONITOR_API_KEY
grep -q "ping/123" $CLI_LOGFILE
}

@test "Configure writes hostname correctly to config file" {
../cronitor $CRONITOR_ARGS configure --hostname "$MSG" >/dev/null
grep "CRONITOR_HOSTNAME" $CLI_CONFIGFILE | grep -q "$MSG"
}

@test "Configure writes API Key correctly to config file" {
../cronitor $CRONITOR_ARGS configure --api-key "$MSG" >/dev/null
grep "CRONITOR_API_KEY" $CLI_CONFIGFILE | grep -q "$MSG"
}

@test "Configure writes API Key correctly to new config file" {
../cronitor $CRONITOR_ARGS configure --api-key "$CLI_ACTUAL_API_KEY" >/dev/null # Using actual API key here so it will be avail for later integration tests..
grep "CRONITOR_API_KEY" $CLI_CONFIGFILE | grep -q "$CLI_ACTUAL_API_KEY"
}

@test "Configure writes API Key correctly to custom config file set by param" {
../cronitor $CRONITOR_ARGS configure --config $CLI_CONFIGFILE_ALTERNATE --api-key "$MSG" 2>/dev/null
grep "CRONITOR_API_KEY" $CLI_CONFIGFILE_ALTERNATE | grep -q "$MSG"
}

@test "Configure writes API Key correctly to custom config file set by env var" {
CRONITOR_CONFIG=$CLI_CONFIGFILE_ALTERNATE ../cronitor $CRONITOR_ARGS configure --api-key "$MSG" 2>/dev/null
grep "CRONITOR_API_KEY" $CLI_CONFIGFILE_ALTERNATE | grep -q "$MSG"
}

@test "Configure writes Ping API Key correctly to config file" {
../cronitor $CRONITOR_ARGS configure --ping-api-key "$MSG" >/dev/null
grep "CRONITOR_PING_API_KEY" $CLI_CONFIGFILE | grep -q "$MSG"
}

@test "Configure writes log path correctly to config file" {
../cronitor $CRONITOR_ARGS configure --log $CLI_LOGFILE_ALTERNATE 2>/dev/null
grep "CRONITOR_LOG" $CLI_CONFIGFILE | grep -q $CLI_LOGFILE_ALTERNATE
}

@test "Configure writes exclude text correctly to config file" {
../cronitor $CRONITOR_ARGS configure --exclude-from-name "$MSG" 2>/dev/null
grep -q "CRONITOR_EXCLUDE_TEXT" $CLI_CONFIGFILE && grep -q "$MSG" $CLI_CONFIGFILE
}

@test "Configure writes multiple exclude text entries correctly to config file" {
../cronitor $CRONITOR_ARGS configure --exclude-from-name "${MSG}A" --exclude-from-name "${MSG}B" 2>/dev/null
grep -q "CRONITOR_EXCLUDE_TEXT" $CLI_CONFIGFILE && grep -q "${MSG}A" $CLI_CONFIGFILE && grep -q "${MSG}B" $CLI_CONFIGFILE
}
129 changes: 0 additions & 129 deletions tests/test-configure.sh

This file was deleted.

Loading

0 comments on commit 1b4c7d8

Please sign in to comment.