github.com/hs0210/hashicorp-terraform@v0.11.12-beta1/backend/local/hook_count_test.go (about)

     1  package local
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/terraform"
     8  )
     9  
    10  func TestCountHook_impl(t *testing.T) {
    11  	var _ terraform.Hook = new(CountHook)
    12  }
    13  
    14  func TestCountHookPostDiff_DestroyDeposed(t *testing.T) {
    15  	h := new(CountHook)
    16  
    17  	resources := map[string]*terraform.InstanceDiff{
    18  		"lorem": &terraform.InstanceDiff{DestroyDeposed: true},
    19  	}
    20  
    21  	n := &terraform.InstanceInfo{} // TODO
    22  
    23  	for _, d := range resources {
    24  		h.PostDiff(n, d)
    25  	}
    26  
    27  	expected := new(CountHook)
    28  	expected.ToAdd = 0
    29  	expected.ToChange = 0
    30  	expected.ToRemoveAndAdd = 0
    31  	expected.ToRemove = 1
    32  
    33  	if !reflect.DeepEqual(expected, h) {
    34  		t.Fatalf("Expected %#v, got %#v instead.",
    35  			expected, h)
    36  	}
    37  }
    38  
    39  func TestCountHookPostDiff_DestroyOnly(t *testing.T) {
    40  	h := new(CountHook)
    41  
    42  	resources := map[string]*terraform.InstanceDiff{
    43  		"foo":   &terraform.InstanceDiff{Destroy: true},
    44  		"bar":   &terraform.InstanceDiff{Destroy: true},
    45  		"lorem": &terraform.InstanceDiff{Destroy: true},
    46  		"ipsum": &terraform.InstanceDiff{Destroy: true},
    47  	}
    48  
    49  	n := &terraform.InstanceInfo{} // TODO
    50  
    51  	for _, d := range resources {
    52  		h.PostDiff(n, d)
    53  	}
    54  
    55  	expected := new(CountHook)
    56  	expected.ToAdd = 0
    57  	expected.ToChange = 0
    58  	expected.ToRemoveAndAdd = 0
    59  	expected.ToRemove = 4
    60  
    61  	if !reflect.DeepEqual(expected, h) {
    62  		t.Fatalf("Expected %#v, got %#v instead.",
    63  			expected, h)
    64  	}
    65  }
    66  
    67  func TestCountHookPostDiff_AddOnly(t *testing.T) {
    68  	h := new(CountHook)
    69  
    70  	resources := map[string]*terraform.InstanceDiff{
    71  		"foo": &terraform.InstanceDiff{
    72  			Attributes: map[string]*terraform.ResourceAttrDiff{
    73  				"foo": &terraform.ResourceAttrDiff{RequiresNew: true},
    74  			},
    75  		},
    76  		"bar": &terraform.InstanceDiff{
    77  			Attributes: map[string]*terraform.ResourceAttrDiff{
    78  				"foo": &terraform.ResourceAttrDiff{RequiresNew: true},
    79  			},
    80  		},
    81  		"lorem": &terraform.InstanceDiff{
    82  			Attributes: map[string]*terraform.ResourceAttrDiff{
    83  				"foo": &terraform.ResourceAttrDiff{RequiresNew: true},
    84  			},
    85  		},
    86  	}
    87  
    88  	n := &terraform.InstanceInfo{}
    89  
    90  	for _, d := range resources {
    91  		h.PostDiff(n, d)
    92  	}
    93  
    94  	expected := new(CountHook)
    95  	expected.ToAdd = 3
    96  	expected.ToChange = 0
    97  	expected.ToRemoveAndAdd = 0
    98  	expected.ToRemove = 0
    99  
   100  	if !reflect.DeepEqual(expected, h) {
   101  		t.Fatalf("Expected %#v, got %#v instead.",
   102  			expected, h)
   103  	}
   104  }
   105  
   106  func TestCountHookPostDiff_ChangeOnly(t *testing.T) {
   107  	h := new(CountHook)
   108  
   109  	resources := map[string]*terraform.InstanceDiff{
   110  		"foo": &terraform.InstanceDiff{
   111  			Destroy: false,
   112  			Attributes: map[string]*terraform.ResourceAttrDiff{
   113  				"foo": &terraform.ResourceAttrDiff{},
   114  			},
   115  		},
   116  		"bar": &terraform.InstanceDiff{
   117  			Destroy: false,
   118  			Attributes: map[string]*terraform.ResourceAttrDiff{
   119  				"foo": &terraform.ResourceAttrDiff{},
   120  			},
   121  		},
   122  		"lorem": &terraform.InstanceDiff{
   123  			Destroy: false,
   124  			Attributes: map[string]*terraform.ResourceAttrDiff{
   125  				"foo": &terraform.ResourceAttrDiff{},
   126  			},
   127  		},
   128  	}
   129  
   130  	n := &terraform.InstanceInfo{}
   131  
   132  	for _, d := range resources {
   133  		h.PostDiff(n, d)
   134  	}
   135  
   136  	expected := new(CountHook)
   137  	expected.ToAdd = 0
   138  	expected.ToChange = 3
   139  	expected.ToRemoveAndAdd = 0
   140  	expected.ToRemove = 0
   141  
   142  	if !reflect.DeepEqual(expected, h) {
   143  		t.Fatalf("Expected %#v, got %#v instead.",
   144  			expected, h)
   145  	}
   146  }
   147  
   148  func TestCountHookPostDiff_Mixed(t *testing.T) {
   149  	h := new(CountHook)
   150  
   151  	resources := map[string]*terraform.InstanceDiff{
   152  		"foo": &terraform.InstanceDiff{
   153  			Destroy: true,
   154  		},
   155  		"bar": &terraform.InstanceDiff{},
   156  		"lorem": &terraform.InstanceDiff{
   157  			Destroy: false,
   158  			Attributes: map[string]*terraform.ResourceAttrDiff{
   159  				"foo": &terraform.ResourceAttrDiff{},
   160  			},
   161  		},
   162  		"ipsum": &terraform.InstanceDiff{Destroy: true},
   163  	}
   164  
   165  	n := &terraform.InstanceInfo{}
   166  
   167  	for _, d := range resources {
   168  		h.PostDiff(n, d)
   169  	}
   170  
   171  	expected := new(CountHook)
   172  	expected.ToAdd = 0
   173  	expected.ToChange = 1
   174  	expected.ToRemoveAndAdd = 0
   175  	expected.ToRemove = 2
   176  
   177  	if !reflect.DeepEqual(expected, h) {
   178  		t.Fatalf("Expected %#v, got %#v instead.",
   179  			expected, h)
   180  	}
   181  }
   182  
   183  func TestCountHookPostDiff_NoChange(t *testing.T) {
   184  	h := new(CountHook)
   185  
   186  	resources := map[string]*terraform.InstanceDiff{
   187  		"foo":   &terraform.InstanceDiff{},
   188  		"bar":   &terraform.InstanceDiff{},
   189  		"lorem": &terraform.InstanceDiff{},
   190  		"ipsum": &terraform.InstanceDiff{},
   191  	}
   192  
   193  	n := &terraform.InstanceInfo{}
   194  
   195  	for _, d := range resources {
   196  		h.PostDiff(n, d)
   197  	}
   198  
   199  	expected := new(CountHook)
   200  	expected.ToAdd = 0
   201  	expected.ToChange = 0
   202  	expected.ToRemoveAndAdd = 0
   203  	expected.ToRemove = 0
   204  
   205  	if !reflect.DeepEqual(expected, h) {
   206  		t.Fatalf("Expected %#v, got %#v instead.",
   207  			expected, h)
   208  	}
   209  }
   210  
   211  func TestCountHookPostDiff_DataSource(t *testing.T) {
   212  	h := new(CountHook)
   213  
   214  	resources := map[string]*terraform.InstanceDiff{
   215  		"data.foo": &terraform.InstanceDiff{
   216  			Destroy: true,
   217  		},
   218  		"data.bar": &terraform.InstanceDiff{},
   219  		"data.lorem": &terraform.InstanceDiff{
   220  			Destroy: false,
   221  			Attributes: map[string]*terraform.ResourceAttrDiff{
   222  				"foo": &terraform.ResourceAttrDiff{},
   223  			},
   224  		},
   225  		"data.ipsum": &terraform.InstanceDiff{Destroy: true},
   226  	}
   227  
   228  	for k, d := range resources {
   229  		n := &terraform.InstanceInfo{Id: k}
   230  		h.PostDiff(n, d)
   231  	}
   232  
   233  	expected := new(CountHook)
   234  	expected.ToAdd = 0
   235  	expected.ToChange = 0
   236  	expected.ToRemoveAndAdd = 0
   237  	expected.ToRemove = 0
   238  
   239  	if !reflect.DeepEqual(expected, h) {
   240  		t.Fatalf("Expected %#v, got %#v instead.",
   241  			expected, h)
   242  	}
   243  }