Files
myos/spec/unit/hooks_spec.sh
Yann Autissier 084a25c627 make the hooks usable by the whole catalogue
- hooks load the _stack files of every directory between the stack path root
  and the stack, outermost first: make included both $(dir)/*.mk and
  $(dir)/*/*.mk, so a stack in a subdirectory saw its parent's settings
- a group may be declared in <group>/<group>.env, where the stack lives
- MYOS_STACK_DIR lets a hook read a file it ships next to itself
- myos_filter no longer confuses a literal * in a make pattern with a wildcard,
  and the list helpers no longer let the shell expand a * into filenames
- MACHINE, SYSTEM, HOST and DOMAINNAME join the framework variables a hook sees
- the make shim gains $(call myos-var,NAME), so a .mk target can read a
  setting that now lives in a hook, and it picks up the .mk of every stack
  directory rather than only the project's
2026-09-03 22:30:01 +02:00

86 lines
3.1 KiB
Bash

#shellcheck shell=sh
Include lib/str.sh
Include lib/core.sh
Include lib/var.sh
Include lib/tags.sh
Include lib/naming.sh
Include lib/config.sh
Include lib/stack.sh
Include lib/hooks.sh
# A stack ships its computed settings as a shell hook, so the catalogue works
# on a machine without make. These check the hook and the uri it builds on.
Describe 'lib/hooks.sh'
setup() {
MYOS_TMP=$(mktemp -d "${TMPDIR:-/tmp}/myos-hook.XXXXXX")
MYOS_TMP=$(cd "$MYOS_TMP" && pwd -P)
ENV=local; DOMAIN=example.org; USER=tester; HOSTNAME=testhost
MYOS_PATH=$MYOS_TMP
APP_HOST=demo.example.org; APP_URI=demo.example.org/
}
cleanup() { rm -rf "$MYOS_TMP"; }
BeforeEach setup
AfterEach cleanup
It 'loads plain values from a .env hook'
printf 'DEMO_VERSION=1.2.3\n' > "$MYOS_TMP/demo.env"
When run source spec/unit/hooks_helper.sh "$MYOS_TMP" demo DEMO_VERSION
The output should equal "1.2.3"
End
It 'loads a computed value from a .sh hook'
printf 'DEMO_TAGS=$(myos_tagprefix demo 8000)\n' > "$MYOS_TMP/demo.sh"
When run source spec/unit/hooks_helper.sh "$MYOS_TMP" demo DEMO_TAGS
The output should equal "urlprefix-demo.demo.example.org/*"
End
It 'lets a hook name the service it publishes'
printf 'DEMO_SERVICE_8000_NAME=www\nDEMO_TAGS=$(myos_tagprefix demo 8000)\n' > "$MYOS_TMP/demo.sh"
When run source spec/unit/hooks_helper.sh "$MYOS_TMP" demo DEMO_TAGS
The output should equal "urlprefix-www.demo.example.org/*"
End
It 'lets the environment win over a hook default'
printf 'DEMO_VERSION=${DEMO_VERSION:-1.2.3}\n' > "$MYOS_TMP/demo.sh"
When run source spec/unit/hooks_helper.sh "$MYOS_TMP" demo DEMO_VERSION 9.9.9
The output should equal "9.9.9"
End
It 'is quiet when a stack has no hook'
When call myos_stack_hooks "$MYOS_TMP" nothing
The status should be success
The output should equal ""
End
End
Describe 'lib/naming.sh service uris'
BeforeEach 'ENV=local; unset APP_HOST_MULTI_APP APP_HOST_MULTI_USER APP_HOST_MULTI_ENV HOST_LB 2>/dev/null || true'
It 'serves a host stack on <hostname>.<domain>'
When call myos_app_host host aya local fabio example.org sonic
The output should equal "sonic.example.org"
End
It 'adds the bare domain when the host is the load balancer'
HOST_LB=true
When call myos_app_host host aya local fabio example.org sonic
The output should equal "sonic.example.org example.org"
End
It 'does not prefix a main environment'
When call myos_app_host app aya master duniter example.org sonic
The output should equal "example.org"
End
It 'prefixes any other environment'
When call myos_app_host app aya staging duniter example.org sonic
The output should equal "staging.example.org"
End
It 'prefixes the user when asked to'
APP_HOST_MULTI_USER=true
When call myos_app_host app aya master duniter example.org sonic
The output should equal "aya.example.org"
End
It 'builds a uri that ends with a slash'
When call myos_app_uri "a.example.org b.example.org"
The output should equal "a.example.org/ b.example.org/"
End
End