Skip to content
Snippets Groups Projects
Unverified Commit a89f456d authored by Bryan Liles's avatar Bryan Liles Committed by GitHub
Browse files

Merge pull request #395 from bryanl/bug-resolve-ns-from-cli-flag

introduce method to test cmd flags
parents 9c4279da 7b6c9a54
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
......@@ -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
}
// 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
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
......
......@@ -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`" + `.
......
......@@ -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 {
......
......@@ -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 {
......
......@@ -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
}
......
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