base_app_test.go 8.43 KB
Newer Older
bryanl's avatar
bryanl committed
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 app

import (
bryanl's avatar
bryanl committed
19
	"path/filepath"
bryanl's avatar
bryanl committed
20 21 22
	"testing"

	"github.com/spf13/afero"
bryanl's avatar
bryanl committed
23
	"github.com/stretchr/testify/assert"
bryanl's avatar
bryanl committed
24 25 26
	"github.com/stretchr/testify/require"
)

bryanl's avatar
bryanl committed
27 28 29 30 31 32 33 34 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
func Test_baseapp_CurrentEnvironment(t *testing.T) {
	cases := []struct {
		name     string
		init     func(*testing.T, afero.Fs)
		expected string
	}{
		{
			name: "without a current environment set",
		},
		{
			name: "with a current environment set",
			init: func(t *testing.T, fs afero.Fs) {
				path := filepath.Join("/", currentEnvName)
				err := afero.WriteFile(fs, path, []byte("default"), DefaultFilePermissions)
				require.NoError(t, err)
			},
			expected: "default",
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			fs := afero.NewMemMapFs()
			stageFile(t, fs, "app010_app.yaml", "/app.yaml")
			ba := newBaseApp(fs, "/")

			if tc.init != nil {
				tc.init(t, fs)
			}
			got := ba.CurrentEnvironment()
			require.Equal(t, tc.expected, got)
		})
	}
}

func Test_baseapp_SetCurrentEnvironment(t *testing.T) {
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
	cases := []struct {
		name    string
		envName string
		isErr   bool
	}{
		{
			name:    "environment exists",
			envName: "default",
		},
		{
			name:    "environment does not exist",
			envName: "invalid",
			isErr:   true,
		},
	}
bryanl's avatar
bryanl committed
78

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			fs := afero.NewMemMapFs()
			stageFile(t, fs, "app010_app.yaml", "/app.yaml")
			ba := newBaseApp(fs, "/")

			err := ba.SetCurrentEnvironment(tc.envName)
			if tc.isErr {
				require.Error(t, err)
				return
			}

			require.NoError(t, err)

			current := ba.CurrentEnvironment()
			assert.Equal(t, tc.envName, current)
		})
	}
bryanl's avatar
bryanl committed
97 98 99

}

bryanl's avatar
bryanl committed
100 101 102 103 104 105 106
func Test_baseApp_AddRegistry(t *testing.T) {
	fs := afero.NewMemMapFs()

	stageFile(t, fs, "app010_app.yaml", "/app.yaml")

	ba := newBaseApp(fs, "/")

107
	reg := &RegistryConfig{
bryanl's avatar
bryanl committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
		Name: "new",
	}

	err := ba.AddRegistry(reg, false)
	require.NoError(t, err)

	assertContents(t, fs, "add-registry.yaml", ba.configPath())
	assertNotExists(t, fs, ba.overridePath())
}

func Test_baseApp_AddRegistry_override(t *testing.T) {
	fs := afero.NewMemMapFs()

	stageFile(t, fs, "app010_app.yaml", "/app.yaml")

	ba := newBaseApp(fs, "/")

125
	reg := &RegistryConfig{
bryanl's avatar
bryanl committed
126 127 128 129 130 131
		Name: "new",
	}

	err := ba.AddRegistry(reg, true)
	require.NoError(t, err)

132
	assertContents(t, fs, "app020_app.yaml", ba.configPath())
bryanl's avatar
bryanl committed
133 134 135 136 137 138 139 140 141 142
	assertContents(t, fs, "add-registry-override.yaml", ba.overridePath())
}

func Test_baseApp_AddRegistry_override_existing(t *testing.T) {
	fs := afero.NewMemMapFs()

	stageFile(t, fs, "app010_app.yaml", "/app.yaml")

	ba := newBaseApp(fs, "/")

143
	reg := &RegistryConfig{
bryanl's avatar
bryanl committed
144 145 146 147 148 149 150
		Name: "incubator",
	}

	err := ba.AddRegistry(reg, true)
	require.NoError(t, err)
}

151 152 153
func Test_baseApp_UpdateRegistry(t *testing.T) {
	tests := []struct {
		name           string
154
		regSpec        RegistryConfig
155 156 157 158 159 160
		appFilePath    string
		expectFilePath string
		expectErr      bool
	}{
		{
			name:           "no such registry",
161
			regSpec:        RegistryConfig{Name: "no-such-registry"},
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
			appFilePath:    "app010_app.yaml",
			expectFilePath: "",
			expectErr:      true,
		},
	}

	for _, tc := range tests {
		fs := afero.NewMemMapFs()

		if tc.appFilePath != "" {
			stageFile(t, fs, tc.appFilePath, "/app.yaml")
		}

		ba := newBaseApp(fs, "/")

		// Test updating non-existing registry
		err := ba.UpdateRegistry(&tc.regSpec)
		if tc.expectErr {
			require.Error(t, err)
		} else {
			require.NoError(t, err)
		}

		if tc.expectFilePath != "" {
			assertContents(t, fs, tc.expectFilePath, ba.configPath())
		}
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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
	}

}

