|
| 1 | +package health |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/google/go-cmp/cmp" |
| 5 | + corev1 "k8s.io/api/core/v1" |
| 6 | + "karto/analyzer/health/podhealth" |
| 7 | + "karto/testutils" |
| 8 | + "karto/types" |
| 9 | + "reflect" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAnalyze(t *testing.T) { |
| 14 | + type args struct { |
| 15 | + clusterState ClusterState |
| 16 | + } |
| 17 | + type mocks struct { |
| 18 | + podHealth []mockPodHealthAnalyzerCall |
| 19 | + } |
| 20 | + k8sPod1 := testutils.NewPodBuilder().WithContainerStatus(true, false, 0).Build() |
| 21 | + k8sPod2 := testutils.NewPodBuilder().WithContainerStatus(true, false, 0). |
| 22 | + WithContainerStatus(false, false, 0).Build() |
| 23 | + podRef1 := types.PodRef{Name: k8sPod1.Name, Namespace: k8sPod1.Namespace} |
| 24 | + podRef2 := types.PodRef{Name: k8sPod2.Name, Namespace: k8sPod2.Namespace} |
| 25 | + podHealth1 := &types.PodHealth{Pod: podRef1, Containers: 1, ContainersRunning: 1, ContainersReady: 0, |
| 26 | + ContainersWithoutRestart: 1} |
| 27 | + podHealth2 := &types.PodHealth{Pod: podRef2, Containers: 2, ContainersRunning: 1, ContainersReady: 0, |
| 28 | + ContainersWithoutRestart: 2} |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + mocks mocks |
| 32 | + args args |
| 33 | + expectedAnalysisResult AnalysisResult |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "delegates to sub-analyzers and merges results", |
| 37 | + mocks: mocks{ |
| 38 | + podHealth: []mockPodHealthAnalyzerCall{ |
| 39 | + { |
| 40 | + args: mockPodHealthAnalyzerCallArgs{ |
| 41 | + pod: k8sPod1, |
| 42 | + }, |
| 43 | + returnValue: podHealth1, |
| 44 | + }, |
| 45 | + { |
| 46 | + args: mockPodHealthAnalyzerCallArgs{ |
| 47 | + pod: k8sPod2, |
| 48 | + }, |
| 49 | + returnValue: podHealth2, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + args: args{ |
| 54 | + clusterState: ClusterState{ |
| 55 | + Pods: []*corev1.Pod{k8sPod1, k8sPod2}, |
| 56 | + }, |
| 57 | + }, |
| 58 | + expectedAnalysisResult: AnalysisResult{ |
| 59 | + Pods: []*types.PodHealth{podHealth1, podHealth2}, |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | + for _, tt := range tests { |
| 64 | + t.Run(tt.name, func(t *testing.T) { |
| 65 | + podHealthAnalyzer := createMockPodHealthAnalyzer(t, tt.mocks.podHealth) |
| 66 | + analyzer := NewAnalyzer(podHealthAnalyzer) |
| 67 | + analysisResult := analyzer.Analyze(tt.args.clusterState) |
| 68 | + if diff := cmp.Diff(tt.expectedAnalysisResult, analysisResult); diff != "" { |
| 69 | + t.Errorf("Analyze() result mismatch (-want +got):\n%s", diff) |
| 70 | + } |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +type mockPodHealthAnalyzerCallArgs struct { |
| 76 | + pod *corev1.Pod |
| 77 | +} |
| 78 | + |
| 79 | +type mockPodHealthAnalyzerCall struct { |
| 80 | + args mockPodHealthAnalyzerCallArgs |
| 81 | + returnValue *types.PodHealth |
| 82 | +} |
| 83 | + |
| 84 | +type mockPodHealthAnalyzer struct { |
| 85 | + t *testing.T |
| 86 | + calls []mockPodHealthAnalyzerCall |
| 87 | +} |
| 88 | + |
| 89 | +func (mock mockPodHealthAnalyzer) Analyze(pod *corev1.Pod) *types.PodHealth { |
| 90 | + for _, call := range mock.calls { |
| 91 | + if reflect.DeepEqual(call.args.pod, pod) { |
| 92 | + return call.returnValue |
| 93 | + } |
| 94 | + } |
| 95 | + mock.t.Fatalf("mockPodHealthAnalyzer was called with unexpected arguments:\n\tpod: %s\n", pod) |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +func createMockPodHealthAnalyzer(t *testing.T, calls []mockPodHealthAnalyzerCall) podhealth.Analyzer { |
| 100 | + return mockPodHealthAnalyzer{ |
| 101 | + t: t, |
| 102 | + calls: calls, |
| 103 | + } |
| 104 | +} |
0 commit comments