forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_test_environment.go
93 lines (77 loc) · 1.97 KB
/
remote_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package acceptance
import (
"fmt"
"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 remoteTestEnvironment struct {
vmUsername string
vmIP string
vmPort string
privateKeyPath string
cmdRunner boshsys.CmdRunner
fileSystem boshsys.FileSystem
}
func NewRemoteTestEnvironment(
vmUsername string,
vmIP string,
vmPort string,
privateKeyPath string,
fileSystem boshsys.FileSystem,
logger boshlog.Logger,
) Environment {
return remoteTestEnvironment{
vmUsername: vmUsername,
vmIP: vmIP,
vmPort: vmPort,
privateKeyPath: privateKeyPath,
cmdRunner: boshsys.NewExecCmdRunner(logger),
fileSystem: fileSystem,
}
}
func (e remoteTestEnvironment) Home() string {
return filepath.Join("/", "home", e.vmUsername)
}
func (e remoteTestEnvironment) Path(name string) string {
return filepath.Join(e.Home(), name)
}
func (e remoteTestEnvironment) 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(
"scp",
"-o", "StrictHostKeyChecking=no",
"-i", e.privateKeyPath,
"-P", e.vmPort,
srcPath,
fmt.Sprintf("%s@%s:%s", e.vmUsername, e.vmIP, e.Path(destName)),
)
if exitCode != 0 {
return fmt.Errorf("scp of '%s' to '%s' failed", srcPath, destName)
}
return err
}
func (e remoteTestEnvironment) 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())
}