Updating unit tests for pkg/client so that they pass with v1beta3 api

This commit is contained in:
nikhiljindal
2015-03-24 16:40:18 -07:00
parent f584069573
commit ba3746172f
10 changed files with 780 additions and 581 deletions

View File

@@ -27,8 +27,6 @@ import (
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
@@ -37,8 +35,6 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/version"
)
// TODO: Move this to a common place, it's needed in multiple tests.
const apiPath = "/api/v1beta1"
const nameRequiredError = "name is required parameter to Get"
type testRequest struct {
@@ -135,14 +131,14 @@ func (c *testClient) ValidateCommon(t *testing.T, err error) {
// We check the query manually, so blank it out so that FakeHandler.ValidateRequest
// won't check it.
c.handler.RequestReceived.URL.RawQuery = ""
c.handler.ValidateRequest(t, path.Join(apiPath, c.Request.Path), c.Request.Method, requestBody)
c.handler.ValidateRequest(t, path.Join(c.Request.Path), c.Request.Method, requestBody)
for key, values := range c.Request.Query {
validator, ok := c.QueryValidator[key]
if !ok {
switch key {
case "labels":
case api.LabelSelectorQueryParam(testapi.Version()):
validator = validateLabels
case "fields":
case api.FieldSelectorQueryParam(testapi.Version()):
validator = validateFields
default:
validator = func(a, b string) bool { return a == b }
@@ -186,49 +182,13 @@ func buildQueryValues(namespace string, query url.Values) url.Values {
}
}
if len(namespace) > 0 {
if testapi.Version() == "v1beta1" || testapi.Version() == "v1beta2" {
if api.PreV1Beta3(testapi.Version()) {
v.Set("namespace", namespace)
}
}
return v
}
func TestListEmptyPods(t *testing.T) {
ns := api.NamespaceDefault
c := &testClient{
Request: testRequest{Method: "GET", Path: buildResourcePath(ns, "/pods"), Query: buildQueryValues(ns, nil)},
Response: Response{StatusCode: 200, Body: &api.PodList{}},
}
podList, err := c.Setup().Pods(ns).List(labels.Everything())
c.Validate(t, podList, err)
}
func TestListPods(t *testing.T) {
ns := api.NamespaceDefault
c := &testClient{
Request: testRequest{Method: "GET", Path: buildResourcePath(ns, "/pods"), Query: buildQueryValues(ns, nil)},
Response: Response{StatusCode: 200,
Body: &api.PodList{
Items: []api.Pod{
{
Status: api.PodStatus{
Phase: api.PodRunning,
},
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
},
},
},
},
},
}
receivedPodList, err := c.Setup().Pods(ns).List(labels.Everything())
c.Validate(t, receivedPodList, err)
}
func validateLabels(a, b string) bool {
sA, eA := labels.Parse(a)
if eA != nil {
@@ -247,421 +207,15 @@ func validateFields(a, b string) bool {
return sA.String() == sB.String()
}
func TestListPodsLabels(t *testing.T) {
ns := api.NamespaceDefault
c := &testClient{
Request: testRequest{Method: "GET", Path: buildResourcePath(ns, "/pods"), Query: buildQueryValues(ns, url.Values{"labels": []string{"foo=bar,name=baz"}})},
Response: Response{
StatusCode: 200,
Body: &api.PodList{
Items: []api.Pod{
{
Status: api.PodStatus{
Phase: api.PodRunning,
},
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
},
},
},
},
},
}
c.Setup()
c.QueryValidator["labels"] = validateLabels
selector := labels.Set{"foo": "bar", "name": "baz"}.AsSelector()
receivedPodList, err := c.Pods(ns).List(selector)
c.Validate(t, receivedPodList, err)
}
func TestGetPod(t *testing.T) {
ns := api.NamespaceDefault
c := &testClient{
Request: testRequest{Method: "GET", Path: buildResourcePath(ns, "/pods/foo"), Query: buildQueryValues(ns, nil)},
Response: Response{
StatusCode: 200,
Body: &api.Pod{
Status: api.PodStatus{
Phase: api.PodRunning,
},
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
},
},
},
}
receivedPod, err := c.Setup().Pods(ns).Get("foo")
c.Validate(t, receivedPod, err)
}
func TestGetPodWithNoName(t *testing.T) {
ns := api.NamespaceDefault
c := &testClient{Error: true}
receivedPod, err := c.Setup().Pods(ns).Get("")
if (err != nil) && (err.Error() != nameRequiredError) {
t.Errorf("Expected error: %v, but got %v", nameRequiredError, err)
}
c.Validate(t, receivedPod, err)
}
func TestDeletePod(t *testing.T) {
ns := api.NamespaceDefault
c := &testClient{
Request: testRequest{Method: "DELETE", Path: buildResourcePath(ns, "/pods/foo"), Query: buildQueryValues(ns, nil)},
Response: Response{StatusCode: 200},
}
err := c.Setup().Pods(ns).Delete("foo")
c.Validate(t, nil, err)
}
func TestCreatePod(t *testing.T) {
ns := api.NamespaceDefault
requestPod := &api.Pod{
Status: api.PodStatus{
Phase: api.PodRunning,
},
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
},
}
c := &testClient{
Request: testRequest{Method: "POST", Path: buildResourcePath(ns, "/pods"), Query: buildQueryValues(ns, nil), Body: requestPod},
Response: Response{
StatusCode: 200,
Body: requestPod,
},
}
receivedPod, err := c.Setup().Pods(ns).Create(requestPod)
c.Validate(t, receivedPod, err)
}
func TestUpdatePod(t *testing.T) {
ns := api.NamespaceDefault
requestPod := &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "foo",
ResourceVersion: "1",
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
},
Status: api.PodStatus{
Phase: api.PodRunning,
},
}
c := &testClient{
Request: testRequest{Method: "PUT", Path: buildResourcePath(ns, "/pods/foo"), Query: buildQueryValues(ns, nil)},
Response: Response{StatusCode: 200, Body: requestPod},
}
receivedPod, err := c.Setup().Pods(ns).Update(requestPod)
c.Validate(t, receivedPod, err)
}
func TestListControllers(t *testing.T) {
c := &testClient{
Request: testRequest{Method: "GET", Path: "/replicationControllers"},
Response: Response{StatusCode: 200,
Body: &api.ReplicationControllerList{
Items: []api.ReplicationController{
{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
},
Spec: api.ReplicationControllerSpec{
Replicas: 2,
Template: &api.PodTemplateSpec{},
},
},
},
},
},
}
receivedControllerList, err := c.Setup().ReplicationControllers(api.NamespaceAll).List(labels.Everything())
c.Validate(t, receivedControllerList, err)
}
func TestGetController(t *testing.T) {
ns := api.NamespaceDefault
c := &testClient{
Request: testRequest{Method: "GET", Path: buildResourcePath(ns, "/replicationControllers/foo"), Query: buildQueryValues(ns, nil)},
Response: Response{
StatusCode: 200,
Body: &api.ReplicationController{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
},
Spec: api.ReplicationControllerSpec{
Replicas: 2,
Template: &api.PodTemplateSpec{},
},
},
},
}
receivedController, err := c.Setup().ReplicationControllers(ns).Get("foo")
c.Validate(t, receivedController, err)
}
func TestGetControllerWithNoName(t *testing.T) {
ns := api.NamespaceDefault
c := &testClient{Error: true}
receivedPod, err := c.Setup().ReplicationControllers(ns).Get("")
if (err != nil) && (err.Error() != nameRequiredError) {
t.Errorf("Expected error: %v, but got %v", nameRequiredError, err)
}
c.Validate(t, receivedPod, err)
}
func TestUpdateController(t *testing.T) {
ns := api.NamespaceDefault
requestController := &api.ReplicationController{
ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "1"},
}
c := &testClient{
Request: testRequest{Method: "PUT", Path: buildResourcePath(ns, "/replicationControllers/foo"), Query: buildQueryValues(ns, nil)},
Response: Response{
StatusCode: 200,
Body: &api.ReplicationController{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
},
Spec: api.ReplicationControllerSpec{
Replicas: 2,
Template: &api.PodTemplateSpec{},
},
},
},
}
receivedController, err := c.Setup().ReplicationControllers(ns).Update(requestController)
c.Validate(t, receivedController, err)
}
func TestDeleteController(t *testing.T) {
ns := api.NamespaceDefault
c := &testClient{
Request: testRequest{Method: "DELETE", Path: buildResourcePath(ns, "/replicationControllers/foo"), Query: buildQueryValues(ns, nil)},
Response: Response{StatusCode: 200},
}
err := c.Setup().ReplicationControllers(ns).Delete("foo")
c.Validate(t, nil, err)
}
func TestCreateController(t *testing.T) {
ns := api.NamespaceDefault
requestController := &api.ReplicationController{
ObjectMeta: api.ObjectMeta{Name: "foo"},
}
c := &testClient{
Request: testRequest{Method: "POST", Path: buildResourcePath(ns, "/replicationControllers"), Body: requestController, Query: buildQueryValues(ns, nil)},
Response: Response{
StatusCode: 200,
Body: &api.ReplicationController{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
},
Spec: api.ReplicationControllerSpec{
Replicas: 2,
Template: &api.PodTemplateSpec{},
},
},
},
}
receivedController, err := c.Setup().ReplicationControllers(ns).Create(requestController)
c.Validate(t, receivedController, err)
}
func body(obj runtime.Object, raw *string) *string {
if obj != nil {
bs, _ := latest.Codec.Encode(obj)
bs, _ := testapi.Codec().Encode(obj)
body := string(bs)
return &body
}
return raw
}
func TestListServices(t *testing.T) {
ns := api.NamespaceDefault
c := &testClient{
Request: testRequest{Method: "GET", Path: buildResourcePath(ns, "/services"), Query: buildQueryValues(ns, nil)},
Response: Response{StatusCode: 200,
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",
},
},
},
},
},
},
}
receivedServiceList, err := c.Setup().Services(ns).List(labels.Everything())
t.Logf("received services: %v %#v", err, receivedServiceList)
c.Validate(t, receivedServiceList, err)
}
func TestListServicesLabels(t *testing.T) {
ns := api.NamespaceDefault
c := &testClient{
Request: testRequest{Method: "GET", Path: buildResourcePath(ns, "/services"), Query: buildQueryValues(ns, url.Values{"labels": []string{"foo=bar,name=baz"}})},
Response: Response{StatusCode: 200,
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",
},
},
},
},
},
},
}
c.Setup()
c.QueryValidator["labels"] = validateLabels
selector := labels.Set{"foo": "bar", "name": "baz"}.AsSelector()
receivedServiceList, err := c.Services(ns).List(selector)
c.Validate(t, receivedServiceList, err)
}
func TestGetService(t *testing.T) {
ns := api.NamespaceDefault
c := &testClient{
Request: testRequest{Method: "GET", Path: buildResourcePath(ns, "/services/1"), Query: buildQueryValues(ns, nil)},
Response: Response{StatusCode: 200, Body: &api.Service{ObjectMeta: api.ObjectMeta{Name: "service-1"}}},
}
response, err := c.Setup().Services(ns).Get("1")
c.Validate(t, response, err)
}
func TestGetServiceWithNoName(t *testing.T) {
ns := api.NamespaceDefault
c := &testClient{Error: true}
receivedPod, err := c.Setup().Services(ns).Get("")
if (err != nil) && (err.Error() != nameRequiredError) {
t.Errorf("Expected error: %v, but got %v", nameRequiredError, err)
}
c.Validate(t, receivedPod, err)
}
func TestCreateService(t *testing.T) {
ns := api.NamespaceDefault
c := &testClient{
Request: testRequest{Method: "POST", Path: buildResourcePath(ns, "/services"), Body: &api.Service{ObjectMeta: api.ObjectMeta{Name: "service-1"}}, Query: buildQueryValues(ns, nil)},
Response: Response{StatusCode: 200, Body: &api.Service{ObjectMeta: api.ObjectMeta{Name: "service-1"}}},
}
response, err := c.Setup().Services(ns).Create(&api.Service{ObjectMeta: api.ObjectMeta{Name: "service-1"}})
c.Validate(t, response, err)
}
func TestUpdateService(t *testing.T) {
ns := api.NamespaceDefault
svc := &api.Service{ObjectMeta: api.ObjectMeta{Name: "service-1", ResourceVersion: "1"}}
c := &testClient{
Request: testRequest{Method: "PUT", Path: buildResourcePath(ns, "/services/service-1"), Body: svc, Query: buildQueryValues(ns, nil)},
Response: Response{StatusCode: 200, Body: svc},
}
response, err := c.Setup().Services(ns).Update(svc)
c.Validate(t, response, err)
}
func TestDeleteService(t *testing.T) {
ns := api.NamespaceDefault
c := &testClient{
Request: testRequest{Method: "DELETE", Path: buildResourcePath(ns, "/services/1"), Query: buildQueryValues(ns, nil)},
Response: Response{StatusCode: 200},
}
err := c.Setup().Services(ns).Delete("1")
c.Validate(t, nil, err)
}
func TestListEndpooints(t *testing.T) {
ns := api.NamespaceDefault
c := &testClient{
Request: testRequest{Method: "GET", Path: buildResourcePath(ns, "/endpoints"), Query: buildQueryValues(ns, nil)},
Response: Response{StatusCode: 200,
Body: &api.EndpointsList{
Items: []api.Endpoints{
{
ObjectMeta: api.ObjectMeta{Name: "endpoint-1"},
Endpoints: []api.Endpoint{
{IP: "10.245.1.2", Port: 8080}, {IP: "10.245.1.3", Port: 8080}},
},
},
},
},
}
receivedEndpointsList, err := c.Setup().Endpoints(ns).List(labels.Everything())
c.Validate(t, receivedEndpointsList, err)
}
func TestGetEndpoints(t *testing.T) {
ns := api.NamespaceDefault
c := &testClient{
Request: testRequest{Method: "GET", Path: buildResourcePath(ns, "/endpoints/endpoint-1"), Query: buildQueryValues(ns, nil)},
Response: Response{StatusCode: 200, Body: &api.Endpoints{ObjectMeta: api.ObjectMeta{Name: "endpoint-1"}}},
}
response, err := c.Setup().Endpoints(ns).Get("endpoint-1")
c.Validate(t, response, err)
}
func TestGetEndpointWithNoName(t *testing.T) {
ns := api.NamespaceDefault
c := &testClient{Error: true}
receivedPod, err := c.Setup().Endpoints(ns).Get("")
if (err != nil) && (err.Error() != nameRequiredError) {
t.Errorf("Expected error: %v, but got %v", nameRequiredError, err)
}
c.Validate(t, receivedPod, err)
}
func TestGetServerVersion(t *testing.T) {
expect := version.Info{
Major: "foo",
@@ -711,97 +265,3 @@ func TestGetServerAPIVersions(t *testing.T) {
t.Errorf("expected %v, got %v", e, a)
}
}
func TestListMinions(t *testing.T) {
c := &testClient{
Request: testRequest{Method: "GET", Path: "/minions"},
Response: Response{StatusCode: 200, Body: &api.NodeList{ListMeta: api.ListMeta{ResourceVersion: "1"}}},
}
response, err := c.Setup().Nodes().List()
c.Validate(t, response, err)
}
func TestGetMinion(t *testing.T) {
c := &testClient{
Request: testRequest{Method: "GET", Path: "/minions/1"},
Response: Response{StatusCode: 200, Body: &api.Node{ObjectMeta: api.ObjectMeta{Name: "minion-1"}}},
}
response, err := c.Setup().Nodes().Get("1")
c.Validate(t, response, err)
}
func TestGetMinionWithNoName(t *testing.T) {
c := &testClient{Error: true}
receivedPod, err := c.Setup().Nodes().Get("")
if (err != nil) && (err.Error() != nameRequiredError) {
t.Errorf("Expected error: %v, but got %v", nameRequiredError, err)
}
c.Validate(t, receivedPod, err)
}
func TestCreateMinion(t *testing.T) {
requestMinion := &api.Node{
ObjectMeta: api.ObjectMeta{
Name: "minion-1",
},
Spec: api.NodeSpec{
Capacity: api.ResourceList{
api.ResourceCPU: resource.MustParse("1000m"),
api.ResourceMemory: resource.MustParse("1Mi"),
},
Unschedulable: false,
},
}
c := &testClient{
Request: testRequest{Method: "POST", Path: "/minions", Body: requestMinion},
Response: Response{
StatusCode: 200,
Body: requestMinion,
},
}
receivedMinion, err := c.Setup().Nodes().Create(requestMinion)
c.Validate(t, receivedMinion, err)
}
func TestDeleteMinion(t *testing.T) {
c := &testClient{
Request: testRequest{Method: "DELETE", Path: "/minions/foo"},
Response: Response{StatusCode: 200},
}
err := c.Setup().Nodes().Delete("foo")
c.Validate(t, nil, err)
}
func TestUpdateMinion(t *testing.T) {
requestMinion := &api.Node{
ObjectMeta: api.ObjectMeta{
Name: "foo",
ResourceVersion: "1",
},
Spec: api.NodeSpec{
Capacity: api.ResourceList{
api.ResourceCPU: resource.MustParse("1000m"),
api.ResourceMemory: resource.MustParse("1Mi"),
},
Unschedulable: true,
},
}
c := &testClient{
Request: testRequest{Method: "PUT", Path: "/minions/foo"},
Response: Response{StatusCode: 200, Body: requestMinion},
}
response, err := c.Setup().Nodes().Update(requestMinion)
c.Validate(t, response, err)
}
func TestNewMinionPath(t *testing.T) {
c := &testClient{
Request: testRequest{Method: "DELETE", Path: "/nodes/foo"},
Response: Response{StatusCode: 200},
}
cl := c.Setup()
cl.apiVersion = "v1beta3"
err := cl.Nodes().Delete("foo")
c.Validate(t, nil, err)
}