mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-03 19:58:17 +00:00 
			
		
		
		
	make use-context return error when context does not exist
This commit is contained in:
		@@ -44,13 +44,6 @@ func newRedFederalCowHammerConfig() clientcmdapi.Config {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type configCommandTest struct {
 | 
			
		||||
	args            []string
 | 
			
		||||
	startingConfig  clientcmdapi.Config
 | 
			
		||||
	expectedConfig  clientcmdapi.Config
 | 
			
		||||
	expectedOutputs []string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ExampleView() {
 | 
			
		||||
	expectedConfig := newRedFederalCowHammerConfig()
 | 
			
		||||
	test := configCommandTest{
 | 
			
		||||
@@ -83,16 +76,36 @@ func ExampleView() {
 | 
			
		||||
 | 
			
		||||
func TestSetCurrentContext(t *testing.T) {
 | 
			
		||||
	expectedConfig := newRedFederalCowHammerConfig()
 | 
			
		||||
	expectedConfig.CurrentContext = "the-new-context"
 | 
			
		||||
	startingConfig := newRedFederalCowHammerConfig()
 | 
			
		||||
 | 
			
		||||
	newContextName := "the-new-context"
 | 
			
		||||
	newContext := clientcmdapi.NewContext()
 | 
			
		||||
 | 
			
		||||
	startingConfig.Contexts[newContextName] = *newContext
 | 
			
		||||
	expectedConfig.Contexts[newContextName] = *newContext
 | 
			
		||||
 | 
			
		||||
	expectedConfig.CurrentContext = newContextName
 | 
			
		||||
 | 
			
		||||
	test := configCommandTest{
 | 
			
		||||
		args:           []string{"use-context", "the-new-context"},
 | 
			
		||||
		startingConfig: newRedFederalCowHammerConfig(),
 | 
			
		||||
		startingConfig: startingConfig,
 | 
			
		||||
		expectedConfig: expectedConfig,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	test.run(t)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestSetNonExistantContext(t *testing.T) {
 | 
			
		||||
	expectedConfig := newRedFederalCowHammerConfig()
 | 
			
		||||
	test := configCommandTest{
 | 
			
		||||
		args:            []string{"use-context", "non-existant-config"},
 | 
			
		||||
		startingConfig:  expectedConfig,
 | 
			
		||||
		expectedConfig:  expectedConfig,
 | 
			
		||||
		expectedOutputs: []string{`No context exists with the name: "non-existant-config"`},
 | 
			
		||||
	}
 | 
			
		||||
	test.run(t)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestSetIntoExistingStruct(t *testing.T) {
 | 
			
		||||
	expectedConfig := newRedFederalCowHammerConfig()
 | 
			
		||||
	a := expectedConfig.AuthInfos["red-user"]
 | 
			
		||||
@@ -691,6 +704,13 @@ func testConfigCommand(args []string, startingConfig clientcmdapi.Config) (strin
 | 
			
		||||
	return buf.String(), *config
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type configCommandTest struct {
 | 
			
		||||
	args            []string
 | 
			
		||||
	startingConfig  clientcmdapi.Config
 | 
			
		||||
	expectedConfig  clientcmdapi.Config
 | 
			
		||||
	expectedOutputs []string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (test configCommandTest) run(t *testing.T) string {
 | 
			
		||||
	out, actualConfig := testConfigCommand(test.args, test.startingConfig)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -22,6 +22,8 @@ import (
 | 
			
		||||
	"io"
 | 
			
		||||
 | 
			
		||||
	"github.com/spf13/cobra"
 | 
			
		||||
 | 
			
		||||
	clientcmdapi "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd/api"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type useContextOptions struct {
 | 
			
		||||
@@ -43,7 +45,7 @@ func NewCmdConfigUseContext(out io.Writer, configAccess ConfigAccess) *cobra.Com
 | 
			
		||||
 | 
			
		||||
			err := options.run()
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				fmt.Printf("%v\n", err)
 | 
			
		||||
				fmt.Fprintf(out, "%v\n", err)
 | 
			
		||||
			}
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
@@ -52,12 +54,12 @@ func NewCmdConfigUseContext(out io.Writer, configAccess ConfigAccess) *cobra.Com
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (o useContextOptions) run() error {
 | 
			
		||||
	err := o.validate()
 | 
			
		||||
	config, err := o.configAccess.GetStartingConfig()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	config, err := o.configAccess.GetStartingConfig()
 | 
			
		||||
	err = o.validate(config)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
@@ -82,10 +84,16 @@ func (o *useContextOptions) complete(cmd *cobra.Command) bool {
 | 
			
		||||
	return true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (o useContextOptions) validate() error {
 | 
			
		||||
func (o useContextOptions) validate(config *clientcmdapi.Config) error {
 | 
			
		||||
	if len(o.contextName) == 0 {
 | 
			
		||||
		return errors.New("You must specify a current-context")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
	for name := range config.Contexts {
 | 
			
		||||
		if name == o.contextName {
 | 
			
		||||
			return nil
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return fmt.Errorf("No context exists with the name: %q.", o.contextName)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user