Skip to content

Commit

Permalink
Rewrite some e2e tests to handle resource defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
karlkfi committed Jul 19, 2023
1 parent 310c8b9 commit e005fb5
Show file tree
Hide file tree
Showing 6 changed files with 316 additions and 199 deletions.
100 changes: 87 additions & 13 deletions e2e/nomostest/testpredicates/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -251,8 +250,9 @@ func HasExactlyImage(containerName, expectImageName, expectImageTag, expectImage
}
}

// HasCorrectResourceRequestsLimits verify a root/namespace reconciler container has the correct resource requests and limits.
func HasCorrectResourceRequestsLimits(containerName string, cpuRequest, cpuLimit, memoryRequest, memoryLimit resource.Quantity) Predicate {
// DeploymentContainerResourcesEqual verify a reconciler deployment container
// has the expected resource requests and limits.
func DeploymentContainerResourcesEqual(expectedSpec v1beta1.ContainerResourcesSpec) Predicate {
return func(o client.Object) error {
if o == nil {
return ErrObjectNotFound
Expand All @@ -272,24 +272,98 @@ func HasCorrectResourceRequestsLimits(containerName string, cpuRequest, cpuLimit
return WrongTypeErr(dep, &appsv1.Deployment{})
}
for _, container := range dep.Spec.Template.Spec.Containers {
if containerName == container.Name {
if !equality.Semantic.DeepEqual(container.Resources.Requests[corev1.ResourceCPU], cpuRequest) {
return errors.Errorf("The CPU request of the %q container should be %v, got %v", container.Name, cpuRequest, container.Resources.Requests[corev1.ResourceCPU])
if expectedSpec.ContainerName == container.Name {

expected := expectedSpec.CPURequest
found := container.Resources.Requests.Cpu()
if found.Cmp(expected) != 0 {
return fmt.Errorf("expected CPU request of the %q container: %s, got: %s",
container.Name, &expected, found)
}
if !equality.Semantic.DeepEqual(container.Resources.Limits[corev1.ResourceCPU], cpuLimit) {
return errors.Errorf("The CPU limit of the %q container should be %v, got %v", container.Name, cpuLimit, container.Resources.Limits[corev1.ResourceCPU])

expected = expectedSpec.MemoryRequest
found = container.Resources.Requests.Memory()
if found.Cmp(expected) != 0 {
return fmt.Errorf("expected Memory request of the %q container: %s, got: %s",
container.Name, &expected, found)
}
if !equality.Semantic.DeepEqual(container.Resources.Requests[corev1.ResourceMemory], memoryRequest) {
return errors.Errorf("The memory request of the %q container should be %v, got %v", container.Name, memoryRequest, container.Resources.Requests[corev1.ResourceMemory])

expected = expectedSpec.CPULimit
found = container.Resources.Limits.Cpu()
if found.Cmp(expected) != 0 {
return fmt.Errorf("expected CPU limit of the %q container: %s, got: %s",
container.Name, &expected, found)
}
if !equality.Semantic.DeepEqual(container.Resources.Limits[corev1.ResourceMemory], memoryLimit) {
return errors.Errorf("The memory limit of the %q container should be %v, got %v", container.Name, memoryLimit, container.Resources.Limits[corev1.ResourceMemory])

expected = expectedSpec.MemoryLimit
found = container.Resources.Limits.Memory()
if found.Cmp(expected) != 0 {
return fmt.Errorf("expected Memory limit of the %q container: %s, got: %s",
container.Name, &expected, found)
}

return nil
}
}
return errors.Errorf("Container %q not found", containerName)
return errors.Errorf("Container %q not found", expectedSpec.ContainerName)
}
}

func DeploymentContainerResourcesAllEqual(expectedByName map[string]v1beta1.ContainerResourcesSpec) Predicate {
return func(o client.Object) error {
if o == nil {
return ErrObjectNotFound
}
if uObj, ok := o.(*unstructured.Unstructured); ok {
rObj, err := kinds.ToTypedObject(uObj, core.Scheme)
if err != nil {
return err
}
o, err = kinds.ObjectAsClientObject(rObj)
if err != nil {
return err
}
}
d, ok := o.(*appsv1.Deployment)
if !ok {
return WrongTypeErr(d, &appsv1.Deployment{})
}
for _, container := range d.Spec.Template.Spec.Containers {
expectedSpec, ok := expectedByName[container.Name]
if !ok {
return fmt.Errorf("found unexpected container: %q",
container.Name)
}

expected := expectedSpec.CPURequest
found := container.Resources.Requests.Cpu()
if found.Cmp(expected) != 0 {
return fmt.Errorf("expected CPU request of the %q container: %s, got: %s",
container.Name, &expected, found)
}

expected = expectedSpec.MemoryRequest
found = container.Resources.Requests.Memory()
if found.Cmp(expected) != 0 {
return fmt.Errorf("expected Memory request of the %q container: %s, got: %s",
container.Name, &expected, found)
}

expected = expectedSpec.CPULimit
found = container.Resources.Limits.Cpu()
if found.Cmp(expected) != 0 {
return fmt.Errorf("expected CPU limit of the %q container: %s, got: %s",
container.Name, &expected, found)
}

expected = expectedSpec.MemoryLimit
found = container.Resources.Limits.Memory()
if found.Cmp(expected) != 0 {
return fmt.Errorf("expected Memory limit of the %q container: %s, got: %s",
container.Name, &expected, found)
}
}
return nil
}
}

Expand Down
24 changes: 14 additions & 10 deletions e2e/testcases/helm_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ func TestPublicHelm(t *testing.T) {
expectedMemoryLimit = "300Mi"
}
if err := nt.Validate("my-wordpress", "wordpress", &appsv1.Deployment{}, containerImagePullPolicy("Always"),
testpredicates.HasCorrectResourceRequestsLimits("wordpress",
resource.MustParse(expectedCPURequest),
resource.MustParse(expectedCPULimit),
resource.MustParse(expectedMemoryRequest),
resource.MustParse(expectedMemoryLimit)),
testpredicates.DeploymentContainerResourcesEqual(v1beta1.ContainerResourcesSpec{
ContainerName: "wordpress",
CPURequest: resource.MustParse(expectedCPURequest),
CPULimit: resource.MustParse(expectedCPULimit),
MemoryRequest: resource.MustParse(expectedMemoryRequest),
MemoryLimit: resource.MustParse(expectedMemoryLimit),
}),
testpredicates.HasExactlyImage("wordpress", "bitnami/wordpress", "", "sha256:362cb642db481ebf6f14eb0244fbfb17d531a84ecfe099cd3bba6810db56694e"),
testpredicates.DeploymentHasEnvVar("wordpress", "WORDPRESS_USERNAME", "test-user"),
testpredicates.DeploymentHasEnvVar("wordpress", "WORDPRESS_EMAIL", "test-user@example.com"),
Expand Down Expand Up @@ -273,11 +275,13 @@ service:
expectedMemoryLimit = "300Mi"
}
if err := nt.Validate("my-wordpress", "wordpress", &appsv1.Deployment{}, containerImagePullPolicy("Always"),
testpredicates.HasCorrectResourceRequestsLimits("wordpress",
resource.MustParse(expectedCPURequest),
resource.MustParse(expectedCPULimit),
resource.MustParse(expectedMemoryRequest),
resource.MustParse(expectedMemoryLimit)),
testpredicates.DeploymentContainerResourcesEqual(v1beta1.ContainerResourcesSpec{
ContainerName: "wordpress",
CPURequest: resource.MustParse(expectedCPURequest),
CPULimit: resource.MustParse(expectedCPULimit),
MemoryRequest: resource.MustParse(expectedMemoryRequest),
MemoryLimit: resource.MustParse(expectedMemoryLimit),
}),
testpredicates.HasExactlyImage("wordpress", "bitnami/wordpress", "", "sha256:362cb642db481ebf6f14eb0244fbfb17d531a84ecfe099cd3bba6810db56694e"),
testpredicates.DeploymentHasEnvVar("wordpress", "WORDPRESS_USERNAME", "test-user-1"),
testpredicates.DeploymentHasEnvVar("wordpress", "WORDPRESS_EMAIL", "test-user@example.com"),
Expand Down
Loading

0 comments on commit e005fb5

Please sign in to comment.