Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .web-docs/components/builder/linode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ can also be supplied to override the typical auto-generated key:
for more information on the Images available for use. Examples are `linode/debian12`,
`linode/debian13`, `linode/ubuntu24.04`, `linode/arch`, and `private/12345`.

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

- `private_ip` (bool) - If true, the created Linode will have private networking enabled and assigned
a private IPv4 address.
Expand Down
59 changes: 59 additions & 0 deletions builder/linode/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,44 @@ func TestBuilderPrepare_Size(t *testing.T) {
}
}

func TestBuilderPrepare_SwapSize(t *testing.T) {
t.Run("omitted remains nil", func(t *testing.T) {
var b Builder
config := testConfig()
delete(config, "swap_size")

_, warnings, err := b.Prepare(config)
if len(warnings) > 0 {
t.Fatalf("bad: %#v", warnings)
}
if err != nil {
t.Fatalf("should not have error: %s", err)
}

if b.config.SwapSize != nil {
t.Fatalf("swap_size = %v, want nil", b.config.SwapSize)
}
})

t.Run("explicit zero remains non-nil", func(t *testing.T) {
var b Builder
config := testConfig()
config["swap_size"] = 0

_, warnings, err := b.Prepare(config)
if len(warnings) > 0 {
t.Fatalf("bad: %#v", warnings)
}
if err != nil {
t.Fatalf("should not have error: %s", err)
}

if b.config.SwapSize == nil || *b.config.SwapSize != 0 {
t.Fatalf("swap_size = %v, want pointer to 0", b.config.SwapSize)
}
})
}

func TestBuilderPrepare_Image(t *testing.T) {
var b Builder
config := testConfig()
Expand Down Expand Up @@ -989,6 +1027,27 @@ func TestBuilderPrepare_CustomDisksValidation(t *testing.T) {
}
})

t.Run("IncompatibleSwapSizeZero", func(t *testing.T) {
var b Builder
config := testConfig()
delete(config, "image")
config["swap_size"] = 0
config["disk"] = []map[string]any{
{"label": "boot", "size": 25000, "image": "linode/arch"},
}
config["config"] = []map[string]any{
{"label": "my-config"},
}

_, _, err := b.Prepare(config)
if err == nil {
t.Fatal("expected error with swap_size=0 and custom disks")
}
if !strings.Contains(err.Error(), "swap_size cannot be specified when using custom disks") {
t.Fatalf("expected specific error message, got: %s", err)
}
})

t.Run("IncompatibleStackScriptID", func(t *testing.T) {
var b Builder
config := testConfig()
Expand Down
4 changes: 2 additions & 2 deletions builder/linode/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ type Config struct {
Image string `mapstructure:"image" required:"false"`

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

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

if c.SwapSize > 0 {
if c.SwapSize != nil {
errs = packersdk.MultiErrorAppend(
errs, errors.New("swap_size cannot be specified when using custom disks (create a swap disk instead)"))
}
Expand Down
2 changes: 1 addition & 1 deletion builder/linode/step_create_linode.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (s *stepCreateLinode) Run(ctx context.Context, state multistep.StateBag) mu
if !useCustomDisks {
createOpts.RootPass = c.Comm.Password()
createOpts.Image = c.Image
createOpts.SwapSize = &c.SwapSize
createOpts.SwapSize = c.SwapSize
createOpts.StackScriptID = c.StackScriptID
createOpts.StackScriptData = c.StackScriptData

Expand Down
Loading