github.com/sinangedik/terraform@v0.3.5/command/init_test.go (about) 1 package command 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/hashicorp/terraform/remote" 9 "github.com/hashicorp/terraform/terraform" 10 "github.com/mitchellh/cli" 11 ) 12 13 func TestInit(t *testing.T) { 14 dir := tempDir(t) 15 16 ui := new(cli.MockUi) 17 c := &InitCommand{ 18 Meta: Meta{ 19 ContextOpts: testCtxConfig(testProvider()), 20 Ui: ui, 21 }, 22 } 23 24 args := []string{ 25 testFixturePath("init"), 26 dir, 27 } 28 if code := c.Run(args); code != 0 { 29 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 30 } 31 32 if _, err := os.Stat(filepath.Join(dir, "hello.tf")); err != nil { 33 t.Fatalf("err: %s", err) 34 } 35 } 36 37 func TestInit_cwd(t *testing.T) { 38 dir := tempDir(t) 39 if err := os.MkdirAll(dir, 0755); err != nil { 40 t.Fatalf("err: %s", err) 41 } 42 43 // Change to the temporary directory 44 cwd, err := os.Getwd() 45 if err != nil { 46 t.Fatalf("err: %s", err) 47 } 48 if err := os.Chdir(dir); err != nil { 49 t.Fatalf("err: %s", err) 50 } 51 defer os.Chdir(cwd) 52 53 ui := new(cli.MockUi) 54 c := &InitCommand{ 55 Meta: Meta{ 56 ContextOpts: testCtxConfig(testProvider()), 57 Ui: ui, 58 }, 59 } 60 61 args := []string{ 62 testFixturePath("init"), 63 } 64 if code := c.Run(args); code != 0 { 65 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 66 } 67 68 if _, err := os.Stat("hello.tf"); err != nil { 69 t.Fatalf("err: %s", err) 70 } 71 } 72 73 func TestInit_multipleArgs(t *testing.T) { 74 ui := new(cli.MockUi) 75 c := &InitCommand{ 76 Meta: Meta{ 77 ContextOpts: testCtxConfig(testProvider()), 78 Ui: ui, 79 }, 80 } 81 82 args := []string{ 83 "bad", 84 "bad", 85 } 86 if code := c.Run(args); code != 1 { 87 t.Fatalf("bad: \n%s", ui.OutputWriter.String()) 88 } 89 } 90 91 func TestInit_noArgs(t *testing.T) { 92 ui := new(cli.MockUi) 93 c := &InitCommand{ 94 Meta: Meta{ 95 ContextOpts: testCtxConfig(testProvider()), 96 Ui: ui, 97 }, 98 } 99 100 args := []string{} 101 if code := c.Run(args); code != 1 { 102 t.Fatalf("bad: \n%s", ui.OutputWriter.String()) 103 } 104 } 105 106 // https://github.com/hashicorp/terraform/issues/518 107 func TestInit_dstInSrc(t *testing.T) { 108 dir := tempDir(t) 109 if err := os.MkdirAll(dir, 0755); err != nil { 110 t.Fatalf("err: %s", err) 111 } 112 113 // Change to the temporary directory 114 cwd, err := os.Getwd() 115 if err != nil { 116 t.Fatalf("err: %s", err) 117 } 118 if err := os.Chdir(dir); err != nil { 119 t.Fatalf("err: %s", err) 120 } 121 defer os.Chdir(cwd) 122 123 if _, err := os.Create("issue518.tf"); err != nil { 124 t.Fatalf("err: %s", err) 125 } 126 127 ui := new(cli.MockUi) 128 c := &InitCommand{ 129 Meta: Meta{ 130 ContextOpts: testCtxConfig(testProvider()), 131 Ui: ui, 132 }, 133 } 134 135 args := []string{ 136 ".", 137 "foo", 138 } 139 if code := c.Run(args); code != 0 { 140 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 141 } 142 143 if _, err := os.Stat(filepath.Join(dir, "foo", "issue518.tf")); err != nil { 144 t.Fatalf("err: %s", err) 145 } 146 } 147 148 func TestInit_remoteState(t *testing.T) { 149 tmp, cwd := testCwd(t) 150 defer testFixCwd(t, tmp, cwd) 151 152 s := terraform.NewState() 153 conf, srv := testRemoteState(t, s, 200) 154 defer srv.Close() 155 156 ui := new(cli.MockUi) 157 c := &InitCommand{ 158 Meta: Meta{ 159 ContextOpts: testCtxConfig(testProvider()), 160 Ui: ui, 161 }, 162 } 163 164 args := []string{ 165 "-backend", "http", 166 "-address", conf.Config["address"], 167 testFixturePath("init"), 168 tmp, 169 } 170 if code := c.Run(args); code != 0 { 171 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 172 } 173 174 if _, err := os.Stat(filepath.Join(tmp, "hello.tf")); err != nil { 175 t.Fatalf("err: %s", err) 176 } 177 178 path, _ := remote.HiddenStatePath() 179 _, err := os.Stat(path) 180 if err != nil { 181 t.Fatalf("missing state") 182 } 183 }