-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathgit_revision_test.go
245 lines (206 loc) · 5.52 KB
/
git_revision_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package build
import (
"context"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/hashicorp/go-version"
"github.com/hashicorp/hc-install/internal/testutil"
"github.com/hashicorp/hc-install/product"
"github.com/hashicorp/hc-install/src"
)
var (
_ src.Buildable = &GitRevision{}
_ src.Removable = &GitRevision{}
_ src.LoggerSettable = &GitRevision{}
)
func TestGitRevision_terraform(t *testing.T) {
testutil.EndToEndTest(t)
tempDir := t.TempDir()
gr := &GitRevision{
Product: product.Terraform,
LicenseDir: tempDir,
}
gr.SetLogger(testutil.TestLogger())
ctx := context.Background()
execPath, err := gr.Build(ctx)
if err != nil {
t.Fatal(err)
}
licensePath := filepath.Join(tempDir, dstLicenseFileName)
t.Cleanup(func() {
gr.Remove(ctx)
// check if license was deleted
if _, err := os.Stat(licensePath); !os.IsNotExist(err) {
t.Fatalf("license file not deleted at %q: %s", licensePath, err)
}
})
v, err := product.Terraform.GetVersion(ctx, execPath)
if err != nil {
t.Fatal(err)
}
latestConstraint, err := version.NewConstraint(">= 1.0")
if err != nil {
t.Fatal(err)
}
if !latestConstraint.Check(v.Core()) {
t.Fatalf("versions don't match (expected: %s, installed: %s)",
latestConstraint, v)
}
// check if license was copied
if _, err := os.Stat(licensePath); err != nil {
t.Fatalf("expected license file not found at %q: %s", licensePath, err)
}
}
func TestGitRevision_consul(t *testing.T) {
testutil.EndToEndTest(t)
gr := &GitRevision{Product: product.Consul}
gr.SetLogger(testutil.TestLogger())
ctx := context.Background()
execPath, err := gr.Build(ctx)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { gr.Remove(ctx) })
v, err := product.Consul.GetVersion(ctx, execPath)
if err != nil {
t.Fatal(err)
}
latestConstraint, err := version.NewConstraint(">= 1.0")
if err != nil {
t.Fatal(err)
}
if !latestConstraint.Check(v.Core()) {
t.Fatalf("versions don't match (expected: %s, installed: %s)",
latestConstraint, v)
}
}
func TestGitRevision_vault(t *testing.T) {
testutil.EndToEndTest(t)
gr := &GitRevision{Product: product.Vault}
gr.SetLogger(testutil.TestLogger())
ctx := context.Background()
execPath, err := gr.Build(ctx)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { gr.Remove(ctx) })
v, err := product.Vault.GetVersion(ctx, execPath)
if err != nil {
t.Fatal(err)
}
latestConstraint, err := version.NewConstraint(">= 1.0")
if err != nil {
t.Fatal(err)
}
if !latestConstraint.Check(v.Core()) {
t.Fatalf("versions don't match (expected: %s, installed: %s)",
latestConstraint, v)
}
}
func TestGitRevision_packer(t *testing.T) {
testutil.EndToEndTest(t)
gr := &GitRevision{Product: product.Packer}
gr.SetLogger(testutil.TestLogger())
ctx := context.Background()
execPath, err := gr.Build(ctx)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { gr.Remove(ctx) })
v, err := product.Packer.GetVersion(ctx, execPath)
if err != nil {
t.Fatal(err)
}
latestConstraint, err := version.NewConstraint(">= 1.0")
if err != nil {
t.Fatal(err)
}
if !latestConstraint.Check(v.Core()) {
t.Fatalf("versions don't match (expected: %s, installed: %s)",
latestConstraint, v)
}
}
func TestGitRevisionValidate(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
gr GitRevision
expectedErr error
}{
"Product-incorrect-binary-name": {
gr: GitRevision{
Product: product.Product{
BinaryName: func() string { return "invalid!" },
Name: product.Terraform.Name,
},
},
expectedErr: fmt.Errorf("invalid binary name: \"invalid!\""),
},
"Product-incorrect-name": {
gr: GitRevision{
Product: product.Product{
BinaryName: product.Terraform.BinaryName,
Name: "invalid!",
},
},
expectedErr: fmt.Errorf("invalid product name: \"invalid!\""),
},
"Product-missing-build-instructions": {
gr: GitRevision{
Product: product.Product{
BinaryName: product.Terraform.BinaryName,
Name: product.Terraform.Name,
},
},
expectedErr: fmt.Errorf("no build instructions"),
},
"Product-missing-build-instructions-build": {
gr: GitRevision{
Product: product.Product{
BinaryName: product.Terraform.BinaryName,
BuildInstructions: &product.BuildInstructions{
GitRepoURL: product.Terraform.BuildInstructions.GitRepoURL,
},
Name: product.Terraform.Name,
},
},
expectedErr: fmt.Errorf("missing build instructions"),
},
"Product-missing-build-instructions-gitrepourl": {
gr: GitRevision{
Product: product.Product{
BinaryName: product.Terraform.BinaryName,
BuildInstructions: &product.BuildInstructions{
Build: product.Terraform.BuildInstructions.Build,
},
Name: product.Terraform.Name,
},
},
expectedErr: fmt.Errorf("missing repository URL"),
},
"Product-valid": {
gr: GitRevision{
Product: product.Terraform,
},
},
}
for name, testCase := range testCases {
name, testCase := name, testCase
t.Run(name, func(t *testing.T) {
t.Parallel()
err := testCase.gr.Validate()
if err == nil && testCase.expectedErr != nil {
t.Fatalf("expected error: %s, got no error", testCase.expectedErr)
}
if err != nil && testCase.expectedErr == nil {
t.Fatalf("expected no error, got error: %s", err)
}
if err != nil && testCase.expectedErr != nil && err.Error() != testCase.expectedErr.Error() {
t.Fatalf("expected error: %s, got error: %s", testCase.expectedErr, err)
}
})
}
}