github.com/trawler/terraform@v0.10.8-0.20171106022149-4b1c7a1d9b48/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 if err != nil { 59 t.Fatalf("error on DynamicExpand: %s", err) 60 } 61 62 actual := g.StringWithNodeTypes() 63 expected := `data.aws_instance.foo[0] - *terraform.NodeRefreshableDataResourceInstance 64 data.aws_instance.foo[1] - *terraform.NodeRefreshableDataResourceInstance 65 data.aws_instance.foo[2] - *terraform.NodeRefreshableDataResourceInstance 66 root - terraform.graphNodeRoot 67 data.aws_instance.foo[0] - *terraform.NodeRefreshableDataResourceInstance 68 data.aws_instance.foo[1] - *terraform.NodeRefreshableDataResourceInstance 69 data.aws_instance.foo[2] - *terraform.NodeRefreshableDataResourceInstance 70 ` 71 if expected != actual { 72 t.Fatalf("Expected:\n%s\nGot:\n%s", expected, actual) 73 } 74 } 75 76 func TestNodeRefreshableDataResourceDynamicExpand_scaleIn(t *testing.T) { 77 var stateLock sync.RWMutex 78 79 addr, err := ParseResourceAddress("data.aws_instance.foo") 80 if err != nil { 81 t.Fatalf("bad: %s", err) 82 } 83 84 m := testModule(t, "refresh-data-scale-inout") 85 86 state := &State{ 87 Modules: []*ModuleState{ 88 &ModuleState{ 89 Path: rootModulePath, 90 Resources: map[string]*ResourceState{ 91 "data.aws_instance.foo.0": &ResourceState{ 92 Type: "aws_instance", 93 Deposed: []*InstanceState{ 94 &InstanceState{ 95 ID: "foo", 96 }, 97 }, 98 }, 99 "data.aws_instance.foo.1": &ResourceState{ 100 Type: "aws_instance", 101 Deposed: []*InstanceState{ 102 &InstanceState{ 103 ID: "bar", 104 }, 105 }, 106 }, 107 "data.aws_instance.foo.2": &ResourceState{ 108 Type: "aws_instance", 109 Deposed: []*InstanceState{ 110 &InstanceState{ 111 ID: "baz", 112 }, 113 }, 114 }, 115 "data.aws_instance.foo.3": &ResourceState{ 116 Type: "aws_instance", 117 Deposed: []*InstanceState{ 118 &InstanceState{ 119 ID: "qux", 120 }, 121 }, 122 }, 123 }, 124 }, 125 }, 126 } 127 128 n := &NodeRefreshableDataResource{ 129 NodeAbstractCountResource: &NodeAbstractCountResource{ 130 NodeAbstractResource: &NodeAbstractResource{ 131 Addr: addr, 132 Config: m.Config().Resources[0], 133 }, 134 }, 135 } 136 137 g, err := n.DynamicExpand(&MockEvalContext{ 138 PathPath: []string{"root"}, 139 StateState: state, 140 StateLock: &stateLock, 141 }) 142 if err != nil { 143 t.Fatalf("error on DynamicExpand: %s", err) 144 } 145 actual := g.StringWithNodeTypes() 146 expected := `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 root - terraform.graphNodeRoot 151 data.aws_instance.foo[0] - *terraform.NodeRefreshableDataResourceInstance 152 data.aws_instance.foo[1] - *terraform.NodeRefreshableDataResourceInstance 153 data.aws_instance.foo[2] - *terraform.NodeRefreshableDataResourceInstance 154 data.aws_instance.foo[3] - *terraform.NodeDestroyableDataResource 155 ` 156 if expected != actual { 157 t.Fatalf("Expected:\n%s\nGot:\n%s", expected, actual) 158 } 159 }