sigs.k8s.io/cluster-api@v1.7.1/util/predicates/cluster_predicates_test.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes 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 predicates_test
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/go-logr/logr"
    23  	. "github.com/onsi/gomega"
    24  	"sigs.k8s.io/controller-runtime/pkg/event"
    25  	"sigs.k8s.io/controller-runtime/pkg/log"
    26  
    27  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    28  	"sigs.k8s.io/cluster-api/util/conditions"
    29  	"sigs.k8s.io/cluster-api/util/predicates"
    30  )
    31  
    32  func TestClusterControlplaneInitializedPredicate(t *testing.T) {
    33  	g := NewWithT(t)
    34  	predicate := predicates.ClusterControlPlaneInitialized(logr.New(log.NullLogSink{}))
    35  
    36  	markedFalse := clusterv1.Cluster{}
    37  	conditions.MarkFalse(&markedFalse, clusterv1.ControlPlaneInitializedCondition, clusterv1.MissingNodeRefReason, clusterv1.ConditionSeverityWarning, "")
    38  
    39  	markedTrue := clusterv1.Cluster{}
    40  	conditions.MarkTrue(&markedTrue, clusterv1.ControlPlaneInitializedCondition)
    41  
    42  	notMarked := clusterv1.Cluster{}
    43  
    44  	testcases := []struct {
    45  		name       string
    46  		oldCluster clusterv1.Cluster
    47  		newCluster clusterv1.Cluster
    48  		expected   bool
    49  	}{
    50  		{
    51  			name:       "no conditions -> no conditions: should return false",
    52  			oldCluster: notMarked,
    53  			newCluster: notMarked,
    54  			expected:   false,
    55  		},
    56  		{
    57  			name:       "no conditions -> true: should return true",
    58  			oldCluster: notMarked,
    59  			newCluster: markedTrue,
    60  			expected:   true,
    61  		},
    62  		{
    63  			name:       "false -> true: should return true",
    64  			oldCluster: markedFalse,
    65  			newCluster: markedTrue,
    66  			expected:   true,
    67  		},
    68  		{
    69  			name:       "no conditions -> false: should return false",
    70  			oldCluster: notMarked,
    71  			newCluster: markedFalse,
    72  			expected:   false,
    73  		},
    74  		{
    75  			name:       "true -> false: should return false",
    76  			oldCluster: markedTrue,
    77  			newCluster: markedFalse,
    78  			expected:   false,
    79  		},
    80  		{
    81  			name:       "true -> true: should return false",
    82  			oldCluster: markedTrue,
    83  			newCluster: markedTrue,
    84  			expected:   false,
    85  		},
    86  	}
    87  
    88  	for i := range testcases {
    89  		tc := testcases[i]
    90  		t.Run(tc.name, func(*testing.T) {
    91  			ev := event.UpdateEvent{
    92  				ObjectOld: &tc.oldCluster,
    93  				ObjectNew: &tc.newCluster,
    94  			}
    95  
    96  			g.Expect(predicate.Update(ev)).To(Equal(tc.expected))
    97  		})
    98  	}
    99  }