github.com/skyscape-cloud-services/terraform@v0.9.2-0.20170609144644-7ece028a1747/terraform/transform_provisioner_test.go (about) 1 package terraform 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/hashicorp/terraform/dag" 8 ) 9 10 func TestMissingProvisionerTransformer(t *testing.T) { 11 mod := testModule(t, "transform-provisioner-basic") 12 13 g := Graph{Path: RootModulePath} 14 { 15 tf := &ConfigTransformer{Module: mod} 16 if err := tf.Transform(&g); err != nil { 17 t.Fatalf("err: %s", err) 18 } 19 } 20 21 { 22 transform := &AttachResourceConfigTransformer{Module: mod} 23 if err := transform.Transform(&g); err != nil { 24 t.Fatalf("err: %s", err) 25 } 26 } 27 28 { 29 transform := &MissingProvisionerTransformer{Provisioners: []string{"shell"}} 30 if err := transform.Transform(&g); err != nil { 31 t.Fatalf("err: %s", err) 32 } 33 } 34 35 { 36 transform := &ProvisionerTransformer{} 37 if err := transform.Transform(&g); err != nil { 38 t.Fatalf("err: %s", err) 39 } 40 } 41 42 actual := strings.TrimSpace(g.String()) 43 expected := strings.TrimSpace(testTransformMissingProvisionerBasicStr) 44 if actual != expected { 45 t.Fatalf("bad:\n\n%s", actual) 46 } 47 } 48 49 func TestMissingProvisionerTransformer_module(t *testing.T) { 50 mod := testModule(t, "transform-provisioner-module") 51 52 g := Graph{Path: RootModulePath} 53 { 54 concreteResource := func(a *NodeAbstractResource) dag.Vertex { 55 return a 56 } 57 58 var state State 59 state.init() 60 state.Modules = []*ModuleState{ 61 &ModuleState{ 62 Path: []string{"root"}, 63 Resources: map[string]*ResourceState{ 64 "aws_instance.foo": &ResourceState{ 65 Primary: &InstanceState{ID: "foo"}, 66 }, 67 }, 68 }, 69 70 &ModuleState{ 71 Path: []string{"root", "child"}, 72 Resources: map[string]*ResourceState{ 73 "aws_instance.foo": &ResourceState{ 74 Primary: &InstanceState{ID: "foo"}, 75 }, 76 }, 77 }, 78 } 79 80 tf := &StateTransformer{ 81 Concrete: concreteResource, 82 State: &state, 83 } 84 if err := tf.Transform(&g); err != nil { 85 t.Fatalf("err: %s", err) 86 } 87 } 88 89 { 90 transform := &AttachResourceConfigTransformer{Module: mod} 91 if err := transform.Transform(&g); err != nil { 92 t.Fatalf("err: %s", err) 93 } 94 } 95 96 { 97 transform := &MissingProvisionerTransformer{Provisioners: []string{"shell"}} 98 if err := transform.Transform(&g); err != nil { 99 t.Fatalf("err: %s", err) 100 } 101 } 102 103 { 104 transform := &ProvisionerTransformer{} 105 if err := transform.Transform(&g); err != nil { 106 t.Fatalf("err: %s", err) 107 } 108 } 109 110 actual := strings.TrimSpace(g.String()) 111 expected := strings.TrimSpace(testTransformMissingProvisionerModuleStr) 112 if actual != expected { 113 t.Fatalf("bad:\n\n%s", actual) 114 } 115 } 116 117 func TestCloseProvisionerTransformer(t *testing.T) { 118 mod := testModule(t, "transform-provisioner-basic") 119 120 g := Graph{Path: RootModulePath} 121 { 122 tf := &ConfigTransformer{Module: mod} 123 if err := tf.Transform(&g); err != nil { 124 t.Fatalf("err: %s", err) 125 } 126 } 127 128 { 129 transform := &AttachResourceConfigTransformer{Module: mod} 130 if err := transform.Transform(&g); err != nil { 131 t.Fatalf("err: %s", err) 132 } 133 } 134 135 { 136 transform := &MissingProvisionerTransformer{Provisioners: []string{"shell"}} 137 if err := transform.Transform(&g); err != nil { 138 t.Fatalf("err: %s", err) 139 } 140 } 141 142 { 143 transform := &ProvisionerTransformer{} 144 if err := transform.Transform(&g); err != nil { 145 t.Fatalf("err: %s", err) 146 } 147 } 148 149 { 150 transform := &CloseProvisionerTransformer{} 151 if err := transform.Transform(&g); err != nil { 152 t.Fatalf("err: %s", err) 153 } 154 } 155 156 actual := strings.TrimSpace(g.String()) 157 expected := strings.TrimSpace(testTransformCloseProvisionerBasicStr) 158 if actual != expected { 159 t.Fatalf("bad:\n\n%s", actual) 160 } 161 } 162 163 const testTransformMissingProvisionerBasicStr = ` 164 aws_instance.web 165 provisioner.shell 166 provisioner.shell 167 ` 168 169 const testTransformMissingProvisionerModuleStr = ` 170 aws_instance.foo 171 provisioner.shell 172 module.child.aws_instance.foo 173 module.child.provisioner.shell 174 module.child.provisioner.shell 175 provisioner.shell 176 ` 177 178 const testTransformCloseProvisionerBasicStr = ` 179 aws_instance.web 180 provisioner.shell 181 provisioner.shell 182 provisioner.shell (close) 183 aws_instance.web 184 `