Skip to content

Commit

Permalink
add pot mock, example tests
Browse files Browse the repository at this point in the history
  • Loading branch information
msterle committed Mar 26, 2024
1 parent 90d7354 commit 1d40fb6
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ To configure healthchecks via `crontab -e`, add the following in your editor of
*/2 * * * * /etc/cron.d/restart-actions.sh
```

Development
-----------

### Testing
This project uses [`shellspec`](https://github.com/shellspec/shellspec) as a test framework. To run tests locally, `shellspec` must first be installed on the local system according to the `shellspec` [installation guide](https://github.com/shellspec/shellspec?tab=readme-ov-file#installation).

Tests are defined in the `spec` directory, and are all files matching the pattern `<suite_name>_spec.sh`. Mocks, custom matchers and other fixtures can be defined in the `spec/support` directory.

The `pot` mock has already been defined and is imported into all tests. By default, all `pot` subcommands return truthy with no side effects, but these can be overloaded on a per-subcommand basis, as each subcommand has its own stub function. See [here](spec/simple_spec.sh#L47) for an example of overloading the `pot exec` subcommand, and [here](spec/support/pot_mock.sh#L59) for a list of all subcommand stubs that can be overloaded.

Wrapper scripts to run GitHub actions in a jail on FreeBSD
----------------------------------------------------------
Expand Down
31 changes: 30 additions & 1 deletion spec/simple_spec.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#shellcheck shell=bash

Describe 'check-pots.sh'
pot() { return 0; }
Include ./check-pots.sh
prepare() { pot='test'; }
Before 'prepare'
Expand Down Expand Up @@ -38,4 +37,34 @@ Describe 'check-pots.sh'
rm -fr $tree_name
End
End

Describe 'check_pot()'
It 'Should emit a warning if sshd is disabled'
check_tree() { :; }
pot_exec() {
case $3 in
sysrc) echo 'NO';;
which) echo '/usr/sbin/pkg64';;
esac
}
When call check_pot
The lines of output should equal 1
The output should include 'sshd is disabled on'
End

It 'Should emit a warning if sshd is disabled'
check_tree() { :; }
pot_exec() {
case $3 in
sysrc) echo '';;
which) echo '';;
esac
}
When call check_pot
The lines of output should equal 3
The line 1 of output should include "pkg64 on $pot was not found"
The line 2 of output should include "pkg64c on $pot was not found"
The line 3 of output should include "pk64cb on $pot was not found"
End
End
End
2 changes: 1 addition & 1 deletion spec/spec_helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ spec_helper_loaded() {
# This callback function will be invoked after core modules has been loaded.
spec_helper_configure() {
# Available functions: import, before_each, after_each, before_all, after_all
: import 'support/custom_matcher'
import 'support/pot_mock'
}
107 changes: 107 additions & 0 deletions spec/support/pot_mock.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# shellcheck shell=sh

pot() {
case $1 in
help) fn='pot_help';;
version) fn='pot_version';;
config) fn='pot_config';;
ls|list) fn='pot_list';;
show) fn='pot_show';;
info) fn='pot_info';;
top) fn='pot_top';;
ps) fn='pot_ps';;
init) fn='pot_init';;
de-init) fn='pot_de_init';;
vnet-start) fn='pot_vnet_start';;
create-base) fn='pot_create_base';;
create-fscomp) fn='pot_create_fscomp';;
create-private-bridge) fn='pot_create_private_bridge';;
create) fn='pot_create';;
clone) fn='pot_clone';;
clone-fscomp) fn='pot_clone_fscomp';;
rename) fn='pot_rename';;
destroy) fn='pot_destroy';;
prune) fn='pot_prune';;
copy-in) fn='pot_copy_in';;
copy-out) fn='pot_copy_out';;
mount-in) fn='pot_mount_in';;
mount-out) fn='pot_mount_out';;
add-dep) fn='pot_add_dep';;
set-rss) fn='pot_set_rss';;
get-rss) fn='pot_get_rss';;
set-cmd) fn='pot_set_cmd';;
set-env) fn='pot_set_env';;
set-hosts) fn='pot_set_hosts';;
set-hook) fn='pot_set_hook';;
set-attribute|set-attr) fn='pot_set_attribute';;
get-attribute|get-attr) fn='pot_get_attribute';;
export-ports) fn='pot_export_ports';;
start) fn='pot_start';;
stop) fn='pot_stop';;
term) fn='pot_term';;
run) fn='pot_run';;
snap|snapshot) fn='pot_snapshot';;
rollback|revert) fn='pot_revert';;
purge-snapshots) fn='pot_purge_snapshots';;
export) fn='pot_export';;
import) fn='pot_import';;
prepare) fn='pot_prepare';;
update-config) fn='pot_update_config';;
last-run-stats) fn='pot_last_run_stats';;
signal) fn='pot_signal';;
exec) fn='pot_exec';;
*) fn='pot_other';;
esac
shift 1
$fn "$@"
}

pot_help() { :; }
pot_version() { :; }
pot_config() { :; }
pot_list() { :; }
pot_show() { :; }
pot_info() { :; }
pot_top() { :; }
pot_ps() { :; }
pot_init() { :; }
pot_de_init() { :; }
pot_vnet_start() { :; }
pot_create_base() { :; }
pot_create_fscomp() { :; }
pot_create_private_bridge() { :; }
pot_create() { :; }
pot_clone() { :; }
pot_clone_fscomp() { :; }
pot_rename() { :; }
pot_destroy() { :; }
pot_prune() { :; }
pot_copy_in() { :; }
pot_copy_out() { :; }
pot_mount_in() { :; }
pot_mount_out() { :; }
pot_add_dep() { :; }
pot_set_rss() { :; }
pot_get_rss() { :; }
pot_set_cmd() { :; }
pot_set_env() { :; }
pot_set_hosts() { :; }
pot_set_hook() { :; }
pot_set_attribute() { :; }
pot_get_attribute() { :; }
pot_export_ports() { :; }
pot_start() { :; }
pot_stop() { :; }
pot_term() { :; }
pot_run() { :; }
pot_snapshot() { :; }
pot_revert() { :; }
pot_purge_snapshots() { :; }
pot_export() { :; }
pot_import() { :; }
pot_prepare() { :; }
pot_update_config() { :; }
pot_last_run_stats() { :; }
pot_signal() { :; }
pot_exec() { :; }
pot_other() { :; }

0 comments on commit 1d40fb6

Please sign in to comment.