env_targets_test.go 2.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
// 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 (
	"errors"
	"testing"

	"github.com/stretchr/testify/mock"

	cmocks "github.com/ksonnet/ksonnet/component/mocks"
	"github.com/ksonnet/ksonnet/metadata/app"
	amocks "github.com/ksonnet/ksonnet/metadata/app/mocks"
	"github.com/stretchr/testify/require"
)

func TestEnvTargets(t *testing.T) {
	withApp(t, func(appMock *amocks.App) {
		envName := "default"
33
		modules := []string{"foo"}
34 35 36

		env := &app.EnvironmentSpec{}
		appMock.On("Environment", "default").Return(env, nil)
37
		appMock.On("UpdateTargets", envName, modules).Return(nil)
38

bryanl's avatar
bryanl committed
39
		in := map[string]interface{}{
40 41 42
			OptionApp:     appMock,
			OptionEnvName: envName,
			OptionModule:  modules,
bryanl's avatar
bryanl committed
43 44 45
		}

		a, err := NewEnvTargets(in)
46 47
		require.NoError(t, err)

48
		ns := &cmocks.Module{}
49 50

		cm := &cmocks.Manager{}
51
		cm.On("Module", mock.Anything, "foo").Return(ns, nil)
52 53 54 55 56 57 58 59

		a.cm = cm

		err = a.Run()
		require.NoError(t, err)
	})
}

60
func TestEnvTargets_invalid_module(t *testing.T) {
61 62
	withApp(t, func(appMock *amocks.App) {
		envName := "default"
63
		modules := []string{"foo"}
64 65 66

		env := &app.EnvironmentSpec{}
		appMock.On("Environment", "default").Return(env, nil)
67
		appMock.On("UpdateTargets", envName, modules).Return(nil)
68

bryanl's avatar
bryanl committed
69
		in := map[string]interface{}{
70 71 72
			OptionApp:     appMock,
			OptionEnvName: envName,
			OptionModule:  modules,
bryanl's avatar
bryanl committed
73 74 75
		}

		a, err := NewEnvTargets(in)
76 77
		require.NoError(t, err)

78
		ns := &cmocks.Module{}
79 80

		cm := &cmocks.Manager{}
81
		cm.On("Module", mock.Anything, "foo").Return(ns, errors.New("fail"))
82 83 84 85 86 87 88 89 90 91 92

		a.cm = cm

		err = a.Run()
		require.Error(t, err)
	})
}

func TestEnvTargets_invalid_environment(t *testing.T) {
	withApp(t, func(appMock *amocks.App) {
		envName := "invalid"
93
		modules := []string{"foo"}
94 95 96 97 98

		env := &app.EnvironmentSpec{}
		envErr := errors.New("environment invalid was not found")
		appMock.On("Environment", "invalid").Return(env, envErr)

bryanl's avatar
bryanl committed
99
		in := map[string]interface{}{
100 101 102
			OptionApp:     appMock,
			OptionEnvName: envName,
			OptionModule:  modules,
bryanl's avatar
bryanl committed
103 104 105
		}

		a, err := NewEnvTargets(in)
106 107 108 109 110 111 112 113 114 115
		require.NoError(t, err)

		cm := &cmocks.Manager{}

		a.cm = cm

		err = a.Run()
		require.Error(t, err)
	})
}