From dded26625bf3f1c6e86b2303299689ad58de1cc1 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 26 Jun 2022 14:31:08 -0400 Subject: [PATCH] fix: always respect app.json formations in scale settings Without this change, it was possible would end up in a state where we might still try to scale processes up that no longer exists. Closes #5205 --- plugins/app-json/functions.go | 2 +- tests/apps/python/app-5205a.json | 10 ++++ tests/apps/python/app-5205b.json | 10 ++++ tests/unit/app-json-3.bats | 84 ++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 tests/apps/python/app-5205a.json create mode 100644 tests/apps/python/app-5205b.json create mode 100644 tests/unit/app-json-3.bats diff --git a/plugins/app-json/functions.go b/plugins/app-json/functions.go index 9c9e13b2a..97e97cb64 100644 --- a/plugins/app-json/functions.go +++ b/plugins/app-json/functions.go @@ -497,7 +497,7 @@ func setScale(appName string, image string) error { } skipDeploy := true - clearExisting := false + clearExisting := true args := []string{appName, strconv.FormatBool(skipDeploy), strconv.FormatBool(clearExisting)} for processType, formation := range appJSON.Formation { if formation.Quantity != nil { diff --git a/tests/apps/python/app-5205a.json b/tests/apps/python/app-5205a.json new file mode 100644 index 000000000..268a27a4c --- /dev/null +++ b/tests/apps/python/app-5205a.json @@ -0,0 +1,10 @@ +{ + "formation": { + "web": { + "quantity": 1 + }, + "worker": { + "quantity": 1 + } + } +} diff --git a/tests/apps/python/app-5205b.json b/tests/apps/python/app-5205b.json new file mode 100644 index 000000000..b58838b9e --- /dev/null +++ b/tests/apps/python/app-5205b.json @@ -0,0 +1,10 @@ +{ + "formation": { + "web": { + "quantity": 1 + }, + "cron": { + "quantity": 1 + } + } +} diff --git a/tests/unit/app-json-3.bats b/tests/unit/app-json-3.bats new file mode 100644 index 000000000..ead921aed --- /dev/null +++ b/tests/unit/app-json-3.bats @@ -0,0 +1,84 @@ +#!/usr/bin/env bats +load test_helper + +setup() { + global_setup + create_app +} + +teardown() { + destroy_app + global_teardown +} + +@test "(app-json) persist scale" { + local CUSTOM_TMP=$(mktemp -d "/tmp/dokku.me.XXXXX") + trap 'popd &>/dev/null || true; rm -rf "$TMP"' INT TERM + + CUSTOM_TMP="$CUSTOM_TMP" run deploy_app python dokku@dokku.me:$TEST_APP persist_scale_callback_a + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku --quiet ps:scale $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "web: 1" + assert_output_contains "worker: 1" + assert_output_contains "cron: 0" 0 + + run persist_scale_callback_b "$TEST_APP" "$CUSTOM_TMP" + echo "output: $output" + echo "status: $status" + assert_success + + run git -C "$CUSTOM_TMP" add app.json Procfile + echo "output: $output" + echo "status: $status" + assert_success + + run git -C "$CUSTOM_TMP" commit -m 'Update scaling parameters' + echo "output: $output" + echo "status: $status" + assert_success + + run git -C "$CUSTOM_TMP" push target master:master + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku --quiet ps:scale $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "web: 1" + assert_output_contains "worker: 1" 0 + assert_output_contains "worker: 0" 0 + assert_output_contains "cron: 1" 1 +} + +persist_scale_callback_a() { + local APP="$1" + local APP_REPO_DIR="$2" + [[ -z "$APP" ]] && local APP="$TEST_APP" + + rm "$APP_REPO_DIR/Procfile" + touch "$APP_REPO_DIR/Procfile" + echo "web: python3 web.py" >>"$APP_REPO_DIR/Procfile" + echo "worker: python3 worker.py" >>"$APP_REPO_DIR/Procfile" + mv "$APP_REPO_DIR/app-5205a.json" "$APP_REPO_DIR/app.json" +} + + +persist_scale_callback_b() { + local APP="$1" + local APP_REPO_DIR="$2" + [[ -z "$APP" ]] && local APP="$TEST_APP" + + rm "$APP_REPO_DIR/Procfile" + touch "$APP_REPO_DIR/Procfile" + echo "web: python3 web.py" >>"$APP_REPO_DIR/Procfile" + echo "cron: python3 worker.py" >>"$APP_REPO_DIR/Procfile" + mv "$APP_REPO_DIR/app-5205b.json" "$APP_REPO_DIR/app.json" +}