github.com/hashicorp/packer@v1.14.3/acctest/plugin/plugin_acc_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 // plugin_acc_test.go should contain acceptance tests for features related to 5 // installing, discovering and running plugins. 6 package plugin 7 8 import ( 9 _ "embed" 10 "fmt" 11 "io" 12 "os" 13 "os/exec" 14 "path/filepath" 15 "regexp" 16 "testing" 17 18 "github.com/hashicorp/packer-plugin-sdk/acctest" 19 "github.com/hashicorp/packer-plugin-sdk/acctest/testutils" 20 "github.com/hashicorp/packer/hcl2template/addrs" 21 "github.com/hashicorp/packer/packer" 22 ) 23 24 //go:embed test-fixtures/basic-amazon-ebs.pkr.hcl 25 var basicAmazonEbsHCL2Template string 26 27 func TestAccInitAndBuildBasicAmazonEbs(t *testing.T) { 28 plugin := addrs.Plugin{ 29 Source: "github.com/hashicorp/amazon", 30 } 31 testCase := &acctest.PluginTestCase{ 32 Name: "amazon-ebs_basic_plugin_init_and_build_test", 33 Setup: func() error { 34 return cleanupPluginInstallation(plugin) 35 }, 36 Template: basicAmazonEbsHCL2Template, 37 Type: "amazon-ebs", 38 Init: true, 39 CheckInit: func(initCommand *exec.Cmd, logfile string) error { 40 if initCommand.ProcessState != nil { 41 if initCommand.ProcessState.ExitCode() != 0 { 42 return fmt.Errorf("Bad exit code. Logfile: %s", logfile) 43 } 44 } 45 logs, err := os.Open(logfile) 46 if err != nil { 47 return fmt.Errorf("Unable find %s", logfile) 48 } 49 defer logs.Close() 50 51 logsBytes, err := io.ReadAll(logs) 52 if err != nil { 53 return fmt.Errorf("Unable to read %s", logfile) 54 } 55 initOutput := string(logsBytes) 56 return checkPluginInstallation(initOutput, plugin) 57 }, 58 Check: func(buildCommand *exec.Cmd, logfile string) error { 59 if buildCommand.ProcessState != nil { 60 if buildCommand.ProcessState.ExitCode() != 0 { 61 return fmt.Errorf("Bad exit code. Logfile: %s", logfile) 62 } 63 } 64 return nil 65 }, 66 } 67 acctest.TestPlugin(t, testCase) 68 } 69 70 func pluginDirectory(plugin addrs.Plugin) (string, error) { 71 pluginDir, err := packer.PluginFolder() 72 if err != nil { 73 return "", err 74 } 75 76 pluginParts := []string{pluginDir} 77 pluginParts = append(pluginParts, plugin.Parts()...) 78 return filepath.Join(pluginParts...), nil 79 } 80 81 func cleanupPluginInstallation(plugin addrs.Plugin) error { 82 pluginPath, err := pluginDirectory(plugin) 83 if err != nil { 84 return err 85 } 86 testutils.CleanupFiles(pluginPath) 87 return nil 88 } 89 90 func checkPluginInstallation(initOutput string, plugin addrs.Plugin) error { 91 expectedInitLog := "Installed plugin " + plugin.String() 92 if matched, _ := regexp.MatchString(expectedInitLog+".*", initOutput); !matched { 93 return fmt.Errorf("logs doesn't contain expected foo value %q", initOutput) 94 } 95 96 pluginPath, err := pluginDirectory(plugin) 97 if err != nil { 98 return err 99 } 100 101 if !testutils.FileExists(pluginPath) { 102 return fmt.Errorf("%s plugin installation not found", plugin.String()) 103 } 104 return nil 105 }