import_test.go 2.3 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
// 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"

	"github.com/stretchr/testify/mock"

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

func TestImport_file(t *testing.T) {
	withApp(t, func(appMock *amocks.App) {
32
		module := "/"
33 34 35 36
		path := "/file.yaml"

		stageFile(t, appMock.Fs(), "import/file.yaml", path)

bryanl's avatar
bryanl committed
37
		in := map[string]interface{}{
38 39 40
			OptionApp:    appMock,
			OptionModule: module,
			OptionPath:   path,
bryanl's avatar
bryanl committed
41 42 43
		}

		a, err := NewImport(in)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
		require.NoError(t, err)

		cm := &cmocks.Manager{}
		cm.On("CreateComponent", mock.Anything, "/file", "",
			params.Params{}, prototype.YAML).Return("/", nil)

		a.cm = cm

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

func TestImport_directory(t *testing.T) {
	withApp(t, func(appMock *amocks.App) {
59
		module := "/"
60 61 62 63
		path := "/import"

		stageFile(t, appMock.Fs(), "import/file.yaml", "/import/file.yaml")

bryanl's avatar
bryanl committed
64
		in := map[string]interface{}{
65 66 67
			OptionApp:    appMock,
			OptionModule: module,
			OptionPath:   path,
bryanl's avatar
bryanl committed
68 69 70
		}

		a, err := NewImport(in)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
		require.NoError(t, err)

		cm := &cmocks.Manager{}
		cm.On("CreateComponent", mock.Anything, "/file", "",
			params.Params{}, prototype.YAML).Return("/", nil)

		a.cm = cm

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

func TestImport_invalid_file(t *testing.T) {
	withApp(t, func(appMock *amocks.App) {
86
		module := "/"
87 88
		path := "/import"

bryanl's avatar
bryanl committed
89
		in := map[string]interface{}{
90 91 92
			OptionApp:    appMock,
			OptionModule: module,
			OptionPath:   path,
bryanl's avatar
bryanl committed
93 94 95
		}

		a, err := NewImport(in)
96 97 98 99 100 101
		require.NoError(t, err)

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