2014-08-14 19:48:34 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. 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 endpoint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-09-03 21:16:00 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
2014-08-29 02:31:41 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2014-08-14 19:48:34 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetEndpoints(t *testing.T) {
|
|
|
|
registry := ®istrytest.ServiceRegistry{
|
|
|
|
Endpoints: api.Endpoints{
|
2014-10-22 17:02:02 +00:00
|
|
|
TypeMeta: api.TypeMeta{Name: "foo"},
|
2014-08-14 19:48:34 +00:00
|
|
|
Endpoints: []string{"127.0.0.1:9000"},
|
|
|
|
},
|
|
|
|
}
|
2014-09-08 21:40:56 +00:00
|
|
|
storage := NewREST(registry)
|
2014-09-25 18:35:10 +00:00
|
|
|
ctx := api.NewContext()
|
|
|
|
obj, err := storage.Get(ctx, "foo")
|
2014-08-14 19:48:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %#v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual([]string{"127.0.0.1:9000"}, obj.(*api.Endpoints).Endpoints) {
|
|
|
|
t.Errorf("unexpected endpoints: %#v", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetEndpointsMissingService(t *testing.T) {
|
|
|
|
registry := ®istrytest.ServiceRegistry{
|
2014-09-03 21:16:00 +00:00
|
|
|
Err: errors.NewNotFound("service", "foo"),
|
2014-08-14 19:48:34 +00:00
|
|
|
}
|
2014-09-08 21:40:56 +00:00
|
|
|
storage := NewREST(registry)
|
2014-09-25 18:35:10 +00:00
|
|
|
ctx := api.NewContext()
|
2014-08-14 19:48:34 +00:00
|
|
|
// returns service not found
|
2014-09-25 18:35:10 +00:00
|
|
|
_, err := storage.Get(ctx, "foo")
|
2014-09-03 21:16:00 +00:00
|
|
|
if !errors.IsNotFound(err) || !reflect.DeepEqual(err, errors.NewNotFound("service", "foo")) {
|
2014-08-14 19:48:34 +00:00
|
|
|
t.Errorf("expected NotFound error, got %#v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns empty endpoints
|
|
|
|
registry.Err = nil
|
|
|
|
registry.Service = &api.Service{
|
2014-10-22 17:02:02 +00:00
|
|
|
TypeMeta: api.TypeMeta{Name: "foo"},
|
2014-08-14 19:48:34 +00:00
|
|
|
}
|
2014-09-25 18:35:10 +00:00
|
|
|
obj, err := storage.Get(ctx, "foo")
|
2014-08-14 19:48:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if obj.(*api.Endpoints).Endpoints != nil {
|
|
|
|
t.Errorf("unexpected endpoints: %#v", obj)
|
|
|
|
}
|
|
|
|
}
|
2014-08-29 02:31:41 +00:00
|
|
|
|
|
|
|
func TestEndpointsRegistryList(t *testing.T) {
|
|
|
|
registry := registrytest.NewServiceRegistry()
|
2014-09-08 21:40:56 +00:00
|
|
|
storage := NewREST(registry)
|
2014-08-29 02:31:41 +00:00
|
|
|
registry.EndpointsList = api.EndpointsList{
|
2014-10-07 20:51:28 +00:00
|
|
|
TypeMeta: api.TypeMeta{ResourceVersion: "1"},
|
2014-08-29 02:31:41 +00:00
|
|
|
Items: []api.Endpoints{
|
2014-10-22 17:02:02 +00:00
|
|
|
{TypeMeta: api.TypeMeta{Name: "foo"}},
|
|
|
|
{TypeMeta: api.TypeMeta{Name: "bar"}},
|
2014-08-29 02:31:41 +00:00
|
|
|
},
|
|
|
|
}
|
2014-09-25 18:35:10 +00:00
|
|
|
ctx := api.NewContext()
|
|
|
|
s, _ := storage.List(ctx, labels.Everything(), labels.Everything())
|
2014-08-29 02:31:41 +00:00
|
|
|
sl := s.(*api.EndpointsList)
|
|
|
|
if len(sl.Items) != 2 {
|
|
|
|
t.Fatalf("Expected 2 endpoints, but got %v", len(sl.Items))
|
|
|
|
}
|
2014-10-22 17:02:02 +00:00
|
|
|
if e, a := "foo", sl.Items[0].Name; e != a {
|
2014-08-29 02:31:41 +00:00
|
|
|
t.Errorf("Expected %v, but got %v", e, a)
|
|
|
|
}
|
2014-10-22 17:02:02 +00:00
|
|
|
if e, a := "bar", sl.Items[1].Name; e != a {
|
2014-08-29 02:31:41 +00:00
|
|
|
t.Errorf("Expected %v, but got %v", e, a)
|
|
|
|
}
|
2014-10-07 20:51:28 +00:00
|
|
|
if sl.ResourceVersion != "1" {
|
2014-08-29 02:31:41 +00:00
|
|
|
t.Errorf("Unexpected resource version: %#v", sl)
|
|
|
|
}
|
|
|
|
}
|