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