github.com/verrazzano/verrazzano@v1.7.0/pkg/k8s/ready/available_test.go (about) 1 // Copyright (c) 2022, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package ready 5 6 import ( 7 "github.com/stretchr/testify/assert" 8 "github.com/verrazzano/verrazzano/pkg/log/vzlog" 9 vzapi "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1alpha1" 10 appsv1 "k8s.io/api/apps/v1" 11 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 12 "k8s.io/apimachinery/pkg/types" 13 clipkg "sigs.k8s.io/controller-runtime/pkg/client" 14 "sigs.k8s.io/controller-runtime/pkg/client/fake" 15 "testing" 16 ) 17 18 func TestIsComponentAvailable(t *testing.T) { 19 const ( 20 zeroReplicas = 0 21 oneReplica = 1 22 name = "foo" 23 namespace = "bar" 24 ) 25 emptyClient := fake.NewClientBuilder().WithScheme(getScheme()).Build() 26 selectors := []clipkg.ListOption{ 27 clipkg.InNamespace(namespace), 28 } 29 nsn := types.NamespacedName{ 30 Name: name, 31 Namespace: namespace, 32 } 33 objectMeta := metav1.ObjectMeta{ 34 Name: name, 35 Namespace: namespace, 36 } 37 readyAndAvailableClient := fake.NewClientBuilder().WithScheme(getScheme()). 38 WithObjects(&appsv1.Deployment{ 39 ObjectMeta: objectMeta, 40 Status: appsv1.DeploymentStatus{ 41 Replicas: oneReplica, 42 ReadyReplicas: oneReplica, 43 }, 44 }, &appsv1.StatefulSet{ 45 ObjectMeta: objectMeta, 46 Status: appsv1.StatefulSetStatus{ 47 Replicas: oneReplica, 48 ReadyReplicas: oneReplica, 49 }, 50 }, &appsv1.DaemonSet{ 51 ObjectMeta: objectMeta, 52 Status: appsv1.DaemonSetStatus{ 53 NumberReady: oneReplica, 54 DesiredNumberScheduled: oneReplica, 55 }, 56 }).Build() 57 unreadyClient := fake.NewClientBuilder().WithScheme(getScheme()). 58 WithObjects(&appsv1.Deployment{ 59 ObjectMeta: objectMeta, 60 Status: appsv1.DeploymentStatus{ 61 Replicas: oneReplica, 62 ReadyReplicas: zeroReplicas, 63 }, 64 }, &appsv1.StatefulSet{ 65 ObjectMeta: objectMeta, 66 Status: appsv1.StatefulSetStatus{ 67 Replicas: oneReplica, 68 ReadyReplicas: zeroReplicas, 69 }, 70 }, &appsv1.DaemonSet{ 71 ObjectMeta: objectMeta, 72 Status: appsv1.DaemonSetStatus{ 73 DesiredNumberScheduled: oneReplica, 74 NumberReady: zeroReplicas, 75 }, 76 }).Build() 77 var tests = []struct { 78 name string 79 ao *AvailabilityObjects 80 client clipkg.Client 81 available vzapi.ComponentAvailability 82 }{ 83 { 84 "available when no objects", 85 &AvailabilityObjects{}, 86 emptyClient, 87 vzapi.ComponentAvailable, 88 }, 89 { 90 "unavailable when deploy not present", 91 &AvailabilityObjects{ 92 DeploymentNames: []types.NamespacedName{nsn}, 93 }, 94 emptyClient, 95 vzapi.ComponentUnavailable, 96 }, 97 { 98 "unavailable when sts not present", 99 &AvailabilityObjects{ 100 StatefulsetNames: []types.NamespacedName{nsn}, 101 }, 102 emptyClient, 103 vzapi.ComponentUnavailable, 104 }, 105 { 106 "unavailable when ds not present", 107 &AvailabilityObjects{ 108 DaemonsetNames: []types.NamespacedName{nsn}, 109 }, 110 emptyClient, 111 vzapi.ComponentUnavailable, 112 }, 113 { 114 "unavailable when deploy replicas not ready", 115 &AvailabilityObjects{ 116 DeploymentNames: []types.NamespacedName{nsn}, 117 }, 118 unreadyClient, 119 vzapi.ComponentUnavailable, 120 }, 121 { 122 "unavailable when sts replicas not ready", 123 &AvailabilityObjects{ 124 StatefulsetNames: []types.NamespacedName{nsn}, 125 }, 126 unreadyClient, 127 vzapi.ComponentUnavailable, 128 }, 129 { 130 "unavailable when ds replicas not ready", 131 &AvailabilityObjects{ 132 DaemonsetNames: []types.NamespacedName{nsn}, 133 }, 134 unreadyClient, 135 vzapi.ComponentUnavailable, 136 }, 137 { 138 "available when all objects present", 139 &AvailabilityObjects{ 140 DeploymentNames: []types.NamespacedName{nsn}, 141 StatefulsetNames: []types.NamespacedName{nsn}, 142 DaemonsetNames: []types.NamespacedName{nsn}, 143 }, 144 readyAndAvailableClient, 145 vzapi.ComponentAvailable, 146 }, 147 { 148 "(selectors) unavailable when deployment not ready", 149 &AvailabilityObjects{ 150 DeploymentSelectors: selectors, 151 }, 152 unreadyClient, 153 vzapi.ComponentUnavailable, 154 }, 155 { 156 "(selectors) unavailable when deployment not found", 157 &AvailabilityObjects{ 158 DeploymentSelectors: selectors, 159 }, 160 emptyClient, 161 vzapi.ComponentUnavailable, 162 }, 163 { 164 "(selectors) available when all objects present", 165 &AvailabilityObjects{ 166 DeploymentSelectors: selectors, 167 }, 168 readyAndAvailableClient, 169 vzapi.ComponentAvailable, 170 }, 171 } 172 173 log := vzlog.DefaultLogger() 174 for _, tt := range tests { 175 t.Run(tt.name, func(t *testing.T) { 176 _, available := tt.ao.IsAvailable(log, tt.client) 177 assert.Equal(t, tt.available, available) 178 }) 179 } 180 }