env_set_test.go 5.28 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
func TestEnvSet(t *testing.T) {
	envName := "old_env_name"
	newName := "new_env_name"
	oldNamespace := "old_namespace"
31
	namespace := "new_namespace"
32 33 34 35
	oldServer := "old_server"
	server := "new_server"
	newk8sAPISpec := "version:new_api_spec"

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

bryanl's avatar
bryanl committed
46
	withApp(t, func(appMock *amocks.App) {
47 48 49
		cases := []struct {
			name        string
			in          map[string]interface{}
50
			spec        *app.EnvironmentConfig
51
			envRenameFn func(t *testing.T) envRenameFn
52
			saveFn      func(t *testing.T) saveFn
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
		}{
			{
				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
70
			},
71 72 73 74 75 76 77
			{
				name: "set new namespace",
				in: map[string]interface{}{
					OptionApp:       appMock,
					OptionEnvName:   envName,
					OptionNamespace: namespace,
				},
78
				saveFn: func(t *testing.T) saveFn {
79
					return func(a app.App, envName, k8sAPISpec string, spec *app.EnvironmentConfig, override bool) error {
80 81
						assert.Equal(t, &app.EnvironmentConfig{
							Name: envName,
82 83 84 85
							Destination: &app.EnvironmentDestinationSpec{
								Namespace: namespace,
								Server:    oldServer,
							},
86
						}, spec)
87 88 89 90 91 92 93 94 95 96 97
						return nil
					}
				},
			},
			{
				name: "set new server",
				in: map[string]interface{}{
					OptionApp:     appMock,
					OptionEnvName: envName,
					OptionServer:  server,
				},
98
				saveFn: func(t *testing.T) saveFn {
99
					return func(a app.App, envName, k8sAPISpec string, spec *app.EnvironmentConfig, override bool) error {
100 101
						assert.Equal(t, &app.EnvironmentConfig{
							Name: envName,
102 103 104 105
							Destination: &app.EnvironmentDestinationSpec{
								Namespace: oldNamespace,
								Server:    server,
							},
106
						}, spec)
107 108 109 110 111 112 113 114 115 116 117
						return nil
					}
				},
			},
			{
				name: "set new api spec",
				in: map[string]interface{}{
					OptionApp:      appMock,
					OptionEnvName:  envName,
					OptionSpecFlag: newk8sAPISpec,
				},
118
				saveFn: func(t *testing.T) saveFn {
119
					return func(a app.App, envName, k8sAPISpec string, spec *app.EnvironmentConfig, override bool) error {
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
						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,
				},
135
				saveFn: func(t *testing.T) saveFn {
136
					return func(a app.App, newName, k8sAPISpec string, spec *app.EnvironmentConfig, override bool) error {
137 138
						assert.Equal(t, &app.EnvironmentConfig{
							Name: newName,
139 140 141 142
							Destination: &app.EnvironmentDestinationSpec{
								Namespace: namespace,
								Server:    server,
							},
143
						}, spec)
144 145 146 147 148 149 150 151 152 153 154 155 156
						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
157
			},
Oren Shomron's avatar
Oren Shomron committed
158
			// TODO add tests for overrides here
bryanl's avatar
bryanl committed
159 160
		}

161 162 163 164
		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
165

166 167
				if tc.envRenameFn != nil {
					a.envRenameFn = tc.envRenameFn(t)
168 169 170 171 172
				} else {
					a.envRenameFn = func(a app.App, from, to string, override bool) error {
						t.Errorf("unexpected call: rename")
						return nil
					}
173
				}
bryanl's avatar
bryanl committed
174

175 176 177 178 179 180 181
				if tc.saveFn != nil {
					a.saveFn = tc.saveFn(t)
				} else {
					a.saveFn = func(a app.App, newName, k8sAPISpec string, spec *app.EnvironmentConfig, override bool) error {
						t.Errorf("unexpected call: save")
						return nil
					}
182
				}
bryanl's avatar
bryanl committed
183

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

186 187
				err = a.Run()
				require.NoError(t, err)
bryanl's avatar
bryanl committed
188

189
			})
bryanl's avatar
bryanl committed
190 191 192
		}
	})
}
193 194 195 196 197 198

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