-
Notifications
You must be signed in to change notification settings - Fork 43
/
test_support.go
109 lines (95 loc) · 2.88 KB
/
test_support.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
// Copyright (c) 2020 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package ssh
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)
func makeLocalTestDir(t *testing.T, dir string) {
t.Logf("creating local test dir: %s", dir)
if err := os.MkdirAll(dir, 0744); err != nil && !os.IsExist(err) {
t.Fatalf("makeLocalTestDir: failed to create dir: %s", err)
}
t.Logf("dir created: %s", dir)
}
func MakeLocalTestFile(t *testing.T, filePath, content string) {
srcDir := filepath.Dir(filePath)
if len(srcDir) > 0 && srcDir != "." {
makeLocalTestDir(t, srcDir)
}
t.Logf("creating local test file: %s", filePath)
file, err := os.Create(filePath)
if err != nil {
t.Fatalf("MakeLocalTestFile: failed to create file: %s", err)
}
defer file.Close()
buf := bytes.NewBufferString(content)
if _, err := buf.WriteTo(file); err != nil {
t.Fatal(err)
}
t.Logf("local test file created: %s", file.Name())
}
func RemoveLocalTestFile(t *testing.T, fileName string) {
t.Logf("removing local test path: %s", fileName)
if err := os.RemoveAll(fileName); err != nil && !os.IsNotExist(err) {
t.Fatalf("RemoveLocalTestFile: failed: %s", err)
}
}
func makeRemoteTestSSHDir(t *testing.T, args SSHArgs, dir string) {
t.Logf("creating remote test dir over SSH: %s", dir)
_, err := Run(args, nil, fmt.Sprintf(`mkdir -p %s`, dir))
if err != nil {
t.Fatalf("makeRemoteTestSSHDir: failed: %s", err)
}
// validate
result, err := Run(args, nil, fmt.Sprintf(`stat %s`, dir))
if err != nil {
t.Fatalf("makeRemoteTestSSHDir %s", err)
}
t.Logf("dir created: %s", result)
}
func MakeRemoteTestSSHFile(t *testing.T, args SSHArgs, filePath, content string) {
MakeRemoteTestSSHDir(t, args, filePath)
t.Logf("creating test file over SSH: %s", filePath)
_, err := Run(args, nil, fmt.Sprintf(`echo '%s' > %s`, content, filePath))
if err != nil {
t.Fatalf("MakeRemoteTestSSHFile: failed: %s", err)
}
result, _ := Run(args, nil, fmt.Sprintf(`ls %s`, filePath))
t.Logf("file created: %s", result)
}
func MakeRemoteTestSSHDir(t *testing.T, args SSHArgs, filePath string) {
dir := filepath.Dir(filePath)
if len(dir) > 0 && dir != "." {
makeRemoteTestSSHDir(t, args, dir)
}
}
func AssertRemoteTestSSHFile(t *testing.T, args SSHArgs, filePath string) {
t.Logf("stat remote SSH test file: %s", filePath)
_, err := Run(args, nil, fmt.Sprintf(`stat %s`, filePath))
if err != nil {
t.Fatal(err)
}
}
func RemoveRemoteTestSSHFile(t *testing.T, args SSHArgs, fileName string) {
t.Logf("removing test file over SSH: %s", fileName)
_, err := Run(args, nil, fmt.Sprintf(`rm -rf %s`, fileName))
if err != nil {
t.Fatal(err)
}
}
func getTestFileContent(t *testing.T, fileName string) string {
file, err := os.Open(fileName)
if err != nil {
t.Fatal(err)
}
buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(file); err != nil {
t.Fatal(err)
}
return strings.TrimSpace(buf.String())
}