github.com/verrazzano/verrazzano@v1.7.0/tools/vz/cmd/uninstall/uninstall_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 uninstall
     5  
     6  import (
     7  	"bytes"
     8  	"context"
     9  	"os"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  	vzconstants "github.com/verrazzano/verrazzano/pkg/constants"
    15  	vzapi "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1alpha1"
    16  	"github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1beta1"
    17  	"github.com/verrazzano/verrazzano/tools/vz/pkg/constants"
    18  	"github.com/verrazzano/verrazzano/tools/vz/pkg/helpers"
    19  	testhelpers "github.com/verrazzano/verrazzano/tools/vz/test/helpers"
    20  	adminv1 "k8s.io/api/admissionregistration/v1"
    21  	appsv1 "k8s.io/api/apps/v1"
    22  	corev1 "k8s.io/api/core/v1"
    23  	rbacv1 "k8s.io/api/rbac/v1"
    24  	"k8s.io/apimachinery/pkg/api/errors"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/types"
    27  	"k8s.io/cli-runtime/pkg/genericclioptions"
    28  	dynfake "k8s.io/client-go/dynamic/fake"
    29  	ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
    30  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    31  )
    32  
    33  const (
    34  	testKubeConfig    = "kubeconfig"
    35  	testK8sContext    = "testcontext"
    36  	VzVpoFailureError = "Failed to find the Verrazzano platform operator in namespace verrazzano-install"
    37  	PodNotFoundError  = "Waiting for verrazzano-uninstall-verrazzano, verrazzano-uninstall-verrazzano pod not found in namespace verrazzano-install"
    38  	BugReportNotExist = "cannot find bug report file in current directory"
    39  )
    40  
    41  // TestUninstallCmd
    42  // GIVEN a CLI uninstall command with all defaults
    43  //
    44  //	WHEN I call cmd.Execute for uninstall
    45  //	THEN the CLI uninstall command is successful
    46  func TestUninstallCmd(t *testing.T) {
    47  	deployment := createVpoDeployment(map[string]string{"app.kubernetes.io/version": "1.4.0"})
    48  	vpo := createVpoPod()
    49  	namespace := createNamespace()
    50  	validatingWebhookConfig := createWebhook()
    51  	clusterRoleBinding := createClusterRoleBinding()
    52  	mcClusterRole := createMCClusterRole()
    53  	registrarClusterRole := createRegistrarClusterRoleForRancher()
    54  	vz := createVz()
    55  	c := fake.NewClientBuilder().WithScheme(helpers.NewScheme()).WithObjects(deployment, vpo, vz, namespace, validatingWebhookConfig, clusterRoleBinding, mcClusterRole, registrarClusterRole).Build()
    56  
    57  	// Send stdout stderr to a byte buffer
    58  	buf := new(bytes.Buffer)
    59  	errBuf := new(bytes.Buffer)
    60  	rc := testhelpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf})
    61  	rc.SetClient(c)
    62  	cmd := NewCmdUninstall(rc)
    63  	assert.NotNil(t, cmd)
    64  
    65  	// Suppressing uninstall prompt
    66  	cmd.PersistentFlags().Set(ConfirmUninstallFlag, "true")
    67  
    68  	// Run uninstall command, check for the expected status results to be displayed
    69  	err := cmd.Execute()
    70  	assert.NoError(t, err)
    71  	assert.Equal(t, "", errBuf.String())
    72  	assert.Contains(t, buf.String(), "Uninstalling Verrazzano\n")
    73  
    74  	// Ensure resources have been deleted
    75  	ensureResourcesDeleted(t, c)
    76  }
    77  
    78  // TestUninstallCmdUninstallJob
    79  // GIVEN a CLI uninstall command with all defaults and a 1.3.1 version install
    80  //
    81  //	WHEN I call cmd.Execute for uninstall
    82  //	THEN the CLI uninstall command is successful
    83  func TestUninstallCmdUninstallJob(t *testing.T) {
    84  	deployment := createVpoDeployment(nil)
    85  	job := &corev1.Pod{
    86  		ObjectMeta: metav1.ObjectMeta{
    87  			Namespace: vzconstants.VerrazzanoInstallNamespace,
    88  			Name:      constants.VerrazzanoUninstall,
    89  			Labels: map[string]string{
    90  				"job-name": constants.VerrazzanoUninstall + "-verrazzano",
    91  			},
    92  		},
    93  		Status: corev1.PodStatus{
    94  			ContainerStatuses: []corev1.ContainerStatus{
    95  				{
    96  					Ready: true,
    97  				},
    98  			},
    99  		},
   100  	}
   101  	namespace := createNamespace()
   102  	validatingWebhookConfig := createWebhook()
   103  	clusterRoleBinding := createClusterRoleBinding()
   104  	mcClusterRole := createMCClusterRole()
   105  	registrarClusterRole := createRegistrarClusterRoleForRancher()
   106  	vz := createVz()
   107  	c := fake.NewClientBuilder().WithScheme(helpers.NewScheme()).WithObjects(deployment, job, vz, namespace, validatingWebhookConfig, clusterRoleBinding, mcClusterRole, registrarClusterRole).Build()
   108  
   109  	// Send stdout stderr to a byte buffer
   110  	buf := new(bytes.Buffer)
   111  	errBuf := new(bytes.Buffer)
   112  	rc := testhelpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf})
   113  	rc.SetClient(c)
   114  	cmd := NewCmdUninstall(rc)
   115  	assert.NotNil(t, cmd)
   116  
   117  	// Suppressing uninstall prompt
   118  	cmd.PersistentFlags().Set(ConfirmUninstallFlag, "true")
   119  
   120  	// Run uninstall command, check for the expected status results to be displayed
   121  	err := cmd.Execute()
   122  	assert.NoError(t, err)
   123  	assert.Equal(t, "", errBuf.String())
   124  	assert.Contains(t, buf.String(), "Uninstalling Verrazzano\n")
   125  
   126  	// Ensure resources have been deleted
   127  	ensureResourcesDeleted(t, c)
   128  }
   129  
   130  // TestUninstallCmdDefaultTimeout
   131  // GIVEN a CLI uninstall command with all defaults and --timeout=2ms
   132  //
   133  //	WHEN I call cmd.Execute for uninstall
   134  //	THEN the CLI uninstall command times out and a bug-report is generated
   135  func TestUninstallCmdDefaultTimeout(t *testing.T) {
   136  	deployment := createVpoDeployment(map[string]string{"app.kubernetes.io/version": "1.4.0"})
   137  	vpo := createVpoPod()
   138  	namespace := createNamespace()
   139  	vz := createVz()
   140  	webhook := createWebhook()
   141  	mcClusterRole := createMCClusterRole()
   142  	registrarClusterRole := createRegistrarClusterRoleForRancher()
   143  	crb := createClusterRoleBinding()
   144  	c := fake.NewClientBuilder().WithScheme(helpers.NewScheme()).WithObjects(deployment, vpo, vz, namespace, webhook, mcClusterRole, registrarClusterRole, crb).Build()
   145  
   146  	// Send stdout stderr to a byte buffer
   147  	buf := new(bytes.Buffer)
   148  	errBuf := new(bytes.Buffer)
   149  	rc := testhelpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf})
   150  	rc.SetClient(c)
   151  	rc.SetDynamicClient(dynfake.NewSimpleDynamicClient(helpers.GetScheme()))
   152  	cmd := NewCmdUninstall(rc)
   153  	assert.NotNil(t, cmd)
   154  	tempKubeConfigPath, _ := os.CreateTemp(os.TempDir(), testKubeConfig)
   155  	cmd.Flags().String(constants.GlobalFlagKubeConfig, tempKubeConfigPath.Name(), "")
   156  	cmd.Flags().String(constants.GlobalFlagContext, testK8sContext, "")
   157  	_ = cmd.PersistentFlags().Set(constants.TimeoutFlag, "2ms")
   158  	defer os.RemoveAll(tempKubeConfigPath.Name())
   159  
   160  	// Suppressing uninstall prompt
   161  	cmd.PersistentFlags().Set(ConfirmUninstallFlag, "true")
   162  
   163  	// Run upgrade command
   164  	err := cmd.Execute()
   165  	assert.Error(t, err)
   166  	// This must be less than the 1 second polling delay to pass
   167  	// since the Verrazzano resource gets deleted almost instantaneously
   168  	assert.Equal(t, "Error: Timeout 2ms exceeded waiting for uninstall to complete\n", errBuf.String())
   169  	ensureResourcesNotDeleted(t, c)
   170  	if !helpers.CheckAndRemoveBugReportExistsInDir("") {
   171  		t.Fatal(BugReportNotExist)
   172  	}
   173  }
   174  
   175  // TestUninstallCmdDefaultTimeoutNoBugReport
   176  // GIVEN a CLI uninstall command with all defaults, --timeout=2ms, and auto-bug-report=false
   177  //
   178  //	WHEN I call cmd.Execute for uninstall
   179  //	THEN the CLI uninstall command times out and bug-report is not generated
   180  func TestUninstallCmdDefaultTimeoutNoBugReport(t *testing.T) {
   181  	deployment := createVpoDeployment(map[string]string{"app.kubernetes.io/version": "1.4.0"})
   182  	vpo := createVpoPod()
   183  	namespace := createNamespace()
   184  	vz := createVz()
   185  	webhook := createWebhook()
   186  	mcClusterRole := createMCClusterRole()
   187  	registrarClusterRole := createRegistrarClusterRoleForRancher()
   188  	crb := createClusterRoleBinding()
   189  	c := fake.NewClientBuilder().WithScheme(helpers.NewScheme()).WithObjects(deployment, vpo, vz, namespace, webhook, mcClusterRole, registrarClusterRole, crb).Build()
   190  
   191  	// Send stdout stderr to a byte buffer
   192  	buf := new(bytes.Buffer)
   193  	errBuf := new(bytes.Buffer)
   194  	rc := testhelpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf})
   195  	rc.SetClient(c)
   196  	cmd := NewCmdUninstall(rc)
   197  	assert.NotNil(t, cmd)
   198  	_ = cmd.PersistentFlags().Set(constants.TimeoutFlag, "2ms")
   199  	_ = cmd.PersistentFlags().Set(constants.AutoBugReportFlag, "false")
   200  
   201  	// Suppressing uninstall prompt
   202  	cmd.PersistentFlags().Set(ConfirmUninstallFlag, "true")
   203  
   204  	// Run upgrade command
   205  	err := cmd.Execute()
   206  	assert.Error(t, err)
   207  	// This must be less than the 1 second polling delay to pass
   208  	// since the Verrazzano resource gets deleted almost instantaneously
   209  	assert.Equal(t, "Error: Timeout 2ms exceeded waiting for uninstall to complete\n", errBuf.String())
   210  	ensureResourcesNotDeleted(t, c)
   211  	// Bug Report must not exist
   212  	if helpers.CheckAndRemoveBugReportExistsInDir("") {
   213  		t.Fatal("found bug report file in current directory")
   214  	}
   215  }
   216  
   217  // TestUninstallCmdDefaultNoWait
   218  // GIVEN a CLI uninstall command with all defaults and --wait==false
   219  //
   220  //	WHEN I call cmd.Execute for uninstall
   221  //	THEN the CLI uninstall command is successful
   222  func TestUninstallCmdDefaultNoWait(t *testing.T) {
   223  	deployment := createVpoDeployment(map[string]string{"app.kubernetes.io/version": "1.4.0"})
   224  	vpo := createVpoPod()
   225  	namespace := createNamespace()
   226  	vz := createVz()
   227  	webhook := createWebhook()
   228  	mcClusterRole := createMCClusterRole()
   229  	registrarClusterRole := createRegistrarClusterRoleForRancher()
   230  	crb := createClusterRoleBinding()
   231  	c := fake.NewClientBuilder().WithScheme(helpers.NewScheme()).WithObjects(deployment, vpo, vz, namespace, webhook, mcClusterRole, registrarClusterRole, crb).Build()
   232  
   233  	// Send stdout stderr to a byte buffer
   234  	buf := new(bytes.Buffer)
   235  	errBuf := new(bytes.Buffer)
   236  	rc := testhelpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf})
   237  	rc.SetClient(c)
   238  	cmd := NewCmdUninstall(rc)
   239  	assert.NotNil(t, cmd)
   240  	_ = cmd.PersistentFlags().Set(constants.WaitFlag, "false")
   241  
   242  	// Suppressing uninstall prompt
   243  	cmd.PersistentFlags().Set(ConfirmUninstallFlag, "true")
   244  
   245  	// Run uninstall command
   246  	err := cmd.Execute()
   247  	assert.NoError(t, err)
   248  	assert.Equal(t, "", errBuf.String())
   249  
   250  	ensureResourcesNotDeleted(t, c)
   251  }
   252  
   253  // TestUninstallCmdJsonLogFormat
   254  // GIVEN a CLI uninstall command with defaults and --log-format=json and --wait==false
   255  //
   256  //	WHEN I call cmd.Execute for uninstall
   257  //	THEN the CLI uninstall command is successful
   258  func TestUninstallCmdJsonLogFormat(t *testing.T) {
   259  	deployment := createVpoDeployment(map[string]string{"app.kubernetes.io/version": "1.4.0"})
   260  	vz := createVz()
   261  	vpo := createVpoPod()
   262  	c := fake.NewClientBuilder().WithScheme(helpers.NewScheme()).WithObjects(deployment, vz, vpo).Build()
   263  
   264  	// Send stdout stderr to a byte buffer
   265  	buf := new(bytes.Buffer)
   266  	errBuf := new(bytes.Buffer)
   267  	rc := testhelpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf})
   268  	rc.SetClient(c)
   269  	cmd := NewCmdUninstall(rc)
   270  	assert.NotNil(t, cmd)
   271  	cmd.PersistentFlags().Set(constants.LogFormatFlag, "json")
   272  	cmd.PersistentFlags().Set(constants.WaitFlag, "false")
   273  
   274  	// Suppressing uninstall prompt
   275  	cmd.PersistentFlags().Set(ConfirmUninstallFlag, "true")
   276  
   277  	// Run uninstall command
   278  	err := cmd.Execute()
   279  	assert.NoError(t, err)
   280  	assert.Equal(t, "", errBuf.String())
   281  }
   282  
   283  // TestUninstallCmdDefaultNoVPO
   284  // GIVEN a CLI uninstall command with all defaults and no VPO found
   285  //
   286  //	WHEN I call cmd.Execute for uninstall
   287  //	THEN the CLI uninstall command fails and a bug-report is generated
   288  func TestUninstallCmdDefaultNoVPO(t *testing.T) {
   289  	deployment := createVpoDeployment(map[string]string{"app.kubernetes.io/version": "1.4.0"})
   290  	vz := createVz()
   291  	c := fake.NewClientBuilder().WithScheme(helpers.NewScheme()).WithObjects(deployment, vz).Build()
   292  
   293  	// Send stdout stderr to a byte buffer
   294  	buf := new(bytes.Buffer)
   295  	errBuf := new(bytes.Buffer)
   296  	rc := testhelpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf})
   297  	rc.SetClient(c)
   298  	rc.SetDynamicClient(dynfake.NewSimpleDynamicClient(helpers.GetScheme()))
   299  	cmd := NewCmdUninstall(rc)
   300  	assert.NotNil(t, cmd)
   301  	tempKubeConfigPath, _ := os.CreateTemp(os.TempDir(), testKubeConfig)
   302  	cmd.Flags().String(constants.GlobalFlagKubeConfig, tempKubeConfigPath.Name(), "")
   303  	cmd.Flags().String(constants.GlobalFlagContext, testK8sContext, "")
   304  	defer os.RemoveAll(tempKubeConfigPath.Name())
   305  
   306  	// Suppressing uninstall prompt
   307  	cmd.PersistentFlags().Set(ConfirmUninstallFlag, "true")
   308  
   309  	// Run uninstall command
   310  	err := cmd.Execute()
   311  	assert.Error(t, err)
   312  	assert.ErrorContains(t, err, VzVpoFailureError)
   313  	assert.Contains(t, errBuf.String(), VzVpoFailureError)
   314  	if !helpers.CheckAndRemoveBugReportExistsInDir("") {
   315  		t.Fatal(BugReportNotExist)
   316  	}
   317  }
   318  
   319  // TestUninstallCmdDefaultNoUninstallJob
   320  // GIVEN a CLI uninstall command with all defaults and no uninstall job pod
   321  //
   322  //	WHEN I call cmd.Execute for uninstall
   323  //	THEN the CLI uninstall command fails and a bug-report is generated
   324  func TestUninstallCmdDefaultNoUninstallJob(t *testing.T) {
   325  	deployment := createVpoDeployment(map[string]string{"app.kubernetes.io/version": "1.3.0"})
   326  	vz := createVz()
   327  	c := fake.NewClientBuilder().WithScheme(helpers.NewScheme()).WithObjects(deployment, vz).Build()
   328  
   329  	// Send stdout stderr to a byte buffer
   330  	buf := new(bytes.Buffer)
   331  	errBuf := new(bytes.Buffer)
   332  	rc := testhelpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf})
   333  	rc.SetClient(c)
   334  	rc.SetDynamicClient(dynfake.NewSimpleDynamicClient(helpers.GetScheme()))
   335  	cmd := NewCmdUninstall(rc)
   336  	assert.NotNil(t, cmd)
   337  	cmd.PersistentFlags().Set(constants.LogFormatFlag, "simple")
   338  	tempKubeConfigPath, _ := os.CreateTemp(os.TempDir(), testKubeConfig)
   339  	cmd.Flags().String(constants.GlobalFlagKubeConfig, tempKubeConfigPath.Name(), "")
   340  	cmd.Flags().String(constants.GlobalFlagContext, testK8sContext, "")
   341  
   342  	setWaitRetries(1)
   343  	defer resetWaitRetries()
   344  	defer os.RemoveAll(tempKubeConfigPath.Name())
   345  
   346  	// Suppressing uninstall prompt
   347  	cmd.PersistentFlags().Set(ConfirmUninstallFlag, "true")
   348  
   349  	// Run uninstall command
   350  	err := cmd.Execute()
   351  	assert.Error(t, err)
   352  	assert.ErrorContains(t, err, PodNotFoundError)
   353  	assert.Contains(t, errBuf.String(), PodNotFoundError)
   354  	if !helpers.CheckAndRemoveBugReportExistsInDir("") {
   355  		t.Fatal(BugReportNotExist)
   356  	}
   357  }
   358  
   359  // TestUninstallCmdDefaultNoVzResource
   360  // GIVEN a CLI uninstall command with all defaults and no vz resource found
   361  //
   362  //	WHEN I call cmd.Execute for uninstall
   363  //	THEN the CLI uninstall command fails and bug report should not be generated
   364  func TestUninstallCmdDefaultNoVzResource(t *testing.T) {
   365  	c := fake.NewClientBuilder().WithScheme(helpers.NewScheme()).Build()
   366  
   367  	// Send stdout stderr to a byte buffer
   368  	buf := new(bytes.Buffer)
   369  	errBuf := new(bytes.Buffer)
   370  	rc := testhelpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf})
   371  	rc.SetClient(c)
   372  	rc.SetDynamicClient(dynfake.NewSimpleDynamicClient(helpers.GetScheme()))
   373  	cmd := NewCmdUninstall(rc)
   374  	tempKubeConfigPath, _ := os.CreateTemp(os.TempDir(), testKubeConfig)
   375  	cmd.Flags().String(constants.GlobalFlagKubeConfig, tempKubeConfigPath.Name(), "")
   376  	cmd.Flags().String(constants.GlobalFlagContext, testK8sContext, "")
   377  	assert.NotNil(t, cmd)
   378  
   379  	// Suppressing uninstall prompt
   380  	cmd.PersistentFlags().Set(ConfirmUninstallFlag, "true")
   381  
   382  	// Run uninstall command
   383  	err := cmd.Execute()
   384  	assert.Error(t, err)
   385  	assert.ErrorContains(t, err, "Verrazzano is not installed: Failed to find any Verrazzano resources")
   386  	assert.Contains(t, errBuf.String(), "Verrazzano is not installed: Failed to find any Verrazzano resources")
   387  	if helpers.CheckAndRemoveBugReportExistsInDir("") {
   388  		t.Fatal(BugReportNotExist)
   389  	}
   390  }
   391  
   392  // TestUninstallWithConfirmUninstallFlag
   393  // Given the "--skip-confirmation or -y" flag the Verrazzano Uninstall prompt will be suppressed
   394  // any other input to the command-line other than Y or y will kill the uninstall process
   395  func TestUninstallWithConfirmUninstallFlag(t *testing.T) {
   396  	type fields struct {
   397  		cmdLineInput  string
   398  		doesUninstall bool
   399  	}
   400  	tests := []struct {
   401  		name   string
   402  		fields fields
   403  	}{
   404  		{"Suppress Uninstall prompt with --skip-confirmation=true", fields{cmdLineInput: "", doesUninstall: true}},
   405  		{"Proceed with Uninstall, Y", fields{cmdLineInput: "Y", doesUninstall: true}},
   406  		{"Proceed with Uninstall, y", fields{cmdLineInput: "y", doesUninstall: true}},
   407  		{"Halt with Uninstall, n", fields{cmdLineInput: "n", doesUninstall: false}},
   408  		{"Garbage input passed on cmdLine", fields{cmdLineInput: "GARBAGE", doesUninstall: false}},
   409  	}
   410  	for _, tt := range tests {
   411  		t.Run(tt.name, func(t *testing.T) {
   412  			deployment := createVpoDeployment(map[string]string{"app.kubernetes.io/version": "1.4.0"})
   413  			vpo := createVpoPod()
   414  			namespace := createNamespace()
   415  			validatingWebhookConfig := createWebhook()
   416  			clusterRoleBinding := createClusterRoleBinding()
   417  			mcClusterRole := createMCClusterRole()
   418  			registrarClusterRole := createRegistrarClusterRoleForRancher()
   419  			vz := createVz()
   420  			c := fake.NewClientBuilder().WithScheme(helpers.NewScheme()).WithObjects(
   421  				deployment, vpo, vz, namespace, validatingWebhookConfig, clusterRoleBinding, mcClusterRole, registrarClusterRole).Build()
   422  
   423  			if tt.fields.cmdLineInput != "" {
   424  				content := []byte(tt.fields.cmdLineInput)
   425  				tempfile, err := os.CreateTemp("", "test-input.txt")
   426  				if err != nil {
   427  					assert.Error(t, err)
   428  				}
   429  				// clean up tempfile
   430  				defer os.Remove(tempfile.Name())
   431  				if _, err := tempfile.Write(content); err != nil {
   432  					assert.Error(t, err)
   433  				}
   434  				if _, err := tempfile.Seek(0, 0); err != nil {
   435  					assert.Error(t, err)
   436  				}
   437  				oldStdin := os.Stdin
   438  				// Restore original Stdin
   439  				defer func() { os.Stdin = oldStdin }()
   440  				os.Stdin = tempfile
   441  			}
   442  
   443  			// Send stdout stderr to a byte bufferF
   444  			buf := new(bytes.Buffer)
   445  			errBuf := new(bytes.Buffer)
   446  			rc := testhelpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf})
   447  			rc.SetClient(c)
   448  			rc.SetDynamicClient(dynfake.NewSimpleDynamicClient(helpers.GetScheme()))
   449  			cmd := NewCmdUninstall(rc)
   450  
   451  			if tt.fields.doesUninstall {
   452  				if strings.Contains(tt.name, "skip-confirmation") {
   453  					// Suppressing uninstall prompt
   454  					cmd.PersistentFlags().Set(ConfirmUninstallFlag, "true")
   455  				}
   456  				err := cmd.Execute()
   457  				assert.NoError(t, err)
   458  				ensureResourcesDeleted(t, c)
   459  			} else if !tt.fields.doesUninstall {
   460  				err := cmd.Execute()
   461  				assert.NoError(t, err)
   462  				ensureResourcesNotDeleted(t, c)
   463  			}
   464  		})
   465  	}
   466  }
   467  
   468  func createNamespace() *corev1.Namespace {
   469  	return &corev1.Namespace{
   470  		TypeMeta: metav1.TypeMeta{},
   471  		ObjectMeta: metav1.ObjectMeta{
   472  			Name: vzconstants.VerrazzanoInstallNamespace,
   473  		},
   474  	}
   475  }
   476  
   477  func createVz() *v1beta1.Verrazzano {
   478  	return &v1beta1.Verrazzano{
   479  		TypeMeta: metav1.TypeMeta{},
   480  		ObjectMeta: metav1.ObjectMeta{
   481  			Namespace: "default",
   482  			Name:      "verrazzano",
   483  		},
   484  	}
   485  }
   486  
   487  func createVpoDeployment(labels map[string]string) *appsv1.Deployment {
   488  	return &appsv1.Deployment{
   489  		ObjectMeta: metav1.ObjectMeta{
   490  			Namespace: vzconstants.VerrazzanoInstallNamespace,
   491  			Name:      constants.VerrazzanoPlatformOperator,
   492  			Labels:    labels,
   493  		},
   494  	}
   495  }
   496  
   497  func createVpoPod() *corev1.Pod {
   498  	return &corev1.Pod{
   499  		ObjectMeta: metav1.ObjectMeta{
   500  			Namespace: vzconstants.VerrazzanoInstallNamespace,
   501  			Name:      constants.VerrazzanoPlatformOperator,
   502  			Labels: map[string]string{
   503  				"app": constants.VerrazzanoPlatformOperator,
   504  			},
   505  		},
   506  	}
   507  }
   508  
   509  func createWebhook() *adminv1.ValidatingWebhookConfiguration {
   510  	return &adminv1.ValidatingWebhookConfiguration{
   511  		TypeMeta: metav1.TypeMeta{},
   512  		ObjectMeta: metav1.ObjectMeta{
   513  			Name: constants.VerrazzanoPlatformOperatorWebhook,
   514  		},
   515  	}
   516  }
   517  
   518  func createClusterRoleBinding() *rbacv1.ClusterRoleBinding {
   519  	return &rbacv1.ClusterRoleBinding{
   520  		TypeMeta: metav1.TypeMeta{},
   521  		ObjectMeta: metav1.ObjectMeta{
   522  			Name: constants.VerrazzanoPlatformOperator,
   523  		},
   524  	}
   525  }
   526  
   527  func createMCClusterRole() *rbacv1.ClusterRole {
   528  	return &rbacv1.ClusterRole{
   529  		TypeMeta: metav1.TypeMeta{},
   530  		ObjectMeta: metav1.ObjectMeta{
   531  			Name: constants.VerrazzanoManagedCluster,
   532  		},
   533  	}
   534  }
   535  
   536  func createRegistrarClusterRoleForRancher() *rbacv1.ClusterRole {
   537  	return &rbacv1.ClusterRole{
   538  		TypeMeta: metav1.TypeMeta{},
   539  		ObjectMeta: metav1.ObjectMeta{
   540  			Name: vzconstants.VerrazzanoClusterRancherName,
   541  		},
   542  	}
   543  }
   544  
   545  func ensureResourcesDeleted(t *testing.T, client ctrlclient.Client) {
   546  	// Expect the Verrazzano resource to be deleted
   547  	v := vzapi.Verrazzano{}
   548  	err := client.Get(context.TODO(), types.NamespacedName{Namespace: "default", Name: "verrazzano"}, &v)
   549  	assert.True(t, errors.IsNotFound(err))
   550  
   551  	// Expect the install namespace to be deleted
   552  	ns := corev1.Namespace{}
   553  	err = client.Get(context.TODO(), types.NamespacedName{Name: vzconstants.VerrazzanoInstallNamespace}, &ns)
   554  	assert.True(t, errors.IsNotFound(err))
   555  
   556  	// Expect the Validating Webhook Configuration to be deleted
   557  	vwc := adminv1.ValidatingWebhookConfiguration{}
   558  	err = client.Get(context.TODO(), types.NamespacedName{Name: constants.VerrazzanoPlatformOperatorWebhook}, &vwc)
   559  	assert.True(t, errors.IsNotFound(err))
   560  
   561  	// Expect the Cluster Role Binding to be deleted
   562  	crb := rbacv1.ClusterRoleBinding{}
   563  	err = client.Get(context.TODO(), types.NamespacedName{Name: constants.VerrazzanoPlatformOperator}, &crb)
   564  	assert.True(t, errors.IsNotFound(err))
   565  
   566  	// Expect the managed cluster Cluster Role to be deleted
   567  	cr := rbacv1.ClusterRole{}
   568  	err = client.Get(context.TODO(), types.NamespacedName{Name: constants.VerrazzanoManagedCluster}, &cr)
   569  	assert.True(t, errors.IsNotFound(err))
   570  
   571  	// Expect the cluster Registrar Cluster Role to be deleted
   572  	cr = rbacv1.ClusterRole{}
   573  	err = client.Get(context.TODO(), types.NamespacedName{Name: vzconstants.VerrazzanoClusterRancherName}, &cr)
   574  	assert.True(t, errors.IsNotFound(err))
   575  }
   576  
   577  func ensureResourcesNotDeleted(t *testing.T, client ctrlclient.Client) {
   578  	// Expect the install namespace not to be deleted
   579  	ns := corev1.Namespace{}
   580  	err := client.Get(context.TODO(), types.NamespacedName{Name: vzconstants.VerrazzanoInstallNamespace}, &ns)
   581  	assert.NoError(t, err)
   582  
   583  	// Expect the Validating Webhook Configuration not to be deleted
   584  	vwc := adminv1.ValidatingWebhookConfiguration{}
   585  	err = client.Get(context.TODO(), types.NamespacedName{Name: constants.VerrazzanoPlatformOperatorWebhook}, &vwc)
   586  	assert.NoError(t, err)
   587  
   588  	// Expect the Cluster Role Binding not to be deleted
   589  	crb := rbacv1.ClusterRoleBinding{}
   590  	err = client.Get(context.TODO(), types.NamespacedName{Name: constants.VerrazzanoPlatformOperator}, &crb)
   591  	assert.NoError(t, err)
   592  
   593  	// Expect the managed cluster Cluster Role not to be deleted
   594  	cr := rbacv1.ClusterRole{}
   595  	err = client.Get(context.TODO(), types.NamespacedName{Name: constants.VerrazzanoManagedCluster}, &cr)
   596  	assert.NoError(t, err)
   597  
   598  	// Expect the cluster Registrar Cluster Role not to be deleted
   599  	cr = rbacv1.ClusterRole{}
   600  	err = client.Get(context.TODO(), types.NamespacedName{Name: vzconstants.VerrazzanoClusterRancherName}, &cr)
   601  	assert.NoError(t, err)
   602  }