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

bryanl's avatar
bryanl committed
23 24
	"github.com/ksonnet/ksonnet/pkg/app"
	amocks "github.com/ksonnet/ksonnet/pkg/app/mocks"
bryanl's avatar
bryanl committed
25
	"github.com/ksonnet/ksonnet/pkg/util/test"
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
	"github.com/stretchr/testify/require"
)

func TestEnvList(t *testing.T) {
	withApp(t, func(appMock *amocks.App) {

		env := &app.EnvironmentSpec{
			KubernetesVersion: "v1.7.0",
			Destination: &app.EnvironmentDestinationSpec{
				Namespace: "default",
				Server:    "http://example.com",
			},
		}
		envs := app.EnvironmentSpecs{
			"default": env,
		}

		appMock.On("Environments").Return(envs, nil)
bryanl's avatar
bryanl committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

		cases := []struct {
			name         string
			outputType   string
			expectedFile string
			isErr        bool
		}{
			{
				name:         "no format specified",
				expectedFile: filepath.Join("env", "list", "output.txt"),
			},
			{
				name:         "wide output",
				outputType:   OutputWide,
				expectedFile: filepath.Join("env", "list", "output.txt"),
			},
			{
				name:         "json output",
bryanl's avatar
bryanl committed
62
				outputType:   OutputJSON,
bryanl's avatar
bryanl committed
63 64 65 66 67 68 69
				expectedFile: filepath.Join("env", "list", "output.json"),
			},
			{
				name:       "invalid output format",
				outputType: "invalid",
				isErr:      true,
			},
bryanl's avatar
bryanl committed
70
		}
71

bryanl's avatar
bryanl committed
72 73 74 75 76 77 78 79 80
		for _, tc := range cases {
			t.Run(tc.name, func(t *testing.T) {
				in := map[string]interface{}{
					OptionApp:    appMock,
					OptionOutput: tc.outputType,
				}

				a, err := NewEnvList(in)
				require.NoError(t, err)
81

bryanl's avatar
bryanl committed
82 83
				var buf bytes.Buffer
				a.out = &buf
84

bryanl's avatar
bryanl committed
85 86 87 88 89 90 91 92 93 94
				err = a.Run()
				if tc.isErr {
					require.Error(t, err)
					return
				}

				require.NoError(t, err)
				test.AssertOutput(t, tc.expectedFile, buf.String())
			})
		}
95 96 97

	})
}