2015-05-07 15:30:28 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 The Kubernetes Authors 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 cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-09-11 20:47:53 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/unversioned/fake"
|
2015-10-02 23:52:42 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
|
|
|
"k8s.io/kubernetes/pkg/util"
|
2015-05-07 15:30:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRunExposeService(t *testing.T) {
|
|
|
|
tests := []struct {
|
2015-09-10 10:13:00 +00:00
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
ns string
|
|
|
|
calls map[string]string
|
|
|
|
input runtime.Object
|
|
|
|
flags map[string]string
|
|
|
|
output runtime.Object
|
|
|
|
expected string
|
|
|
|
status int
|
2015-05-07 15:30:28 +00:00
|
|
|
}{
|
2015-08-12 05:48:00 +00:00
|
|
|
{
|
2015-09-10 10:13:00 +00:00
|
|
|
name: "expose-service-from-service-no-selector-defined",
|
2015-08-12 05:48:00 +00:00
|
|
|
args: []string{"service", "baz"},
|
|
|
|
ns: "test",
|
|
|
|
calls: map[string]string{
|
|
|
|
"GET": "/namespaces/test/services/baz",
|
|
|
|
"POST": "/namespaces/test/services",
|
|
|
|
},
|
|
|
|
input: &api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Selector: map[string]string{"app": "go"},
|
|
|
|
},
|
|
|
|
},
|
2015-09-10 10:13:00 +00:00
|
|
|
flags: map[string]string{"protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test"},
|
2015-08-12 05:48:00 +00:00
|
|
|
output: &api.Service{
|
2015-09-10 10:13:00 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "", Labels: map[string]string{"svc": "test"}},
|
2015-08-12 05:48:00 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Ports: []api.ServicePort{
|
|
|
|
{
|
2015-09-10 10:13:00 +00:00
|
|
|
Protocol: api.ProtocolUDP,
|
|
|
|
Port: 14,
|
|
|
|
TargetPort: util.NewIntOrStringFromInt(14),
|
2015-08-12 05:48:00 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Selector: map[string]string{"app": "go"},
|
|
|
|
},
|
|
|
|
},
|
2015-09-10 10:13:00 +00:00
|
|
|
expected: "service \"foo\" exposed",
|
|
|
|
status: 200,
|
2015-08-12 05:48:00 +00:00
|
|
|
},
|
2015-05-07 15:30:28 +00:00
|
|
|
{
|
|
|
|
name: "expose-service-from-service",
|
|
|
|
args: []string{"service", "baz"},
|
2015-05-26 12:03:11 +00:00
|
|
|
ns: "test",
|
|
|
|
calls: map[string]string{
|
|
|
|
"GET": "/namespaces/test/services/baz",
|
|
|
|
"POST": "/namespaces/test/services",
|
|
|
|
},
|
2015-05-07 15:30:28 +00:00
|
|
|
input: &api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Selector: map[string]string{"app": "go"},
|
|
|
|
},
|
|
|
|
},
|
2015-05-13 12:14:44 +00:00
|
|
|
flags: map[string]string{"selector": "func=stream", "protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test"},
|
2015-05-07 15:30:28 +00:00
|
|
|
output: &api.Service{
|
2015-09-10 10:13:00 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "", Labels: map[string]string{"svc": "test"}},
|
2015-05-07 15:30:28 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Ports: []api.ServicePort{
|
|
|
|
{
|
2015-09-10 10:13:00 +00:00
|
|
|
Protocol: api.ProtocolUDP,
|
|
|
|
Port: 14,
|
|
|
|
TargetPort: util.NewIntOrStringFromInt(14),
|
2015-05-07 15:30:28 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Selector: map[string]string{"func": "stream"},
|
|
|
|
},
|
|
|
|
},
|
2015-09-10 10:13:00 +00:00
|
|
|
expected: "service \"foo\" exposed",
|
|
|
|
status: 200,
|
2015-05-26 12:03:11 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no-name-passed-from-the-cli",
|
|
|
|
args: []string{"service", "mayor"},
|
|
|
|
ns: "default",
|
|
|
|
calls: map[string]string{
|
|
|
|
"GET": "/namespaces/default/services/mayor",
|
|
|
|
"POST": "/namespaces/default/services",
|
|
|
|
},
|
|
|
|
input: &api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "mayor", Namespace: "default", ResourceVersion: "12"},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Selector: map[string]string{"run": "this"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// No --name flag specified below. Service will use the rc's name passed via the 'default-name' parameter
|
|
|
|
flags: map[string]string{"selector": "run=this", "port": "80", "labels": "runas=amayor"},
|
|
|
|
output: &api.Service{
|
2015-09-10 10:13:00 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "mayor", Namespace: "", Labels: map[string]string{"runas": "amayor"}},
|
2015-05-26 12:03:11 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Ports: []api.ServicePort{
|
|
|
|
{
|
2015-09-10 10:13:00 +00:00
|
|
|
Protocol: api.ProtocolTCP,
|
|
|
|
Port: 80,
|
|
|
|
TargetPort: util.NewIntOrStringFromInt(80),
|
2015-05-26 12:03:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Selector: map[string]string{"run": "this"},
|
|
|
|
},
|
|
|
|
},
|
2015-09-10 10:13:00 +00:00
|
|
|
expected: "service \"mayor\" exposed",
|
|
|
|
status: 200,
|
2015-06-05 03:52:46 +00:00
|
|
|
},
|
|
|
|
{
|
2015-09-29 11:39:10 +00:00
|
|
|
name: "expose-service",
|
2015-06-05 03:52:46 +00:00
|
|
|
args: []string{"service", "baz"},
|
|
|
|
ns: "test",
|
|
|
|
calls: map[string]string{
|
|
|
|
"GET": "/namespaces/test/services/baz",
|
|
|
|
"POST": "/namespaces/test/services",
|
|
|
|
},
|
|
|
|
input: &api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Selector: map[string]string{"app": "go"},
|
|
|
|
},
|
|
|
|
},
|
2015-10-02 23:52:42 +00:00
|
|
|
flags: map[string]string{"selector": "func=stream", "protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test", "type": "LoadBalancer", "dry-run": "true"},
|
2015-06-05 03:52:46 +00:00
|
|
|
output: &api.Service{
|
2015-09-10 10:13:00 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "", Labels: map[string]string{"svc": "test"}},
|
2015-06-05 03:52:46 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Ports: []api.ServicePort{
|
|
|
|
{
|
2015-09-10 10:13:00 +00:00
|
|
|
Protocol: api.ProtocolUDP,
|
2015-07-13 13:45:21 +00:00
|
|
|
Port: 14,
|
|
|
|
TargetPort: util.NewIntOrStringFromInt(14),
|
2015-06-05 03:52:46 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Selector: map[string]string{"func": "stream"},
|
|
|
|
Type: api.ServiceTypeLoadBalancer,
|
|
|
|
},
|
|
|
|
},
|
2015-10-02 23:52:42 +00:00
|
|
|
status: 200,
|
2015-05-07 15:30:28 +00:00
|
|
|
},
|
2015-07-31 17:34:52 +00:00
|
|
|
{
|
2015-09-29 11:39:10 +00:00
|
|
|
name: "expose-affinity-service",
|
2015-07-31 17:34:52 +00:00
|
|
|
args: []string{"service", "baz"},
|
|
|
|
ns: "test",
|
|
|
|
calls: map[string]string{
|
|
|
|
"GET": "/namespaces/test/services/baz",
|
|
|
|
"POST": "/namespaces/test/services",
|
|
|
|
},
|
|
|
|
input: &api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Selector: map[string]string{"app": "go"},
|
|
|
|
},
|
|
|
|
},
|
2015-10-02 23:52:42 +00:00
|
|
|
flags: map[string]string{"selector": "func=stream", "protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test", "type": "LoadBalancer", "session-affinity": "ClientIP", "dry-run": "true"},
|
2015-07-31 17:34:52 +00:00
|
|
|
output: &api.Service{
|
2015-09-10 10:13:00 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "", Labels: map[string]string{"svc": "test"}},
|
2015-07-31 17:34:52 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Ports: []api.ServicePort{
|
|
|
|
{
|
2015-09-10 10:13:00 +00:00
|
|
|
Protocol: api.ProtocolUDP,
|
2015-07-31 17:34:52 +00:00
|
|
|
Port: 14,
|
|
|
|
TargetPort: util.NewIntOrStringFromInt(14),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Selector: map[string]string{"func": "stream"},
|
|
|
|
Type: api.ServiceTypeLoadBalancer,
|
|
|
|
SessionAffinity: api.ServiceAffinityClientIP,
|
|
|
|
},
|
|
|
|
},
|
2015-10-02 23:52:42 +00:00
|
|
|
status: 200,
|
2015-09-10 10:13:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "expose-external-service",
|
|
|
|
args: []string{"service", "baz"},
|
|
|
|
ns: "test",
|
|
|
|
calls: map[string]string{
|
|
|
|
"GET": "/namespaces/test/services/baz",
|
|
|
|
"POST": "/namespaces/test/services",
|
|
|
|
},
|
|
|
|
input: &api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Ports: []api.ServicePort{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Even if we specify --selector, since service/test doesn't need one it will ignore it
|
2015-10-02 23:52:42 +00:00
|
|
|
flags: map[string]string{"selector": "svc=fromexternal", "port": "90", "labels": "svc=fromexternal", "name": "frombaz", "generator": "service/test", "dry-run": "true"},
|
2015-09-10 10:13:00 +00:00
|
|
|
output: &api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "frombaz", Namespace: "", Labels: map[string]string{"svc": "fromexternal"}},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Ports: []api.ServicePort{
|
|
|
|
{
|
|
|
|
Protocol: api.ProtocolTCP,
|
|
|
|
Port: 90,
|
|
|
|
TargetPort: util.NewIntOrStringFromInt(90),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-10-02 23:52:42 +00:00
|
|
|
status: 200,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "expose-from-file",
|
|
|
|
args: []string{},
|
|
|
|
ns: "test",
|
|
|
|
calls: map[string]string{
|
|
|
|
"GET": "/namespaces/test/services/redis-master",
|
|
|
|
"POST": "/namespaces/test/services",
|
|
|
|
},
|
|
|
|
input: &api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "redis-master", Namespace: "test", ResourceVersion: "12"},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Selector: map[string]string{"app": "go"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
flags: map[string]string{"filename": "../../../examples/guestbook/redis-master-service.yaml", "selector": "func=stream", "protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test", "dry-run": "true"},
|
|
|
|
output: &api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo", Labels: map[string]string{"svc": "test"}},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Ports: []api.ServicePort{
|
|
|
|
{
|
|
|
|
Protocol: api.ProtocolUDP,
|
|
|
|
Port: 14,
|
|
|
|
TargetPort: util.NewIntOrStringFromInt(14),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Selector: map[string]string{"func": "stream"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
status: 200,
|
2015-07-31 17:34:52 +00:00
|
|
|
},
|
2015-09-29 11:39:10 +00:00
|
|
|
{
|
|
|
|
name: "truncate-name",
|
|
|
|
args: []string{"pod", "a-name-that-is-toooo-big-for-a-service"},
|
|
|
|
ns: "test",
|
|
|
|
calls: map[string]string{
|
|
|
|
"GET": "/namespaces/test/pods/a-name-that-is-toooo-big-for-a-service",
|
|
|
|
"POST": "/namespaces/test/services",
|
|
|
|
},
|
|
|
|
input: &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
|
|
|
|
},
|
|
|
|
flags: map[string]string{"selector": "svc=frompod", "port": "90", "labels": "svc=frompod", "generator": "service/v2"},
|
|
|
|
output: &api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "a-name-that-is-toooo-big", Namespace: "", Labels: map[string]string{"svc": "frompod"}},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Ports: []api.ServicePort{
|
|
|
|
{
|
|
|
|
Protocol: api.ProtocolTCP,
|
|
|
|
Port: 90,
|
|
|
|
TargetPort: util.NewIntOrStringFromInt(90),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Selector: map[string]string{"svc": "frompod"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "service \"a-name-that-is-toooo-big\" exposed",
|
|
|
|
status: 200,
|
|
|
|
},
|
2015-05-07 15:30:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
f, tf, codec := NewAPIFactory()
|
2015-10-02 23:52:42 +00:00
|
|
|
tf.Printer = &kubectl.JSONPrinter{}
|
2015-09-11 20:47:53 +00:00
|
|
|
tf.Client = &fake.RESTClient{
|
2015-05-07 15:30:28 +00:00
|
|
|
Codec: codec,
|
2015-09-11 20:47:53 +00:00
|
|
|
Client: fake.HTTPClientFunc(func(req *http.Request) (*http.Response, error) {
|
2015-05-07 15:30:28 +00:00
|
|
|
switch p, m := req.URL.Path, req.Method; {
|
2015-05-26 12:03:11 +00:00
|
|
|
case p == test.calls[m] && m == "GET":
|
2015-05-07 15:30:28 +00:00
|
|
|
return &http.Response{StatusCode: test.status, Body: objBody(codec, test.input)}, nil
|
2015-05-26 12:03:11 +00:00
|
|
|
case p == test.calls[m] && m == "POST":
|
2015-05-07 15:30:28 +00:00
|
|
|
return &http.Response{StatusCode: test.status, Body: objBody(codec, test.output)}, nil
|
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
2015-05-26 12:03:11 +00:00
|
|
|
tf.Namespace = test.ns
|
2015-05-07 15:30:28 +00:00
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
|
|
|
|
cmd := NewCmdExposeService(f, buf)
|
|
|
|
cmd.SetOutput(buf)
|
|
|
|
for flag, value := range test.flags {
|
|
|
|
cmd.Flags().Set(flag, value)
|
|
|
|
}
|
|
|
|
cmd.Run(cmd, test.args)
|
2015-09-10 10:13:00 +00:00
|
|
|
|
2015-10-02 23:52:42 +00:00
|
|
|
out := buf.String()
|
|
|
|
if _, ok := test.flags["dry-run"]; ok {
|
|
|
|
buf.Reset()
|
|
|
|
if err := tf.Printer.PrintObj(test.output, buf); err != nil {
|
|
|
|
t.Errorf("%s: Unexpected error: %v", test.name, err)
|
|
|
|
continue
|
2015-08-11 08:38:27 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 23:52:42 +00:00
|
|
|
test.expected = buf.String()
|
|
|
|
}
|
2015-08-11 08:38:27 +00:00
|
|
|
|
|
|
|
if !strings.Contains(out, test.expected) {
|
2015-10-02 23:52:42 +00:00
|
|
|
t.Errorf("%s: Unexpected output! Expected\n%s\ngot\n%s", test.name, test.expected, out)
|
2015-08-11 08:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|