Skip to content

Commit dfdf7af

Browse files
author
Matthew Fisher
committed
fix(cmd): check for invalid probe types
1 parent 1ffbad5 commit dfdf7af

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

cmd/healthchecks_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ func TestHealthchecksUnset(t *testing.T) {
307307
}`)
308308
})
309309

310-
err = cmdr.HealthchecksUnset("foo", "web/cmd", []string{"web/cmd"})
310+
err = cmdr.HealthchecksUnset("foo", "web/cmd", []string{"liveness"})
311311
assert.NoErr(t, err)
312312
assert.Equal(t, testutil.StripProgress(b.String()), `Removing healthchecks... done
313313

parser/healthchecks.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ Options:
164164
probeType := args["<probe-type>"].(string)
165165
probeArgs := args["<args>"].([]string)
166166

167-
if healthcheckType != "liveness" && healthcheckType != "readiness" {
168-
return fmt.Errorf("Invalid healthcheck type. Must be one of: \"liveness\", \"readiness\"")
167+
if err := checkProbeType(healthcheckType); err != nil {
168+
return err
169169
}
170170

171171
// NOTE(bacongobbler): k8s healthchecks use the term "livenessProbe" and "readinessProbe", so let's
@@ -247,6 +247,9 @@ Options:
247247
// NOTE(bacongobbler): k8s healthchecks use the term "livenessProbe" and "readinessProbe", so let's
248248
// add that to the end of the healthcheck type so the controller sees the right probe type
249249
for healthcheck := range healthchecks {
250+
if err := checkProbeType(healthchecks[healthcheck]); err != nil {
251+
return err
252+
}
250253
healthchecks[healthcheck] += "Probe"
251254
}
252255

@@ -275,3 +278,20 @@ func parseHeader(header string) (*api.KVPair, error) {
275278
Value: strings.TrimSpace(headerParts[1]),
276279
}, nil
277280
}
281+
282+
func checkProbeType(probe string) error {
283+
var found bool
284+
probeTypes := []string{
285+
"liveness",
286+
"readiness",
287+
}
288+
for _, ptype := range probeTypes {
289+
if probe == ptype {
290+
found = true
291+
}
292+
}
293+
if !found {
294+
return fmt.Errorf("probe type %s is invalid. Must be one of %s", probe, probeTypes)
295+
}
296+
return nil
297+
}

parser/healthchecks_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ func TestHealthchecks(t *testing.T) {
7070
args: []string{"healthchecks"},
7171
expected: "healthchecks:list",
7272
},
73+
{
74+
args: []string{"healthchecks:set", "alien", "httpGet", "80"},
75+
expected: "probe type alien is invalid. Must be one of [liveness readiness]",
76+
},
77+
{
78+
args: []string{"healthchecks:unset", "alien", "httpGet", "80"},
79+
expected: "probe type alien is invalid. Must be one of [liveness readiness]",
80+
},
7381
}
7482

7583
// For each case, check that calling the route with the arguments

0 commit comments

Comments
 (0)