diff --git a/pkg/kubectl/cmd/exec_test.go b/pkg/kubectl/cmd/exec_test.go index 7a53a5b3ee..b582ea1fc7 100644 --- a/pkg/kubectl/cmd/exec_test.go +++ b/pkg/kubectl/cmd/exec_test.go @@ -118,26 +118,38 @@ func TestPodAndContainer(t *testing.T) { func TestExec(t *testing.T) { tests := []struct { name, version, podPath, execPath, container string - nsInQuery bool pod *api.Pod execErr bool }{ { - name: "v1beta3 - pod exec", - version: "v1beta3", - podPath: "/api/v1beta3/namespaces/test/pods/foo", - execPath: "/api/v1beta3/namespaces/test/pods/foo/exec", - nsInQuery: false, - pod: execPod(), + name: "v1beta3 - pod exec", + version: "v1beta3", + podPath: "/api/v1beta3/namespaces/test/pods/foo", + execPath: "/api/v1beta3/namespaces/test/pods/foo/exec", + pod: execPod(), }, { - name: "v1beta3 - pod exec error", - version: "v1beta3", - podPath: "/api/v1beta3/namespaces/test/pods/foo", - execPath: "/api/v1beta3/namespaces/test/pods/foo/exec", - nsInQuery: false, - pod: execPod(), - execErr: true, + name: "v1beta3 - pod exec error", + version: "v1beta3", + podPath: "/api/v1beta3/namespaces/test/pods/foo", + execPath: "/api/v1beta3/namespaces/test/pods/foo/exec", + pod: execPod(), + execErr: true, + }, + { + name: "v1 - pod exec", + version: "v1", + podPath: "/api/v1/namespaces/test/pods/foo", + execPath: "/api/v1/namespaces/test/pods/foo/exec", + pod: execPod(), + }, + { + name: "v1 - pod exec error", + version: "v1", + podPath: "/api/v1/namespaces/test/pods/foo", + execPath: "/api/v1/namespaces/test/pods/foo/exec", + pod: execPod(), + execErr: true, }, } for _, test := range tests { @@ -147,11 +159,6 @@ func TestExec(t *testing.T) { Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == test.podPath && m == "GET": - if test.nsInQuery { - if ns := req.URL.Query().Get("namespace"); ns != "test" { - t.Errorf("%s: did not get expected namespace: %s\n", test.name, ns) - } - } body := objBody(codec, test.pod) return &http.Response{StatusCode: 200, Body: body}, nil default: diff --git a/pkg/kubectl/cmd/log_test.go b/pkg/kubectl/cmd/log_test.go index d4e88e205d..a142cdd5e8 100644 --- a/pkg/kubectl/cmd/log_test.go +++ b/pkg/kubectl/cmd/log_test.go @@ -153,16 +153,21 @@ func TestLog(t *testing.T) { tests := []struct { name, version, podPath, logPath, container string - nsInQuery bool pod *api.Pod }{ { - name: "v1beta3 - pod log", - version: "v1beta3", - podPath: "/api/v1beta3/namespaces/test/pods/foo", - logPath: "/api/v1beta3/namespaces/test/pods/foo/log", - nsInQuery: false, - pod: testPod(), + name: "v1beta3 - pod log", + version: "v1beta3", + podPath: "/api/v1beta3/namespaces/test/pods/foo", + logPath: "/api/v1beta3/namespaces/test/pods/foo/log", + pod: testPod(), + }, + { + name: "v1 - pod log", + version: "v1", + podPath: "/api/v1/namespaces/test/pods/foo", + logPath: "/api/v1/namespaces/test/pods/foo/log", + pod: testPod(), }, } for _, test := range tests { @@ -173,19 +178,9 @@ func TestLog(t *testing.T) { Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == test.podPath && m == "GET": - if test.nsInQuery { - if ns := req.URL.Query().Get("namespace"); ns != "test" { - t.Errorf("%s: did not get expected namespace: %s\n", test.name, ns) - } - } body := objBody(codec, test.pod) return &http.Response{StatusCode: 200, Body: body}, nil case p == test.logPath && m == "GET": - if test.nsInQuery { - if ns := req.URL.Query().Get("namespace"); ns != "test" { - t.Errorf("%s: did not get expected namespace: %s\n", test.name, ns) - } - } body := ioutil.NopCloser(bytes.NewBufferString(logContent)) return &http.Response{StatusCode: 200, Body: body}, nil default: diff --git a/pkg/kubectl/cmd/portforward_test.go b/pkg/kubectl/cmd/portforward_test.go index fadcf94c5d..1833468ca8 100644 --- a/pkg/kubectl/cmd/portforward_test.go +++ b/pkg/kubectl/cmd/portforward_test.go @@ -41,26 +41,38 @@ func TestPortForward(t *testing.T) { tests := []struct { name, version, podPath, pfPath, container string - nsInQuery bool pod *api.Pod pfErr bool }{ { - name: "v1beta3 - pod portforward", - version: "v1beta3", - podPath: "/api/v1beta3/namespaces/test/pods/foo", - pfPath: "/api/v1beta3/namespaces/test/pods/foo/portforward", - nsInQuery: false, - pod: execPod(), + name: "v1beta3 - pod portforward", + version: "v1beta3", + podPath: "/api/v1beta3/namespaces/test/pods/foo", + pfPath: "/api/v1beta3/namespaces/test/pods/foo/portforward", + pod: execPod(), }, { - name: "v1beta3 - pod portforward error", - version: "v1beta3", - podPath: "/api/v1beta3/namespaces/test/pods/foo", - pfPath: "/api/v1beta3/namespaces/test/pods/foo/portforward", - nsInQuery: false, - pod: execPod(), - pfErr: true, + name: "v1beta3 - pod portforward error", + version: "v1beta3", + podPath: "/api/v1beta3/namespaces/test/pods/foo", + pfPath: "/api/v1beta3/namespaces/test/pods/foo/portforward", + pod: execPod(), + pfErr: true, + }, + { + name: "v1 - pod portforward", + version: "v1", + podPath: "/api/v1/namespaces/test/pods/foo", + pfPath: "/api/v1/namespaces/test/pods/foo/portforward", + pod: execPod(), + }, + { + name: "v1 - pod portforward error", + version: "v1", + podPath: "/api/v1/namespaces/test/pods/foo", + pfPath: "/api/v1/namespaces/test/pods/foo/portforward", + pod: execPod(), + pfErr: true, }, } for _, test := range tests { @@ -70,11 +82,6 @@ func TestPortForward(t *testing.T) { Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == test.podPath && m == "GET": - if test.nsInQuery { - if ns := req.URL.Query().Get("namespace"); ns != "test" { - t.Errorf("%s: did not get expected namespace: %s\n", test.name, ns) - } - } body := objBody(codec, test.pod) return &http.Response{StatusCode: 200, Body: body}, nil default: diff --git a/pkg/kubectl/cmd/util/helpers_test.go b/pkg/kubectl/cmd/util/helpers_test.go index 232eefea14..261474ec87 100644 --- a/pkg/kubectl/cmd/util/helpers_test.go +++ b/pkg/kubectl/cmd/util/helpers_test.go @@ -174,6 +174,144 @@ func TestMerge(t *testing.T) { }, }, }, + { + kind: "Pod", + obj: &api.Pod{ + ObjectMeta: api.ObjectMeta{ + Name: "foo", + }, + }, + fragment: `{ "apiVersion": "v1" }`, + expected: &api.Pod{ + ObjectMeta: api.ObjectMeta{ + Name: "foo", + }, + Spec: api.PodSpec{ + RestartPolicy: api.RestartPolicyAlways, + DNSPolicy: api.DNSClusterFirst, + }, + }, + }, + /* TODO: uncomment this test once Merge is updated to use + strategic-merge-patch. See #844. + { + kind: "Pod", + obj: &api.Pod{ + ObjectMeta: api.ObjectMeta{ + Name: "foo", + }, + Spec: api.PodSpec{ + Containers: []api.Container{ + api.Container{ + Name: "c1", + Image: "red-image", + }, + api.Container{ + Name: "c2", + Image: "blue-image", + }, + }, + }, + }, + fragment: `{ "apiVersion": "v1", "spec": { "containers": [ { "name": "c1", "image": "green-image" } ] } }`, + expected: &api.Pod{ + ObjectMeta: api.ObjectMeta{ + Name: "foo", + }, + Spec: api.PodSpec{ + Containers: []api.Container{ + api.Container{ + Name: "c1", + Image: "green-image", + }, + api.Container{ + Name: "c2", + Image: "blue-image", + }, + }, + }, + }, + }, */ + { + kind: "Pod", + obj: &api.Pod{ + ObjectMeta: api.ObjectMeta{ + Name: "foo", + }, + }, + fragment: `{ "apiVersion": "v1", "spec": { "volumes": [ {"name": "v1"}, {"name": "v2"} ] } }`, + expected: &api.Pod{ + ObjectMeta: api.ObjectMeta{ + Name: "foo", + }, + Spec: api.PodSpec{ + Volumes: []api.Volume{ + { + Name: "v1", + VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}, + }, + { + Name: "v2", + VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}, + }, + }, + RestartPolicy: api.RestartPolicyAlways, + DNSPolicy: api.DNSClusterFirst, + }, + }, + }, + { + kind: "Pod", + obj: &api.Pod{}, + fragment: "invalid json", + expected: &api.Pod{}, + expectErr: true, + }, + { + kind: "Service", + obj: &api.Service{}, + fragment: `{ "apiVersion": "badVersion" }`, + expectErr: true, + }, + { + kind: "Service", + obj: &api.Service{ + Spec: api.ServiceSpec{}, + }, + fragment: `{ "apiVersion": "v1", "spec": { "ports": [ { "port": 0 } ] } }`, + expected: &api.Service{ + Spec: api.ServiceSpec{ + SessionAffinity: "None", + Type: api.ServiceTypeClusterIP, + Ports: []api.ServicePort{ + { + Protocol: api.ProtocolTCP, + Port: 0, + }, + }, + }, + }, + }, + { + kind: "Service", + obj: &api.Service{ + Spec: api.ServiceSpec{ + Selector: map[string]string{ + "version": "v1", + }, + }, + }, + fragment: `{ "apiVersion": "v1", "spec": { "selector": { "version": "v2" } } }`, + expected: &api.Service{ + Spec: api.ServiceSpec{ + SessionAffinity: "None", + Type: api.ServiceTypeClusterIP, + Selector: map[string]string{ + "version": "v2", + }, + }, + }, + }, } for i, test := range tests {