Merge pull request #9225 from caesarxuchao/add-v1-tests-general

Add v1 tests
This commit is contained in:
Brian Grant 2015-06-03 22:18:21 -07:00
commit 656fa7bea1
19 changed files with 78 additions and 254 deletions

View File

@ -22,7 +22,6 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
_ "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta3"
) )
func BenchmarkPodConversion(b *testing.B) { func BenchmarkPodConversion(b *testing.B) {

View File

@ -46,9 +46,9 @@ func TestResourcePathWithPrefixForV1Beta3(t *testing.T) {
} }
} }
func TestResourcePathWithPrefixForV1Beta1(t *testing.T) { func TestResourcePathWithPrefixForV1(t *testing.T) {
if Version() != "v1beta1" { if Version() != "v1" {
// Skip the test if we are not testing v1beta1. // Skip the test if we are not testing v1.
return return
} }
@ -59,11 +59,11 @@ func TestResourcePathWithPrefixForV1Beta1(t *testing.T) {
name string name string
expected string expected string
}{ }{
{"prefix", "resource", "mynamespace", "myresource", "/api/v1beta1/prefix/resource/myresource"}, {"prefix", "resource", "mynamespace", "myresource", "/api/v1/prefix/namespaces/mynamespace/resource/myresource"},
{"prefix", "resource", "", "myresource", "/api/v1beta1/prefix/resource/myresource"}, {"prefix", "resource", "", "myresource", "/api/v1/prefix/resource/myresource"},
{"prefix", "resource", "mynamespace", "", "/api/v1beta1/prefix/resource"}, {"prefix", "resource", "mynamespace", "", "/api/v1/prefix/namespaces/mynamespace/resource"},
{"prefix", "resource", "", "", "/api/v1beta1/prefix/resource"}, {"prefix", "resource", "", "", "/api/v1/prefix/resource"},
{"", "resource", "mynamespace", "myresource", "/api/v1beta1/resource/myresource"}, {"", "resource", "mynamespace", "myresource", "/api/v1/namespaces/mynamespace/resource/myresource"},
} }
for _, item := range testCases { for _, item := range testCases {
if actual := ResourcePathWithPrefix(item.prefix, item.resource, item.namespace, item.name); actual != item.expected { if actual := ResourcePathWithPrefix(item.prefix, item.resource, item.namespace, item.name); actual != item.expected {
@ -96,9 +96,9 @@ func TestResourcePathForV1Beta3(t *testing.T) {
} }
} }
func TestResourcePathForV1Beta1(t *testing.T) { func TestResourcePathForV1(t *testing.T) {
if Version() != "v1beta1" { if Version() != "v1" {
// Skip the test if we are not testing v1beta1. // Skip the test if we are not testing v1.
return return
} }
@ -108,10 +108,10 @@ func TestResourcePathForV1Beta1(t *testing.T) {
name string name string
expected string expected string
}{ }{
{"resource", "mynamespace", "myresource", "/api/v1beta1/resource/myresource"}, {"resource", "mynamespace", "myresource", "/api/v1/namespaces/mynamespace/resource/myresource"},
{"resource", "", "myresource", "/api/v1beta1/resource/myresource"}, {"resource", "", "myresource", "/api/v1/resource/myresource"},
{"resource", "mynamespace", "", "/api/v1beta1/resource"}, {"resource", "mynamespace", "", "/api/v1/namespaces/mynamespace/resource"},
{"resource", "", "", "/api/v1beta1/resource"}, {"resource", "", "", "/api/v1/resource"},
} }
for _, item := range testCases { for _, item := range testCases {
if actual := ResourcePath(item.resource, item.namespace, item.name); actual != item.expected { if actual := ResourcePath(item.resource, item.namespace, item.name); actual != item.expected {
@ -144,9 +144,9 @@ func TestResourcePathWithNamespaceQueryForV1Beta3(t *testing.T) {
} }
} }
func TestResourcePathWithNamespaceQueryForV1Beta1(t *testing.T) { func TestResourcePathWithNamespaceQueryForV1(t *testing.T) {
if Version() != "v1beta1" { if Version() != "v1" {
// Skip the test if we are not testing v1beta1. // Skip the test if we are not testing v1.
return return
} }
@ -156,10 +156,10 @@ func TestResourcePathWithNamespaceQueryForV1Beta1(t *testing.T) {
name string name string
expected string expected string
}{ }{
{"resource", "mynamespace", "myresource", "/api/v1beta1/resource/myresource?namespace=mynamespace"}, {"resource", "mynamespace", "myresource", "/api/v1/namespaces/mynamespace/resource/myresource"},
{"resource", "", "myresource", "/api/v1beta1/resource/myresource"}, {"resource", "", "myresource", "/api/v1/resource/myresource"},
{"resource", "mynamespace", "", "/api/v1beta1/resource?namespace=mynamespace"}, {"resource", "mynamespace", "", "/api/v1/namespaces/mynamespace/resource"},
{"resource", "", "", "/api/v1beta1/resource"}, {"resource", "", "", "/api/v1/resource"},
} }
for _, item := range testCases { for _, item := range testCases {
if actual := ResourcePathWithNamespaceQuery(item.resource, item.namespace, item.name); actual != item.expected { if actual := ResourcePathWithNamespaceQuery(item.resource, item.namespace, item.name); actual != item.expected {

View File

@ -28,14 +28,14 @@ import (
func TestScopeNamingGenerateLink(t *testing.T) { func TestScopeNamingGenerateLink(t *testing.T) {
selfLinker := &setTestSelfLinker{ selfLinker := &setTestSelfLinker{
t: t, t: t,
expectedSet: "/api/v1beta3/namespaces/other/services/foo", expectedSet: "/api/v1/namespaces/other/services/foo",
name: "foo", name: "foo",
namespace: "other", namespace: "other",
} }
s := scopeNaming{ s := scopeNaming{
meta.RESTScopeNamespace, meta.RESTScopeNamespace,
selfLinker, selfLinker,
"/api/v1beta3/namespaces/{namespace}/services/{name}", "/api/v1/namespaces/{namespace}/services/{name}",
true, true,
} }
service := &api.Service{ service := &api.Service{

View File

@ -28,9 +28,6 @@ import (
) )
func getLimitRangesResourceName() string { func getLimitRangesResourceName() string {
if api.PreV1Beta3(testapi.Version()) {
return "limitRanges"
}
return "limitranges" return "limitranges"
} }

View File

@ -28,9 +28,6 @@ import (
) )
func getNodesResourceName() string { func getNodesResourceName() string {
if api.PreV1Beta3(testapi.Version()) {
return "minions"
}
return "nodes" return "nodes"
} }

View File

@ -28,9 +28,6 @@ import (
) )
func getPersistentVolumesResoureName() string { func getPersistentVolumesResoureName() string {
if api.PreV1Beta3(testapi.Version()) {
return "persistentVolumes"
}
return "persistentvolumes" return "persistentvolumes"
} }

View File

@ -28,9 +28,6 @@ import (
) )
func getPersistentVolumeClaimsResoureName() string { func getPersistentVolumeClaimsResoureName() string {
if api.PreV1Beta3(testapi.Version()) {
return "persistentVolumeClaims"
}
return "persistentvolumeclaims" return "persistentvolumeclaims"
} }

View File

@ -31,10 +31,6 @@ func getPodTemplatesResoureName() string {
} }
func TestPodTemplateCreate(t *testing.T) { func TestPodTemplateCreate(t *testing.T) {
if api.PreV1Beta3(testapi.Version()) {
return
}
ns := api.NamespaceDefault ns := api.NamespaceDefault
podTemplate := api.PodTemplate{ podTemplate := api.PodTemplate{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
@ -58,10 +54,6 @@ func TestPodTemplateCreate(t *testing.T) {
} }
func TestPodTemplateGet(t *testing.T) { func TestPodTemplateGet(t *testing.T) {
if api.PreV1Beta3(testapi.Version()) {
return
}
ns := api.NamespaceDefault ns := api.NamespaceDefault
podTemplate := &api.PodTemplate{ podTemplate := &api.PodTemplate{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
@ -85,10 +77,6 @@ func TestPodTemplateGet(t *testing.T) {
} }
func TestPodTemplateList(t *testing.T) { func TestPodTemplateList(t *testing.T) {
if api.PreV1Beta3(testapi.Version()) {
return
}
ns := api.NamespaceDefault ns := api.NamespaceDefault
podTemplateList := &api.PodTemplateList{ podTemplateList := &api.PodTemplateList{
Items: []api.PodTemplate{ Items: []api.PodTemplate{
@ -114,10 +102,6 @@ func TestPodTemplateList(t *testing.T) {
} }
func TestPodTemplateUpdate(t *testing.T) { func TestPodTemplateUpdate(t *testing.T) {
if api.PreV1Beta3(testapi.Version()) {
return
}
ns := api.NamespaceDefault ns := api.NamespaceDefault
podTemplate := &api.PodTemplate{ podTemplate := &api.PodTemplate{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
@ -136,10 +120,6 @@ func TestPodTemplateUpdate(t *testing.T) {
} }
func TestPodTemplateDelete(t *testing.T) { func TestPodTemplateDelete(t *testing.T) {
if api.PreV1Beta3(testapi.Version()) {
return
}
ns := api.NamespaceDefault ns := api.NamespaceDefault
c := &testClient{ c := &testClient{
Request: testRequest{Method: "DELETE", Path: testapi.ResourcePath(getPodTemplatesResoureName(), ns, "foo"), Query: buildQueryValues(ns, nil)}, Request: testRequest{Method: "DELETE", Path: testapi.ResourcePath(getPodTemplatesResoureName(), ns, "foo"), Query: buildQueryValues(ns, nil)},
@ -150,10 +130,6 @@ func TestPodTemplateDelete(t *testing.T) {
} }
func TestPodTemplateWatch(t *testing.T) { func TestPodTemplateWatch(t *testing.T) {
if api.PreV1Beta3(testapi.Version()) {
return
}
c := &testClient{ c := &testClient{
Request: testRequest{ Request: testRequest{
Method: "GET", Method: "GET",

View File

@ -67,7 +67,7 @@ func TestAddOrUpdateEventExisting(t *testing.T) {
Name: "anOkName", Name: "anOkName",
Namespace: "someNamespace", Namespace: "someNamespace",
UID: "C934D3234CD0242", UID: "C934D3234CD0242",
APIVersion: "v1beta2", APIVersion: "version",
}, },
Source: api.EventSource{ Source: api.EventSource{
Component: "kubelet", Component: "kubelet",
@ -88,7 +88,7 @@ func TestAddOrUpdateEventExisting(t *testing.T) {
Name: "anOkName", Name: "anOkName",
Namespace: "someNamespace", Namespace: "someNamespace",
UID: "C934D3234CD0242", UID: "C934D3234CD0242",
APIVersion: "v1beta2", APIVersion: "version",
}, },
Source: api.EventSource{ Source: api.EventSource{
Component: "kubelet", Component: "kubelet",
@ -119,7 +119,7 @@ func TestGetEventNoExisting(t *testing.T) {
Name: "iAmAController", Name: "iAmAController",
Namespace: "IHaveANamespace", Namespace: "IHaveANamespace",
UID: "9039D34AFBCDA42", UID: "9039D34AFBCDA42",
APIVersion: "v1beta3", APIVersion: "version",
}, },
Source: api.EventSource{ Source: api.EventSource{
Component: "kubelet", Component: "kubelet",

View File

@ -25,9 +25,6 @@ import (
) )
func getRCResourceName() string { func getRCResourceName() string {
if api.PreV1Beta3(testapi.Version()) {
return "replicationControllers"
}
return "replicationcontrollers" return "replicationcontrollers"
} }

View File

@ -28,9 +28,6 @@ import (
) )
func getResourceQuotasResoureName() string { func getResourceQuotasResoureName() string {
if api.PreV1Beta3(testapi.Version()) {
return "resourceQuotas"
}
return "resourcequotas" return "resourcequotas"
} }

View File

@ -166,9 +166,6 @@ func validateSyncReplication(t *testing.T, fakePodControl *FakePodControl, expec
} }
func replicationControllerResourceName() string { func replicationControllerResourceName() string {
if api.PreV1Beta3(testapi.Version()) {
return "replicationControllers"
}
return "replicationcontrollers" return "replicationcontrollers"
} }
@ -193,7 +190,7 @@ func makeTestServer(t *testing.T, namespace, name string, podResponse, controlle
mux := http.NewServeMux() mux := http.NewServeMux()
mux.Handle(testapi.ResourcePath("pods", namespace, ""), &fakePodHandler) mux.Handle(testapi.ResourcePath("pods", namespace, ""), &fakePodHandler)
mux.Handle(testapi.ResourcePath(replicationControllerResourceName(), "", ""), &fakeControllerHandler) mux.Handle(testapi.ResourcePath(replicationControllerResourceName(), "", ""), &fakeControllerHandler)
if !api.PreV1Beta3(testapi.Version()) && namespace != "" { if namespace != "" {
mux.Handle(testapi.ResourcePath(replicationControllerResourceName(), namespace, ""), &fakeControllerHandler) mux.Handle(testapi.ResourcePath(replicationControllerResourceName(), namespace, ""), &fakeControllerHandler)
} }
if name != "" { if name != "" {

View File

@ -49,7 +49,7 @@ func TestRunExposeService(t *testing.T) {
}, },
input: &api.Service{ input: &api.Service{
ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"}, ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1beta3"}, TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Selector: map[string]string{"app": "go"}, Selector: map[string]string{"app": "go"},
}, },
@ -57,7 +57,7 @@ func TestRunExposeService(t *testing.T) {
flags: map[string]string{"selector": "func=stream", "protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test"}, flags: map[string]string{"selector": "func=stream", "protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test"},
output: &api.Service{ output: &api.Service{
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "test", ResourceVersion: "12", Labels: map[string]string{"svc": "test"}}, ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "test", ResourceVersion: "12", Labels: map[string]string{"svc": "test"}},
TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1beta3"}, TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Ports: []api.ServicePort{ Ports: []api.ServicePort{
{ {
@ -82,7 +82,7 @@ func TestRunExposeService(t *testing.T) {
}, },
input: &api.Service{ input: &api.Service{
ObjectMeta: api.ObjectMeta{Name: "mayor", Namespace: "default", ResourceVersion: "12"}, ObjectMeta: api.ObjectMeta{Name: "mayor", Namespace: "default", ResourceVersion: "12"},
TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1beta3"}, TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Selector: map[string]string{"run": "this"}, Selector: map[string]string{"run": "this"},
}, },
@ -91,7 +91,7 @@ func TestRunExposeService(t *testing.T) {
flags: map[string]string{"selector": "run=this", "port": "80", "labels": "runas=amayor"}, flags: map[string]string{"selector": "run=this", "port": "80", "labels": "runas=amayor"},
output: &api.Service{ output: &api.Service{
ObjectMeta: api.ObjectMeta{Name: "mayor", Namespace: "default", ResourceVersion: "12", Labels: map[string]string{"runas": "amayor"}}, ObjectMeta: api.ObjectMeta{Name: "mayor", Namespace: "default", ResourceVersion: "12", Labels: map[string]string{"runas": "amayor"}},
TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1beta3"}, TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Ports: []api.ServicePort{ Ports: []api.ServicePort{
{ {

View File

@ -117,7 +117,7 @@ func TestLabelsForObject(t *testing.T) {
name: "successful re-use of labels", name: "successful re-use of labels",
object: &api.Service{ object: &api.Service{
ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", Labels: map[string]string{"svc": "test"}}, ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", Labels: map[string]string{"svc": "test"}},
TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1beta3"}, TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1"},
}, },
expected: "svc=test", expected: "svc=test",
err: nil, err: nil,
@ -126,7 +126,7 @@ func TestLabelsForObject(t *testing.T) {
name: "empty labels", name: "empty labels",
object: &api.Service{ object: &api.Service{
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "test", Labels: map[string]string{}}, ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "test", Labels: map[string]string{}},
TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1beta3"}, TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1"},
}, },
expected: "", expected: "",
err: nil, err: nil,
@ -135,7 +135,7 @@ func TestLabelsForObject(t *testing.T) {
name: "nil labels", name: "nil labels",
object: &api.Service{ object: &api.Service{
ObjectMeta: api.ObjectMeta{Name: "zen", Namespace: "test", Labels: nil}, ObjectMeta: api.ObjectMeta{Name: "zen", Namespace: "test", Labels: nil},
TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1beta3"}, TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1"},
}, },
expected: "", expected: "",
err: nil, err: nil,

View File

@ -416,20 +416,7 @@ func TestTemplateStrings(t *testing.T) {
} }
// The point of this test is to verify that the below template works. If you change this // The point of this test is to verify that the below template works. If you change this
// template, you need to update hack/e2e-suite/update.sh. // template, you need to update hack/e2e-suite/update.sh.
tmpl := `` tmpl := `{{if (exists . "status" "containerStatuses")}}{{range .status.containerStatuses}}{{if (and (eq .name "foo") (exists . "state" "running"))}}true{{end}}{{end}}{{end}}`
if api.PreV1Beta3(testapi.Version()) {
tmpl = `{{exists . "currentState" "info" "foo" "state" "running"}}`
useThisToDebug := `
a: {{exists . "currentState"}}
b: {{exists . "currentState" "info"}}
c: {{exists . "currentState" "info" "foo"}}
d: {{exists . "currentState" "info" "foo" "state"}}
e: {{exists . "currentState" "info" "foo" "state" "running"}}
f: {{exists . "currentState" "info" "foo" "state" "running" "startedAt"}}`
_ = useThisToDebug // don't complain about unused var
} else {
tmpl = `{{if (exists . "status" "containerStatuses")}}{{range .status.containerStatuses}}{{if (and (eq .name "foo") (exists . "state" "running"))}}true{{end}}{{end}}{{end}}`
}
p, err := NewTemplatePrinter([]byte(tmpl)) p, err := NewTemplatePrinter([]byte(tmpl))
if err != nil { if err != nil {
t.Fatalf("tmpl fail: %v", err) t.Fatalf("tmpl fail: %v", err)

View File

@ -21,6 +21,7 @@ import (
"testing" "testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/registered"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
"github.com/GoogleCloudPlatform/kubernetes/pkg/securitycontext" "github.com/GoogleCloudPlatform/kubernetes/pkg/securitycontext"
@ -56,11 +57,6 @@ func TestDecodeSinglePod(t *testing.T) {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
parsed, podOut, err := tryDecodeSinglePod(json, noDefault) parsed, podOut, err := tryDecodeSinglePod(json, noDefault)
if testapi.Version() == "v1beta1" {
// v1beta1 conversion leaves empty lists that should be nil
podOut.Spec.Containers[0].Resources.Limits = nil
podOut.Spec.Containers[0].Resources.Requests = nil
}
if !parsed { if !parsed {
t.Errorf("expected to have parsed file: (%s)", string(json)) t.Errorf("expected to have parsed file: (%s)", string(json))
} }
@ -71,24 +67,26 @@ func TestDecodeSinglePod(t *testing.T) {
t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, podOut, string(json)) t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, podOut, string(json))
} }
externalPod, err := testapi.Converter().ConvertToVersion(pod, "v1beta3") for _, version := range registered.RegisteredVersions {
if err != nil { externalPod, err := testapi.Converter().ConvertToVersion(pod, version)
t.Errorf("unexpected error: %v", err) if err != nil {
} t.Errorf("unexpected error: %v", err)
yaml, err := yaml.Marshal(externalPod) }
if err != nil { yaml, err := yaml.Marshal(externalPod)
t.Errorf("unexpected error: %v", err) if err != nil {
} t.Errorf("unexpected error: %v", err)
}
parsed, podOut, err = tryDecodeSinglePod(yaml, noDefault) parsed, podOut, err = tryDecodeSinglePod(yaml, noDefault)
if !parsed { if !parsed {
t.Errorf("expected to have parsed file: (%s)", string(yaml)) t.Errorf("expected to have parsed file: (%s)", string(yaml))
} }
if err != nil { if err != nil {
t.Errorf("unexpected error: %v (%s)", err, string(yaml)) t.Errorf("unexpected error: %v (%s)", err, string(yaml))
} }
if !reflect.DeepEqual(pod, podOut) { if !reflect.DeepEqual(pod, podOut) {
t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, podOut, string(yaml)) t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, podOut, string(yaml))
}
} }
} }
@ -137,23 +135,25 @@ func TestDecodePodList(t *testing.T) {
t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", podList, &podListOut, string(json)) t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", podList, &podListOut, string(json))
} }
externalPodList, err := testapi.Converter().ConvertToVersion(podList, "v1beta3") for _, version := range registered.RegisteredVersions {
if err != nil { externalPodList, err := testapi.Converter().ConvertToVersion(podList, version)
t.Errorf("unexpected error: %v", err) if err != nil {
} t.Errorf("unexpected error: %v", err)
yaml, err := yaml.Marshal(externalPodList) }
if err != nil { yaml, err := yaml.Marshal(externalPodList)
t.Errorf("unexpected error: %v", err) if err != nil {
} t.Errorf("unexpected error: %v", err)
}
parsed, podListOut, err = tryDecodePodList(yaml, noDefault) parsed, podListOut, err = tryDecodePodList(yaml, noDefault)
if !parsed { if !parsed {
t.Errorf("expected to have parsed file: (%s)", string(yaml)) t.Errorf("expected to have parsed file: (%s)", string(yaml))
} }
if err != nil { if err != nil {
t.Errorf("unexpected error: %v (%s)", err, string(yaml)) t.Errorf("unexpected error: %v (%s)", err, string(yaml))
} }
if !reflect.DeepEqual(podList, &podListOut) { if !reflect.DeepEqual(podList, &podListOut) {
t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, &podListOut, string(yaml)) t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, &podListOut, string(yaml))
}
} }
} }

View File

@ -17,7 +17,6 @@ limitations under the License.
package config package config
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"testing" "testing"
@ -68,99 +67,6 @@ func writeTestFile(t *testing.T, dir, name string, contents string) *os.File {
return file return file
} }
func TestReadContainerManifestFromFile(t *testing.T) {
// ContainerManifest is supported only for pre v1beta3 versions.
if !api.PreV1Beta3(testapi.Version()) {
return
}
hostname := "random-test-hostname"
var testCases = []struct {
desc string
fileContents string
expected kubelet.PodUpdate
}{
{
desc: "Manifest",
fileContents: fmt.Sprintf(`{
"version": "%s",
"uuid": "12345",
"id": "test",
"containers": [{ "name": "image", "image": "test/image", "imagePullPolicy": "PullAlways"}]
}`, testapi.Version()),
expected: CreatePodUpdate(kubelet.SET, kubelet.FileSource, &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "test-" + hostname,
UID: "12345",
Namespace: kubelet.NamespaceDefault,
SelfLink: getSelfLink("test-"+hostname, kubelet.NamespaceDefault),
},
Spec: api.PodSpec{
NodeName: hostname,
RestartPolicy: api.RestartPolicyAlways,
DNSPolicy: api.DNSClusterFirst,
Containers: []api.Container{{
Name: "image",
Image: "test/image",
TerminationMessagePath: "/dev/termination-log",
ImagePullPolicy: "Always",
SecurityContext: securitycontext.ValidSecurityContextWithContainerDefaults()}},
},
}),
},
{
desc: "Manifest without ID",
fileContents: fmt.Sprintf(`{
"version": "%s",
"uuid": "12345",
"containers": [{ "name": "image", "image": "test/image", "imagePullPolicy": "PullAlways"}]
}`, testapi.Version()),
expected: CreatePodUpdate(kubelet.SET, kubelet.FileSource, &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "12345-" + hostname,
UID: "12345",
Namespace: kubelet.NamespaceDefault,
SelfLink: getSelfLink("12345-"+hostname, kubelet.NamespaceDefault),
},
Spec: api.PodSpec{
NodeName: hostname,
RestartPolicy: api.RestartPolicyAlways,
DNSPolicy: api.DNSClusterFirst,
Containers: []api.Container{{
Name: "image",
Image: "test/image",
TerminationMessagePath: "/dev/termination-log",
ImagePullPolicy: "Always",
SecurityContext: securitycontext.ValidSecurityContextWithContainerDefaults()}},
},
}),
},
}
for _, testCase := range testCases {
func() {
file := writeTestFile(t, os.TempDir(), "test_pod_config", testCase.fileContents)
defer os.Remove(file.Name())
ch := make(chan interface{})
NewSourceFile(file.Name(), hostname, time.Millisecond, ch)
select {
case got := <-ch:
update := got.(kubelet.PodUpdate)
for _, pod := range update.Pods {
if errs := validation.ValidatePod(pod); len(errs) > 0 {
t.Errorf("%s: Invalid pod %#v, %#v", testCase.desc, pod, errs)
}
}
if !api.Semantic.DeepEqual(testCase.expected, update) {
t.Errorf("%s: Expected %#v, Got %#v", testCase.desc, testCase.expected, update)
}
case <-time.After(time.Second):
t.Errorf("%s: Expected update, timeout instead", testCase.desc)
}
}()
}
}
func TestReadPodsFromFile(t *testing.T) { func TestReadPodsFromFile(t *testing.T) {
hostname := "random-test-hostname" hostname := "random-test-hostname"
var testCases = []struct { var testCases = []struct {
@ -276,32 +182,6 @@ func TestReadPodsFromFile(t *testing.T) {
} }
} }
func TestReadManifestFromFileWithDefaults(t *testing.T) {
if !api.PreV1Beta3(testapi.Version()) {
return
}
file := writeTestFile(t, os.TempDir(), "test_pod_config",
fmt.Sprintf(`{
"version": "%s",
"id": "test",
"containers": [{ "name": "image", "image": "test/image" }]
}`, testapi.Version()))
defer os.Remove(file.Name())
ch := make(chan interface{})
NewSourceFile(file.Name(), "localhost", time.Millisecond, ch)
select {
case got := <-ch:
update := got.(kubelet.PodUpdate)
if update.Pods[0].UID == "" {
t.Errorf("Unexpected UID: %s", update.Pods[0].UID)
}
case <-time.After(time.Second):
t.Errorf("Expected update, timeout instead")
}
}
func TestExtractFromBadDataFile(t *testing.T) { func TestExtractFromBadDataFile(t *testing.T) {
file := writeTestFile(t, os.TempDir(), "test_pod_config", string([]byte{1, 2, 3})) file := writeTestFile(t, os.TempDir(), "test_pod_config", string([]byte{1, 2, 3}))
defer os.Remove(file.Name()) defer os.Remove(file.Name())

View File

@ -138,6 +138,7 @@ func TestDecodeList(t *testing.T) {
Items: []runtime.Object{ Items: []runtime.Object{
&api.Pod{ObjectMeta: api.ObjectMeta{Name: "1"}}, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "1"}},
&runtime.Unknown{TypeMeta: runtime.TypeMeta{Kind: "Pod", APIVersion: "v1beta3"}, RawJSON: []byte(`{"kind":"Pod","apiVersion":"v1beta3","metadata":{"name":"test"}}`)}, &runtime.Unknown{TypeMeta: runtime.TypeMeta{Kind: "Pod", APIVersion: "v1beta3"}, RawJSON: []byte(`{"kind":"Pod","apiVersion":"v1beta3","metadata":{"name":"test"}}`)},
&runtime.Unknown{TypeMeta: runtime.TypeMeta{Kind: "Pod", APIVersion: "v1"}, RawJSON: []byte(`{"kind":"Pod","apiVersion":"v1","metadata":{"name":"test"}}`)},
&runtime.Unstructured{TypeMeta: runtime.TypeMeta{Kind: "Foo", APIVersion: "Bar"}, Object: map[string]interface{}{"test": "value"}}, &runtime.Unstructured{TypeMeta: runtime.TypeMeta{Kind: "Foo", APIVersion: "Bar"}, Object: map[string]interface{}{"test": "value"}},
}, },
} }

View File

@ -29,6 +29,8 @@ func TestDecodeUnstructured(t *testing.T) {
&api.Pod{ObjectMeta: api.ObjectMeta{Name: "1"}}, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "1"}},
&runtime.Unknown{TypeMeta: runtime.TypeMeta{Kind: "Pod", APIVersion: "v1beta3"}, RawJSON: []byte(`{"kind":"Pod","apiVersion":"v1beta3","metadata":{"name":"test"}}`)}, &runtime.Unknown{TypeMeta: runtime.TypeMeta{Kind: "Pod", APIVersion: "v1beta3"}, RawJSON: []byte(`{"kind":"Pod","apiVersion":"v1beta3","metadata":{"name":"test"}}`)},
&runtime.Unknown{TypeMeta: runtime.TypeMeta{Kind: "", APIVersion: "v1beta3"}, RawJSON: []byte(`{"kind":"Pod","apiVersion":"v1beta3","metadata":{"name":"test"}}`)}, &runtime.Unknown{TypeMeta: runtime.TypeMeta{Kind: "", APIVersion: "v1beta3"}, RawJSON: []byte(`{"kind":"Pod","apiVersion":"v1beta3","metadata":{"name":"test"}}`)},
&runtime.Unknown{TypeMeta: runtime.TypeMeta{Kind: "Pod", APIVersion: "v1"}, RawJSON: []byte(`{"kind":"Pod","apiVersion":"v1","metadata":{"name":"test"}}`)},
&runtime.Unknown{TypeMeta: runtime.TypeMeta{Kind: "", APIVersion: "v1"}, RawJSON: []byte(`{"kind":"Pod","apiVersion":"v1","metadata":{"name":"test"}}`)},
&runtime.Unstructured{TypeMeta: runtime.TypeMeta{Kind: "Foo", APIVersion: "Bar"}, Object: map[string]interface{}{"test": "value"}}, &runtime.Unstructured{TypeMeta: runtime.TypeMeta{Kind: "Foo", APIVersion: "Bar"}, Object: map[string]interface{}{"test": "value"}},
}, },
} }