@@ -6,20 +6,20 @@ package certinjectionwebhook_test
6
6
import (
7
7
"context"
8
8
"encoding/json"
9
+ "os"
9
10
"testing"
10
11
11
12
jp "github.com/evanphx/json-patch/v5"
12
13
"github.com/sclevine/spec"
13
14
"github.com/stretchr/testify/assert"
14
15
"github.com/stretchr/testify/require"
16
+ "github.com/vmware-tanzu/cert-injection-webhook/pkg/certinjectionwebhook"
15
17
"gomodules.xyz/jsonpatch/v3"
16
18
admissionv1 "k8s.io/api/admission/v1"
17
19
corev1 "k8s.io/api/core/v1"
18
20
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19
21
"k8s.io/apimachinery/pkg/runtime"
20
22
wtesting "knative.dev/pkg/webhook/testing"
21
-
22
- "github.com/vmware-tanzu/cert-injection-webhook/pkg/certinjectionwebhook"
23
23
)
24
24
25
25
func TestPodAdmissionController (t * testing.T ) {
@@ -1396,3 +1396,104 @@ func testPodAdmissionController(t *testing.T, when spec.G, it spec.S) {
1396
1396
})
1397
1397
1398
1398
}
1399
+
1400
+ func TestParseResource (t * testing.T ) {
1401
+ tests := []struct {
1402
+ name string
1403
+ envVar string
1404
+ envValue string
1405
+ expectErr bool
1406
+ expectVal string
1407
+ }{
1408
+ {"Valid CPU Request" , "TEST_CPU_REQUEST" , "100m" , false , "100m" },
1409
+ {"Valid Memory Request" , "TEST_MEMORY_REQUEST" , "128Mi" , false , "128Mi" },
1410
+ {"Invalid Format" , "TEST_INVALID" , "invalid" , true , "" },
1411
+ {"Missing Env Var" , "TEST_MISSING" , "" , false , "" },
1412
+ }
1413
+
1414
+ for _ , tt := range tests {
1415
+ t .Run (tt .name , func (t * testing.T ) {
1416
+ // Set or unset the environment variable
1417
+ if tt .envValue != "" {
1418
+ os .Setenv (tt .envVar , tt .envValue )
1419
+ defer os .Unsetenv (tt .envVar )
1420
+ } else {
1421
+ os .Unsetenv (tt .envVar )
1422
+ }
1423
+
1424
+ qty , err := certinjectionwebhook .ParseResource (tt .envVar )
1425
+ if (err != nil ) != tt .expectErr {
1426
+ t .Errorf ("Expected error: %v, got: %v" , tt .expectErr , err )
1427
+ }
1428
+
1429
+ if err == nil && tt .expectVal != "" && qty .String () != tt .expectVal {
1430
+ t .Errorf ("Expected value: %s, got: %s" , tt .expectVal , qty .String ())
1431
+ }
1432
+ })
1433
+ }
1434
+ }
1435
+
1436
+ func TestResourceRequirementsParsing (t * testing.T ) {
1437
+ // Set environment variables
1438
+ os .Setenv ("INIT_CONTAINER_CPU_REQUEST" , "200m" )
1439
+ os .Setenv ("INIT_CONTAINER_MEMORY_REQUEST" , "256Mi" )
1440
+ os .Setenv ("INIT_CONTAINER_CPU_LIMIT" , "1" )
1441
+ os .Setenv ("INIT_CONTAINER_MEMORY_LIMIT" , "512Mi" )
1442
+ defer func () {
1443
+ os .Unsetenv ("INIT_CONTAINER_CPU_REQUEST" )
1444
+ os .Unsetenv ("INIT_CONTAINER_MEMORY_REQUEST" )
1445
+ os .Unsetenv ("INIT_CONTAINER_CPU_LIMIT" )
1446
+ os .Unsetenv ("INIT_CONTAINER_MEMORY_LIMIT" )
1447
+ }()
1448
+
1449
+ var resources corev1.ResourceRequirements
1450
+
1451
+ // Apply the logic under test
1452
+ if cpuRequest , err := certinjectionwebhook .ParseResource ("INIT_CONTAINER_CPU_REQUEST" ); err == nil {
1453
+ if resources .Requests == nil {
1454
+ resources .Requests = corev1.ResourceList {}
1455
+ }
1456
+ resources .Requests [corev1 .ResourceCPU ] = cpuRequest
1457
+ }
1458
+ if memoryRequest , err := certinjectionwebhook .ParseResource ("INIT_CONTAINER_MEMORY_REQUEST" ); err == nil {
1459
+ if resources .Requests == nil {
1460
+ resources .Requests = corev1.ResourceList {}
1461
+ }
1462
+ resources .Requests [corev1 .ResourceMemory ] = memoryRequest
1463
+ }
1464
+ if cpuLimit , err := certinjectionwebhook .ParseResource ("INIT_CONTAINER_CPU_LIMIT" ); err == nil {
1465
+ if resources .Limits == nil {
1466
+ resources .Limits = corev1.ResourceList {}
1467
+ }
1468
+ resources .Limits [corev1 .ResourceCPU ] = cpuLimit
1469
+ }
1470
+ if memoryLimit , err := certinjectionwebhook .ParseResource ("INIT_CONTAINER_MEMORY_LIMIT" ); err == nil {
1471
+ if resources .Limits == nil {
1472
+ resources .Limits = corev1.ResourceList {}
1473
+ }
1474
+ resources .Limits [corev1 .ResourceMemory ] = memoryLimit
1475
+ }
1476
+
1477
+ expectedRequests := map [corev1.ResourceName ]string {
1478
+ corev1 .ResourceCPU : "200m" ,
1479
+ corev1 .ResourceMemory : "256Mi" ,
1480
+ }
1481
+ expectedLimits := map [corev1.ResourceName ]string {
1482
+ corev1 .ResourceCPU : "1" ,
1483
+ corev1 .ResourceMemory : "512Mi" ,
1484
+ }
1485
+
1486
+ for k , v := range expectedRequests {
1487
+ qty := resources .Requests [k ] // Copy value from map
1488
+ if qty .String () != v {
1489
+ t .Errorf ("Expected request %s for %s, got %s" , v , k , qty .String ())
1490
+ }
1491
+ }
1492
+
1493
+ for k , v := range expectedLimits {
1494
+ qty := resources .Limits [k ] // Copy value from map
1495
+ if qty .String () != v {
1496
+ t .Errorf ("Expected limit %s for %s, got %s" , v , k , qty .String ())
1497
+ }
1498
+ }
1499
+ }
0 commit comments