knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/duck/v1beta1/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 v1beta1
    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  			"burning": "the",
    67  			"bridges": "down",
    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("len(s2.ObservedGeneration) = %d, wanted %d",
   120  			gotGeneration, wantGeneration)
   121  	}
   122  
   123  	for _, c := range []apis.ConditionType{"Foo"} {
   124  		mgr.MarkTrue(c)
   125  	}
   126  
   127  	for _, c := range []apis.ConditionType{apis.ConditionReady, "Foo"} {
   128  		if cond := mgr.GetCondition(c); cond == nil {
   129  			t.Errorf("GetCondition(%q) = nil, wanted non-nil", c)
   130  		} else if got, want := cond.Status, corev1.ConditionTrue; got != want {
   131  			t.Errorf("GetCondition(%q) = %v, wanted %v", c, got, want)
   132  		}
   133  	}
   134  
   135  	s2 = &Status{}
   136  	s.ConvertTo(context.Background(), s2)
   137  	if !condSet.Manage(s2).IsHappy() {
   138  		t.Error("s2.IsHappy() = false, wanted true")
   139  	}
   140  	if got, want := len(s2.Conditions), 1; got != want {
   141  		t.Errorf("len(s2.Conditions) = %d, wanted %d", got, want)
   142  	}
   143  	if gotGeneration := s2.ObservedGeneration; wantGeneration != gotGeneration {
   144  		t.Errorf("len(s2.ObservedGeneration) = %d, wanted %d",
   145  			gotGeneration, wantGeneration)
   146  	}
   147  	s.Annotations = nil
   148  	s2 = &Status{}
   149  	s.ConvertTo(context.Background(), s2)
   150  	if s2.Annotations != nil {
   151  		t.Error("Annotations were not nil:", s2.Annotations)
   152  	}
   153  }