func Test_baseApp_UpdateLibrary(t *testing.T) {
	tests := []struct {
		name           string
		libCfg         LibraryConfig
		env            string
		appFilePath    string
		expectFilePath string
		expectErr      bool
	}{
		{
			name: "no such environment",
			libCfg: LibraryConfig{
				Name:     "nginx",
				Registry: "incubator",
				Version:  "1.2.3",
			},
			env:         "no-such-environment",
			appFilePath: "app020_app.yaml",
			expectErr:   true,
		},
		{
			name: "success - global scope",
			libCfg: LibraryConfig{
				Name:     "nginx",
				Registry: "incubator",
				Version:  "1.2.3",
			},
			env:            "",
			appFilePath:    "app020_app.yaml",
			expectFilePath: "pkg-install-global.yaml",
			expectErr:      false,
		},
		{
			name: "success - environment scope",
			libCfg: LibraryConfig{
				Name:     "nginx",
				Registry: "incubator",
				Version:  "1.2.3",
			},
			env:            "default",
			appFilePath:    "app010_app.yaml",
			expectFilePath: "pkg-install-env-scope.yaml",
			expectErr:      false,
		},
	}

	for _, tc := range tests {
		fs := afero.NewMemMapFs()

		if tc.appFilePath != "" {
			stageFile(t, fs, tc.appFilePath, "/app.yaml")
		}

		ba := newBaseApp(fs, "/")

		// Test updating non-existing registry
		err := ba.UpdateLib(tc.libCfg.Name, tc.env, &tc.libCfg)
		if tc.expectErr {
			require.Error(t, err)
		} else {
			require.NoError(t, err)
		}

		if tc.expectFilePath != "" {
			assertContents(t, fs, tc.expectFilePath, ba.configPath())
		}
258 259 260 261
	}

}

bryanl's avatar
bryanl committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275
func Test_baseApp_load_override(t *testing.T) {
	fs := afero.NewMemMapFs()

	stageFile(t, fs, "app010_app.yaml", "/app.yaml")
	stageFile(t, fs, "add-registry-override.yaml", "/app.override.yaml")

	ba := newBaseApp(fs, "/")

	err := ba.load()
	require.NoError(t, err)

	_, ok := ba.overrides.Registries["new"]
	require.True(t, ok)
}
276 277 278 279 280 281 282 283 284 285 286 287

func Test_baseApp_load_override_invalid(t *testing.T) {
	fs := afero.NewMemMapFs()

	stageFile(t, fs, "app010_app.yaml", "/app.yaml")
	stageFile(t, fs, "add-registry-override-invalid.yaml", "/app.override.yaml")

	ba := newBaseApp(fs, "/")

	err := ba.load()
	require.Error(t, err)
}
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377

func Test_baseApp_environment_override_is_merged(t *testing.T) {
	fs := afero.NewMemMapFs()
	ba := newBaseApp(fs, "/")
	ba.load = func() error {
		return nil
	}
	ba.config.Environments = EnvironmentConfigs{
		"default": &EnvironmentConfig{
			Name: "default",
			Libraries: LibraryConfigs{
				"nginx": &LibraryConfig{Name: "nginx"},
			},
			KubernetesVersion: "v1.7.0",
			Destination: &EnvironmentDestinationSpec{
				Server:    "http://server.com",
				Namespace: "namespace",
			},
			Path:    "default",
			Targets: []string{"target1", "target2"},
		},
	}
	ba.overrides.Environments["default"] = &EnvironmentConfig{
		Name:              "default",
		KubernetesVersion: "v1.8.0",
		Destination: &EnvironmentDestinationSpec{
			Server:    "http://override.com",
			Namespace: "override",
		},
		Path:    "overrides/path",
		Targets: []string{"override1", "override2"},
	}

	expected := &EnvironmentConfig{
		Name: "default",
		Libraries: LibraryConfigs{
			"nginx": &LibraryConfig{Name: "nginx"},
		},
		KubernetesVersion: "v1.8.0",
		Destination: &EnvironmentDestinationSpec{
			Server:    "http://override.com",
			Namespace: "override",
		},
		Path:       "overrides/path",
		Targets:    []string{"override1", "override2"},
		isOverride: true,
	}

	e, err := ba.Environment("default")
	assert.NoError(t, err, "fetching environment")

	assert.Equal(t, expected, e)
}

func Test_baseApp_environment_just_override(t *testing.T) {
	fs := afero.NewMemMapFs()
	ba := newBaseApp(fs, "/")
	ba.load = func() error {
		return nil
	}
	ba.config.Environments = EnvironmentConfigs{}
	ba.overrides.Environments["default"] = &EnvironmentConfig{
		Name:              "default",
		KubernetesVersion: "v1.8.0",
		Destination: &EnvironmentDestinationSpec{
			Server:    "http://override.com",
			Namespace: "override",
		},
		Path:       "overrides/path",
		Targets:    []string{"override1", "override2"},
		isOverride: false,
	}

	expected := &EnvironmentConfig{
		Name:              "default",
		KubernetesVersion: "v1.8.0",
		Destination: &EnvironmentDestinationSpec{
			Server:    "http://override.com",
			Namespace: "override",
		},
		Path:       "overrides/path",
		Targets:    []string{"override1", "override2"},
		isOverride: true,
	}

	e, err := ba.Environment("default")
	assert.NoError(t, err, "fetching environment")

	assert.Equal(t, expected, e)
}