...

Source file src/github.com/cert-manager/issuer-lib/conditions/issuer_test.go

Documentation: github.com/cert-manager/issuer-lib/conditions

     1  /*
     2  Copyright 2023 The cert-manager 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  	cmapi "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
    23  	cmmeta "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"
    24  	"github.com/stretchr/testify/require"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	clocktesting "k8s.io/utils/clock/testing"
    27  )
    28  
    29  func TestSetIssuerStatusCondition(t *testing.T) {
    30  	type testCase struct {
    31  		name string
    32  
    33  		existingConditions []cmapi.IssuerCondition
    34  		patchConditions    []cmapi.IssuerCondition
    35  		conditionType      cmapi.IssuerConditionType
    36  		status             cmmeta.ConditionStatus
    37  
    38  		expectedCondition *cmapi.IssuerCondition
    39  		expectNewEntry    bool
    40  	}
    41  
    42  	fakeTime1 := randomTime()
    43  	fakeTimeObj1 := metav1.NewTime(fakeTime1)
    44  
    45  	fakeTime2 := randomTime()
    46  	fakeTimeObj2 := metav1.NewTime(fakeTime2)
    47  	fakeClock2 := clocktesting.NewFakeClock(fakeTime2)
    48  
    49  	testCases := []testCase{
    50  		{
    51  			name: "if the condition does NOT change its status, the last transition time should not be updated",
    52  			existingConditions: []cmapi.IssuerCondition{
    53  				{
    54  					Type:   cmapi.IssuerConditionReady,
    55  					Status: cmmeta.ConditionTrue,
    56  				},
    57  			},
    58  			patchConditions: []cmapi.IssuerCondition{},
    59  			conditionType:   cmapi.IssuerConditionReady,
    60  			status:          cmmeta.ConditionTrue,
    61  
    62  			expectedCondition: &cmapi.IssuerCondition{
    63  				Type:               cmapi.IssuerConditionReady,
    64  				Status:             cmmeta.ConditionTrue,
    65  				LastTransitionTime: &fakeTimeObj1,
    66  			},
    67  			expectNewEntry: true,
    68  		},
    69  		{
    70  			name: "if the condition DOES change its status, the last transition time should be updated",
    71  			existingConditions: []cmapi.IssuerCondition{
    72  				{
    73  					Type:   cmapi.IssuerConditionReady,
    74  					Status: cmmeta.ConditionTrue,
    75  				},
    76  			},
    77  			patchConditions: []cmapi.IssuerCondition{},
    78  			conditionType:   cmapi.IssuerConditionReady,
    79  			status:          cmmeta.ConditionFalse,
    80  
    81  			expectedCondition: &cmapi.IssuerCondition{
    82  				Type:               cmapi.IssuerConditionReady,
    83  				Status:             cmmeta.ConditionFalse,
    84  				LastTransitionTime: &fakeTimeObj2,
    85  			},
    86  			expectNewEntry: true,
    87  		},
    88  		{
    89  			name: "if the patch contains already contains the condition, it should get overwritten",
    90  			existingConditions: []cmapi.IssuerCondition{
    91  				{
    92  					Type:   cmapi.IssuerConditionReady,
    93  					Status: cmmeta.ConditionTrue,
    94  				},
    95  			},
    96  			patchConditions: []cmapi.IssuerCondition{
    97  				{
    98  					Type:   cmapi.IssuerConditionReady,
    99  					Status: cmmeta.ConditionTrue,
   100  				},
   101  			},
   102  			conditionType: cmapi.IssuerConditionReady,
   103  			status:        cmmeta.ConditionTrue,
   104  
   105  			expectedCondition: &cmapi.IssuerCondition{
   106  				Type:               cmapi.IssuerConditionReady,
   107  				Status:             cmmeta.ConditionTrue,
   108  				LastTransitionTime: &fakeTimeObj1,
   109  			},
   110  			expectNewEntry: false,
   111  		},
   112  		{
   113  			name: "if the patch contains another condition type, it should get added",
   114  			existingConditions: []cmapi.IssuerCondition{
   115  				{
   116  					Type:   cmapi.IssuerConditionReady,
   117  					Status: cmmeta.ConditionTrue,
   118  				},
   119  			},
   120  			patchConditions: []cmapi.IssuerCondition{
   121  				{
   122  					Type:   cmapi.IssuerConditionReady,
   123  					Status: cmmeta.ConditionTrue,
   124  				},
   125  			},
   126  			conditionType: cmapi.IssuerConditionType("AnotherCondition"),
   127  			status:        cmmeta.ConditionTrue,
   128  
   129  			expectedCondition: &cmapi.IssuerCondition{
   130  				Type:               cmapi.IssuerConditionType("AnotherCondition"),
   131  				Status:             cmmeta.ConditionTrue,
   132  				LastTransitionTime: &fakeTimeObj2,
   133  			},
   134  			expectNewEntry: true,
   135  		},
   136  	}
   137  
   138  	defaultConditions := func(t *testing.T, conditions []cmapi.IssuerCondition) []cmapi.IssuerCondition {
   139  		t.Helper()
   140  
   141  		for i := range conditions {
   142  			if !conditions[i].LastTransitionTime.IsZero() ||
   143  				conditions[i].Reason != "" ||
   144  				conditions[i].Message != "" ||
   145  				conditions[i].ObservedGeneration != 0 {
   146  				t.Fatal("this field is managed by the test and should not be set")
   147  			}
   148  			conditions[i].LastTransitionTime = &fakeTimeObj1
   149  			conditions[i].Reason = "OldReason"
   150  			conditions[i].Message = "OldMessage"
   151  			conditions[i].ObservedGeneration = 7
   152  		}
   153  
   154  		return conditions
   155  	}
   156  
   157  	for _, test := range testCases {
   158  		test := test
   159  
   160  		t.Run(test.name, func(t *testing.T) {
   161  			test.existingConditions = defaultConditions(t, test.existingConditions)
   162  			test.patchConditions = defaultConditions(t, test.patchConditions)
   163  
   164  			patchConditions := append([]cmapi.IssuerCondition{}, test.patchConditions...)
   165  
   166  			cond, time := SetIssuerStatusCondition(
   167  				fakeClock2,
   168  				test.existingConditions,
   169  				&patchConditions,
   170  				8,
   171  				test.conditionType,
   172  				test.status,
   173  				"NewReason",
   174  				"NewMessage",
   175  			)
   176  
   177  			if test.expectedCondition.Reason != "" ||
   178  				test.expectedCondition.Message != "" ||
   179  				test.expectedCondition.ObservedGeneration != 0 {
   180  				t.Fatal("this field is managed by the test and should not be set")
   181  			}
   182  			test.expectedCondition.Reason = "NewReason"
   183  			test.expectedCondition.Message = "NewMessage"
   184  			test.expectedCondition.ObservedGeneration = 8
   185  			require.Equal(t, test.expectedCondition, cond)
   186  			require.Equal(t, &fakeTimeObj2, time)
   187  
   188  			// Check that the patchConditions slice got a new entry if expected
   189  			if test.expectNewEntry {
   190  				require.Equal(t, len(test.patchConditions)+1, len(patchConditions))
   191  			} else {
   192  				require.Equal(t, len(test.patchConditions), len(patchConditions))
   193  			}
   194  
   195  			// Make sure only the expected condition in the patchConditions slice got updated
   196  			for _, c := range patchConditions {
   197  				if c.Type == test.conditionType {
   198  					require.Equal(t, test.expectedCondition, &c)
   199  					continue
   200  				}
   201  
   202  				for _, ec := range test.patchConditions {
   203  					if ec.Type == c.Type {
   204  						require.Equal(t, ec, c)
   205  					}
   206  				}
   207  			}
   208  		})
   209  	}
   210  }
   211  

View as plain text