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 {
}
}
func (o *optionLoader) loadFs(name string) afero.Fs {
func (o *optionLoader) LoadFs(name string) afero.Fs {
i := o.load(name)
if i == nil {
return nil
......@@ -163,7 +163,7 @@ func (o *optionLoader) loadFs(name string) afero.Fs {
return a
}
func (o *optionLoader) loadBool(name string) bool {
func (o *optionLoader) LoadBool(name string) bool {
i := o.load(name)
if i == nil {
return false
......@@ -178,7 +178,7 @@ func (o *optionLoader) loadBool(name string) bool {
return a
}
func (o *optionLoader) loadOptionalBool(name string) bool {
func (o *optionLoader) LoadOptionalBool(name string) bool {
i := o.loadOptional(name)
if i == nil {
return false
......@@ -192,7 +192,7 @@ func (o *optionLoader) loadOptionalBool(name string) bool {
return a
}
func (o *optionLoader) loadInt(name string) int {
func (o *optionLoader) LoadInt(name string) int {
i := o.load(name)
if i == nil {
return 0
......@@ -207,7 +207,7 @@ func (o *optionLoader) loadInt(name string) int {
return a
}
func (o *optionLoader) loadInt64(name string) int64 {
func (o *optionLoader) LoadInt64(name string) int64 {
i := o.load(name)
if i == nil {
return 0
......@@ -222,7 +222,7 @@ func (o *optionLoader) loadInt64(name string) int64 {
return a
}
func (o *optionLoader) loadOptionalInt(name string) int {
func (o *optionLoader) LoadOptionalInt(name string) int {
i := o.loadOptional(name)
if i == nil {
return 0
......@@ -236,7 +236,7 @@ func (o *optionLoader) loadOptionalInt(name string) int {
return a
}
func (o *optionLoader) loadString(name string) string {
func (o *optionLoader) LoadString(name string) string {
i := o.load(name)
if i == nil {
return ""
......@@ -251,7 +251,7 @@ func (o *optionLoader) loadString(name string) string {
return a
}
func (o *optionLoader) loadOptionalString(name string) string {
func (o *optionLoader) LoadOptionalString(name string) string {
i := o.loadOptional(name)
if i == nil {
return ""
......@@ -265,7 +265,7 @@ func (o *optionLoader) loadOptionalString(name string) string {
return a
}
func (o *optionLoader) loadStringSlice(name string) []string {
func (o *optionLoader) LoadStringSlice(name string) []string {
i := o.load(name)
if i == nil {
return nil
......@@ -280,7 +280,7 @@ func (o *optionLoader) loadStringSlice(name string) []string {
return a
}
func (o *optionLoader) loadClientConfig() *client.Config {
func (o *optionLoader) LoadClientConfig() *client.Config {
i := o.load(OptionClientConfig)
if i == nil {
return nil
......@@ -295,7 +295,7 @@ func (o *optionLoader) loadClientConfig() *client.Config {
return a
}
func (o *optionLoader) loadApp() app.App {
func (o *optionLoader) LoadApp() app.App {
i := o.load(OptionApp)
if i == nil {
o.err = ErrNotInApp
......
......@@ -16,57 +16,227 @@
package actions
import (
"fmt"
"io/ioutil"
"path/filepath"
"reflect"
"testing"
"github.com/ksonnet/ksonnet/client"
cmocks "github.com/ksonnet/ksonnet/component/mocks"
"github.com/ksonnet/ksonnet/metadata/app/mocks"
"github.com/ksonnet/ksonnet/pkg/registry"
rmocks "github.com/ksonnet/ksonnet/pkg/registry/mocks"
"github.com/pkg/errors"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_optionsLoader_loadApp(t *testing.T) {
withApp(t, func(a *mocks.App) {
cases := []struct {
name string
m map[string]interface{}
isErr bool
}{
{
name: "with app",
m: map[string]interface{}{
OptionApp: a,
},
},
{
name: "with invalid app",
m: map[string]interface{}{
OptionApp: "invalid",
},
isErr: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
ol := newOptionLoader(tc.m)
got := ol.loadApp()
if tc.isErr {
require.Error(t, ol.err)
return
}
require.NoError(t, ol.err)
assert.Equal(t, a, got)
})
}
})
func Test_optionLoader_types(t *testing.T) {
cases := []struct {
name string
hasArg bool
valid interface{}
invalid interface{}
keyName string
}{
{
name: "App",
valid: &mocks.App{},
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: "StringSlice",
hasArg: true,
valid: []string{},
invalid: "invalid",
keyName: OptionName,
},
{
name: "ClientConfig",
valid: &client.Config{},
invalid: "invalid",
keyName: OptionClientConfig,
},
}
for _, tc := range cases {
methodName := fmt.Sprintf("Load%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 := 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))
}
loader.Call(callValues)
require.Error(t, ol.err)
})
}
}
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,
}
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.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)) {
......
......@@ -54,13 +54,13 @@ func newApply(m map[string]interface{}, opts ...applyOpt) (*Apply, error) {
ol := newOptionLoader(m)
a := &Apply{
app: ol.loadApp(),
clientConfig: ol.loadClientConfig(),
componentNames: ol.loadStringSlice(OptionComponentNames),
create: ol.loadBool(OptionCreate),
dryRun: ol.loadBool(OptionDryRun),
gcTag: ol.loadString(OptionGcTag),
skipGc: ol.loadBool(OptionSkipGc),
app: ol.LoadApp(),
clientConfig: ol.LoadClientConfig(),
componentNames: ol.LoadStringSlice(OptionComponentNames),
create: ol.LoadBool(OptionCreate),
dryRun: ol.LoadBool(OptionDryRun),
gcTag: ol.LoadString(OptionGcTag),
skipGc: ol.LoadBool(OptionSkipGc),
runApplyFn: cluster.RunApply,
}
......
......@@ -50,9 +50,9 @@ func NewComponentList(m map[string]interface{}) (*ComponentList, error) {
ol := newOptionLoader(m)
cl := &ComponentList{
app: ol.loadApp(),
module: ol.loadString(OptionModule),
output: ol.loadString(OptionOutput),
app: ol.LoadApp(),
module: ol.LoadString(OptionModule),
output: ol.LoadString(OptionOutput),
cm: component.DefaultManager,
out: os.Stdout,
......
......@@ -43,8 +43,8 @@ func NewComponentRm(m map[string]interface{}) (*ComponentRm, error) {
ol := newOptionLoader(m)
cr := &ComponentRm{
app: ol.loadApp(),
name: ol.loadString(OptionComponentName),
app: ol.LoadApp(),
name: ol.LoadString(OptionComponentName),
componentDeleteFn: component.Delete,
}
......
......@@ -26,7 +26,7 @@ type currentEnver interface {
}
func setCurrentEnv(em environmentMetadata, ce currentEnver, ol *optionLoader) error {
envName := ol.loadOptionalString(OptionEnvName)
envName := ol.LoadOptionalString(OptionEnvName)
if envName == "" {
envName = em.CurrentEnvironment()
}
......
......@@ -51,10 +51,10 @@ func newDelete(m map[string]interface{}, opts ...deleteOpt) (*Delete, error) {
ol := newOptionLoader(m)
d := &Delete{
app: ol.loadApp(),
clientConfig: ol.loadClientConfig(),
componentNames: ol.loadStringSlice(OptionComponentNames),
gracePeriod: ol.loadInt64(OptionGracePeriod),
app: ol.LoadApp(),
clientConfig: ol.LoadClientConfig(),
componentNames: ol.LoadStringSlice(OptionComponentNames),
gracePeriod: ol.LoadInt64(OptionGracePeriod),
runDeleteFn: cluster.RunDelete,
}
......
......@@ -47,12 +47,12 @@ func NewEnvAdd(m map[string]interface{}) (*EnvAdd, error) {
ol := newOptionLoader(m)
ea := &EnvAdd{
app: ol.loadApp(),
envName: ol.loadString(OptionEnvName),
server: ol.loadString(OptionServer),
namespace: ol.loadString(OptionModule),
k8sSpecFlag: ol.loadString(OptionSpecFlag),
isOverride: ol.loadBool(OptionOverride),
app: ol.LoadApp(),
envName: ol.LoadString(OptionEnvName),
server: ol.LoadString(OptionServer),
namespace: ol.LoadString(OptionModule),
k8sSpecFlag: ol.LoadString(OptionSpecFlag),
isOverride: ol.LoadBool(OptionOverride),
envCreateFn: env.Create,
}
......
......@@ -48,9 +48,9 @@ func newEnvCurrent(m map[string]interface{}) (*EnvCurrent, error) {
ol := newOptionLoader(m)
d := &EnvCurrent{
app: ol.loadApp(),
envName: ol.loadOptionalString(OptionEnvName),
unset: ol.loadBool(OptionUnset),
app: ol.LoadApp(),
envName: ol.LoadOptionalString(OptionEnvName),
unset: ol.LoadBool(OptionUnset),
out: os.Stdout,
}
......
......@@ -46,8 +46,8 @@ func NewEnvDescribe(m map[string]interface{}) (*EnvDescribe, error) {
ol := newOptionLoader(m)
ed := &EnvDescribe{
app: ol.loadApp(),
envName: ol.loadString(OptionEnvName),
app: ol.LoadApp(),
envName: ol.LoadString(OptionEnvName),
out: os.Stdout,
}
......
......@@ -48,8 +48,8 @@ func NewEnvList(m map[string]interface{}) (*EnvList, error) {
ol := newOptionLoader(m)
el := &EnvList{
app: ol.loadApp(),
outputType: ol.loadOptionalString(OptionOutput),
app: ol.LoadApp(),
outputType: ol.LoadOptionalString(OptionOutput),
out: os.Stdout,
}
......
......@@ -46,9 +46,9 @@ func NewEnvRm(m map[string]interface{}) (*EnvRm, error) {
ol := newOptionLoader(m)
ea := &EnvRm{
app: ol.loadApp(),
envName: ol.loadString(OptionEnvName),
isOverride: ol.loadBool(OptionOverride),
app: ol.LoadApp(),
envName: ol.LoadString(OptionEnvName),
isOverride: ol.LoadBool(OptionOverride),
envDeleteFn: env.Delete,
}
......
......@@ -64,10 +64,10 @@ func NewEnvSet(m map[string]interface{}) (*EnvSet, error) {
ol := newOptionLoader(m)
es := &EnvSet{
app: ol.loadApp(),
envName: ol.loadString(OptionEnvName),
newName: ol.loadOptionalString(OptionNewEnvName),
newNsName: ol.loadOptionalString(OptionNamespace),
app: ol.LoadApp(),
envName: ol.LoadString(OptionEnvName),
newName: ol.LoadOptionalString(OptionNewEnvName),
newNsName: ol.LoadOptionalString(OptionNamespace),
envRenameFn: env.Rename,
updateEnvFn: updateEnv,
......
......@@ -43,9 +43,9 @@ func NewEnvTargets(m map[string]interface{}) (*EnvTargets, error) {
ol := newOptionLoader(m)
et := &EnvTargets{
app: ol.loadApp(),
envName: ol.loadString(OptionEnvName),
modules: ol.loadStringSlice(OptionModule),
app: ol.LoadApp(),
envName: ol.LoadString(OptionEnvName),
modules: ol.LoadStringSlice(OptionModule),
cm: component.DefaultManager,
}
......
......@@ -57,9 +57,9 @@ func NewImport(m map[string]interface{}) (*Import, error) {
ol := newOptionLoader(m)
i := &Import{
app: ol.loadApp(),
module: ol.loadString(OptionModule),
path: ol.loadString(OptionPath),
app: ol.LoadApp(),
module: ol.LoadString(OptionModule),
path: ol.LoadString(OptionPath),
cm: component.DefaultManager,
}
......
......@@ -64,14 +64,14 @@ func NewInit(m map[string]interface{}) (*Init, error) {
ol := newOptionLoader(m)
i := &Init{
fs: ol.loadFs(OptionFs),
name: ol.loadString(OptionName),
rootPath: ol.loadString(OptionRootPath),
envName: ol.loadString(OptionEnvName),
k8sSpecFlag: ol.loadString(OptionSpecFlag),
serverURI: ol.loadOptionalString(OptionServer),
namespace: ol.loadString(OptionNamespace),
skipDefaultRegistries: ol.loadBool(OptionSkipDefaultRegistries),
fs: ol.LoadFs(OptionFs),
name: ol.LoadString(OptionName),
rootPath: ol.LoadString(OptionRootPath),
envName: ol.LoadString(OptionEnvName),
k8sSpecFlag: ol.LoadString(OptionSpecFlag),
serverURI: ol.LoadOptionalString(OptionServer),
namespace: ol.LoadString(OptionNamespace),
skipDefaultRegistries: ol.LoadBool(OptionSkipDefaultRegistries),
appInitFn: appinit.Init,
appLoadFn: app.Load,
......
......@@ -43,8 +43,8 @@ func NewModuleCreate(m map[string]interface{}) (*ModuleCreate, error) {
ol := newOptionLoader(m)
mc := &ModuleCreate{
app: ol.loadApp(),
module: ol.loadString(OptionModule),
app: ol.LoadApp(),
module: ol.LoadString(OptionModule),
cm: component.DefaultManager,
}
......
......@@ -48,8 +48,8 @@ func NewModuleList(m map[string]interface{}) (*ModuleList, error) {
ol := newOptionLoader(m)
nl := &ModuleList{
app: ol.loadApp(),
envName: ol.loadString(OptionEnvName),
app: ol.LoadApp(),
envName: ol.LoadString(OptionEnvName),
out: os.Stdout,
cm: component.DefaultManager,
......
......@@ -58,12 +58,12 @@ func NewParamDelete(m map[string]interface{}) (*ParamDelete, error) {
ol := newOptionLoader(m)
pd := &ParamDelete{
app: ol.loadApp(),
name: ol.loadOptionalString(OptionName),
rawPath: ol.loadString(OptionPath),
global: ol.loadOptionalBool(OptionGlobal),
envName: ol.loadOptionalString(OptionEnvName),
index: ol.loadOptionalInt(OptionIndex),