istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/cmd/istioctl_test.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cmd
    16  
    17  import (
    18  	"testing"
    19  
    20  	istioctlutil "istio.io/istio/istioctl/pkg/util"
    21  )
    22  
    23  func TestBadParse(t *testing.T) {
    24  	// unknown flags should be a command parse
    25  	rootCmd := GetRootCmd([]string{"--unknown-flag"})
    26  	fErr := rootCmd.Execute()
    27  
    28  	switch fErr.(type) {
    29  	case istioctlutil.CommandParseError:
    30  		// do nothing
    31  	default:
    32  		t.Errorf("Expected a CommandParseError, but got %q.", fErr)
    33  	}
    34  
    35  	// we should propagate to subcommands
    36  	rootCmd = GetRootCmd([]string{"analyze", "--unknown-flag"})
    37  	fErr = rootCmd.Execute()
    38  
    39  	switch fErr.(type) {
    40  	case istioctlutil.CommandParseError:
    41  		// do nothing
    42  	default:
    43  		t.Errorf("Expected a CommandParseError, but got %q.", fErr)
    44  	}
    45  
    46  	// all of the subcommands
    47  	rootCmd = GetRootCmd([]string{"x", "authz", "check", "--unknown-flag"})
    48  	fErr = rootCmd.Execute()
    49  
    50  	switch fErr.(type) {
    51  	case istioctlutil.CommandParseError:
    52  		// do nothing
    53  	default:
    54  		t.Errorf("Expected a CommandParseError, but got %q.", fErr)
    55  	}
    56  }