Skip to content

Commit

Permalink
Improve test casing on resource defaults sample plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
derekwaynecarr committed Feb 6, 2015
1 parent 94d852e commit 409abdc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
4 changes: 3 additions & 1 deletion plugin/pkg/admission/resourcedefaults/admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ func (resourceDefaults) Admit(a admission.Attributes) (err error) {
obj := a.GetObject()
pod := obj.(*api.Pod)
for index := range pod.Spec.Containers {
pod.Spec.Containers[index].Resources.Limits = api.ResourceList{}
if pod.Spec.Containers[index].Resources.Limits == nil {
pod.Spec.Containers[index].Resources.Limits = api.ResourceList{}
}
if pod.Spec.Containers[index].Resources.Limits.Memory().Value() == 0 {
pod.Spec.Containers[index].Resources.Limits[api.ResourceMemory] = resource.MustParse(defaultMemory)
}
Expand Down
45 changes: 45 additions & 0 deletions plugin/pkg/admission/resourcedefaults/admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/GoogleCloudPlatform/kubernetes/pkg/admission"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
)

func TestAdmission(t *testing.T) {
Expand Down Expand Up @@ -51,3 +52,47 @@ func TestAdmission(t *testing.T) {
}
}
}

func TestIgnoreAdmission(t *testing.T) {
namespace := "default"

handler := NewResourceDefaults()
pod := api.Pod{
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: "ns"},
Spec: api.PodSpec{
Volumes: []api.Volume{{Name: "vol"}},
Containers: []api.Container{
{
Name: "ctr",
Image: "image",
Resources: api.ResourceRequirementSpec{
Limits: getResourceLimits("2", "750Mi"),
},
},
},
},
}

err := handler.Admit(admission.NewAttributesRecord(&pod, namespace, "pods", "CREATE"))
if err != nil {
t.Errorf("Unexpected error returned from admission handler")
}

for i := range pod.Spec.Containers {
memory := pod.Spec.Containers[i].Resources.Limits.Memory().String()
cpu := pod.Spec.Containers[i].Resources.Limits.Cpu().String()
if memory != "750Mi" {
t.Errorf("Unexpected memory value %s", memory)
}
if cpu != "2" {
t.Errorf("Unexpected cpu value %s", cpu)
}
}
}

func getResourceLimits(cpu, memory string) api.ResourceList {
res := api.ResourceList{}
res[api.ResourceCPU] = resource.MustParse(cpu)
res[api.ResourceMemory] = resource.MustParse(memory)
return res
}

0 comments on commit 409abdc

Please sign in to comment.