k8c.io/api/v3@v3.0.0-20230904060738-b0a93889c0b6/pkg/apis/kubermatic/v1/helper/helper_test.go (about) 1 /* 2 Copyright 2023 The Kubermatic Kubernetes Platform contributors. 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 helper 18 19 import ( 20 "testing" 21 22 kubermaticv1 "k8c.io/api/v3/pkg/apis/kubermatic/v1" 23 24 corev1 "k8s.io/api/core/v1" 25 apiequality "k8s.io/apimachinery/pkg/api/equality" 26 ) 27 28 func TestSetClusterCondition(t *testing.T) { 29 conditionType := kubermaticv1.ClusterConditionSeedResourcesUpToDate 30 31 testCases := []struct { 32 name string 33 cluster *kubermaticv1.Cluster 34 conditionStatus corev1.ConditionStatus 35 conditionReason string 36 conditionMessage string 37 conditionChangeExpected bool 38 }{ 39 { 40 name: "Condition already exists, nothing to do", 41 cluster: getCluster(conditionType, &kubermaticv1.ClusterCondition{ 42 Status: corev1.ConditionTrue, 43 KubermaticVersion: "test", 44 Reason: "my-reason", 45 Message: "my-message", 46 }), 47 conditionStatus: corev1.ConditionTrue, 48 conditionReason: "my-reason", 49 conditionMessage: "my-message", 50 conditionChangeExpected: false, 51 }, 52 { 53 name: "Condition doesn't exist and is created", 54 cluster: getCluster("", nil), 55 conditionStatus: corev1.ConditionTrue, 56 conditionReason: "my-reason", 57 conditionMessage: "my-message", 58 conditionChangeExpected: true, 59 }, 60 { 61 name: "Update because of Kubermatic version", 62 cluster: getCluster(conditionType, &kubermaticv1.ClusterCondition{ 63 Status: corev1.ConditionTrue, 64 KubermaticVersion: "outdated", 65 Reason: "my-reason", 66 Message: "my-message", 67 }), 68 conditionStatus: corev1.ConditionTrue, 69 conditionReason: "my-reason", 70 conditionMessage: "my-message", 71 conditionChangeExpected: true, 72 }, 73 { 74 name: "Update because of status", 75 cluster: getCluster(conditionType, &kubermaticv1.ClusterCondition{ 76 Status: corev1.ConditionFalse, 77 KubermaticVersion: "test", 78 Reason: "my-reason", 79 Message: "my-message", 80 }), 81 conditionStatus: corev1.ConditionTrue, 82 conditionReason: "my-reason", 83 conditionMessage: "my-message", 84 conditionChangeExpected: true, 85 }, 86 { 87 name: "Update because of reason", 88 cluster: getCluster(conditionType, &kubermaticv1.ClusterCondition{ 89 Status: corev1.ConditionTrue, 90 KubermaticVersion: "test", 91 Reason: "outdated-reason", 92 Message: "my-message", 93 }), 94 conditionStatus: corev1.ConditionTrue, 95 conditionReason: "my-reason", 96 conditionMessage: "my-message", 97 conditionChangeExpected: true, 98 }, 99 { 100 name: "Update because of message", 101 cluster: getCluster(conditionType, &kubermaticv1.ClusterCondition{ 102 Status: corev1.ConditionTrue, 103 KubermaticVersion: "test", 104 Reason: "my-reason", 105 Message: "outdated-message", 106 }), 107 conditionStatus: corev1.ConditionTrue, 108 conditionReason: "my-reason", 109 conditionMessage: "my-message", 110 conditionChangeExpected: true, 111 }, 112 } 113 114 for idx := range testCases { 115 tc := testCases[idx] 116 t.Run(tc.name, func(t *testing.T) { 117 t.Parallel() 118 initialCluster := tc.cluster.DeepCopy() 119 SetClusterCondition(tc.cluster, "test", conditionType, tc.conditionStatus, tc.conditionReason, tc.conditionMessage) 120 hasChanged := !apiequality.Semantic.DeepEqual(initialCluster, tc.cluster) 121 if hasChanged != tc.conditionChangeExpected { 122 t.Errorf("Change doesn't match expectation: hasChanged: %t: changeExpected: %t", hasChanged, tc.conditionChangeExpected) 123 } 124 }) 125 } 126 } 127 128 func getCluster(conditionType kubermaticv1.ClusterConditionType, condition *kubermaticv1.ClusterCondition) *kubermaticv1.Cluster { 129 c := &kubermaticv1.Cluster{} 130 if condition != nil { 131 c.Status.Conditions = map[kubermaticv1.ClusterConditionType]kubermaticv1.ClusterCondition{ 132 conditionType: *condition, 133 } 134 } 135 136 return c 137 }