From 7b6c9a5440ba1ebd937fd062b86e3a20a92585de Mon Sep 17 00:00:00 2001 From: bryanl <bryanliles@gmail.com> Date: Wed, 28 Mar 2018 10:28:51 -0400 Subject: [PATCH] introduce method to test cmd flags Signed-off-by: bryanl <bryanliles@gmail.com> --- actions/init_test.go | 2 +- client/client.go | 32 +++++++++++++++++---------- cmd/actions.go | 30 +++++++++++++++++++++++++ cmd/env.go | 12 ++++++---- cmd/init.go | 21 ++++++++++-------- cmd/init_test.go | 47 +++++++++++++++++++++++++++++++++++++++- pkg/appinit/init_test.go | 10 ++++----- pkg/kubecfg/validate.go | 2 +- 8 files changed, 123 insertions(+), 33 deletions(-) create mode 100644 cmd/actions.go diff --git a/actions/init_test.go b/actions/init_test.go index e96c913c..53a8067d 100644 --- a/actions/init_test.go +++ b/actions/init_test.go @@ -33,7 +33,7 @@ func TestInit(t *testing.T) { aRootPath := appMock.Root() aK8sSpecFlag := "specFlag" aServerURI := "http://example.com" - aNamespace := "default" + aNamespace := "my-namespace" a, err := NewInit(aFs, aName, aRootPath, aK8sSpecFlag, aServerURI, aNamespace) require.NoError(t, err) diff --git a/client/client.go b/client/client.go index b052ce3e..bb6d33a4 100644 --- a/client/client.go +++ b/client/client.go @@ -287,19 +287,27 @@ func (c *Config) overrideCluster(envName string) error { return err } - if _, ok := servers[server]; ok { - clusterName := servers[server] - if c.Overrides.Context.Cluster == "" { - log.Debugf("Overwriting --cluster flag with '%s'", clusterName) - c.Overrides.Context.Cluster = clusterName + if len(servers) > 0 { + if _, ok := servers[server]; ok { + clusterName := servers[server] + if c.Overrides.Context.Cluster == "" { + log.Debugf("Overwriting --cluster flag with '%s'", clusterName) + c.Overrides.Context.Cluster = clusterName + } + if c.Overrides.Context.Namespace == "" { + log.Debugf("Overwriting --namespace flag with '%s'", destination.Namespace()) + c.Overrides.Context.Namespace = destination.Namespace() + } + return nil } - if c.Overrides.Context.Namespace == "" { - log.Debugf("Overwriting --namespace flag with '%s'", destination.Namespace()) - c.Overrides.Context.Namespace = destination.Namespace() - } - return nil + + return fmt.Errorf("Attempting to deploy to environment '%s' at '%s', but cannot locate a server at that address", + envName, destination.Server()) } - return errors.Errorf("Attempting to deploy to environment '%s' at '%s', but cannot locate a server at that address", - envName, destination.Server()) + c.Overrides.Context.Namespace = destination.Namespace() + c.Overrides.ClusterInfo.Server = server + // NOTE: ignore TLS verify since we don't have a CA cert to verify with. + c.Overrides.ClusterInfo.InsecureSkipTLSVerify = true + return nil } diff --git a/cmd/actions.go b/cmd/actions.go new file mode 100644 index 00000000..d5649107 --- /dev/null +++ b/cmd/actions.go @@ -0,0 +1,30 @@ +// Copyright 2018 The ksonnet authors +// +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import "github.com/ksonnet/ksonnet/actions" + +type initName int + +const ( + actionInit initName = iota +) + +var ( + actionMap = map[initName]interface{}{ + actionInit: actions.RunInit, + } +) diff --git a/cmd/env.go b/cmd/env.go index 975c26d3..1b6f3a17 100644 --- a/cmd/env.go +++ b/cmd/env.go @@ -162,21 +162,25 @@ func commonEnvFlags(flags *pflag.FlagSet) (server, namespace, context string, er func resolveEnvFlags(flags *pflag.FlagSet) (string, string, error) { defaultNamespace := "default" - server, ns, context, err := commonEnvFlags(flags) + server, envNs, context, err := commonEnvFlags(flags) if err != nil { return "", "", err } + var ctxNs string if server == "" { // server is not provided -- use the context. - server, ns, err = envClientConfig.ResolveContext(context) + server, ctxNs, err = envClientConfig.ResolveContext(context) if err != nil { return "", "", err } } - if ns == "" { - ns = defaultNamespace + ns := defaultNamespace + if envNs != "" { + ns = envNs + } else if ctxNs != "" { + ns = ctxNs } return server, ns, nil diff --git a/cmd/init.go b/cmd/init.go index 5a57a311..944458d3 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -21,8 +21,8 @@ import ( "os" "path/filepath" - "github.com/ksonnet/ksonnet/actions" "github.com/ksonnet/ksonnet/client" + "github.com/spf13/afero" "github.com/spf13/cobra" ) @@ -84,7 +84,17 @@ var initCmd = &cobra.Command{ specFlag = initClientConfig.GetAPISpec(server) } - return actions.RunInit( + v, ok := actionMap[actionInit] + if !ok { + return errors.New("init action does not exist") + } + + fn, ok := v.(func(afero.Fs, string, string, string, string, string) error) + if !ok { + return errors.New("init action was not in the proper format") + } + + return fn( appFs, appName, appRoot, @@ -92,13 +102,6 @@ var initCmd = &cobra.Command{ server, namespace, ) - - // c, err := kubecfg.NewInitCmd(appName, appRoot, &specFlag, &server, &namespace) - // if err != nil { - // return err - // } - - // return c.Run() }, Long: ` The ` + "`init`" + ` command initializes a ksonnet application in a new directory,` + " `app-name`" + `. diff --git a/cmd/init_test.go b/cmd/init_test.go index 9136dd31..abf986e3 100644 --- a/cmd/init_test.go +++ b/cmd/init_test.go @@ -15,7 +15,52 @@ package cmd -import "testing" +import ( + "os" + "path/filepath" + "testing" + + "github.com/spf13/afero" + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func withCmd(t *testing.T, cmd *cobra.Command, id initName, override interface{}, fn func()) { + ogAction := actionMap[id] + actionMap[id] = override + + envConfig := os.Getenv("KUBECONFIG") + + defer func() { + actionMap[id] = ogAction + os.Setenv("KUBECONFIG", envConfig) + }() + + fn() +} + +func Test_initCmd(t *testing.T) { + override := func(fs afero.Fs, name, root, specFlag, server, namespace string) error { + wd, err := os.Getwd() + require.NoError(t, err) + + appRoot := filepath.Join(wd, "app") + + assert.Equal(t, "new-namespace", namespace) + assert.Equal(t, appRoot, root) + assert.Equal(t, "app", name) + return nil + } + + withCmd(t, initCmd, actionInit, override, func() { + args := []string{"init", "app", "--namespace", "new-namespace", "--server", "http://127.0.0.1"} + RootCmd.SetArgs(args) + + err := RootCmd.Execute() + require.NoError(t, err) + }) +} func Test_genKsRoot(t *testing.T) { cases := []struct { diff --git a/pkg/appinit/init_test.go b/pkg/appinit/init_test.go index 83e3a49d..3c48fb8f 100644 --- a/pkg/appinit/init_test.go +++ b/pkg/appinit/init_test.go @@ -31,7 +31,7 @@ func TestInit(t *testing.T) { rootPath := "/app" specFlag := "version:v1.8.7" serverURI := "http://example.com" - namespace := "default" + namespace := "my-namespace" r := &mocks.Registry{} @@ -86,21 +86,21 @@ func TestInit(t *testing.T) { } require.NoError(t, err) - checkApp(t, fs, rootPath, "v1.8.7") + checkApp(t, fs, rootPath, "v1.8.7", namespace) }) } } -func checkApp(t *testing.T, fs afero.Fs, rootPath, version string) { +func checkApp(t *testing.T, fs afero.Fs, rootPath, version, namespace string) { expectedDirs := []string{ "app.yaml", filepath.Join(".ksonnet", "registries", "testdata", "registry.yaml"), filepath.Join("components", "params.libsonnet"), "vendor", filepath.Join("environments", "base.libsonnet"), - filepath.Join("environments", "default", "main.jsonnet"), - filepath.Join("environments", "default", "params.libsonnet"), + filepath.Join("environments", namespace, "main.jsonnet"), + filepath.Join("environments", namespace, "params.libsonnet"), } for _, d := range expectedDirs { diff --git a/pkg/kubecfg/validate.go b/pkg/kubecfg/validate.go index 275f780f..b376058a 100644 --- a/pkg/kubecfg/validate.go +++ b/pkg/kubecfg/validate.go @@ -33,7 +33,7 @@ type ValidateCmd struct { } func (c ValidateCmd) Run(apiObjects []*unstructured.Unstructured, out io.Writer) error { - _, discovery, _, err := client.InitClient(c.Env) + _, discovery, _, err := c.ClientConfig.RestClient(&c.Env) if err != nil { return err } -- GitLab