sigs.k8s.io/cluster-api@v1.6.3/internal/contract/infrastructure_machine_test.go (about) 1 /* 2 Copyright 2022 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 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 26 ) 27 28 func TestInfrastructureMachine(t *testing.T) { 29 obj := &unstructured.Unstructured{Object: map[string]interface{}{}} 30 31 t.Run("Manages spec.providerID", func(t *testing.T) { 32 g := NewWithT(t) 33 34 g.Expect(InfrastructureMachine().ProviderID().Path()).To(Equal(Path{"spec", "providerID"})) 35 36 err := InfrastructureMachine().ProviderID().Set(obj, "fake-provider-id") 37 g.Expect(err).ToNot(HaveOccurred()) 38 39 got, err := InfrastructureMachine().ProviderID().Get(obj) 40 g.Expect(err).ToNot(HaveOccurred()) 41 g.Expect(got).ToNot(BeNil()) 42 g.Expect(*got).To(Equal("fake-provider-id")) 43 }) 44 t.Run("Manages status.ready", func(t *testing.T) { 45 g := NewWithT(t) 46 47 g.Expect(InfrastructureMachine().Ready().Path()).To(Equal(Path{"status", "ready"})) 48 49 err := InfrastructureMachine().Ready().Set(obj, true) 50 g.Expect(err).ToNot(HaveOccurred()) 51 52 got, err := InfrastructureMachine().Ready().Get(obj) 53 g.Expect(err).ToNot(HaveOccurred()) 54 g.Expect(got).ToNot(BeNil()) 55 g.Expect(*got).To(BeTrue()) 56 }) 57 t.Run("Manages optional status.failureReason", func(t *testing.T) { 58 g := NewWithT(t) 59 60 g.Expect(InfrastructureMachine().FailureReason().Path()).To(Equal(Path{"status", "failureReason"})) 61 62 err := InfrastructureMachine().FailureReason().Set(obj, "fake-reason") 63 g.Expect(err).ToNot(HaveOccurred()) 64 65 got, err := InfrastructureMachine().FailureReason().Get(obj) 66 g.Expect(err).ToNot(HaveOccurred()) 67 g.Expect(got).ToNot(BeNil()) 68 g.Expect(*got).To(Equal("fake-reason")) 69 }) 70 t.Run("Manages optional status.failureMessage", func(t *testing.T) { 71 g := NewWithT(t) 72 73 g.Expect(InfrastructureMachine().FailureMessage().Path()).To(Equal(Path{"status", "failureMessage"})) 74 75 err := InfrastructureMachine().FailureMessage().Set(obj, "fake-message") 76 g.Expect(err).ToNot(HaveOccurred()) 77 78 got, err := InfrastructureMachine().FailureMessage().Get(obj) 79 g.Expect(err).ToNot(HaveOccurred()) 80 g.Expect(got).ToNot(BeNil()) 81 g.Expect(*got).To(Equal("fake-message")) 82 }) 83 t.Run("Manages optional status.addresses", func(t *testing.T) { 84 g := NewWithT(t) 85 86 addresses := []clusterv1.MachineAddress{ 87 { 88 Type: clusterv1.MachineHostName, 89 Address: "fake-address-1", 90 }, 91 { 92 Type: clusterv1.MachineInternalIP, 93 Address: "fake-address-2", 94 }, 95 } 96 g.Expect(InfrastructureMachine().Addresses().Path()).To(Equal(Path{"status", "addresses"})) 97 98 err := InfrastructureMachine().Addresses().Set(obj, addresses) 99 g.Expect(err).ToNot(HaveOccurred()) 100 101 got, err := InfrastructureMachine().Addresses().Get(obj) 102 g.Expect(err).ToNot(HaveOccurred()) 103 g.Expect(got).ToNot(BeNil()) 104 g.Expect(*got).To(BeComparableTo(addresses)) 105 }) 106 t.Run("Manages optional spec.failureDomain", func(t *testing.T) { 107 g := NewWithT(t) 108 109 g.Expect(InfrastructureMachine().FailureDomain().Path()).To(Equal(Path{"spec", "failureDomain"})) 110 111 err := InfrastructureMachine().FailureDomain().Set(obj, "fake-failure-domain") 112 g.Expect(err).ToNot(HaveOccurred()) 113 114 got, err := InfrastructureMachine().FailureDomain().Get(obj) 115 g.Expect(err).ToNot(HaveOccurred()) 116 g.Expect(got).ToNot(BeNil()) 117 g.Expect(*got).To(Equal("fake-failure-domain")) 118 }) 119 }