2014-11-18 18:04:10 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. 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.
|
|
|
|
*/
|
|
|
|
|
2015-03-26 01:52:12 +00:00
|
|
|
package cmd
|
2014-11-18 18:04:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2015-02-05 00:14:48 +00:00
|
|
|
"os"
|
2015-01-29 21:31:22 +00:00
|
|
|
"testing"
|
2014-11-18 18:04:10 +00:00
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
2014-11-18 18:04:10 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
|
2014-12-31 23:35:52 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation"
|
2015-01-02 18:08:37 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
2014-11-18 18:04:10 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
|
2014-12-31 23:35:52 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
|
2014-11-18 18:04:10 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
type internalType struct {
|
|
|
|
Kind string
|
|
|
|
APIVersion string
|
|
|
|
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
type externalType struct {
|
|
|
|
Kind string `json:"kind"`
|
|
|
|
APIVersion string `json:"apiVersion"`
|
|
|
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*internalType) IsAnAPIObject() {}
|
|
|
|
func (*externalType) IsAnAPIObject() {}
|
|
|
|
|
|
|
|
func newExternalScheme() (*runtime.Scheme, meta.RESTMapper, runtime.Codec) {
|
|
|
|
scheme := runtime.NewScheme()
|
|
|
|
scheme.AddKnownTypeWithName("", "Type", &internalType{})
|
|
|
|
scheme.AddKnownTypeWithName("unlikelyversion", "Type", &externalType{})
|
|
|
|
|
|
|
|
codec := runtime.CodecFor(scheme, "unlikelyversion")
|
|
|
|
mapper := meta.NewDefaultRESTMapper([]string{"unlikelyversion"}, func(version string) (*meta.VersionInterfaces, bool) {
|
|
|
|
return &meta.VersionInterfaces{
|
|
|
|
Codec: codec,
|
|
|
|
ObjectConvertor: scheme,
|
|
|
|
MetadataAccessor: meta.NewAccessor(),
|
|
|
|
}, (version == "unlikelyversion")
|
|
|
|
})
|
2015-01-29 22:46:54 +00:00
|
|
|
for _, version := range []string{"unlikelyversion"} {
|
|
|
|
for kind := range scheme.KnownTypes(version) {
|
|
|
|
mixedCase := false
|
|
|
|
scope := meta.RESTScopeNamespace
|
|
|
|
mapper.Add(scope, kind, version, mixedCase)
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 18:04:10 +00:00
|
|
|
|
|
|
|
return scheme, mapper, codec
|
|
|
|
}
|
|
|
|
|
|
|
|
type testPrinter struct {
|
2014-12-31 00:42:55 +00:00
|
|
|
Objects []runtime.Object
|
|
|
|
Err error
|
2014-11-18 18:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testPrinter) PrintObj(obj runtime.Object, out io.Writer) error {
|
2014-12-31 00:42:55 +00:00
|
|
|
t.Objects = append(t.Objects, obj)
|
2014-11-18 18:04:10 +00:00
|
|
|
fmt.Fprintf(out, "%#v", obj)
|
|
|
|
return t.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
type testDescriber struct {
|
|
|
|
Name, Namespace string
|
|
|
|
Output string
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testDescriber) Describe(namespace, name string) (output string, err error) {
|
|
|
|
t.Namespace, t.Name = namespace, name
|
|
|
|
return t.Output, t.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
type testFactory struct {
|
2015-01-02 18:08:37 +00:00
|
|
|
Mapper meta.RESTMapper
|
|
|
|
Typer runtime.ObjectTyper
|
|
|
|
Client kubectl.RESTClient
|
|
|
|
Describer kubectl.Describer
|
|
|
|
Printer kubectl.ResourcePrinter
|
|
|
|
Validator validation.Schema
|
|
|
|
Namespace string
|
|
|
|
ClientConfig *client.Config
|
|
|
|
Err error
|
2014-11-18 18:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTestFactory() (*Factory, *testFactory, runtime.Codec) {
|
|
|
|
scheme, mapper, codec := newExternalScheme()
|
2014-12-31 23:35:52 +00:00
|
|
|
t := &testFactory{
|
|
|
|
Validator: validation.NullSchema{},
|
2015-01-03 20:38:33 +00:00
|
|
|
Mapper: mapper,
|
|
|
|
Typer: scheme,
|
2014-12-31 23:35:52 +00:00
|
|
|
}
|
2014-11-18 18:04:10 +00:00
|
|
|
return &Factory{
|
2015-03-14 10:45:18 +00:00
|
|
|
Object: func() (meta.RESTMapper, runtime.ObjectTyper) {
|
2015-01-03 20:38:33 +00:00
|
|
|
return t.Mapper, t.Typer
|
2014-12-31 23:35:52 +00:00
|
|
|
},
|
2015-03-14 10:45:18 +00:00
|
|
|
RESTClient: func(*meta.RESTMapping) (resource.RESTClient, error) {
|
2014-11-18 18:04:10 +00:00
|
|
|
return t.Client, t.Err
|
|
|
|
},
|
2015-03-14 10:45:18 +00:00
|
|
|
Describer: func(*meta.RESTMapping) (kubectl.Describer, error) {
|
2014-11-18 18:04:10 +00:00
|
|
|
return t.Describer, t.Err
|
|
|
|
},
|
2015-03-14 10:45:18 +00:00
|
|
|
Printer: func(mapping *meta.RESTMapping, noHeaders bool) (kubectl.ResourcePrinter, error) {
|
2014-11-18 18:04:10 +00:00
|
|
|
return t.Printer, t.Err
|
|
|
|
},
|
2015-03-14 10:45:18 +00:00
|
|
|
Validator: func() (validation.Schema, error) {
|
2014-12-31 23:35:52 +00:00
|
|
|
return t.Validator, t.Err
|
|
|
|
},
|
2015-03-14 10:45:18 +00:00
|
|
|
DefaultNamespace: func() (string, error) {
|
2015-01-02 18:08:37 +00:00
|
|
|
return t.Namespace, t.Err
|
|
|
|
},
|
2015-03-14 10:45:18 +00:00
|
|
|
ClientConfig: func() (*client.Config, error) {
|
2015-01-02 18:08:37 +00:00
|
|
|
return t.ClientConfig, t.Err
|
|
|
|
},
|
2014-11-18 18:04:10 +00:00
|
|
|
}, t, codec
|
|
|
|
}
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
func NewAPIFactory() (*Factory, *testFactory, runtime.Codec) {
|
2014-12-31 23:35:52 +00:00
|
|
|
t := &testFactory{
|
|
|
|
Validator: validation.NullSchema{},
|
|
|
|
}
|
2014-12-31 00:42:55 +00:00
|
|
|
return &Factory{
|
2015-03-14 10:45:18 +00:00
|
|
|
Object: func() (meta.RESTMapper, runtime.ObjectTyper) {
|
2014-12-31 23:35:52 +00:00
|
|
|
return latest.RESTMapper, api.Scheme
|
|
|
|
},
|
2015-03-14 10:45:18 +00:00
|
|
|
RESTClient: func(*meta.RESTMapping) (resource.RESTClient, error) {
|
2014-12-31 00:42:55 +00:00
|
|
|
return t.Client, t.Err
|
|
|
|
},
|
2015-03-14 10:45:18 +00:00
|
|
|
Describer: func(*meta.RESTMapping) (kubectl.Describer, error) {
|
2014-12-31 00:42:55 +00:00
|
|
|
return t.Describer, t.Err
|
|
|
|
},
|
2015-03-14 10:45:18 +00:00
|
|
|
Printer: func(mapping *meta.RESTMapping, noHeaders bool) (kubectl.ResourcePrinter, error) {
|
2014-12-31 00:42:55 +00:00
|
|
|
return t.Printer, t.Err
|
|
|
|
},
|
2015-03-14 10:45:18 +00:00
|
|
|
Validator: func() (validation.Schema, error) {
|
2014-12-31 23:35:52 +00:00
|
|
|
return t.Validator, t.Err
|
|
|
|
},
|
2015-03-14 10:45:18 +00:00
|
|
|
DefaultNamespace: func() (string, error) {
|
2015-01-02 18:08:37 +00:00
|
|
|
return t.Namespace, t.Err
|
|
|
|
},
|
2015-03-14 10:45:18 +00:00
|
|
|
ClientConfig: func() (*client.Config, error) {
|
2015-01-02 18:08:37 +00:00
|
|
|
return t.ClientConfig, t.Err
|
|
|
|
},
|
2014-12-31 00:42:55 +00:00
|
|
|
}, t, latest.Codec
|
|
|
|
}
|
|
|
|
|
2014-11-18 18:04:10 +00:00
|
|
|
func objBody(codec runtime.Codec, obj runtime.Object) io.ReadCloser {
|
|
|
|
return ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(codec, obj))))
|
|
|
|
}
|
2015-01-03 23:02:39 +00:00
|
|
|
|
|
|
|
func stringBody(body string) io.ReadCloser {
|
|
|
|
return ioutil.NopCloser(bytes.NewReader([]byte(body)))
|
|
|
|
}
|
2015-01-29 21:31:22 +00:00
|
|
|
|
|
|
|
// Verify that resource.RESTClients constructed from a factory respect mapping.APIVersion
|
|
|
|
func TestClientVersions(t *testing.T) {
|
|
|
|
f := NewFactory(nil)
|
|
|
|
|
|
|
|
versions := []string{
|
|
|
|
"v1beta1",
|
|
|
|
"v1beta2",
|
|
|
|
"v1beta3",
|
|
|
|
}
|
|
|
|
for _, version := range versions {
|
|
|
|
mapping := &meta.RESTMapping{
|
|
|
|
APIVersion: version,
|
|
|
|
}
|
2015-03-14 10:45:18 +00:00
|
|
|
c, err := f.RESTClient(mapping)
|
2015-01-29 21:31:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
client := c.(*client.RESTClient)
|
|
|
|
if client.APIVersion() != version {
|
|
|
|
t.Errorf("unexpected Client APIVersion: %s %v", client.APIVersion, client)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-05 00:14:48 +00:00
|
|
|
|
|
|
|
func ExamplePrintReplicationController() {
|
|
|
|
f, tf, codec := NewAPIFactory()
|
|
|
|
tf.Printer = kubectl.NewHumanReadablePrinter(false)
|
|
|
|
tf.Client = &client.FakeRESTClient{
|
|
|
|
Codec: codec,
|
|
|
|
Client: nil,
|
|
|
|
}
|
|
|
|
cmd := f.NewCmdRunContainer(os.Stdout)
|
|
|
|
ctrl := &api.ReplicationController{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"foo": "bar"},
|
|
|
|
},
|
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Replicas: 1,
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
|
|
|
Template: &api.PodTemplateSpec{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Labels: map[string]string{"foo": "bar"},
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Image: "someimage",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := f.PrintObject(cmd, ctrl, os.Stdout)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
// Output:
|
2015-03-27 23:50:29 +00:00
|
|
|
// CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS
|
|
|
|
// foo foo someimage foo=bar 1
|
2015-02-05 00:14:48 +00:00
|
|
|
}
|