mirror of https://github.com/k3s-io/k3s
Merge pull request #9217 from caesarxuchao/add-v1-tests-to-pkg/kubectl/cmd
add v1 tests to kubectl/cmd/*pull/6/head
commit
c92b4255b3
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue