sigs.k8s.io/cluster-api@v1.6.3/controlplane/kubeadm/internal/control_plane_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 internal 18 19 import ( 20 "testing" 21 22 . "github.com/onsi/gomega" 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 25 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 26 controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1" 27 "sigs.k8s.io/cluster-api/util/collections" 28 "sigs.k8s.io/cluster-api/util/conditions" 29 ) 30 31 func TestControlPlane(t *testing.T) { 32 g := NewWithT(t) 33 34 t.Run("Failure domains", func(t *testing.T) { 35 controlPlane := &ControlPlane{ 36 KCP: &controlplanev1.KubeadmControlPlane{}, 37 Cluster: &clusterv1.Cluster{ 38 Status: clusterv1.ClusterStatus{ 39 FailureDomains: clusterv1.FailureDomains{ 40 "one": failureDomain(true), 41 "two": failureDomain(true), 42 "three": failureDomain(true), 43 "four": failureDomain(false), 44 }, 45 }, 46 }, 47 Machines: collections.Machines{ 48 "machine-1": machine("machine-1", withFailureDomain("one")), 49 "machine-2": machine("machine-2", withFailureDomain("two")), 50 "machine-3": machine("machine-3", withFailureDomain("two")), 51 }, 52 } 53 54 t.Run("With all machines in known failure domain, should return the FD with most number of machines", func(t *testing.T) { 55 g.Expect(*controlPlane.FailureDomainWithMostMachines(controlPlane.Machines)).To(Equal("two")) 56 }) 57 58 t.Run("With some machines in non defined failure domains", func(t *testing.T) { 59 controlPlane.Machines.Insert(machine("machine-5", withFailureDomain("unknown"))) 60 g.Expect(*controlPlane.FailureDomainWithMostMachines(controlPlane.Machines)).To(Equal("unknown")) 61 }) 62 }) 63 } 64 65 func TestHasUnhealthyMachine(t *testing.T) { 66 // healthy machine (without MachineHealthCheckSucceded condition) 67 healthyMachine1 := &clusterv1.Machine{} 68 // healthy machine (with MachineHealthCheckSucceded == true) 69 healthyMachine2 := &clusterv1.Machine{} 70 conditions.MarkTrue(healthyMachine2, clusterv1.MachineHealthCheckSucceededCondition) 71 // unhealthy machine NOT eligible for KCP remediation (with MachineHealthCheckSucceded == False, but without MachineOwnerRemediated condition) 72 unhealthyMachineNOTOwnerRemediated := &clusterv1.Machine{} 73 conditions.MarkFalse(unhealthyMachineNOTOwnerRemediated, clusterv1.MachineHealthCheckSucceededCondition, clusterv1.MachineHasFailureReason, clusterv1.ConditionSeverityWarning, "") 74 // unhealthy machine eligible for KCP remediation (with MachineHealthCheckSucceded == False, with MachineOwnerRemediated condition) 75 unhealthyMachineOwnerRemediated := &clusterv1.Machine{} 76 conditions.MarkFalse(unhealthyMachineOwnerRemediated, clusterv1.MachineHealthCheckSucceededCondition, clusterv1.MachineHasFailureReason, clusterv1.ConditionSeverityWarning, "") 77 conditions.MarkFalse(unhealthyMachineOwnerRemediated, clusterv1.MachineOwnerRemediatedCondition, clusterv1.WaitingForRemediationReason, clusterv1.ConditionSeverityWarning, "") 78 79 c := ControlPlane{ 80 Machines: collections.FromMachines( 81 healthyMachine1, 82 healthyMachine2, 83 unhealthyMachineNOTOwnerRemediated, 84 unhealthyMachineOwnerRemediated, 85 ), 86 } 87 88 g := NewWithT(t) 89 g.Expect(c.HasUnhealthyMachine()).To(BeTrue()) 90 } 91 92 type machineOpt func(*clusterv1.Machine) 93 94 func failureDomain(controlPlane bool) clusterv1.FailureDomainSpec { 95 return clusterv1.FailureDomainSpec{ 96 ControlPlane: controlPlane, 97 } 98 } 99 100 func withFailureDomain(fd string) machineOpt { 101 return func(m *clusterv1.Machine) { 102 m.Spec.FailureDomain = &fd 103 } 104 } 105 106 func machine(name string, opts ...machineOpt) *clusterv1.Machine { 107 m := &clusterv1.Machine{ 108 ObjectMeta: metav1.ObjectMeta{ 109 Name: name, 110 }, 111 } 112 for _, opt := range opts { 113 opt(m) 114 } 115 return m 116 }