-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
63 lines (55 loc) · 2.17 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
_ "embed"
"os"
"os/exec"
"regexp"
"testing"
"github.com/hashicorp/packer-plugin-sdk/acctest"
)
//go:embed fixtures/test.pkr.hcl
var testTemplate string
// testinfra basic acceptance testing function
func TestProvisioner(test *testing.T) {
// initialize acceptance test config struct
testCase := &acctest.PluginTestCase{
Name: "testinfra_provisioner_test",
Init: false,
Setup: func() error { return nil },
Template: testTemplate,
Type: "provisioner",
Check: func(buildCommand *exec.Cmd, logfile string) error {
// verify good exit code from packer process
if buildCommand.ProcessState != nil && buildCommand.ProcessState.ExitCode() != 0 {
test.Errorf("unexpected exit code from Packer build; logfile: %s", logfile)
return nil
}
// assign logfile from content
logsBytes, err := os.ReadFile(logfile)
if err != nil {
test.Errorf("unable to read logfile at: %s", logfile)
return err
}
// convert log byte slice to string
logsString := string(logsBytes)
// verify logfile content for each communicator
if dockerMatches, _ := regexp.MatchString("docker.ubuntu: packer plugin testinfra provisioning complete.*", logsString); !dockerMatches {
test.Errorf("logs do not contain expected docker testinfra value in logfile: %s", logfile)
}
if nullMatches, _ := regexp.MatchString("null.vbox: packer plugin testinfra provisioning complete.*", logsString); !nullMatches {
test.Errorf("logs do not contain expected ssh testinfra values in logfile: %s", logfile)
}
//TODO: https://github.com/hashicorp/packer-plugin-virtualbox/issues/77
/*if vboxMatched, _ := regexp.MatchString("virtualbox-vm.ubuntu: packer plugin testinfra provisioning complete.*", logsString); !vboxMatched {
test.Fatalf("logs do not contain expected local testinfra values in logfile: %s", logfile)
}*/
// verify testinfra output is as expected
if testsMatches, _ := regexp.MatchString("2 passed in.*", logsString); !testsMatches {
test.Errorf("logs do not contain expected testinfra value: %s", logsString)
}
return nil
},
}
// invoke acceptance test function
acctest.TestPlugin(test, testCase)
}