github.com/verrazzano/verrazzano@v1.7.1/tools/vz/cmd/root/root_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 root
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/verrazzano/verrazzano/tools/vz/cmd/analyze"
    14  	"github.com/verrazzano/verrazzano/tools/vz/cmd/bugreport"
    15  	"github.com/verrazzano/verrazzano/tools/vz/cmd/export"
    16  	"github.com/verrazzano/verrazzano/tools/vz/cmd/install"
    17  	"github.com/verrazzano/verrazzano/tools/vz/cmd/status"
    18  	"github.com/verrazzano/verrazzano/tools/vz/cmd/uninstall"
    19  	"github.com/verrazzano/verrazzano/tools/vz/cmd/upgrade"
    20  	"github.com/verrazzano/verrazzano/tools/vz/cmd/version"
    21  	"github.com/verrazzano/verrazzano/tools/vz/pkg/constants"
    22  	"github.com/verrazzano/verrazzano/tools/vz/test/helpers"
    23  )
    24  
    25  func TestNewRootCmd(t *testing.T) {
    26  
    27  	rc := helpers.NewFakeRootCmdContextWithFiles(t)
    28  	defer helpers.CleanUpNewFakeRootCmdContextWithFiles(rc)
    29  	rootCmd := NewRootCmd(rc)
    30  	assert.NotNil(t, rootCmd)
    31  
    32  	// Verify the expected commands are defined
    33  	assert.Len(t, rootCmd.Commands(), 9)
    34  	foundCount := 0
    35  	for _, cmd := range rootCmd.Commands() {
    36  		switch cmd.Name() {
    37  		case status.CommandName:
    38  			foundCount++
    39  		case version.CommandName:
    40  			foundCount++
    41  		case install.CommandName:
    42  			foundCount++
    43  		case upgrade.CommandName:
    44  			foundCount++
    45  		case uninstall.CommandName:
    46  			foundCount++
    47  		case analyze.CommandName:
    48  			foundCount++
    49  		case bugreport.CommandName:
    50  			foundCount++
    51  		case export.CommandName:
    52  			foundCount++
    53  		}
    54  	}
    55  	assert.Equal(t, 8, foundCount)
    56  
    57  	// Verify the expected global flags are defined
    58  	assert.NotNil(t, rootCmd.PersistentFlags().Lookup(constants.GlobalFlagKubeConfig))
    59  	assert.NotNil(t, rootCmd.PersistentFlags().Lookup(constants.GlobalFlagContext))
    60  
    61  	// Verify help has the expected elements
    62  	rootCmd.SetArgs([]string{fmt.Sprintf("--%s", constants.GlobalFlagHelp)})
    63  	err := rootCmd.Execute()
    64  	assert.NoError(t, err)
    65  	fileBuffer, err := os.ReadFile(rc.Out.Name())
    66  	assert.Nil(t, err)
    67  	assert.True(t, strings.Contains(string(fileBuffer), "Usage:"))
    68  	assert.True(t, strings.Contains(string(fileBuffer), "Available Commands:"))
    69  	assert.True(t, strings.Contains(string(fileBuffer), "Flags:"))
    70  }