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