2016-12-05 18:24:57 +01:00
|
|
|
package external
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
2017-03-31 12:46:48 +02:00
|
|
|
"path/filepath"
|
2016-12-05 18:24:57 +01:00
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
const testDataSourceConfig_basic = `
|
|
|
|
data "external" "test" {
|
|
|
|
program = ["%s", "cheese"]
|
|
|
|
|
|
|
|
query = {
|
|
|
|
value = "pizza"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output "query_value" {
|
|
|
|
value = "${data.external.test.result["query_value"]}"
|
|
|
|
}
|
|
|
|
|
|
|
|
output "argument" {
|
|
|
|
value = "${data.external.test.result["argument"]}"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestDataSource_basic(t *testing.T) {
|
|
|
|
programPath, err := buildDataSourceTestProgram()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.UnitTest(t, resource.TestCase{
|
|
|
|
Providers: testProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: fmt.Sprintf(testDataSourceConfig_basic, programPath),
|
|
|
|
Check: func(s *terraform.State) error {
|
|
|
|
_, ok := s.RootModule().Resources["data.external.test"]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("missing data resource")
|
|
|
|
}
|
|
|
|
|
|
|
|
outputs := s.RootModule().Outputs
|
|
|
|
|
|
|
|
if outputs["argument"] == nil {
|
|
|
|
return fmt.Errorf("missing 'argument' output")
|
|
|
|
}
|
|
|
|
if outputs["query_value"] == nil {
|
|
|
|
return fmt.Errorf("missing 'query_value' output")
|
|
|
|
}
|
|
|
|
|
|
|
|
if outputs["argument"].Value != "cheese" {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"'argument' output is %q; want 'cheese'",
|
|
|
|
outputs["argument"].Value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if outputs["query_value"].Value != "pizza" {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"'query_value' output is %q; want 'pizza'",
|
|
|
|
outputs["query_value"].Value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const testDataSourceConfig_error = `
|
|
|
|
data "external" "test" {
|
|
|
|
program = ["%s"]
|
|
|
|
|
|
|
|
query = {
|
|
|
|
fail = "true"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestDataSource_error(t *testing.T) {
|
|
|
|
programPath, err := buildDataSourceTestProgram()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.UnitTest(t, resource.TestCase{
|
|
|
|
Providers: testProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: fmt.Sprintf(testDataSourceConfig_error, programPath),
|
|
|
|
ExpectError: regexp.MustCompile("I was asked to fail"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildDataSourceTestProgram() (string, error) {
|
|
|
|
// We have a simple Go program that we use as a stub for testing.
|
|
|
|
cmd := exec.Command(
|
|
|
|
"go", "install",
|
|
|
|
"github.com/hashicorp/terraform/builtin/providers/external/test-programs/tf-acc-external-data-source",
|
|
|
|
)
|
|
|
|
err := cmd.Run()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to build test stub program: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-03-31 12:46:48 +02:00
|
|
|
gopath := os.Getenv("GOPATH")
|
|
|
|
if gopath == "" {
|
|
|
|
gopath = filepath.Join(os.Getenv("HOME") + "/go")
|
|
|
|
}
|
|
|
|
|
2016-12-05 18:24:57 +01:00
|
|
|
programPath := path.Join(
|
2017-03-31 12:46:48 +02:00
|
|
|
filepath.SplitList(gopath)[0], "bin", "tf-acc-external-data-source",
|
2016-12-05 18:24:57 +01:00
|
|
|
)
|
|
|
|
return programPath, nil
|
|
|
|
}
|