k8s.io/kubernetes@v1.29.3/pkg/client/conditions/conditions_test.go (about) 1 /* 2 Copyright 2023 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 conditions 18 19 import ( 20 "testing" 21 22 corev1 "k8s.io/api/core/v1" 23 "k8s.io/apimachinery/pkg/watch" 24 ) 25 26 func TestPodRunning(t *testing.T) { 27 tests := []struct { 28 name string 29 event watch.Event 30 want bool 31 wantErr bool 32 }{ 33 { 34 name: "Watch type is deleted", 35 event: watch.Event{ 36 Type: watch.Deleted, 37 }, 38 want: false, 39 wantErr: true, 40 }, 41 { 42 name: "Pod Status type is PodRunning", 43 event: watch.Event{ 44 Type: watch.Added, 45 Object: &corev1.Pod{ 46 Status: corev1.PodStatus{ 47 Phase: corev1.PodRunning, 48 }, 49 }, 50 }, 51 want: true, 52 wantErr: false, 53 }, 54 { 55 name: "Pod Status is PodFailed", 56 event: watch.Event{ 57 Type: watch.Added, 58 Object: &corev1.Pod{ 59 Status: corev1.PodStatus{ 60 Phase: corev1.PodFailed, 61 }, 62 }, 63 }, 64 want: false, 65 wantErr: true, 66 }, 67 { 68 name: "Object type is not pod", 69 event: watch.Event{ 70 Type: watch.Added, 71 Object: &corev1.Node{}, 72 }, 73 want: false, 74 wantErr: false, 75 }, 76 } 77 for _, tt := range tests { 78 t.Run(tt.name, func(t *testing.T) { 79 got, err := PodRunning(tt.event) 80 if (err != nil) != tt.wantErr { 81 t.Errorf("PodRunning() error = %v, wantErr %v", err, tt.wantErr) 82 return 83 } 84 if got != tt.want { 85 t.Errorf("PodRunning() = %v, want %v", got, tt.want) 86 } 87 }) 88 } 89 } 90 91 func TestPodCompleted(t *testing.T) { 92 tests := []struct { 93 name string 94 event watch.Event 95 want bool 96 wantErr bool 97 }{ 98 { 99 name: "Watch type is deleted", 100 event: watch.Event{ 101 Type: watch.Deleted, 102 }, 103 want: false, 104 wantErr: true, 105 }, 106 { 107 name: "Pod Status is PodSucceeded", 108 event: watch.Event{ 109 Type: watch.Added, 110 Object: &corev1.Pod{ 111 Status: corev1.PodStatus{ 112 Phase: corev1.PodSucceeded, 113 }, 114 }, 115 }, 116 want: true, 117 wantErr: false, 118 }, 119 { 120 name: "Pod Status is PodRunning", 121 event: watch.Event{ 122 Type: watch.Added, 123 Object: &corev1.Pod{ 124 Status: corev1.PodStatus{ 125 Phase: corev1.PodRunning, 126 }, 127 }, 128 }, 129 want: false, 130 wantErr: false, 131 }, 132 { 133 name: "Object type is not pod", 134 event: watch.Event{ 135 Type: watch.Added, 136 Object: &corev1.Node{}, 137 }, 138 want: false, 139 wantErr: false, 140 }, 141 } 142 for _, tt := range tests { 143 t.Run(tt.name, func(t *testing.T) { 144 got, err := PodCompleted(tt.event) 145 if (err != nil) != tt.wantErr { 146 t.Errorf("PodCompleted() error = %v, wantErr %v", err, tt.wantErr) 147 return 148 } 149 if got != tt.want { 150 t.Errorf("PodCompleted() = %v, want %v", got, tt.want) 151 } 152 }) 153 } 154 }