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

Kubernetes-commit: 0623f630ab37ad75961bf836195e190e6bcf560e
This commit is contained in:
Han Kang 2018-11-07 09:26:11 -08:00 committed by Kubernetes Publisher
parent c8132c133c
commit 85a1725c91
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)
}
})
}
}