Support Node/NodeList as a valid name in all versions

Also refactor v1beta1/conversion_test.go `v1beta` -> `current` to allow
easier cut and paste of tests.
This commit is contained in:
Clayton Coleman 2014-12-07 22:31:46 -05:00
parent 5447e74129
commit 650aead4c4
7 changed files with 133 additions and 28 deletions

View File

@ -46,6 +46,9 @@ func init() {
&BoundPod{}, &BoundPod{},
&BoundPods{}, &BoundPods{},
) )
// Legacy names are supported
Scheme.AddKnownTypeWithName("", "Node", &Minion{})
Scheme.AddKnownTypeWithName("", "NodeList", &MinionList{})
} }
func (*Pod) IsAnAPIObject() {} func (*Pod) IsAnAPIObject() {}

View File

@ -21,13 +21,36 @@ import (
"testing" "testing"
newer "github.com/GoogleCloudPlatform/kubernetes/pkg/api" newer "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1" current "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
) )
var Convert = newer.Scheme.Convert var Convert = newer.Scheme.Convert
func TestNodeConversion(t *testing.T) {
obj, err := current.Codec.Decode([]byte(`{"kind":"Node","apiVersion":"v1beta1"}`))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if _, ok := obj.(*newer.Minion); !ok {
t.Errorf("unexpected type: %#v", obj)
}
obj, err = current.Codec.Decode([]byte(`{"kind":"NodeList","apiVersion":"v1beta1"}`))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if _, ok := obj.(*newer.MinionList); !ok {
t.Errorf("unexpected type: %#v", obj)
}
obj = &newer.Node{}
if err := current.Codec.DecodeInto([]byte(`{"kind":"Node","apiVersion":"v1beta1"}`), obj); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestEnvConversion(t *testing.T) { func TestEnvConversion(t *testing.T) {
nonCanonical := []v1beta1.EnvVar{ nonCanonical := []current.EnvVar{
{Key: "EV"}, {Key: "EV"},
{Key: "EV", Name: "EX"}, {Key: "EV", Name: "EX"},
} }
@ -48,7 +71,7 @@ func TestEnvConversion(t *testing.T) {
// Test conversion the other way, too. // Test conversion the other way, too.
for i := range canonical { for i := range canonical {
var got v1beta1.EnvVar var got current.EnvVar
err := Convert(&canonical[i], &got) err := Convert(&canonical[i], &got)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
@ -65,15 +88,15 @@ func TestEnvConversion(t *testing.T) {
func TestVolumeMountConversionToOld(t *testing.T) { func TestVolumeMountConversionToOld(t *testing.T) {
table := []struct { table := []struct {
in newer.VolumeMount in newer.VolumeMount
out v1beta1.VolumeMount out current.VolumeMount
}{ }{
{ {
in: newer.VolumeMount{Name: "foo", MountPath: "/dev/foo", ReadOnly: true}, in: newer.VolumeMount{Name: "foo", MountPath: "/dev/foo", ReadOnly: true},
out: v1beta1.VolumeMount{Name: "foo", MountPath: "/dev/foo", Path: "/dev/foo", ReadOnly: true}, out: current.VolumeMount{Name: "foo", MountPath: "/dev/foo", Path: "/dev/foo", ReadOnly: true},
}, },
} }
for _, item := range table { for _, item := range table {
got := v1beta1.VolumeMount{} got := current.VolumeMount{}
err := Convert(&item.in, &got) err := Convert(&item.in, &got)
if err != nil { if err != nil {
t.Errorf("Unexpected error: %v", err) t.Errorf("Unexpected error: %v", err)
@ -87,17 +110,17 @@ func TestVolumeMountConversionToOld(t *testing.T) {
func TestVolumeMountConversionToNew(t *testing.T) { func TestVolumeMountConversionToNew(t *testing.T) {
table := []struct { table := []struct {
in v1beta1.VolumeMount in current.VolumeMount
out newer.VolumeMount out newer.VolumeMount
}{ }{
{ {
in: v1beta1.VolumeMount{Name: "foo", MountPath: "/dev/foo", ReadOnly: true}, in: current.VolumeMount{Name: "foo", MountPath: "/dev/foo", ReadOnly: true},
out: newer.VolumeMount{Name: "foo", MountPath: "/dev/foo", ReadOnly: true}, out: newer.VolumeMount{Name: "foo", MountPath: "/dev/foo", ReadOnly: true},
}, { }, {
in: v1beta1.VolumeMount{Name: "foo", MountPath: "/dev/foo", Path: "/dev/bar", ReadOnly: true}, in: current.VolumeMount{Name: "foo", MountPath: "/dev/foo", Path: "/dev/bar", ReadOnly: true},
out: newer.VolumeMount{Name: "foo", MountPath: "/dev/foo", ReadOnly: true}, out: newer.VolumeMount{Name: "foo", MountPath: "/dev/foo", ReadOnly: true},
}, { }, {
in: v1beta1.VolumeMount{Name: "foo", Path: "/dev/bar", ReadOnly: true}, in: current.VolumeMount{Name: "foo", Path: "/dev/bar", ReadOnly: true},
out: newer.VolumeMount{Name: "foo", MountPath: "/dev/bar", ReadOnly: true}, out: newer.VolumeMount{Name: "foo", MountPath: "/dev/bar", ReadOnly: true},
}, },
} }
@ -115,13 +138,13 @@ func TestVolumeMountConversionToNew(t *testing.T) {
} }
func TestMinionListConversionToNew(t *testing.T) { func TestMinionListConversionToNew(t *testing.T) {
oldMinion := func(id string) v1beta1.Minion { oldMinion := func(id string) current.Minion {
return v1beta1.Minion{TypeMeta: v1beta1.TypeMeta{ID: id}} return current.Minion{TypeMeta: current.TypeMeta{ID: id}}
} }
newMinion := func(id string) newer.Minion { newMinion := func(id string) newer.Minion {
return newer.Minion{ObjectMeta: newer.ObjectMeta{Name: id}} return newer.Minion{ObjectMeta: newer.ObjectMeta{Name: id}}
} }
oldMinions := []v1beta1.Minion{ oldMinions := []current.Minion{
oldMinion("foo"), oldMinion("foo"),
oldMinion("bar"), oldMinion("bar"),
} }
@ -131,19 +154,19 @@ func TestMinionListConversionToNew(t *testing.T) {
} }
table := []struct { table := []struct {
oldML *v1beta1.MinionList oldML *current.MinionList
newML *newer.MinionList newML *newer.MinionList
}{ }{
{ {
oldML: &v1beta1.MinionList{Items: oldMinions}, oldML: &current.MinionList{Items: oldMinions},
newML: &newer.MinionList{Items: newMinions}, newML: &newer.MinionList{Items: newMinions},
}, { }, {
oldML: &v1beta1.MinionList{Minions: oldMinions}, oldML: &current.MinionList{Minions: oldMinions},
newML: &newer.MinionList{Items: newMinions}, newML: &newer.MinionList{Items: newMinions},
}, { }, {
oldML: &v1beta1.MinionList{ oldML: &current.MinionList{
Items: oldMinions, Items: oldMinions,
Minions: []v1beta1.Minion{oldMinion("baz")}, Minions: []current.Minion{oldMinion("baz")},
}, },
newML: &newer.MinionList{Items: newMinions}, newML: &newer.MinionList{Items: newMinions},
}, },
@ -162,13 +185,13 @@ func TestMinionListConversionToNew(t *testing.T) {
} }
func TestMinionListConversionToOld(t *testing.T) { func TestMinionListConversionToOld(t *testing.T) {
oldMinion := func(id string) v1beta1.Minion { oldMinion := func(id string) current.Minion {
return v1beta1.Minion{TypeMeta: v1beta1.TypeMeta{ID: id}} return current.Minion{TypeMeta: current.TypeMeta{ID: id}}
} }
newMinion := func(id string) newer.Minion { newMinion := func(id string) newer.Minion {
return newer.Minion{ObjectMeta: newer.ObjectMeta{Name: id}} return newer.Minion{ObjectMeta: newer.ObjectMeta{Name: id}}
} }
oldMinions := []v1beta1.Minion{ oldMinions := []current.Minion{
oldMinion("foo"), oldMinion("foo"),
oldMinion("bar"), oldMinion("bar"),
} }
@ -178,12 +201,12 @@ func TestMinionListConversionToOld(t *testing.T) {
} }
newML := &newer.MinionList{Items: newMinions} newML := &newer.MinionList{Items: newMinions}
oldML := &v1beta1.MinionList{ oldML := &current.MinionList{
Items: oldMinions, Items: oldMinions,
Minions: oldMinions, Minions: oldMinions,
} }
got := &v1beta1.MinionList{} got := &current.MinionList{}
err := Convert(newML, got) err := Convert(newML, got)
if err != nil { if err != nil {
t.Errorf("Unexpected error: %v", err) t.Errorf("Unexpected error: %v", err)
@ -195,7 +218,7 @@ func TestMinionListConversionToOld(t *testing.T) {
func TestServiceEmptySelector(t *testing.T) { func TestServiceEmptySelector(t *testing.T) {
// Nil map should be preserved // Nil map should be preserved
svc := &v1beta1.Service{Selector: nil} svc := &current.Service{Selector: nil}
data, err := newer.Scheme.EncodeToVersion(svc, "v1beta1") data, err := newer.Scheme.EncodeToVersion(svc, "v1beta1")
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
@ -210,7 +233,7 @@ func TestServiceEmptySelector(t *testing.T) {
} }
// Empty map should be preserved // Empty map should be preserved
svc2 := &v1beta1.Service{Selector: map[string]string{}} svc2 := &current.Service{Selector: map[string]string{}}
data, err = newer.Scheme.EncodeToVersion(svc2, "v1beta1") data, err = newer.Scheme.EncodeToVersion(svc2, "v1beta1")
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)

View File

@ -51,6 +51,9 @@ func init() {
&BoundPod{}, &BoundPod{},
&BoundPods{}, &BoundPods{},
) )
// Future names are supported
api.Scheme.AddKnownTypeWithName("v1beta1", "Node", &Minion{})
api.Scheme.AddKnownTypeWithName("v1beta1", "NodeList", &MinionList{})
} }
func (*Pod) IsAnAPIObject() {} func (*Pod) IsAnAPIObject() {}

View File

@ -20,12 +20,12 @@ import (
"testing" "testing"
newer "github.com/GoogleCloudPlatform/kubernetes/pkg/api" newer "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta2" current "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
) )
func TestServiceEmptySelector(t *testing.T) { func TestServiceEmptySelector(t *testing.T) {
// Nil map should be preserved // Nil map should be preserved
svc := &v1beta2.Service{Selector: nil} svc := &current.Service{Selector: nil}
data, err := newer.Scheme.EncodeToVersion(svc, "v1beta2") data, err := newer.Scheme.EncodeToVersion(svc, "v1beta2")
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
@ -40,7 +40,7 @@ func TestServiceEmptySelector(t *testing.T) {
} }
// Empty map should be preserved // Empty map should be preserved
svc2 := &v1beta2.Service{Selector: map[string]string{}} svc2 := &current.Service{Selector: map[string]string{}}
data, err = newer.Scheme.EncodeToVersion(svc2, "v1beta2") data, err = newer.Scheme.EncodeToVersion(svc2, "v1beta2")
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
@ -54,3 +54,26 @@ func TestServiceEmptySelector(t *testing.T) {
t.Errorf("unexpected selector: %#v", obj) t.Errorf("unexpected selector: %#v", obj)
} }
} }
func TestNodeConversion(t *testing.T) {
obj, err := current.Codec.Decode([]byte(`{"kind":"Node","apiVersion":"v1beta2"}`))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if _, ok := obj.(*newer.Minion); !ok {
t.Errorf("unexpected type: %#v", obj)
}
obj, err = current.Codec.Decode([]byte(`{"kind":"NodeList","apiVersion":"v1beta2"}`))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if _, ok := obj.(*newer.MinionList); !ok {
t.Errorf("unexpected type: %#v", obj)
}
obj = &newer.Node{}
if err := current.Codec.DecodeInto([]byte(`{"kind":"Node","apiVersion":"v1beta2"}`), obj); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}

View File

@ -51,6 +51,9 @@ func init() {
&BoundPod{}, &BoundPod{},
&BoundPods{}, &BoundPods{},
) )
// Future names are supported
api.Scheme.AddKnownTypeWithName("v1beta2", "Node", &Minion{})
api.Scheme.AddKnownTypeWithName("v1beta2", "NodeList", &MinionList{})
} }
func (*Pod) IsAnAPIObject() {} func (*Pod) IsAnAPIObject() {}

View File

@ -0,0 +1,47 @@
/*
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 v1beta3_test
import (
"testing"
newer "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
current "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta3"
)
func TestNodeConversion(t *testing.T) {
obj, err := current.Codec.Decode([]byte(`{"kind":"Minion","apiVersion":"v1beta3"}`))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if _, ok := obj.(*newer.Minion); !ok {
t.Errorf("unexpected type: %#v", obj)
}
obj, err = current.Codec.Decode([]byte(`{"kind":"MinionList","apiVersion":"v1beta3"}`))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if _, ok := obj.(*newer.MinionList); !ok {
t.Errorf("unexpected type: %#v", obj)
}
obj = &newer.Node{}
if err := current.Codec.DecodeInto([]byte(`{"kind":"Minion","apiVersion":"v1beta3"}`), obj); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}

View File

@ -47,6 +47,9 @@ func init() {
&Event{}, &Event{},
&EventList{}, &EventList{},
) )
// Legacy names are supported
api.Scheme.AddKnownTypeWithName("v1beta3", "Minion", &Node{})
api.Scheme.AddKnownTypeWithName("v1beta3", "MinionList", &NodeList{})
} }
func (*Pod) IsAnAPIObject() {} func (*Pod) IsAnAPIObject() {}