-
Notifications
You must be signed in to change notification settings - Fork 58
/
terraform_version_test.go
132 lines (123 loc) · 2.73 KB
/
terraform_version_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package tfexec
import (
"context"
"fmt"
"os"
"testing"
"github.com/hashicorp/go-version"
)
func TestTerraformCLIVersion(t *testing.T) {
cases := []struct {
desc string
mockCommands []*mockCommand
want string
ok bool
}{
{
desc: "parse outputs of terraform version",
mockCommands: []*mockCommand{
{
args: []string{"terraform", "version"},
stdout: "Terraform v0.12.28\n",
exitCode: 0,
},
},
want: "0.12.28",
ok: true,
},
{
desc: "failed to run terraform version",
mockCommands: []*mockCommand{
{
args: []string{"terraform", "version"},
exitCode: 1,
},
},
want: "",
ok: false,
},
{
desc: "with check point warning",
mockCommands: []*mockCommand{
{
args: []string{"terraform", "version"},
stdout: `Terraform v0.12.28
Your version of Terraform is out of date! The latest version
is 0.12.29. You can update by downloading from https://www.terraform.io/downloads.html
`,
exitCode: 0,
},
},
want: "0.12.28",
ok: true,
},
}
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
e := NewMockExecutor(tc.mockCommands)
terraformCLI := NewTerraformCLI(e)
got, err := terraformCLI.Version(context.Background())
if tc.ok && err != nil {
t.Fatalf("unexpected err: %s", err)
}
if !tc.ok && err == nil {
t.Fatalf("expected to return an error, but no error, got = %s", got)
}
if tc.ok && got.String() != tc.want {
t.Errorf("got: %s, want: %s", got, tc.want)
}
})
}
}
func TestAccTerraformCLIVersion(t *testing.T) {
SkipUnlessAcceptanceTestEnabled(t)
e := NewExecutor("", os.Environ())
terraformCLI := NewTerraformCLI(e)
got, err := terraformCLI.Version(context.Background())
if err != nil {
t.Fatalf("failed to run terraform version: %s", err)
}
if got.String() == "" {
t.Error("failed to parse terraform version")
}
fmt.Printf("got = %s\n", got)
}
func TestTruncatePreReleaseVersion(t *testing.T) {
cases := []struct {
desc string
v string
want string
ok bool
}{
{
desc: "pre-release",
v: "1.6.0-rc1",
want: "1.6.0",
ok: true,
},
{
desc: "not pre-release",
v: "1.6.0",
want: "1.6.0",
ok: true,
},
}
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
v, err := version.NewVersion(tc.v)
if err != nil {
t.Fatalf("failed to parse version: %s", err)
}
got, err := truncatePreReleaseVersion(v)
if tc.ok && err != nil {
t.Fatalf("unexpected err: %s", err)
}
if !tc.ok && err == nil {
t.Fatalf("expected to return an error, but no error, got = %s", got)
}
if tc.ok && got.String() != tc.want {
t.Errorf("got: %s, want: %s", got, tc.want)
}
})
}
}