test: int suite fixes

This commit is contained in:
decentral1se 2024-07-17 13:22:14 +02:00
parent e5a6dea10c
commit 8b8e158664
Signed by untrusted user: decentral1se
GPG Key ID: 03789458B3D0C410
20 changed files with 115 additions and 79 deletions

View File

@ -64,7 +64,7 @@ EXAMPLE:
app.Recipe.Version = specificVersion app.Recipe.Version = specificVersion
} }
if specificVersion == "" && app.Recipe.Version != "" { if specificVersion == "" && app.Recipe.Version != "" && !internal.Chaos {
log.Debugf("retrieved %s as version from env file", app.Recipe.Version) log.Debugf("retrieved %s as version from env file", app.Recipe.Version)
specificVersion = app.Recipe.Version specificVersion = app.Recipe.Version
} }

View File

@ -160,6 +160,7 @@ var appSecretInsertCommand = cli.Command{
internal.PassFlag, internal.PassFlag,
internal.FileFlag, internal.FileFlag,
internal.TrimFlag, internal.TrimFlag,
internal.ChaosFlag,
}, },
Before: internal.SubCommandBefore, Before: internal.SubCommandBefore,
ArgsUsage: "<domain> <secret-name> <version> <data>", ArgsUsage: "<domain> <secret-name> <version> <data>",

View File

@ -61,7 +61,6 @@ var recipeVersionCommand = cli.Command{
log.Fatalf("%s has no published versions?", recipe.Name) log.Fatalf("%s has no published versions?", recipe.Name)
} }
var allRows [][]string
for i := len(recipeMeta.Versions) - 1; i >= 0; i-- { for i := len(recipeMeta.Versions) - 1; i >= 0; i-- {
table, err := formatter.CreateTable() table, err := formatter.CreateTable()
if err != nil { if err != nil {
@ -71,6 +70,7 @@ var recipeVersionCommand = cli.Command{
table.Headers("SERVICE", "NAME", "TAG") table.Headers("SERVICE", "NAME", "TAG")
for version, meta := range recipeMeta.Versions[i] { for version, meta := range recipeMeta.Versions[i] {
var allRows [][]string
var rows [][]string var rows [][]string
for service, serviceMeta := range meta { for service, serviceMeta := range meta {
@ -86,6 +86,18 @@ var recipeVersionCommand = cli.Command{
fmt.Println(table) fmt.Println(table)
log.Infof("VERSION: %s", version) log.Infof("VERSION: %s", version)
fmt.Println() fmt.Println()
continue
}
if internal.MachineReadable {
sort.Slice(allRows, sortServiceByName(allRows))
headers := []string{"VERSION", "SERVICE", "NAME", "TAG"}
out, err := formatter.ToJSON(headers, allRows)
if err != nil {
log.Fatal("unable to render to JSON: %s", err)
}
fmt.Println(out)
continue
} }
} }
} }
@ -96,17 +108,6 @@ var recipeVersionCommand = cli.Command{
} }
} }
if internal.MachineReadable {
sort.Slice(allRows, sortServiceByName(allRows))
headers := []string{"VERSION", "SERVICE", "NAME", "TAG"}
out, err := formatter.ToJSON(headers, allRows)
if err != nil {
log.Fatal("unable to render to JSON: %s", err)
}
fmt.Println(out)
return nil
}
return nil return nil
}, },
} }

View File

@ -576,6 +576,7 @@ func (a App) WriteRecipeVersion(version string) error {
} }
defer file.Close() defer file.Close()
skipped := false
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
lines := []string{} lines := []string{}
for scanner.Scan() { for scanner.Scan() {
@ -590,6 +591,12 @@ func (a App) WriteRecipeVersion(version string) error {
continue continue
} }
if strings.Contains(line, version) {
skipped = true
lines = append(lines, line)
continue
}
splitted := strings.Split(line, ":") splitted := strings.Split(line, ":")
line = fmt.Sprintf("%s:%s", splitted[0], version) line = fmt.Sprintf("%s:%s", splitted[0], version)
lines = append(lines, line) lines = append(lines, line)
@ -603,7 +610,11 @@ func (a App) WriteRecipeVersion(version string) error {
log.Fatal(err) log.Fatal(err)
} }
log.Infof("version %s saved to %s.env", version, a.Domain) if !skipped {
log.Infof("version %s saved to %s.env", version, a.Domain)
} else {
log.Debugf("skipping version %s write as already exists in %s.env", version, a.Domain)
}
return nil return nil
} }

