From 83ee4e7d13fcb63535160737fd9a040230eff949 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Tue, 15 Sep 2020 16:42:08 -0700 Subject: [PATCH] miscellaneous qemu cleanup. Fix generator strings; generate code fix diskimage logic linting fix tests --- builder/qemu/builder.go | 3 --- builder/qemu/config.go | 3 +++ .../qemu/{builder.hcl2spec.go => config.hcl2spec.go} | 0 builder/qemu/config_test.go | 12 +++++++++--- builder/qemu/step_run.go | 2 +- builder/qemu/step_run_test.go | 4 +++- builder/qemu/step_wait_guest_address.go | 10 +++++----- .../partials/builder/qemu/Config-not-required.mdx | 2 +- 8 files changed, 22 insertions(+), 14 deletions(-) rename builder/qemu/{builder.hcl2spec.go => config.hcl2spec.go} (100%) diff --git a/builder/qemu/builder.go b/builder/qemu/builder.go index 2b68e1035..b80afe275 100644 --- a/builder/qemu/builder.go +++ b/builder/qemu/builder.go @@ -1,6 +1,3 @@ -//go:generate struct-markdown -//go:generate mapstructure-to-hcl2 -type Config - package qemu import ( diff --git a/builder/qemu/config.go b/builder/qemu/config.go index 66fb1c3f0..5c64a3f64 100644 --- a/builder/qemu/config.go +++ b/builder/qemu/config.go @@ -1,3 +1,6 @@ +//go:generate struct-markdown +//go:generate mapstructure-to-hcl2 -type Config + package qemu import ( diff --git a/builder/qemu/builder.hcl2spec.go b/builder/qemu/config.hcl2spec.go similarity index 100% rename from builder/qemu/builder.hcl2spec.go rename to builder/qemu/config.hcl2spec.go diff --git a/builder/qemu/config_test.go b/builder/qemu/config_test.go index 9ca3155cd..67cb3e13a 100644 --- a/builder/qemu/config_test.go +++ b/builder/qemu/config_test.go @@ -519,9 +519,15 @@ func TestBuilderPrepare_SSHPrivateKey(t *testing.T) { } // Test good contents - tf.Seek(0, 0) - tf.Truncate(0) - tf.Write([]byte(testPem)) + if _, err := tf.Seek(0, 0); err != nil { + t.Fatalf("errorf getting key") + } + if err := tf.Truncate(0); err != nil { + t.Fatalf("errorf getting key") + } + if _, err := tf.Write([]byte(testPem)); err != nil { + t.Fatalf("errorf getting key") + } config["ssh_private_key_file"] = tf.Name() c = Config{} warns, err = c.Prepare(config) diff --git a/builder/qemu/step_run.go b/builder/qemu/step_run.go index 3943667c5..98b913cbd 100644 --- a/builder/qemu/step_run.go +++ b/builder/qemu/step_run.go @@ -35,7 +35,7 @@ func (s *stepRun) Run(ctx context.Context, state multistep.StateBag) multistep.S // installation CD or a pre-baked image bootDrive := "once=d" message := "Starting VM, booting from CD-ROM" - if !s.DiskImage { + if s.DiskImage { bootDrive = "c" message = "Starting VM, booting disk image" } diff --git a/builder/qemu/step_run_test.go b/builder/qemu/step_run_test.go index 426665a5d..1facfce12 100644 --- a/builder/qemu/step_run_test.go +++ b/builder/qemu/step_run_test.go @@ -76,9 +76,11 @@ func Test_CDFilesPath(t *testing.T) { "-machine", "type=,accel=", "-device", ",netdev=user.0", "-drive", "file=/path/to/test.iso,index=0,media=cdrom", - "-drive", "file=fake_cd_path.iso,index=1,media=cdrom ", + "-drive", "file=fake_cd_path.iso,index=1,media=cdrom", } + assert.ElementsMatch(t, args, expected, fmt.Sprintf("unexpected generated args: %#v", args)) + // cd_path is set and DiskImage is true config := &Config{ DiskImage: true, diff --git a/builder/qemu/step_wait_guest_address.go b/builder/qemu/step_wait_guest_address.go index 0fa946a4c..7e6f9ad1e 100644 --- a/builder/qemu/step_wait_guest_address.go +++ b/builder/qemu/step_wait_guest_address.go @@ -21,15 +21,15 @@ import ( type stepWaitGuestAddress struct { CommunicatorType string NetBridge string - timeout time.Duration + + timeout time.Duration } func (s *stepWaitGuestAddress) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { - config := state.Get("config").(*Config) ui := state.Get("ui").(packer.Ui) if s.CommunicatorType == "none" { - ui.Message("Not using a communicator -- skipping StepWaitGuestAddress") + ui.Message("No communicator is configured -- skipping StepWaitGuestAddress") return multistep.ActionContinue } if s.NetBridge == "" { @@ -41,9 +41,9 @@ func (s *stepWaitGuestAddress) Run(ctx context.Context, state multistep.StateBag ctx, cancel := context.WithTimeout(ctx, s.timeout) defer cancel() - ui.Say(fmt.Sprintf("Waiting for the guest address to become available in the %s network bridge...", config.NetBridge)) + ui.Say(fmt.Sprintf("Waiting for the guest address to become available in the %s network bridge...", s.NetBridge)) for { - guestAddress := getGuestAddress(qmpMonitor, config.NetBridge, "user.0") + guestAddress := getGuestAddress(qmpMonitor, s.NetBridge, "user.0") if guestAddress != "" { log.Printf("Found guest address %s", guestAddress) state.Put("guestAddress", guestAddress) diff --git a/website/pages/partials/builder/qemu/Config-not-required.mdx b/website/pages/partials/builder/qemu/Config-not-required.mdx index 802b4e0c8..ef4d2f935 100644 --- a/website/pages/partials/builder/qemu/Config-not-required.mdx +++ b/website/pages/partials/builder/qemu/Config-not-required.mdx @@ -1,4 +1,4 @@ - + - `iso_skip_cache` (bool) - Use iso from provided url. Qemu must support curl block device. This defaults to `false`.