/
bbl.go
269 lines (221 loc) · 6.14 KB
/
bbl.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package actors
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
"time"
"github.com/cloudfoundry/bosh-bootloader/integration-test"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)
const (
AWSIAAS = iota
GCPIAAS
)
type BBL struct {
stateDirectory string
pathToBBL string
configuration integration.Config
envID string
}
type IAAS int
func NewBBL(stateDirectory string, pathToBBL string, configuration integration.Config, envIDSuffix string) BBL {
envIDPrefix := os.Getenv("BBL_TEST_ENV_ID_PREFIX")
if envIDPrefix == "" {
envIDPrefix = "bbl-test"
}
return BBL{
stateDirectory: stateDirectory,
pathToBBL: pathToBBL,
configuration: configuration,
envID: fmt.Sprintf("%s-%s", envIDPrefix, envIDSuffix),
}
}
func (b BBL) PredefinedEnvID() string {
return b.envID
}
func (b BBL) Up(iaas IAAS, additionalArgs []string) {
args := []string{
"--state-dir", b.stateDirectory,
"--debug",
"up",
}
args = append(args, additionalArgs...)
switch iaas {
case AWSIAAS:
args = append(args, []string{
"--iaas", "aws",
"--aws-access-key-id", b.configuration.AWSAccessKeyID,
"--aws-secret-access-key", b.configuration.AWSSecretAccessKey,
"--aws-region", b.configuration.AWSRegion,
}...)
if b.configuration.EnableTerraformFlag {
args = append(args, "--terraform")
}
case GCPIAAS:
args = append(args, []string{
"--iaas", "gcp",
"--gcp-service-account-key", b.configuration.GCPServiceAccountKeyPath,
"--gcp-project-id", b.configuration.GCPProjectID,
"--gcp-region", b.configuration.GCPRegion,
"--gcp-zone", b.configuration.GCPZone,
}...)
default:
panic(errors.New("invalid iaas"))
}
session := b.execute(args, os.Stdout, os.Stderr)
Eventually(session, 40*time.Minute).Should(gexec.Exit(0))
}
func (b BBL) UpWithInvalidAWSCredentials() {
args := []string{
"--state-dir", b.stateDirectory,
"up",
"--iaas", "aws",
"--aws-access-key-id", "some-bad-access-key-id",
"--aws-secret-access-key", "some-bad-secret-access-key",
"--aws-region", b.configuration.AWSRegion,
}
session := b.execute(args, os.Stdout, os.Stderr)
Eventually(session, 10*time.Second).Should(gexec.Exit(1))
}
func (b BBL) Destroy() {
session := b.execute([]string{
"--state-dir", b.stateDirectory,
"destroy",
"--no-confirm",
}, os.Stdout, os.Stderr)
Eventually(session, 10*time.Minute).Should(gexec.Exit(0))
}
func (b BBL) SaveDirectorCA() string {
stdout := bytes.NewBuffer([]byte{})
session := b.execute([]string{
"--state-dir", b.stateDirectory,
"director-ca-cert",
}, stdout, os.Stderr)
Eventually(session, 10*time.Minute).Should(gexec.Exit(0))
file, err := ioutil.TempFile("", "")
defer file.Close()
Expect(err).NotTo(HaveOccurred())
file.Write(stdout.Bytes())
return file.Name()
}
func (b BBL) DirectorUsername() string {
return b.fetchValue("director-username")
}
func (b BBL) DirectorPassword() string {
return b.fetchValue("director-password")
}
func (b BBL) DirectorAddress() string {
return b.fetchValue("director-address")
}
func (b BBL) DirectorCACert() string {
return b.fetchValue("director-ca-cert")
}
func (b BBL) SSHKey() string {
return b.fetchValue("ssh-key")
}
func (b BBL) EnvID() string {
return b.fetchValue("env-id")
}
func (b BBL) CreateLB(loadBalancerType string, cert string, key string, chain string) {
args := []string{
"--state-dir", b.stateDirectory,
"create-lbs",
"--type", loadBalancerType,
}
if loadBalancerType == "cf" || GetIAAS(b.configuration) == AWSIAAS {
args = append(args,
"--cert", cert,
"--key", key,
"--chain", chain,
)
}
session := b.execute(args, os.Stdout, os.Stderr)
Eventually(session, 10*time.Minute).Should(gexec.Exit(0))
}
func (b BBL) CreateGCPLB(loadBalancerType string) {
args := []string{
"--state-dir", b.stateDirectory,
"create-lbs",
"--type", loadBalancerType,
}
session := b.execute(args, os.Stdout, os.Stderr)
Eventually(session, 10*time.Minute).Should(gexec.Exit(0))
}
func (b BBL) LBs() *gexec.Session {
args := []string{
"--state-dir", b.stateDirectory,
"lbs",
}
session := b.execute(args, os.Stdout, os.Stderr)
Eventually(session, 10*time.Minute).Should(gexec.Exit(0))
return session
}
func (b BBL) UpdateLB(certPath, keyPath string) {
args := []string{
"--state-dir", b.stateDirectory,
"update-lbs",
"--cert", certPath,
"--key", keyPath,
}
session := b.execute(args, os.Stdout, os.Stderr)
Eventually(session, 10*time.Minute).Should(gexec.Exit(0))
}
func (b BBL) DeleteLBs() {
args := []string{
"--state-dir", b.stateDirectory,
"delete-lbs",
}
session := b.execute(args, os.Stdout, os.Stderr)
Eventually(session, 10*time.Minute).Should(gexec.Exit(0))
}
func (b BBL) fetchValue(value string) string {
args := []string{
"--state-dir", b.stateDirectory,
value,
}
stdout := bytes.NewBuffer([]byte{})
stderr := bytes.NewBuffer([]byte{})
b.execute(args, stdout, stderr).Wait(30 * time.Second)
return strings.TrimSpace(string(stdout.Bytes()))
}
func (b BBL) execute(args []string, stdout io.Writer, stderr io.Writer) *gexec.Session {
cmd := exec.Command(b.pathToBBL, args...)
session, err := gexec.Start(cmd, stdout, stderr)
Expect(err).NotTo(HaveOccurred())
return session
}
func LBURL(config integration.Config, bbl BBL, state integration.State) (string, error) {
lbs := bbl.fetchValue("lbs")
var url string
if IAASString(config) == "aws" {
cutLBsPrefix := strings.Split(lbs, "[")[1]
url = strings.Split(cutLBsPrefix, "]")[0]
} else {
url = strings.Split(lbs, " ")[2]
}
return fmt.Sprintf("https://%s", url), nil
}
func IAASString(config integration.Config) string {
if config.AWSAccessKeyID != "" && config.AWSSecretAccessKey != "" && config.AWSRegion != "" {
return "aws"
}
if config.GCPServiceAccountKeyPath != "" && config.GCPProjectID != "" && config.GCPRegion != "" && config.GCPZone != "" {
return "gcp"
}
return ""
}
func GetIAAS(config integration.Config) IAAS {
if config.AWSAccessKeyID != "" && config.AWSSecretAccessKey != "" && config.AWSRegion != "" {
return AWSIAAS
}
if config.GCPServiceAccountKeyPath != "" && config.GCPProjectID != "" && config.GCPRegion != "" && config.GCPZone != "" {
return GCPIAAS
}
return -1
}