k8s.io/kubernetes@v1.29.3/pkg/registry/core/componentstatus/validator_test.go (about) 1 /* 2 Copyright 2014 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package componentstatus 18 19 import ( 20 "errors" 21 "fmt" 22 "testing" 23 24 "k8s.io/kubernetes/pkg/probe" 25 ) 26 27 func matchError(data []byte) error { 28 if string(data) != "bar" { 29 return errors.New("match error") 30 } 31 return nil 32 } 33 34 func TestValidate(t *testing.T) { 35 tests := []struct { 36 probeResult probe.Result 37 probeData string 38 probeErr error 39 40 expectResult probe.Result 41 expectData string 42 expectErr bool 43 44 validator ValidatorFn 45 }{ 46 {probe.Unknown, "", fmt.Errorf("probe error"), probe.Unknown, "", true, nil}, 47 {probe.Failure, "", nil, probe.Failure, "", false, nil}, 48 {probe.Success, "foo", nil, probe.Failure, "foo", true, matchError}, 49 {probe.Success, "foo", nil, probe.Success, "foo", false, nil}, 50 } 51 52 s := HttpServer{Addr: "foo.com", Port: 8080, Path: "/healthz"} 53 54 for _, test := range tests { 55 fakeProber := &fakeHttpProber{ 56 result: test.probeResult, 57 body: test.probeData, 58 err: test.probeErr, 59 } 60 61 s.Validate = test.validator 62 s.Prober = fakeProber 63 result, data, err := s.DoServerCheck() 64 if test.expectErr && err == nil { 65 t.Error("unexpected non-error") 66 } 67 if !test.expectErr && err != nil { 68 t.Errorf("unexpected error: %v", err) 69 } 70 if data != test.expectData { 71 t.Errorf("expected %s, got %s", test.expectData, data) 72 } 73 if result != test.expectResult { 74 t.Errorf("expected %s, got %s", test.expectResult, result) 75 } 76 } 77 }