github.com/lamielle/terraform@v0.3.2-0.20141121070651-81f008ba53d5/config/interpolate_funcs_test.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "testing" 8 ) 9 10 func TestInterpolateFuncConcat(t *testing.T) { 11 cases := []struct { 12 Args []string 13 Result string 14 Error bool 15 }{ 16 { 17 []string{"foo", "bar", "baz"}, 18 "foobarbaz", 19 false, 20 }, 21 22 { 23 []string{"foo", "bar"}, 24 "foobar", 25 false, 26 }, 27 28 { 29 []string{"foo"}, 30 "foo", 31 false, 32 }, 33 } 34 35 for i, tc := range cases { 36 actual, err := interpolationFuncConcat(nil, tc.Args...) 37 if (err != nil) != tc.Error { 38 t.Fatalf("%d: err: %s", i, err) 39 } 40 41 if actual != tc.Result { 42 t.Fatalf("%d: bad: %#v", i, actual) 43 } 44 } 45 } 46 47 func TestInterpolateFuncFile(t *testing.T) { 48 tf, err := ioutil.TempFile("", "tf") 49 if err != nil { 50 t.Fatalf("err: %s", err) 51 } 52 path := tf.Name() 53 tf.Write([]byte("foo")) 54 tf.Close() 55 defer os.Remove(path) 56 57 cases := []struct { 58 Args []string 59 Result string 60 Error bool 61 }{ 62 { 63 []string{path}, 64 "foo", 65 false, 66 }, 67 68 // Invalid path 69 { 70 []string{"/i/dont/exist"}, 71 "", 72 true, 73 }, 74 75 // Too many args 76 { 77 []string{"foo", "bar"}, 78 "", 79 true, 80 }, 81 } 82 83 for i, tc := range cases { 84 actual, err := interpolationFuncFile(nil, tc.Args...) 85 if (err != nil) != tc.Error { 86 t.Fatalf("%d: err: %s", i, err) 87 } 88 89 if actual != tc.Result { 90 t.Fatalf("%d: bad: %#v", i, actual) 91 } 92 } 93 } 94 95 func TestInterpolateFuncJoin(t *testing.T) { 96 cases := []struct { 97 Args []string 98 Result string 99 Error bool 100 }{ 101 { 102 []string{","}, 103 "", 104 true, 105 }, 106 107 { 108 []string{",", "foo"}, 109 "foo", 110 false, 111 }, 112 113 { 114 []string{",", "foo", "bar"}, 115 "foo,bar", 116 false, 117 }, 118 119 { 120 []string{ 121 ".", 122 fmt.Sprintf( 123 "foo%sbar%sbaz", 124 InterpSplitDelim, 125 InterpSplitDelim), 126 }, 127 "foo.bar.baz", 128 false, 129 }, 130 } 131 132 for i, tc := range cases { 133 actual, err := interpolationFuncJoin(nil, tc.Args...) 134 if (err != nil) != tc.Error { 135 t.Fatalf("%d: err: %s", i, err) 136 } 137 138 if actual != tc.Result { 139 t.Fatalf("%d: bad: %#v", i, actual) 140 } 141 } 142 } 143 144 func TestInterpolateFuncLookup(t *testing.T) { 145 cases := []struct { 146 M map[string]string 147 Args []string 148 Result string 149 Error bool 150 }{ 151 { 152 map[string]string{ 153 "var.foo.bar": "baz", 154 }, 155 []string{"foo", "bar"}, 156 "baz", 157 false, 158 }, 159 160 // Invalid key 161 { 162 map[string]string{ 163 "var.foo.bar": "baz", 164 }, 165 []string{"foo", "baz"}, 166 "", 167 true, 168 }, 169 170 // Too many args 171 { 172 map[string]string{ 173 "var.foo.bar": "baz", 174 }, 175 []string{"foo", "bar", "baz"}, 176 "", 177 true, 178 }, 179 } 180 181 for i, tc := range cases { 182 actual, err := interpolationFuncLookup(tc.M, tc.Args...) 183 if (err != nil) != tc.Error { 184 t.Fatalf("%d: err: %s", i, err) 185 } 186 187 if actual != tc.Result { 188 t.Fatalf("%d: bad: %#v", i, actual) 189 } 190 } 191 } 192 193 func TestInterpolateFuncElement(t *testing.T) { 194 cases := []struct { 195 Args []string 196 Result string 197 Error bool 198 }{ 199 { 200 []string{"foo" + InterpSplitDelim + "baz", "1"}, 201 "baz", 202 false, 203 }, 204 205 { 206 []string{"foo", "0"}, 207 "foo", 208 false, 209 }, 210 211 // Invalid index should wrap vs. out-of-bounds 212 { 213 []string{"foo" + InterpSplitDelim + "baz", "2"}, 214 "foo", 215 false, 216 }, 217 218 // Too many args 219 { 220 []string{"foo" + InterpSplitDelim + "baz", "0", "1"}, 221 "", 222 true, 223 }, 224 } 225 226 for i, tc := range cases { 227 actual, err := interpolationFuncElement(nil, tc.Args...) 228 if (err != nil) != tc.Error { 229 t.Fatalf("%d: err: %s", i, err) 230 } 231 232 if actual != tc.Result { 233 t.Fatalf("%d: bad: %#v", i, actual) 234 } 235 } 236 }