sigs.k8s.io/cluster-api@v1.7.1/util/conversion/conversion_test.go (about)

     1  /*
     2  Copyright 2019 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 conversion
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    25  	"k8s.io/apimachinery/pkg/runtime/schema"
    26  
    27  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    28  )
    29  
    30  var (
    31  	oldMachineGVK = schema.GroupVersionKind{
    32  		Group:   clusterv1.GroupVersion.Group,
    33  		Version: "v1old",
    34  		Kind:    "Machine",
    35  	}
    36  )
    37  
    38  func TestMarshalData(t *testing.T) {
    39  	g := NewWithT(t)
    40  
    41  	t.Run("should write source object to destination", func(*testing.T) {
    42  		version := "v1.16.4"
    43  		providerID := "aws://some-id"
    44  		src := &clusterv1.Machine{
    45  			ObjectMeta: metav1.ObjectMeta{
    46  				Name: "test-1",
    47  				Labels: map[string]string{
    48  					"label1": "",
    49  				},
    50  			},
    51  			Spec: clusterv1.MachineSpec{
    52  				ClusterName: "test-cluster",
    53  				Version:     &version,
    54  				ProviderID:  &providerID,
    55  			},
    56  		}
    57  
    58  		dst := &unstructured.Unstructured{}
    59  		dst.SetGroupVersionKind(oldMachineGVK)
    60  		dst.SetName("test-1")
    61  
    62  		g.Expect(MarshalData(src, dst)).To(Succeed())
    63  		// ensure the src object is not modified
    64  		g.Expect(src.GetLabels()).ToNot(BeEmpty())
    65  
    66  		g.Expect(dst.GetAnnotations()[DataAnnotation]).ToNot(BeEmpty())
    67  		g.Expect(dst.GetAnnotations()[DataAnnotation]).To(ContainSubstring("test-cluster"))
    68  		g.Expect(dst.GetAnnotations()[DataAnnotation]).To(ContainSubstring("v1.16.4"))
    69  		g.Expect(dst.GetAnnotations()[DataAnnotation]).To(ContainSubstring("aws://some-id"))
    70  		g.Expect(dst.GetAnnotations()[DataAnnotation]).ToNot(ContainSubstring("metadata"))
    71  		g.Expect(dst.GetAnnotations()[DataAnnotation]).ToNot(ContainSubstring("label1"))
    72  	})
    73  
    74  	t.Run("should append the annotation", func(*testing.T) {
    75  		src := &clusterv1.Machine{
    76  			ObjectMeta: metav1.ObjectMeta{
    77  				Name: "test-1",
    78  			},
    79  		}
    80  		dst := &unstructured.Unstructured{}
    81  		dst.SetGroupVersionKind(clusterv1.GroupVersion.WithKind("Machine"))
    82  		dst.SetName("test-1")
    83  		dst.SetAnnotations(map[string]string{
    84  			"annotation": "1",
    85  		})
    86  
    87  		g.Expect(MarshalData(src, dst)).To(Succeed())
    88  		g.Expect(dst.GetAnnotations()).To(HaveLen(2))
    89  	})
    90  }
    91  
    92  func TestUnmarshalData(t *testing.T) {
    93  	g := NewWithT(t)
    94  
    95  	t.Run("should return false without errors if annotation doesn't exist", func(*testing.T) {
    96  		src := &clusterv1.Machine{
    97  			ObjectMeta: metav1.ObjectMeta{
    98  				Name: "test-1",
    99  			},
   100  		}
   101  		dst := &unstructured.Unstructured{}
   102  		dst.SetGroupVersionKind(oldMachineGVK)
   103  		dst.SetName("test-1")
   104  
   105  		ok, err := UnmarshalData(src, dst)
   106  		g.Expect(ok).To(BeFalse())
   107  		g.Expect(err).ToNot(HaveOccurred())
   108  	})
   109  
   110  	t.Run("should return true when a valid annotation with data exists", func(*testing.T) {
   111  		src := &unstructured.Unstructured{}
   112  		src.SetGroupVersionKind(oldMachineGVK)
   113  		src.SetName("test-1")
   114  		src.SetAnnotations(map[string]string{
   115  			DataAnnotation: "{\"metadata\":{\"name\":\"test-1\",\"creationTimestamp\":null,\"labels\":{\"label1\":\"\"}},\"spec\":{\"clusterName\":\"\",\"bootstrap\":{},\"infrastructureRef\":{}},\"status\":{\"bootstrapReady\":true,\"infrastructureReady\":true}}",
   116  		})
   117  
   118  		dst := &clusterv1.Machine{
   119  			ObjectMeta: metav1.ObjectMeta{
   120  				Name: "test-1",
   121  			},
   122  		}
   123  
   124  		ok, err := UnmarshalData(src, dst)
   125  		g.Expect(err).ToNot(HaveOccurred())
   126  		g.Expect(ok).To(BeTrue())
   127  
   128  		g.Expect(dst.GetLabels()).To(HaveLen(1))
   129  		g.Expect(dst.GetName()).To(Equal("test-1"))
   130  		g.Expect(dst.GetLabels()).To(HaveKeyWithValue("label1", ""))
   131  		g.Expect(dst.GetAnnotations()).To(BeEmpty())
   132  	})
   133  
   134  	t.Run("should clean the annotation on successful unmarshal", func(*testing.T) {
   135  		src := &unstructured.Unstructured{}
   136  		src.SetGroupVersionKind(oldMachineGVK)
   137  		src.SetName("test-1")
   138  		src.SetAnnotations(map[string]string{
   139  			"annotation-1": "",
   140  			DataAnnotation: "{\"metadata\":{\"name\":\"test-1\",\"creationTimestamp\":null,\"labels\":{\"label1\":\"\"}},\"spec\":{\"clusterName\":\"\",\"bootstrap\":{},\"infrastructureRef\":{}},\"status\":{\"bootstrapReady\":true,\"infrastructureReady\":true}}",
   141  		})
   142  
   143  		dst := &clusterv1.Machine{
   144  			ObjectMeta: metav1.ObjectMeta{
   145  				Name: "test-1",
   146  			},
   147  		}
   148  
   149  		ok, err := UnmarshalData(src, dst)
   150  		g.Expect(err).ToNot(HaveOccurred())
   151  		g.Expect(ok).To(BeTrue())
   152  
   153  		g.Expect(src.GetAnnotations()).ToNot(HaveKey(DataAnnotation))
   154  		g.Expect(src.GetAnnotations()).To(HaveLen(1))
   155  	})
   156  }