github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/local/resource_local_file_test.go (about)

     1  package local
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"testing"
     9  
    10  	r "github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestLocalFile_Basic(t *testing.T) {
    15  	var cases = []struct {
    16  		path    string
    17  		content string
    18  		config  string
    19  	}{
    20  		{
    21  			"local_file",
    22  			"This is some content",
    23  			`resource "local_file" "file" {
    24           content     = "This is some content"
    25           filename    = "local_file"
    26        }`,
    27  		},
    28  	}
    29  
    30  	for _, tt := range cases {
    31  		r.UnitTest(t, r.TestCase{
    32  			Providers: testProviders,
    33  			Steps: []r.TestStep{
    34  				{
    35  					Config: tt.config,
    36  					Check: func(s *terraform.State) error {
    37  						content, err := ioutil.ReadFile(tt.path)
    38  						if err != nil {
    39  							return fmt.Errorf("config:\n%s\n,got: %s\n", tt.config, err)
    40  						}
    41  						if string(content) != tt.content {
    42  							return fmt.Errorf("config:\n%s\ngot:\n%s\nwant:\n%s\n", tt.config, content, tt.content)
    43  						}
    44  						return nil
    45  					},
    46  				},
    47  			},
    48  			CheckDestroy: func(*terraform.State) error {
    49  				if _, err := os.Stat(tt.path); os.IsNotExist(err) {
    50  					return nil
    51  				}
    52  				return errors.New("local_file did not get destroyed")
    53  			},
    54  		})
    55  	}
    56  }