2015-03-24 23:40:18 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
2015-03-24 23:40:18 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2015-12-04 01:22:48 +00:00
|
|
|
package unversioned_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
. "k8s.io/kubernetes/pkg/client/unversioned"
|
|
|
|
"k8s.io/kubernetes/pkg/client/unversioned/testclient/simple"
|
|
|
|
)
|
2015-03-24 23:40:18 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/api/testapi"
|
2015-10-09 01:32:15 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/labels"
|
2015-03-24 23:40:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestListServices(t *testing.T) {
|
|
|
|
ns := api.NamespaceDefault
|
2015-12-04 01:22:48 +00:00
|
|
|
c := &simple.Client{
|
|
|
|
Request: simple.Request{
|
2015-03-24 23:40:18 +00:00
|
|
|
Method: "GET",
|
2015-09-04 07:06:01 +00:00
|
|
|
Path: testapi.Default.ResourcePath("services", ns, ""),
|
2015-12-04 01:22:48 +00:00
|
|
|
Query: simple.BuildQueryValues(nil)},
|
|
|
|
Response: simple.Response{StatusCode: 200,
|
2015-03-24 23:40:18 +00:00
|
|
|
Body: &api.ServiceList{
|
|
|
|
Items: []api.Service{
|
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "name",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Selector: map[string]string{
|
|
|
|
"one": "two",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-12-10 09:39:03 +00:00
|
|
|
receivedServiceList, err := c.Setup(t).Services(ns).List(api.ListOptions{})
|
2015-12-05 12:49:36 +00:00
|
|
|
defer c.Close()
|
2015-03-24 23:40:18 +00:00
|
|
|
t.Logf("received services: %v %#v", err, receivedServiceList)
|
|
|
|
c.Validate(t, receivedServiceList, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListServicesLabels(t *testing.T) {
|
|
|
|
ns := api.NamespaceDefault
|
2015-11-30 20:28:48 +00:00
|
|
|
labelSelectorQueryParamName := unversioned.LabelSelectorQueryParam(testapi.Default.GroupVersion().String())
|
2015-12-04 01:22:48 +00:00
|
|
|
c := &simple.Client{
|
|
|
|
Request: simple.Request{
|
2015-03-24 23:40:18 +00:00
|
|
|
Method: "GET",
|
2015-09-04 07:06:01 +00:00
|
|
|
Path: testapi.Default.ResourcePath("services", ns, ""),
|
2015-12-04 01:22:48 +00:00
|
|
|
Query: simple.BuildQueryValues(url.Values{labelSelectorQueryParamName: []string{"foo=bar,name=baz"}})},
|
|
|
|
Response: simple.Response{StatusCode: 200,
|
2015-03-24 23:40:18 +00:00
|
|
|
Body: &api.ServiceList{
|
|
|
|
Items: []api.Service{
|
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "name",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Selector: map[string]string{
|
|
|
|
"one": "two",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-09-04 07:06:01 +00:00
|
|
|
c.Setup(t)
|
2015-12-05 12:49:36 +00:00
|
|
|
defer c.Close()
|
2015-12-04 01:22:48 +00:00
|
|
|
c.QueryValidator[labelSelectorQueryParamName] = simple.ValidateLabels
|
2015-03-24 23:40:18 +00:00
|
|
|
selector := labels.Set{"foo": "bar", "name": "baz"}.AsSelector()
|
2015-12-10 09:39:03 +00:00
|
|
|
options := api.ListOptions{LabelSelector: selector}
|
2015-12-02 11:12:57 +00:00
|
|
|
receivedServiceList, err := c.Services(ns).List(options)
|
2015-03-24 23:40:18 +00:00
|
|
|
c.Validate(t, receivedServiceList, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetService(t *testing.T) {
|
|
|
|
ns := api.NamespaceDefault
|
2015-12-04 01:22:48 +00:00
|
|
|
c := &simple.Client{
|
|
|
|
Request: simple.Request{
|
2015-03-24 23:40:18 +00:00
|
|
|
Method: "GET",
|
2015-09-04 07:06:01 +00:00
|
|
|
Path: testapi.Default.ResourcePath("services", ns, "1"),
|
2015-12-04 01:22:48 +00:00
|
|
|
Query: simple.BuildQueryValues(nil)},
|
|
|
|
Response: simple.Response{StatusCode: 200, Body: &api.Service{ObjectMeta: api.ObjectMeta{Name: "service-1"}}},
|
2015-03-24 23:40:18 +00:00
|
|
|
}
|
2015-09-04 07:06:01 +00:00
|
|
|
response, err := c.Setup(t).Services(ns).Get("1")
|
2015-12-05 12:49:36 +00:00
|
|
|
defer c.Close()
|
2015-03-24 23:40:18 +00:00
|
|
|
c.Validate(t, response, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetServiceWithNoName(t *testing.T) {
|
|
|
|
ns := api.NamespaceDefault
|
2015-12-04 01:22:48 +00:00
|
|
|
c := &simple.Client{Error: true}
|
2015-09-04 07:06:01 +00:00
|
|
|
receivedPod, err := c.Setup(t).Services(ns).Get("")
|
2015-12-05 12:49:36 +00:00
|
|
|
defer c.Close()
|
2015-12-04 01:22:48 +00:00
|
|
|
if (err != nil) && (err.Error() != simple.NameRequiredError) {
|
|
|
|
t.Errorf("Expected error: %v, but got %v", simple.NameRequiredError, err)
|
2015-03-24 23:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Validate(t, receivedPod, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateService(t *testing.T) {
|
|
|
|
ns := api.NamespaceDefault
|
2015-12-04 01:22:48 +00:00
|
|
|
c := &simple.Client{
|
|
|
|
Request: simple.Request{
|
2015-03-24 23:40:18 +00:00
|
|
|
Method: "POST",
|
2015-09-04 07:06:01 +00:00
|
|
|
Path: testapi.Default.ResourcePath("services", ns, ""),
|
2015-03-24 23:40:18 +00:00
|
|
|
Body: &api.Service{ObjectMeta: api.ObjectMeta{Name: "service-1"}},
|
2015-12-04 01:22:48 +00:00
|
|
|
Query: simple.BuildQueryValues(nil)},
|
|
|
|
Response: simple.Response{StatusCode: 200, Body: &api.Service{ObjectMeta: api.ObjectMeta{Name: "service-1"}}},
|
2015-03-24 23:40:18 +00:00
|
|
|
}
|
2015-09-04 07:06:01 +00:00
|
|
|
response, err := c.Setup(t).Services(ns).Create(&api.Service{ObjectMeta: api.ObjectMeta{Name: "service-1"}})
|
2015-12-05 12:49:36 +00:00
|
|
|
defer c.Close()
|
2015-03-24 23:40:18 +00:00
|
|
|
c.Validate(t, response, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateService(t *testing.T) {
|
|
|
|
ns := api.NamespaceDefault
|
|
|
|
svc := &api.Service{ObjectMeta: api.ObjectMeta{Name: "service-1", ResourceVersion: "1"}}
|
2015-12-04 01:22:48 +00:00
|
|
|
c := &simple.Client{
|
|
|
|
Request: simple.Request{Method: "PUT", Path: testapi.Default.ResourcePath("services", ns, "service-1"), Body: svc, Query: simple.BuildQueryValues(nil)},
|
|
|
|
Response: simple.Response{StatusCode: 200, Body: svc},
|
2015-03-24 23:40:18 +00:00
|
|
|
}
|
2015-09-04 07:06:01 +00:00
|
|
|
response, err := c.Setup(t).Services(ns).Update(svc)
|
2015-12-05 12:49:36 +00:00
|
|
|
defer c.Close()
|
2015-03-24 23:40:18 +00:00
|
|
|
c.Validate(t, response, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteService(t *testing.T) {
|
|
|
|
ns := api.NamespaceDefault
|
2015-12-04 01:22:48 +00:00
|
|
|
c := &simple.Client{
|
|
|
|
Request: simple.Request{Method: "DELETE", Path: testapi.Default.ResourcePath("services", ns, "1"), Query: simple.BuildQueryValues(nil)},
|
|
|
|
Response: simple.Response{StatusCode: 200},
|
2015-03-24 23:40:18 +00:00
|
|
|
}
|
2015-09-04 07:06:01 +00:00
|
|
|
err := c.Setup(t).Services(ns).Delete("1")
|
2015-12-05 12:49:36 +00:00
|
|
|
defer c.Close()
|
2015-03-24 23:40:18 +00:00
|
|
|
c.Validate(t, nil, err)
|
|
|
|
}
|
2015-08-25 11:56:08 +00:00
|
|
|
|
2016-01-12 00:51:31 +00:00
|
|
|
func TestUpdateServiceStatus(t *testing.T) {
|
|
|
|
ns := api.NamespaceDefault
|
|
|
|
lbStatus := api.LoadBalancerStatus{
|
|
|
|
Ingress: []api.LoadBalancerIngress{
|
|
|
|
{IP: "127.0.0.1"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
requestService := &api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Namespace: ns,
|
|
|
|
ResourceVersion: "1",
|
|
|
|
},
|
|
|
|
Status: api.ServiceStatus{
|
|
|
|
LoadBalancer: lbStatus,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
c := &simple.Client{
|
|
|
|
Request: simple.Request{
|
|
|
|
Method: "PUT",
|
|
|
|
Path: testapi.Default.ResourcePath("services", ns, "foo") + "/status",
|
|
|
|
Query: simple.BuildQueryValues(nil),
|
|
|
|
},
|
|
|
|
Response: simple.Response{
|
|
|
|
StatusCode: 200,
|
|
|
|
Body: &api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: api.ServiceSpec{},
|
|
|
|
Status: api.ServiceStatus{
|
|
|
|
LoadBalancer: lbStatus,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
receivedService, err := c.Setup(t).Services(ns).UpdateStatus(requestService)
|
|
|
|
defer c.Close()
|
|
|
|
c.Validate(t, receivedService, err)
|
|
|
|
}
|
|
|
|
|
2015-08-25 11:56:08 +00:00
|
|
|
func TestServiceProxyGet(t *testing.T) {
|
|
|
|
body := "OK"
|
|
|
|
ns := api.NamespaceDefault
|
2015-12-04 01:22:48 +00:00
|
|
|
c := &simple.Client{
|
|
|
|
Request: simple.Request{
|
2015-08-25 11:56:08 +00:00
|
|
|
Method: "GET",
|
2015-11-18 03:42:03 +00:00
|
|
|
Path: testapi.Default.ResourcePath("services", ns, "service-1") + "/proxy/foo",
|
2015-12-04 01:22:48 +00:00
|
|
|
Query: simple.BuildQueryValues(url.Values{"param-name": []string{"param-value"}}),
|
2015-08-25 11:56:08 +00:00
|
|
|
},
|
2015-12-04 01:22:48 +00:00
|
|
|
Response: simple.Response{StatusCode: 200, RawBody: &body},
|
2015-08-25 11:56:08 +00:00
|
|
|
}
|
2015-11-06 17:55:31 +00:00
|
|
|
response, err := c.Setup(t).Services(ns).ProxyGet("", "service-1", "", "foo", map[string]string{"param-name": "param-value"}).DoRaw()
|
2015-12-05 12:49:36 +00:00
|
|
|
defer c.Close()
|
2015-11-06 17:55:31 +00:00
|
|
|
c.ValidateRaw(t, response, err)
|
|
|
|
|
|
|
|
// With scheme and port specified
|
2015-12-04 01:22:48 +00:00
|
|
|
c = &simple.Client{
|
|
|
|
Request: simple.Request{
|
2015-11-06 17:55:31 +00:00
|
|
|
Method: "GET",
|
2015-11-18 03:42:03 +00:00
|
|
|
Path: testapi.Default.ResourcePath("services", ns, "https:service-1:my-port") + "/proxy/foo",
|
2015-12-04 01:22:48 +00:00
|
|
|
Query: simple.BuildQueryValues(url.Values{"param-name": []string{"param-value"}}),
|
2015-11-06 17:55:31 +00:00
|
|
|
},
|
2015-12-04 01:22:48 +00:00
|
|
|
Response: simple.Response{StatusCode: 200, RawBody: &body},
|
2015-11-06 17:55:31 +00:00
|
|
|
}
|
|
|
|
response, err = c.Setup(t).Services(ns).ProxyGet("https", "service-1", "my-port", "foo", map[string]string{"param-name": "param-value"}).DoRaw()
|
2015-12-05 12:49:36 +00:00
|
|
|
defer c.Close()
|
2015-08-25 11:56:08 +00:00
|
|
|
c.ValidateRaw(t, response, err)
|
|
|
|
}
|