|
| 1 | +package deployment |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + appsv1 "k8s.io/api/apps/v1" |
| 7 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 8 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 9 | +) |
| 10 | + |
| 11 | +func TestSetRollingUpdateParameters(t *testing.T) { |
| 12 | + testCases := []struct { |
| 13 | + name string |
| 14 | + controlPlaneCount int32 |
| 15 | + expectedMaxUnavailable int32 |
| 16 | + expectedMaxSurge int32 |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "single control plane node", |
| 20 | + controlPlaneCount: 1, |
| 21 | + expectedMaxUnavailable: 1, // max(1-1, 1) = max(0, 1) = 1 |
| 22 | + expectedMaxSurge: 1, |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "two control plane nodes", |
| 26 | + controlPlaneCount: 2, |
| 27 | + expectedMaxUnavailable: 1, // max(2-1, 1) = max(1, 1) = 1 |
| 28 | + expectedMaxSurge: 2, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "three control plane nodes", |
| 32 | + controlPlaneCount: 3, |
| 33 | + expectedMaxUnavailable: 2, // max(3-1, 1) = max(2, 1) = 2 |
| 34 | + expectedMaxSurge: 3, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "four control plane nodes", |
| 38 | + controlPlaneCount: 4, |
| 39 | + expectedMaxUnavailable: 3, // max(4-1, 1) = max(3, 1) = 3 |
| 40 | + expectedMaxSurge: 4, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "five control plane nodes", |
| 44 | + controlPlaneCount: 5, |
| 45 | + expectedMaxUnavailable: 4, // max(5-1, 1) = max(4, 1) = 4 |
| 46 | + expectedMaxSurge: 5, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + for _, tc := range testCases { |
| 51 | + t.Run(tc.name, func(t *testing.T) { |
| 52 | + // Create a test deployment with rolling update strategy |
| 53 | + deployment := &appsv1.Deployment{ |
| 54 | + ObjectMeta: metav1.ObjectMeta{ |
| 55 | + Name: "test-deployment", |
| 56 | + Namespace: "test-namespace", |
| 57 | + }, |
| 58 | + Spec: appsv1.DeploymentSpec{ |
| 59 | + Strategy: appsv1.DeploymentStrategy{ |
| 60 | + Type: appsv1.RollingUpdateDeploymentStrategyType, |
| 61 | + RollingUpdate: &appsv1.RollingUpdateDeployment{ |
| 62 | + MaxUnavailable: &intstr.IntOrString{Type: intstr.Int, IntVal: 0}, |
| 63 | + MaxSurge: &intstr.IntOrString{Type: intstr.Int, IntVal: 0}, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + // Call the function under test |
| 70 | + setRollingUpdateParameters(tc.controlPlaneCount, deployment) |
| 71 | + |
| 72 | + // Verify MaxUnavailable is set correctly |
| 73 | + if deployment.Spec.Strategy.RollingUpdate.MaxUnavailable == nil { |
| 74 | + t.Errorf("MaxUnavailable should not be nil") |
| 75 | + } else { |
| 76 | + actualMaxUnavailable := deployment.Spec.Strategy.RollingUpdate.MaxUnavailable.IntVal |
| 77 | + if actualMaxUnavailable != tc.expectedMaxUnavailable { |
| 78 | + t.Errorf("Expected MaxUnavailable to be %d, got %d", tc.expectedMaxUnavailable, actualMaxUnavailable) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Verify MaxSurge is set correctly |
| 83 | + if deployment.Spec.Strategy.RollingUpdate.MaxSurge == nil { |
| 84 | + t.Errorf("MaxSurge should not be nil") |
| 85 | + } else { |
| 86 | + actualMaxSurge := deployment.Spec.Strategy.RollingUpdate.MaxSurge.IntVal |
| 87 | + if actualMaxSurge != tc.expectedMaxSurge { |
| 88 | + t.Errorf("Expected MaxSurge to be %d, got %d", tc.expectedMaxSurge, actualMaxSurge) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Verify the values are of type Int (not String) |
| 93 | + if deployment.Spec.Strategy.RollingUpdate.MaxUnavailable.Type != intstr.Int { |
| 94 | + t.Errorf("Expected MaxUnavailable to be of type Int, got %v", deployment.Spec.Strategy.RollingUpdate.MaxUnavailable.Type) |
| 95 | + } |
| 96 | + if deployment.Spec.Strategy.RollingUpdate.MaxSurge.Type != intstr.Int { |
| 97 | + t.Errorf("Expected MaxSurge to be of type Int, got %v", deployment.Spec.Strategy.RollingUpdate.MaxSurge.Type) |
| 98 | + } |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
0 commit comments