github.com/verrazzano/verrazzano@v1.7.0/tools/vz/cmd/status/status_test.go (about)

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