-
Notifications
You must be signed in to change notification settings - Fork 24
/
utils.go
72 lines (63 loc) · 1.98 KB
/
utils.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
package integration
import (
"encoding/json"
"math/rand"
"os"
"os/exec"
"time"
"github.com/cloudfoundry/bosh-s3cli/config"
. "github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)
const alphaNum = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
// GenerateRandomString generates a random string of desired length (default: 25)
func GenerateRandomString(params ...int) string {
size := 25
if len(params) == 1 {
size = params[0]
}
randBytes := make([]byte, size)
for i := range randBytes {
randBytes[i] = alphaNum[rand.Intn(len(alphaNum))]
}
return string(randBytes)
}
// MakeConfigFile creates a config file from a S3Cli config struct
func MakeConfigFile(cfg *config.S3Cli) string {
cfgBytes, err := json.Marshal(cfg)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
tmpFile, err := os.CreateTemp("", "s3cli-test")
gomega.Expect(err).ToNot(gomega.HaveOccurred())
_, err = tmpFile.Write(cfgBytes)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
err = tmpFile.Close()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
return tmpFile.Name()
}
// MakeContentFile creates a temporary file with content to upload to S3
func MakeContentFile(content string) string {
tmpFile, err := os.CreateTemp("", "s3cli-test-content")
gomega.Expect(err).ToNot(gomega.HaveOccurred())
_, err = tmpFile.Write([]byte(content))
gomega.Expect(err).ToNot(gomega.HaveOccurred())
err = tmpFile.Close()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
return tmpFile.Name()
}
// RunS3CLI runs the s3cli and outputs the session after waiting for it to finish
func RunS3CLI(s3CLIPath string, configPath string, subcommand string, args ...string) (*gexec.Session, error) {
cmdArgs := []string{
"-c",
configPath,
subcommand,
}
cmdArgs = append(cmdArgs, args...)
command := exec.Command(s3CLIPath, cmdArgs...)
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
if err != nil {
return nil, err
}
session.Wait(1 * time.Minute)
return session, nil
}