Skip to content

Commit

Permalink
Merge pull request kubernetes#5159 from brendandburns/coverage
Browse files Browse the repository at this point in the history
Expand test coverage in master.
  • Loading branch information
alex-mohr committed Mar 9, 2015
2 parents d8bbda2 + 7c654a3 commit 37bfb0d
Show file tree
Hide file tree
Showing 13 changed files with 899 additions and 233 deletions.
74 changes: 74 additions & 0 deletions pkg/api/rest/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ limitations under the License.
package rest

import (
"reflect"
"testing"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
)

func TestCheckGeneratedNameError(t *testing.T) {
Expand All @@ -39,3 +41,75 @@ func TestCheckGeneratedNameError(t *testing.T) {
t.Errorf("expected try again later error: %v", err)
}
}

func TestBeforeCreate(t *testing.T) {
failures := []runtime.Object{
&api.Service{},
&api.Service{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Namespace: "#$%%invalid",
},
},
&api.Service{
ObjectMeta: api.ObjectMeta{
Name: "##&*(&invalid",
Namespace: api.NamespaceDefault,
},
},
}
for _, test := range failures {
ctx := api.NewDefaultContext()
err := BeforeCreate(Services, ctx, test)
if err == nil {
t.Errorf("unexpected non-error for %v", test)
}
}

obj := &api.ReplicationController{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Namespace: api.NamespaceDefault,
},
Spec: api.ReplicationControllerSpec{
Selector: map[string]string{"name": "foo"},
Template: &api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{
"name": "foo",
},
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: "foo",
Image: "foo",
ImagePullPolicy: api.PullAlways,
},
},
RestartPolicy: api.RestartPolicy{Always: &api.RestartPolicyAlways{}},
DNSPolicy: api.DNSDefault,
},
},
},
Status: api.ReplicationControllerStatus{
Replicas: 3,
},
}
ctx := api.NewDefaultContext()
err := BeforeCreate(ReplicationControllers, ctx, obj)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !reflect.DeepEqual(obj.Status, api.ReplicationControllerStatus{}) {
t.Errorf("status was not cleared as expected.")
}
if obj.Name != "foo" || obj.Namespace != api.NamespaceDefault {
t.Errorf("unexpected object metadata: %v", obj.ObjectMeta)
}

obj.Spec.Replicas = -1
if err := BeforeCreate(ReplicationControllers, ctx, obj); err == nil {
t.Errorf("unexpected non-error for invalid replication controller.")
}
}
10 changes: 9 additions & 1 deletion pkg/api/rest/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ type svcStrategy struct {

// Services is the default logic that applies when creating and updating Service
// objects.
var Services RESTCreateStrategy = svcStrategy{api.Scheme, api.SimpleNameGenerator}
var Services = svcStrategy{api.Scheme, api.SimpleNameGenerator}

// NamespaceScoped is true for services.
func (svcStrategy) NamespaceScoped() bool {
Expand All @@ -100,6 +100,14 @@ func (svcStrategy) Validate(obj runtime.Object) errors.ValidationErrorList {
return validation.ValidateService(service)
}

func (svcStrategy) AllowCreateOnUpdate() bool {
return true
}

func (svcStrategy) ValidateUpdate(obj, old runtime.Object) errors.ValidationErrorList {
return validation.ValidateServiceUpdate(old.(*api.Service), obj.(*api.Service))
}

// nodeStrategy implements behavior for nodes
// TODO: move to a node specific package.
type nodeStrategy struct {
Expand Down
111 changes: 111 additions & 0 deletions pkg/api/rest/update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
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 rest

import (
"testing"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
)

func TestBeforeUpdate(t *testing.T) {
tests := []struct {
old runtime.Object
obj runtime.Object
expectErr bool
}{
{
obj: &api.Service{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Namespace: "#$%%invalid",
},
},
old: &api.Service{},
expectErr: true,
},
{
obj: &api.Service{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Namespace: "valid",
},
},
old: &api.Service{
ObjectMeta: api.ObjectMeta{
Name: "bar",
Namespace: "valid",
},
},
expectErr: true,
},
{
obj: &api.Service{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Namespace: "valid",
},
Spec: api.ServiceSpec{
PortalIP: "1.2.3.4",
},
},
old: &api.Service{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Namespace: "valid",
},
Spec: api.ServiceSpec{
PortalIP: "4.3.2.1",
},
},
expectErr: true,
},
{
obj: &api.Service{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Namespace: api.NamespaceDefault,
},
Spec: api.ServiceSpec{
PortalIP: "1.2.3.4",
Selector: map[string]string{"foo": "bar"},
},
},
old: &api.Service{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Namespace: api.NamespaceDefault,
},
Spec: api.ServiceSpec{
PortalIP: "1.2.3.4",
Selector: map[string]string{"bar": "foo"},
},
},
},
}
for _, test := range tests {
ctx := api.NewDefaultContext()
err := BeforeUpdate(Services, ctx, test.obj, test.old)
if test.expectErr && err == nil {
t.Errorf("unexpected non-error for %v", test)
}
if !test.expectErr && err != nil {
t.Errorf("unexpected error: %v for %v -> %v", err, test.obj, test.old)
}
}
}
47 changes: 0 additions & 47 deletions pkg/apiserver/async.go

This file was deleted.

Loading

0 comments on commit 37bfb0d

Please sign in to comment.