Skip to content

Commit 4f70284

Browse files
refactor: move SMB specific test utils into smb_test.go
1 parent 53fc811 commit 4f70284

File tree

2 files changed

+115
-110
lines changed

2 files changed

+115
-110
lines changed

integrationtests/smb_test.go

+115
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ package integrationtests
33
import (
44
"context"
55
"fmt"
6+
"io/ioutil"
7+
"math/rand"
68
"os"
9+
"os/exec"
10+
"strings"
711
"testing"
12+
"time"
813

914
"github.com/stretchr/testify/assert"
1015
"github.com/stretchr/testify/require"
@@ -87,3 +92,113 @@ func TestSmb(t *testing.T) {
8792
err = writeReadFile(localPath)
8893
assert.NotNil(t, err)
8994
}
95+
96+
const letterset = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
97+
98+
var seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))
99+
100+
func stringWithCharset(length int, charset string) string {
101+
b := make([]byte, length)
102+
for i := range b {
103+
b[i] = charset[seededRand.Intn(len(charset))]
104+
}
105+
return string(b)
106+
}
107+
108+
// RandomString generates a random string with specified length
109+
func randomString(length int) string {
110+
return stringWithCharset(length, letterset)
111+
}
112+
113+
func setupUser(username, password string) error {
114+
cmdLine := fmt.Sprintf(`$PWord = ConvertTo-SecureString $Env:password -AsPlainText -Force` +
115+
`;New-Localuser -name $Env:username -accountneverexpires -password $PWord`)
116+
cmd := exec.Command("powershell", "/c", cmdLine)
117+
cmd.Env = append(os.Environ(),
118+
fmt.Sprintf("username=%s", username),
119+
fmt.Sprintf("password=%s", password))
120+
if output, err := cmd.CombinedOutput(); err != nil {
121+
return fmt.Errorf("setupUser failed: %v, output: %q", err, string(output))
122+
}
123+
return nil
124+
}
125+
126+
func removeUser(t *testing.T, username string) {
127+
cmdLine := fmt.Sprintf(`Remove-Localuser -name $Env:username`)
128+
cmd := exec.Command("powershell", "/c", cmdLine)
129+
cmd.Env = append(os.Environ(),
130+
fmt.Sprintf("username=%s", username))
131+
if output, err := cmd.CombinedOutput(); err != nil {
132+
t.Fatalf("setupUser failed: %v, output: %q", err, string(output))
133+
}
134+
}
135+
136+
func setupSmbShare(shareName, localPath, username string) error {
137+
if err := os.MkdirAll(localPath, 0755); err != nil {
138+
return fmt.Errorf("setupSmbShare failed to create local path %q: %v", localPath, err)
139+
}
140+
cmdLine := fmt.Sprintf(`New-SMBShare -Name $Env:sharename -Path $Env:path -fullaccess $Env:username`)
141+
cmd := exec.Command("powershell", "/c", cmdLine)
142+
cmd.Env = append(os.Environ(),
143+
fmt.Sprintf("sharename=%s", shareName),
144+
fmt.Sprintf("path=%s", localPath),
145+
fmt.Sprintf("username=%s", username))
146+
if output, err := cmd.CombinedOutput(); err != nil {
147+
return fmt.Errorf("setupSmbShare failed: %v, output: %q", err, string(output))
148+
}
149+
150+
return nil
151+
}
152+
153+
func removeSmbShare(t *testing.T, shareName string) {
154+
cmdLine := fmt.Sprintf(`Remove-SMBShare -Name $Env:sharename -Force`)
155+
cmd := exec.Command("powershell", "/c", cmdLine)
156+
cmd.Env = append(os.Environ(),
157+
fmt.Sprintf("sharename=%s", shareName))
158+
if output, err := cmd.CombinedOutput(); err != nil {
159+
t.Fatalf("setupSmbShare failed: %v, output: %q", err, string(output))
160+
}
161+
return
162+
}
163+
164+
func getSmbGlobalMapping(remotePath string) error {
165+
// use PowerShell Environment Variables to store user input string to prevent command line injection
166+
// https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-5.1
167+
cmdLine := fmt.Sprintf(`(Get-SmbGlobalMapping -RemotePath $Env:smbremotepath).Status`)
168+
169+
cmd := exec.Command("powershell", "/c", cmdLine)
170+
cmd.Env = append(os.Environ(),
171+
fmt.Sprintf("smbremotepath=%s", remotePath))
172+
output, err := cmd.CombinedOutput()
173+
if err != nil {
174+
return fmt.Errorf("Get-SmbGlobalMapping failed: %v, output: %q", err, string(output))
175+
}
176+
if !strings.Contains(string(output), "OK") {
177+
return fmt.Errorf("Get-SmbGlobalMapping return status %q instead of OK", string(output))
178+
}
179+
return nil
180+
}
181+
182+
func writeReadFile(path string) error {
183+
fileName := path + "\\hello.txt"
184+
f, err := os.Create(fileName)
185+
if err != nil {
186+
return fmt.Errorf("create file %q failed: %v", fileName, err)
187+
}
188+
defer f.Close()
189+
fileContent := "Hello World"
190+
if _, err = f.WriteString(fileContent); err != nil {
191+
return fmt.Errorf("write to file %q failed: %v", fileName, err)
192+
}
193+
if err = f.Sync(); err != nil {
194+
return fmt.Errorf("sync file %q failed: %v", fileName, err)
195+
}
196+
dat, err := ioutil.ReadFile(fileName)
197+
if err != nil {
198+
return fmt.Errorf("read file %q failed: %v", fileName, err)
199+
}
200+
if fileContent != string(dat) {
201+
return fmt.Errorf("read content of file %q failed: expected %q, got %q", fileName, fileContent, string(dat))
202+
}
203+
return nil
204+
}

