func TestSetCurrentContext(t *testing.T) { expectedConfig := newRedFederalCowHammerConfig() startingConfig := newRedFederalCowHammerConfig() newContextName := "the-new-context" startingConfig.Contexts[newContextName] = clientcmdapi.NewContext() expectedConfig.Contexts[newContextName] = clientcmdapi.NewContext() expectedConfig.CurrentContext = newContextName test := configCommandTest{ args: []string{"use-context", "the-new-context"}, startingConfig: startingConfig, expectedConfig: expectedConfig, } test.run(t) }
func TestNewEmptyContext(t *testing.T) { expectedConfig := *clientcmdapi.NewConfig() expectedConfig.Contexts["new-context"] = clientcmdapi.NewContext() test := configCommandTest{ args: []string{"set-context", "new-context"}, startingConfig: *clientcmdapi.NewConfig(), expectedConfig: expectedConfig, } test.run(t) }
func TestSetIntoNewConfig(t *testing.T) { expectedConfig := *clientcmdapi.NewConfig() context := clientcmdapi.NewContext() context.AuthInfo = "fake-user" expectedConfig.Contexts["new-context"] = context test := configCommandTest{ args: []string{"set", "contexts.new-context.user", "fake-user"}, startingConfig: *clientcmdapi.NewConfig(), expectedConfig: expectedConfig, } test.run(t) }
func TestAdditionalContext(t *testing.T) { expectedConfig := newRedFederalCowHammerConfig() context := clientcmdapi.NewContext() context.Cluster = "some-cluster" context.AuthInfo = "some-user" context.Namespace = "different-namespace" expectedConfig.Contexts["different-context"] = context test := configCommandTest{ args: []string{"set-context", "different-context", "--" + clientcmd.FlagClusterName + "=some-cluster", "--" + clientcmd.FlagAuthInfoName + "=some-user", "--" + clientcmd.FlagNamespace + "=different-namespace"}, startingConfig: newRedFederalCowHammerConfig(), expectedConfig: expectedConfig, } test.run(t) }
func TestKubectlValidation(t *testing.T) { testCases := []struct { data string err bool }{ {`{"apiVersion": "v1", "kind": "thisObjectShouldNotExistInAnyGroup"}`, true}, {`{"apiVersion": "invalidVersion", "kind": "Pod"}`, true}, {`{"apiVersion": "v1", "kind": "Pod"}`, false}, // The following test the experimental api. // TOOD: Replace with something more robust. These may move. {`{"apiVersion": "extensions/v1beta1", "kind": "Ingress"}`, false}, {`{"apiVersion": "extensions/v1beta1", "kind": "Job"}`, false}, {`{"apiVersion": "vNotAVersion", "kind": "Job"}`, true}, } components := framework.NewMasterComponents(&framework.Config{}) defer components.Stop(true, true) ctx := clientcmdapi.NewContext() cfg := clientcmdapi.NewConfig() cluster := clientcmdapi.NewCluster() cluster.Server = components.ApiServer.URL cluster.InsecureSkipTLSVerify = true overrides := clientcmd.ConfigOverrides{ ClusterInfo: *cluster, Context: *ctx, CurrentContext: "test", } cmdConfig := clientcmd.NewNonInteractiveClientConfig(*cfg, "test", &overrides) factory := util.NewFactory(cmdConfig) schema, err := factory.Validator(true, "") if err != nil { t.Errorf("failed to get validator: %v", err) return } for i, test := range testCases { err := schema.ValidateBytes([]byte(test.data)) if err == nil { if test.err { t.Errorf("case %d: expected error", i) } } else { if !test.err { t.Errorf("case %d: unexpected error: %v", i, err) } } } }
func (o createContextOptions) run() error { err := o.validate() if err != nil { return err } config, err := o.configAccess.GetStartingConfig() if err != nil { return err } startingStanza, exists := config.Contexts[o.name] if !exists { startingStanza = clientcmdapi.NewContext() } context := o.modifyContext(*startingStanza) config.Contexts[o.name] = &context if err := ModifyConfig(o.configAccess, *config, true); err != nil { return err } return nil }
func init() { err := api.Scheme.AddConversionFuncs( func(in *Cluster, out *api.Cluster, s conversion.Scope) error { return s.DefaultConvert(in, out, conversion.IgnoreMissingFields) }, func(in *api.Cluster, out *Cluster, s conversion.Scope) error { return s.DefaultConvert(in, out, conversion.IgnoreMissingFields) }, func(in *Preferences, out *api.Preferences, s conversion.Scope) error { return s.DefaultConvert(in, out, conversion.IgnoreMissingFields) }, func(in *api.Preferences, out *Preferences, s conversion.Scope) error { return s.DefaultConvert(in, out, conversion.IgnoreMissingFields) }, func(in *AuthInfo, out *api.AuthInfo, s conversion.Scope) error { return s.DefaultConvert(in, out, conversion.IgnoreMissingFields) }, func(in *api.AuthInfo, out *AuthInfo, s conversion.Scope) error { return s.DefaultConvert(in, out, conversion.IgnoreMissingFields) }, func(in *Context, out *api.Context, s conversion.Scope) error { return s.DefaultConvert(in, out, conversion.IgnoreMissingFields) }, func(in *api.Context, out *Context, s conversion.Scope) error { return s.DefaultConvert(in, out, conversion.IgnoreMissingFields) }, func(in *Config, out *api.Config, s conversion.Scope) error { out.CurrentContext = in.CurrentContext if err := s.Convert(&in.Preferences, &out.Preferences, 0); err != nil { return err } out.Clusters = make(map[string]*api.Cluster) if err := s.Convert(&in.Clusters, &out.Clusters, 0); err != nil { return err } out.AuthInfos = make(map[string]*api.AuthInfo) if err := s.Convert(&in.AuthInfos, &out.AuthInfos, 0); err != nil { return err } out.Contexts = make(map[string]*api.Context) if err := s.Convert(&in.Contexts, &out.Contexts, 0); err != nil { return err } out.Extensions = make(map[string]*runtime.EmbeddedObject) if err := s.Convert(&in.Extensions, &out.Extensions, 0); err != nil { return err } return nil }, func(in *api.Config, out *Config, s conversion.Scope) error { out.CurrentContext = in.CurrentContext if err := s.Convert(&in.Preferences, &out.Preferences, 0); err != nil { return err } out.Clusters = make([]NamedCluster, 0, 0) if err := s.Convert(&in.Clusters, &out.Clusters, 0); err != nil { return err } out.AuthInfos = make([]NamedAuthInfo, 0, 0) if err := s.Convert(&in.AuthInfos, &out.AuthInfos, 0); err != nil { return err } out.Contexts = make([]NamedContext, 0, 0) if err := s.Convert(&in.Contexts, &out.Contexts, 0); err != nil { return err } out.Extensions = make([]NamedExtension, 0, 0) if err := s.Convert(&in.Extensions, &out.Extensions, 0); err != nil { return err } return nil }, func(in *[]NamedCluster, out *map[string]*api.Cluster, s conversion.Scope) error { for _, curr := range *in { newCluster := api.NewCluster() if err := s.Convert(&curr.Cluster, newCluster, 0); err != nil { return err } (*out)[curr.Name] = newCluster } return nil }, func(in *map[string]*api.Cluster, out *[]NamedCluster, s conversion.Scope) error { allKeys := make([]string, 0, len(*in)) for key := range *in { allKeys = append(allKeys, key) } sort.Strings(allKeys) for _, key := range allKeys { newCluster := (*in)[key] oldCluster := &Cluster{} if err := s.Convert(newCluster, oldCluster, 0); err != nil { return err } namedCluster := NamedCluster{key, *oldCluster} *out = append(*out, namedCluster) } return nil }, func(in *[]NamedAuthInfo, out *map[string]*api.AuthInfo, s conversion.Scope) error { for _, curr := range *in { newAuthInfo := api.NewAuthInfo() if err := s.Convert(&curr.AuthInfo, newAuthInfo, 0); err != nil { return err } (*out)[curr.Name] = newAuthInfo } return nil }, func(in *map[string]*api.AuthInfo, out *[]NamedAuthInfo, s conversion.Scope) error { allKeys := make([]string, 0, len(*in)) for key := range *in { allKeys = append(allKeys, key) } sort.Strings(allKeys) for _, key := range allKeys { newAuthInfo := (*in)[key] oldAuthInfo := &AuthInfo{} if err := s.Convert(newAuthInfo, oldAuthInfo, 0); err != nil { return err } namedAuthInfo := NamedAuthInfo{key, *oldAuthInfo} *out = append(*out, namedAuthInfo) } return nil }, func(in *[]NamedContext, out *map[string]*api.Context, s conversion.Scope) error { for _, curr := range *in { newContext := api.NewContext() if err := s.Convert(&curr.Context, newContext, 0); err != nil { return err } (*out)[curr.Name] = newContext } return nil }, func(in *map[string]*api.Context, out *[]NamedContext, s conversion.Scope) error { allKeys := make([]string, 0, len(*in)) for key := range *in { allKeys = append(allKeys, key) } sort.Strings(allKeys) for _, key := range allKeys { newContext := (*in)[key] oldContext := &Context{} if err := s.Convert(newContext, oldContext, 0); err != nil { return err } namedContext := NamedContext{key, *oldContext} *out = append(*out, namedContext) } return nil }, func(in *[]NamedExtension, out *map[string]*runtime.EmbeddedObject, s conversion.Scope) error { for _, curr := range *in { newExtension := &runtime.EmbeddedObject{} if err := s.Convert(&curr.Extension, newExtension, 0); err != nil { return err } (*out)[curr.Name] = newExtension } return nil }, func(in *map[string]*runtime.EmbeddedObject, out *[]NamedExtension, s conversion.Scope) error { allKeys := make([]string, 0, len(*in)) for key := range *in { allKeys = append(allKeys, key) } sort.Strings(allKeys) for _, key := range allKeys { newExtension := (*in)[key] oldExtension := &runtime.RawExtension{} if err := s.Convert(newExtension, oldExtension, 0); err != nil { return err } namedExtension := NamedExtension{key, *oldExtension} *out = append(*out, namedExtension) } return nil }, ) if err != nil { // If one of the conversion functions is malformed, detect it immediately. panic(err) } }