-
Notifications
You must be signed in to change notification settings - Fork 161
/
test_environment.go
78 lines (62 loc) · 1.53 KB
/
test_environment.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
package acceptance
import (
"fmt"
"os"
"path/filepath"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type Environment interface {
Home() string
Path(string) string
Copy(string, string) error
WriteContent(string, []byte) error
}
type testEnvironment struct {
cmdRunner boshsys.CmdRunner
fileSystem boshsys.FileSystem
home string
}
func NewTestEnvironment(fileSystem boshsys.FileSystem, logger boshlog.Logger) Environment {
return testEnvironment{
cmdRunner: boshsys.NewExecCmdRunner(logger),
fileSystem: fileSystem,
home: os.TempDir(),
}
}
func (e testEnvironment) Home() string {
return e.home
}
func (e testEnvironment) Path(name string) string {
return filepath.Join(e.home, name)
}
func (e testEnvironment) Copy(destName, srcPath string) error {
if srcPath == "" {
return fmt.Errorf("Cannot use an empty source file path '' for destination file '%s'", destName)
}
_, _, exitCode, err := e.cmdRunner.RunCommand(
"cp",
srcPath,
e.Path(destName),
)
if exitCode != 0 {
return fmt.Errorf("cp of '%s' to '%s' failed", srcPath, destName)
}
return err
}
func (e testEnvironment) WriteContent(destName string, contents []byte) error {
tmpFile, err := e.fileSystem.TempFile("bosh-cli-acceptance")
if err != nil {
return err
}
defer e.fileSystem.RemoveAll(tmpFile.Name())
_, err = tmpFile.Write(contents)
if err != nil {
return err
}
err = tmpFile.Close()
if err != nil {
return err
}
return e.Copy(destName, tmpFile.Name())
}