github.com/trawler/terraform@v0.10.8-0.20171106022149-4b1c7a1d9b48/terraform/node_resource_refresh_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"reflect"
     5  	"sync"
     6  	"testing"
     7  
     8  	"github.com/davecgh/go-spew/spew"
     9  )
    10  
    11  func TestNodeRefreshableManagedResourceDynamicExpand_scaleOut(t *testing.T) {
    12  	var stateLock sync.RWMutex
    13  
    14  	addr, err := ParseResourceAddress("aws_instance.foo")
    15  	if err != nil {
    16  		t.Fatalf("bad: %s", err)
    17  	}
    18  
    19  	m := testModule(t, "refresh-resource-scale-inout")
    20  
    21  	state := &State{
    22  		Modules: []*ModuleState{
    23  			&ModuleState{
    24  				Path: rootModulePath,
    25  				Resources: map[string]*ResourceState{
    26  					"aws_instance.foo.0": &ResourceState{
    27  						Type: "aws_instance",
    28  						Deposed: []*InstanceState{
    29  							&InstanceState{
    30  								ID: "foo",
    31  							},
    32  						},
    33  					},
    34  					"aws_instance.foo.1": &ResourceState{
    35  						Type: "aws_instance",
    36  						Deposed: []*InstanceState{
    37  							&InstanceState{
    38  								ID: "bar",
    39  							},
    40  						},
    41  					},
    42  				},
    43  			},
    44  		},
    45  	}
    46  
    47  	n := &NodeRefreshableManagedResource{
    48  		NodeAbstractCountResource: &NodeAbstractCountResource{
    49  			NodeAbstractResource: &NodeAbstractResource{
    50  				Addr:   addr,
    51  				Config: m.Config().Resources[0],
    52  			},
    53  		},
    54  	}
    55  
    56  	g, err := n.DynamicExpand(&MockEvalContext{
    57  		PathPath:   []string{"root"},
    58  		StateState: state,
    59  		StateLock:  &stateLock,
    60  	})
    61  	if err != nil {
    62  		t.Fatalf("error attempting DynamicExpand: %s", err)
    63  	}
    64  
    65  	actual := g.StringWithNodeTypes()
    66  	expected := `aws_instance.foo[0] - *terraform.NodeRefreshableManagedResourceInstance
    67  aws_instance.foo[1] - *terraform.NodeRefreshableManagedResourceInstance
    68  aws_instance.foo[2] - *terraform.NodeRefreshableManagedResourceInstance
    69  root - terraform.graphNodeRoot
    70    aws_instance.foo[0] - *terraform.NodeRefreshableManagedResourceInstance
    71    aws_instance.foo[1] - *terraform.NodeRefreshableManagedResourceInstance
    72    aws_instance.foo[2] - *terraform.NodeRefreshableManagedResourceInstance
    73  `
    74  	if expected != actual {
    75  		t.Fatalf("Expected:\n%s\nGot:\n%s", expected, actual)
    76  	}
    77  }
    78  
    79  func TestNodeRefreshableManagedResourceDynamicExpand_scaleIn(t *testing.T) {
    80  	var stateLock sync.RWMutex
    81  
    82  	addr, err := ParseResourceAddress("aws_instance.foo")
    83  	if err != nil {
    84  		t.Fatalf("bad: %s", err)
    85  	}
    86  
    87  	m := testModule(t, "refresh-resource-scale-inout")
    88  
    89  	state := &State{
    90  		Modules: []*ModuleState{
    91  			&ModuleState{
    92  				Path: rootModulePath,
    93  				Resources: map[string]*ResourceState{
    94  					"aws_instance.foo.0": &ResourceState{
    95  						Type: "aws_instance",
    96  						Deposed: []*InstanceState{
    97  							&InstanceState{
    98  								ID: "foo",
    99  							},
   100  						},
   101  					},
   102  					"aws_instance.foo.1": &ResourceState{
   103  						Type: "aws_instance",
   104  						Deposed: []*InstanceState{
   105  							&InstanceState{
   106  								ID: "bar",
   107  							},
   108  						},
   109  					},
   110  					"aws_instance.foo.2": &ResourceState{
   111  						Type: "aws_instance",
   112  						Deposed: []*InstanceState{
   113  							&InstanceState{
   114  								ID: "baz",
   115  							},
   116  						},
   117  					},
   118  					"aws_instance.foo.3": &ResourceState{
   119  						Type: "aws_instance",
   120  						Deposed: []*InstanceState{
   121  							&InstanceState{
   122  								ID: "qux",
   123  							},
   124  						},
   125  					},
   126  				},
   127  			},
   128  		},
   129  	}
   130  
   131  	n := &NodeRefreshableManagedResource{
   132  		NodeAbstractCountResource: &NodeAbstractCountResource{
   133  			NodeAbstractResource: &NodeAbstractResource{
   134  				Addr:   addr,
   135  				Config: m.Config().Resources[0],
   136  			},
   137  		},
   138  	}
   139  
   140  	g, err := n.DynamicExpand(&MockEvalContext{
   141  		PathPath:   []string{"root"},
   142  		StateState: state,
   143  		StateLock:  &stateLock,
   144  	})
   145  	if err != nil {
   146  		t.Fatalf("error attempting DynamicExpand: %s", err)
   147  	}
   148  	actual := g.StringWithNodeTypes()
   149  	expected := `aws_instance.foo[0] - *terraform.NodeRefreshableManagedResourceInstance
   150  aws_instance.foo[1] - *terraform.NodeRefreshableManagedResourceInstance
   151  aws_instance.foo[2] - *terraform.NodeRefreshableManagedResourceInstance
   152  aws_instance.foo[3] - *terraform.NodeRefreshableManagedResourceInstance
   153  root - terraform.graphNodeRoot
   154    aws_instance.foo[0] - *terraform.NodeRefreshableManagedResourceInstance
   155    aws_instance.foo[1] - *terraform.NodeRefreshableManagedResourceInstance
   156    aws_instance.foo[2] - *terraform.NodeRefreshableManagedResourceInstance
   157    aws_instance.foo[3] - *terraform.NodeRefreshableManagedResourceInstance
   158  `
   159  	if expected != actual {
   160  		t.Fatalf("Expected:\n%s\nGot:\n%s", expected, actual)
   161  	}
   162  }
   163  
   164  func TestNodeRefreshableManagedResourceEvalTree_scaleOut(t *testing.T) {
   165  	addr, err := ParseResourceAddress("aws_instance.foo[2]")
   166  	if err != nil {
   167  		t.Fatalf("bad: %s", err)
   168  	}
   169  
   170  	m := testModule(t, "refresh-resource-scale-inout")
   171  
   172  	n := &NodeRefreshableManagedResourceInstance{
   173  		NodeAbstractResource: &NodeAbstractResource{
   174  			Addr:   addr,
   175  			Config: m.Config().Resources[0],
   176  		},
   177  	}
   178  
   179  	actual := n.EvalTree()
   180  	expected := n.evalTreeManagedResourceNoState()
   181  
   182  	if !reflect.DeepEqual(expected, actual) {
   183  		t.Fatalf("Expected:\n\n%s\nGot:\n\n%s\n", spew.Sdump(expected), spew.Sdump(actual))
   184  	}
   185  }