sigs.k8s.io/cluster-api@v1.7.1/util/conditions/unstructured_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  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    26  	"k8s.io/client-go/kubernetes/scheme"
    27  
    28  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    29  )
    30  
    31  func TestUnstructuredGetConditions(t *testing.T) {
    32  	g := NewWithT(t)
    33  
    34  	// GetConditions should return conditions from an unstructured object
    35  	c := &clusterv1.Cluster{}
    36  	c.SetConditions(conditionList(true1))
    37  	u := &unstructured.Unstructured{}
    38  	g.Expect(scheme.Scheme.Convert(c, u, nil)).To(Succeed())
    39  
    40  	g.Expect(UnstructuredGetter(u).GetConditions()).To(haveSameConditionsOf(conditionList(true1)))
    41  
    42  	// GetConditions should return nil for an unstructured object with empty conditions
    43  	c = &clusterv1.Cluster{}
    44  	u = &unstructured.Unstructured{}
    45  	g.Expect(scheme.Scheme.Convert(c, u, nil)).To(Succeed())
    46  
    47  	g.Expect(UnstructuredGetter(u).GetConditions()).To(BeNil())
    48  
    49  	// GetConditions should return nil for an unstructured object without conditions
    50  	e := &corev1.Endpoints{}
    51  	u = &unstructured.Unstructured{}
    52  	g.Expect(scheme.Scheme.Convert(e, u, nil)).To(Succeed())
    53  
    54  	g.Expect(UnstructuredGetter(u).GetConditions()).To(BeNil())
    55  
    56  	// GetConditions should return conditions from an unstructured object with a different type of conditions.
    57  	p := &corev1.Pod{Status: corev1.PodStatus{
    58  		Conditions: []corev1.PodCondition{
    59  			{
    60  				Type:               "foo",
    61  				Status:             "foo",
    62  				LastProbeTime:      metav1.Time{},
    63  				LastTransitionTime: metav1.Time{},
    64  				Reason:             "foo",
    65  				Message:            "foo",
    66  			},
    67  		},
    68  	}}
    69  	u = &unstructured.Unstructured{}
    70  	g.Expect(scheme.Scheme.Convert(p, u, nil)).To(Succeed())
    71  
    72  	g.Expect(UnstructuredGetter(u).GetConditions()).To(HaveLen(1))
    73  }
    74  
    75  func TestUnstructuredSetConditions(t *testing.T) {
    76  	g := NewWithT(t)
    77  
    78  	c := &clusterv1.Cluster{}
    79  	u := &unstructured.Unstructured{}
    80  	g.Expect(scheme.Scheme.Convert(c, u, nil)).To(Succeed())
    81  
    82  	// set conditions
    83  	conditions := conditionList(true1, falseInfo1)
    84  
    85  	s := UnstructuredSetter(u)
    86  	s.SetConditions(conditions)
    87  	g.Expect(s.GetConditions()).To(BeComparableTo(conditions))
    88  }