From 5eb600bf88a47976bf52d340d158f573f9c63cfc Mon Sep 17 00:00:00 2001 From: Calle Pettersson Date: Wed, 13 Mar 2019 21:21:14 +0100 Subject: [PATCH] Add draft of step test --- .../proxmox/step_finalize_template_config.go | 7 ++- .../step_finalize_template_config_test.go | 63 +++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 builder/proxmox/step_finalize_template_config_test.go diff --git a/builder/proxmox/step_finalize_template_config.go b/builder/proxmox/step_finalize_template_config.go index ab74b245b..2e442654a 100644 --- a/builder/proxmox/step_finalize_template_config.go +++ b/builder/proxmox/step_finalize_template_config.go @@ -15,9 +15,14 @@ import ( // unmounting the installation ISO. type stepFinalizeTemplateConfig struct{} +type templateFinalizer interface { + GetVmConfig(*proxmox.VmRef) (map[string]interface{}, error) + SetVmConfig(*proxmox.VmRef, map[string]interface{}) (string, error) +} + func (s *stepFinalizeTemplateConfig) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) - client := state.Get("proxmoxClient").(*proxmox.Client) + client := state.Get("proxmoxClient").(templateFinalizer) c := state.Get("config").(*Config) vmRef := state.Get("vmRef").(*proxmox.VmRef) diff --git a/builder/proxmox/step_finalize_template_config_test.go b/builder/proxmox/step_finalize_template_config_test.go new file mode 100644 index 000000000..1f0a82541 --- /dev/null +++ b/builder/proxmox/step_finalize_template_config_test.go @@ -0,0 +1,63 @@ +package proxmox + +import ( + "context" + "testing" + + "github.com/Telmate/proxmox-api-go/proxmox" + "github.com/hashicorp/packer/helper/multistep" + "github.com/hashicorp/packer/packer" +) + +type finalizerMock struct { + getConfig func() (map[string]interface{}, error) + setConfig func(map[string]interface{}) (string, error) +} + +func (m finalizerMock) GetVmConfig(*proxmox.VmRef) (map[string]interface{}, error) { + return m.getConfig() +} +func (m finalizerMock) SetVmConfig(vmref *proxmox.VmRef, c map[string]interface{}) (string, error) { + return m.setConfig(c) +} + +func TestTemplateFinalize(t *testing.T) { + finalizer := finalizerMock{ + getConfig: func() (map[string]interface{}, error) { + return map[string]interface{}{ + "name": "dummy", + "description": "Packer ephemeral build VM", + "ide2": "local:iso/Fedora-Server-dvd-x86_64-29-1.2.iso,media=cdrom", + }, nil + }, + setConfig: func(c map[string]interface{}) (string, error) { + if c["name"] != "my-template" { + t.Errorf("Expected name to be my-template, got %q", c["name"]) + } + if c["description"] != "foo" { + t.Errorf("Expected description to be foo, got %q", c["description"]) + } + if c["ide2"] != "none,media=cdrom" { + t.Errorf("Expected ide2 to be none,media=cdrom, got %q", c["ide2"]) + } + + return "", nil + }, + } + + state := new(multistep.BasicStateBag) + state.Put("ui", packer.TestUi(t)) + state.Put("config", &Config{ + TemplateName: "my-template", + TemplateDescription: "foo", + UnmountISO: true, + }) + state.Put("vmRef", proxmox.NewVmRef(1)) + state.Put("proxmoxClient", finalizer) + + step := stepFinalizeTemplateConfig{} + action := step.Run(context.TODO(), state) + if action != multistep.ActionContinue { + t.Error("Expected action to be Continue, got Halt") + } +}