2015-09-11 20:47:53 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
|
|
|
|
|
|
|
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 unversioned_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-11-10 21:40:51 +00:00
|
|
|
"errors"
|
2015-09-11 20:47:53 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2015-10-20 15:45:46 +00:00
|
|
|
"strings"
|
2015-09-11 20:47:53 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/api/testapi"
|
2015-10-19 21:08:35 +00:00
|
|
|
unversionedapi "k8s.io/kubernetes/pkg/api/unversioned"
|
2015-09-11 20:47:53 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/unversioned"
|
|
|
|
"k8s.io/kubernetes/pkg/client/unversioned/fake"
|
|
|
|
)
|
|
|
|
|
|
|
|
func objBody(object interface{}) io.ReadCloser {
|
|
|
|
output, err := json.MarshalIndent(object, "", "")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return ioutil.NopCloser(bytes.NewReader([]byte(output)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNegotiateVersion(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name, version, expectedVersion string
|
|
|
|
serverVersions []string
|
|
|
|
clientVersions []string
|
|
|
|
config *unversioned.Config
|
2015-10-20 15:45:46 +00:00
|
|
|
expectErr func(err error) bool
|
|
|
|
sendErr error
|
2015-09-11 20:47:53 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "server supports client default",
|
|
|
|
version: "version1",
|
|
|
|
config: &unversioned.Config{},
|
|
|
|
serverVersions: []string{"version1", testapi.Default.Version()},
|
|
|
|
clientVersions: []string{"version1", testapi.Default.Version()},
|
|
|
|
expectedVersion: "version1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "server falls back to client supported",
|
|
|
|
version: testapi.Default.Version(),
|
|
|
|
config: &unversioned.Config{},
|
|
|
|
serverVersions: []string{"version1"},
|
|
|
|
clientVersions: []string{"version1", testapi.Default.Version()},
|
|
|
|
expectedVersion: "version1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "explicit version supported",
|
|
|
|
version: "",
|
|
|
|
config: &unversioned.Config{Version: testapi.Default.Version()},
|
|
|
|
serverVersions: []string{"version1", testapi.Default.Version()},
|
|
|
|
clientVersions: []string{"version1", testapi.Default.Version()},
|
|
|
|
expectedVersion: testapi.Default.Version(),
|
|
|
|
},
|
|
|
|
{
|
2015-10-20 15:45:46 +00:00
|
|
|
name: "explicit version not supported",
|
|
|
|
version: "",
|
|
|
|
config: &unversioned.Config{Version: testapi.Default.Version()},
|
|
|
|
serverVersions: []string{"version1"},
|
|
|
|
clientVersions: []string{"version1", testapi.Default.Version()},
|
|
|
|
expectErr: func(err error) bool { return strings.Contains(err.Error(), `server does not support API version "v1"`) },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "connection refused error",
|
|
|
|
config: &unversioned.Config{Version: testapi.Default.Version()},
|
|
|
|
serverVersions: []string{"version1"},
|
|
|
|
clientVersions: []string{"version1", testapi.Default.Version()},
|
2015-11-10 21:40:51 +00:00
|
|
|
sendErr: errors.New("connection refused"),
|
|
|
|
expectErr: func(err error) bool { return strings.Contains(err.Error(), "connection refused") },
|
2015-09-11 20:47:53 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
codec := testapi.Default.Codec()
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
fakeClient := &fake.RESTClient{
|
|
|
|
Codec: codec,
|
|
|
|
Resp: &http.Response{
|
|
|
|
StatusCode: 200,
|
2015-10-19 21:08:35 +00:00
|
|
|
Body: objBody(&unversionedapi.APIVersions{Versions: test.serverVersions}),
|
2015-09-11 20:47:53 +00:00
|
|
|
},
|
|
|
|
Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) {
|
2015-10-20 15:45:46 +00:00
|
|
|
if test.sendErr != nil {
|
|
|
|
return nil, test.sendErr
|
|
|
|
}
|
2015-10-19 21:08:35 +00:00
|
|
|
return &http.Response{StatusCode: 200, Body: objBody(&unversionedapi.APIVersions{Versions: test.serverVersions})}, nil
|
2015-09-11 20:47:53 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
c := unversioned.NewOrDie(test.config)
|
|
|
|
c.Client = fakeClient.Client
|
|
|
|
response, err := unversioned.NegotiateVersion(c, test.config, test.version, test.clientVersions)
|
2015-10-20 15:45:46 +00:00
|
|
|
if err == nil && test.expectErr != nil {
|
2015-09-11 20:47:53 +00:00
|
|
|
t.Errorf("expected error, got nil for [%s].", test.name)
|
|
|
|
}
|
2015-10-20 15:45:46 +00:00
|
|
|
if err != nil {
|
|
|
|
if test.expectErr == nil || !test.expectErr(err) {
|
|
|
|
t.Errorf("unexpected error for [%s]: %v.", test.name, err)
|
|
|
|
}
|
|
|
|
continue
|
2015-09-11 20:47:53 +00:00
|
|
|
}
|
|
|
|
if response != test.expectedVersion {
|
|
|
|
t.Errorf("expected version %s, got %s.", test.expectedVersion, response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|