Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 82af81b

Browse files
committed
command: tests for apply
1 parent b62ff04 commit 82af81b

4 files changed

Lines changed: 164 additions & 2 deletions

File tree

command/apply.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package command
33
import (
44
"flag"
55
"fmt"
6+
"os"
67
"strings"
78

89
"github.com/hashicorp/terraform/config"
@@ -18,7 +19,11 @@ type ApplyCommand struct {
1819
}
1920

2021
func (c *ApplyCommand) Run(args []string) int {
22+
var statePath, stateOutPath string
23+
2124
cmdFlags := flag.NewFlagSet("apply", flag.ContinueOnError)
25+
cmdFlags.StringVar(&statePath, "state", "terraform.tfstate", "path")
26+
cmdFlags.StringVar(&stateOutPath, "state-out", "", "path")
2227
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
2328
if err := cmdFlags.Parse(args); err != nil {
2429
return 1
@@ -33,6 +38,16 @@ func (c *ApplyCommand) Run(args []string) int {
3338
return 1
3439
}
3540

41+
// TODO: if state, but not exist, -init required
42+
43+
if statePath == "" {
44+
c.Ui.Error("-state cannot be blank")
45+
return 1
46+
}
47+
if stateOutPath == "" {
48+
stateOutPath = statePath
49+
}
50+
3651
b, err := config.Load(args[0])
3752
if err != nil {
3853
c.Ui.Error(fmt.Sprintf("Error loading blueprint: %s", err))
@@ -60,6 +75,19 @@ func (c *ApplyCommand) Run(args []string) int {
6075
return 1
6176
}
6277

78+
// Write state out to the file
79+
f, err := os.Create(stateOutPath)
80+
if err != nil {
81+
c.Ui.Error(fmt.Sprintf("Failed to save state: %s", err))
82+
return 1
83+
}
84+
defer f.Close()
85+
86+
if err := terraform.WriteState(state, f); err != nil {
87+
c.Ui.Error(fmt.Sprintf("Failed to save state: %s", err))
88+
return 1
89+
}
90+
6391
c.Ui.Output(strings.TrimSpace(state.String()))
6492

6593
return 0
@@ -74,8 +102,15 @@ Usage: terraform apply [terraform.tf]
74102
75103
Options:
76104
77-
-init If specified, it is okay to build brand new infrastructure
78-
(with no state file specified).
105+
-init If specified, it is okay to build brand new
106+
infrastructure (with no state file specified).
107+
108+
-state=terraform.tfstate Path to the state file to build off of. This file
109+
will also be written to with updated state unless
110+
-state-out is specified.
111+
112+
-state-out=file.tfstate Path to save the new state. If not specified, the
113+
-state value will be used.
79114
80115
`
81116
return strings.TrimSpace(helpText)

command/apply_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package command
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform/terraform"
9+
"github.com/mitchellh/cli"
10+
)
11+
12+
func TestApply(t *testing.T) {
13+
tf, err := ioutil.TempFile("", "tf")
14+
if err != nil {
15+
t.Fatalf("err: %s", err)
16+
}
17+
statePath := tf.Name()
18+
tf.Close()
19+
os.Remove(tf.Name())
20+
21+
p := testProvider()
22+
ui := new(cli.MockUi)
23+
c := &ApplyCommand{
24+
TFConfig: testTFConfig(p),
25+
Ui: ui,
26+
}
27+
28+
args := []string{
29+
"-state", statePath,
30+
testFixturePath("apply"),
31+
}
32+
if code := c.Run(args); code != 0 {
33+
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
34+
}
35+
36+
if _, err := os.Stat(statePath); err != nil {
37+
t.Fatalf("err: %s", err)
38+
}
39+
40+
f, err := os.Open(statePath)
41+
if err != nil {
42+
t.Fatalf("err: %s", err)
43+
}
44+
defer f.Close()
45+
46+
state, err := terraform.ReadState(f)
47+
if err != nil {
48+
t.Fatalf("err: %s", err)
49+
}
50+
if state == nil {
51+
t.Fatal("state should not be nil")
52+
}
53+
}
54+
55+
func TestApply_noState(t *testing.T) {
56+
p := testProvider()
57+
ui := new(cli.MockUi)
58+
c := &ApplyCommand{
59+
TFConfig: testTFConfig(p),
60+
Ui: ui,
61+
}
62+
63+
args := []string{
64+
"-state=",
65+
testFixturePath("apply"),
66+
}
67+
if code := c.Run(args); code != 1 {
68+
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
69+
}
70+
}
71+
72+
func TestApply_stateNoExist(t *testing.T) {
73+
p := testProvider()
74+
ui := new(cli.MockUi)
75+
c := &ApplyCommand{
76+
TFConfig: testTFConfig(p),
77+
Ui: ui,
78+
}
79+
80+
args := []string{
81+
"-state=idontexist.tfstate",
82+
testFixturePath("apply"),
83+
}
84+
// TODO
85+
return
86+
if code := c.Run(args); code != 1 {
87+
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
88+
}
89+
}

command/command_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package command
2+
3+
import (
4+
"path/filepath"
5+
6+
"github.com/hashicorp/terraform/terraform"
7+
)
8+
9+
// This is the directory where our test fixtures are.
10+
const fixtureDir = "./test-fixtures"
11+
12+
func testFixturePath(name string) string {
13+
return filepath.Join(fixtureDir, name, "main.tf")
14+
}
15+
16+
func testTFConfig(p terraform.ResourceProvider) *terraform.Config {
17+
return &terraform.Config{
18+
Providers: map[string]terraform.ResourceProviderFactory{
19+
"test": func() (terraform.ResourceProvider, error) {
20+
return p, nil
21+
},
22+
},
23+
}
24+
}
25+
26+
func testProvider() *terraform.MockResourceProvider {
27+
p := new(terraform.MockResourceProvider)
28+
p.ResourcesReturn = []terraform.ResourceType{
29+
terraform.ResourceType{
30+
Name: "test_instance",
31+
},
32+
}
33+
34+
return p
35+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
resource "test_instance" "foo" {
2+
ami = "bar"
3+
}

0 commit comments

Comments
 (0)