...
1
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 }
78
View as plain text