fix: reject negative process counts when scaling
A process count below zero was accepted and stored verbatim, leaving the scheduler to reject the value long after the command reported success. Counts below zero are now rejected, as are process tuples that omit a process type, across `ps:scale`, the `ps-set-scale` trigger and the `formation` key of `app.json`. A negative count persisted by an earlier version stays readable so it can be corrected with `ps:scale`.
This commit is contained in:
8 files changed
+292
-3
No files matched your search
@@ -90,3 +90,160 @@ func TestDefaultProcessTuplesWithoutProcfile(t *testing.T) {
|
||||
t.Fatalf("expected [web=1], got %v", processTuples)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseProcessTuples(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
processTuples []string
|
||||
expected []Formation
|
||||
expectedError string
|
||||
}{
|
||||
{
|
||||
name: "no process tuples",
|
||||
processTuples: []string{},
|
||||
expected: []Formation{},
|
||||
},
|
||||
{
|
||||
name: "single process type",
|
||||
processTuples: []string{"web=1"},
|
||||
expected: []Formation{{ProcessType: "web", Quantity: 1}},
|
||||
},
|
||||
{
|
||||
name: "multiple process types",
|
||||
processTuples: []string{"web=1", "worker=2"},
|
||||
expected: []Formation{{ProcessType: "web", Quantity: 1}, {ProcessType: "worker", Quantity: 2}},
|
||||
},
|
||||
{
|
||||
name: "trims surrounding whitespace",
|
||||
processTuples: []string{" web = 2 "},
|
||||
expected: []Formation{{ProcessType: "web", Quantity: 2}},
|
||||
},
|
||||
{
|
||||
name: "first specified quantity wins",
|
||||
processTuples: []string{"web=1", "web=3"},
|
||||
expected: []Formation{{ProcessType: "web", Quantity: 1}},
|
||||
},
|
||||
{
|
||||
// persisted formations are parsed without validation so that a negative
|
||||
// quantity written by an older dokku version remains readable
|
||||
name: "negative quantities are parsed",
|
||||
processTuples: []string{"web=-1"},
|
||||
expected: []Formation{{ProcessType: "web", Quantity: -1}},
|
||||
},
|
||||
{
|
||||
name: "missing count",
|
||||
processTuples: []string{"web"},
|
||||
expectedError: "Missing count for process type web",
|
||||
},
|
||||
{
|
||||
name: "non-numeric count",
|
||||
processTuples: []string{"web=one"},
|
||||
expectedError: "Invalid count for process type web",
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
formations, err := parseProcessTuples(testCase.processTuples)
|
||||
if testCase.expectedError != "" {
|
||||
if err == nil {
|
||||
t.Fatalf("expected error %q, got none", testCase.expectedError)
|
||||
}
|
||||
|
||||
if err.Error() != testCase.expectedError {
|
||||
t.Fatalf("expected error %q, got %q", testCase.expectedError, err.Error())
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error parsing process tuples: %v", err)
|
||||
}
|
||||
|
||||
assertFormations(t, formations, testCase.expected)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateFormations(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
processTuples []string
|
||||
expectedError string
|
||||
}{
|
||||
{
|
||||
name: "zero is a valid quantity",
|
||||
processTuples: []string{"web=0"},
|
||||
},
|
||||
{
|
||||
name: "positive quantities are valid",
|
||||
processTuples: []string{"web=1", "worker=10"},
|
||||
},
|
||||
{
|
||||
name: "negative quantity",
|
||||
processTuples: []string{"web=-1"},
|
||||
expectedError: "Invalid count for process type web: value must be zero or greater",
|
||||
},
|
||||
{
|
||||
name: "names the offending process type",
|
||||
processTuples: []string{"web=1", "worker=-2"},
|
||||
expectedError: "Invalid count for process type worker: value must be zero or greater",
|
||||
},
|
||||
{
|
||||
name: "missing process type",
|
||||
processTuples: []string{"=1"},
|
||||
expectedError: "Missing process type for count 1",
|
||||
},
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
err = validateFormations(formations)
|
||||
if testCase.expectedError == "" {
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("expected error %q, got none", testCase.expectedError)
|
||||
}
|
||||
|
||||
if err.Error() != testCase.expectedError {
|
||||
t.Fatalf("expected error %q, got %q", testCase.expectedError, err.Error())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestScaleSetRejectsNegativeQuantity(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", "")
|
||||
|
||||
err = scaleSet(scaleSetInput{
|
||||
appName: "test-app",
|
||||
skipDeploy: true,
|
||||
processTuples: []string{"web=-1"},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected an error scaling to a negative quantity, got none")
|
||||
}
|
||||
|
||||
if err.Error() != "Invalid count for process type web: value must be zero or greater" {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user