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

     1  /*
     2  Copyright 2020 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 conditions
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  
    25  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    26  )
    27  
    28  func TestGetStepCounterMessage(t *testing.T) {
    29  	g := NewWithT(t)
    30  
    31  	groups := getConditionGroups(conditionsWithSource(&clusterv1.Cluster{},
    32  		nil1,
    33  		true1, true1,
    34  		falseInfo1,
    35  		falseWarning1, falseWarning1,
    36  		falseError1,
    37  		unknown1,
    38  	))
    39  
    40  	got := getStepCounterMessage(groups, 8)
    41  
    42  	// step count message should report n° if true conditions over to number
    43  	g.Expect(got).To(Equal("2 of 8 completed"))
    44  }
    45  
    46  func TestLocalizeReason(t *testing.T) {
    47  	g := NewWithT(t)
    48  
    49  	getter := &clusterv1.Cluster{
    50  		TypeMeta: metav1.TypeMeta{
    51  			Kind: "Cluster",
    52  		},
    53  		ObjectMeta: metav1.ObjectMeta{
    54  			Name: "test-cluster",
    55  		},
    56  	}
    57  
    58  	// localize should reason location
    59  	got := localizeReason("foo", getter)
    60  	g.Expect(got).To(Equal("foo @ Cluster/test-cluster"))
    61  
    62  	// localize should not alter existing location
    63  	got = localizeReason("foo @ SomeKind/some-name", getter)
    64  	g.Expect(got).To(Equal("foo @ SomeKind/some-name"))
    65  }
    66  
    67  func TestGetFirstReasonAndMessage(t *testing.T) {
    68  	g := NewWithT(t)
    69  
    70  	foo := FalseCondition("foo", "falseFoo", clusterv1.ConditionSeverityInfo, "message falseFoo")
    71  	bar := FalseCondition("bar", "falseBar", clusterv1.ConditionSeverityInfo, "message falseBar")
    72  
    73  	getter := &clusterv1.Cluster{
    74  		TypeMeta: metav1.TypeMeta{
    75  			Kind: "Cluster",
    76  		},
    77  		ObjectMeta: metav1.ObjectMeta{
    78  			Name: "test-cluster",
    79  		},
    80  	}
    81  
    82  	groups := getConditionGroups(conditionsWithSource(getter, foo, bar))
    83  
    84  	// getFirst should report first condition in lexicografical order if no order is specified
    85  	gotReason := getFirstReason(groups, nil, false)
    86  	g.Expect(gotReason).To(Equal("falseBar"))
    87  	gotMessage := getFirstMessage(groups, nil)
    88  	g.Expect(gotMessage).To(Equal("message falseBar"))
    89  
    90  	// getFirst should report should respect order
    91  	gotReason = getFirstReason(groups, []clusterv1.ConditionType{"foo", "bar"}, false)
    92  	g.Expect(gotReason).To(Equal("falseFoo"))
    93  	gotMessage = getFirstMessage(groups, []clusterv1.ConditionType{"foo", "bar"})
    94  	g.Expect(gotMessage).To(Equal("message falseFoo"))
    95  
    96  	// getFirst should report should respect order in case of missing conditions
    97  	gotReason = getFirstReason(groups, []clusterv1.ConditionType{"missingBaz", "foo", "bar"}, false)
    98  	g.Expect(gotReason).To(Equal("falseFoo"))
    99  	gotMessage = getFirstMessage(groups, []clusterv1.ConditionType{"missingBaz", "foo", "bar"})
   100  	g.Expect(gotMessage).To(Equal("message falseFoo"))
   101  
   102  	// getFirst should fallback to first condition if none of the conditions in the list exists
   103  	gotReason = getFirstReason(groups, []clusterv1.ConditionType{"missingBaz"}, false)
   104  	g.Expect(gotReason).To(Equal("falseBar"))
   105  	gotMessage = getFirstMessage(groups, []clusterv1.ConditionType{"missingBaz"})
   106  	g.Expect(gotMessage).To(Equal("message falseBar"))
   107  
   108  	// getFirstReason should localize reason if required
   109  	gotReason = getFirstReason(groups, nil, true)
   110  	g.Expect(gotReason).To(Equal("falseBar @ Cluster/test-cluster"))
   111  }