forked from kubernetes-csi/csi-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
286 lines (240 loc) · 8.36 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package integrationtests
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"time"
"github.com/pkg/errors"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/kubernetes-csi/csi-proxy/pkg/server"
srvtypes "github.com/kubernetes-csi/csi-proxy/pkg/server/types"
)
// startServer starts the proxy's GRPC servers, and returns a function to shut them down when done with testing
func startServer(t *testing.T, apiGroups ...srvtypes.APIGroup) func() {
s := server.NewServer(apiGroups...)
listeningChan := make(chan interface{})
go func() {
assert.Nil(t, s.Start(listeningChan))
}()
select {
case <-listeningChan:
case <-time.After(5 * time.Second):
t.Fatalf("Timed out waiting for GRPC servers to start listening")
}
return func() {
assert.Nil(t, s.Stop())
}
}
func close(t *testing.T, closer io.Closer) {
assert.Nil(t, closer.Close())
}
// recursiveDiff ensures that dir1 and dir2 contain the same files, with the same contents.
// fileSuffixesToRemove will be removed from file names, if found.
func recursiveDiff(t *testing.T, dir1, dir2 string, fileSuffixesToRemove ...string) {
hashesDir1, err := fileHashes(dir1, fileSuffixesToRemove...)
require.Nil(t, err, "unable to get file hashes for directory %q", dir1)
hashesDir2, err := fileHashes(dir2, fileSuffixesToRemove...)
require.Nil(t, err, "unable to get file hashes for directory %q", dir2)
t.Logf("Hashes for dir1: %+v", hashesDir1)
t.Logf("Hashes for dir2: %+v", hashesDir2)
for filePath, hash1 := range hashesDir1 {
if hash2, present := hashesDir2[filePath]; assert.True(t, present, "%q present in %q but not in %q", filePath, dir1, dir2) {
if hash1 != hash2 {
contents1 := readFile(t, filepath.Join(dir1, filePath))
contents2 := readFile(t, filepath.Join(dir2, filePath))
differ := diffmatchpatch.New()
diffs := differ.DiffMain(contents1, contents2, true)
assert.Fail(t, fmt.Sprintf("File %q differs in %q and %q:\n", filePath, dir1, dir2), "Diff:\n%s", differ.DiffPrettyText(diffs))
}
delete(hashesDir2, filePath)
}
}
for filePath := range hashesDir2 {
assert.Fail(t, fmt.Sprintf("%q present in %q but not in %q", filePath, dir2, dir1))
}
}
// fileHashes walks through dir, and returns a map mapping file paths to MD5 hashes
func fileHashes(dir string, fileSuffixesToRemove ...string) (map[string]string, error) {
dir = strings.ReplaceAll(dir, "/", string(os.PathSeparator))
hashes := make(map[string]string)
if walkErr := filepath.Walk(dir, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return errors.Wrapf(err, "unable to descend into %q", filePath)
}
if info.IsDir() {
return nil
}
file, err := os.Open(filePath)
if err != nil {
return errors.Wrapf(err, "unable to open %q", filePath)
}
defer file.Close()
hasher := md5.New()
if _, err := io.Copy(hasher, file); err != nil {
return errors.Wrapf(err, "unable to read %q", filePath)
}
hashBytes := hasher.Sum(nil)[:16]
relativePath := strings.TrimPrefix(strings.TrimPrefix(filePath, dir), "/")
for _, suffix := range fileSuffixesToRemove {
relativePath = strings.TrimSuffix(relativePath, suffix)
}
hashes[relativePath] = hex.EncodeToString(hashBytes)
return nil
}); walkErr != nil {
return nil, walkErr
}
return hashes, nil
}
func readFile(t *testing.T, filePath string) string {
contents, err := ioutil.ReadFile(filePath)
require.Nil(t, err, "unable to read %q", filePath)
return string(contents)
}
// getKubeletPathForTest returns the path to the current working directory
// to be used anytime the filepath is required to be within context of csi-proxy
func getKubeletPathForTest(dir string, t *testing.T) string {
return filepath.Join("C:\\var\\lib\\kubelet", "testdir", dir)
}
// returns true if CSI_PROXY_GH_ACTIONS is set to "TRUE"
func isRunningOnGhActions() bool {
return os.Getenv("CSI_PROXY_GH_ACTIONS") == "TRUE"
}
// returns true if underlying os is windows
func isRunningWindows() bool {
return runtime.GOOS == "windows"
}
func skipTestOnCondition(t *testing.T, condition bool) {
if condition {
t.Skip("Skipping test")
}
}
// returns true if ENABLE_ISCSI_TESTS is set to "TRUE"
// used to skip iSCSI tests as they affect the test machine
// e.g. install an iSCSI target, format a disk, etc.
// Take care to use disposable clean VMs for tests
func shouldRunIscsiTests() bool {
return os.Getenv("ENABLE_ISCSI_TESTS") == "TRUE"
}
func runPowershellCmd(t *testing.T, command string) (string, error) {
cmd := exec.Command("powershell", "/c", fmt.Sprintf("& { $global:ProgressPreference = 'SilentlyContinue'; %s }", command))
t.Logf("Executing command: %q", cmd.String())
result, err := cmd.CombinedOutput()
return string(result), err
}
func diskCleanup(t *testing.T, vhdxPath, mountPath, testPluginPath string) {
if t.Failed() {
t.Logf("Test failed. Skipping cleanup!")
t.Logf("Mount path located at %s", mountPath)
t.Logf("VHDx path located at %s", vhdxPath)
return
}
var cmd, out string
var err error
cmd = fmt.Sprintf("Dismount-VHD -Path %s", vhdxPath)
if out, err = runPowershellCmd(t, cmd); err != nil {
t.Errorf("Error: %v. Command: %s. Out: %s", err, cmd, out)
}
cmd = fmt.Sprintf("rm %s", vhdxPath)
if out, err = runPowershellCmd(t, cmd); err != nil {
t.Errorf("Error: %v. Command: %s. Out: %s", err, cmd, out)
}
cmd = fmt.Sprintf("rmdir %s", mountPath)
if out, err = runPowershellCmd(t, cmd); err != nil {
t.Errorf("Error: %v. Command: %s. Out: %s", err, cmd, out)
}
if testPluginPath != "" {
cmd = fmt.Sprintf("rmdir %s", testPluginPath)
if out, err = runPowershellCmd(t, cmd); err != nil {
t.Errorf("Error: %v. Command: %s. Out: %s", err, cmd, out)
}
}
}
// VirtualHardDisk represents a VHD used for e2e tests
type VirtualHardDisk struct {
DiskNumber uint32
Path string
Mount string
TestPluginPath string
InitialSize int64
}
func getTestPluginPath() (string, int) {
s1 := rand.NewSource(time.Now().UTC().UnixNano())
r1 := rand.New(s1)
testId := r1.Intn(1000000)
return fmt.Sprintf("C:\\var\\lib\\kubelet\\plugins\\testplugin-%d.csi.io\\", testId), testId
}
func diskInit(t *testing.T) (*VirtualHardDisk, func()) {
testPluginPath, testId := getTestPluginPath()
mountPath := fmt.Sprintf("%smount-%d", testPluginPath, testId)
vhdxPath := fmt.Sprintf("%sdisk-%d.vhdx", testPluginPath, testId)
var cmd, out string
var err error
const initialSize = 1 * 1024 * 1024 * 1024
const partitionStyle = "GPT"
cmd = fmt.Sprintf("mkdir %s", mountPath)
if out, err = runPowershellCmd(t, cmd); err != nil {
t.Fatalf("Error: %v. Command: %q. Out: %s", err, cmd, out)
}
// Initialize the tests, using powershell directly.
// Create the new vhdx
cmd = fmt.Sprintf("New-VHD -Path %s -SizeBytes %d", vhdxPath, initialSize)
if out, err = runPowershellCmd(t, cmd); err != nil {
t.Fatalf("Error: %v. Command: %q. Out: %s.", err, cmd, out)
}
// Mount the vhdx as a disk
cmd = fmt.Sprintf("Mount-VHD -Path %s", vhdxPath)
if out, err = runPowershellCmd(t, cmd); err != nil {
t.Fatalf("Error: %v. Command: %q. Out: %s", err, cmd, out)
}
var diskNum uint64
var diskNumUnparsed string
cmd = fmt.Sprintf("(Get-VHD -Path %s).DiskNumber", vhdxPath)
if diskNumUnparsed, err = runPowershellCmd(t, cmd); err != nil {
t.Fatalf("Error: %v. Command: %s", err, cmd)
}
diskNumUnparsed = strings.TrimSpace(diskNumUnparsed)
if diskNum, err = strconv.ParseUint(diskNumUnparsed, 10, 32); err != nil {
t.Fatalf("Error: %v", err)
}
cmd = fmt.Sprintf("Initialize-Disk -Number %d -PartitionStyle %s", diskNum, partitionStyle)
if _, err = runPowershellCmd(t, cmd); err != nil {
t.Fatalf("Error: %v. Command: %s", err, cmd)
}
cmd = fmt.Sprintf("New-Partition -DiskNumber %d -UseMaximumSize", diskNum)
if _, err = runPowershellCmd(t, cmd); err != nil {
t.Fatalf("Error: %v. Command: %s", err, cmd)
}
cleanup := func() {
diskCleanup(t, vhdxPath, mountPath, testPluginPath)
}
vhd := &VirtualHardDisk{
DiskNumber: uint32(diskNum),
Path: vhdxPath,
Mount: mountPath,
TestPluginPath: testPluginPath,
InitialSize: initialSize,
}
return vhd, cleanup
}
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}