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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
|
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"
|
|
|
|
. "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd"
|
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"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
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")
|
|
|
|
})
|
|
|
|
mapper.Add(scheme, false, "unlikelyversion")
|
|
|
|
|
|
|
|
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{
|
2014-12-31 23:35:52 +00:00
|
|
|
Object: func(*cobra.Command) (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
|
|
|
},
|
|
|
|
RESTClient: func(*cobra.Command, *meta.RESTMapping) (resource.RESTClient, error) {
|
2014-11-18 18:04:10 +00:00
|
|
|
return t.Client, t.Err
|
|
|
|
},
|
|
|
|
Describer: func(*cobra.Command, *meta.RESTMapping) (kubectl.Describer, error) {
|
|
|
|
return t.Describer, t.Err
|
|
|
|
},
|
|
|
|
Printer: func(cmd *cobra.Command, mapping *meta.RESTMapping, noHeaders bool) (kubectl.ResourcePrinter, error) {
|
|
|
|
return t.Printer, t.Err
|
|
|
|
},
|
2014-12-31 23:35:52 +00:00
|
|
|
Validator: func(cmd *cobra.Command) (validation.Schema, error) {
|
|
|
|
return t.Validator, t.Err
|
|
|
|
},
|
2015-01-02 18:08:37 +00:00
|
|
|
DefaultNamespace: func(cmd *cobra.Command) (string, error) {
|
|
|
|
return t.Namespace, t.Err
|
|
|
|
},
|
|
|
|
ClientConfig: func(cmd *cobra.Command) (*client.Config, error) {
|
|
|
|
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{
|
2014-12-31 23:35:52 +00:00
|
|
|
Object: func(*cobra.Command) (meta.RESTMapper, runtime.ObjectTyper) {
|
|
|
|
return latest.RESTMapper, api.Scheme
|
|
|
|
},
|
|
|
|
RESTClient: func(*cobra.Command, *meta.RESTMapping) (resource.RESTClient, error) {
|
2014-12-31 00:42:55 +00:00
|
|
|
return t.Client, t.Err
|
|
|
|
},
|
|
|
|
Describer: func(*cobra.Command, *meta.RESTMapping) (kubectl.Describer, error) {
|
|
|
|
return t.Describer, t.Err
|
|
|
|
},
|
|
|
|
Printer: func(cmd *cobra.Command, mapping *meta.RESTMapping, noHeaders bool) (kubectl.ResourcePrinter, error) {
|
|
|
|
return t.Printer, t.Err
|
|
|
|
},
|
2014-12-31 23:35:52 +00:00
|
|
|
Validator: func(cmd *cobra.Command) (validation.Schema, error) {
|
|
|
|
return t.Validator, t.Err
|
|
|
|
},
|
2015-01-02 18:08:37 +00:00
|
|
|
DefaultNamespace: func(cmd *cobra.Command) (string, error) {
|
|
|
|
return t.Namespace, t.Err
|
|
|
|
},
|
|
|
|
ClientConfig: func(cmd *cobra.Command) (*client.Config, error) {
|
|
|
|
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)))
|
|
|
|
}
|