...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s/condition_test.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package k8s_test
    16  
    17  import (
    18  	"reflect"
    19  	"runtime"
    20  	"testing"
    21  
    22  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/k8s/v1alpha1"
    23  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
    24  )
    25  
    26  func TestEqualIgnoreTransitionTime(t *testing.T) {
    27  	condition := v1alpha1.Condition{}
    28  	cType := reflect.TypeOf(&condition).Elem()
    29  	if cType.NumField() != 5 {
    30  		t.Fatalf("number of fields in type '%v/%v' has increased, this test needs to be updated",
    31  			cType.PkgPath(), cType.Name())
    32  	}
    33  	testCases := []struct {
    34  		Name           string
    35  		ConditionOne   v1alpha1.Condition
    36  		ConditionTwo   v1alpha1.Condition
    37  		ExpectedResult bool
    38  	}{
    39  		{
    40  			Name:           "Equal structs",
    41  			ConditionOne:   v1alpha1.Condition{LastTransitionTime: "2018-11-08", Message: "Message", Reason: "Reason", Status: "True", Type: "Ready"},
    42  			ConditionTwo:   v1alpha1.Condition{LastTransitionTime: "2018-11-08", Message: "Message", Reason: "Reason", Status: "True", Type: "Ready"},
    43  			ExpectedResult: true,
    44  		},
    45  		{
    46  			Name:           "Different times, all other values equal",
    47  			ConditionOne:   v1alpha1.Condition{LastTransitionTime: "2018-11-08", Message: "Message", Reason: "Reason", Status: "True", Type: "Ready"},
    48  			ConditionTwo:   v1alpha1.Condition{LastTransitionTime: "2018-11-09", Message: "Message", Reason: "Reason", Status: "True", Type: "Ready"},
    49  			ExpectedResult: true,
    50  		},
    51  		{
    52  			Name:           "Different message",
    53  			ConditionOne:   v1alpha1.Condition{LastTransitionTime: "2018-11-08", Message: "Message", Reason: "Reason", Status: "True", Type: "Ready"},
    54  			ConditionTwo:   v1alpha1.Condition{LastTransitionTime: "2018-11-08", Message: "Message2", Reason: "Reason", Status: "True", Type: "Ready"},
    55  			ExpectedResult: false,
    56  		},
    57  		{
    58  			Name:           "Different reason",
    59  			ConditionOne:   v1alpha1.Condition{LastTransitionTime: "2018-11-08", Message: "Message", Reason: "Reason", Status: "True", Type: "Ready"},
    60  			ConditionTwo:   v1alpha1.Condition{LastTransitionTime: "2018-11-08", Message: "Message", Reason: "Reason2", Status: "True", Type: "Ready"},
    61  			ExpectedResult: false,
    62  		},
    63  		{
    64  			Name:           "Different status",
    65  			ConditionOne:   v1alpha1.Condition{LastTransitionTime: "2018-11-08", Message: "Message", Reason: "Reason", Status: "True", Type: "Ready"},
    66  			ConditionTwo:   v1alpha1.Condition{LastTransitionTime: "2018-11-08", Message: "Message", Reason: "Reason", Status: "False", Type: "Ready"},
    67  			ExpectedResult: false,
    68  		},
    69  		{
    70  			Name:           "Different type",
    71  			ConditionOne:   v1alpha1.Condition{LastTransitionTime: "2018-11-08", Message: "Message", Reason: "Reason", Status: "True", Type: "Ready"},
    72  			ConditionTwo:   v1alpha1.Condition{LastTransitionTime: "2018-11-08", Message: "Message", Reason: "Reason", Status: "True", Type: "Ready2"},
    73  			ExpectedResult: false,
    74  		},
    75  	}
    76  	for _, tc := range testCases {
    77  		t.Run(tc.Name, func(t *testing.T) {
    78  			result := k8s.ConditionsEqualIgnoreTransitionTime(tc.ConditionOne, tc.ConditionTwo)
    79  			if result != tc.ExpectedResult {
    80  				functionName := runtime.FuncForPC(reflect.ValueOf(k8s.ConditionsEqualIgnoreTransitionTime).Pointer()).Name()
    81  				t.Errorf("unexpected result for '%v': got '%v', want '%v'", functionName, result, tc.ExpectedResult)
    82  			}
    83  		})
    84  	}
    85  }
    86  
    87  func TestConditionSlicesEqual(t *testing.T) {
    88  	c1 := v1alpha1.Condition{LastTransitionTime: "2018-11-08", Message: "Message", Reason: "Reason", Status: "True"}
    89  	c1DifferentTransitionTime := v1alpha1.Condition{LastTransitionTime: "2018-11-09", Message: "Message", Reason: "Reason", Status: "True"}
    90  	c2 := v1alpha1.Condition{LastTransitionTime: "2018-11-08", Message: "Different Message", Reason: "Reason", Status: "True"}
    91  	testCases := []struct {
    92  		Name           string
    93  		ConditionsOne  []v1alpha1.Condition
    94  		ConditionsTwo  []v1alpha1.Condition
    95  		ExpectedResult bool
    96  	}{
    97  		{
    98  			Name:           "Nil slices",
    99  			ConditionsOne:  nil,
   100  			ConditionsTwo:  nil,
   101  			ExpectedResult: true,
   102  		},
   103  		{
   104  			Name:           "Empty slices",
   105  			ConditionsOne:  []v1alpha1.Condition{},
   106  			ConditionsTwo:  []v1alpha1.Condition{},
   107  			ExpectedResult: true,
   108  		},
   109  		{
   110  			Name:           "Equal slices, size one",
   111  			ConditionsOne:  []v1alpha1.Condition{c1},
   112  			ConditionsTwo:  []v1alpha1.Condition{c1},
   113  			ExpectedResult: true,
   114  		},
   115  		{
   116  			Name:           "Equal slices, with different transition times, size one",
   117  			ConditionsOne:  []v1alpha1.Condition{c1},
   118  			ConditionsTwo:  []v1alpha1.Condition{c1DifferentTransitionTime},
   119  			ExpectedResult: true,
   120  		},
   121  		{
   122  			Name:           "Different slices, size one",
   123  			ConditionsOne:  []v1alpha1.Condition{c1},
   124  			ConditionsTwo:  []v1alpha1.Condition{c2},
   125  			ExpectedResult: false,
   126  		},
   127  		{
   128  			Name:           "Equal slices, size two",
   129  			ConditionsOne:  []v1alpha1.Condition{c1, c2},
   130  			ConditionsTwo:  []v1alpha1.Condition{c1, c2},
   131  			ExpectedResult: true,
   132  		},
   133  		{
   134  			Name:           "Equal slices, with different transition times, size two",
   135  			ConditionsOne:  []v1alpha1.Condition{c1, c2},
   136  			ConditionsTwo:  []v1alpha1.Condition{c1DifferentTransitionTime, c2},
   137  			ExpectedResult: true,
   138  		},
   139  		{
   140  			Name:           "Different slices, size two",
   141  			ConditionsOne:  []v1alpha1.Condition{c1, c1},
   142  			ConditionsTwo:  []v1alpha1.Condition{c1, c2},
   143  			ExpectedResult: false,
   144  		},
   145  	}
   146  	for _, tc := range testCases {
   147  		t.Run(tc.Name, func(t *testing.T) {
   148  			result := k8s.ConditionSlicesEqual(tc.ConditionsOne, tc.ConditionsTwo)
   149  			if result != tc.ExpectedResult {
   150  				functionName := runtime.FuncForPC(reflect.ValueOf(k8s.ConditionSlicesEqual).Pointer()).Name()
   151  				t.Errorf("unexpected result for '%v': got '%v', want '%v'", functionName, result, tc.ExpectedResult)
   152  			}
   153  		})
   154  	}
   155  }
   156  

View as plain text