diff --git a/probes_test.go b/probes_test.go index 059c288..c614145 100644 --- a/probes_test.go +++ b/probes_test.go @@ -15,7 +15,100 @@ func getStatusFromEndpoint(t *testing.T, client *http.Client, endpoint string) i return resp.StatusCode } -func TestKubeprobes(t *testing.T) { +func TestValidation(t *testing.T) { + var ( + live = NewStatefulProbe() + ready = NewStatefulProbe() + ) + + tests := map[string]struct { + opts []Option + expectedError bool + }{ + "no modifications and no error": { + opts: []Option{ + WithLivenessStatefulProbes(live), + WithReadinessStatefulProbes(ready), + }, + }, + "modifications and no error": { + opts: []Option{ + WithLivenessStatefulProbes(live), + WithReadinessStatefulProbes(ready), + WithLivenessPath("/livez"), + WithReadinessPath("/readyz"), + }, + }, + "missing liveness probes": { + opts: []Option{ + WithReadinessStatefulProbes(ready), + }, + expectedError: true, + }, + "missing readiness probes": { + opts: []Option{ + WithLivenessStatefulProbes(live), + }, + expectedError: true, + }, + "liveness probe path empty": { + opts: []Option{ + WithLivenessStatefulProbes(live), + WithReadinessStatefulProbes(ready), + WithLivenessPath(""), + }, + expectedError: true, + }, + "readiness probe path empty": { + opts: []Option{ + WithLivenessStatefulProbes(live), + WithReadinessStatefulProbes(ready), + WithReadinessPath(""), + }, + expectedError: true, + }, + "liveness probe path does not start with slash": { + opts: []Option{ + WithLivenessStatefulProbes(live), + WithReadinessStatefulProbes(ready), + WithLivenessPath("livez"), + }, + expectedError: true, + }, + "readiness probe path does not start with slash": { + opts: []Option{ + WithLivenessStatefulProbes(live), + WithReadinessStatefulProbes(ready), + WithReadinessPath("readyz"), + }, + expectedError: true, + }, + "liveness and readiness probe paths are equal": { + opts: []Option{ + WithLivenessStatefulProbes(live), + WithReadinessStatefulProbes(ready), + WithLivenessPath("/check"), + WithReadinessPath("/check"), + }, + expectedError: true, + }, + } + + for name, tc := range tests { + name, tc := name, tc + t.Run(name, func(t *testing.T) { + _, err := New(tc.opts...) + switch { + case err == nil && tc.expectedError: + t.Error("expected error, but no error was returned") + case err != nil && !tc.expectedError: + t.Errorf("expected no error but got %v", err) + } + }) + } +} + +func TestHandler(t *testing.T) { live, ready := NewStatefulProbe(), NewStatefulProbe() tests := map[string]struct { @@ -50,10 +143,13 @@ func TestKubeprobes(t *testing.T) { }, } - kp, _ := New( + kp, err := New( WithLivenessStatefulProbes(live), WithReadinessStatefulProbes(ready), ) + if err != nil { + t.Errorf("expected no error, got %v", err) + } srv := httptest.NewServer(kp) defer srv.Close()