feat: add --clear and --replace flags to ps:scale

The `ps:scale` command merges the specified process types into the existing formation, so replacing an entire formation from the CLI required reading the current scale, diffing it, and issuing a second call to zero out each unlisted process type. The `--replace` flag treats the specified process types as the whole formation and sets the process count for every unspecified process type to zero, while the `--clear` flag resets the formation to the scale a newly created app has. The two flags cannot be combined, `--replace` requires at least one process type, and `--clear` accepts none. Process types cleared this way are now scaled down as part of the same command rather than being left running with a stale formation.
This commit is contained in:
Jose Diaz-Gonzalez committed 2026-09-02 14:55:16 -04:00
1 parent 63351209f8
commit db45f8608b
10 files changed
+391 -21

No files matched your search

+92
View File
@@ -0,0 +1,92 @@
package ps
import (
"os"
"testing"
)
// assertFormations fails the test when the specified formations do not match
// the expected process type and quantity pairs, in order
func assertFormations(t *testing.T, formations FormationSlice, expected []Formation) {
t.Helper()
if len(formations) != len(expected) {
t.Fatalf("expected %d formations, got %d", len(expected), len(formations))
}
for i, formation := range formations {
if formation.ProcessType != expected[i].ProcessType || formation.Quantity != expected[i].Quantity {
t.Fatalf("expected %s=%d at index %d, got %s=%d", expected[i].ProcessType, expected[i].Quantity, i, formation.ProcessType, formation.Quantity)
}
}
}
func TestAppendClearedFormations(t *testing.T) {
testCases := []struct {
name string
processTuples []string
existingTuple []string
expected []Formation
}{
{
name: "no existing formations",
processTuples: []string{"web=1"},
existingTuple: []string{},
expected: []Formation{{ProcessType: "web", Quantity: 1}},
},
{
name: "zeroes out unspecified process types",
processTuples: []string{"web=1"},
existingTuple: []string{"web=2", "worker=3"},
expected: []Formation{{ProcessType: "web", Quantity: 1}, {ProcessType: "worker", Quantity: 0}},
},
{
name: "does not overwrite specified process types",
processTuples: []string{"web=1", "worker=2"},
existingTuple: []string{"web=4", "worker=5"},
expected: []Formation{{ProcessType: "web", Quantity: 1}, {ProcessType: "worker", Quantity: 2}},
},
{
name: "zeroes out every process type when none are specified",
processTuples: []string{},
existingTuple: []string{"web=2", "worker=3"},
expected: []Formation{{ProcessType: "web", Quantity: 0}, {ProcessType: "worker", Quantity: 0}},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
formations, err := parseProcessTuples(testCase.processTuples)
if err != nil {
t.Fatalf("unexpected error parsing process tuples: %v", err)
}
existingFormations, err := parseProcessTuples(testCase.existingTuple)
if err != nil {
t.Fatalf("unexpected error parsing existing process tuples: %v", err)
}
assertFormations(t, appendClearedFormations(formations, existingFormations), testCase.expected)
})
}
}
func TestDefaultProcessTuplesWithoutProcfile(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "ps-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
t.Setenv("DOKKU_LIB_ROOT", tmpDir)
t.Setenv("DOKKU_PID", "")
processTuples, err := defaultProcessTuples("test-app")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(processTuples) != 1 || processTuples[0] != "web=1" {
t.Fatalf("expected [web=1], got %v", processTuples)
}
}