k8s.io/kubernetes@v1.29.3/pkg/kubelet/prober/results/results_manager_test.go (about) 1 /* 2 Copyright 2015 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 results 18 19 import ( 20 "testing" 21 "time" 22 23 "github.com/stretchr/testify/assert" 24 25 corev1 "k8s.io/api/core/v1" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 "k8s.io/apimachinery/pkg/util/wait" 28 kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" 29 ) 30 31 func TestCacheOperations(t *testing.T) { 32 m := NewManager() 33 34 unsetID := kubecontainer.ContainerID{Type: "test", ID: "unset"} 35 setID := kubecontainer.ContainerID{Type: "test", ID: "set"} 36 37 _, found := m.Get(unsetID) 38 assert.False(t, found, "unset result found") 39 40 m.Set(setID, Success, &corev1.Pod{}) 41 result, found := m.Get(setID) 42 assert.True(t, result == Success, "set result") 43 assert.True(t, found, "set result found") 44 45 m.Remove(setID) 46 _, found = m.Get(setID) 47 assert.False(t, found, "removed result found") 48 } 49 50 func TestUpdates(t *testing.T) { 51 m := NewManager() 52 53 pod := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "test-pod"}} 54 fooID := kubecontainer.ContainerID{Type: "test", ID: "foo"} 55 barID := kubecontainer.ContainerID{Type: "test", ID: "bar"} 56 57 expectUpdate := func(expected Update, msg string) { 58 select { 59 case u := <-m.Updates(): 60 if expected != u { 61 t.Errorf("Expected update %v, received %v: %s", expected, u, msg) 62 } 63 case <-time.After(wait.ForeverTestTimeout): 64 t.Errorf("Timed out waiting for update %v: %s", expected, msg) 65 } 66 } 67 68 expectNoUpdate := func(msg string) { 69 // NOTE: Since updates are accumulated asynchronously, this method is not guaranteed to fail 70 // when it should. In the event it misses a failure, the following calls to expectUpdate should 71 // still fail. 72 select { 73 case u := <-m.Updates(): 74 t.Errorf("Unexpected update %v: %s", u, msg) 75 default: 76 // Pass 77 } 78 } 79 80 // New result should always push an update. 81 m.Set(fooID, Success, pod) 82 expectUpdate(Update{fooID, Success, pod.UID}, "new success") 83 84 m.Set(barID, Failure, pod) 85 expectUpdate(Update{barID, Failure, pod.UID}, "new failure") 86 87 // Unchanged results should not send an update. 88 m.Set(fooID, Success, pod) 89 expectNoUpdate("unchanged foo") 90 91 m.Set(barID, Failure, pod) 92 expectNoUpdate("unchanged bar") 93 94 // Changed results should send an update. 95 m.Set(fooID, Failure, pod) 96 expectUpdate(Update{fooID, Failure, pod.UID}, "changed foo") 97 98 m.Set(barID, Success, pod) 99 expectUpdate(Update{barID, Success, pod.UID}, "changed bar") 100 } 101 102 func TestResult_ToPrometheusType(t *testing.T) { 103 tests := []struct { 104 name string 105 result Result 106 expected float64 107 }{ 108 { 109 name: "result is Success", 110 result: Success, 111 expected: 0, 112 }, 113 { 114 name: "result is Failure", 115 result: Failure, 116 expected: 1, 117 }, 118 { 119 name: "result is other", 120 result: 123, 121 expected: -1, 122 }, 123 } 124 for _, test := range tests { 125 t.Run(test.name, func(t *testing.T) { 126 if got := test.result.ToPrometheusType(); got != test.expected { 127 t.Errorf("Result.ToPrometheusType() = %v, expected %v", got, test.expected) 128 } 129 }) 130 } 131 } 132 133 func TestResult_String(t *testing.T) { 134 tests := []struct { 135 name string 136 result Result 137 expected string 138 }{ 139 { 140 name: "result is Success", 141 result: Success, 142 expected: "Success", 143 }, 144 { 145 name: "result is Failure", 146 result: Failure, 147 expected: "Failure", 148 }, 149 { 150 name: "result is other", 151 result: -123, 152 expected: "UNKNOWN", 153 }, 154 } 155 for _, test := range tests { 156 t.Run(test.name, func(t *testing.T) { 157 if got := test.result.String(); got != test.expected { 158 t.Errorf("Result.String() = %v, expected %v", got, test.expected) 159 } 160 }) 161 } 162 }