github.com/paybyphone/terraform@v0.9.5-0.20170613192930-9706042ddd51/terraform/node_data_refresh_test.go (about) 1 package terraform 2 3 import ( 4 "sync" 5 "testing" 6 ) 7 8 func TestNodeRefreshableDataResourceDynamicExpand_scaleOut(t *testing.T) { 9 var stateLock sync.RWMutex 10 11 addr, err := ParseResourceAddress("data.aws_instance.foo") 12 if err != nil { 13 t.Fatalf("bad: %s", err) 14 } 15 16 m := testModule(t, "refresh-data-scale-inout") 17 18 state := &State{ 19 Modules: []*ModuleState{ 20 &ModuleState{ 21 Path: rootModulePath, 22 Resources: map[string]*ResourceState{ 23 "data.aws_instance.foo.0": &ResourceState{ 24 Type: "aws_instance", 25 Deposed: []*InstanceState{ 26 &InstanceState{ 27 ID: "foo", 28 }, 29 }, 30 }, 31 "data.aws_instance.foo.1": &ResourceState{ 32 Type: "aws_instance", 33 Deposed: []*InstanceState{ 34 &InstanceState{ 35 ID: "bar", 36 }, 37 }, 38 }, 39 }, 40 }, 41 }, 42 } 43 44 n := &NodeRefreshableDataResource{ 45 NodeAbstractCountResource: &NodeAbstractCountResource{ 46 NodeAbstractResource: &NodeAbstractResource{ 47 Addr: addr, 48 Config: m.Config().Resources[0], 49 }, 50 }, 51 } 52 53 g, err := n.DynamicExpand(&MockEvalContext{ 54 PathPath: []string{"root"}, 55 StateState: state, 56 StateLock: &stateLock, 57 }) 58 59 actual := g.StringWithNodeTypes() 60 expected := `data.aws_instance.foo[0] - *terraform.NodeRefreshableDataResourceInstance 61 data.aws_instance.foo[1] - *terraform.NodeRefreshableDataResourceInstance 62 data.aws_instance.foo[2] - *terraform.NodeRefreshableDataResourceInstance 63 root - terraform.graphNodeRoot 64 data.aws_instance.foo[0] - *terraform.NodeRefreshableDataResourceInstance 65 data.aws_instance.foo[1] - *terraform.NodeRefreshableDataResourceInstance 66 data.aws_instance.foo[2] - *terraform.NodeRefreshableDataResourceInstance 67 ` 68 if expected != actual { 69 t.Fatalf("Expected:\n%s\nGot:\n%s", expected, actual) 70 } 71 } 72 73 func TestNodeRefreshableDataResourceDynamicExpand_scaleIn(t *testing.T) { 74 var stateLock sync.RWMutex 75 76 addr, err := ParseResourceAddress("data.aws_instance.foo") 77 if err != nil { 78 t.Fatalf("bad: %s", err) 79 } 80 81 m := testModule(t, "refresh-data-scale-inout") 82 83 state := &State{ 84 Modules: []*ModuleState{ 85 &ModuleState{ 86 Path: rootModulePath, 87 Resources: map[string]*ResourceState{ 88 "data.aws_instance.foo.0": &ResourceState{ 89 Type: "aws_instance", 90 Deposed: []*InstanceState{ 91 &InstanceState{ 92 ID: "foo", 93 }, 94 }, 95 }, 96 "data.aws_instance.foo.1": &ResourceState{ 97 Type: "aws_instance", 98 Deposed: []*InstanceState{ 99 &InstanceState{ 100 ID: "bar", 101 }, 102 }, 103 }, 104 "data.aws_instance.foo.2": &ResourceState{ 105 Type: "aws_instance", 106 Deposed: []*InstanceState{ 107 &InstanceState{ 108 ID: "baz", 109 }, 110 }, 111 }, 112 "data.aws_instance.foo.3": &ResourceState{ 113 Type: "aws_instance", 114 Deposed: []*InstanceState{ 115 &InstanceState{ 116 ID: "qux", 117 }, 118 }, 119 }, 120 }, 121 }, 122 }, 123 } 124 125 n := &NodeRefreshableDataResource{ 126 NodeAbstractCountResource: &NodeAbstractCountResource{ 127 NodeAbstractResource: &NodeAbstractResource{ 128 Addr: addr, 129 Config: m.Config().Resources[0], 130 }, 131 }, 132 } 133 134 g, err := n.DynamicExpand(&MockEvalContext{ 135 PathPath: []string{"root"}, 136 StateState: state, 137 StateLock: &stateLock, 138 }) 139 140 actual := g.StringWithNodeTypes() 141 expected := `data.aws_instance.foo[0] - *terraform.NodeRefreshableDataResourceInstance 142 data.aws_instance.foo[1] - *terraform.NodeRefreshableDataResourceInstance 143 data.aws_instance.foo[2] - *terraform.NodeRefreshableDataResourceInstance 144 data.aws_instance.foo[3] - *terraform.NodeDestroyableDataResource 145 root - terraform.graphNodeRoot 146 data.aws_instance.foo[0] - *terraform.NodeRefreshableDataResourceInstance 147 data.aws_instance.foo[1] - *terraform.NodeRefreshableDataResourceInstance 148 data.aws_instance.foo[2] - *terraform.NodeRefreshableDataResourceInstance 149 data.aws_instance.foo[3] - *terraform.NodeDestroyableDataResource 150 ` 151 if expected != actual { 152 t.Fatalf("Expected:\n%s\nGot:\n%s", expected, actual) 153 } 154 }