Unverified Commit 7b6c9a54 authored by bryanl's avatar bryanl
Browse files

introduce method to test cmd flags


Signed-off-by: default avatarbryanl <bryanliles@gmail.com>
parent 9c4279da
...@@ -33,7 +33,7 @@ func TestInit(t *testing.T) { ...@@ -33,7 +33,7 @@ func TestInit(t *testing.T) {
aRootPath := appMock.Root() aRootPath := appMock.Root()
aK8sSpecFlag := "specFlag" aK8sSpecFlag := "specFlag"
aServerURI := "http://example.com" aServerURI := "http://example.com"
aNamespace := "default" aNamespace := "my-namespace"
a, err := NewInit(aFs, aName, aRootPath, aK8sSpecFlag, aServerURI, aNamespace) a, err := NewInit(aFs, aName, aRootPath, aK8sSpecFlag, aServerURI, aNamespace)
require.NoError(t, err) require.NoError(t, err)
......
...@@ -287,19 +287,27 @@ func (c *Config) overrideCluster(envName string) error { ...@@ -287,19 +287,27 @@ func (c *Config) overrideCluster(envName string) error {
return err return err
} }
if _, ok := servers[server]; ok { if len(servers) > 0 {
clusterName := servers[server] if _, ok := servers[server]; ok {
if c.Overrides.Context.Cluster == "" { clusterName := servers[server]
log.Debugf("Overwriting --cluster flag with '%s'", clusterName) if c.Overrides.Context.Cluster == "" {
c.Overrides.Context.Cluster = clusterName 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()) return fmt.Errorf("Attempting to deploy to environment '%s' at '%s', but cannot locate a server at that address",
c.Overrides.Context.Namespace = destination.Namespace() envName, destination.Server())
}
return nil
} }
return errors.Errorf("Attempting to deploy to environment '%s' at '%s', but cannot locate a server at that address", c.Overrides.Context.Namespace = destination.Namespace()
envName, destination.Server()) 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
} }
// 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,
}
)
...@@ -162,21 +162,25 @@ func commonEnvFlags(flags *pflag.FlagSet) (server, namespace, context string, er ...@@ -162,21 +162,25 @@ func commonEnvFlags(flags *pflag.FlagSet) (server, namespace, context string, er
func resolveEnvFlags(flags *pflag.FlagSet) (string, string, error) { func resolveEnvFlags(flags *pflag.FlagSet) (string, string, error) {
defaultNamespace := "default" defaultNamespace := "default"
server, ns, context, err := commonEnvFlags(flags) server, envNs, context, err := commonEnvFlags(flags)
if err != nil { if err != nil {
return "", "", err return "", "", err
} }
var ctxNs string
if server == "" { if server == "" {
// server is not provided -- use the context. // server is not provided -- use the context.
server, ns, err = envClientConfig.ResolveContext(context) server, ctxNs, err = envClientConfig.ResolveContext(context)
if err != nil { if err != nil {
return "", "", err return "", "", err
} }
} }
if ns == "" { ns := defaultNamespace
ns = defaultNamespace if envNs != "" {
ns = envNs
} else if ctxNs != "" {
ns = ctxNs
} }
return server, ns, nil return server, ns, nil
......
...@@ -21,8 +21,8 @@ import ( ...@@ -21,8 +21,8 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"github.com/ksonnet/ksonnet/actions"
"github.com/ksonnet/ksonnet/client" "github.com/ksonnet/ksonnet/client"
"github.com/spf13/afero"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
...@@ -84,7 +84,17 @@ var initCmd = &cobra.Command{ ...@@ -84,7 +84,17 @@ var initCmd = &cobra.Command{
specFlag = initClientConfig.GetAPISpec(server) 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, appFs,
appName, appName,
appRoot, appRoot,
...@@ -92,13 +102,6 @@ var initCmd = &cobra.Command{ ...@@ -92,13 +102,6 @@ var initCmd = &cobra.Command{
server, server,
namespace, namespace,
) )
// c, err := kubecfg.NewInitCmd(appName, appRoot, &specFlag, &server, &namespace)
// if err != nil {
// return err
// }
// return c.Run()
}, },
Long: ` Long: `
The ` + "`init`" + ` command initializes a ksonnet application in a new directory,` + " `app-name`" + `. The ` + "`init`" + ` command initializes a ksonnet application in a new directory,` + " `app-name`" + `.
......
...@@ -15,7 +15,52 @@ ...@@ -15,7 +15,52 @@
package cmd 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) { func Test_genKsRoot(t *testing.T) {
cases := []struct { cases := []struct {
......
...@@ -31,7 +31,7 @@ func TestInit(t *testing.T) { ...@@ -31,7 +31,7 @@ func TestInit(t *testing.T) {
rootPath := "/app" rootPath := "/app"
specFlag := "version:v1.8.7" specFlag := "version:v1.8.7"
serverURI := "http://example.com" serverURI := "http://example.com"
namespace := "default" namespace := "my-namespace"
r := &mocks.Registry{} r := &mocks.Registry{}
...@@ -86,21 +86,21 @@ func TestInit(t *testing.T) { ...@@ -86,21 +86,21 @@ func TestInit(t *testing.T) {
} }
require.NoError(t, err) 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{ expectedDirs := []string{
"app.yaml", "app.yaml",
filepath.Join(".ksonnet", "registries", "testdata", "registry.yaml"), filepath.Join(".ksonnet", "registries", "testdata", "registry.yaml"),
filepath.Join("components", "params.libsonnet"), filepath.Join("components", "params.libsonnet"),
"vendor", "vendor",
filepath.Join("environments", "base.libsonnet"), filepath.Join("environments", "base.libsonnet"),
filepath.Join("environments", "default", "main.jsonnet"), filepath.Join("environments", namespace, "main.jsonnet"),
filepath.Join("environments", "default", "params.libsonnet"), filepath.Join("environments", namespace, "params.libsonnet"),
} }
for _, d := range expectedDirs { for _, d := range expectedDirs {
......
...@@ -33,7 +33,7 @@ type ValidateCmd struct { ...@@ -33,7 +33,7 @@ type ValidateCmd struct {
} }
func (c ValidateCmd) Run(apiObjects []*unstructured.Unstructured, out io.Writer) error { 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 { if err != nil {
return err return err
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment