github.com/mehmetalisavas/terraform@v0.7.10/command/show_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/config/module" 11 "github.com/hashicorp/terraform/terraform" 12 "github.com/mitchellh/cli" 13 ) 14 15 func TestShow(t *testing.T) { 16 ui := new(cli.MockUi) 17 c := &ShowCommand{ 18 Meta: Meta{ 19 ContextOpts: testCtxConfig(testProvider()), 20 Ui: ui, 21 }, 22 } 23 24 args := []string{ 25 "bad", 26 "bad", 27 } 28 if code := c.Run(args); code != 1 { 29 t.Fatalf("bad: \n%s", ui.OutputWriter.String()) 30 } 31 } 32 33 func TestShow_noArgs(t *testing.T) { 34 // Create the default state 35 td, err := ioutil.TempDir("", "tf") 36 if err != nil { 37 t.Fatalf("err: %s", err) 38 } 39 statePath := filepath.Join(td, DefaultStateFilename) 40 41 f, err := os.Create(statePath) 42 if err != nil { 43 t.Fatalf("err: %s", err) 44 } 45 err = terraform.WriteState(testState(), f) 46 f.Close() 47 if err != nil { 48 t.Fatalf("err: %s", err) 49 } 50 51 // Change to the temporary directory 52 cwd, err := os.Getwd() 53 if err != nil { 54 t.Fatalf("err: %s", err) 55 } 56 if err := os.Chdir(filepath.Dir(statePath)); err != nil { 57 t.Fatalf("err: %s", err) 58 } 59 defer os.Chdir(cwd) 60 61 ui := new(cli.MockUi) 62 c := &ShowCommand{ 63 Meta: Meta{ 64 ContextOpts: testCtxConfig(testProvider()), 65 Ui: ui, 66 }, 67 } 68 69 args := []string{} 70 if code := c.Run(args); code != 0 { 71 t.Fatalf("bad: \n%s", ui.OutputWriter.String()) 72 } 73 } 74 75 func TestShow_noArgsNoState(t *testing.T) { 76 // Create the default state 77 td, err := ioutil.TempDir("", "tf") 78 if err != nil { 79 t.Fatalf("err: %s", err) 80 } 81 statePath := filepath.Join(td, DefaultStateFilename) 82 83 // Change to the temporary directory 84 cwd, err := os.Getwd() 85 if err != nil { 86 t.Fatalf("err: %s", err) 87 } 88 if err := os.Chdir(filepath.Dir(statePath)); err != nil { 89 t.Fatalf("err: %s", err) 90 } 91 defer os.Chdir(cwd) 92 93 ui := new(cli.MockUi) 94 c := &ShowCommand{ 95 Meta: Meta{ 96 ContextOpts: testCtxConfig(testProvider()), 97 Ui: ui, 98 }, 99 } 100 101 args := []string{} 102 if code := c.Run(args); code != 0 { 103 t.Fatalf("bad: \n%s", ui.OutputWriter.String()) 104 } 105 } 106 107 func TestShow_plan(t *testing.T) { 108 planPath := testPlanFile(t, &terraform.Plan{ 109 Module: new(module.Tree), 110 }) 111 112 ui := new(cli.MockUi) 113 c := &ShowCommand{ 114 Meta: Meta{ 115 ContextOpts: testCtxConfig(testProvider()), 116 Ui: ui, 117 }, 118 } 119 120 args := []string{ 121 planPath, 122 } 123 if code := c.Run(args); code != 0 { 124 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 125 } 126 } 127 128 func TestShow_noArgsRemoteState(t *testing.T) { 129 tmp, cwd := testCwd(t) 130 defer testFixCwd(t, tmp, cwd) 131 132 // Pretend like we have a local cache of remote state 133 remoteStatePath := filepath.Join(tmp, DefaultDataDir, DefaultStateFilename) 134 if err := os.MkdirAll(filepath.Dir(remoteStatePath), 0755); err != nil { 135 t.Fatalf("err: %s", err) 136 } 137 f, err := os.Create(remoteStatePath) 138 if err != nil { 139 t.Fatalf("err: %s", err) 140 } 141 err = terraform.WriteState(testState(), f) 142 f.Close() 143 if err != nil { 144 t.Fatalf("err: %s", err) 145 } 146 147 ui := new(cli.MockUi) 148 c := &ShowCommand{ 149 Meta: Meta{ 150 ContextOpts: testCtxConfig(testProvider()), 151 Ui: ui, 152 }, 153 } 154 155 args := []string{} 156 if code := c.Run(args); code != 0 { 157 t.Fatalf("bad: \n%s", ui.OutputWriter.String()) 158 } 159 160 expected := "test_instance.foo" 161 actual := ui.OutputWriter.String() 162 if !strings.Contains(actual, expected) { 163 t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected) 164 } 165 } 166 167 func TestShow_state(t *testing.T) { 168 originalState := testState() 169 statePath := testStateFile(t, originalState) 170 171 ui := new(cli.MockUi) 172 c := &ShowCommand{ 173 Meta: Meta{ 174 ContextOpts: testCtxConfig(testProvider()), 175 Ui: ui, 176 }, 177 } 178 179 args := []string{ 180 statePath, 181 } 182 if code := c.Run(args); code != 0 { 183 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 184 } 185 }