github.com/Hashicorp/terraform@v0.11.12-beta1/helper/customdiff/force_new_test.go (about)

     1  package customdiff
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/terraform/helper/schema"
     7  )
     8  
     9  func TestForceNewIf(t *testing.T) {
    10  	t.Run("true", func(t *testing.T) {
    11  		var condCalls int
    12  		var gotOld1, gotNew1, gotOld2, gotNew2 string
    13  
    14  		provider := testProvider(
    15  			map[string]*schema.Schema{
    16  				"foo": {
    17  					Type:     schema.TypeString,
    18  					Optional: true,
    19  				},
    20  			},
    21  			ForceNewIf("foo", func(d *schema.ResourceDiff, meta interface{}) bool {
    22  				// When we set "ForceNew", our CustomizeDiff function is actually
    23  				// called a second time to construct the "create" portion of
    24  				// the replace diff. On the second call, the old value is masked
    25  				// as "" to suggest that the object is being created rather than
    26  				// updated.
    27  
    28  				condCalls++
    29  				old, new := d.GetChange("foo")
    30  
    31  				switch condCalls {
    32  				case 1:
    33  					gotOld1 = old.(string)
    34  					gotNew1 = new.(string)
    35  				case 2:
    36  					gotOld2 = old.(string)
    37  					gotNew2 = new.(string)
    38  				}
    39  
    40  				return true
    41  			}),
    42  		)
    43  
    44  		diff, err := testDiff(
    45  			provider,
    46  			map[string]string{
    47  				"foo": "bar",
    48  			},
    49  			map[string]string{
    50  				"foo": "baz",
    51  			},
    52  		)
    53  
    54  		if err != nil {
    55  			t.Fatalf("Diff failed with error: %s", err)
    56  		}
    57  
    58  		if condCalls != 2 {
    59  			t.Fatalf("Wrong number of conditional callback calls %d; want %d", condCalls, 2)
    60  		} else {
    61  			if got, want := gotOld1, "bar"; got != want {
    62  				t.Errorf("wrong old value %q on first call; want %q", got, want)
    63  			}
    64  			if got, want := gotNew1, "baz"; got != want {
    65  				t.Errorf("wrong new value %q on first call; want %q", got, want)
    66  			}
    67  			if got, want := gotOld2, ""; got != want {
    68  				t.Errorf("wrong old value %q on first call; want %q", got, want)
    69  			}
    70  			if got, want := gotNew2, "baz"; got != want {
    71  				t.Errorf("wrong new value %q on first call; want %q", got, want)
    72  			}
    73  		}
    74  
    75  		if !diff.Attributes["foo"].RequiresNew {
    76  			t.Error("Attribute 'foo' is not marked as RequiresNew")
    77  		}
    78  	})
    79  	t.Run("false", func(t *testing.T) {
    80  		var condCalls int
    81  		var gotOld, gotNew string
    82  
    83  		provider := testProvider(
    84  			map[string]*schema.Schema{
    85  				"foo": {
    86  					Type:     schema.TypeString,
    87  					Optional: true,
    88  				},
    89  			},
    90  			ForceNewIf("foo", func(d *schema.ResourceDiff, meta interface{}) bool {
    91  				condCalls++
    92  				old, new := d.GetChange("foo")
    93  				gotOld = old.(string)
    94  				gotNew = new.(string)
    95  
    96  				return false
    97  			}),
    98  		)
    99  
   100  		diff, err := testDiff(
   101  			provider,
   102  			map[string]string{
   103  				"foo": "bar",
   104  			},
   105  			map[string]string{
   106  				"foo": "baz",
   107  			},
   108  		)
   109  
   110  		if err != nil {
   111  			t.Fatalf("Diff failed with error: %s", err)
   112  		}
   113  
   114  		if condCalls != 1 {
   115  			t.Fatalf("Wrong number of conditional callback calls %d; want %d", condCalls, 1)
   116  		} else {
   117  			if got, want := gotOld, "bar"; got != want {
   118  				t.Errorf("wrong old value %q on first call; want %q", got, want)
   119  			}
   120  			if got, want := gotNew, "baz"; got != want {
   121  				t.Errorf("wrong new value %q on first call; want %q", got, want)
   122  			}
   123  		}
   124  
   125  		if diff.Attributes["foo"].RequiresNew {
   126  			t.Error("Attribute 'foo' is marked as RequiresNew, but should not be")
   127  		}
   128  	})
   129  }
   130  
   131  func TestForceNewIfChange(t *testing.T) {
   132  	t.Run("true", func(t *testing.T) {
   133  		var condCalls int
   134  		var gotOld1, gotNew1, gotOld2, gotNew2 string
   135  
   136  		provider := testProvider(
   137  			map[string]*schema.Schema{
   138  				"foo": {
   139  					Type:     schema.TypeString,
   140  					Optional: true,
   141  				},
   142  			},
   143  			ForceNewIfChange("foo", func(old, new, meta interface{}) bool {
   144  				// When we set "ForceNew", our CustomizeDiff function is actually
   145  				// called a second time to construct the "create" portion of
   146  				// the replace diff. On the second call, the old value is masked
   147  				// as "" to suggest that the object is being created rather than
   148  				// updated.
   149  
   150  				condCalls++
   151  
   152  				switch condCalls {
   153  				case 1:
   154  					gotOld1 = old.(string)
   155  					gotNew1 = new.(string)
   156  				case 2:
   157  					gotOld2 = old.(string)
   158  					gotNew2 = new.(string)
   159  				}
   160  
   161  				return true
   162  			}),
   163  		)
   164  
   165  		diff, err := testDiff(
   166  			provider,
   167  			map[string]string{
   168  				"foo": "bar",
   169  			},
   170  			map[string]string{
   171  				"foo": "baz",
   172  			},
   173  		)
   174  
   175  		if err != nil {
   176  			t.Fatalf("Diff failed with error: %s", err)
   177  		}
   178  
   179  		if condCalls != 2 {
   180  			t.Fatalf("Wrong number of conditional callback calls %d; want %d", condCalls, 2)
   181  		} else {
   182  			if got, want := gotOld1, "bar"; got != want {
   183  				t.Errorf("wrong old value %q on first call; want %q", got, want)
   184  			}
   185  			if got, want := gotNew1, "baz"; got != want {
   186  				t.Errorf("wrong new value %q on first call; want %q", got, want)
   187  			}
   188  			if got, want := gotOld2, ""; got != want {
   189  				t.Errorf("wrong old value %q on first call; want %q", got, want)
   190  			}
   191  			if got, want := gotNew2, "baz"; got != want {
   192  				t.Errorf("wrong new value %q on first call; want %q", got, want)
   193  			}
   194  		}
   195  
   196  		if !diff.Attributes["foo"].RequiresNew {
   197  			t.Error("Attribute 'foo' is not marked as RequiresNew")
   198  		}
   199  	})
   200  	t.Run("false", func(t *testing.T) {
   201  		var condCalls int
   202  		var gotOld, gotNew string
   203  
   204  		provider := testProvider(
   205  			map[string]*schema.Schema{
   206  				"foo": {
   207  					Type:     schema.TypeString,
   208  					Optional: true,
   209  				},
   210  			},
   211  			ForceNewIfChange("foo", func(old, new, meta interface{}) bool {
   212  				condCalls++
   213  				gotOld = old.(string)
   214  				gotNew = new.(string)
   215  
   216  				return false
   217  			}),
   218  		)
   219  
   220  		diff, err := testDiff(
   221  			provider,
   222  			map[string]string{
   223  				"foo": "bar",
   224  			},
   225  			map[string]string{
   226  				"foo": "baz",
   227  			},
   228  		)
   229  
   230  		if err != nil {
   231  			t.Fatalf("Diff failed with error: %s", err)
   232  		}
   233  
   234  		if condCalls != 1 {
   235  			t.Fatalf("Wrong number of conditional callback calls %d; want %d", condCalls, 1)
   236  		} else {
   237  			if got, want := gotOld, "bar"; got != want {
   238  				t.Errorf("wrong old value %q on first call; want %q", got, want)
   239  			}
   240  			if got, want := gotNew, "baz"; got != want {
   241  				t.Errorf("wrong new value %q on first call; want %q", got, want)
   242  			}
   243  		}
   244  
   245  		if diff.Attributes["foo"].RequiresNew {
   246  			t.Error("Attribute 'foo' is marked as RequiresNew, but should not be")
   247  		}
   248  	})
   249  }