github.com/tarrant/terraform@v0.3.8-0.20150402012457-f68c9eee638e/terraform/interpolate_test.go (about) 1 package terraform 2 3 import ( 4 "os" 5 "reflect" 6 "sync" 7 "testing" 8 9 "github.com/hashicorp/terraform/config" 10 "github.com/hashicorp/terraform/config/lang/ast" 11 ) 12 13 func TestInterpolater_countIndex(t *testing.T) { 14 i := &Interpolater{} 15 16 scope := &InterpolationScope{ 17 Path: rootModulePath, 18 Resource: &Resource{CountIndex: 42}, 19 } 20 21 testInterpolate(t, i, scope, "count.index", ast.Variable{ 22 Value: 42, 23 Type: ast.TypeInt, 24 }) 25 } 26 27 func TestInterpolater_moduleVariable(t *testing.T) { 28 lock := new(sync.RWMutex) 29 state := &State{ 30 Modules: []*ModuleState{ 31 &ModuleState{ 32 Path: rootModulePath, 33 Resources: map[string]*ResourceState{ 34 "aws_instance.web": &ResourceState{ 35 Type: "aws_instance", 36 Primary: &InstanceState{ 37 ID: "bar", 38 }, 39 }, 40 }, 41 }, 42 &ModuleState{ 43 Path: []string{RootModuleName, "child"}, 44 Outputs: map[string]string{ 45 "foo": "bar", 46 }, 47 }, 48 }, 49 } 50 51 i := &Interpolater{ 52 State: state, 53 StateLock: lock, 54 } 55 56 scope := &InterpolationScope{ 57 Path: rootModulePath, 58 } 59 60 testInterpolate(t, i, scope, "module.child.foo", ast.Variable{ 61 Value: "bar", 62 Type: ast.TypeString, 63 }) 64 } 65 66 func TestInterpolater_pathCwd(t *testing.T) { 67 i := &Interpolater{} 68 scope := &InterpolationScope{} 69 70 expected, err := os.Getwd() 71 if err != nil { 72 t.Fatalf("err: %s", err) 73 } 74 75 testInterpolate(t, i, scope, "path.cwd", ast.Variable{ 76 Value: expected, 77 Type: ast.TypeString, 78 }) 79 } 80 81 func TestInterpolater_pathModule(t *testing.T) { 82 mod := testModule(t, "interpolate-path-module") 83 i := &Interpolater{ 84 Module: mod, 85 } 86 scope := &InterpolationScope{ 87 Path: []string{RootModuleName, "child"}, 88 } 89 90 path := mod.Child([]string{"child"}).Config().Dir 91 testInterpolate(t, i, scope, "path.module", ast.Variable{ 92 Value: path, 93 Type: ast.TypeString, 94 }) 95 } 96 97 func TestInterpolater_pathRoot(t *testing.T) { 98 mod := testModule(t, "interpolate-path-module") 99 i := &Interpolater{ 100 Module: mod, 101 } 102 scope := &InterpolationScope{ 103 Path: []string{RootModuleName, "child"}, 104 } 105 106 path := mod.Config().Dir 107 testInterpolate(t, i, scope, "path.root", ast.Variable{ 108 Value: path, 109 Type: ast.TypeString, 110 }) 111 } 112 113 func testInterpolate( 114 t *testing.T, i *Interpolater, 115 scope *InterpolationScope, 116 n string, expectedVar ast.Variable) { 117 v, err := config.NewInterpolatedVariable(n) 118 if err != nil { 119 t.Fatalf("err: %s", err) 120 } 121 122 actual, err := i.Values(scope, map[string]config.InterpolatedVariable{ 123 "foo": v, 124 }) 125 if err != nil { 126 t.Fatalf("err: %s", err) 127 } 128 129 expected := map[string]ast.Variable{ 130 "foo": expectedVar, 131 } 132 if !reflect.DeepEqual(actual, expected) { 133 t.Fatalf("bad: %#v", actual) 134 } 135 }