validate_test.go 3.39 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
// 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"

	swagger "github.com/emicklei/go-restful-swagger12"
	"github.com/googleapis/gnostic/OpenAPIv2"
	"github.com/ksonnet/ksonnet/client"
	"github.com/ksonnet/ksonnet/metadata/app"
	amocks "github.com/ksonnet/ksonnet/metadata/app/mocks"
	"github.com/pkg/errors"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/apimachinery/pkg/version"
	"k8s.io/client-go/discovery"
	restclient "k8s.io/client-go/rest"
)

func TestValidate(t *testing.T) {
	withApp(t, func(appMock *amocks.App) {
		aEnvName := "default"
		aComponentNames := make([]string, 0)
		aModuleName := "module"
		aClientConfig := &client.Config{}

		env := &app.EnvironmentSpec{}
		appMock.On("Environment", aEnvName).Return(env, nil)

		a, err := NewValidate(appMock, aEnvName, aModuleName, aComponentNames, aClientConfig)
		require.NoError(t, err)

		a.discoveryFn = func(a app.App, clientConfig *client.Config, envName string) (discovery.DiscoveryInterface, error) {
			assert.Equal(t, aEnvName, envName)
			return &stubDiscovery{}, nil
		}

		objects := []*unstructured.Unstructured{
			{},
		}
		a.findObjectsFn = func(a app.App, envName string, componentNames []string) ([]*unstructured.Unstructured, error) {
			assert.Equal(t, "default", envName)
			assert.Equal(t, aComponentNames, componentNames)

			return objects, nil
		}

		a.validateObjectFn = func(d discovery.DiscoveryInterface, obj *unstructured.Unstructured) []error {
			return make([]error, 0)
		}

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

type stubDiscovery struct{}

func (d *stubDiscovery) RESTClient() restclient.Interface {
	return nil
}

func (d *stubDiscovery) ServerGroups() (*metav1.APIGroupList, error) {
	return nil, errors.New("not implemented")
}

func (d *stubDiscovery) ServerResourcesForGroupVersion(groupVersion string) (*metav1.APIResourceList, error) {
	return nil, errors.New("not implemented")
}

func (d *stubDiscovery) ServerResources() ([]*metav1.APIResourceList, error) {
	return nil, errors.New("not implemented")
}

func (d *stubDiscovery) ServerPreferredResources() ([]*metav1.APIResourceList, error) {
	return nil, errors.New("not implemented")
}

func (d *stubDiscovery) ServerPreferredNamespacedResources() ([]*metav1.APIResourceList, error) {
	return nil, errors.New("not implemented")
}

func (d *stubDiscovery) ServerVersion() (*version.Info, error) {
	return nil, errors.New("not implemented")
}

func (d *stubDiscovery) SwaggerSchema(version schema.GroupVersion) (*swagger.ApiDeclaration, error) {
	return nil, errors.New("not implemented")
}

func (d *stubDiscovery) OpenAPISchema() (*openapi_v2.Document, error) {
	return nil, errors.New("not implemented")
}