mirror of https://github.com/k3s-io/k3s
Improve test casing on resource defaults sample plugin
parent
94d852e8a6
commit
409abdc745
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue