init_test.go 2.82 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 21 22
// 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"

	amocks "github.com/ksonnet/ksonnet/metadata/app/mocks"
	"github.com/ksonnet/ksonnet/pkg/registry"
23
	rmocks "github.com/ksonnet/ksonnet/pkg/registry/mocks"
bryanl's avatar
bryanl committed
24 25 26 27 28 29 30 31 32 33 34 35
	"github.com/spf13/afero"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestInit(t *testing.T) {
	withApp(t, func(appMock *amocks.App) {
		aFs := appMock.Fs()
		aName := "my-app"
		aRootPath := appMock.Root()
		aK8sSpecFlag := "specFlag"
		aServerURI := "http://example.com"
bryanl's avatar
bryanl committed
36
		aNamespace := "my-namespace"
bryanl's avatar
bryanl committed
37

38 39 40 41 42 43 44 45 46 47 48
		cases := []struct {
			name           string
			skipRegistries bool
		}{
			{
				name: "with registries",
			},
			{
				name:           "without registries",
				skipRegistries: true,
			},
bryanl's avatar
bryanl committed
49 50
		}

51 52 53 54 55 56 57 58 59 60 61
		for _, tc := range cases {
			t.Run(tc.name, func(t *testing.T) {
				in := map[string]interface{}{
					OptionFs:                    aFs,
					OptionName:                  aName,
					OptionRootPath:              aRootPath,
					OptionSpecFlag:              aK8sSpecFlag,
					OptionServer:                aServerURI,
					OptionNamespaceName:         aNamespace,
					OptionSkipDefaultRegistries: tc.skipRegistries,
				}
bryanl's avatar
bryanl committed
62

63 64
				a, err := NewInit(in)
				require.NoError(t, err)
bryanl's avatar
bryanl committed
65

66 67 68 69 70 71 72
				a.appInitFn = func(fs afero.Fs, name, rootPath, k8sSpecFlag, serverURI, namespace string, registries []registry.Registry) error {
					assert.Equal(t, aFs, fs)
					assert.Equal(t, aName, name)
					assert.Equal(t, aRootPath, rootPath)
					assert.Equal(t, aK8sSpecFlag, k8sSpecFlag)
					assert.Equal(t, aServerURI, serverURI)
					assert.Equal(t, aNamespace, namespace)
bryanl's avatar
bryanl committed
73

74 75 76
					if !tc.skipRegistries {
						assert.Len(t, registries, 1)
						r := registries[0]
bryanl's avatar
bryanl committed
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
						assert.Equal(t, "github", r.Protocol())
						assert.Equal(t, "github.com/ksonnet/parts/tree/master/incubator", r.URI())
						assert.Equal(t, "incubator", r.Name())
					} else {
						assert.Empty(t, registries)
					}

					return nil
				}

				a.initIncubatorFn = func() (registry.Registry, error) {
					r := &rmocks.Registry{}
					r.On("Protocol").Return("github")
					r.On("URI").Return("github.com/ksonnet/parts/tree/master/incubator")
					r.On("Name").Return("incubator")
					return r, nil
				}
bryanl's avatar
bryanl committed
95

96 97 98
				err = a.Run()
				require.NoError(t, err)
			})
99 100
		}

bryanl's avatar
bryanl committed
101 102
	})
}