prole/tests/shellspec/spec/lib/prole_helpers_spec.sh

95 lines
2.5 KiB
Bash

#!/usr/bin/env bash
Describe 'etc/lib/shell helpers'
Include etc/lib/shell/knoe_env.sh
Include etc/lib/shell/knoe_string.sh
Include etc/lib/shell/knoe_secrets.sh
Include etc/lib/shell/knoe_yaml.sh
Include etc/lib/shell/knoe_guardrails.sh
Include etc/lib/shell/knoe_cmd.sh
Describe 'knoe_first_nonempty'
It 'returns the first non-empty argument'
When call knoe_first_nonempty '' 'a' 'b'
The status should be success
The output should eq 'a'
End
It 'fails when all are empty'
When call knoe_first_nonempty '' ''
The status should be failure
End
End
Describe 'knoe_normalize_realm'
It 'trims and uppercases'
When call knoe_normalize_realm ' knoe.org '
The output should eq 'PROLE.ORG'
End
It 'returns empty for empty input'
When call knoe_normalize_realm ''
The output should eq ''
End
End
Describe 'knoe_secret_choose_value'
It 'keeps literal current values'
When call knoe_secret_choose_value 'literal' 'fetched'
The output should eq 'literal'
End
It 'prefers fetched when current is empty'
When call knoe_secret_choose_value '' 'fetched'
The output should eq 'fetched'
End
It 'prefers fetched when current is a secret ref'
When call knoe_secret_choose_value '${OPENBAO:kv/knoe/default/kerberos#password}' 'fetched'
The output should eq 'fetched'
End
End
Describe 'knoe_yaml_get_scalar'
setup_yaml() {
yml_file="$(mktemp)"
cat >"$yml_file" <<'EOF'
samba_dns_server: "10.0.0.10"
samba_dns_admin_user: administrator
EOF
}
teardown_yaml() {
rm -f "${yml_file:-}"
}
BeforeAll 'setup_yaml'
AfterAll 'teardown_yaml'
It 'extracts quoted scalars'
When call knoe_yaml_get_scalar "$yml_file" samba_dns_server
The output should eq '10.0.0.10'
End
It 'extracts unquoted scalars'
When call knoe_yaml_get_scalar "$yml_file" samba_dns_admin_user
The output should eq 'administrator'
End
End
Describe 'knoe_guard_nonempty'
It 'returns 2 and emits an error for missing values'
When run knoe_guard_nonempty 'KRB5_REALM' ''
The status should eq 2
The stderr should include 'ERROR: KRB5_REALM is required'
End
End
Describe 'knoe_kubectl_exec_cmd'
It 'assembles a deterministic, escaped command'
When call knoe_kubectl_exec_cmd default pod-1 'echo hi'
The output should eq 'kubectl -n default exec pod-1 -- sh -c echo\ hi'
End
End
End