View File

@ -69,7 +69,7 @@ func ToJSON(headers []string, rows [][]string) (string, error) {
buff.Write([]byte("[")) buff.Write([]byte("["))
for _, row := range rows { for idx, row := range rows {
payload := make(map[string]string) payload := make(map[string]string)
for idx, header := range headers { for idx, header := range headers {
@ -82,6 +82,10 @@ func ToJSON(headers []string, rows [][]string) (string, error) {
} }
buff.Write(serialized) buff.Write(serialized)
if idx < (len(rows) - 1) {
buff.Write([]byte(","))
}
} }
buff.Write([]byte("]")) buff.Write([]byte("]"))

View File

@ -26,21 +26,26 @@ func (r Recipe) Ensure(chaos bool, offline bool) error {
if err := r.EnsureIsClean(); err != nil { if err := r.EnsureIsClean(); err != nil {
return err return err
} }
if !offline { if !offline {
if err := r.EnsureUpToDate(); err != nil { if err := r.EnsureUpToDate(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
if r.Version != "" { if r.Version != "" {
log.Debugf("ensuring version %s", r.Version) log.Debugf("ensuring version %s", r.Version)
if _, err := r.EnsureVersion(r.Version); err != nil { if _, err := r.EnsureVersion(r.Version); err != nil {
return err return err
} }
} else {
if err := r.EnsureLatest(); err != nil { return nil
return err
}
} }
if err := r.EnsureLatest(); err != nil {
return err
}
return nil return nil
} }

View File

@ -20,6 +20,7 @@ setup(){
teardown(){ teardown(){
_reset_recipe _reset_recipe
_reset_tags
} }
@test "validate app argument" { @test "validate app argument" {
@ -82,19 +83,17 @@ teardown(){
} }
@test "ensure recipe not up to date if --offline" { @test "ensure recipe not up to date if --offline" {
wantHash=$(_get_n_hash 1) _ensure_env_version "0.1.0+1.20.0"
latestRelease=$(_latest_release)
run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" reset --hard HEAD~1 run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -d "$latestRelease"
assert_success assert_success
assert_equal $(_get_current_hash) "$wantHash" # NOTE(d1): don't assert success because it might flake
# NOTE(d1): we can't quite tell if this will fail or not in the future, so,
# since it isn't an important part of what we're testing here, we don't check
# it
run $ABRA app check "$TEST_APP_DOMAIN" --offline run $ABRA app check "$TEST_APP_DOMAIN" --offline
assert_equal $(_get_current_hash) "$wantHash" run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l
refute_output --partial "$latestRelease"
} }
@test "error if missing .env.sample" { @test "error if missing .env.sample" {

View File

@ -19,8 +19,9 @@ setup(){
} }
teardown(){ teardown(){
_undeploy_app
_reset_recipe _reset_recipe
_reset_tags
_undeploy_app
} }
# bats test_tags=slow # bats test_tags=slow
@ -105,20 +106,18 @@ test_cmd_export"
} }
@test "ensure recipe not up to date if --offline" { @test "ensure recipe not up to date if --offline" {
wantHash=$(_get_n_hash 3) _ensure_env_version "0.1.0+1.20.0"
latestRelease=$(_latest_release)
run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" reset --hard HEAD~3 run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -d "$latestRelease"
assert_success assert_success
assert_equal $(_get_current_hash) "$wantHash"
run $ABRA app cmd --local --offline "$TEST_APP_DOMAIN" test_cmd run $ABRA app cmd --local --offline "$TEST_APP_DOMAIN" test_cmd
assert_success assert_success
assert_output --partial 'baz' assert_output --partial 'baz'
assert_equal $(_get_current_hash) $wantHash run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l
refute_output --partial "$latestRelease"
_reset_recipe "$TEST_RECIPE"
} }
@test "error if missing arguments without passing --local" { @test "error if missing arguments without passing --local" {

View File

@ -87,19 +87,18 @@ teardown(){
# bats test_tags=slow # bats test_tags=slow
@test "ensure recipe not up to date if --offline" { @test "ensure recipe not up to date if --offline" {
wantHash=$(_get_n_hash 3) _ensure_env_version "0.1.0+1.20.0"
latestRelease=$(_latest_release)
run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" reset --hard HEAD~3 run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -d "$latestRelease"
assert_success assert_success
assert_equal $(_get_current_hash) "$wantHash"
# NOTE(d1): need to use --chaos to force same commit
run $ABRA app deploy "$TEST_APP_DOMAIN" \ run $ABRA app deploy "$TEST_APP_DOMAIN" \
--no-input --no-converge-checks --chaos --offline --no-input --no-converge-checks --offline
assert_success assert_success
assert_equal $(_get_current_hash) "$wantHash" run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l
refute_output --partial "$latestRelease"
} }
# bats test_tags=slow # bats test_tags=slow
@ -107,6 +106,7 @@ teardown(){
latestCommit="$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" rev-parse --short HEAD)" latestCommit="$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" rev-parse --short HEAD)"
_remove_tags _remove_tags
_wipe_env_version
# NOTE(d1): need to pass --offline to stop tags being pulled again # NOTE(d1): need to pass --offline to stop tags being pulled again
run $ABRA app deploy "$TEST_APP_DOMAIN" \ run $ABRA app deploy "$TEST_APP_DOMAIN" \
@ -126,6 +126,8 @@ teardown(){
assert_equal $(_get_current_hash) "$wantHash" assert_equal $(_get_current_hash) "$wantHash"
_wipe_env_version
run $ABRA app deploy "$TEST_APP_DOMAIN" \ run $ABRA app deploy "$TEST_APP_DOMAIN" \
--no-input --no-converge-checks --chaos --no-input --no-converge-checks --chaos
assert_success assert_success

View File

@ -67,8 +67,7 @@ teardown(){
run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input --no-converge-checks run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input --no-converge-checks
assert_success assert_success
latestRelease=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | tail -n 1) run grep -q "TYPE=git.coopcloud.tech\/coop-cloud\/abra-test-recipe:$(_latest_release)" \
run grep -q "TYPE=git.coopcloud.tech\/coop-cloud\/abra-test-recipe:$latestRelease" \
"$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
assert_success assert_success
} }

View File

@ -62,8 +62,8 @@ teardown(){
run $ABRA app ls --server foo.com run $ABRA app ls --server foo.com
assert_success assert_success
refute_output --partial "server: $TEST_SERVER |" refute_output --partial "SERVER: $TEST_SERVER"
assert_output --partial "server: foo.com |" assert_output --partial "SERVER: foo.com"
run rm -rf "$ABRA_DIR/servers/foo.com" run rm -rf "$ABRA_DIR/servers/foo.com"
assert_success assert_success
@ -97,8 +97,8 @@ teardown(){
@test "server stats are correct" { @test "server stats are correct" {
run $ABRA app ls run $ABRA app ls
assert_success assert_success
assert_output --partial "server: $TEST_SERVER" assert_output --partial "SERVER: $TEST_SERVER"
assert_output --partial "total apps: 1" assert_output --partial "TOTAL APPS: 1"
run mkdir -p "$ABRA_DIR/servers/foo.com" run mkdir -p "$ABRA_DIR/servers/foo.com"
assert_success assert_success
@ -113,8 +113,8 @@ teardown(){
assert_success assert_success
assert_output --partial "$TEST_SERVER" assert_output --partial "$TEST_SERVER"
assert_output --partial "foo.com" assert_output --partial "foo.com"
assert_output --partial "total servers: 2" assert_output --partial "TOTAL SERVERS: 2"
assert_output --partial "total apps: 2" assert_output --partial "TOTAL APPS: 2"
run rm -rf "$ABRA_DIR/servers/foo.com" run rm -rf "$ABRA_DIR/servers/foo.com"
assert_success assert_success

View File

@ -40,8 +40,7 @@ teardown(){
_get_current_hash _get_current_hash
assert_equal "$headHash" "$currentHash" assert_equal "$headHash" "$currentHash"
latestRelease=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | tail -n 1) run grep -q "TYPE=$TEST_RECIPE:$(_latest_release)" \
run grep -q "TYPE=$TEST_RECIPE:$latestRelease" \
"$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
assert_success assert_success
} }

View File

@ -41,13 +41,11 @@ teardown(){
@test "show ps report" { @test "show ps report" {
_deploy_app _deploy_app
latestRelease=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | tail -n 1)
run $ABRA app ps "$TEST_APP_DOMAIN" run $ABRA app ps "$TEST_APP_DOMAIN"
assert_success assert_success
assert_output --partial 'app' assert_output --partial 'app'
assert_output --partial 'healthy' assert_output --partial 'healthy'
assert_output --partial "$latestRelease" assert_output --partial $(_latest_release)
assert_output --partial 'false' # not a chaos deploy assert_output --partial 'false' # not a chaos deploy
} }

View File

@ -58,18 +58,18 @@ teardown(){
} }
@test "ensure recipe not up to date if --offline" { @test "ensure recipe not up to date if --offline" {
wantHash=$(_get_n_hash 3) _ensure_env_version "0.1.0+1.20.0"
latestRelease=$(_latest_release)
run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" reset --hard HEAD~3 run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -d "$latestRelease"
assert_success assert_success
assert_equal $(_get_current_hash) "$wantHash"
run $ABRA app rollback "$TEST_APP_DOMAIN" \ run $ABRA app rollback "$TEST_APP_DOMAIN" \
--no-input --no-converge-checks --offline --no-input --no-converge-checks --offline
assert_failure assert_failure
assert_equal $(_get_current_hash) $wantHash run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l
refute_output --partial "$latestRelease"
} }
@test "error if not already deployed" { @test "error if not already deployed" {

View File

@ -217,7 +217,8 @@ teardown(){
run bash -c "echo bar >> $ABRA_DIR/recipes/$TEST_RECIPE/foo" run bash -c "echo bar >> $ABRA_DIR/recipes/$TEST_RECIPE/foo"
run $ABRA app secret insert \ run $ABRA app secret insert \
--file "$TEST_APP_DOMAIN" test_pass_one v1 "$ABRA_DIR/recipes/$TEST_RECIPE/foo" --chaos \
--file "$TEST_APP_DOMAIN" test_pass_one v1 "$ABRA_DIR/recipes/$TEST_RECIPE/foo"
assert_success assert_success
assert_output --partial 'successfully stored on server' assert_output --partial 'successfully stored on server'
@ -317,9 +318,10 @@ teardown(){
run $ABRA app secret generate "$TEST_APP_DOMAIN" --all run $ABRA app secret generate "$TEST_APP_DOMAIN" --all
assert_success assert_success
run $ABRA app secret ls "$TEST_APP_DOMAIN" --machine run bash -c '$ABRA app secret ls "$TEST_APP_DOMAIN" --machine \
| jq -r ".[] | select(.name==\"test_pass_two\") | .version"'
assert_success assert_success
assert_output --partial '"created-on-server":"true"' assert_output --partial 'v1'
} }
@test "ls: bail if unstaged changes and no --chaos" { @test "ls: bail if unstaged changes and no --chaos" {

View File

@ -64,8 +64,8 @@ teardown(){
run sed -i 's/TYPE=abra-test-recipe:.*/TYPE=abra-test-recipe:0.2.0+1.21.0/g' \ run sed -i 's/TYPE=abra-test-recipe:.*/TYPE=abra-test-recipe:0.2.0+1.21.0/g' \
"$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
assert_success assert_success
run $ABRA app secret generate "$TEST_APP_DOMAIN" --all run $ABRA app secret generate "$TEST_APP_DOMAIN" --all
assert_success assert_success
@ -80,6 +80,7 @@ teardown(){
run sed -i 's/TYPE=abra-test-recipe:.*/TYPE=abra-test-recipe:0.2.0+1.21.0/g' \ run sed -i 's/TYPE=abra-test-recipe:.*/TYPE=abra-test-recipe:0.2.0+1.21.0/g' \
"$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
assert_success
run $ABRA app secret generate "$TEST_APP_DOMAIN" --all run $ABRA app secret generate "$TEST_APP_DOMAIN" --all
assert_success assert_success

View File

@ -18,8 +18,9 @@ setup(){
} }
teardown(){ teardown(){
_undeploy_app
_reset_recipe _reset_recipe
_reset_app
_undeploy_app
} }
@test "validate app argument" { @test "validate app argument" {
@ -123,11 +124,9 @@ teardown(){
assert_success assert_success
assert_output --partial '0.1.0+1.20.0' assert_output --partial '0.1.0+1.20.0'
latestRelease=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | tail -n 1)
run $ABRA app upgrade "$TEST_APP_DOMAIN" --no-input --no-converge-checks run $ABRA app upgrade "$TEST_APP_DOMAIN" --no-input --no-converge-checks
assert_success assert_success
assert_output --partial "$latestRelease" assert_output --partial "$(_latest_release)"
} }
# bats test_tags=slow # bats test_tags=slow
@ -136,11 +135,9 @@ teardown(){
assert_success assert_success
assert_output --partial '0.1.1+1.20.2' assert_output --partial '0.1.1+1.20.2'
latestRelease=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | tail -n 1)
run $ABRA app upgrade "$TEST_APP_DOMAIN" --no-input --no-converge-checks run $ABRA app upgrade "$TEST_APP_DOMAIN" --no-input --no-converge-checks
assert_success assert_success
assert_output --partial "$latestRelease" assert_output --partial "$(_latest_release)"
assert_output --partial 'release notes baz' # 0.2.0+1.21.0 assert_output --partial 'release notes baz' # 0.2.0+1.21.0
refute_output --partial 'release notes bar' # 0.1.1+1.20.2 refute_output --partial 'release notes bar' # 0.1.1+1.20.2
} }
@ -164,11 +161,9 @@ teardown(){
assert_success assert_success
assert_output --partial '0.1.0+1.20.0' assert_output --partial '0.1.0+1.20.0'
latestRelease=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | tail -n 1)
run $ABRA app upgrade "$TEST_APP_DOMAIN" --no-input --no-converge-checks run $ABRA app upgrade "$TEST_APP_DOMAIN" --no-input --no-converge-checks
assert_success assert_success
assert_output --partial "$latestRelease" assert_output --partial "$(_latest_release)"
assert_output --partial 'release notes bar' # 0.1.1+1.20.2 assert_output --partial 'release notes bar' # 0.1.1+1.20.2
assert_output --partial 'release notes baz' # 0.2.0+1.21.0 assert_output --partial 'release notes baz' # 0.2.0+1.21.0
} }

View File

@ -1,5 +1,9 @@
#!/usr/bin/env bash #!/usr/bin/env bash
_latest_release(){
echo $(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | tail -n 1)
}
_fetch_recipe() { _fetch_recipe() {
if [[ ! -d "$ABRA_DIR/recipes/$TEST_RECIPE" ]]; then if [[ ! -d "$ABRA_DIR/recipes/$TEST_RECIPE" ]]; then
run mkdir -p "$ABRA_DIR/recipes" run mkdir -p "$ABRA_DIR/recipes"
@ -19,7 +23,7 @@ _reset_recipe(){
} }
_ensure_latest_version(){ _ensure_latest_version(){
latestRelease=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | tail -n 1) latestRelease=$(_latest_release)
if [ ! $latestRelease = "$1" ]; then if [ ! $latestRelease = "$1" ]; then
echo "expected latest recipe version of '$1', saw: $latestRelease" echo "expected latest recipe version of '$1', saw: $latestRelease"
@ -33,3 +37,15 @@ _ensure_catalogue(){
assert_success assert_success
fi fi
} }
_ensure_env_version(){
run sed -i 's/TYPE=abra-test-recipe:.*/TYPE=abra-test-recipe:$1/g' \
"$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
assert_success
}
_wipe_env_version(){
run sed -i 's/TYPE=abra-test-recipe:.*/TYPE=abra-test-recipe/g' \
"$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
assert_success
}

View File

@ -1,10 +1,17 @@
#!/usr/bin/env bash #!/usr/bin/env bash
setup_file(){
load "$PWD/tests/integration/helpers/common"
_common_setup
_ensure_catalogue
}
setup() { setup() {
load "$PWD/tests/integration/helpers/common" load "$PWD/tests/integration/helpers/common"
_common_setup _common_setup
} }
@test "recipe versions" { @test "recipe versions" {
run $ABRA recipe versions gitea run $ABRA recipe versions gitea
assert_success assert_success
@ -12,11 +19,9 @@ setup() {
} }
@test "local tags used if no catalogue entry" { @test "local tags used if no catalogue entry" {
latestRelease=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | tail -n 1)
run $ABRA recipe versions "$TEST_RECIPE" run $ABRA recipe versions "$TEST_RECIPE"
assert_success assert_success
assert_output --partial "$latestRelease" assert_output --partial "$(_latest_release)"
} }
@test "versions listed in correct order" { @test "versions listed in correct order" {

View File

@ -66,7 +66,6 @@ teardown(){
fi fi
output=$("$ABRA" server ls --machine) output=$("$ABRA" server ls --machine)
run diff \ run diff \
<(jq -S "." <(echo "$output")) \ <(jq -S "." <(echo "$output")) \
<(jq -S "." <(echo '[{"host":"local","name":"default"}]')) <(jq -S "." <(echo '[{"host":"local","name":"default"}]'))