File: step_create_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 (176 lines) | stat: -rw-r--r-- 4,375 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package qemu

import (
	"context"
	"fmt"
	"testing"

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

func Test_buildCreateCommand(t *testing.T) {
	type testCase struct {
		Step     *stepCreateDisk
		I        int
		Expected []string
		Reason   string
	}
	testcases := []testCase{
		{
			&stepCreateDisk{
				Format:         "qcow2",
				UseBackingFile: false,
			},
			0,
			[]string{"create", "-f", "qcow2", "target.qcow2", "1234M"},
			"Basic, happy path, no backing store, no extra args",
		},
		{
			&stepCreateDisk{
				Format:             "qcow2",
				DiskImage:          true,
				UseBackingFile:     true,
				AdditionalDiskSize: []string{"1M", "2M"},
			},
			0,
			[]string{"create", "-f", "qcow2", "-b", "source.qcow2", "target.qcow2", "1234M"},
			"Basic, happy path, backing store, additional disks",
		},
		{
			&stepCreateDisk{
				Format:         "qcow2",
				UseBackingFile: true,
				DiskImage:      true,
			},
			1,
			[]string{"create", "-f", "qcow2", "target.qcow2", "1234M"},
			"Basic, happy path, backing store set but not at first index, no extra args",
		},
		{
			&stepCreateDisk{
				Format:         "qcow2",
				UseBackingFile: true,
				DiskImage:      true,
				QemuImgArgs: QemuImgArgs{
					Create: []string{"-foo", "bar"},
				},
			},
			0,
			[]string{"create", "-f", "qcow2", "-b", "source.qcow2", "-foo", "bar", "target.qcow2", "1234M"},
			"Basic, happy path, backing store set, extra args",
		},
		{
			&stepCreateDisk{
				Format:         "qcow2",
				UseBackingFile: true,
				QemuImgArgs: QemuImgArgs{
					Create: []string{"-foo", "bar"},
				},
			},
			1,
			[]string{"create", "-f", "qcow2", "-foo", "bar", "target.qcow2", "1234M"},
			"Basic, happy path, backing store set but not at first index, extra args",
		},
	}

	for _, tc := range testcases {
		state := new(multistep.BasicStateBag)
		state.Put("iso_path", "source.qcow2")
		command := tc.Step.buildCreateCommand("target.qcow2", "1234M", tc.I, state)

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

func Test_StepCreateCalled(t *testing.T) {
	type testCase struct {
		Step     *stepCreateDisk
		Expected []string
		Reason   string
	}
	testcases := []testCase{
		{
			&stepCreateDisk{
				Format:         "qcow2",
				DiskImage:      true,
				DiskSize:       "1M",
				VMName:         "target",
				UseBackingFile: true,
			},
			[]string{
				"create", "-f", "qcow2", "-b", "source.qcow2", "target", "1M",
			},
			"Basic, happy path, backing store, no additional disks",
		},
		{
			&stepCreateDisk{
				Format:         "raw",
				DiskImage:      false,
				DiskSize:       "4M",
				VMName:         "target",
				UseBackingFile: false,
			},
			[]string{
				"create", "-f", "raw", "target", "4M",
			},
			"Basic, happy path, raw, no additional disks",
		},
		{
			&stepCreateDisk{
				Format:             "qcow2",
				DiskImage:          true,
				DiskSize:           "4M",
				VMName:             "target",
				UseBackingFile:     false,
				AdditionalDiskSize: []string{"3M", "8M"},
			},
			[]string{
				"create", "-f", "qcow2", "target-1", "3M",
				"create", "-f", "qcow2", "target-2", "8M",
			},
			"Skips disk creation when disk can be copied",
		},
		{
			&stepCreateDisk{
				Format:         "qcow2",
				DiskImage:      true,
				DiskSize:       "4M",
				VMName:         "target",
				UseBackingFile: false,
			},
			nil,
			"Skips disk creation when disk can be copied",
		},
		{
			&stepCreateDisk{
				Format:             "qcow2",
				DiskImage:          true,
				DiskSize:           "1M",
				VMName:             "target",
				UseBackingFile:     true,
				AdditionalDiskSize: []string{"3M", "8M"},
			},
			[]string{
				"create", "-f", "qcow2", "-b", "source.qcow2", "target", "1M",
				"create", "-f", "qcow2", "target-1", "3M",
				"create", "-f", "qcow2", "target-2", "8M",
			},
			"Basic, happy path, backing store, additional disks",
		},
	}

	for _, tc := range testcases {
		d := new(DriverMock)
		state := copyTestState(t, d)
		state.Put("iso_path", "source.qcow2")
		action := tc.Step.Run(context.TODO(), state)
		if action != multistep.ActionContinue {
			t.Fatalf("Should have gotten an ActionContinue")
		}

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