From 73eade6eef57a6c4f0ae71662ffe9abcf0782bcd Mon Sep 17 00:00:00 2001 From: Pedro Soares Date: Wed, 24 Jul 2024 13:29:37 -0300 Subject: [PATCH] test(healthcontroller): add tests to compute rolling surge func --- .../healthcontroller/executor_test.go | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/internal/core/operations/healthcontroller/executor_test.go b/internal/core/operations/healthcontroller/executor_test.go index 5f661bfab..accedf749 100644 --- a/internal/core/operations/healthcontroller/executor_test.go +++ b/internal/core/operations/healthcontroller/executor_test.go @@ -1467,3 +1467,79 @@ func newValidScheduler(autoscaling *autoscaling.Autoscaling) *entities.Scheduler Autoscaling: autoscaling, } } + +func TestComputeRollingSurgeVariants(t *testing.T) { + testCases := []struct { + name string + maxSurge string + desiredNumberOfRooms int + totalRoomsAmount int + expectedSurgeAmount int + expectError bool + }{ + { + name: "Standard surge calculation", + maxSurge: "10", + desiredNumberOfRooms: 20, + totalRoomsAmount: 15, + expectedSurgeAmount: 5, + expectError: false, + }, + { + name: "Low amount of rooms", + maxSurge: "10", + desiredNumberOfRooms: 5, + totalRoomsAmount: 3, + expectedSurgeAmount: 10, // Surge is fixed at 10, regardless of the low number of rooms + expectError: false, + }, + { + name: "High amount of rooms above 1000", + maxSurge: "30%", + desiredNumberOfRooms: 1500, + totalRoomsAmount: 1000, + expectedSurgeAmount: 10, // Surge is fixed at 10, even for high number of rooms + expectError: false, + }, + { + name: "Relative maxSurge with rooms above 1000", + maxSurge: "25%", + desiredNumberOfRooms: 1200, + totalRoomsAmount: 1000, + expectedSurgeAmount: 300, // 25% of 1200 is 300 + expectError: false, + }, + { + name: "Invalid maxSurge string format", + maxSurge: "twenty%", // Invalid because it's not a number + desiredNumberOfRooms: 300, + totalRoomsAmount: 250, + expectedSurgeAmount: 1, // Expected to be 1 because the function should return an error + expectError: true, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + scheduler := &entities.Scheduler{ + MaxSurge: tc.maxSurge, + } + + ex := &Executor{} + + surgeAmount, err := ex.computeRollingSurge(scheduler, tc.desiredNumberOfRooms, tc.totalRoomsAmount) + if tc.expectError { + if err == nil { + t.Errorf("expected an error but got none") + } + } else { + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if surgeAmount != tc.expectedSurgeAmount { + t.Errorf("expected surge amount to be %d, but got %d", tc.expectedSurgeAmount, surgeAmount) + } + } + }) + } +}