Skip to content

Commit

Permalink
test for #161
Browse files Browse the repository at this point in the history
  • Loading branch information
NorseGaud committed Aug 29, 2023
1 parent d39014e commit e522ecf
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
42 changes: 42 additions & 0 deletions builder/anka/step_clone_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions examples/clone-existing.pkr.hcl
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
42 changes: 42 additions & 0 deletions examples/pull-and-clone-existing.pkr.hcl
Original file line number Diff line number Diff line change
@@ -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"
]
}
}

0 comments on commit e522ecf

Please sign in to comment.