From e522ecf3e072967343b2d988eaab34fe539faade Mon Sep 17 00:00:00 2001 From: Nathan Pierce Date: Mon, 28 Aug 2023 20:14:24 -0400 Subject: [PATCH] test for https://github.com/veertuinc/packer-plugin-veertu-anka/issues/161 --- Makefile | 2 +- builder/anka/step_clone_vm_test.go | 42 ++++++++++++++++++++++++ examples/clone-existing.pkr.hcl | 9 +++++ examples/pull-and-clone-existing.pkr.hcl | 42 ++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 examples/pull-and-clone-existing.pkr.hcl diff --git a/Makefile b/Makefile index cc0f4761..fa87d281 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ export PATH := $(shell go env GOPATH)/bin:$(PATH) .DEFAULT_GOAL := help -all: clean go.releaser anka.clean-images anka.clean-clones generate-docs +all: clean go.releaser anka.clean-images anka.clean-clones generate-docs install #help: @ List available tasks on this project help: diff --git a/builder/anka/step_clone_vm_test.go b/builder/anka/step_clone_vm_test.go index 2b692e04..15ebceb6 100644 --- a/builder/anka/step_clone_vm_test.go +++ b/builder/anka/step_clone_vm_test.go @@ -265,6 +265,48 @@ func TestCloneVMRun(t *testing.T) { assert.Equal(t, multistep.ActionContinue, stepAction) }) + t.Run("pull from anka registry with specific tag", func(t *testing.T) { + sourceVMTag := "vanilla" + config := &Config{ + AlwaysFetch: true, + VMName: "foo", + SourceVMName: "source_foo", + SourceVMTag: sourceVMTag, + PackerConfig: common.PackerConfig{ + PackerBuilderType: "veertu-anka-vm-clone", + }, + } + registryParams := client.RegistryParams{} + registryPullParams := client.RegistryPullParams{ + VMID: config.SourceVMName, + Tag: sourceVMTag, + Local: false, + Shrink: false, + } + + step.vmName = config.VMName + + state.Put("vm_name", step.vmName) + state.Put("config", config) + + gomock.InOrder( + ankaClient.EXPECT().RegistryPull(registryParams, registryPullParams).Return(nil).Times(1), + ankaClient.EXPECT().Show(config.SourceVMName).Return(sourceShowResponse, nil).Times(1), + ankaClient.EXPECT().Clone(client.CloneParams{VMName: step.vmName, SourceUUID: sourceShowResponse.UUID}).Return(nil).Times(1), + ankaClient.EXPECT().Show(step.vmName).Return(clonedShowResponse, nil).Times(1), + ) + + mockui := packer.MockUi{} + mockui.Say(fmt.Sprintf("Pulling source VM %s with vanilla tag from Anka Registry", config.SourceVMName)) + mockui.Say(fmt.Sprintf("Cloning source VM %s into a new virtual machine: %s", sourceShowResponse.Name, step.vmName)) + + stepAction := step.Run(ctx, state) + + assert.Equal(t, mockui.SayMessages[0].Message, "Pulling source VM source_foo with vanilla tag from Anka Registry") + assert.Equal(t, mockui.SayMessages[1].Message, "Cloning source VM source_foo into a new virtual machine: foo") + assert.Equal(t, multistep.ActionContinue, stepAction) + }) + t.Run("clone vm with always fetch flag when source vm does not exist in anka registry should throw error", func(t *testing.T) { config := &Config{ AlwaysFetch: true, diff --git a/examples/clone-existing.pkr.hcl b/examples/clone-existing.pkr.hcl index d3c5d64a..26fed945 100644 --- a/examples/clone-existing.pkr.hcl +++ b/examples/clone-existing.pkr.hcl @@ -1,3 +1,12 @@ +// packer { +// required_plugins { +// veertu-anka = { +// version = "= v3.2.1" +// source = "github.com/veertuinc/veertu-anka" +// } +// } +// } + variable "source_vm_name" { type = string default = "anka-packer-base-macos" diff --git a/examples/pull-and-clone-existing.pkr.hcl b/examples/pull-and-clone-existing.pkr.hcl new file mode 100644 index 00000000..d49a304a --- /dev/null +++ b/examples/pull-and-clone-existing.pkr.hcl @@ -0,0 +1,42 @@ +// packer { +// required_plugins { +// veertu-anka = { +// version = "= v3.2.1" +// source = "github.com/veertuinc/veertu-anka" +// } +// } +// } + +variable "source_vm_name" { + type = string + default = "anka-packer-base-macos" +} + +variable "source_vm_tag" { + type = string + default = "" +} + +variable "vm_name" { + type = string + default = "anka-packer-from-source" +} + +source "veertu-anka-vm-clone" "anka-packer-from-source" { + vm_name = "${var.vm_name}" + source_vm_name = "${var.source_vm_name}" + source_vm_tag = "${var.source_vm_tag}" +} + +build { + sources = [ + "source.veertu-anka-vm-clone.anka-packer-from-source", + ] + + provisioner "shell" { + inline = [ + "echo hello world", + "echo llamas rock" + ] + } +} \ No newline at end of file