From 2e2374b6be16edc6811c71abfa15bfcc73e46d36 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Tue, 5 May 2020 18:52:31 -0700 Subject: [PATCH] add tests --- builder/virtualbox/common/step_export_test.go | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/builder/virtualbox/common/step_export_test.go b/builder/virtualbox/common/step_export_test.go index 6d37b3150..366515d07 100644 --- a/builder/virtualbox/common/step_export_test.go +++ b/builder/virtualbox/common/step_export_test.go @@ -43,3 +43,71 @@ func TestStepExport(t *testing.T) { t.Fatal("bad") } } + +func TestStepExport_OutputPath(t *testing.T) { + type testCase struct { + Step *StepExport + Expected string + Reason string + } + tcs := []testCase{ + { + Step: &StepExport{ + Format: "ova", + OutputDir: "output-dir", + OutputFilename: "output-filename", + }, + Expected: "output-dir/output-filename.ova", + Reason: "output_filename should not be vmName if set.", + }, + { + Step: &StepExport{ + Format: "ovf", + OutputDir: "output-dir", + OutputFilename: "", + }, + Expected: "output-dir/foo.ovf", + Reason: "output_filename should default to vmName.", + }, + } + for _, tc := range tcs { + state := testState(t) + state.Put("vmName", "foo") + + // Test the run + if action := tc.Step.Run(context.Background(), state); action != multistep.ActionContinue { + t.Fatalf("bad action: %#v", action) + } + + // Test output state + path, ok := state.GetOk("exportPath") + if !ok { + t.Fatal("should set exportPath") + } + if path != tc.Expected { + t.Fatalf("Expected %s didn't match received %s: %s", tc.Expected, path, tc.Reason) + } + } +} + +func TestStepExport_SkipExport(t *testing.T) { + state := testState(t) + step := StepExport{SkipExport: true} + + state.Put("vmName", "foo") + + driver := state.Get("driver").(*DriverMock) + + // 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") + } + // Test driver + if len(driver.VBoxManageCalls) != 0 { + t.Fatal("shouldn't have called vboxmanage; skip_export was set.") + } + +}