File: step_resize_disk_test.go

package info (click to toggle)
packer 1.6.6%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 32,016 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (77 lines) | stat: -rw-r--r-- 1,643 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package qemu

import (
	"context"
	"fmt"
	"testing"

	"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
	"github.com/stretchr/testify/assert"
)

func TestStepResizeDisk_Skips(t *testing.T) {
	testConfigs := []*Config{
		&Config{
			DiskImage:      false,
			SkipResizeDisk: false,
		},
		&Config{
			DiskImage:      false,
			SkipResizeDisk: true,
		},
	}
	for _, config := range testConfigs {
		state := testState(t)
		driver := state.Get("driver").(*DriverMock)

		state.Put("config", config)
		step := new(stepResizeDisk)

		// Test the run
		if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
			t.Fatalf("bad action: %#v", action)
		}
		if _, ok := state.GetOk("error"); ok {
			t.Fatal("should NOT have error")
		}
		if len(driver.QemuImgCalls) > 0 {
			t.Fatal("should NOT have called qemu-img")
		}
	}
}

func Test_buildResizeCommand(t *testing.T) {
	type testCase struct {
		Step     *stepResizeDisk
		Expected []string
		Reason   string
	}
	testcases := []testCase{
		{
			&stepResizeDisk{
				Format:   "qcow2",
				DiskSize: "1234M",
			},
			[]string{"resize", "-f", "qcow2", "source.qcow", "1234M"},
			"no extra args",
		},
		{
			&stepResizeDisk{
				Format:   "qcow2",
				DiskSize: "1234M",
				QemuImgArgs: QemuImgArgs{
					Resize: []string{"-foo", "bar"},
				},
			},
			[]string{"resize", "-f", "qcow2", "-foo", "bar", "source.qcow", "1234M"},
			"one set of extra args",
		},
	}

	for _, tc := range testcases {
		command := tc.Step.buildResizeCommand("source.qcow")

		assert.Equal(t, command, tc.Expected,
			fmt.Sprintf("%s. Expected %#v", tc.Reason, tc.Expected))
	}
}