github.com/verrazzano/verrazzano@v1.7.0/platform-operator/controllers/controller_utils_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 controllers 5 6 import ( 7 vzstatus "github.com/verrazzano/verrazzano/platform-operator/controllers/verrazzano/healthcheck" 8 "k8s.io/apimachinery/pkg/runtime" 9 "sigs.k8s.io/controller-runtime/pkg/client/fake" 10 "testing" 11 12 "github.com/golang/mock/gomock" 13 "github.com/stretchr/testify/assert" 14 15 vzapi "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1alpha1" 16 "github.com/verrazzano/verrazzano/platform-operator/controllers/verrazzano/component/registry" 17 "github.com/verrazzano/verrazzano/platform-operator/controllers/verrazzano/component/spi" 18 "github.com/verrazzano/verrazzano/platform-operator/internal/config" 19 "github.com/verrazzano/verrazzano/platform-operator/mocks" 20 21 corev1 "k8s.io/api/core/v1" 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 "sigs.k8s.io/controller-runtime/pkg/client" 24 ) 25 26 const ( 27 testNS = "verrazzano" 28 testCMName = "po-val" 29 testVZName = "test-vz" 30 ) 31 32 // newScheme creates a new scheme that includes this package's object to use for testing 33 func newScheme() *runtime.Scheme { 34 scheme := runtime.NewScheme() 35 // _ = clientgoscheme.AddToScheme(scheme) 36 // _ = core.AddToScheme(scheme) 37 _ = vzapi.AddToScheme(scheme) 38 return scheme 39 } 40 41 // TestVzContainsResource tests that the component name along with 42 // bool value true is returned if k8s object is referenced in the CR as 43 // override. Return false along with an empty string for other cases 44 func TestVzContainsResource(t *testing.T) { 45 asserts := assert.New(t) 46 mocker := gomock.NewController(t) 47 mock := mocks.NewMockClient(mocker) 48 49 compContext := fakeComponentContext(mock, &testVZ) 50 res0, ok0 := VzContainsResource(compContext, testConfigMap.Name, testConfigMap.Kind) 51 52 asserts.True(ok0) 53 asserts.NotEmpty(res0) 54 asserts.Equal(res0, "prometheus-operator") 55 56 anotherCM := testConfigMap 57 anotherCM.Name = "MonfigCap" 58 59 res1, ok1 := VzContainsResource(compContext, anotherCM.Name, anotherCM.Kind) 60 mocker.Finish() 61 asserts.False(ok1) 62 asserts.Empty(res1) 63 } 64 65 // TestVzContainsResourceMonitoringDisabled tests that if MonitorChanges is set to false, 66 // then the component should be ignored and an empty string along with false bool value is 67 // returned. 68 func TestVzContainsResourceMonitoringDisabled(t *testing.T) { 69 asserts := assert.New(t) 70 mocker := gomock.NewController(t) 71 mock := mocks.NewMockClient(mocker) 72 73 vz := testVZ 74 *vz.Spec.Components.PrometheusOperator.MonitorChanges = false 75 compContext := fakeComponentContext(mock, &vz) 76 res0, ok0 := VzContainsResource(compContext, testConfigMap.Name, testConfigMap.Kind) 77 78 mocker.Finish() 79 asserts.False(ok0) 80 asserts.Empty(res0) 81 } 82 83 // TestUpdateVerrazzanoForInstallOverrides tests that the call to update Verrazzano Status 84 // is made and doesn't return an error 85 func TestUpdateVerrazzanoForInstallOverrides(t *testing.T) { 86 c := fake.NewClientBuilder().WithScheme(newScheme()).WithObjects(&testVZ).Build() 87 statusUpdater := &vzstatus.FakeVerrazzanoStatusUpdater{Client: c} 88 asserts := assert.New(t) 89 config.TestProfilesDir = "../../manifests/profiles" 90 defer func() { config.TestProfilesDir = "" }() 91 92 compContext := fakeComponentContext(c, &testVZ) 93 err := UpdateVerrazzanoForInstallOverrides(statusUpdater, compContext, "prometheus-operator") 94 asserts.Nil(err) 95 } 96 97 // TestUpdateVerrazzanoForInstallOverrides tests that if Verrazzano hasn't initialized component status 98 // an error will be returned by the function 99 func TestUpdateVerrazzanoForInstallOverridesError(t *testing.T) { 100 c := fake.NewClientBuilder().WithScheme(newScheme()).WithObjects(&testVZ).Build() 101 statusUpdater := &vzstatus.FakeVerrazzanoStatusUpdater{Client: c} 102 asserts := assert.New(t) 103 config.TestProfilesDir = "../../manifests/profiles" 104 defer func() { config.TestProfilesDir = "" }() 105 106 vz := testVZ 107 vz.Status.Components = nil 108 compContext := fakeComponentContext(c, &vz) 109 err := UpdateVerrazzanoForInstallOverrides(statusUpdater, compContext, "prometheus-operator") 110 asserts.NotNil(err) 111 } 112 113 var testConfigMap = corev1.ConfigMap{ 114 TypeMeta: metav1.TypeMeta{ 115 Kind: "ConfigMap", 116 }, 117 ObjectMeta: metav1.ObjectMeta{ 118 Name: testCMName, 119 Namespace: testNS, 120 }, 121 Immutable: nil, 122 Data: map[string]string{"override": "true"}, 123 BinaryData: nil, 124 } 125 126 // creates a component context for testing 127 func fakeComponentContext(c client.Client, vz *vzapi.Verrazzano) spi.ComponentContext { 128 compContext := spi.NewFakeContext(c, vz, nil, false) 129 return compContext 130 } 131 132 var compStatusMap = makeVerrazzanoComponentStatusMap() 133 var testVZ = vzapi.Verrazzano{ 134 TypeMeta: metav1.TypeMeta{ 135 APIVersion: "install.verrazzano.io/v1alpha1", 136 Kind: "Verrazzano", 137 }, 138 ObjectMeta: metav1.ObjectMeta{ 139 Name: testVZName, 140 Namespace: testNS, 141 }, 142 Spec: vzapi.VerrazzanoSpec{ 143 Components: vzapi.ComponentSpec{PrometheusOperator: &vzapi.PrometheusOperatorComponent{ 144 Enabled: True(), 145 InstallOverrides: vzapi.InstallOverrides{ 146 MonitorChanges: True(), 147 ValueOverrides: []vzapi.Overrides{ 148 { 149 ConfigMapRef: &corev1.ConfigMapKeySelector{ 150 LocalObjectReference: corev1.LocalObjectReference{ 151 Name: testCMName, 152 }, 153 Key: "", 154 Optional: nil, 155 }, 156 }, 157 }, 158 }, 159 }}, 160 }, 161 Status: vzapi.VerrazzanoStatus{ 162 State: vzapi.VzStateReady, 163 Conditions: []vzapi.Condition{ 164 { 165 Type: vzapi.CondInstallComplete, 166 }, 167 }, 168 Components: compStatusMap, 169 }, 170 } 171 172 // creates a component status map for testing 173 func makeVerrazzanoComponentStatusMap() vzapi.ComponentStatusMap { 174 statusMap := make(vzapi.ComponentStatusMap) 175 for _, comp := range registry.GetComponents() { 176 if comp.IsOperatorInstallSupported() { 177 statusMap[comp.Name()] = &vzapi.ComponentStatusDetails{ 178 Name: comp.Name(), 179 Conditions: []vzapi.Condition{ 180 { 181 Type: vzapi.CondInstallComplete, 182 Status: corev1.ConditionTrue, 183 }, 184 }, 185 State: vzapi.CompStateReady, 186 } 187 } 188 } 189 return statusMap 190 } 191 192 // return address of a bool var with true value 193 func True() *bool { 194 x := true 195 return &x 196 }