integrationtests/utils.go

-110
Original file line numberDiff line numberDiff line change
@@ -346,113 +346,3 @@ func volumeInit(volumeClient volume.Interface, t *testing.T) (*VirtualHardDisk,
346346
}
347347
return vhd, volumeID, vhdCleanup
348348
}
349-
350-
const letterset = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
351-
352-
var seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))
353-
354-
func stringWithCharset(length int, charset string) string {
355-
b := make([]byte, length)
356-
for i := range b {
357-
b[i] = charset[seededRand.Intn(len(charset))]
358-
}
359-
return string(b)
360-
}
361-
362-
// RandomString generates a random string with specified length
363-
func randomString(length int) string {
364-
return stringWithCharset(length, letterset)
365-
}
366-
367-
func setupUser(username, password string) error {
368-
cmdLine := fmt.Sprintf(`$PWord = ConvertTo-SecureString $Env:password -AsPlainText -Force` +
369-
`;New-Localuser -name $Env:username -accountneverexpires -password $PWord`)
370-
cmd := exec.Command("powershell", "/c", cmdLine)
371-
cmd.Env = append(os.Environ(),
372-
fmt.Sprintf("username=%s", username),
373-
fmt.Sprintf("password=%s", password))
374-
if output, err := cmd.CombinedOutput(); err != nil {
375-
return fmt.Errorf("setupUser failed: %v, output: %q", err, string(output))
376-
}
377-
return nil
378-
}
379-
380-
func removeUser(t *testing.T, username string) {
381-
cmdLine := fmt.Sprintf(`Remove-Localuser -name $Env:username`)
382-
cmd := exec.Command("powershell", "/c", cmdLine)
383-
cmd.Env = append(os.Environ(),
384-
fmt.Sprintf("username=%s", username))
385-
if output, err := cmd.CombinedOutput(); err != nil {
386-
t.Fatalf("setupUser failed: %v, output: %q", err, string(output))
387-
}
388-
}
389-
390-
func setupSmbShare(shareName, localPath, username string) error {
391-
if err := os.MkdirAll(localPath, 0755); err != nil {
392-
return fmt.Errorf("setupSmbShare failed to create local path %q: %v", localPath, err)
393-
}
394-
cmdLine := fmt.Sprintf(`New-SMBShare -Name $Env:sharename -Path $Env:path -fullaccess $Env:username`)
395-
cmd := exec.Command("powershell", "/c", cmdLine)
396-
cmd.Env = append(os.Environ(),
397-
fmt.Sprintf("sharename=%s", shareName),
398-
fmt.Sprintf("path=%s", localPath),
399-
fmt.Sprintf("username=%s", username))
400-
if output, err := cmd.CombinedOutput(); err != nil {
401-
return fmt.Errorf("setupSmbShare failed: %v, output: %q", err, string(output))
402-
}
403-
404-
return nil
405-
}
406-
407-
func removeSmbShare(t *testing.T, shareName string) {
408-
cmdLine := fmt.Sprintf(`Remove-SMBShare -Name $Env:sharename -Force`)
409-
cmd := exec.Command("powershell", "/c", cmdLine)
410-
cmd.Env = append(os.Environ(),
411-
fmt.Sprintf("sharename=%s", shareName))
412-
if output, err := cmd.CombinedOutput(); err != nil {
413-
t.Fatalf("setupSmbShare failed: %v, output: %q", err, string(output))
414-
}
415-
return
416-
}
417-
418-
func getSmbGlobalMapping(remotePath string) error {
419-
// use PowerShell Environment Variables to store user input string to prevent command line injection
420-
// https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-5.1
421-
cmdLine := fmt.Sprintf(`(Get-SmbGlobalMapping -RemotePath $Env:smbremotepath).Status`)
422-
423-
cmd := exec.Command("powershell", "/c", cmdLine)
424-
cmd.Env = append(os.Environ(),
425-
fmt.Sprintf("smbremotepath=%s", remotePath))
426-
output, err := cmd.CombinedOutput()
427-
if err != nil {
428-
return fmt.Errorf("Get-SmbGlobalMapping failed: %v, output: %q", err, string(output))
429-
}
430-
if !strings.Contains(string(output), "OK") {
431-
return fmt.Errorf("Get-SmbGlobalMapping return status %q instead of OK", string(output))
432-
}
433-
return nil
434-
}
435-
436-
func writeReadFile(path string) error {
437-
fileName := path + "\\hello.txt"
438-
f, err := os.Create(fileName)
439-
if err != nil {
440-
return fmt.Errorf("create file %q failed: %v", fileName, err)
441-
}
442-
defer f.Close()
443-
fileContent := "Hello World"
444-
if _, err = f.WriteString(fileContent); err != nil {
445-
return fmt.Errorf("write to file %q failed: %v", fileName, err)
446-
}
447-
if err = f.Sync(); err != nil {
448-
return fmt.Errorf("sync file %q failed: %v", fileName, err)
449-
}
450-
dat, err := ioutil.ReadFile(fileName)
451-
if err != nil {
452-
return fmt.Errorf("read file %q failed: %v", fileName, err)
453-
}
454-
if fileContent != string(dat) {
455-
return fmt.Errorf("read content of file %q failed: expected %q, got %q", fileName, fileContent, string(dat))
456-
}
457-
return nil
458-
}

0 commit comments

Comments
 (0)