knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/duck/v1/status_types_test.go (about)

     1  /*
     2  Copyright 2019 The Knative 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 v1
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  	corev1 "k8s.io/api/core/v1"
    25  	"knative.dev/pkg/apis"
    26  )
    27  
    28  func TestStatusGetCondition(t *testing.T) {
    29  	foo := &apis.Condition{
    30  		Type:    "Foo",
    31  		Status:  corev1.ConditionTrue,
    32  		Message: "Something something foo",
    33  	}
    34  	bar := &apis.Condition{
    35  		Type:    "Bar",
    36  		Status:  corev1.ConditionTrue,
    37  		Message: "Something something bar",
    38  	}
    39  	s := &Status{
    40  		Conditions: Conditions{*foo, *bar},
    41  	}
    42  
    43  	got := s.GetCondition(foo.Type)
    44  	if diff := cmp.Diff(got, foo); diff != "" {
    45  		t.Error("GetCondition(foo) =", diff)
    46  	}
    47  
    48  	got = s.GetCondition(bar.Type)
    49  	if diff := cmp.Diff(got, bar); diff != "" {
    50  		t.Error("GetCondition(bar) =", diff)
    51  	}
    52  
    53  	if got := s.GetCondition("None"); got != nil {
    54  		t.Errorf("GetCondition(None) = %v, wanted nil", got)
    55  	}
    56  }
    57  
    58  func TestConditionSet(t *testing.T) {
    59  	condSet := apis.NewLivingConditionSet("Foo")
    60  
    61  	const wantGeneration = 42
    62  
    63  	s := &Status{
    64  		ObservedGeneration: wantGeneration,
    65  		Annotations: map[string]string{
    66  			"peace": "was",
    67  			"never": "an-option",
    68  		},
    69  	}
    70  	mgr := condSet.Manage(s)
    71  
    72  	mgr.InitializeConditions()
    73  
    74  	for _, c := range []apis.ConditionType{apis.ConditionReady, "Foo"} {
    75  		if cond := mgr.GetCondition(c); cond == nil {
    76  			t.Errorf("GetCondition(%q) = nil, wanted non-nil", c)
    77  		} else if got, want := cond.Status, corev1.ConditionUnknown; got != want {
    78  			t.Errorf("GetCondition(%q) = %v, wanted %v", c, got, want)
    79  		}
    80  	}
    81  
    82  	s2 := &Status{}
    83  	s.ConvertTo(context.Background(), s2)
    84  	if condSet.Manage(s2).IsHappy() {
    85  		t.Error("s2.IsHappy() = true, wanted false")
    86  	}
    87  	if got, want := len(s2.Conditions), 1; got != want {
    88  		t.Errorf("len(s2.Conditions) = %d, wanted %d", got, want)
    89  	}
    90  	if gotGeneration := s2.ObservedGeneration; wantGeneration != gotGeneration {
    91  		t.Errorf("s2.ObservedGeneration = %d, wanted %d",
    92  			gotGeneration, wantGeneration)
    93  	}
    94  	if got, want := s2.Annotations, s.Annotations; !cmp.Equal(got, want) {
    95  		t.Errorf("Annotations mismatch: diff(-want,+got):\n%s", cmp.Diff(want, got))
    96  	}
    97  
    98  	for _, c := range []apis.ConditionType{"Foo"} {
    99  		mgr.MarkFalse(c, "bad", "for business")
   100  	}
   101  
   102  	for _, c := range []apis.ConditionType{apis.ConditionReady, "Foo"} {
   103  		if cond := mgr.GetCondition(c); cond == nil {
   104  			t.Errorf("GetCondition(%q) = nil, wanted non-nil", c)
   105  		} else if got, want := cond.Status, corev1.ConditionFalse; got != want {
   106  			t.Errorf("GetCondition(%q) = %v, wanted %v", c, got, want)
   107  		}
   108  	}
   109  
   110  	s2 = &Status{}
   111  	s.ConvertTo(context.Background(), s2)
   112  	if condSet.Manage(s2).IsHappy() {
   113  		t.Error("s2.IsHappy() = true, wanted false")
   114  	}
   115  	if got, want := len(s2.Conditions), 1; got != want {
   116  		t.Errorf("len(s2.Conditions) = %d, wanted %d", got, want)
   117  	}
   118  	if gotGeneration := s2.ObservedGeneration; wantGeneration != gotGeneration {
   119  		t.Errorf("s2.ObservedGeneration = %d, wanted %d",
   120  			gotGeneration, wantGeneration)
   121  	}
   122  	if got, want := s2.Annotations, s.Annotations; !cmp.Equal(got, want) {
   123  		t.Errorf("Annotations mismatch: diff(-want,+got):\n%s", cmp.Diff(want, got))
   124  	}
   125  
   126  	for _, c := range []apis.ConditionType{"Foo"} {
   127  		mgr.MarkTrue(c)
   128  	}
   129  
   130  	for _, c := range []apis.ConditionType{apis.ConditionReady, "Foo"} {
   131  		if cond := mgr.GetCondition(c); cond == nil {
   132  			t.Errorf("GetCondition(%q) = nil, wanted non-nil", c)
   133  		} else if got, want := cond.Status, corev1.ConditionTrue; got != want {
   134  			t.Errorf("GetCondition(%q) = %v, wanted %v", c, got, want)
   135  		}
   136  	}
   137  
   138  	s2 = &Status{}
   139  	s.ConvertTo(context.Background(), s2)
   140  	if !condSet.Manage(s2).IsHappy() {
   141  		t.Error("s2.IsHappy() = false, wanted true")
   142  	}
   143  	if got, want := len(s2.Conditions), 1; got != want {
   144  		t.Errorf("len(s2.Conditions) = %d, wanted %d", got, want)
   145  	}
   146  	if gotGeneration := s2.ObservedGeneration; wantGeneration != gotGeneration {
   147  		t.Errorf("s2.ObservedGeneration = %d, wanted %d",
   148  			gotGeneration, wantGeneration)
   149  	}
   150  	if got, want := s2.Annotations, s.Annotations; !cmp.Equal(got, want) {
   151  		t.Errorf("Annotations mismatch: diff(-want,+got):\n%s", cmp.Diff(want, got))
   152  	}
   153  
   154  	s2 = &Status{}
   155  	s.ConvertTo(context.Background(), s2,
   156  		func(cond apis.ConditionType) bool {
   157  			return cond == "Foo"
   158  		},
   159  		func(cond apis.ConditionType) bool {
   160  			return cond == "Ready"
   161  		},
   162  		func(cond apis.ConditionType) bool {
   163  			return cond == "Foo"
   164  		},
   165  	)
   166  
   167  	if !condSet.Manage(s2).IsHappy() {
   168  		t.Error("s2.IsHappy() = false, wanted true")
   169  	}
   170  	if got, want := len(s2.Conditions), 2; got != want {
   171  		t.Errorf("len(s2.Conditions) = %d, wanted %d", got, want)
   172  	}
   173  	for _, c := range []apis.ConditionType{apis.ConditionReady, "Foo"} {
   174  		if cond := mgr.GetCondition(c); cond == nil {
   175  			t.Errorf("GetCondition(%q) = nil, wanted non-nil", c)
   176  		} else if got, want := cond.Status, corev1.ConditionTrue; got != want {
   177  			t.Errorf("GetCondition(%q) = %v, wanted %v", c, got, want)
   178  		}
   179  	}
   180  	if gotGeneration := s2.ObservedGeneration; wantGeneration != gotGeneration {
   181  		t.Errorf("s2.ObservedGeneration = %d, wanted %d",
   182  			gotGeneration, wantGeneration)
   183  	}
   184  	if got, want := s2.Annotations, s.Annotations; !cmp.Equal(got, want) {
   185  		t.Errorf("Annotations mismatch: diff(-want,+got):\n%s", cmp.Diff(want, got))
   186  	}
   187  
   188  	s.Annotations = nil
   189  	s2 = &Status{}
   190  	s.ConvertTo(context.Background(), s2)
   191  	if s2.Annotations != nil {
   192  		t.Error("Annotations were not nil:", s2.Annotations)
   193  	}
   194  }