Merge pull request #70753 from logicalhan/healthz-test

fix healthz checkerNames test so that it tests against the expected output

Kubernetes-commit: daea190bee5972fe3960cf363a2992d11e0d21d9
This commit is contained in:
Kubernetes Publisher 2018-11-09 14:31:09 -08:00
commit ace16cbf26
2 changed files with 40 additions and 12 deletions

View File

@ -112,7 +112,7 @@ func InstallPathHandler(mux mux, path string, checks ...HealthzChecker) {
checks = []HealthzChecker{PingHealthz}
}
glog.V(5).Info("Installing healthz checkers:", strings.Join(checkerNames(checks...), ", "))
glog.V(5).Info("Installing healthz checkers:", formatQuoted(checkerNames(checks...)...))
mux.Handle(path, handleRootHealthz(checks...))
for _, check := range checks {
@ -187,14 +187,20 @@ func adaptCheckToHandler(c func(r *http.Request) error) http.HandlerFunc {
// checkerNames returns the names of the checks in the same order as passed in.
func checkerNames(checks ...HealthzChecker) []string {
if len(checks) > 0 {
// accumulate the names of checks for printing them out.
checkerNames := make([]string, 0, len(checks))
for _, check := range checks {
// quote the Name so we can disambiguate
checkerNames = append(checkerNames, fmt.Sprintf("%q", check.Name()))
}
return checkerNames
// accumulate the names of checks for printing them out.
checkerNames := make([]string, 0, len(checks))
for _, check := range checks {
checkerNames = append(checkerNames, check.Name())
}
return nil
return checkerNames
}
// formatQuoted returns a formatted string of the health check names,
// preserving the order passed in.
func formatQuoted(names ...string) string {
quoted := make([]string, 0, len(names))
for _, name := range names {
quoted = append(quoted, fmt.Sprintf("%q", name))
}
return strings.Join(quoted, ",")
}

View File

@ -148,10 +148,32 @@ func TestCheckerNames(t *testing.T) {
for _, tc := range testCases {
result := checkerNames(tc.have...)
t.Run(tc.desc, func(t *testing.T) {
if reflect.DeepEqual(tc.want, result) {
if !reflect.DeepEqual(tc.want, result) {
t.Errorf("want %#v, got %#v", tc.want, result)
}
})
}
}
func TestFormatQuoted(t *testing.T) {
n1 := "n1"
n2 := "n2"
testCases := []struct {
desc string
names []string
expected string
}{
{"empty", []string{}, ""},
{"single name", []string{n1}, "\"n1\""},
{"two names", []string{n1, n2}, "\"n1\",\"n2\""},
{"two names, reverse order", []string{n2, n1}, "\"n2\",\"n1\""},
}
for _, tc := range testCases {
result := formatQuoted(tc.names...)
t.Run(tc.desc, func(t *testing.T) {
if result != tc.expected {
t.Errorf("expected %#v, got %#v", tc.expected, result)
}
})
}
}