github.com/nalum/terraform@v0.3.2-0.20141223102918-aa2c22ffeff6/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_noVars(t *testing.T) { 146 originalState := &terraform.State{ 147 Modules: []*terraform.ModuleState{ 148 &terraform.ModuleState{ 149 Path: []string{"root"}, 150 Outputs: map[string]string{}, 151 }, 152 }, 153 } 154 155 statePath := testStateFile(t, originalState) 156 157 ui := new(cli.MockUi) 158 c := &OutputCommand{ 159 Meta: Meta{ 160 ContextOpts: testCtxConfig(testProvider()), 161 Ui: ui, 162 }, 163 } 164 165 args := []string{ 166 "-state", statePath, 167 "bar", 168 } 169 if code := c.Run(args); code != 1 { 170 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 171 } 172 } 173 174 func TestOutput_stateDefault(t *testing.T) { 175 originalState := &terraform.State{ 176 Modules: []*terraform.ModuleState{ 177 &terraform.ModuleState{ 178 Path: []string{"root"}, 179 Outputs: map[string]string{ 180 "foo": "bar", 181 }, 182 }, 183 }, 184 } 185 186 // Write the state file in a temporary directory with the 187 // default filename. 188 td, err := ioutil.TempDir("", "tf") 189 if err != nil { 190 t.Fatalf("err: %s", err) 191 } 192 statePath := filepath.Join(td, DefaultStateFilename) 193 194 f, err := os.Create(statePath) 195 if err != nil { 196 t.Fatalf("err: %s", err) 197 } 198 err = terraform.WriteState(originalState, f) 199 f.Close() 200 if err != nil { 201 t.Fatalf("err: %s", err) 202 } 203 204 // Change to that directory 205 cwd, err := os.Getwd() 206 if err != nil { 207 t.Fatalf("err: %s", err) 208 } 209 if err := os.Chdir(filepath.Dir(statePath)); err != nil { 210 t.Fatalf("err: %s", err) 211 } 212 defer os.Chdir(cwd) 213 214 ui := new(cli.MockUi) 215 c := &OutputCommand{ 216 Meta: Meta{ 217 ContextOpts: testCtxConfig(testProvider()), 218 Ui: ui, 219 }, 220 } 221 222 args := []string{ 223 "foo", 224 } 225 if code := c.Run(args); code != 0 { 226 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 227 } 228 229 actual := strings.TrimSpace(ui.OutputWriter.String()) 230 if actual != "bar" { 231 t.Fatalf("bad: %#v", actual) 232 } 233 }