github.com/verrazzano/verrazzano@v1.7.1/tools/vz/cmd/status/status_test.go (about) 1 // Copyright (c) 2022, 2024, 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 status 5 6 import ( 7 "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1beta1" 8 "os" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 vzapi "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1alpha1" 13 "github.com/verrazzano/verrazzano/platform-operator/controllers/verrazzano/component/registry" 14 "github.com/verrazzano/verrazzano/tools/vz/pkg/helpers" 15 "github.com/verrazzano/verrazzano/tools/vz/pkg/templates" 16 testhelpers "github.com/verrazzano/verrazzano/tools/vz/test/helpers" 17 corev1 "k8s.io/api/core/v1" 18 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 19 "sigs.k8s.io/controller-runtime/pkg/client/fake" 20 ) 21 22 var ( 23 name = "verrazzano" 24 namespace = "test" 25 version = "1.2.3" 26 consoleURL = "https://verrazzano.default.10.107.141.8.nip.io" 27 keycloakURL = "https://keycloak.default.10.107.141.8.nip.io" 28 rancherURL = "https://rancher.default.10.107.141.8.nip.io" 29 osURL = "https://elasticsearch.vmi.system.10.107.141.8.nip.io" 30 osdURL = "https://kibana.vmi.system.10.107.141.8.nip.io" 31 grafanaURL = "https://grafana.vmi.system.10.107.141.8.nip.io" 32 prometheusURL = "https://prometheus.vmi.system.10.107.141.8.nip.io" 33 kialiURL = "https://kiali.vmi.system.10.107.141.8.nip.io" 34 jaegerURL = "https://jaeger.default.10.107.141.8.nip.io" 35 ) 36 37 // TestStatusCmd tests the status command 38 // GIVEN an environment with a single VZ resource 39 // 40 // WHEN I run the command vz status 41 // THEN expect a successful status report 42 func TestStatusCmd(t *testing.T) { 43 vz := v1beta1.Verrazzano{ 44 ObjectMeta: metav1.ObjectMeta{ 45 Namespace: namespace, 46 Name: name, 47 }, 48 Spec: v1beta1.VerrazzanoSpec{ 49 Profile: v1beta1.Dev, 50 }, 51 Status: v1beta1.VerrazzanoStatus{ 52 Version: version, 53 VerrazzanoInstance: &v1beta1.InstanceInfo{ 54 ConsoleURL: &consoleURL, 55 KeyCloakURL: &keycloakURL, 56 RancherURL: &rancherURL, 57 OpenSearchURL: &osURL, 58 OpenSearchDashboardsURL: &osdURL, 59 GrafanaURL: &grafanaURL, 60 PrometheusURL: &prometheusURL, 61 KialiURL: &kialiURL, 62 JaegerURL: &jaegerURL, 63 }, 64 Conditions: nil, 65 State: v1beta1.VzStateReconciling, 66 Components: makeVerrazzanoComponentStatusMap(), 67 }, 68 } 69 70 c := fake.NewClientBuilder().WithScheme(helpers.NewScheme()).WithObjects(&vz).Build() 71 72 rc := testhelpers.NewFakeRootCmdContextWithFiles(t) 73 defer testhelpers.CleanUpNewFakeRootCmdContextWithFiles(rc) 74 rc.SetClient(c) 75 statusCmd := NewCmdStatus(rc) 76 assert.NotNil(t, statusCmd) 77 78 // Run the status command, check for the expected status results to be displayed 79 err := statusCmd.Execute() 80 assert.NoError(t, err) 81 outBytes, err := os.ReadFile(rc.Out.Name()) 82 assert.NoError(t, err) 83 result := string(outBytes) 84 85 templateInput := TemplateInput{ 86 Endpoints: getEndpoints(vz.Status.VerrazzanoInstance), 87 Components: getComponents(vz.Status.Components), 88 ComponentsEnabled: false, 89 Name: name, 90 Namespace: namespace, 91 Version: version, 92 State: string(vzapi.VzStateReconciling), 93 Profile: string(vzapi.Dev), 94 AvailableComponents: getAvailableComponents(vz.Status.Available), 95 } 96 expectedResult, err := templates.ApplyTemplate(statusOutputTemplate, templateInput) 97 assert.NoError(t, err) 98 assert.Equal(t, expectedResult, result) 99 } 100 101 // TestStatusCmdDefaultProfile tests the status command 102 // GIVEN an environment with a single VZ resource and the default prod profile 103 // 104 // WHEN I run the command vz status 105 // THEN expect a successful status report 106 func TestStatusCmdDefaultProfile(t *testing.T) { 107 vz := v1beta1.Verrazzano{ 108 ObjectMeta: metav1.ObjectMeta{ 109 Namespace: namespace, 110 Name: name, 111 }, 112 Status: v1beta1.VerrazzanoStatus{ 113 Version: version, 114 VerrazzanoInstance: &v1beta1.InstanceInfo{ 115 ConsoleURL: &consoleURL, 116 KeyCloakURL: &keycloakURL, 117 RancherURL: &rancherURL, 118 OpenSearchURL: &osURL, 119 OpenSearchDashboardsURL: &osdURL, 120 GrafanaURL: &grafanaURL, 121 PrometheusURL: &prometheusURL, 122 KialiURL: &kialiURL, 123 JaegerURL: &jaegerURL, 124 }, 125 Conditions: nil, 126 State: v1beta1.VzStateReconciling, 127 Components: makeVerrazzanoComponentStatusMap(), 128 }, 129 } 130 131 c := fake.NewClientBuilder().WithScheme(helpers.NewScheme()).WithObjects(&vz).Build() 132 133 rc := testhelpers.NewFakeRootCmdContextWithFiles(t) 134 defer testhelpers.CleanUpNewFakeRootCmdContextWithFiles(rc) 135 rc.SetClient(c) 136 statusCmd := NewCmdStatus(rc) 137 assert.NotNil(t, statusCmd) 138 139 // Run the status command, check for the expected status results to be displayed 140 err := statusCmd.Execute() 141 assert.NoError(t, err) 142 outBytes, err := os.ReadFile(rc.Out.Name()) 143 assert.NoError(t, err) 144 result := string(outBytes) 145 templateInput := TemplateInput{ 146 Endpoints: getEndpoints(vz.Status.VerrazzanoInstance), 147 Components: getComponents(vz.Status.Components), 148 ComponentsEnabled: false, 149 Name: name, 150 Namespace: namespace, 151 Version: version, 152 State: string(vzapi.VzStateReconciling), 153 Profile: string(vzapi.Prod), 154 AvailableComponents: getAvailableComponents(vz.Status.Available), 155 } 156 expectedResult, err := templates.ApplyTemplate(statusOutputTemplate, templateInput) 157 assert.NoError(t, err) 158 assert.Equal(t, expectedResult, result) 159 } 160 161 // TestVZNotFound tests the status command 162 // GIVEN an environment with a no VZ resources exist 163 // 164 // WHEN I run the command vz status 165 // THEN expect an error of no VZ resources found 166 func TestVZNotFound(t *testing.T) { 167 168 c := fake.NewClientBuilder().WithScheme(helpers.NewScheme()).WithObjects().Build() 169 170 rc := testhelpers.NewFakeRootCmdContextWithFiles(t) 171 defer testhelpers.CleanUpNewFakeRootCmdContextWithFiles(rc) 172 rc.SetClient(c) 173 statusCmd := NewCmdStatus(rc) 174 assert.NotNil(t, statusCmd) 175 176 // Run the status command, check for the expected status results to be displayed 177 err := statusCmd.Execute() 178 assert.Error(t, err) 179 assert.Equal(t, "Failed to find any Verrazzano resources", err.Error()) 180 } 181 182 // TestStatusMultipleVZ tests the status command 183 // GIVEN an environment with a two VZ resources 184 // 185 // WHEN I run the command vz status 186 // THEN expect an error of only expecting one VZ 187 func TestStatusMultipleVZ(t *testing.T) { 188 name := "verrazzano" 189 namespace1 := "test1" 190 namespace2 := "test2" 191 192 vz1 := v1beta1.Verrazzano{ 193 ObjectMeta: metav1.ObjectMeta{ 194 Namespace: namespace1, 195 Name: name, 196 }, 197 } 198 vz2 := v1beta1.Verrazzano{ 199 ObjectMeta: metav1.ObjectMeta{ 200 Namespace: namespace2, 201 Name: name, 202 }, 203 } 204 205 c := fake.NewClientBuilder().WithScheme(helpers.NewScheme()).WithObjects(&vz1, &vz2).Build() 206 207 rc := testhelpers.NewFakeRootCmdContextWithFiles(t) 208 defer testhelpers.CleanUpNewFakeRootCmdContextWithFiles(rc) 209 rc.SetClient(c) 210 statusCmd := NewCmdStatus(rc) 211 assert.NotNil(t, statusCmd) 212 213 // Run the status command, check for the expected status results to be displayed 214 err := statusCmd.Execute() 215 assert.Error(t, err) 216 assert.Equal(t, "Expected to only find one Verrazzano resource, but found 2", err.Error()) 217 } 218 219 func TestNilInstance(t *testing.T) { 220 vz := v1beta1.Verrazzano{ 221 ObjectMeta: metav1.ObjectMeta{ 222 Namespace: namespace, 223 Name: name, 224 }, 225 Status: v1beta1.VerrazzanoStatus{ 226 Version: version, 227 Conditions: nil, 228 State: v1beta1.VzStateReconciling, 229 Components: makeVerrazzanoComponentStatusMap(), 230 }, 231 } 232 c := fake.NewClientBuilder().WithScheme(helpers.NewScheme()).WithObjects(&vz).Build() 233 rc := testhelpers.NewFakeRootCmdContextWithFiles(t) 234 defer testhelpers.CleanUpNewFakeRootCmdContextWithFiles(rc) 235 rc.SetClient(c) 236 statusCmd := NewCmdStatus(rc) 237 assert.NotNil(t, statusCmd) 238 // Run the status command, check for the expected status results to be displayed 239 err := statusCmd.Execute() 240 assert.NoError(t, err) 241 } 242 243 func makeVerrazzanoComponentStatusMap() v1beta1.ComponentStatusMap { 244 statusMap := make(v1beta1.ComponentStatusMap) 245 for _, comp := range registry.GetComponents() { 246 if comp.IsOperatorInstallSupported() { 247 statusMap[comp.Name()] = &v1beta1.ComponentStatusDetails{ 248 Name: comp.Name(), 249 Conditions: []v1beta1.Condition{ 250 { 251 Type: v1beta1.CondInstallComplete, 252 Status: corev1.ConditionTrue, 253 }, 254 }, 255 State: v1beta1.CompStateReady, 256 } 257 } 258 } 259 return statusMap 260 }