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