Update test cases to use default context

pull/6/head
derekwaynecarr 2014-09-29 17:18:18 -04:00
parent e4ec49ee6b
commit 0312d80ffa
5 changed files with 64 additions and 41 deletions

View File

@ -46,6 +46,10 @@ func TestValidNamespaceOnCreateOrUpdate(t *testing.T) {
}
resource = api.ReplicationController{JSONBase: api.JSONBase{Namespace: "other"}}
if api.ValidNamespaceOnCreateOrUpdate(ctx, &resource.JSONBase) {
t.Errorf("Expected error that resource and context errors do not match")
t.Errorf("Expected error that resource and context errors do not match because resource has different namespace")
}
ctx = api.NewContext()
if api.ValidNamespaceOnCreateOrUpdate(ctx, &resource.JSONBase) {
t.Errorf("Expected error that resource and context errors do not match since context has no namespace")
}
}

View File

@ -366,7 +366,7 @@ func TestValidateManifest(t *testing.T) {
func TestValidatePod(t *testing.T) {
errs := ValidatePod(&api.Pod{
JSONBase: api.JSONBase{ID: "foo"},
JSONBase: api.JSONBase{ID: "foo", Namespace: api.NamespaceDefault},
Labels: map[string]string{
"foo": "bar",
},
@ -384,7 +384,7 @@ func TestValidatePod(t *testing.T) {
t.Errorf("Unexpected non-zero error list: %#v", errs)
}
errs = ValidatePod(&api.Pod{
JSONBase: api.JSONBase{ID: "foo"},
JSONBase: api.JSONBase{ID: "foo", Namespace: api.NamespaceDefault},
Labels: map[string]string{
"foo": "bar",
},
@ -397,7 +397,7 @@ func TestValidatePod(t *testing.T) {
}
errs = ValidatePod(&api.Pod{
JSONBase: api.JSONBase{ID: "foo"},
JSONBase: api.JSONBase{ID: "foo", Namespace: api.NamespaceDefault},
Labels: map[string]string{
"foo": "bar",
},
@ -424,16 +424,27 @@ func TestValidateService(t *testing.T) {
{
name: "missing id",
svc: api.Service{
JSONBase: api.JSONBase{Namespace: api.NamespaceDefault},
Port: 8675,
Selector: map[string]string{"foo": "bar"},
},
// Should fail because the ID is missing.
numErrs: 1,
},
{
name: "missing namespace",
svc: api.Service{
JSONBase: api.JSONBase{ID: "foo"},
Port: 8675,
Selector: map[string]string{"foo": "bar"},
},
// Should fail because the Namespace is missing.
numErrs: 1,
},
{
name: "invalid id",
svc: api.Service{
JSONBase: api.JSONBase{ID: "123abc"},
JSONBase: api.JSONBase{ID: "123abc", Namespace: api.NamespaceDefault},
Port: 8675,
Selector: map[string]string{"foo": "bar"},
},
@ -443,7 +454,7 @@ func TestValidateService(t *testing.T) {
{
name: "missing port",
svc: api.Service{
JSONBase: api.JSONBase{ID: "abc123"},
JSONBase: api.JSONBase{ID: "abc123", Namespace: api.NamespaceDefault},
Selector: map[string]string{"foo": "bar"},
},
// Should fail because the port number is missing/invalid.
@ -452,7 +463,7 @@ func TestValidateService(t *testing.T) {
{
name: "invalid port",
svc: api.Service{
JSONBase: api.JSONBase{ID: "abc123"},
JSONBase: api.JSONBase{ID: "abc123", Namespace: api.NamespaceDefault},
Port: 65536,
Selector: map[string]string{"foo": "bar"},
},
@ -462,7 +473,7 @@ func TestValidateService(t *testing.T) {
{
name: "invalid protocol",
svc: api.Service{
JSONBase: api.JSONBase{ID: "abc123"},
JSONBase: api.JSONBase{ID: "abc123", Namespace: api.NamespaceDefault},
Port: 8675,
Protocol: "INVALID",
Selector: map[string]string{"foo": "bar"},
@ -473,7 +484,7 @@ func TestValidateService(t *testing.T) {
{
name: "missing selector",
svc: api.Service{
JSONBase: api.JSONBase{ID: "foo"},
JSONBase: api.JSONBase{ID: "foo", Namespace: api.NamespaceDefault},
Port: 8675,
},
// Should fail because the selector is missing.
@ -482,7 +493,7 @@ func TestValidateService(t *testing.T) {
{
name: "valid 1",
svc: api.Service{
JSONBase: api.JSONBase{ID: "abc123"},
JSONBase: api.JSONBase{ID: "abc123", Namespace: api.NamespaceDefault},
Port: 1,
Protocol: "TCP",
Selector: map[string]string{"foo": "bar"},
@ -492,7 +503,7 @@ func TestValidateService(t *testing.T) {
{
name: "valid 2",
svc: api.Service{
JSONBase: api.JSONBase{ID: "abc123"},
JSONBase: api.JSONBase{ID: "abc123", Namespace: api.NamespaceDefault},
Port: 65535,
Protocol: "UDP",
Selector: map[string]string{"foo": "bar"},
@ -502,7 +513,7 @@ func TestValidateService(t *testing.T) {
{
name: "valid 3",
svc: api.Service{
JSONBase: api.JSONBase{ID: "abc123"},
JSONBase: api.JSONBase{ID: "abc123", Namespace: api.NamespaceDefault},
Port: 80,
Selector: map[string]string{"foo": "bar"},
},
@ -519,7 +530,7 @@ func TestValidateService(t *testing.T) {
svc := api.Service{
Port: 6502,
JSONBase: api.JSONBase{ID: "foo"},
JSONBase: api.JSONBase{ID: "foo", Namespace: api.NamespaceDefault},
Selector: map[string]string{"foo": "bar"},
}
errs := ValidateService(&svc)
@ -544,14 +555,14 @@ func TestValidateReplicationController(t *testing.T) {
successCases := []api.ReplicationController{
{
JSONBase: api.JSONBase{ID: "abc"},
JSONBase: api.JSONBase{ID: "abc", Namespace: api.NamespaceDefault},
DesiredState: api.ReplicationControllerState{
ReplicaSelector: validSelector,
PodTemplate: validPodTemplate,
},
},
{
JSONBase: api.JSONBase{ID: "abc-123"},
JSONBase: api.JSONBase{ID: "abc-123", Namespace: api.NamespaceDefault},
DesiredState: api.ReplicationControllerState{
ReplicaSelector: validSelector,
PodTemplate: validPodTemplate,
@ -566,33 +577,40 @@ func TestValidateReplicationController(t *testing.T) {
errorCases := map[string]api.ReplicationController{
"zero-length ID": {
JSONBase: api.JSONBase{ID: ""},
JSONBase: api.JSONBase{ID: "", Namespace: api.NamespaceDefault},
DesiredState: api.ReplicationControllerState{
ReplicaSelector: validSelector,
PodTemplate: validPodTemplate,
},
},
"missing-namespace": {
JSONBase: api.JSONBase{ID: "abc-123"},
DesiredState: api.ReplicationControllerState{
ReplicaSelector: validSelector,
PodTemplate: validPodTemplate,
},
},
"empty selector": {
JSONBase: api.JSONBase{ID: "abc"},
JSONBase: api.JSONBase{ID: "abc", Namespace: api.NamespaceDefault},
DesiredState: api.ReplicationControllerState{
PodTemplate: validPodTemplate,
},
},
"selector_doesnt_match": {
JSONBase: api.JSONBase{ID: "abc"},
JSONBase: api.JSONBase{ID: "abc", Namespace: api.NamespaceDefault},
DesiredState: api.ReplicationControllerState{
ReplicaSelector: map[string]string{"foo": "bar"},
PodTemplate: validPodTemplate,
},
},
"invalid manifest": {
JSONBase: api.JSONBase{ID: "abc"},
JSONBase: api.JSONBase{ID: "abc", Namespace: api.NamespaceDefault},
DesiredState: api.ReplicationControllerState{
ReplicaSelector: validSelector,
},
},
"negative_replicas": {
JSONBase: api.JSONBase{ID: "abc"},
JSONBase: api.JSONBase{ID: "abc", Namespace: api.NamespaceDefault},
DesiredState: api.ReplicationControllerState{
Replicas: -1,
ReplicaSelector: validSelector,
@ -608,6 +626,7 @@ func TestValidateReplicationController(t *testing.T) {
field := errs[i].(errors.ValidationError).Field
if !strings.HasPrefix(field, "desiredState.podTemplate.") &&
field != "id" &&
field != "controller.Namespace" &&
field != "desiredState.replicaSelector" &&
field != "desiredState.replicas" {
t.Errorf("%s: missing prefix for: %v", k, errs[i])

View File

@ -243,7 +243,7 @@ func TestCreateController(t *testing.T) {
PodTemplate: validPodTemplate,
},
}
ctx := api.NewContext()
ctx := api.NewDefaultContext()
channel, err := storage.Create(ctx, controller)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
@ -279,8 +279,8 @@ func TestControllerStorageValidatesCreate(t *testing.T) {
DesiredState: api.ReplicationControllerState{},
},
}
ctx := api.NewDefaultContext()
for _, failureCase := range failureCases {
ctx := api.NewContext()
c, err := storage.Create(ctx, &failureCase)
if c != nil {
t.Errorf("Expected nil channel")
@ -310,8 +310,8 @@ func TestControllerStorageValidatesUpdate(t *testing.T) {
DesiredState: api.ReplicationControllerState{},
},
}
ctx := api.NewDefaultContext()
for _, failureCase := range failureCases {
ctx := api.NewContext()
c, err := storage.Update(ctx, &failureCase)
if c != nil {
t.Errorf("Expected nil channel")

View File

@ -69,7 +69,7 @@ func TestCreatePodRegistryError(t *testing.T) {
},
}
pod := &api.Pod{DesiredState: desiredState}
ctx := api.NewContext()
ctx := api.NewDefaultContext()
ch, err := storage.Create(ctx, pod)
if err != nil {
t.Errorf("Expected %#v, Got %#v", nil, err)
@ -89,7 +89,7 @@ func TestCreatePodSetsIds(t *testing.T) {
},
}
pod := &api.Pod{DesiredState: desiredState}
ctx := api.NewContext()
ctx := api.NewDefaultContext()
ch, err := storage.Create(ctx, pod)
if err != nil {
t.Errorf("Expected %#v, Got %#v", nil, err)
@ -116,7 +116,7 @@ func TestCreatePodSetsUUIDs(t *testing.T) {
},
}
pod := &api.Pod{DesiredState: desiredState}
ctx := api.NewContext()
ctx := api.NewDefaultContext()
ch, err := storage.Create(ctx, pod)
if err != nil {
t.Errorf("Expected %#v, Got %#v", nil, err)
@ -496,7 +496,7 @@ func TestPodStorageValidatesCreate(t *testing.T) {
storage := REST{
registry: podRegistry,
}
ctx := api.NewContext()
ctx := api.NewDefaultContext()
pod := &api.Pod{}
c, err := storage.Create(ctx, pod)
if c != nil {
@ -513,7 +513,7 @@ func TestPodStorageValidatesUpdate(t *testing.T) {
storage := REST{
registry: podRegistry,
}
ctx := api.NewContext()
ctx := api.NewDefaultContext()
pod := &api.Pod{}
c, err := storage.Update(ctx, pod)
if c != nil {
@ -545,7 +545,7 @@ func TestCreatePod(t *testing.T) {
JSONBase: api.JSONBase{ID: "foo"},
DesiredState: desiredState,
}
ctx := api.NewContext()
ctx := api.NewDefaultContext()
channel, err := storage.Create(ctx, pod)
if err != nil {
t.Errorf("unexpected error: %v", err)

View File

@ -40,7 +40,7 @@ func TestServiceRegistryCreate(t *testing.T) {
JSONBase: api.JSONBase{ID: "foo"},
Selector: map[string]string{"bar": "baz"},
}
ctx := api.NewContext()
ctx := api.NewDefaultContext()
c, _ := storage.Create(ctx, svc)
created_svc := <-c
created_service := created_svc.(*api.Service)
@ -76,7 +76,7 @@ func TestServiceStorageValidatesCreate(t *testing.T) {
Selector: map[string]string{},
},
}
ctx := api.NewContext()
ctx := api.NewDefaultContext()
for _, failureCase := range failureCases {
c, err := storage.Create(ctx, &failureCase)
if c != nil {
@ -90,7 +90,7 @@ func TestServiceStorageValidatesCreate(t *testing.T) {
}
func TestServiceRegistryUpdate(t *testing.T) {
ctx := api.NewContext()
ctx := api.NewDefaultContext()
registry := registrytest.NewServiceRegistry()
registry.CreateService(ctx, &api.Service{
Port: 6502,
@ -120,7 +120,7 @@ func TestServiceRegistryUpdate(t *testing.T) {
}
func TestServiceStorageValidatesUpdate(t *testing.T) {
ctx := api.NewContext()
ctx := api.NewDefaultContext()
registry := registrytest.NewServiceRegistry()
registry.CreateService(ctx, &api.Service{
Port: 6502,
@ -152,7 +152,7 @@ func TestServiceStorageValidatesUpdate(t *testing.T) {
}
func TestServiceRegistryExternalService(t *testing.T) {
ctx := api.NewContext()
ctx := api.NewDefaultContext()
registry := registrytest.NewServiceRegistry()
fakeCloud := &cloud.FakeCloud{}
machines := []string{"foo", "bar", "baz"}
@ -190,7 +190,7 @@ func TestServiceRegistryExternalServiceError(t *testing.T) {
Selector: map[string]string{"bar": "baz"},
CreateExternalLoadBalancer: true,
}
ctx := api.NewContext()
ctx := api.NewDefaultContext()
c, _ := storage.Create(ctx, svc)
<-c
if len(fakeCloud.Calls) != 1 || fakeCloud.Calls[0] != "get-zone" {
@ -202,7 +202,7 @@ func TestServiceRegistryExternalServiceError(t *testing.T) {
}
func TestServiceRegistryDelete(t *testing.T) {
ctx := api.NewContext()
ctx := api.NewDefaultContext()
registry := registrytest.NewServiceRegistry()
fakeCloud := &cloud.FakeCloud{}
machines := []string{"foo", "bar", "baz"}
@ -223,7 +223,7 @@ func TestServiceRegistryDelete(t *testing.T) {
}
func TestServiceRegistryDeleteExternal(t *testing.T) {
ctx := api.NewContext()
ctx := api.NewDefaultContext()
registry := registrytest.NewServiceRegistry()
fakeCloud := &cloud.FakeCloud{}
machines := []string{"foo", "bar", "baz"}
@ -245,7 +245,7 @@ func TestServiceRegistryDeleteExternal(t *testing.T) {
}
func TestServiceRegistryMakeLinkVariables(t *testing.T) {
ctx := api.NewContext()
ctx := api.NewDefaultContext()
registry := registrytest.NewServiceRegistry()
registry.List = api.ServiceList{
Items: []api.Service{
@ -310,7 +310,7 @@ func TestServiceRegistryMakeLinkVariables(t *testing.T) {
}
func TestServiceRegistryGet(t *testing.T) {
ctx := api.NewContext()
ctx := api.NewDefaultContext()
registry := registrytest.NewServiceRegistry()
fakeCloud := &cloud.FakeCloud{}
machines := []string{"foo", "bar", "baz"}
@ -329,7 +329,7 @@ func TestServiceRegistryGet(t *testing.T) {
}
func TestServiceRegistryResourceLocation(t *testing.T) {
ctx := api.NewContext()
ctx := api.NewDefaultContext()
registry := registrytest.NewServiceRegistry()
registry.Endpoints = api.Endpoints{Endpoints: []string{"foo:80"}}
fakeCloud := &cloud.FakeCloud{}
@ -359,7 +359,7 @@ func TestServiceRegistryResourceLocation(t *testing.T) {
}
func TestServiceRegistryList(t *testing.T) {
ctx := api.NewContext()
ctx := api.NewDefaultContext()
registry := registrytest.NewServiceRegistry()
fakeCloud := &cloud.FakeCloud{}
machines := []string{"foo", "bar", "baz"}