Skip to content

Commit

Permalink
add test for packer init
Browse files Browse the repository at this point in the history
Signed-off-by: William Albertus Dembo <29192168+walbertus@users.noreply.github.com>
  • Loading branch information
walbertus committed Jan 31, 2024
1 parent bf09995 commit 5d82310
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
58 changes: 58 additions & 0 deletions modules/packer/packer_test.go
Expand Up @@ -2,9 +2,12 @@ package packer

import (
"fmt"
"path/filepath"
"strings"
"testing"

"github.com/gruntwork-io/terratest/modules/files"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -174,3 +177,58 @@ func TestFormatPackerArgs(t *testing.T) {
assert.Equal(t, strings.Join(args, " "), test.expected)
}
}

func TestPackertInitWithFile(t *testing.T) {
t.Parallel()
tests := []struct {
packerTestFile string
}{
{
packerTestFile: "packer.pkr.hcl",
}, {
packerTestFile: "packer.json",
},
}
for _, test := range tests {
packerDirectory := t.TempDir()
packerTestFile := filepath.Join("../../test/fixtures/packer-basic", test.packerTestFile)
packerFile := filepath.Join(packerDirectory, test.packerTestFile)
err := files.CopyFile(packerTestFile, packerFile)
if err != nil {
t.Fatal(err)
}

options := &Options{
Template: packerFile,
WorkingDir: packerDirectory,
}
err = packerInit(t, options)
assert.NoError(t, err)
}
}

func TestPackertInitWithDirectory(t *testing.T) {
t.Parallel()
tests := []struct {
packerTestDirectory string
}{
{
packerTestDirectory: "packer-basic",
},
}
for _, test := range tests {
packerTestDirectory := filepath.Join("../../test/fixtures", test.packerTestDirectory)
packerDirectory, err := files.CopyFolderToTemp(packerTestDirectory, t.Name(), func(path string) bool { return true })
if err != nil {
t.Fatal(err)
}

options := &Options{
Template: ".",
WorkingDir: packerDirectory,
}

err = packerInit(t, options)
assert.NoError(t, err)
}
}
14 changes: 14 additions & 0 deletions test/fixtures/packer-basic/packer.json
@@ -0,0 +1,14 @@
{
"builders": [
{
"type": "null",
"communicator": "none"
}
],
"provisioners": [
{
"type": "shell",
"inline": ["echo hellooooo"]
}
]
}
11 changes: 11 additions & 0 deletions test/fixtures/packer-basic/packer.pkr.hcl
@@ -0,0 +1,11 @@
source "file" "hello" {
content = "hello world"
target = "test.txt"
}

build {
name = "file_hello"
sources = [
"source.file.hello",
]
}

0 comments on commit 5d82310

Please sign in to comment.