Improve pkg/registry/service code coverage.

pull/6/head
Deyuan Deng 2014-08-18 23:18:00 -04:00
parent 9bafb8c541
commit c104551025
2 changed files with 89 additions and 7 deletions

View File

@ -32,6 +32,7 @@ type ServiceRegistry struct {
DeletedID string DeletedID string
GottenID string GottenID string
UpdatedID string
} }
func (r *ServiceRegistry) ListServices() (api.ServiceList, error) { func (r *ServiceRegistry) ListServices() (api.ServiceList, error) {
@ -40,6 +41,7 @@ func (r *ServiceRegistry) ListServices() (api.ServiceList, error) {
func (r *ServiceRegistry) CreateService(svc api.Service) error { func (r *ServiceRegistry) CreateService(svc api.Service) error {
r.Service = &svc r.Service = &svc
r.List.Items = append(r.List.Items, svc)
return r.Err return r.Err
} }
@ -54,6 +56,7 @@ func (r *ServiceRegistry) DeleteService(id string) error {
} }
func (r *ServiceRegistry) UpdateService(svc api.Service) error { func (r *ServiceRegistry) UpdateService(svc api.Service) error {
r.UpdatedID = svc.ID
return r.Err return r.Err
} }

View File

@ -22,12 +22,13 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider" "github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/minion" "github.com/GoogleCloudPlatform/kubernetes/pkg/registry/minion"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest" "github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
) )
func TestRegistry(t *testing.T) { func TestServiceRegistryCreate(t *testing.T) {
registry := registrytest.NewServiceRegistry() registry := registrytest.NewServiceRegistry()
fakeCloud := &cloudprovider.FakeCloud{} fakeCloud := &cloudprovider.FakeCloud{}
machines := []string{"foo", "bar", "baz"} machines := []string{"foo", "bar", "baz"}
@ -37,7 +38,11 @@ func TestRegistry(t *testing.T) {
Selector: map[string]string{"bar": "baz"}, Selector: map[string]string{"bar": "baz"},
} }
c, _ := storage.Create(svc) c, _ := storage.Create(svc)
<-c created_svc := <-c
created_service := created_svc.(*api.Service)
if created_service.ID != "foo" {
t.Errorf("Expected foo, but got %v", created_service.ID)
}
if len(fakeCloud.Calls) != 0 { if len(fakeCloud.Calls) != 0 {
t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls) t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls)
} }
@ -74,6 +79,33 @@ func TestServiceStorageValidatesCreate(t *testing.T) {
} }
} }
func TestServiceRegistryUpdate(t *testing.T) {
registry := registrytest.NewServiceRegistry()
registry.CreateService(api.Service{
JSONBase: api.JSONBase{ID: "foo"},
Selector: map[string]string{"bar": "baz1"},
})
storage := NewRegistryStorage(registry, nil, nil)
c, err := storage.Update(&api.Service{
JSONBase: api.JSONBase{ID: "foo"},
Selector: map[string]string{"bar": "baz2"},
})
if c == nil {
t.Errorf("Expected non-nil channel")
}
if err != nil {
t.Errorf("Expected no error")
}
updated_svc := <-c
updated_service := updated_svc.(*api.Service)
if updated_service.ID != "foo" {
t.Errorf("Expected foo, but got %v", updated_service.ID)
}
if e, a := "foo", registry.UpdatedID; e != a {
t.Errorf("Expected %v, but got %v", e, a)
}
}
func TestServiceStorageValidatesUpdate(t *testing.T) { func TestServiceStorageValidatesUpdate(t *testing.T) {
registry := registrytest.NewServiceRegistry() registry := registrytest.NewServiceRegistry()
registry.CreateService(api.Service{ registry.CreateService(api.Service{
@ -119,7 +151,7 @@ func TestServiceRegistryExternalService(t *testing.T) {
} }
srv, err := registry.GetService(svc.ID) srv, err := registry.GetService(svc.ID)
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("Unexpected error: %v", err)
} }
if srv == nil { if srv == nil {
t.Errorf("Failed to find service: %s", svc.ID) t.Errorf("Failed to find service: %s", svc.ID)
@ -144,7 +176,7 @@ func TestServiceRegistryExternalServiceError(t *testing.T) {
t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls) t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls)
} }
if registry.Service != nil { if registry.Service != nil {
t.Errorf("expected registry.CreateService to not get called, but it got %#v", registry.Service) t.Errorf("Expected registry.CreateService to not get called, but it got %#v", registry.Service)
} }
} }
@ -164,7 +196,7 @@ func TestServiceRegistryDelete(t *testing.T) {
t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls) t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls)
} }
if e, a := "foo", registry.DeletedID; e != a { if e, a := "foo", registry.DeletedID; e != a {
t.Errorf("expected %v, but got %v", e, a) t.Errorf("Expected %v, but got %v", e, a)
} }
} }
@ -185,7 +217,7 @@ func TestServiceRegistryDeleteExternal(t *testing.T) {
t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls) t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls)
} }
if e, a := "foo", registry.DeletedID; e != a { if e, a := "foo", registry.DeletedID; e != a {
t.Errorf("expected %v, but got %v", e, a) t.Errorf("Expected %v, but got %v", e, a)
} }
} }
@ -202,7 +234,7 @@ func TestServiceRegistryMakeLinkVariables(t *testing.T) {
machine := "machine" machine := "machine"
vars, err := GetServiceEnvironmentVariables(registry, machine) vars, err := GetServiceEnvironmentVariables(registry, machine)
if err != nil { if err != nil {
t.Errorf("unexpected err: %v", err) t.Errorf("Unexpected err: %v", err)
} }
for _, v := range vars { for _, v := range vars {
if !util.IsCIdentifier(v.Name) { if !util.IsCIdentifier(v.Name) {
@ -210,3 +242,50 @@ func TestServiceRegistryMakeLinkVariables(t *testing.T) {
} }
} }
} }
func TestServiceRegistryGet(t *testing.T) {
registry := registrytest.NewServiceRegistry()
fakeCloud := &cloudprovider.FakeCloud{}
machines := []string{"foo", "bar", "baz"}
storage := NewRegistryStorage(registry, fakeCloud, minion.NewRegistry(machines))
registry.CreateService(api.Service{
JSONBase: api.JSONBase{ID: "foo"},
Selector: map[string]string{"bar": "baz"},
})
storage.Get("foo")
if len(fakeCloud.Calls) != 0 {
t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls)
}
if e, a := "foo", registry.GottenID; e != a {
t.Errorf("Expected %v, but got %v", e, a)
}
}
func TestServiceRegistryList(t *testing.T) {
registry := registrytest.NewServiceRegistry()
fakeCloud := &cloudprovider.FakeCloud{}
machines := []string{"foo", "bar", "baz"}
storage := NewRegistryStorage(registry, fakeCloud, minion.NewRegistry(machines))
registry.CreateService(api.Service{
JSONBase: api.JSONBase{ID: "foo"},
Selector: map[string]string{"bar": "baz"},
})
registry.CreateService(api.Service{
JSONBase: api.JSONBase{ID: "foo2"},
Selector: map[string]string{"bar2": "baz2"},
})
s, _ := storage.List(labels.Everything())
sl := s.(api.ServiceList)
if len(fakeCloud.Calls) != 0 {
t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls)
}
if len(sl.Items) != 2 {
t.Fatalf("Expected 2 services, but got %v", len(sl.Items))
}
if e, a := "foo", sl.Items[0].ID; e != a {
t.Errorf("Expected %v, but got %v", e, a)
}
if e, a := "foo2", sl.Items[1].ID; e != a {
t.Errorf("Expected %v, but got %v", e, a)
}
}