actions_test.go 6.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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 (
bryanl's avatar
bryanl committed
19
	"fmt"
20 21
	"io/ioutil"
	"path/filepath"
bryanl's avatar
bryanl committed
22
	"reflect"
23 24
	"testing"

bryanl's avatar
bryanl committed
25
	"github.com/ksonnet/ksonnet/pkg/app/mocks"
bryanl's avatar
bryanl committed
26
	"github.com/ksonnet/ksonnet/pkg/client"
bryanl's avatar
bryanl committed
27
	cmocks "github.com/ksonnet/ksonnet/pkg/component/mocks"
bryanl's avatar
bryanl committed
28
	"github.com/ksonnet/ksonnet/pkg/registry"
29
	rmocks "github.com/ksonnet/ksonnet/pkg/registry/mocks"
bryanl's avatar
bryanl committed
30
	"github.com/pkg/errors"
31 32 33 34
	"github.com/spf13/afero"
	"github.com/stretchr/testify/require"
)

bryanl's avatar
bryanl committed
35 36 37 38 39 40 41 42 43 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
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",
			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)
		})
	}
bryanl's avatar
bryanl committed
157 158
}

bryanl's avatar
bryanl committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
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)
		})
	}
bryanl's avatar
bryanl committed
239 240
}

241 242 243 244 245 246
func withApp(t *testing.T, fn func(*mocks.App)) {
	fs := afero.NewMemMapFs()

	appMock := &mocks.App{}
	appMock.On("Fs").Return(fs)
	appMock.On("Root").Return("/")
247
	appMock.On("VendorPath").Return("/vendor")
248 249 250 251 252

	fn(appMock)
}

func assertOutput(t *testing.T, filename, actual string) {
bryanl's avatar
bryanl committed
253
	require.NotEmpty(t, filename, "filename can not be empty")
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	path := filepath.Join("testdata", filename)
	b, err := ioutil.ReadFile(path)
	require.NoError(t, err)

	require.Equal(t, string(b), actual)
}

func stageFile(t *testing.T, fs afero.Fs, src, dest string) {
	in := filepath.Join("testdata", src)

	b, err := ioutil.ReadFile(in)
	require.NoError(t, err)

	dir := filepath.Dir(dest)
	err = fs.MkdirAll(dir, 0755)
	require.NoError(t, err)

	err = afero.WriteFile(fs, dest, b, 0644)
	require.NoError(t, err)
}

275 276
func mockNsWithName(name string) *cmocks.Module {
	m := &cmocks.Module{}
277 278 279
	m.On("Name").Return(name)
	return m
}
280

Oren Shomron's avatar
Oren Shomron committed
281
func mockRegistry(name string) *rmocks.Registry {
282 283
	m := &rmocks.Registry{}
	m.On("Name").Return(name)
bryanl's avatar
bryanl committed
284
	m.On("Protocol").Return(registry.ProtocolGitHub)
285 286 287 288
	m.On("URI").Return("github.com/ksonnet/parts/tree/master/incubator")

	return m
}