github.com/hooklift/terraform@v0.11.0-beta1.0.20171117000744-6786c1361ffe/builtin/providers/test/resource_with_custom_diff_test.go (about)

     1  package test
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  )
    10  
    11  // TestResourceWithCustomDiff test custom diff behaviour.
    12  func TestResourceWithCustomDiff(t *testing.T) {
    13  	resource.UnitTest(t, resource.TestCase{
    14  		Providers: testAccProviders,
    15  		Steps: []resource.TestStep{
    16  			{
    17  				Config: resourceWithCustomDiffConfig(false),
    18  				Check: resource.ComposeTestCheckFunc(
    19  					resource.TestCheckResourceAttr("test_resource_with_custom_diff.foo", "computed", "1"),
    20  					resource.TestCheckResourceAttr("test_resource_with_custom_diff.foo", "index", "1"),
    21  					resource.TestCheckResourceAttr("test_resource_with_custom_diff.foo", "list.#", "1"),
    22  					resource.TestCheckResourceAttr("test_resource_with_custom_diff.foo", "list.0", "dc1"),
    23  				),
    24  				ExpectNonEmptyPlan: true,
    25  			},
    26  			{
    27  				Config: resourceWithCustomDiffConfig(false),
    28  				Check: resource.ComposeTestCheckFunc(
    29  					resource.TestCheckResourceAttr("test_resource_with_custom_diff.foo", "computed", "2"),
    30  					resource.TestCheckResourceAttr("test_resource_with_custom_diff.foo", "index", "2"),
    31  					resource.TestCheckResourceAttr("test_resource_with_custom_diff.foo", "list.#", "2"),
    32  					resource.TestCheckResourceAttr("test_resource_with_custom_diff.foo", "list.0", "dc2"),
    33  					resource.TestCheckResourceAttr("test_resource_with_custom_diff.foo", "list.1", "dc3"),
    34  					resource.TestCheckNoResourceAttr("test_resource_with_custom_diff.foo", "list.2"),
    35  				),
    36  				ExpectNonEmptyPlan: true,
    37  			},
    38  			{
    39  				Config:      resourceWithCustomDiffConfig(true),
    40  				ExpectError: regexp.MustCompile("veto is true, diff vetoed"),
    41  			},
    42  		},
    43  	})
    44  }
    45  
    46  func resourceWithCustomDiffConfig(veto bool) string {
    47  	return fmt.Sprintf(`
    48  resource "test_resource_with_custom_diff" "foo" {
    49  	required = "yep"
    50  	veto = %t
    51  }
    52  `, veto)
    53  }