github.com/nicgrayson/terraform@v0.4.3-0.20150415203910-c4de50829380/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]string{ 20 "foo": "bar", 21 }, 22 }, 23 }, 24 } 25 26 statePath := testStateFile(t, originalState) 27 28 ui := new(cli.MockUi) 29 c := &OutputCommand{ 30 Meta: Meta{ 31 ContextOpts: testCtxConfig(testProvider()), 32 Ui: ui, 33 }, 34 } 35 36 args := []string{ 37 "-state", statePath, 38 "foo", 39 } 40 if code := c.Run(args); code != 0 { 41 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 42 } 43 44 actual := strings.TrimSpace(ui.OutputWriter.String()) 45 if actual != "bar" { 46 t.Fatalf("bad: %#v", actual) 47 } 48 } 49 50 func TestOutput_badVar(t *testing.T) { 51 originalState := &terraform.State{ 52 Modules: []*terraform.ModuleState{ 53 &terraform.ModuleState{ 54 Path: []string{"root"}, 55 Outputs: map[string]string{ 56 "foo": "bar", 57 }, 58 }, 59 }, 60 } 61 62 statePath := testStateFile(t, originalState) 63 64 ui := new(cli.MockUi) 65 c := &OutputCommand{ 66 Meta: Meta{ 67 ContextOpts: testCtxConfig(testProvider()), 68 Ui: ui, 69 }, 70 } 71 72 args := []string{ 73 "-state", statePath, 74 "bar", 75 } 76 if code := c.Run(args); code != 1 { 77 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 78 } 79 } 80 81 func TestOutput_blank(t *testing.T) { 82 originalState := &terraform.State{ 83 Modules: []*terraform.ModuleState{ 84 &terraform.ModuleState{ 85 Path: []string{"root"}, 86 Outputs: map[string]string{ 87 "foo": "bar", 88 }, 89 }, 90 }, 91 } 92 93 statePath := testStateFile(t, originalState) 94 95 ui := new(cli.MockUi) 96 c := &OutputCommand{ 97 Meta: Meta{ 98 ContextOpts: testCtxConfig(testProvider()), 99 Ui: ui, 100 }, 101 } 102 103 args := []string{ 104 "-state", statePath, 105 "", 106 } 107 if code := c.Run(args); code != 1 { 108 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 109 } 110 } 111 112 func TestOutput_manyArgs(t *testing.T) { 113 ui := new(cli.MockUi) 114 c := &OutputCommand{ 115 Meta: Meta{ 116 ContextOpts: testCtxConfig(testProvider()), 117 Ui: ui, 118 }, 119 } 120 121 args := []string{ 122 "bad", 123 "bad", 124 } 125 if code := c.Run(args); code != 1 { 126 t.Fatalf("bad: \n%s", ui.OutputWriter.String()) 127 } 128 } 129 130 func TestOutput_noArgs(t *testing.T) { 131 ui := new(cli.MockUi) 132 c := &OutputCommand{ 133 Meta: Meta{ 134 ContextOpts: testCtxConfig(testProvider()), 135 Ui: ui, 136 }, 137 } 138 139 args := []string{} 140 if code := c.Run(args); code != 1 { 141 t.Fatalf("bad: \n%s", ui.OutputWriter.String()) 142 } 143 } 144 145 func TestOutput_noState(t *testing.T) { 146 originalState := &terraform.State{} 147 statePath := testStateFile(t, originalState) 148 149 ui := new(cli.MockUi) 150 c := &OutputCommand{ 151 Meta: Meta{ 152 ContextOpts: testCtxConfig(testProvider()), 153 Ui: ui, 154 }, 155 } 156 157 args := []string{ 158 "-state", statePath, 159 "foo", 160 } 161 if code := c.Run(args); code != 1 { 162 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 163 } 164 } 165 166 func TestOutput_noVars(t *testing.T) { 167 originalState := &terraform.State{ 168 Modules: []*terraform.ModuleState{ 169 &terraform.ModuleState{ 170 Path: []string{"root"}, 171 Outputs: map[string]string{}, 172 }, 173 }, 174 } 175 176 statePath := testStateFile(t, originalState) 177 178 ui := new(cli.MockUi) 179 c := &OutputCommand{ 180 Meta: Meta{ 181 ContextOpts: testCtxConfig(testProvider()), 182 Ui: ui, 183 }, 184 } 185 186 args := []string{ 187 "-state", statePath, 188 "bar", 189 } 190 if code := c.Run(args); code != 1 { 191 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 192 } 193 } 194 195 func TestOutput_stateDefault(t *testing.T) { 196 originalState := &terraform.State{ 197 Modules: []*terraform.ModuleState{ 198 &terraform.ModuleState{ 199 Path: []string{"root"}, 200 Outputs: map[string]string{ 201 "foo": "bar", 202 }, 203 }, 204 }, 205 } 206 207 // Write the state file in a temporary directory with the 208 // default filename. 209 td, err := ioutil.TempDir("", "tf") 210 if err != nil { 211 t.Fatalf("err: %s", err) 212 } 213 statePath := filepath.Join(td, DefaultStateFilename) 214 215 f, err := os.Create(statePath) 216 if err != nil { 217 t.Fatalf("err: %s", err) 218 } 219 err = terraform.WriteState(originalState, f) 220 f.Close() 221 if err != nil { 222 t.Fatalf("err: %s", err) 223 } 224 225 // Change to that directory 226 cwd, err := os.Getwd() 227 if err != nil { 228 t.Fatalf("err: %s", err) 229 } 230 if err := os.Chdir(filepath.Dir(statePath)); err != nil { 231 t.Fatalf("err: %s", err) 232 } 233 defer os.Chdir(cwd) 234 235 ui := new(cli.MockUi) 236 c := &OutputCommand{ 237 Meta: Meta{ 238 ContextOpts: testCtxConfig(testProvider()), 239 Ui: ui, 240 }, 241 } 242 243 args := []string{ 244 "foo", 245 } 246 if code := c.Run(args); code != 0 { 247 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 248 } 249 250 actual := strings.TrimSpace(ui.OutputWriter.String()) 251 if actual != "bar" { 252 t.Fatalf("bad: %#v", actual) 253 } 254 }