sigs.k8s.io/cluster-api@v1.7.1/internal/contract/references_test.go (about)

     1  /*
     2  Copyright 2021 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 contract
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    24  )
    25  
    26  var fooRefBuilder = func() *unstructured.Unstructured {
    27  	refObj := &unstructured.Unstructured{}
    28  	refObj.SetAPIVersion("fooApiVersion")
    29  	refObj.SetKind("fooKind")
    30  	refObj.SetNamespace("fooNamespace")
    31  	refObj.SetName("fooName")
    32  	return refObj
    33  }
    34  
    35  func TestGetNestedRef(t *testing.T) {
    36  	t.Run("Gets a nested ref if defined", func(t *testing.T) {
    37  		g := NewWithT(t)
    38  
    39  		refObj := fooRefBuilder()
    40  		obj := &unstructured.Unstructured{Object: map[string]interface{}{}}
    41  
    42  		err := SetNestedRef(obj, refObj, "spec", "machineTemplate", "infrastructureRef")
    43  		g.Expect(err).ToNot(HaveOccurred())
    44  
    45  		ref, err := GetNestedRef(obj, "spec", "machineTemplate", "infrastructureRef")
    46  		g.Expect(err).ToNot(HaveOccurred())
    47  		g.Expect(ref).ToNot(BeNil())
    48  		g.Expect(ref.APIVersion).To(Equal(refObj.GetAPIVersion()))
    49  		g.Expect(ref.Kind).To(Equal(refObj.GetKind()))
    50  		g.Expect(ref.Name).To(Equal(refObj.GetName()))
    51  		g.Expect(ref.Namespace).To(Equal(refObj.GetNamespace()))
    52  	})
    53  	t.Run("getNestedRef fails if the nested ref does not exist", func(t *testing.T) {
    54  		g := NewWithT(t)
    55  
    56  		obj := &unstructured.Unstructured{Object: map[string]interface{}{}}
    57  
    58  		ref, err := GetNestedRef(obj, "spec", "machineTemplate", "infrastructureRef")
    59  		g.Expect(err).To(HaveOccurred())
    60  		g.Expect(ref).To(BeNil())
    61  	})
    62  	t.Run("getNestedRef fails if the nested ref exist but it is incomplete", func(t *testing.T) {
    63  		g := NewWithT(t)
    64  
    65  		obj := &unstructured.Unstructured{Object: map[string]interface{}{}}
    66  
    67  		err := unstructured.SetNestedField(obj.UnstructuredContent(), "foo", "spec", "machineTemplate", "infrastructureRef", "kind")
    68  		g.Expect(err).ToNot(HaveOccurred())
    69  		err = unstructured.SetNestedField(obj.UnstructuredContent(), "bar", "spec", "machineTemplate", "infrastructureRef", "namespace")
    70  		g.Expect(err).ToNot(HaveOccurred())
    71  		err = unstructured.SetNestedField(obj.UnstructuredContent(), "baz", "spec", "machineTemplate", "infrastructureRef", "apiVersion")
    72  		g.Expect(err).ToNot(HaveOccurred())
    73  		// Reference name missing
    74  
    75  		ref, err := GetNestedRef(obj, "spec", "machineTemplate", "infrastructureRef")
    76  		g.Expect(err).To(HaveOccurred())
    77  		g.Expect(ref).To(BeNil())
    78  	})
    79  }
    80  
    81  func TestSetNestedRef(t *testing.T) {
    82  	t.Run("Sets a nested ref", func(t *testing.T) {
    83  		g := NewWithT(t)
    84  
    85  		refObj := fooRefBuilder()
    86  		obj := &unstructured.Unstructured{Object: map[string]interface{}{}}
    87  
    88  		err := SetNestedRef(obj, refObj, "spec", "machineTemplate", "infrastructureRef")
    89  		g.Expect(err).ToNot(HaveOccurred())
    90  
    91  		ref, err := GetNestedRef(obj, "spec", "machineTemplate", "infrastructureRef")
    92  		g.Expect(err).ToNot(HaveOccurred())
    93  		g.Expect(ref).ToNot(BeNil())
    94  		g.Expect(ref.APIVersion).To(Equal(refObj.GetAPIVersion()))
    95  		g.Expect(ref.Kind).To(Equal(refObj.GetKind()))
    96  		g.Expect(ref.Name).To(Equal(refObj.GetName()))
    97  		g.Expect(ref.Namespace).To(Equal(refObj.GetNamespace()))
    98  	})
    99  }
   100  
   101  func TestObjToRef(t *testing.T) {
   102  	t.Run("Gets a ref from an obj", func(t *testing.T) {
   103  		g := NewWithT(t)
   104  
   105  		refObj := fooRefBuilder()
   106  		ref := ObjToRef(refObj)
   107  
   108  		g.Expect(ref).ToNot(BeNil())
   109  		g.Expect(ref.APIVersion).To(Equal(refObj.GetAPIVersion()))
   110  		g.Expect(ref.Kind).To(Equal(refObj.GetKind()))
   111  		g.Expect(ref.Name).To(Equal(refObj.GetName()))
   112  		g.Expect(ref.Namespace).To(Equal(refObj.GetNamespace()))
   113  	})
   114  }