github.com/kubevela/workflow@v0.6.0/api/condition/condition_test.go (about)

     1  // This file is originally from https://github.com/crossplane/crossplane-runtime/blob/master/apis/common/v1/condition_test.go
     2  // We copy it here to reduce dependency
     3  
     4  /*
     5  Copyright 2019 The Crossplane Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10      http://www.apache.org/licenses/LICENSE-2.0
    11  Unless required by applicable law or agreed to in writing, software
    12  distributed under the License is distributed on an "AS IS" BASIS,
    13  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  See the License for the specific language governing permissions and
    15  limitations under the License.
    16  */
    17  
    18  package condition
    19  
    20  import (
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  	"github.com/pkg/errors"
    25  	corev1 "k8s.io/api/core/v1"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  )
    28  
    29  func TestConditionEqual(t *testing.T) {
    30  	cases := map[string]struct {
    31  		a    Condition
    32  		b    Condition
    33  		want bool
    34  	}{
    35  		"IdenticalIgnoringTimestamp": {
    36  			a:    Condition{Type: TypeReady, LastTransitionTime: metav1.Now()},
    37  			b:    Condition{Type: TypeReady, LastTransitionTime: metav1.Now()},
    38  			want: true,
    39  		},
    40  		"DifferentType": {
    41  			a:    Condition{Type: TypeReady},
    42  			b:    Condition{Type: TypeSynced},
    43  			want: false,
    44  		},
    45  		"DifferentStatus": {
    46  			a:    Condition{Status: corev1.ConditionTrue},
    47  			b:    Condition{Status: corev1.ConditionFalse},
    48  			want: false,
    49  		},
    50  		"DifferentReason": {
    51  			a:    Condition{Reason: ReasonCreating},
    52  			b:    Condition{Reason: ReasonDeleting},
    53  			want: false,
    54  		},
    55  		"DifferentMessage": {
    56  			a:    Condition{Message: "cool"},
    57  			b:    Condition{Message: "uncool"},
    58  			want: false,
    59  		},
    60  	}
    61  
    62  	for name, tc := range cases {
    63  		t.Run(name, func(t *testing.T) {
    64  			got := tc.a.Equal(tc.b)
    65  
    66  			if diff := cmp.Diff(tc.want, got); diff != "" {
    67  				t.Errorf("a.Equal(b): -want, +got:\n%s", diff)
    68  			}
    69  		})
    70  	}
    71  }
    72  
    73  func TestConditionedStatusEqual(t *testing.T) {
    74  	cases := map[string]struct {
    75  		a    *ConditionedStatus
    76  		b    *ConditionedStatus
    77  		want bool
    78  	}{
    79  		"Identical": {
    80  			a:    NewConditionedStatus(Available(), ReconcileSuccess()),
    81  			b:    NewConditionedStatus(Available(), ReconcileSuccess()),
    82  			want: true,
    83  		},
    84  		"IdenticalExceptOrder": {
    85  			a:    NewConditionedStatus(Unavailable(), ReconcileSuccess()),
    86  			b:    NewConditionedStatus(ReconcileSuccess(), Unavailable()),
    87  			want: true,
    88  		},
    89  		"DifferentLength": {
    90  			a:    NewConditionedStatus(Available(), ReconcileSuccess()),
    91  			b:    NewConditionedStatus(ReconcileSuccess()),
    92  			want: false,
    93  		},
    94  		"DifferentCondition": {
    95  			a:    NewConditionedStatus(Creating(), ReconcileSuccess()),
    96  			b:    NewConditionedStatus(Creating(), ReconcileError(errors.New("boom"))),
    97  			want: false,
    98  		},
    99  		"AIsNil": {
   100  			a:    nil,
   101  			b:    NewConditionedStatus(Deleting(), ReconcileSuccess()),
   102  			want: false,
   103  		},
   104  		"BIsNil": {
   105  			a:    NewConditionedStatus(Available(), ReconcileSuccess()),
   106  			b:    nil,
   107  			want: false,
   108  		},
   109  	}
   110  
   111  	for name, tc := range cases {
   112  		t.Run(name, func(t *testing.T) {
   113  			got := tc.a.Equal(tc.b)
   114  
   115  			if diff := cmp.Diff(tc.want, got); diff != "" {
   116  				t.Errorf("a.Equal(b): -want, +got:\n%s", diff)
   117  			}
   118  		})
   119  	}
   120  }
   121  
   122  func TestSetConditions(t *testing.T) {
   123  	cases := map[string]struct {
   124  		cs   *ConditionedStatus
   125  		c    []Condition
   126  		want *ConditionedStatus
   127  	}{
   128  		"TypeIsIdentical": {
   129  			cs:   NewConditionedStatus(Available()),
   130  			c:    []Condition{Available()},
   131  			want: NewConditionedStatus(Available()),
   132  		},
   133  		"TypeIsDifferent": {
   134  			cs:   NewConditionedStatus(Creating()),
   135  			c:    []Condition{Available()},
   136  			want: NewConditionedStatus(Available()),
   137  		},
   138  		"TypeDoesNotExist": {
   139  			cs:   NewConditionedStatus(ReconcileSuccess()),
   140  			c:    []Condition{Available()},
   141  			want: NewConditionedStatus(ReconcileSuccess(), Available()),
   142  		},
   143  	}
   144  
   145  	for name, tc := range cases {
   146  		t.Run(name, func(t *testing.T) {
   147  			tc.cs.SetConditions(tc.c...)
   148  
   149  			got := tc.cs
   150  			if diff := cmp.Diff(tc.want, got); diff != "" {
   151  				t.Errorf("tc.cs.SetConditions(...): -want, +got:\n%s", diff)
   152  			}
   153  		})
   154  	}
   155  }
   156  
   157  func TestGetCondition(t *testing.T) {
   158  	cases := map[string]struct {
   159  		cs   *ConditionedStatus
   160  		t    ConditionType
   161  		want Condition
   162  	}{
   163  		"ConditionExists": {
   164  			cs:   NewConditionedStatus(Available()),
   165  			t:    TypeReady,
   166  			want: Available(),
   167  		},
   168  		"ConditionDoesNotExist": {
   169  			cs: NewConditionedStatus(Available()),
   170  			t:  TypeSynced,
   171  			want: Condition{
   172  				Type:   TypeSynced,
   173  				Status: corev1.ConditionUnknown,
   174  			},
   175  		},
   176  	}
   177  
   178  	for name, tc := range cases {
   179  		t.Run(name, func(t *testing.T) {
   180  			got := tc.cs.GetCondition(tc.t)
   181  			if diff := cmp.Diff(tc.want, got); diff != "" {
   182  				t.Errorf("tc.cs.GetConditions(...): -want, +got:\n%s", diff)
   183  			}
   184  		})
   185  	}
   186  }
   187  
   188  func TestConditionWithMessage(t *testing.T) {
   189  	testMsg := "Something went wrong on cloud side"
   190  	cases := map[string]struct {
   191  		c    Condition
   192  		msg  string
   193  		want Condition
   194  	}{
   195  		"MessageAdded": {
   196  			c:    Condition{Type: TypeReady, Reason: ReasonUnavailable},
   197  			msg:  testMsg,
   198  			want: Condition{Type: TypeReady, Reason: ReasonUnavailable, Message: testMsg},
   199  		},
   200  		"MessageChanged": {
   201  			c:    Condition{Type: TypeReady, Reason: ReasonUnavailable, Message: "Some other message"},
   202  			msg:  testMsg,
   203  			want: Condition{Type: TypeReady, Reason: ReasonUnavailable, Message: testMsg},
   204  		},
   205  		"MessageCleared": {
   206  			c:    Condition{Type: TypeReady, Reason: ReasonUnavailable, Message: testMsg},
   207  			msg:  "",
   208  			want: Condition{Type: TypeReady, Reason: ReasonUnavailable},
   209  		},
   210  	}
   211  
   212  	for name, tc := range cases {
   213  		t.Run(name, func(t *testing.T) {
   214  			got := tc.c.WithMessage(tc.msg)
   215  
   216  			if diff := cmp.Diff(tc.want, got); diff != "" {
   217  				t.Errorf("a.Equal(b): -want, +got:\n%s", diff)
   218  			}
   219  		})
   220  	}
   221  }