|
| 1 | +package kubeletconfig |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + ctrlcommon "github.com/openshift/machine-config-operator/pkg/controller/common" |
| 8 | +) |
| 9 | + |
| 10 | +func TestCreateNewKubeletDynamicSystemReservedIgnition(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + autoSystemReserved *bool |
| 14 | + expectedAutoSizing string |
| 15 | + expectedMemory string |
| 16 | + expectedCPU string |
| 17 | + expectedEphemeral string |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "Auto sizing should be true if autoSystemReserved is passed as nil", |
| 21 | + autoSystemReserved: nil, |
| 22 | + expectedAutoSizing: "NODE_SIZING_ENABLED=true", |
| 23 | + expectedMemory: "SYSTEM_RESERVED_MEMORY=1Gi", |
| 24 | + expectedCPU: "SYSTEM_RESERVED_CPU=500m", |
| 25 | + expectedEphemeral: "SYSTEM_RESERVED_ES=1Gi", |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "Auto sizing should be false if autoSystemReserved is passed as false", |
| 29 | + autoSystemReserved: boolPtr(false), |
| 30 | + expectedAutoSizing: "NODE_SIZING_ENABLED=false", |
| 31 | + expectedMemory: "SYSTEM_RESERVED_MEMORY=1Gi", |
| 32 | + expectedCPU: "SYSTEM_RESERVED_CPU=500m", |
| 33 | + expectedEphemeral: "SYSTEM_RESERVED_ES=1Gi", |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "Auto sizing should be true if autoSystemReserved is passed as true", |
| 37 | + autoSystemReserved: boolPtr(true), |
| 38 | + expectedAutoSizing: "NODE_SIZING_ENABLED=true", |
| 39 | + expectedMemory: "SYSTEM_RESERVED_MEMORY=1Gi", |
| 40 | + expectedCPU: "SYSTEM_RESERVED_CPU=500m", |
| 41 | + expectedEphemeral: "SYSTEM_RESERVED_ES=1Gi", |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + for _, tt := range tests { |
| 46 | + t.Run(tt.name, func(t *testing.T) { |
| 47 | + // Call the function with nil for userDefinedSystemReserved as requested |
| 48 | + result := createNewKubeletDynamicSystemReservedIgnition(tt.autoSystemReserved, nil) |
| 49 | + |
| 50 | + // Verify the file path is correct |
| 51 | + expectedPath := "/etc/node-sizing-enabled.env" |
| 52 | + if result.Path != expectedPath { |
| 53 | + t.Errorf("Expected path %s, got %s", expectedPath, result.Path) |
| 54 | + } |
| 55 | + |
| 56 | + // Decode the file contents |
| 57 | + contents, err := ctrlcommon.DecodeIgnitionFileContents(result.Contents.Source, result.Contents.Compression) |
| 58 | + if err != nil { |
| 59 | + t.Fatalf("Failed to decode ignition file contents: %v", err) |
| 60 | + } |
| 61 | + |
| 62 | + contentsStr := string(contents) |
| 63 | + |
| 64 | + // Verify each expected line is in the contents |
| 65 | + if !strings.Contains(contentsStr, tt.expectedAutoSizing) { |
| 66 | + t.Errorf("Expected contents to contain %q, but got: %s", tt.expectedAutoSizing, contentsStr) |
| 67 | + } |
| 68 | + |
| 69 | + if !strings.Contains(contentsStr, tt.expectedMemory) { |
| 70 | + t.Errorf("Expected contents to contain %q, but got: %s", tt.expectedMemory, contentsStr) |
| 71 | + } |
| 72 | + |
| 73 | + if !strings.Contains(contentsStr, tt.expectedCPU) { |
| 74 | + t.Errorf("Expected contents to contain %q, but got: %s", tt.expectedCPU, contentsStr) |
| 75 | + } |
| 76 | + |
| 77 | + if !strings.Contains(contentsStr, tt.expectedEphemeral) { |
| 78 | + t.Errorf("Expected contents to contain %q, but got: %s", tt.expectedEphemeral, contentsStr) |
| 79 | + } |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// boolPtr is a helper function to get a pointer to a bool value |
| 85 | +func boolPtr(b bool) *bool { |
| 86 | + return &b |
| 87 | +} |
0 commit comments