Skip to content

Commit

Permalink
Add unit test for rendering resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Farries committed Apr 26, 2022
1 parent 1981d1f commit 3998444
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions install/installer/pkg/common/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/gitpod-io/gitpod/installer/pkg/config/versions"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/pointer"
)
Expand Down Expand Up @@ -79,6 +81,108 @@ func TestReplicas(t *testing.T) {
}
}

func TestResourceRequirements(t *testing.T) {
defaultResources := corev1.ResourceRequirements{
Requests: corev1.ResourceList{
"cpu": resource.MustParse("200m"),
"memory": resource.MustParse("200Mi"),
},
Limits: corev1.ResourceList{
"cpu": resource.MustParse("200m"),
"memory": resource.MustParse("200Mi"),
},
}

serverResources := corev1.ResourceRequirements{
Requests: corev1.ResourceList{
"cpu": resource.MustParse("50m"),
"memory": resource.MustParse("100Mi"),
},
Limits: corev1.ResourceList{
"cpu": resource.MustParse("500m"),
"memory": resource.MustParse("800Mi"),
},
}

dashboardResources := corev1.ResourceRequirements{
Requests: corev1.ResourceList{
"cpu": resource.MustParse("60m"),
"memory": resource.MustParse("100Mi"),
},
Limits: corev1.ResourceList{
"cpu": resource.MustParse("100m"),
"memory": resource.MustParse("500Mi"),
},
}

testCases := []struct {
Component string
ContainerName string
Name string
ExpectedResources corev1.ResourceRequirements
}{
{
Component: server.Component,
ContainerName: server.Component,
Name: "server takes resource requirements from config",
ExpectedResources: serverResources,
},
{
Component: dashboard.Component,
ContainerName: dashboard.Component,
Name: "dashboard takes resource requirements from config",
ExpectedResources: dashboardResources,
},
{
Component: content_service.Component,
Name: "content_service takes default resource requirements",
ExpectedResources: defaultResources,
},
}
ctx, err := common.NewRenderContext(config.Config{
Experimental: &experimental.Config{
Common: &experimental.CommonConfig{
PodConfig: map[string]*experimental.PodConfig{
server.Component: {
Resources: map[string]*corev1.ResourceRequirements{
server.Component: &serverResources,
},
},
dashboard.Component: {
Resources: map[string]*corev1.ResourceRequirements{
dashboard.Component: &dashboardResources,
},
},
},
},
},
}, versions.Manifest{}, "test_namespace")
require.NoError(t, err)

for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
actualResources := common.ResourceRequirements(ctx, testCase.Component, testCase.ContainerName, defaultResources)

if actualResources.Limits["cpu"] != testCase.ExpectedResources.Limits["cpu"] {
t.Errorf("expected cpu limits for container %q in component %q to be %+v, but got %+v",
testCase.Component, testCase.ContainerName, testCase.ExpectedResources.Limits["cpu"], actualResources.Limits["cpu"])
}
if actualResources.Limits["memory"] != testCase.ExpectedResources.Limits["memory"] {
t.Errorf("expected memory limits for container %q in component %q to be %+v, but got %+v",
testCase.Component, testCase.ContainerName, testCase.ExpectedResources.Limits["memory"], actualResources.Limits["memory"])
}
if actualResources.Requests["cpu"] != testCase.ExpectedResources.Requests["cpu"] {
t.Errorf("expected cpu requests for container %q in component %q to be %+v, but got %+v",
testCase.Component, testCase.ContainerName, testCase.ExpectedResources.Requests["cpu"], actualResources.Requests["cpu"])
}
if actualResources.Requests["memory"] != testCase.ExpectedResources.Requests["memory"] {
t.Errorf("expected memory requests for container %q in component %q to be %+v, but got %+v",
testCase.Component, testCase.ContainerName, testCase.ExpectedResources.Requests["memory"], actualResources.Requests["memory"])
}
})
}
}

func TestRepoName(t *testing.T) {
type Expectation struct {
Result string
Expand Down

0 comments on commit 3998444

Please sign in to comment.