From 07cd76a2eb0b8467f40ac3046d67a15a371afbda Mon Sep 17 00:00:00 2001 From: Sam Stoelinga Date: Wed, 11 Oct 2023 15:07:11 -0700 Subject: [PATCH] add tests for resources.go --- internal/resources/resources_test.go | 48 ++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 internal/resources/resources_test.go diff --git a/internal/resources/resources_test.go b/internal/resources/resources_test.go new file mode 100644 index 00000000..f8e15edb --- /dev/null +++ b/internal/resources/resources_test.go @@ -0,0 +1,48 @@ +package resources + +import ( + "testing" + + "github.com/stretchr/testify/require" + apiv1 "github.com/substratusai/substratus/api/v1" + "github.com/substratusai/substratus/internal/cloud" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/resource" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func Test_Apply(t *testing.T) { + objectMeta := &metav1.ObjectMeta{Name: "test", Namespace: "test"} + podSpec := &corev1.PodSpec{Containers: []corev1.Container{ + {Name: "test"}, + }} + + testCases := []struct { + Name string + Resources *apiv1.Resources + Expected *apiv1.Resources + }{ + { + Name: "not nil", + Resources: &apiv1.Resources{CPU: 8, Memory: 8, Disk: 300}, + Expected: &apiv1.Resources{CPU: 8, Memory: 8, Disk: 300}, + }, + { + Name: "nil", + Resources: nil, + Expected: &apiv1.Resources{CPU: 2, Memory: 4, Disk: 100}, + }, + } + + for _, testCase := range testCases { + t.Logf("Running test case %v", testCase.Name) + err := Apply(objectMeta, podSpec, "test", cloud.GCPName, testCase.Resources) + require.NoError(t, err, "Encountered error with case", testCase.Name) + require.Equal(t, podSpec.Containers[0].Resources.Requests.Cpu(), + resource.NewQuantity(testCase.Expected.CPU, resource.DecimalSI)) + require.Equal(t, podSpec.Containers[0].Resources.Requests.Memory(), + resource.NewQuantity(testCase.Expected.Memory*gigabyte, resource.BinarySI)) + require.Equal(t, podSpec.Containers[0].Resources.Requests.StorageEphemeral(), + resource.NewQuantity(testCase.Expected.Disk*gigabyte, resource.BinarySI)) + } +}