init_test.go 2.12 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 49 50 51 52 53 54 55 56 57 58

		a, err := NewInit(aFs, aName, aRootPath, aK8sSpecFlag, aServerURI, aNamespace)
		require.NoError(t, err)

		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)

			assert.Len(t, registries, 1)
			r := registries[0]

			assert.Equal(t, "github", r.Protocol())
			assert.Equal(t, "github.com/ksonnet/parts/tree/master/incubator", r.URI())
			assert.Equal(t, "incubator", r.Name())

			return nil
		}

59 60 61 62 63 64 65 66
		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
67 68 69 70
		err = a.Run()
		require.NoError(t, err)
	})
}