2020-09-08 14:32:08 -04:00
|
|
|
package qemu
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-16 16:40:23 -04:00
|
|
|
"fmt"
|
2020-09-08 14:32:08 -04:00
|
|
|
"testing"
|
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-09-16 16:40:23 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-09-08 14:32:08 -04:00
|
|
|
)
|
|
|
|
|
2020-09-16 16:40:23 -04:00
|
|
|
func TestStepResizeDisk_Skips(t *testing.T) {
|
|
|
|
testConfigs := []*Config{
|
|
|
|
&Config{
|
|
|
|
DiskImage: false,
|
|
|
|
SkipResizeDisk: false,
|
|
|
|
},
|
|
|
|
&Config{
|
|
|
|
DiskImage: false,
|
|
|
|
SkipResizeDisk: true,
|
|
|
|
},
|
2020-09-08 14:32:08 -04:00
|
|
|
}
|
2020-09-16 16:40:23 -04:00
|
|
|
for _, config := range testConfigs {
|
|
|
|
state := testState(t)
|
|
|
|
driver := state.Get("driver").(*DriverMock)
|
2020-09-08 14:32:08 -04:00
|
|
|
|
2020-09-16 16:40:23 -04:00
|
|
|
state.Put("config", config)
|
|
|
|
step := new(stepResizeDisk)
|
2020-09-08 14:32:08 -04:00
|
|
|
|
2020-09-16 16:40:23 -04:00
|
|
|
// 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")
|
|
|
|
}
|
2020-09-08 14:32:08 -04:00
|
|
|
}
|
2020-09-16 16:40:23 -04:00
|
|
|
}
|
2020-09-08 14:32:08 -04:00
|
|
|
|
2020-09-16 16:40:23 -04:00
|
|
|
func Test_buildResizeCommand(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
Step *stepResizeDisk
|
|
|
|
Expected []string
|
|
|
|
Reason string
|
2020-09-08 14:32:08 -04:00
|
|
|
}
|
2020-09-16 16:40:23 -04:00
|
|
|
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",
|
|
|
|
},
|
2020-09-08 14:32:08 -04:00
|
|
|
}
|
|
|
|
|
2020-09-16 16:40:23 -04:00
|
|
|
for _, tc := range testcases {
|
|
|
|
command := tc.Step.buildResizeCommand("source.qcow")
|
2020-09-08 14:32:08 -04:00
|
|
|
|
2020-09-16 16:40:23 -04:00
|
|
|
assert.Equal(t, command, tc.Expected,
|
|
|
|
fmt.Sprintf("%s. Expected %#v", tc.Reason, tc.Expected))
|
2020-09-08 14:32:08 -04:00
|
|
|
}
|
|
|
|
}
|