Files

64 lines
1.5 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package secrets
import (
_ "embed"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"testing"
"github.com/hashicorp/packer-plugin-sdk/acctest"
)
//go:embed test-fixtures/template.pkr.hcl
var testDatasourceHCL2Basic string
// Run with: KMS_SERVICE_TOKEN=... PACKER_ACC=1 go test -count 1 -v ./datasource/secrets/data_acc_test.go
//
// Folder / in dev environment must contain a secret "FOO" with value "bar" for this test to pass.
func TestAccKMSSecrets(t *testing.T) {
testCase := &acctest.PluginTestCase{
Name: "kms_secrets_datasource_basic_test",
Setup: func() error {
return nil
},
Teardown: func() error {
return nil
},
Template: testDatasourceHCL2Basic,
Type: "kms-secrets",
Check: func(buildCommand *exec.Cmd, logfile string) error {
if buildCommand.ProcessState != nil {
if buildCommand.ProcessState.ExitCode() != 0 {
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
}
}
logs, err := os.Open(logfile)
if err != nil {
return fmt.Errorf("Unable find %s", logfile)
}
defer logs.Close()
logsBytes, err := io.ReadAll(logs)
if err != nil {
return fmt.Errorf("Unable to read %s", logfile)
}
logsString := string(logsBytes)
fooLog := "null.basic-example: secret_value: bar"
if matched, _ := regexp.MatchString(fooLog+".*", logsString); !matched {
t.Fatalf("logs doesn't contain expected foo value %q", logsString)
}
return nil
},
}
acctest.TestPlugin(t, testCase)
}