client_test.go 3.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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.

16 17 18 19 20
package client

import (
	"testing"

21 22
	swagger "github.com/emicklei/go-restful-swagger12"
	"github.com/googleapis/gnostic/OpenAPIv2"
23
	"github.com/pkg/errors"
24
	"github.com/stretchr/testify/require"
25 26 27 28
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/apimachinery/pkg/version"
	"k8s.io/client-go/discovery"
29 30 31
	restclient "k8s.io/client-go/rest"
	"k8s.io/client-go/tools/clientcmd"
	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
32 33 34 35 36
)

func TestConfig_GetAPISpec(t *testing.T) {
	cases := []struct {
		name      string
37 38 39
		version   string
		disc      discovery.DiscoveryInterface
		createErr error
40 41
	}{
		{
42 43 44
			name:    "in general",
			version: "version:v1.9.3",
			disc:    &fakeDiscovery{},
45
		},
46
		{
47 48 49
			name:      "unable to create discovery client",
			version:   "version:v1.8.0",
			createErr: errors.New("failed"),
50 51
		},
		{
52 53 54
			name:    "retrieve open api schema error",
			version: "version:v1.8.0",
			disc:    &fakeDiscovery{withOpenAPISchemaError: true},
55
		},
56 57 58 59
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
60
			c := Config{
61 62 63
				Config: &clientConfig{},
				discoveryClient: func() (discovery.DiscoveryInterface, error) {
					return tc.disc, tc.createErr
64 65
				},
			}
66 67
			got := c.GetAPISpec()
			require.Equal(t, tc.version, got)
68 69
		})
	}
70 71 72 73 74 75 76 77 78 79 80 81 82

}

type clientConfig struct {
}

var _ clientcmd.ClientConfig = (*clientConfig)(nil)

func (c *clientConfig) RawConfig() (clientcmdapi.Config, error) {
	return clientcmdapi.Config{}, errors.Errorf("not implemented")
}

func (c *clientConfig) ClientConfig() (*restclient.Config, error) {
83
	rc := &restclient.Config{}
84 85 86 87 88 89 90 91 92 93

	return rc, nil
}

func (c *clientConfig) Namespace() (string, bool, error) {
	return "", false, errors.Errorf("not implemented")
}

func (c *clientConfig) ConfigAccess() clientcmd.ConfigAccess {
	var ca clientcmd.ConfigAccess
94

95
	return ca
96
}
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

type fakeDiscovery struct {
	withOpenAPISchemaError bool
}

var _ discovery.DiscoveryInterface = (*fakeDiscovery)(nil)

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

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

func (c *fakeDiscovery) ServerPreferredResources() ([]*metav1.APIResourceList, error) {
	return nil, nil
}

func (c *fakeDiscovery) ServerPreferredNamespacedResources() ([]*metav1.APIResourceList, error) {
	return nil, nil
}

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

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

func (c *fakeDiscovery) OpenAPISchema() (*openapi_v2.Document, error) {
	if c.withOpenAPISchemaError {
		return nil, errors.New("schema error")
	}

	return &openapi_v2.Document{
		Info: &openapi_v2.Info{
			Version: "v1.9.3",
		},
	}, nil
}

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

func (c *fakeDiscovery) RESTClient() restclient.Interface {
	return nil
}