github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/external/data_source_test.go (about)

     1  package external
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"path"
     8  	"path/filepath"
     9  	"regexp"
    10  	"testing"
    11  
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/terraform"
    14  )
    15  
    16  const testDataSourceConfig_basic = `
    17  data "external" "test" {
    18    program = ["%s", "cheese"]
    19  
    20    query = {
    21      value = "pizza"
    22    }
    23  }
    24  
    25  output "query_value" {
    26    value = "${data.external.test.result["query_value"]}"
    27  }
    28  
    29  output "argument" {
    30    value = "${data.external.test.result["argument"]}"
    31  }
    32  `
    33  
    34  func TestDataSource_basic(t *testing.T) {
    35  	programPath, err := buildDataSourceTestProgram()
    36  	if err != nil {
    37  		t.Fatal(err)
    38  		return
    39  	}
    40  
    41  	resource.UnitTest(t, resource.TestCase{
    42  		Providers: testProviders,
    43  		Steps: []resource.TestStep{
    44  			resource.TestStep{
    45  				Config: fmt.Sprintf(testDataSourceConfig_basic, programPath),
    46  				Check: func(s *terraform.State) error {
    47  					_, ok := s.RootModule().Resources["data.external.test"]
    48  					if !ok {
    49  						return fmt.Errorf("missing data resource")
    50  					}
    51  
    52  					outputs := s.RootModule().Outputs
    53  
    54  					if outputs["argument"] == nil {
    55  						return fmt.Errorf("missing 'argument' output")
    56  					}
    57  					if outputs["query_value"] == nil {
    58  						return fmt.Errorf("missing 'query_value' output")
    59  					}
    60  
    61  					if outputs["argument"].Value != "cheese" {
    62  						return fmt.Errorf(
    63  							"'argument' output is %q; want 'cheese'",
    64  							outputs["argument"].Value,
    65  						)
    66  					}
    67  					if outputs["query_value"].Value != "pizza" {
    68  						return fmt.Errorf(
    69  							"'query_value' output is %q; want 'pizza'",
    70  							outputs["query_value"].Value,
    71  						)
    72  					}
    73  
    74  					return nil
    75  				},
    76  			},
    77  		},
    78  	})
    79  }
    80  
    81  const testDataSourceConfig_error = `
    82  data "external" "test" {
    83    program = ["%s"]
    84  
    85    query = {
    86      fail = "true"
    87    }
    88  }
    89  `
    90  
    91  func TestDataSource_error(t *testing.T) {
    92  	programPath, err := buildDataSourceTestProgram()
    93  	if err != nil {
    94  		t.Fatal(err)
    95  		return
    96  	}
    97  
    98  	resource.UnitTest(t, resource.TestCase{
    99  		Providers: testProviders,
   100  		Steps: []resource.TestStep{
   101  			resource.TestStep{
   102  				Config:      fmt.Sprintf(testDataSourceConfig_error, programPath),
   103  				ExpectError: regexp.MustCompile("I was asked to fail"),
   104  			},
   105  		},
   106  	})
   107  }
   108  
   109  func buildDataSourceTestProgram() (string, error) {
   110  	// We have a simple Go program that we use as a stub for testing.
   111  	cmd := exec.Command(
   112  		"go", "install",
   113  		"github.com/hashicorp/terraform/builtin/providers/external/test-programs/tf-acc-external-data-source",
   114  	)
   115  	err := cmd.Run()
   116  
   117  	if err != nil {
   118  		return "", fmt.Errorf("failed to build test stub program: %s", err)
   119  	}
   120  
   121  	gopath := os.Getenv("GOPATH")
   122  	if gopath == "" {
   123  		gopath = filepath.Join(os.Getenv("HOME") + "/go")
   124  	}
   125  
   126  	programPath := path.Join(
   127  		filepath.SplitList(gopath)[0], "bin", "tf-acc-external-data-source",
   128  	)
   129  	return programPath, nil
   130  }