param_list_test.go 3.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// 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 actions

import (
	"bytes"
bryanl's avatar
bryanl committed
20
	"path/filepath"
21 22 23 24
	"testing"

	"github.com/ksonnet/ksonnet/component"
	cmocks "github.com/ksonnet/ksonnet/component/mocks"
bryanl's avatar
bryanl committed
25
	"github.com/ksonnet/ksonnet/metadata/app"
26
	amocks "github.com/ksonnet/ksonnet/metadata/app/mocks"
bryanl's avatar
bryanl committed
27
	"github.com/stretchr/testify/assert"
28 29 30
	"github.com/stretchr/testify/require"
)

bryanl's avatar
bryanl committed
31 32 33 34
func TestParamList(t *testing.T) {
	moduleParams := []component.ModuleParameter{
		{Component: "deployment", Index: "0", Key: "key", Value: `"value"`},
	}
35

bryanl's avatar
bryanl committed
36 37 38
	module := &cmocks.Module{}
	module.On("Params", "envName").Return(moduleParams, nil)
	module.On("Params", "").Return(moduleParams, nil)
39

bryanl's avatar
bryanl committed
40 41
	c := &cmocks.Component{}
	c.On("Params", "").Return(moduleParams, nil)
42 43

	withApp(t, func(appMock *amocks.App) {
bryanl's avatar
bryanl committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
		cases := []struct {
			name            string
			in              map[string]interface{}
			findModulesFn   func(t *testing.T) findModulesFn
			findModuleFn    func(t *testing.T) findModuleFn
			findComponentFn func(t *testing.T) findComponentFn
			outputFile      string
		}{
			{
				name: "component name",
				in: map[string]interface{}{
					OptionApp:           appMock,
					OptionComponentName: "deployment",
					OptionModule:        "module",
				},
				findModuleFn: func(t *testing.T) findModuleFn {
					return func(a app.App, moduleName string) (component.Module, error) {
						assert.Equal(t, "module", moduleName)
						return module, nil
					}
				},
				findComponentFn: func(t *testing.T) findComponentFn {
					return func(a app.App, moduleName, componentName string) (component.Component, error) {
						assert.Equal(t, "module", moduleName)
						assert.Equal(t, "deployment", componentName)
						return c, nil
					}
				},
				outputFile: filepath.Join("param", "list", "with_component.txt"),
			},
			{
				name: "no component name",
				in: map[string]interface{}{
					OptionApp:    appMock,
					OptionModule: "module",
				},
				findModuleFn: func(t *testing.T) findModuleFn {
					return func(a app.App, moduleName string) (component.Module, error) {
						assert.Equal(t, "module", moduleName)
						return module, nil
					}
				},
				findComponentFn: func(t *testing.T) findComponentFn {
					return func(a app.App, moduleName, componentName string) (component.Component, error) {
						assert.Equal(t, "module", moduleName)
						assert.Equal(t, "deployment", componentName)
						return c, nil
					}
				},
				outputFile: filepath.Join("param", "list", "without_component.txt"),
			},
			{
				name: "env",
				in: map[string]interface{}{
					OptionApp:     appMock,
					OptionEnvName: "envName",
				},
				findModulesFn: func(t *testing.T) findModulesFn {
					return func(a app.App, envName string) ([]component.Module, error) {
						assert.Equal(t, "envName", envName)
						return []component.Module{module}, nil
					}
				},
				outputFile: filepath.Join("param", "list", "env.txt"),
			},
109 110
		}

bryanl's avatar
bryanl committed
111 112
		for _, tc := range cases {
			t.Run(tc.name, func(t *testing.T) {
113

bryanl's avatar
bryanl committed
114 115
				a, err := NewParamList(tc.in)
				require.NoError(t, err)
116

bryanl's avatar
bryanl committed
117 118 119
				if tc.findModulesFn != nil {
					a.findModulesFn = tc.findModulesFn(t)
				}
bryanl's avatar
bryanl committed
120

bryanl's avatar
bryanl committed
121 122 123
				if tc.findModuleFn != nil {
					a.findModuleFn = tc.findModuleFn(t)
				}
124

bryanl's avatar
bryanl committed
125 126 127
				if tc.findComponentFn != nil {
					a.findComponentFn = tc.findComponentFn(t)
				}
128

bryanl's avatar
bryanl committed
129 130
				var buf bytes.Buffer
				a.out = &buf
131

bryanl's avatar
bryanl committed
132 133
				err = a.Run()
				require.NoError(t, err)
134

bryanl's avatar
bryanl committed
135 136 137
				assertOutput(t, tc.outputFile, buf.String())
			})
		}
138 139
	})
}