mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 04:08:16 +00:00 
			
		
		
		
	Improve test casing on resource defaults sample plugin
This commit is contained in:
		@@ -55,7 +55,9 @@ func (resourceDefaults) Admit(a admission.Attributes) (err error) {
 | 
				
			|||||||
	obj := a.GetObject()
 | 
						obj := a.GetObject()
 | 
				
			||||||
	pod := obj.(*api.Pod)
 | 
						pod := obj.(*api.Pod)
 | 
				
			||||||
	for index := range pod.Spec.Containers {
 | 
						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 {
 | 
							if pod.Spec.Containers[index].Resources.Limits.Memory().Value() == 0 {
 | 
				
			||||||
			pod.Spec.Containers[index].Resources.Limits[api.ResourceMemory] = resource.MustParse(defaultMemory)
 | 
								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/admission"
 | 
				
			||||||
	"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
 | 
						"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
 | 
				
			||||||
 | 
						"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestAdmission(t *testing.T) {
 | 
					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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user