Skip to content

Commit 881c278

Browse files
TPT-4319: change swap_size to pointer type and update related logic (#418)
* fix: change swap_size to pointer type and update related logic * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Add IncompatibleSwapSizeZero test case * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * make generate --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 0c0ff2c commit 881c278

4 files changed

Lines changed: 63 additions & 4 deletions

File tree

.web-docs/components/builder/linode/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ can also be supplied to override the typical auto-generated key:
8888
for more information on the Images available for use. Examples are `linode/debian12`,
8989
`linode/debian13`, `linode/ubuntu24.04`, `linode/arch`, and `private/12345`.
9090

91-
- `swap_size` (int) - The disk size (MiB) allocated for swap space.
91+
- `swap_size` (\*int) - The disk size (MiB) allocated for swap space.
9292

9393
- `private_ip` (bool) - If true, the created Linode will have private networking enabled and assigned
9494
a private IPv4 address.

builder/linode/builder_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,44 @@ func TestBuilderPrepare_Size(t *testing.T) {
123123
}
124124
}
125125

126+
func TestBuilderPrepare_SwapSize(t *testing.T) {
127+
t.Run("omitted remains nil", func(t *testing.T) {
128+
var b Builder
129+
config := testConfig()
130+
delete(config, "swap_size")
131+
132+
_, warnings, err := b.Prepare(config)
133+
if len(warnings) > 0 {
134+
t.Fatalf("bad: %#v", warnings)
135+
}
136+
if err != nil {
137+
t.Fatalf("should not have error: %s", err)
138+
}
139+
140+
if b.config.SwapSize != nil {
141+
t.Fatalf("swap_size = %v, want nil", b.config.SwapSize)
142+
}
143+
})
144+
145+
t.Run("explicit zero remains non-nil", func(t *testing.T) {
146+
var b Builder
147+
config := testConfig()
148+
config["swap_size"] = 0
149+
150+
_, warnings, err := b.Prepare(config)
151+
if len(warnings) > 0 {
152+
t.Fatalf("bad: %#v", warnings)
153+
}
154+
if err != nil {
155+
t.Fatalf("should not have error: %s", err)
156+
}
157+
158+
if b.config.SwapSize == nil || *b.config.SwapSize != 0 {
159+
t.Fatalf("swap_size = %v, want pointer to 0", b.config.SwapSize)
160+
}
161+
})
162+
}
163+
126164
func TestBuilderPrepare_Image(t *testing.T) {
127165
var b Builder
128166
config := testConfig()
@@ -989,6 +1027,27 @@ func TestBuilderPrepare_CustomDisksValidation(t *testing.T) {
9891027
}
9901028
})
9911029

1030+
t.Run("IncompatibleSwapSizeZero", func(t *testing.T) {
1031+
var b Builder
1032+
config := testConfig()
1033+
delete(config, "image")
1034+
config["swap_size"] = 0
1035+
config["disk"] = []map[string]any{
1036+
{"label": "boot", "size": 25000, "image": "linode/arch"},
1037+
}
1038+
config["config"] = []map[string]any{
1039+
{"label": "my-config"},
1040+
}
1041+
1042+
_, _, err := b.Prepare(config)
1043+
if err == nil {
1044+
t.Fatal("expected error with swap_size=0 and custom disks")
1045+
}
1046+
if !strings.Contains(err.Error(), "swap_size cannot be specified when using custom disks") {
1047+
t.Fatalf("expected specific error message, got: %s", err)
1048+
}
1049+
})
1050+
9921051
t.Run("IncompatibleStackScriptID", func(t *testing.T) {
9931052
var b Builder
9941053
config := testConfig()

builder/linode/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ type Config struct {
288288
Image string `mapstructure:"image" required:"false"`
289289

290290
// The disk size (MiB) allocated for swap space.
291-
SwapSize int `mapstructure:"swap_size" required:"false"`
291+
SwapSize *int `mapstructure:"swap_size" required:"false"`
292292

293293
// If true, the created Linode will have private networking enabled and assigned
294294
// a private IPv4 address.
@@ -720,7 +720,7 @@ func (c *Config) Prepare(raws ...any) ([]string, error) {
720720
errs, errors.New("authorized_users cannot be specified when using custom disks (specify in disk blocks instead)"))
721721
}
722722

723-
if c.SwapSize > 0 {
723+
if c.SwapSize != nil {
724724
errs = packersdk.MultiErrorAppend(
725725
errs, errors.New("swap_size cannot be specified when using custom disks (create a swap disk instead)"))
726726
}

builder/linode/step_create_linode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (s *stepCreateLinode) Run(ctx context.Context, state multistep.StateBag) mu
168168
if !useCustomDisks {
169169
createOpts.RootPass = c.Comm.Password()
170170
createOpts.Image = c.Image
171-
createOpts.SwapSize = &c.SwapSize
171+
createOpts.SwapSize = c.SwapSize
172172
createOpts.StackScriptID = c.StackScriptID
173173
createOpts.StackScriptData = c.StackScriptData
174174

0 commit comments

Comments
 (0)