env_set_test.go 4.87 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 19 20
// 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 (
	"testing"

bryanl's avatar
bryanl committed
21 22
	"github.com/ksonnet/ksonnet/pkg/app"
	amocks "github.com/ksonnet/ksonnet/pkg/app/mocks"
bryanl's avatar
bryanl committed
23
	"github.com/stretchr/testify/assert"
bryanl's avatar
bryanl committed
24 25 26
	"github.com/stretchr/testify/require"
)

27 28 29 30 31 32 33 34 35 36 37
func TestEnvSet(t *testing.T) {
	envName := "old_env_name"
	newName := "new_env_name"
	oldNamespace := "old_namespace"
	namespace := "new_namesapce"
	oldServer := "old_server"
	server := "new_server"
	newk8sAPISpec := "version:new_api_spec"

	environmentMockFn := func(name string) *app.EnvironmentSpec {
		return &app.EnvironmentSpec{
bryanl's avatar
bryanl committed
38
			Destination: &app.EnvironmentDestinationSpec{
39
				Namespace: oldNamespace,
40
				Server:    oldServer,
41 42
			},
		}
43
	}
44

bryanl's avatar
bryanl committed
45
	withApp(t, func(appMock *amocks.App) {
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
		cases := []struct {
			name        string
			in          map[string]interface{}
			spec        *app.EnvironmentSpec
			envRenameFn func(t *testing.T) envRenameFn
			updateEnvFn func(t *testing.T) updateEnvFn
		}{
			{
				name: "rename environment",
				in: map[string]interface{}{
					OptionApp:        appMock,
					OptionEnvName:    envName,
					OptionNewEnvName: newName,
				},
				envRenameFn: func(t *testing.T) envRenameFn {
					return func(a app.App, from, to string, override bool) error {
						assert.Equal(t, envName, from)
						assert.Equal(t, newName, to)
						assert.False(t, override)

						return nil
					}
				},
bryanl's avatar
bryanl committed
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
			{
				name: "set new namespace",
				in: map[string]interface{}{
					OptionApp:       appMock,
					OptionEnvName:   envName,
					OptionNamespace: namespace,
				},
				updateEnvFn: func(t *testing.T) updateEnvFn {
					return func(a app.App, envName, k8sAPISpec string, spec *app.EnvironmentSpec, override bool) error {
						assert.Equal(t, spec, &app.EnvironmentSpec{
							Destination: &app.EnvironmentDestinationSpec{
								Namespace: namespace,
								Server:    oldServer,
							},
						})
						return nil
					}
				},
			},
			{
				name: "set new server",
				in: map[string]interface{}{
					OptionApp:     appMock,
					OptionEnvName: envName,
					OptionServer:  server,
				},
				updateEnvFn: func(t *testing.T) updateEnvFn {
					return func(a app.App, envName, k8sAPISpec string, spec *app.EnvironmentSpec, override bool) error {
						assert.Equal(t, spec, &app.EnvironmentSpec{
							Destination: &app.EnvironmentDestinationSpec{
								Namespace: oldNamespace,
								Server:    server,
							},
						})
						return nil
					}
				},
			},
			{
				name: "set new api spec",
				in: map[string]interface{}{
					OptionApp:      appMock,
					OptionEnvName:  envName,
					OptionSpecFlag: newk8sAPISpec,
				},
				updateEnvFn: func(t *testing.T) updateEnvFn {
					return func(a app.App, envName, k8sAPISpec string, spec *app.EnvironmentSpec, override bool) error {
						assert.Equal(t, newk8sAPISpec, k8sAPISpec)
						return nil
					}
				},
			},
			{
				name: "set everything at once",
				in: map[string]interface{}{
					OptionApp:        appMock,
					OptionEnvName:    envName,
					OptionNewEnvName: newName,
					OptionNamespace:  namespace,
					OptionServer:     server,
					OptionSpecFlag:   newk8sAPISpec,
				},
				updateEnvFn: func(t *testing.T) updateEnvFn {
					return func(a app.App, newName, k8sAPISpec string, spec *app.EnvironmentSpec, override bool) error {
						assert.Equal(t, spec, &app.EnvironmentSpec{
							Destination: &app.EnvironmentDestinationSpec{
								Namespace: namespace,
								Server:    server,
							},
						})
						assert.Equal(t, newk8sAPISpec, k8sAPISpec)
						return nil
					}
				},
				envRenameFn: func(t *testing.T) envRenameFn {
					return func(a app.App, from, to string, override bool) error {
						assert.Equal(t, envName, from)
						assert.Equal(t, newName, to)
						assert.False(t, override)

						return nil
					}
				},
bryanl's avatar
bryanl committed
153 154 155
			},
		}

156 157 158 159
		for _, tc := range cases {
			t.Run(tc.name, func(t *testing.T) {
				a, err := NewEnvSet(tc.in)
				require.NoError(t, err)
bryanl's avatar
bryanl committed
160

161 162 163
				if tc.envRenameFn != nil {
					a.envRenameFn = tc.envRenameFn(t)
				}
bryanl's avatar
bryanl committed
164

165 166 167
				if tc.updateEnvFn != nil {
					a.updateEnvFn = tc.updateEnvFn(t)
				}
bryanl's avatar
bryanl committed
168

169
				appMock.On("Environment", tc.in[OptionEnvName]).Return(environmentMockFn, nil)
bryanl's avatar
bryanl committed
170

171 172
				err = a.Run()
				require.NoError(t, err)
bryanl's avatar
bryanl committed
173

174
			})
bryanl's avatar
bryanl committed
175 176 177
		}
	})
}
178 179 180 181 182 183

func TestEnvSet_requires_app(t *testing.T) {
	in := make(map[string]interface{})
	_, err := NewEnvSet(in)
	require.Error(t, err)
}