@@ -15,3 +15,220 @@ limitations under the License.
1515*/
1616
1717package controller
18+
19+ import (
20+ "reflect"
21+ "testing"
22+ "time"
23+
24+ "k8s.io/apimachinery/pkg/runtime"
25+ "sigs.k8s.io/controller-runtime/pkg/client/fake"
26+
27+ corev1 "k8s.io/api/core/v1"
28+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+ ctrl "sigs.k8s.io/controller-runtime"
30+
31+ v1 "github.com/kernel-controller/api/v1"
32+ )
33+
34+ func TestNameFromInvolvedObject (t * testing.T ) {
35+ testPod := & corev1.Pod {
36+ ObjectMeta : metav1.ObjectMeta {
37+ Name : "foo" ,
38+ Namespace : "default" ,
39+ Labels : map [string ]string {
40+ KernelNameLabel : "foo" ,
41+ },
42+ },
43+ }
44+
45+ podEvent := & corev1.Event {
46+ ObjectMeta : metav1.ObjectMeta {
47+ Name : "pod-event" ,
48+ },
49+ InvolvedObject : corev1.ObjectReference {
50+ Kind : "Pod" ,
51+ Name : "foo" ,
52+ Namespace : "default" ,
53+ },
54+ }
55+
56+ tests := []struct {
57+ name string
58+ event * corev1.Event
59+ expectedName string
60+ }{
61+ {
62+ name : "pod event" ,
63+ event : podEvent ,
64+ expectedName : "foo" ,
65+ },
66+ }
67+
68+ objects := []runtime.Object {testPod }
69+ for _ , test := range tests {
70+ t .Run (test .name , func (t * testing.T ) {
71+ c := fake .NewFakeClient (objects ... )
72+ kernelName , err := kernelNameFromInvolvedObject (c , & test .event .InvolvedObject )
73+ if err != nil {
74+ t .Fatalf ("Unexpected error: %v" , err )
75+ }
76+ if kernelName != test .expectedName {
77+ t .Fatalf ("Got %v, Expected %v" , kernelName , test .expectedName )
78+ }
79+ })
80+ }
81+ }
82+
83+ func TestCreateKernelStatus (t * testing.T ) {
84+ tests := []struct {
85+ name string
86+ currentKernel v1.Kernel
87+ pod corev1.Pod
88+ expectedStatus v1.KernelStatus
89+ }{
90+ {
91+ name : "KernelStatusInitialization" ,
92+ currentKernel : v1.Kernel {
93+ ObjectMeta : metav1.ObjectMeta {
94+ Name : "foo" ,
95+ Namespace : "default" ,
96+ },
97+ Status : v1.KernelStatus {},
98+ },
99+ pod : corev1.Pod {},
100+ expectedStatus : v1.KernelStatus {
101+ Conditions : []v1.KernelCondition {},
102+ ContainerState : corev1.ContainerState {},
103+ },
104+ },
105+ {
106+ name : "KernelContainerState" ,
107+ currentKernel : v1.Kernel {
108+ ObjectMeta : metav1.ObjectMeta {
109+ Name : "foo" ,
110+ Namespace : "default" ,
111+ },
112+ Status : v1.KernelStatus {},
113+ },
114+ pod : corev1.Pod {
115+ ObjectMeta : metav1.ObjectMeta {
116+ Name : "foo" ,
117+ Namespace : "default" ,
118+ },
119+ Status : corev1.PodStatus {
120+ ContainerStatuses : []corev1.ContainerStatus {
121+ {
122+ Name : "foo" ,
123+ State : corev1.ContainerState {
124+ Running : & corev1.ContainerStateRunning {
125+ StartedAt : metav1.Time {},
126+ },
127+ },
128+ },
129+ },
130+ },
131+ },
132+ expectedStatus : v1.KernelStatus {
133+ Conditions : []v1.KernelCondition {},
134+ ContainerState : corev1.ContainerState {
135+ Running : & corev1.ContainerStateRunning {
136+ StartedAt : metav1.Time {},
137+ },
138+ },
139+ },
140+ },
141+ {
142+ name : "mirroringPodConditions" ,
143+ pod : corev1.Pod {
144+ ObjectMeta : metav1.ObjectMeta {
145+ Name : "foo" ,
146+ Namespace : "default" ,
147+ },
148+ Status : corev1.PodStatus {
149+ Conditions : []corev1.PodCondition {
150+ {
151+ Type : "Running" ,
152+ LastProbeTime : metav1 .Date (2024 , time .Month (12 ), 30 , 1 , 10 , 30 , 0 , time .UTC ),
153+ LastTransitionTime : metav1 .Date (2024 , time .Month (12 ), 30 , 1 , 10 , 30 , 0 , time .UTC ),
154+ },
155+ {
156+ Type : "Waiting" ,
157+ LastProbeTime : metav1 .Date (2024 , time .Month (12 ), 30 , 1 , 10 , 30 , 0 , time .UTC ),
158+ LastTransitionTime : metav1 .Date (2024 , time .Month (12 ), 30 , 1 , 10 , 30 , 0 , time .UTC ),
159+ Reason : "PodInitializing" ,
160+ },
161+ },
162+ },
163+ },
164+ expectedStatus : v1.KernelStatus {
165+ Conditions : []v1.KernelCondition {
166+ {
167+ Type : "Running" ,
168+ LastProbeTime : metav1 .Date (2024 , time .Month (12 ), 30 , 1 , 10 , 30 , 0 , time .UTC ),
169+ LastTransitionTime : metav1 .Date (2024 , time .Month (12 ), 30 , 1 , 10 , 30 , 0 , time .UTC ),
170+ },
171+ {
172+ Type : "Waiting" ,
173+ LastProbeTime : metav1 .Date (2024 , time .Month (12 ), 30 , 1 , 10 , 30 , 0 , time .UTC ),
174+ LastTransitionTime : metav1 .Date (2024 , time .Month (12 ), 30 , 1 , 10 , 30 , 0 , time .UTC ),
175+ Reason : "PodInitializing" ,
176+ },
177+ },
178+ ContainerState : corev1.ContainerState {},
179+ },
180+ },
181+ {
182+ name : "unschedulablePod" ,
183+ pod : corev1.Pod {
184+ ObjectMeta : metav1.ObjectMeta {
185+ Name : "foo" ,
186+ Namespace : "default" ,
187+ },
188+ Status : corev1.PodStatus {
189+ Conditions : []corev1.PodCondition {
190+ {
191+ Type : "PodScheduled" ,
192+ LastProbeTime : metav1 .Date (2024 , time .Month (4 ), 21 , 1 , 10 , 30 , 0 , time .UTC ),
193+ LastTransitionTime : metav1 .Date (2024 , time .Month (4 ), 21 , 1 , 10 , 30 , 0 , time .UTC ),
194+ Message : "0/1 nodes are available: 1 Insufficient cpu." ,
195+ Status : "false" ,
196+ Reason : "Unschedulable" ,
197+ },
198+ },
199+ },
200+ },
201+ expectedStatus : v1.KernelStatus {
202+ Conditions : []v1.KernelCondition {
203+ {
204+ Type : "PodScheduled" ,
205+ LastProbeTime : metav1 .Date (2024 , time .Month (4 ), 21 , 1 , 10 , 30 , 0 , time .UTC ),
206+ LastTransitionTime : metav1 .Date (2024 , time .Month (4 ), 21 , 1 , 10 , 30 , 0 , time .UTC ),
207+ Message : "0/1 nodes are available: 1 Insufficient cpu." ,
208+ Status : "false" ,
209+ Reason : "Unschedulable" ,
210+ },
211+ },
212+ ContainerState : corev1.ContainerState {},
213+ },
214+ },
215+ }
216+
217+ for _ , test := range tests {
218+ t .Run (test .name , func (t * testing.T ) {
219+ r := createMockReconciler ()
220+ req := ctrl.Request {}
221+ status := r .createKernelStatus (& test .currentKernel , & test .pod , req )
222+ if ! reflect .DeepEqual (status , test .expectedStatus ) {
223+ t .Errorf ("\n Expect: %v; \n Output: %v" , test .expectedStatus , status )
224+ }
225+ })
226+ }
227+ }
228+
229+ func createMockReconciler () * KernelReconciler {
230+ return & KernelReconciler {
231+ Scheme : runtime .NewScheme (),
232+ Log : ctrl .Log ,
233+ }
234+ }
0 commit comments