github.com/serbaut/terraform@v0.6.12-0.20160607213102-ac2d195cc560/command/output_test.go (about) 1 package command 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "strings" 8 "testing" 9 10 "github.com/hashicorp/terraform/terraform" 11 "github.com/mitchellh/cli" 12 ) 13 14 func TestOutput(t *testing.T) { 15 originalState := &terraform.State{ 16 Modules: []*terraform.ModuleState{ 17 &terraform.ModuleState{ 18 Path: []string{"root"}, 19 Outputs: map[string]*terraform.OutputState{ 20 "foo": &terraform.OutputState{ 21 Value: "bar", 22 Type: "string", 23 }, 24 }, 25 }, 26 }, 27 } 28 29 statePath := testStateFile(t, originalState) 30 31 ui := new(cli.MockUi) 32 c := &OutputCommand{ 33 Meta: Meta{ 34 ContextOpts: testCtxConfig(testProvider()), 35 Ui: ui, 36 }, 37 } 38 39 args := []string{ 40 "-state", statePath, 41 "foo", 42 } 43 if code := c.Run(args); code != 0 { 44 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 45 } 46 47 actual := strings.TrimSpace(ui.OutputWriter.String()) 48 if actual != "bar" { 49 t.Fatalf("bad: %#v", actual) 50 } 51 } 52 53 func TestModuleOutput(t *testing.T) { 54 originalState := &terraform.State{ 55 Modules: []*terraform.ModuleState{ 56 &terraform.ModuleState{ 57 Path: []string{"root"}, 58 Outputs: map[string]*terraform.OutputState{ 59 "foo": &terraform.OutputState{ 60 Value: "bar", 61 Type: "string", 62 }, 63 }, 64 }, 65 &terraform.ModuleState{ 66 Path: []string{"root", "my_module"}, 67 Outputs: map[string]*terraform.OutputState{ 68 "blah": &terraform.OutputState{ 69 Value: "tastatur", 70 Type: "string", 71 }, 72 }, 73 }, 74 }, 75 } 76 77 statePath := testStateFile(t, originalState) 78 79 ui := new(cli.MockUi) 80 c := &OutputCommand{ 81 Meta: Meta{ 82 ContextOpts: testCtxConfig(testProvider()), 83 Ui: ui, 84 }, 85 } 86 87 args := []string{ 88 "-state", statePath, 89 "-module", "my_module", 90 "blah", 91 } 92 93 if code := c.Run(args); code != 0 { 94 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 95 } 96 97 actual := strings.TrimSpace(ui.OutputWriter.String()) 98 if actual != "tastatur" { 99 t.Fatalf("bad: %#v", actual) 100 } 101 } 102 103 func TestMissingModuleOutput(t *testing.T) { 104 originalState := &terraform.State{ 105 Modules: []*terraform.ModuleState{ 106 &terraform.ModuleState{ 107 Path: []string{"root"}, 108 Outputs: map[string]*terraform.OutputState{ 109 "foo": &terraform.OutputState{ 110 Value: "bar", 111 Type: "string", 112 }, 113 }, 114 }, 115 }, 116 } 117 118 statePath := testStateFile(t, originalState) 119 120 ui := new(cli.MockUi) 121 c := &OutputCommand{ 122 Meta: Meta{ 123 ContextOpts: testCtxConfig(testProvider()), 124 Ui: ui, 125 }, 126 } 127 128 args := []string{ 129 "-state", statePath, 130 "-module", "not_existing_module", 131 "blah", 132 } 133 134 if code := c.Run(args); code != 1 { 135 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 136 } 137 } 138 139 func TestOutput_badVar(t *testing.T) { 140 originalState := &terraform.State{ 141 Modules: []*terraform.ModuleState{ 142 &terraform.ModuleState{ 143 Path: []string{"root"}, 144 Outputs: map[string]*terraform.OutputState{ 145 "foo": &terraform.OutputState{ 146 Value: "bar", 147 Type: "string", 148 }, 149 }, 150 }, 151 }, 152 } 153 154 statePath := testStateFile(t, originalState) 155 156 ui := new(cli.MockUi) 157 c := &OutputCommand{ 158 Meta: Meta{ 159 ContextOpts: testCtxConfig(testProvider()), 160 Ui: ui, 161 }, 162 } 163 164 args := []string{ 165 "-state", statePath, 166 "bar", 167 } 168 if code := c.Run(args); code != 1 { 169 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 170 } 171 } 172 173 func TestOutput_blank(t *testing.T) { 174 originalState := &terraform.State{ 175 Modules: []*terraform.ModuleState{ 176 &terraform.ModuleState{ 177 Path: []string{"root"}, 178 Outputs: map[string]*terraform.OutputState{ 179 "foo": &terraform.OutputState{ 180 Value: "bar", 181 Type: "string", 182 }, 183 "name": &terraform.OutputState{ 184 Value: "john-doe", 185 Type: "string", 186 }, 187 }, 188 }, 189 }, 190 } 191 192 statePath := testStateFile(t, originalState) 193 194 ui := new(cli.MockUi) 195 c := &OutputCommand{ 196 Meta: Meta{ 197 ContextOpts: testCtxConfig(testProvider()), 198 Ui: ui, 199 }, 200 } 201 202 args := []string{ 203 "-state", statePath, 204 "", 205 } 206 207 if code := c.Run(args); code != 0 { 208 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 209 } 210 211 expectedOutput := "foo = bar\nname = john-doe\n" 212 output := ui.OutputWriter.String() 213 if output != expectedOutput { 214 t.Fatalf("Expected output: %#v\ngiven: %#v", expectedOutput, output) 215 } 216 } 217 218 func TestOutput_manyArgs(t *testing.T) { 219 ui := new(cli.MockUi) 220 c := &OutputCommand{ 221 Meta: Meta{ 222 ContextOpts: testCtxConfig(testProvider()), 223 Ui: ui, 224 }, 225 } 226 227 args := []string{ 228 "bad", 229 "bad", 230 } 231 if code := c.Run(args); code != 1 { 232 t.Fatalf("bad: \n%s", ui.OutputWriter.String()) 233 } 234 } 235 236 func TestOutput_noArgs(t *testing.T) { 237 ui := new(cli.MockUi) 238 c := &OutputCommand{ 239 Meta: Meta{ 240 ContextOpts: testCtxConfig(testProvider()), 241 Ui: ui, 242 }, 243 } 244 245 args := []string{} 246 if code := c.Run(args); code != 1 { 247 t.Fatalf("bad: \n%s", ui.OutputWriter.String()) 248 } 249 } 250 251 func TestOutput_noState(t *testing.T) { 252 originalState := &terraform.State{} 253 statePath := testStateFile(t, originalState) 254 255 ui := new(cli.MockUi) 256 c := &OutputCommand{ 257 Meta: Meta{ 258 ContextOpts: testCtxConfig(testProvider()), 259 Ui: ui, 260 }, 261 } 262 263 args := []string{ 264 "-state", statePath, 265 "foo", 266 } 267 if code := c.Run(args); code != 1 { 268 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 269 } 270 } 271 272 func TestOutput_noVars(t *testing.T) { 273 originalState := &terraform.State{ 274 Modules: []*terraform.ModuleState{ 275 &terraform.ModuleState{ 276 Path: []string{"root"}, 277 Outputs: map[string]*terraform.OutputState{}, 278 }, 279 }, 280 } 281 282 statePath := testStateFile(t, originalState) 283 284 ui := new(cli.MockUi) 285 c := &OutputCommand{ 286 Meta: Meta{ 287 ContextOpts: testCtxConfig(testProvider()), 288 Ui: ui, 289 }, 290 } 291 292 args := []string{ 293 "-state", statePath, 294 "bar", 295 } 296 if code := c.Run(args); code != 1 { 297 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 298 } 299 } 300 301 func TestOutput_stateDefault(t *testing.T) { 302 originalState := &terraform.State{ 303 Modules: []*terraform.ModuleState{ 304 &terraform.ModuleState{ 305 Path: []string{"root"}, 306 Outputs: map[string]*terraform.OutputState{ 307 "foo": &terraform.OutputState{ 308 Value: "bar", 309 Type: "string", 310 }, 311 }, 312 }, 313 }, 314 } 315 316 // Write the state file in a temporary directory with the 317 // default filename. 318 td, err := ioutil.TempDir("", "tf") 319 if err != nil { 320 t.Fatalf("err: %s", err) 321 } 322 statePath := filepath.Join(td, DefaultStateFilename) 323 324 f, err := os.Create(statePath) 325 if err != nil { 326 t.Fatalf("err: %s", err) 327 } 328 err = terraform.WriteState(originalState, f) 329 f.Close() 330 if err != nil { 331 t.Fatalf("err: %s", err) 332 } 333 334 // Change to that directory 335 cwd, err := os.Getwd() 336 if err != nil { 337 t.Fatalf("err: %s", err) 338 } 339 if err := os.Chdir(filepath.Dir(statePath)); err != nil { 340 t.Fatalf("err: %s", err) 341 } 342 defer os.Chdir(cwd) 343 344 ui := new(cli.MockUi) 345 c := &OutputCommand{ 346 Meta: Meta{ 347 ContextOpts: testCtxConfig(testProvider()), 348 Ui: ui, 349 }, 350 } 351 352 args := []string{ 353 "foo", 354 } 355 if code := c.Run(args); code != 0 { 356 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 357 } 358 359 actual := strings.TrimSpace(ui.OutputWriter.String()) 360 if actual != "bar" { 361 t.Fatalf("bad: %#v", actual) 362 } 363 }