sigs.k8s.io/cluster-api@v1.7.1/util/conditions/matcher_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  	corev1 "k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  
    26  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    27  )
    28  
    29  func TestMatchConditions(t *testing.T) {
    30  	testCases := []struct {
    31  		name        string
    32  		actual      interface{}
    33  		expected    clusterv1.Conditions
    34  		expectMatch bool
    35  	}{
    36  		{
    37  			name:        "with an empty conditions",
    38  			actual:      clusterv1.Conditions{},
    39  			expected:    clusterv1.Conditions{},
    40  			expectMatch: true,
    41  		},
    42  		{
    43  			name: "with matching conditions",
    44  			actual: clusterv1.Conditions{
    45  				{
    46  					Type:               clusterv1.ConditionType("type"),
    47  					Status:             corev1.ConditionTrue,
    48  					Severity:           clusterv1.ConditionSeverityNone,
    49  					LastTransitionTime: metav1.Now(),
    50  					Reason:             "reason",
    51  					Message:            "message",
    52  				},
    53  			},
    54  			expected: clusterv1.Conditions{
    55  				{
    56  					Type:               clusterv1.ConditionType("type"),
    57  					Status:             corev1.ConditionTrue,
    58  					Severity:           clusterv1.ConditionSeverityNone,
    59  					LastTransitionTime: metav1.Now(),
    60  					Reason:             "reason",
    61  					Message:            "message",
    62  				},
    63  			},
    64  			expectMatch: true,
    65  		},
    66  		{
    67  			name: "with non-matching conditions",
    68  			actual: clusterv1.Conditions{
    69  				{
    70  					Type:               clusterv1.ConditionType("type"),
    71  					Status:             corev1.ConditionTrue,
    72  					Severity:           clusterv1.ConditionSeverityNone,
    73  					LastTransitionTime: metav1.Now(),
    74  					Reason:             "reason",
    75  					Message:            "message",
    76  				},
    77  				{
    78  					Type:               clusterv1.ConditionType("type"),
    79  					Status:             corev1.ConditionTrue,
    80  					Severity:           clusterv1.ConditionSeverityNone,
    81  					LastTransitionTime: metav1.Now(),
    82  					Reason:             "reason",
    83  					Message:            "message",
    84  				},
    85  			},
    86  			expected: clusterv1.Conditions{
    87  				{
    88  					Type:               clusterv1.ConditionType("type"),
    89  					Status:             corev1.ConditionTrue,
    90  					Severity:           clusterv1.ConditionSeverityNone,
    91  					LastTransitionTime: metav1.Now(),
    92  					Reason:             "reason",
    93  					Message:            "message",
    94  				},
    95  				{
    96  					Type:               clusterv1.ConditionType("different"),
    97  					Status:             corev1.ConditionTrue,
    98  					Severity:           clusterv1.ConditionSeverityNone,
    99  					LastTransitionTime: metav1.Now(),
   100  					Reason:             "different",
   101  					Message:            "different",
   102  				},
   103  			},
   104  			expectMatch: false,
   105  		},
   106  		{
   107  			name: "with a different number of conditions",
   108  			actual: clusterv1.Conditions{
   109  				{
   110  					Type:               clusterv1.ConditionType("type"),
   111  					Status:             corev1.ConditionTrue,
   112  					Severity:           clusterv1.ConditionSeverityNone,
   113  					LastTransitionTime: metav1.Now(),
   114  					Reason:             "reason",
   115  					Message:            "message",
   116  				},
   117  				{
   118  					Type:               clusterv1.ConditionType("type"),
   119  					Status:             corev1.ConditionTrue,
   120  					Severity:           clusterv1.ConditionSeverityNone,
   121  					LastTransitionTime: metav1.Now(),
   122  					Reason:             "reason",
   123  					Message:            "message",
   124  				},
   125  			},
   126  			expected: clusterv1.Conditions{
   127  				{
   128  					Type:               clusterv1.ConditionType("type"),
   129  					Status:             corev1.ConditionTrue,
   130  					Severity:           clusterv1.ConditionSeverityNone,
   131  					LastTransitionTime: metav1.Now(),
   132  					Reason:             "reason",
   133  					Message:            "message",
   134  				},
   135  			},
   136  			expectMatch: false,
   137  		},
   138  	}
   139  
   140  	for _, tc := range testCases {
   141  		t.Run(tc.name, func(t *testing.T) {
   142  			g := NewWithT(t)
   143  			if tc.expectMatch {
   144  				g.Expect(tc.actual).To(MatchConditions(tc.expected))
   145  			} else {
   146  				g.Expect(tc.actual).ToNot(MatchConditions(tc.expected))
   147  			}
   148  		})
   149  	}
   150  }
   151  
   152  func TestMatchCondition(t *testing.T) {
   153  	testCases := []struct {
   154  		name        string
   155  		actual      interface{}
   156  		expected    clusterv1.Condition
   157  		expectMatch bool
   158  	}{
   159  		{
   160  			name:        "with an empty condition",
   161  			actual:      clusterv1.Condition{},
   162  			expected:    clusterv1.Condition{},
   163  			expectMatch: true,
   164  		},
   165  		{
   166  			name: "with a matching condition",
   167  			actual: clusterv1.Condition{
   168  				Type:               clusterv1.ConditionType("type"),
   169  				Status:             corev1.ConditionTrue,
   170  				Severity:           clusterv1.ConditionSeverityNone,
   171  				LastTransitionTime: metav1.Now(),
   172  				Reason:             "reason",
   173  				Message:            "message",
   174  			},
   175  			expected: clusterv1.Condition{
   176  				Type:               clusterv1.ConditionType("type"),
   177  				Status:             corev1.ConditionTrue,
   178  				Severity:           clusterv1.ConditionSeverityNone,
   179  				LastTransitionTime: metav1.Now(),
   180  				Reason:             "reason",
   181  				Message:            "message",
   182  			},
   183  			expectMatch: true,
   184  		},
   185  		{
   186  			name: "with a different time",
   187  			actual: clusterv1.Condition{
   188  				Type:               clusterv1.ConditionType("type"),
   189  				Status:             corev1.ConditionTrue,
   190  				Severity:           clusterv1.ConditionSeverityNone,
   191  				LastTransitionTime: metav1.Now(),
   192  				Reason:             "reason",
   193  				Message:            "message",
   194  			},
   195  			expected: clusterv1.Condition{
   196  				Type:               clusterv1.ConditionType("type"),
   197  				Status:             corev1.ConditionTrue,
   198  				Severity:           clusterv1.ConditionSeverityNone,
   199  				LastTransitionTime: metav1.Time{},
   200  				Reason:             "reason",
   201  				Message:            "message",
   202  			},
   203  			expectMatch: true,
   204  		},
   205  		{
   206  			name: "with a different type",
   207  			actual: clusterv1.Condition{
   208  				Type:               clusterv1.ConditionType("type"),
   209  				Status:             corev1.ConditionTrue,
   210  				Severity:           clusterv1.ConditionSeverityNone,
   211  				LastTransitionTime: metav1.Now(),
   212  				Reason:             "reason",
   213  				Message:            "message",
   214  			},
   215  			expected: clusterv1.Condition{
   216  				Type:               clusterv1.ConditionType("different"),
   217  				Status:             corev1.ConditionTrue,
   218  				Severity:           clusterv1.ConditionSeverityNone,
   219  				LastTransitionTime: metav1.Now(),
   220  				Reason:             "reason",
   221  				Message:            "message",
   222  			},
   223  			expectMatch: false,
   224  		},
   225  		{
   226  			name: "with a different status",
   227  			actual: clusterv1.Condition{
   228  				Type:               clusterv1.ConditionType("type"),
   229  				Status:             corev1.ConditionTrue,
   230  				Severity:           clusterv1.ConditionSeverityNone,
   231  				LastTransitionTime: metav1.Now(),
   232  				Reason:             "reason",
   233  				Message:            "message",
   234  			},
   235  			expected: clusterv1.Condition{
   236  				Type:               clusterv1.ConditionType("type"),
   237  				Status:             corev1.ConditionFalse,
   238  				Severity:           clusterv1.ConditionSeverityNone,
   239  				LastTransitionTime: metav1.Now(),
   240  				Reason:             "reason",
   241  				Message:            "message",
   242  			},
   243  			expectMatch: false,
   244  		},
   245  		{
   246  			name: "with a different severity",
   247  			actual: clusterv1.Condition{
   248  				Type:               clusterv1.ConditionType("type"),
   249  				Status:             corev1.ConditionTrue,
   250  				Severity:           clusterv1.ConditionSeverityNone,
   251  				LastTransitionTime: metav1.Now(),
   252  				Reason:             "reason",
   253  				Message:            "message",
   254  			},
   255  			expected: clusterv1.Condition{
   256  				Type:               clusterv1.ConditionType("type"),
   257  				Status:             corev1.ConditionTrue,
   258  				Severity:           clusterv1.ConditionSeverityInfo,
   259  				LastTransitionTime: metav1.Now(),
   260  				Reason:             "reason",
   261  				Message:            "message",
   262  			},
   263  			expectMatch: false,
   264  		},
   265  		{
   266  			name: "with a different reason",
   267  			actual: clusterv1.Condition{
   268  				Type:               clusterv1.ConditionType("type"),
   269  				Status:             corev1.ConditionTrue,
   270  				Severity:           clusterv1.ConditionSeverityNone,
   271  				LastTransitionTime: metav1.Now(),
   272  				Reason:             "reason",
   273  				Message:            "message",
   274  			},
   275  			expected: clusterv1.Condition{
   276  				Type:               clusterv1.ConditionType("type"),
   277  				Status:             corev1.ConditionTrue,
   278  				Severity:           clusterv1.ConditionSeverityNone,
   279  				LastTransitionTime: metav1.Now(),
   280  				Reason:             "different",
   281  				Message:            "message",
   282  			},
   283  			expectMatch: false,
   284  		},
   285  		{
   286  			name: "with a different message",
   287  			actual: clusterv1.Condition{
   288  				Type:               clusterv1.ConditionType("type"),
   289  				Status:             corev1.ConditionTrue,
   290  				Severity:           clusterv1.ConditionSeverityNone,
   291  				LastTransitionTime: metav1.Now(),
   292  				Reason:             "reason",
   293  				Message:            "message",
   294  			},
   295  			expected: clusterv1.Condition{
   296  				Type:               clusterv1.ConditionType("type"),
   297  				Status:             corev1.ConditionTrue,
   298  				Severity:           clusterv1.ConditionSeverityNone,
   299  				LastTransitionTime: metav1.Now(),
   300  				Reason:             "reason",
   301  				Message:            "different",
   302  			},
   303  			expectMatch: false,
   304  		},
   305  	}
   306  
   307  	for _, tc := range testCases {
   308  		t.Run(tc.name, func(t *testing.T) {
   309  			g := NewWithT(t)
   310  			if tc.expectMatch {
   311  				g.Expect(tc.actual).To(MatchCondition(tc.expected))
   312  			} else {
   313  				g.Expect(tc.actual).ToNot(MatchCondition(tc.expected))
   314  			}
   315  		})
   316  	}
   317  }