2014-09-30 00:15:00 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-09-30 00:15:00 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2015-08-12 17:35:07 +00:00
|
|
|
package unversioned
|
2014-09-30 00:15:00 +00:00
|
|
|
|
|
|
|
import (
|
2015-10-01 21:56:22 +00:00
|
|
|
"encoding/json"
|
2014-09-30 00:15:00 +00:00
|
|
|
"net/http"
|
2015-10-01 21:56:22 +00:00
|
|
|
"net/http/httptest"
|
2015-02-04 21:35:53 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2014-09-30 00:15:00 +00:00
|
|
|
"testing"
|
2015-02-04 21:35:53 +00:00
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/testapi"
|
2015-10-12 20:48:14 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2014-09-30 00:15:00 +00:00
|
|
|
)
|
|
|
|
|
2014-11-14 05:42:36 +00:00
|
|
|
func TestIsConfigTransportTLS(t *testing.T) {
|
2014-09-30 00:15:00 +00:00
|
|
|
testCases := []struct {
|
2014-11-14 05:42:36 +00:00
|
|
|
Config *Config
|
|
|
|
TransportTLS bool
|
2014-09-30 00:15:00 +00:00
|
|
|
}{
|
|
|
|
{
|
2014-11-14 05:42:36 +00:00
|
|
|
Config: &Config{},
|
|
|
|
TransportTLS: false,
|
2014-09-30 00:15:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Config: &Config{
|
|
|
|
Host: "https://localhost",
|
|
|
|
},
|
2014-11-14 05:42:36 +00:00
|
|
|
TransportTLS: true,
|
2014-09-30 00:15:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Config: &Config{
|
2015-01-29 22:43:09 +00:00
|
|
|
Host: "localhost",
|
|
|
|
TLSClientConfig: TLSClientConfig{
|
|
|
|
CertFile: "foo",
|
|
|
|
},
|
2014-09-30 00:15:00 +00:00
|
|
|
},
|
2014-11-14 05:42:36 +00:00
|
|
|
TransportTLS: true,
|
2014-09-30 00:15:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Config: &Config{
|
2015-01-29 22:43:09 +00:00
|
|
|
Host: "///:://localhost",
|
|
|
|
TLSClientConfig: TLSClientConfig{
|
|
|
|
CertFile: "foo",
|
|
|
|
},
|
2014-09-30 00:15:00 +00:00
|
|
|
},
|
2014-11-14 05:42:36 +00:00
|
|
|
TransportTLS: false,
|
2014-09-30 00:15:00 +00:00
|
|
|
},
|
2014-11-13 07:54:54 +00:00
|
|
|
{
|
|
|
|
Config: &Config{
|
|
|
|
Host: "1.2.3.4:567",
|
|
|
|
Insecure: true,
|
|
|
|
},
|
2014-11-14 05:42:36 +00:00
|
|
|
TransportTLS: true,
|
2014-11-13 07:54:54 +00:00
|
|
|
},
|
2014-09-30 00:15:00 +00:00
|
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
2015-01-06 17:36:08 +00:00
|
|
|
if err := SetKubernetesDefaults(testCase.Config); err != nil {
|
|
|
|
t.Errorf("setting defaults failed for %#v: %v", testCase.Config, err)
|
|
|
|
continue
|
|
|
|
}
|
2015-01-08 15:22:09 +00:00
|
|
|
useTLS := IsConfigTransportTLS(*testCase.Config)
|
2014-11-14 05:42:36 +00:00
|
|
|
if testCase.TransportTLS != useTLS {
|
|
|
|
t.Errorf("expected %v for %#v", testCase.TransportTLS, testCase.Config)
|
2014-09-30 00:15:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-04 21:35:53 +00:00
|
|
|
|
|
|
|
func TestSetKubernetesDefaults(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
Config Config
|
|
|
|
After Config
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Config{},
|
|
|
|
Config{
|
2015-11-13 21:20:54 +00:00
|
|
|
Prefix: "/api",
|
|
|
|
GroupVersion: testapi.Default.GroupVersion(),
|
|
|
|
Codec: testapi.Default.Codec(),
|
|
|
|
QPS: 5,
|
|
|
|
Burst: 10,
|
2015-02-04 21:35:53 +00:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Config{
|
2015-11-13 21:20:54 +00:00
|
|
|
GroupVersion: &unversioned.GroupVersion{Group: "not.a.group", Version: "not_an_api"},
|
2015-02-04 21:35:53 +00:00
|
|
|
},
|
|
|
|
Config{},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
val := &testCase.Config
|
|
|
|
err := SetKubernetesDefaults(val)
|
|
|
|
val.UserAgent = ""
|
|
|
|
switch {
|
|
|
|
case err == nil && testCase.Err:
|
|
|
|
t.Errorf("expected error but was nil")
|
|
|
|
continue
|
|
|
|
case err != nil && !testCase.Err:
|
|
|
|
t.Errorf("unexpected error %v", err)
|
|
|
|
continue
|
|
|
|
case err != nil:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(*val, testCase.After) {
|
|
|
|
t.Errorf("unexpected result object: %#v", val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetKubernetesDefaultsUserAgent(t *testing.T) {
|
|
|
|
config := &Config{}
|
|
|
|
if err := SetKubernetesDefaults(config); err != nil {
|
2015-03-31 22:32:02 +00:00
|
|
|
t.Errorf("unexpected error: %v", err)
|
2015-02-04 21:35:53 +00:00
|
|
|
}
|
|
|
|
if !strings.Contains(config.UserAgent, "kubernetes/") {
|
|
|
|
t.Errorf("no user agent set: %#v", config)
|
|
|
|
}
|
|
|
|
}
|
2015-10-01 21:56:22 +00:00
|
|
|
|
|
|
|
func TestHelperGetServerAPIVersions(t *testing.T) {
|
|
|
|
expect := []string{"v1", "v2", "v3"}
|
2015-10-12 20:48:14 +00:00
|
|
|
APIVersions := unversioned.APIVersions{Versions: expect}
|
2015-10-01 21:56:22 +00:00
|
|
|
expect = append(expect, "group1/v1", "group1/v2", "group2/v1", "group2/v2")
|
2015-10-12 20:48:14 +00:00
|
|
|
APIGroupList := unversioned.APIGroupList{
|
|
|
|
Groups: []unversioned.APIGroup{
|
2015-10-01 21:56:22 +00:00
|
|
|
{
|
2015-11-05 22:52:58 +00:00
|
|
|
Versions: []unversioned.GroupVersionForDiscovery{
|
2015-10-01 21:56:22 +00:00
|
|
|
{
|
|
|
|
GroupVersion: "group1/v1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
GroupVersion: "group1/v2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-11-05 22:52:58 +00:00
|
|
|
Versions: []unversioned.GroupVersionForDiscovery{
|
2015-10-01 21:56:22 +00:00
|
|
|
{
|
|
|
|
GroupVersion: "group2/v1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
GroupVersion: "group2/v2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
var output []byte
|
|
|
|
var err error
|
|
|
|
switch req.URL.Path {
|
|
|
|
case "/api":
|
|
|
|
output, err = json.Marshal(APIVersions)
|
|
|
|
|
|
|
|
case "/apis":
|
|
|
|
output, err = json.Marshal(APIGroupList)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected encoding error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write(output)
|
|
|
|
}))
|
2015-11-13 21:20:54 +00:00
|
|
|
got, err := ServerAPIVersions(&Config{Host: server.URL, GroupVersion: &unversioned.GroupVersion{Group: "invalid version", Version: "one"}, Codec: testapi.Default.Codec()})
|
2015-10-01 21:56:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected encoding error: %v", err)
|
|
|
|
}
|
|
|
|
if e, a := expect, got; !reflect.DeepEqual(e, a) {
|
|
|
|
t.Errorf("expected %v, got %v", e, a)
|
|
|
|
}
|
|
|
|
}
|