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