generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 61
/
smb_test.go
189 lines (164 loc) · 5.88 KB
/
smb_test.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
package integrationtests
import (
"context"
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
fs "github.com/kubernetes-csi/csi-proxy/v2/pkg/filesystem"
fsapi "github.com/kubernetes-csi/csi-proxy/v2/pkg/filesystem/hostapi"
"github.com/kubernetes-csi/csi-proxy/v2/pkg/smb"
smbapi "github.com/kubernetes-csi/csi-proxy/v2/pkg/smb/hostapi"
)
func TestSMB(t *testing.T) {
fsClient, err := fs.New(fsapi.New())
require.Nil(t, err)
client, err := smb.New(smbapi.New(), fsClient)
require.Nil(t, err)
username := randomString(5)
password := randomString(10) + "!"
sharePath := fmt.Sprintf("C:\\smbshare%s", randomString(5))
smbShare := randomString(6)
localPath := fmt.Sprintf("C:\\localpath%s", randomString(5))
if err = setupUser(username, password); err != nil {
t.Fatalf("TestSMBAPIGroup %v", err)
}
defer removeUser(t, username)
if err = setupSMBShare(smbShare, sharePath, username); err != nil {
t.Fatalf("TestSMBAPIGroup %v", err)
}
defer removeSMBShare(t, smbShare)
hostname, err := os.Hostname()
assert.Nil(t, err)
username = "domain\\" + username
remotePath := "\\\\" + hostname + "\\" + smbShare
// simulate Mount SMB operations around staging a volume on a node
mountSMBShareReq := &smb.NewSMBGlobalMappingRequest{
RemotePath: remotePath,
Username: username,
Password: password,
}
_, err = client.NewSMBGlobalMapping(context.Background(), mountSMBShareReq)
if err != nil {
t.Fatalf("TestSMBAPIGroup %v", err)
}
err = getSMBGlobalMapping(remotePath)
assert.Nil(t, err)
err = writeReadFile(remotePath)
assert.Nil(t, err)
unmountSMBShareReq := &smb.RemoveSMBGlobalMappingRequest{
RemotePath: remotePath,
}
_, err = client.RemoveSMBGlobalMapping(context.Background(), unmountSMBShareReq)
if err != nil {
t.Fatalf("TestSMBAPIGroup %v", err)
}
err = getSMBGlobalMapping(remotePath)
assert.NotNil(t, err)
err = writeReadFile(localPath)
assert.NotNil(t, err)
}
const letterset = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))
func stringWithCharset(length int, charset string) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}
// RandomString generates a random string with specified length
func randomString(length int) string {
return stringWithCharset(length, letterset)
}
func setupUser(username, password string) error {
cmdLine := fmt.Sprintf(`$PWord = ConvertTo-SecureString $Env:password -AsPlainText -Force` +
`;New-Localuser -name $Env:username -accountneverexpires -password $PWord`)
cmd := exec.Command("powershell", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("username=%s", username),
fmt.Sprintf("password=%s", password))
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("setupUser failed: %v, output: %q", err, string(output))
}
return nil
}
func removeUser(t *testing.T, username string) {
cmdLine := fmt.Sprintf(`Remove-Localuser -name $Env:username`)
cmd := exec.Command("powershell", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("username=%s", username))
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("setupUser failed: %v, output: %q", err, string(output))
}
}
func setupSMBShare(shareName, localPath, username string) error {
if err := os.MkdirAll(localPath, 0755); err != nil {
return fmt.Errorf("setupSMBShare failed to create local path %q: %v", localPath, err)
}
cmdLine := fmt.Sprintf(`New-SMBShare -Name $Env:sharename -Path $Env:path -fullaccess $Env:username`)
cmd := exec.Command("powershell", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("sharename=%s", shareName),
fmt.Sprintf("path=%s", localPath),
fmt.Sprintf("username=%s", username))
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("setupSMBShare failed: %v, output: %q", err, string(output))
}
return nil
}
func removeSMBShare(t *testing.T, shareName string) {
cmdLine := fmt.Sprintf(`Remove-SMBShare -Name $Env:sharename -Force`)
cmd := exec.Command("powershell", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("sharename=%s", shareName))
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("setupSMBShare failed: %v, output: %q", err, string(output))
}
return
}
func getSMBGlobalMapping(remotePath string) error {
// use PowerShell Environment Variables to store user input string to prevent command line injection
// https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-5.1
cmdLine := fmt.Sprintf(`(Get-SmbGlobalMapping -RemotePath $Env:smbremotepath).Status`)
cmd := exec.Command("powershell", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("smbremotepath=%s", remotePath))
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("Get-SmbGlobalMapping failed: %v, output: %q", err, string(output))
}
if !strings.Contains(string(output), "OK") {
return fmt.Errorf("Get-SmbGlobalMapping return status %q instead of OK", string(output))
}
return nil
}
func writeReadFile(path string) error {
fileName := path + "\\hello.txt"
f, err := os.Create(fileName)
if err != nil {
return fmt.Errorf("create file %q failed: %v", fileName, err)
}
defer f.Close()
fileContent := "Hello World"
if _, err = f.WriteString(fileContent); err != nil {
return fmt.Errorf("write to file %q failed: %v", fileName, err)
}
if err = f.Sync(); err != nil {
return fmt.Errorf("sync file %q failed: %v", fileName, err)
}
dat, err := ioutil.ReadFile(fileName)
if err != nil {
return fmt.Errorf("read file %q failed: %v", fileName, err)
}
if fileContent != string(dat) {
return fmt.Errorf("read content of file %q failed: expected %q, got %q", fileName, fileContent, string(dat))
}
return nil
}