Add request value verification for hugepage

This commit is contained in:
lala123912
2021-01-28 15:32:51 +08:00
parent 466e730259
commit e162fcc1bf
8 changed files with 530 additions and 9 deletions

View File

@@ -20,6 +20,7 @@ import (
"reflect"
"testing"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
@@ -247,3 +248,114 @@ func TestDeploymentDefaultGarbageCollectionPolicy(t *testing.T) {
}
}
}
func newDeploymentWithHugePageValue(reousreceName api.ResourceName, value resource.Quantity) *apps.Deployment {
return &apps.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: deploymentName,
Namespace: namespace,
ResourceVersion: "1",
},
Spec: apps.DeploymentSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"foo": "bar"},
MatchExpressions: []metav1.LabelSelectorRequirement{},
},
Strategy: apps.DeploymentStrategy{
Type: apps.RollingUpdateDeploymentStrategyType,
RollingUpdate: &apps.RollingUpdateDeployment{
MaxSurge: intstr.FromInt(1),
MaxUnavailable: intstr.FromInt(1),
},
},
Template: api.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "foo",
Labels: map[string]string{"foo": "bar"},
},
Spec: api.PodSpec{
RestartPolicy: api.RestartPolicyAlways,
DNSPolicy: api.DNSDefault,
Containers: []api.Container{{
Name: fakeImageName,
Image: fakeImage,
ImagePullPolicy: api.PullNever,
TerminationMessagePolicy: api.TerminationMessageReadFile,
Resources: api.ResourceRequirements{
Requests: api.ResourceList{
api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
api.ResourceName(reousreceName): value,
},
Limits: api.ResourceList{
api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
api.ResourceName(reousreceName): value,
},
}},
}},
},
},
}
}
func TestDeploymentStrategyValidate(t *testing.T) {
tests := []struct {
name string
deployment *apps.Deployment
}{
{
name: "validation on a new deployment with indivisible hugepages values",
deployment: newDeploymentWithHugePageValue(api.ResourceHugePagesPrefix+"2Mi", resource.MustParse("2.1Mi")),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if errs := Strategy.Validate(genericapirequest.NewContext(), tc.deployment); len(errs) == 0 {
t.Error("expected failure")
}
})
}
}
func TestDeploymentStrategyValidateUpdate(t *testing.T) {
tests := []struct {
name string
newDeployment *apps.Deployment
oldDeployment *apps.Deployment
}{
{
name: "validation on an existing deployment with indivisible hugepages values to a new deployment with indivisible hugepages values",
newDeployment: newDeploymentWithHugePageValue(api.ResourceHugePagesPrefix+"2Mi", resource.MustParse("2.1Mi")),
oldDeployment: newDeploymentWithHugePageValue(api.ResourceHugePagesPrefix+"1Gi", resource.MustParse("1.1Gi")),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if errs := Strategy.ValidateUpdate(genericapirequest.NewContext(), tc.newDeployment, tc.oldDeployment); len(errs) != 0 {
t.Errorf("unexpected error:%v", errs)
}
})
}
errTests := []struct {
name string
newDeployment *apps.Deployment
oldDeployment *apps.Deployment
}{
{
name: "validation on an existing deployment with divisible hugepages values to a new deployment with indivisible hugepages values",
newDeployment: newDeploymentWithHugePageValue(api.ResourceHugePagesPrefix+"2Mi", resource.MustParse("2.1Mi")),
oldDeployment: newDeploymentWithHugePageValue(api.ResourceHugePagesPrefix+"1Gi", resource.MustParse("2Gi")),
},
}
for _, tc := range errTests {
t.Run(tc.name, func(t *testing.T) {
if errs := Strategy.ValidateUpdate(genericapirequest.NewContext(), tc.newDeployment, tc.oldDeployment); len(errs) == 0 {
t.Error("expected failure")
}
})
}
}