simplify kubectl testing factory

This commit is contained in:
David Eads
2018-02-22 09:52:10 -05:00
parent 82eeda0885
commit 6047ead2e5
50 changed files with 508 additions and 781 deletions

View File

@@ -40,14 +40,13 @@ import (
"k8s.io/client-go/rest/fake"
"k8s.io/kubernetes/pkg/api/legacyscheme"
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/kubectl/categories"
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
"k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/kubectl/scheme"
)
func TestSetEnvLocal(t *testing.T) {
f, tf := cmdtesting.NewAPIFactory()
tf := cmdtesting.NewTestFactory()
ns := legacyscheme.Codecs
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: ""},
@@ -58,10 +57,10 @@ func TestSetEnvLocal(t *testing.T) {
}),
}
tf.Namespace = "test"
tf.ClientConfig = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Version: ""}}}
tf.ClientConfigVal = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Version: ""}}}
buf := bytes.NewBuffer([]byte{})
cmd := NewCmdEnv(f, os.Stdin, buf, buf)
cmd := NewCmdEnv(tf, os.Stdin, buf, buf)
cmd.SetOutput(buf)
cmd.Flags().Set("output", "name")
cmd.Flags().Set("local", "true")
@@ -70,10 +69,10 @@ func TestSetEnvLocal(t *testing.T) {
Filenames: []string{"../../../../examples/storage/cassandra/cassandra-controller.yaml"}},
Out: buf,
Local: true}
opts.Complete(f, cmd)
opts.Complete(tf, cmd)
err := opts.Validate([]string{"env=prod"})
if err == nil {
err = opts.RunEnv(f)
err = opts.RunEnv(tf)
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
@@ -84,7 +83,7 @@ func TestSetEnvLocal(t *testing.T) {
}
func TestSetMultiResourcesEnvLocal(t *testing.T) {
f, tf := cmdtesting.NewAPIFactory()
tf := cmdtesting.NewTestFactory()
ns := legacyscheme.Codecs
tf.Client = &fake.RESTClient{
@@ -96,10 +95,10 @@ func TestSetMultiResourcesEnvLocal(t *testing.T) {
}),
}
tf.Namespace = "test"
tf.ClientConfig = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Version: ""}}}
tf.ClientConfigVal = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Version: ""}}}
buf := bytes.NewBuffer([]byte{})
cmd := NewCmdEnv(f, os.Stdin, buf, buf)
cmd := NewCmdEnv(tf, os.Stdin, buf, buf)
cmd.SetOutput(buf)
cmd.Flags().Set("output", "name")
cmd.Flags().Set("local", "true")
@@ -108,10 +107,10 @@ func TestSetMultiResourcesEnvLocal(t *testing.T) {
Filenames: []string{"../../../../test/fixtures/pkg/kubectl/cmd/set/multi-resource-yaml.yaml"}},
Out: buf,
Local: true}
opts.Complete(f, cmd)
opts.Complete(tf, cmd)
err := opts.Validate([]string{"env=prod"})
if err == nil {
err = opts.RunEnv(f)
err = opts.RunEnv(tf)
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
@@ -498,11 +497,10 @@ func TestSetEnvRemote(t *testing.T) {
for _, input := range inputs {
groupVersion := schema.GroupVersion{Group: input.apiGroup, Version: input.apiVersion}
testapi.Default = testapi.Groups[input.testAPIGroup]
f, tf := cmdtesting.NewAPIFactory()
tf := cmdtesting.NewTestFactory()
codec := scheme.Codecs.CodecForVersions(scheme.Codecs.LegacyCodec(groupVersion), scheme.Codecs.UniversalDecoder(groupVersion), groupVersion, groupVersion)
ns := legacyscheme.Codecs
tf.Namespace = "test"
tf.CategoryExpander = categories.LegacyCategoryExpander
tf.Client = &fake.RESTClient{
GroupVersion: groupVersion,
NegotiatedSerializer: ns,
@@ -529,14 +527,14 @@ func TestSetEnvRemote(t *testing.T) {
}),
VersionedAPIPath: path.Join(input.apiPrefix, testapi.Default.GroupVersion().String()),
}
cmd := NewCmdEnv(f, out, out, out)
cmd := NewCmdEnv(tf, out, out, out)
cmd.SetOutput(out)
cmd.Flags().Set("output", "yaml")
opts := input.opts
opts.Complete(f, cmd)
opts.Complete(tf, cmd)
err := opts.Validate(input.args)
assert.NoError(t, err)
err = opts.RunEnv(f)
err = opts.RunEnv(tf)
assert.NoError(t, err)
}
// TODO This global state restoration needs fixing, b/c it's wrong. Tests should not modify global state