generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 56
/
utils.go
231 lines (199 loc) · 7.05 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
package integrationtests
import (
"context"
"fmt"
"math/rand"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
"github.com/kubernetes-csi/csi-proxy/v2/pkg/volume"
)
// 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"
}
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
}
// sizeIsAround returns true if the actual size is around the expected size
// (considers the fact that some bytes were lost)
func sizeIsAround(t *testing.T, actualSize, expectedSize int64) bool {
// An upper bound on the number of bytes that are lost when creating or resizing a partition
var volumeSizeBytesLoss int64 = (20 * 1024 * 1024)
var lowerBound = expectedSize - volumeSizeBytesLoss
var upperBound = expectedSize
t.Logf("Checking that the size is inside the bounds: %d < (actual) %d < %d", lowerBound, actualSize, upperBound)
return lowerBound <= actualSize && actualSize <= upperBound
}
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
}
// volumeInit initializes a volume, it creates a VHD, initializes it,
// creates a partition with the max size and formats the volume corresponding to that partition
func volumeInit(volumeClient volume.Interface, t *testing.T) (*VirtualHardDisk, string, func()) {
vhd, vhdCleanup := diskInit(t)
listRequest := &volume.ListVolumesOnDiskRequest{
DiskNumber: vhd.DiskNumber,
}
listResponse, err := volumeClient.ListVolumesOnDisk(context.TODO(), listRequest)
if err != nil {
t.Fatalf("List response: %v", err)
}
volumeIDsLen := len(listResponse.VolumeIDs)
if volumeIDsLen != 1 {
t.Fatalf("Number of volumes not equal to 1: %d", volumeIDsLen)
}
volumeID := listResponse.VolumeIDs[0]
t.Logf("VolumeID %v", volumeID)
isVolumeFormattedRequest := &volume.IsVolumeFormattedRequest{
VolumeID: volumeID,
}
isVolumeFormattedResponse, err := volumeClient.IsVolumeFormatted(context.TODO(), isVolumeFormattedRequest)
if err != nil {
t.Fatalf("Is volume formatted request error: %v", err)
}
if isVolumeFormattedResponse.Formatted {
t.Fatal("Volume formatted. Unexpected !!")
}
formatVolumeRequest := &volume.FormatVolumeRequest{
VolumeID: volumeID,
}
_, err = volumeClient.FormatVolume(context.TODO(), formatVolumeRequest)
if err != nil {
t.Fatalf("Volume format failed. Error: %v", err)
}
isVolumeFormattedResponse, err = volumeClient.IsVolumeFormatted(context.TODO(), isVolumeFormattedRequest)
if err != nil {
t.Fatalf("Is volume formatted request error: %v", err)
}
if !isVolumeFormattedResponse.Formatted {
t.Fatal("Volume should be formatted. Unexpected !!")
}
return vhd, volumeID, vhdCleanup
}