Unverified Commit c6b1d85c authored by Bryan Liles's avatar Bryan Liles Committed by GitHub
Browse files

Merge pull request #433 from bryanl/action-option-tests

Action option tests
parents 5127e0ca a8222540
...@@ -148,7 +148,7 @@ func newOptionLoader(m map[string]interface{}) *optionLoader { ...@@ -148,7 +148,7 @@ func newOptionLoader(m map[string]interface{}) *optionLoader {
} }
} }
func (o *optionLoader) loadFs(name string) afero.Fs { func (o *optionLoader) LoadFs(name string) afero.Fs {
i := o.load(name) i := o.load(name)
if i == nil { if i == nil {
return nil return nil
...@@ -163,7 +163,7 @@ func (o *optionLoader) loadFs(name string) afero.Fs { ...@@ -163,7 +163,7 @@ func (o *optionLoader) loadFs(name string) afero.Fs {
return a return a
} }
func (o *optionLoader) loadBool(name string) bool { func (o *optionLoader) LoadBool(name string) bool {
i := o.load(name) i := o.load(name)
if i == nil { if i == nil {
return false return false
...@@ -178,7 +178,7 @@ func (o *optionLoader) loadBool(name string) bool { ...@@ -178,7 +178,7 @@ func (o *optionLoader) loadBool(name string) bool {
return a return a
} }
func (o *optionLoader) loadOptionalBool(name string) bool { func (o *optionLoader) LoadOptionalBool(name string) bool {
i := o.loadOptional(name) i := o.loadOptional(name)
if i == nil { if i == nil {
return false return false
...@@ -192,7 +192,7 @@ func (o *optionLoader) loadOptionalBool(name string) bool { ...@@ -192,7 +192,7 @@ func (o *optionLoader) loadOptionalBool(name string) bool {
return a return a
} }
func (o *optionLoader) loadInt(name string) int { func (o *optionLoader) LoadInt(name string) int {
i := o.load(name) i := o.load(name)
if i == nil { if i == nil {
return 0 return 0
...@@ -207,7 +207,7 @@ func (o *optionLoader) loadInt(name string) int { ...@@ -207,7 +207,7 @@ func (o *optionLoader) loadInt(name string) int {
return a return a
} }
func (o *optionLoader) loadInt64(name string) int64 { func (o *optionLoader) LoadInt64(name string) int64 {
i := o.load(name) i := o.load(name)
if i == nil { if i == nil {
return 0 return 0
...@@ -222,7 +222,7 @@ func (o *optionLoader) loadInt64(name string) int64 { ...@@ -222,7 +222,7 @@ func (o *optionLoader) loadInt64(name string) int64 {
return a return a
} }
func (o *optionLoader) loadOptionalInt(name string) int { func (o *optionLoader) LoadOptionalInt(name string) int {
i := o.loadOptional(name) i := o.loadOptional(name)
if i == nil { if i == nil {
return 0 return 0
...@@ -236,7 +236,7 @@ func (o *optionLoader) loadOptionalInt(name string) int { ...@@ -236,7 +236,7 @@ func (o *optionLoader) loadOptionalInt(name string) int {
return a return a
} }
func (o *optionLoader) loadString(name string) string { func (o *optionLoader) LoadString(name string) string {
i := o.load(name) i := o.load(name)
if i == nil { if i == nil {
return "" return ""
...@@ -251,7 +251,7 @@ func (o *optionLoader) loadString(name string) string { ...@@ -251,7 +251,7 @@ func (o *optionLoader) loadString(name string) string {
return a return a
} }
func (o *optionLoader) loadOptionalString(name string) string { func (o *optionLoader) LoadOptionalString(name string) string {
i := o.loadOptional(name) i := o.loadOptional(name)
if i == nil { if i == nil {
return "" return ""
...@@ -265,7 +265,7 @@ func (o *optionLoader) loadOptionalString(name string) string { ...@@ -265,7 +265,7 @@ func (o *optionLoader) loadOptionalString(name string) string {
return a return a
} }
func (o *optionLoader) loadStringSlice(name string) []string { func (o *optionLoader) LoadStringSlice(name string) []string {
i := o.load(name) i := o.load(name)
if i == nil { if i == nil {
return nil return nil
...@@ -280,7 +280,7 @@ func (o *optionLoader) loadStringSlice(name string) []string { ...@@ -280,7 +280,7 @@ func (o *optionLoader) loadStringSlice(name string) []string {
return a return a
} }
func (o *optionLoader) loadClientConfig() *client.Config { func (o *optionLoader) LoadClientConfig() *client.Config {
i := o.load(OptionClientConfig) i := o.load(OptionClientConfig)
if i == nil { if i == nil {
return nil return nil
...@@ -295,7 +295,7 @@ func (o *optionLoader) loadClientConfig() *client.Config { ...@@ -295,7 +295,7 @@ func (o *optionLoader) loadClientConfig() *client.Config {
return a return a
} }
func (o *optionLoader) loadApp() app.App { func (o *optionLoader) LoadApp() app.App {
i := o.load(OptionApp) i := o.load(OptionApp)
if i == nil { if i == nil {
o.err = ErrNotInApp o.err = ErrNotInApp
......
...@@ -16,57 +16,227 @@ ...@@ -16,57 +16,227 @@
package actions package actions
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"reflect"
"testing" "testing"
"github.com/ksonnet/ksonnet/client"
cmocks "github.com/ksonnet/ksonnet/component/mocks" cmocks "github.com/ksonnet/ksonnet/component/mocks"
"github.com/ksonnet/ksonnet/metadata/app/mocks" "github.com/ksonnet/ksonnet/metadata/app/mocks"
"github.com/ksonnet/ksonnet/pkg/registry" "github.com/ksonnet/ksonnet/pkg/registry"
rmocks "github.com/ksonnet/ksonnet/pkg/registry/mocks" rmocks "github.com/ksonnet/ksonnet/pkg/registry/mocks"
"github.com/pkg/errors"
"github.com/spf13/afero" "github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func Test_optionsLoader_loadApp(t *testing.T) { func Test_optionLoader_types(t *testing.T) {
withApp(t, func(a *mocks.App) {
cases := []struct { cases := []struct {
name string name string
m map[string]interface{} hasArg bool
isErr bool valid interface{}
invalid interface{}
keyName string
}{ }{
{ {
name: "with app", name: "App",
m: map[string]interface{}{ valid: &mocks.App{},
OptionApp: a, invalid: "invalid",
keyName: OptionApp,
}, },
{
name: "Bool",
hasArg: true,
valid: true,
invalid: "invalid",
keyName: OptionSkipGc,
},
{
name: "Fs",
hasArg: true,
valid: afero.NewMemMapFs(),
invalid: "invalid",
keyName: OptionFs,
},
{
name: "Int",
hasArg: true,
valid: 0,
invalid: "invalid",
keyName: OptionName,
},
{
name: "Int64",
hasArg: true,
valid: int64(0),
invalid: "invalid",
keyName: OptionName,
},
{
name: "String",
hasArg: true,
valid: "valid",
invalid: 0,
keyName: OptionName,
}, },
{ {
name: "with invalid app", name: "StringSlice",
m: map[string]interface{}{ hasArg: true,
OptionApp: "invalid", valid: []string{},
invalid: "invalid",
keyName: OptionName,
}, },
isErr: true, {
name: "ClientConfig",
valid: &client.Config{},
invalid: "invalid",
keyName: OptionClientConfig,
}, },
} }
for _, tc := range cases { for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) { methodName := fmt.Sprintf("Load%s", tc.name)
ol := newOptionLoader(tc.m)
t.Run(tc.name+" valid", func(t *testing.T) {
m := map[string]interface{}{
tc.keyName: tc.valid,
}
ol := newOptionLoader(m)
loader := reflect.ValueOf(ol).MethodByName(methodName)
callValues := make([]reflect.Value, 0)
if tc.hasArg {
callValues = append(callValues, reflect.ValueOf(tc.keyName))
}
values := loader.Call(callValues)
require.Len(t, values, 1)
require.EqualValues(t, tc.valid, values[0].Interface())
})
t.Run(tc.name+" invalid", func(t *testing.T) {
m := map[string]interface{}{
tc.keyName: tc.invalid,
}
ol := newOptionLoader(m)
loader := reflect.ValueOf(ol).MethodByName(methodName)
callValues := make([]reflect.Value, 0)
if tc.hasArg {
callValues = append(callValues, reflect.ValueOf(tc.keyName))
}
loader.Call(callValues)
require.Error(t, ol.err)
})
t.Run(tc.name+" previous error", func(t *testing.T) {
m := map[string]interface{}{
tc.keyName: tc.invalid,
}
ol := newOptionLoader(m)
ol.err = errors.New("error")
loader := reflect.ValueOf(ol).MethodByName(methodName)
callValues := make([]reflect.Value, 0)
if tc.hasArg {
callValues = append(callValues, reflect.ValueOf(tc.keyName))
}
got := ol.loadApp() loader.Call(callValues)
if tc.isErr {
require.Error(t, ol.err) require.Error(t, ol.err)
return })
}
}
func Test_optionLoader_optional_types(t *testing.T) {
cases := []struct {
name string
valid interface{}
invalid interface{}
expected interface{}
keyName string
}{
{
name: "Bool",
valid: true,
invalid: "invalid",
expected: false,
keyName: OptionApp,
},
{
name: "Int",
valid: 9,
invalid: "invalid",
expected: 0,
keyName: OptionApp,
},
{
name: "String",
valid: "valid",
invalid: 9,
expected: "",
keyName: OptionApp,
},
}
for _, tc := range cases {
methodName := fmt.Sprintf("LoadOptional%s", tc.name)
t.Run(tc.name+" valid", func(t *testing.T) {
m := map[string]interface{}{
tc.keyName: tc.valid,
}
ol := newOptionLoader(m)
loader := reflect.ValueOf(ol).MethodByName(methodName)
callValues := []reflect.Value{reflect.ValueOf(tc.keyName)}
values := loader.Call(callValues)
require.Len(t, values, 1)
require.EqualValues(t, tc.valid, values[0].Interface())
})
t.Run(tc.name+" invalid", func(t *testing.T) {
m := map[string]interface{}{
tc.keyName: tc.invalid,
} }
require.NoError(t, ol.err) ol := newOptionLoader(m)
assert.Equal(t, a, got)
loader := reflect.ValueOf(ol).MethodByName(methodName)
callValues := []reflect.Value{reflect.ValueOf(tc.keyName)}
values := loader.Call(callValues)
require.Len(t, values, 1)
require.EqualValues(t, tc.expected, values[0].Interface())
}) })
t.Run(tc.name+" previous error", func(t *testing.T) {
m := map[string]interface{}{
tc.keyName: tc.invalid,
} }
ol := newOptionLoader(m)
ol.err = errors.New("error")
loader := reflect.ValueOf(ol).MethodByName(methodName)
callValues := []reflect.Value{reflect.ValueOf(tc.keyName)}
loader.Call(callValues)
require.Error(t, ol.err)
}) })
}
} }
func withApp(t *testing.T, fn func(*mocks.App)) { func withApp(t *testing.T, fn func(*mocks.App)) {
......
...@@ -54,13 +54,13 @@ func newApply(m map[string]interface{}, opts ...applyOpt) (*Apply, error) { ...@@ -54,13 +54,13 @@ func newApply(m map[string]interface{}, opts ...applyOpt) (*Apply, error) {
ol := newOptionLoader(m) ol := newOptionLoader(m)
a := &Apply{ a := &Apply{
app: ol.loadApp(), app: ol.LoadApp(),
clientConfig: ol.loadClientConfig(), clientConfig: ol.LoadClientConfig(),
componentNames: ol.loadStringSlice(OptionComponentNames), componentNames: ol.LoadStringSlice(OptionComponentNames),
create: ol.loadBool(OptionCreate), create: ol.LoadBool(OptionCreate),
dryRun: ol.loadBool(OptionDryRun), dryRun: ol.LoadBool(OptionDryRun),
gcTag: ol.loadString(OptionGcTag), gcTag: ol.LoadString(OptionGcTag),
skipGc: ol.loadBool(OptionSkipGc), skipGc: ol.LoadBool(OptionSkipGc),
runApplyFn: cluster.RunApply, runApplyFn: cluster.RunApply,
} }
......
...@@ -50,9 +50,9 @@ func NewComponentList(m map[string]interface{}) (*ComponentList, error) { ...@@ -50,9 +50,9 @@ func NewComponentList(m map[string]interface{}) (*ComponentList, error) {
ol := newOptionLoader(m) ol := newOptionLoader(m)
cl := &ComponentList{ cl := &ComponentList{
app: ol.loadApp(), app: ol.LoadApp(),
module: ol.loadString(OptionModule), module: ol.LoadString(OptionModule),
output: ol.loadString(OptionOutput), output: ol.LoadString(OptionOutput),
cm: component.DefaultManager, cm: component.DefaultManager,
out: os.Stdout, out: os.Stdout,
......
...@@ -43,8 +43,8 @@ func NewComponentRm(m map[string]interface{}) (*ComponentRm, error) { ...@@ -43,8 +43,8 @@ func NewComponentRm(m map[string]interface{}) (*ComponentRm, error) {
ol := newOptionLoader(m) ol := newOptionLoader(m)
cr := &ComponentRm{ cr := &ComponentRm{
app: ol.loadApp(), app: ol.LoadApp(),
name: ol.loadString(OptionComponentName), name: ol.LoadString(OptionComponentName),
componentDeleteFn: component.Delete, componentDeleteFn: component.Delete,
} }
......
...@@ -26,7 +26,7 @@ type currentEnver interface { ...@@ -26,7 +26,7 @@ type currentEnver interface {
} }
func setCurrentEnv(em environmentMetadata, ce currentEnver, ol *optionLoader) error { func setCurrentEnv(em environmentMetadata, ce currentEnver, ol *optionLoader) error {
envName := ol.loadOptionalString(OptionEnvName) envName := ol.LoadOptionalString(OptionEnvName)
if envName == "" { if envName == "" {
envName = em.CurrentEnvironment() envName = em.CurrentEnvironment()
} }
......
...@@ -51,10 +51,10 @@ func newDelete(m map[string]interface{}, opts ...deleteOpt) (*Delete, error) { ...@@ -51,10 +51,10 @@ func newDelete(m map[string]interface{}, opts ...deleteOpt) (*Delete, error) {
ol := newOptionLoader(m) ol := newOptionLoader(m)
d := &Delete{ d := &Delete{
app: ol.loadApp(), app: ol.LoadApp(),
clientConfig: ol.loadClientConfig(), clientConfig: ol.LoadClientConfig(),
componentNames: ol.loadStringSlice(OptionComponentNames), componentNames: ol.LoadStringSlice(OptionComponentNames),
gracePeriod: ol.loadInt64(OptionGracePeriod), gracePeriod: ol.LoadInt64(OptionGracePeriod),
runDeleteFn: cluster.RunDelete, runDeleteFn: cluster.RunDelete,
} }
......
...@@ -47,12 +47,12 @@ func NewEnvAdd(m map[string]interface{}) (*EnvAdd, error) { ...@@ -47,12 +47,12 @@ func NewEnvAdd(m map[string]interface{}) (*EnvAdd, error) {
ol := newOptionLoader(m) ol := newOptionLoader(m)
ea := &EnvAdd{ ea := &EnvAdd{
app: ol.loadApp(), app: ol.LoadApp(),
envName: ol.loadString(OptionEnvName), envName: ol.LoadString(OptionEnvName),
server: ol.loadString(OptionServer), server: ol.LoadString(OptionServer),
namespace: ol.loadString(OptionModule), namespace: ol.LoadString(OptionModule),
k8sSpecFlag: ol.loadString(OptionSpecFlag), k8sSpecFlag: ol.LoadString(OptionSpecFlag),
isOverride: ol.loadBool(OptionOverride), isOverride: ol.LoadBool(OptionOverride),
envCreateFn: env.Create, envCreateFn: env.Create,
} }
......
...@@ -48,9 +48,9 @@ func newEnvCurrent(m map[string]interface{}) (*EnvCurrent, error) { ...@@ -48,9 +48,9 @@ func newEnvCurrent(m map[string]interface{}) (*EnvCurrent, error) {
ol := newOptionLoader(m) ol := newOptionLoader(m)
d := &EnvCurrent{ d := &EnvCurrent{
app: ol.loadApp(), app: ol.LoadApp(),
envName: ol.loadOptionalString(OptionEnvName), envName: ol.LoadOptionalString(OptionEnvName),
unset: ol.loadBool(OptionUnset), unset: ol.LoadBool(OptionUnset),
out: os.Stdout, out: os.Stdout,
} }
......
...@@ -46,8 +46,8 @@ func NewEnvDescribe(m map[string]interface{}) (*EnvDescribe, error) { ...@@ -46,8 +46,8 @@ func NewEnvDescribe(m map[string]interface{}) (*EnvDescribe, error) {
ol := newOptionLoader(m) ol := newOptionLoader(m)
ed := &EnvDescribe{ ed := &EnvDescribe{
app: ol.loadApp(), app: ol.LoadApp(),
envName: ol.loadString(OptionEnvName), envName: ol.LoadString(OptionEnvName),
out: os.Stdout, out: os.Stdout,
} }
......
...@@ -48,8 +48,8 @@ func NewEnvList(m map[string]interface{}) (*EnvList, error) { ...@@ -48,8 +48,8 @@ func NewEnvList(m map[string]interface{}) (*EnvList, error) {
ol := newOptionLoader(m) ol := newOptionLoader(m)
el := &EnvList{ el := &EnvList{
app: ol.loadApp(), app: ol.LoadApp(),
outputType: ol.loadOptionalString(OptionOutput), outputType: ol.LoadOptionalString(OptionOutput),
out: os.Stdout, out: os.Stdout,
} }
......
...@@ -46,9 +46,9 @@ func NewEnvRm(m map[string]interface{}) (*EnvRm, error) { ...@@ -46,9 +46,9 @@ func NewEnvRm(m map[string]interface{}) (*EnvRm, error) {
ol := newOptionLoader(m) ol := newOptionLoader(m)
ea := &EnvRm{ ea := &EnvRm{
app: ol.loadApp(), app: ol.LoadApp(),
envName: ol.loadString(OptionEnvName), envName: ol.LoadString(OptionEnvName),
isOverride: ol.loadBool(OptionOverride), isOverride: ol.LoadBool(OptionOverride),
envDeleteFn: env.Delete, envDeleteFn: env.Delete,
} }
......
...@@ -64,10 +64,10 @@ func NewEnvSet(m map[string]interface{}) (*EnvSet, error) { ...@@ -64,10 +64,10 @@ func NewEnvSet(m map[string]interface{}) (*EnvSet, error) {
ol := newOptionLoader(m) ol := newOptionLoader(m)
es := &EnvSet{