|
| 1 | +package deployment |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/google/go-cmp/cmp" |
| 7 | + |
| 8 | + appsv1 "k8s.io/api/apps/v1" |
| 9 | + corev1 "k8s.io/api/core/v1" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/utils/ptr" |
| 12 | + |
| 13 | + operatorv1 "github.com/openshift/api/operator/v1" |
| 14 | +) |
| 15 | + |
| 16 | +func TestDeploymentProgressingCondition(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + deploy *appsv1.Deployment |
| 20 | + expected operatorv1.OperatorCondition |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "active rollout, not all pods updated", |
| 24 | + deploy: &appsv1.Deployment{ |
| 25 | + ObjectMeta: metav1.ObjectMeta{Name: "web", Namespace: "ns"}, |
| 26 | + Spec: appsv1.DeploymentSpec{Replicas: ptr.To[int32](3)}, |
| 27 | + Status: appsv1.DeploymentStatus{ |
| 28 | + UpdatedReplicas: 1, |
| 29 | + AvailableReplicas: 2, |
| 30 | + Conditions: []appsv1.DeploymentCondition{ |
| 31 | + {Type: appsv1.DeploymentProgressing, Status: corev1.ConditionTrue, Reason: "ReplicaSetUpdated"}, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + expected: operatorv1.OperatorCondition{ |
| 36 | + Type: operatorv1.OperatorStatusTypeProgressing, |
| 37 | + Status: operatorv1.ConditionTrue, |
| 38 | + Reason: "PodsUpdating", |
| 39 | + Message: "deployment/web.ns: 1/3 pods have been updated to the latest revision and 2/3 pods are available", |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "rollout complete", |
| 44 | + deploy: &appsv1.Deployment{ |
| 45 | + ObjectMeta: metav1.ObjectMeta{Name: "web", Namespace: "ns"}, |
| 46 | + Spec: appsv1.DeploymentSpec{Replicas: ptr.To[int32](3)}, |
| 47 | + Status: appsv1.DeploymentStatus{ |
| 48 | + UpdatedReplicas: 3, |
| 49 | + AvailableReplicas: 3, |
| 50 | + Conditions: []appsv1.DeploymentCondition{ |
| 51 | + {Type: appsv1.DeploymentProgressing, Status: corev1.ConditionTrue, Reason: "NewReplicaSetAvailable"}, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + expected: operatorv1.OperatorCondition{ |
| 56 | + Type: operatorv1.OperatorStatusTypeProgressing, |
| 57 | + Status: operatorv1.ConditionFalse, |
| 58 | + Reason: "AsExpected", |
| 59 | + }, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "progress deadline exceeded", |
| 63 | + deploy: &appsv1.Deployment{ |
| 64 | + ObjectMeta: metav1.ObjectMeta{Name: "web", Namespace: "ns"}, |
| 65 | + Spec: appsv1.DeploymentSpec{Replicas: ptr.To[int32](3)}, |
| 66 | + Status: appsv1.DeploymentStatus{ |
| 67 | + UpdatedReplicas: 1, |
| 68 | + AvailableReplicas: 2, |
| 69 | + Conditions: []appsv1.DeploymentCondition{ |
| 70 | + {Type: appsv1.DeploymentProgressing, Status: corev1.ConditionFalse, Reason: "ProgressDeadlineExceeded", Message: "timed out waiting"}, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + expected: operatorv1.OperatorCondition{ |
| 75 | + Type: operatorv1.OperatorStatusTypeProgressing, |
| 76 | + Status: operatorv1.ConditionFalse, |
| 77 | + Reason: "ProgressDeadlineExceeded", |
| 78 | + Message: "deployment/web.ns has timed out progressing: timed out waiting", |
| 79 | + }, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "no progressing condition on deployment", |
| 83 | + deploy: &appsv1.Deployment{ |
| 84 | + ObjectMeta: metav1.ObjectMeta{Name: "web", Namespace: "ns"}, |
| 85 | + Spec: appsv1.DeploymentSpec{Replicas: ptr.To[int32](1)}, |
| 86 | + Status: appsv1.DeploymentStatus{}, |
| 87 | + }, |
| 88 | + expected: operatorv1.OperatorCondition{ |
| 89 | + Type: operatorv1.OperatorStatusTypeProgressing, |
| 90 | + Status: operatorv1.ConditionTrue, |
| 91 | + Reason: "PodsUpdating", |
| 92 | + Message: "deployment/web.ns: 0/1 pods have been updated to the latest revision and 0/1 pods are available", |
| 93 | + }, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "nil replicas defaults to 1", |
| 97 | + deploy: &appsv1.Deployment{ |
| 98 | + ObjectMeta: metav1.ObjectMeta{Name: "web", Namespace: "ns"}, |
| 99 | + Spec: appsv1.DeploymentSpec{}, |
| 100 | + Status: appsv1.DeploymentStatus{ |
| 101 | + UpdatedReplicas: 1, |
| 102 | + AvailableReplicas: 1, |
| 103 | + Conditions: []appsv1.DeploymentCondition{ |
| 104 | + {Type: appsv1.DeploymentProgressing, Status: corev1.ConditionTrue, Reason: "NewReplicaSetAvailable"}, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + expected: operatorv1.OperatorCondition{ |
| 109 | + Type: operatorv1.OperatorStatusTypeProgressing, |
| 110 | + Status: operatorv1.ConditionFalse, |
| 111 | + Reason: "AsExpected", |
| 112 | + }, |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + for _, tt := range tests { |
| 117 | + t.Run(tt.name, func(t *testing.T) { |
| 118 | + got := DeploymentProgressingCondition(tt.deploy) |
| 119 | + if d := cmp.Diff(tt.expected, got); d != "" { |
| 120 | + t.Errorf("unexpected condition (-want +got):\n%s", d) |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func TestHasDeploymentProgressed(t *testing.T) { |
| 127 | + tests := []struct { |
| 128 | + name string |
| 129 | + conditions []appsv1.DeploymentCondition |
| 130 | + want bool |
| 131 | + }{ |
| 132 | + { |
| 133 | + name: "NewReplicaSetAvailable and True", |
| 134 | + conditions: []appsv1.DeploymentCondition{ |
| 135 | + {Type: appsv1.DeploymentProgressing, Status: corev1.ConditionTrue, Reason: "NewReplicaSetAvailable"}, |
| 136 | + }, |
| 137 | + want: true, |
| 138 | + }, |
| 139 | + { |
| 140 | + name: "ReplicaSetUpdated and True", |
| 141 | + conditions: []appsv1.DeploymentCondition{ |
| 142 | + {Type: appsv1.DeploymentProgressing, Status: corev1.ConditionTrue, Reason: "ReplicaSetUpdated"}, |
| 143 | + }, |
| 144 | + want: false, |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "NewReplicaSetAvailable but False", |
| 148 | + conditions: []appsv1.DeploymentCondition{ |
| 149 | + {Type: appsv1.DeploymentProgressing, Status: corev1.ConditionFalse, Reason: "NewReplicaSetAvailable"}, |
| 150 | + }, |
| 151 | + want: false, |
| 152 | + }, |
| 153 | + { |
| 154 | + name: "no conditions", |
| 155 | + conditions: nil, |
| 156 | + want: false, |
| 157 | + }, |
| 158 | + { |
| 159 | + name: "unrelated condition only", |
| 160 | + conditions: []appsv1.DeploymentCondition{ |
| 161 | + {Type: appsv1.DeploymentAvailable, Status: corev1.ConditionTrue}, |
| 162 | + }, |
| 163 | + want: false, |
| 164 | + }, |
| 165 | + } |
| 166 | + for _, tt := range tests { |
| 167 | + t.Run(tt.name, func(t *testing.T) { |
| 168 | + got := HasDeploymentProgressed(appsv1.DeploymentStatus{Conditions: tt.conditions}) |
| 169 | + if got != tt.want { |
| 170 | + t.Errorf("got %v, want %v", got, tt.want) |
| 171 | + } |
| 172 | + }) |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +func TestHasDeploymentTimedOutProgressing(t *testing.T) { |
| 177 | + tests := []struct { |
| 178 | + name string |
| 179 | + conditions []appsv1.DeploymentCondition |
| 180 | + wantMessage string |
| 181 | + wantTimedOut bool |
| 182 | + }{ |
| 183 | + { |
| 184 | + name: "ProgressDeadlineExceeded", |
| 185 | + conditions: []appsv1.DeploymentCondition{ |
| 186 | + {Type: appsv1.DeploymentProgressing, Status: corev1.ConditionFalse, Reason: "ProgressDeadlineExceeded", Message: "timed out"}, |
| 187 | + }, |
| 188 | + wantMessage: "timed out", |
| 189 | + wantTimedOut: true, |
| 190 | + }, |
| 191 | + { |
| 192 | + name: "ProgressDeadlineExceeded but True status", |
| 193 | + conditions: []appsv1.DeploymentCondition{ |
| 194 | + {Type: appsv1.DeploymentProgressing, Status: corev1.ConditionTrue, Reason: "ProgressDeadlineExceeded", Message: "timed out"}, |
| 195 | + }, |
| 196 | + wantMessage: "timed out", |
| 197 | + wantTimedOut: false, |
| 198 | + }, |
| 199 | + { |
| 200 | + name: "normal progressing", |
| 201 | + conditions: []appsv1.DeploymentCondition{ |
| 202 | + {Type: appsv1.DeploymentProgressing, Status: corev1.ConditionTrue, Reason: "ReplicaSetUpdated", Message: "progressing"}, |
| 203 | + }, |
| 204 | + wantMessage: "progressing", |
| 205 | + wantTimedOut: false, |
| 206 | + }, |
| 207 | + { |
| 208 | + name: "no conditions", |
| 209 | + conditions: nil, |
| 210 | + wantMessage: "", |
| 211 | + wantTimedOut: false, |
| 212 | + }, |
| 213 | + } |
| 214 | + for _, tt := range tests { |
| 215 | + t.Run(tt.name, func(t *testing.T) { |
| 216 | + gotMsg, gotTimedOut := HasDeploymentTimedOutProgressing(appsv1.DeploymentStatus{Conditions: tt.conditions}) |
| 217 | + if gotMsg != tt.wantMessage { |
| 218 | + t.Errorf("message: got %q, want %q", gotMsg, tt.wantMessage) |
| 219 | + } |
| 220 | + if gotTimedOut != tt.wantTimedOut { |
| 221 | + t.Errorf("timedOut: got %v, want %v", gotTimedOut, tt.wantTimedOut) |
| 222 | + } |
| 223 | + }) |
| 224 | + } |
| 225 | +} |
0 commit comments