|
1 | 1 | package integrationtests
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "fmt"
|
5 |
| - "io/ioutil" |
6 |
| - "math/rand" |
7 | 6 | "os"
|
8 |
| - "os/exec" |
9 |
| - "strings" |
10 |
| - "time" |
11 |
| - |
12 | 7 | "testing"
|
13 |
| -) |
14 | 8 |
|
15 |
| -const letterset = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
16 | 11 |
|
17 |
| -var seededRand = rand.New(rand.NewSource(time.Now().UnixNano())) |
| 12 | + fs "github.com/kubernetes-csi/csi-proxy/pkg/filesystem" |
| 13 | + fsapi "github.com/kubernetes-csi/csi-proxy/pkg/filesystem/api" |
| 14 | + "github.com/kubernetes-csi/csi-proxy/pkg/smb" |
| 15 | + smbapi "github.com/kubernetes-csi/csi-proxy/pkg/smb/api" |
| 16 | +) |
18 | 17 |
|
19 |
| -func stringWithCharset(length int, charset string) string { |
20 |
| - b := make([]byte, length) |
21 |
| - for i := range b { |
22 |
| - b[i] = charset[seededRand.Intn(len(charset))] |
23 |
| - } |
24 |
| - return string(b) |
| 18 | +func TestSmbAPIGroup(t *testing.T) { |
| 19 | + t.Run("v1alpha1SmbTests", func(t *testing.T) { |
| 20 | + v1alpha1SmbTests(t) |
| 21 | + }) |
| 22 | + t.Run("v1beta1SmbTests", func(t *testing.T) { |
| 23 | + v1beta1SmbTests(t) |
| 24 | + }) |
| 25 | + t.Run("v1beta2SmbTests", func(t *testing.T) { |
| 26 | + v1beta2SmbTests(t) |
| 27 | + }) |
| 28 | + t.Run("v1SmbTests", func(t *testing.T) { |
| 29 | + v1SmbTests(t) |
| 30 | + }) |
25 | 31 | }
|
26 | 32 |
|
27 |
| -// RandomString generates a random string with specified length |
28 |
| -func randomString(length int) string { |
29 |
| - return stringWithCharset(length, letterset) |
30 |
| -} |
| 33 | +func TestSmb(t *testing.T) { |
| 34 | + fsClient, err := fs.New(fsapi.New()) |
| 35 | + require.Nil(t, err) |
| 36 | + client, err := smb.New(smbapi.New(), fsClient) |
| 37 | + require.Nil(t, err) |
31 | 38 |
|
32 |
| -func setupUser(username, password string) error { |
33 |
| - cmdLine := fmt.Sprintf(`$PWord = ConvertTo-SecureString $Env:password -AsPlainText -Force` + |
34 |
| - `;New-Localuser -name $Env:username -accountneverexpires -password $PWord`) |
35 |
| - cmd := exec.Command("powershell", "/c", cmdLine) |
36 |
| - cmd.Env = append(os.Environ(), |
37 |
| - fmt.Sprintf("username=%s", username), |
38 |
| - fmt.Sprintf("password=%s", password)) |
39 |
| - if output, err := cmd.CombinedOutput(); err != nil { |
40 |
| - return fmt.Errorf("setupUser failed: %v, output: %q", err, string(output)) |
41 |
| - } |
42 |
| - return nil |
43 |
| -} |
| 39 | + username := randomString(5) |
| 40 | + password := randomString(10) + "!" |
| 41 | + sharePath := fmt.Sprintf("C:\\smbshare%s", randomString(5)) |
| 42 | + smbShare := randomString(6) |
44 | 43 |
|
45 |
| -func removeUser(t *testing.T, username string) { |
46 |
| - cmdLine := fmt.Sprintf(`Remove-Localuser -name $Env:username`) |
47 |
| - cmd := exec.Command("powershell", "/c", cmdLine) |
48 |
| - cmd.Env = append(os.Environ(), |
49 |
| - fmt.Sprintf("username=%s", username)) |
50 |
| - if output, err := cmd.CombinedOutput(); err != nil { |
51 |
| - t.Fatalf("setupUser failed: %v, output: %q", err, string(output)) |
52 |
| - } |
53 |
| -} |
| 44 | + localPath := fmt.Sprintf("C:\\localpath%s", randomString(5)) |
54 | 45 |
|
55 |
| -func setupSmbShare(shareName, localPath, username string) error { |
56 |
| - if err := os.MkdirAll(localPath, 0755); err != nil { |
57 |
| - return fmt.Errorf("setupSmbShare failed to create local path %q: %v", localPath, err) |
| 46 | + if err = setupUser(username, password); err != nil { |
| 47 | + t.Fatalf("TestSmbAPIGroup %v", err) |
58 | 48 | }
|
59 |
| - cmdLine := fmt.Sprintf(`New-SMBShare -Name $Env:sharename -Path $Env:path -fullaccess $Env:username`) |
60 |
| - cmd := exec.Command("powershell", "/c", cmdLine) |
61 |
| - cmd.Env = append(os.Environ(), |
62 |
| - fmt.Sprintf("sharename=%s", shareName), |
63 |
| - fmt.Sprintf("path=%s", localPath), |
64 |
| - fmt.Sprintf("username=%s", username)) |
65 |
| - if output, err := cmd.CombinedOutput(); err != nil { |
66 |
| - return fmt.Errorf("setupSmbShare failed: %v, output: %q", err, string(output)) |
67 |
| - } |
68 |
| - |
69 |
| - return nil |
70 |
| -} |
| 49 | + defer removeUser(t, username) |
71 | 50 |
|
72 |
| -func removeSmbShare(t *testing.T, shareName string) { |
73 |
| - cmdLine := fmt.Sprintf(`Remove-SMBShare -Name $Env:sharename -Force`) |
74 |
| - cmd := exec.Command("powershell", "/c", cmdLine) |
75 |
| - cmd.Env = append(os.Environ(), |
76 |
| - fmt.Sprintf("sharename=%s", shareName)) |
77 |
| - if output, err := cmd.CombinedOutput(); err != nil { |
78 |
| - t.Fatalf("setupSmbShare failed: %v, output: %q", err, string(output)) |
| 51 | + if err = setupSmbShare(smbShare, sharePath, username); err != nil { |
| 52 | + t.Fatalf("TestSmbAPIGroup %v", err) |
79 | 53 | }
|
80 |
| - return |
81 |
| -} |
| 54 | + defer removeSmbShare(t, smbShare) |
82 | 55 |
|
83 |
| -func getSmbGlobalMapping(remotePath string) error { |
84 |
| - // use PowerShell Environment Variables to store user input string to prevent command line injection |
85 |
| - // https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-5.1 |
86 |
| - cmdLine := fmt.Sprintf(`(Get-SmbGlobalMapping -RemotePath $Env:smbremotepath).Status`) |
| 56 | + hostname, err := os.Hostname() |
| 57 | + assert.Nil(t, err) |
87 | 58 |
|
88 |
| - cmd := exec.Command("powershell", "/c", cmdLine) |
89 |
| - cmd.Env = append(os.Environ(), |
90 |
| - fmt.Sprintf("smbremotepath=%s", remotePath)) |
91 |
| - output, err := cmd.CombinedOutput() |
92 |
| - if err != nil { |
93 |
| - return fmt.Errorf("Get-SmbGlobalMapping failed: %v, output: %q", err, string(output)) |
| 59 | + username = "domain\\" + username |
| 60 | + remotePath := "\\\\" + hostname + "\\" + smbShare |
| 61 | + // simulate Mount SMB operations around staging a volume on a node |
| 62 | + mountSmbShareReq := &smb.NewSmbGlobalMappingRequest{ |
| 63 | + RemotePath: remotePath, |
| 64 | + Username: username, |
| 65 | + Password: password, |
94 | 66 | }
|
95 |
| - if !strings.Contains(string(output), "OK") { |
96 |
| - return fmt.Errorf("Get-SmbGlobalMapping return status %q instead of OK", string(output)) |
97 |
| - } |
98 |
| - return nil |
99 |
| -} |
100 |
| - |
101 |
| -func writeReadFile(path string) error { |
102 |
| - fileName := path + "\\hello.txt" |
103 |
| - f, err := os.Create(fileName) |
| 67 | + _, err = client.NewSmbGlobalMapping(context.Background(), mountSmbShareReq) |
104 | 68 | if err != nil {
|
105 |
| - return fmt.Errorf("create file %q failed: %v", fileName, err) |
106 |
| - } |
107 |
| - defer f.Close() |
108 |
| - fileContent := "Hello World" |
109 |
| - if _, err = f.WriteString(fileContent); err != nil { |
110 |
| - return fmt.Errorf("write to file %q failed: %v", fileName, err) |
| 69 | + t.Fatalf("TestSmbAPIGroup %v", err) |
111 | 70 | }
|
112 |
| - if err = f.Sync(); err != nil { |
113 |
| - return fmt.Errorf("sync file %q failed: %v", fileName, err) |
| 71 | + |
| 72 | + err = getSmbGlobalMapping(remotePath) |
| 73 | + assert.Nil(t, err) |
| 74 | + |
| 75 | + err = writeReadFile(remotePath) |
| 76 | + assert.Nil(t, err) |
| 77 | + |
| 78 | + unmountSmbShareReq := &smb.RemoveSmbGlobalMappingRequest{ |
| 79 | + RemotePath: remotePath, |
114 | 80 | }
|
115 |
| - dat, err := ioutil.ReadFile(fileName) |
| 81 | + _, err = client.RemoveSmbGlobalMapping(context.Background(), unmountSmbShareReq) |
116 | 82 | if err != nil {
|
117 |
| - return fmt.Errorf("read file %q failed: %v", fileName, err) |
118 |
| - } |
119 |
| - if fileContent != string(dat) { |
120 |
| - return fmt.Errorf("read content of file %q failed: expected %q, got %q", fileName, fileContent, string(dat)) |
| 83 | + t.Fatalf("TestSmbAPIGroup %v", err) |
121 | 84 | }
|
122 |
| - return nil |
123 |
| -} |
124 |
| - |
125 |
| -func TestSmbAPIGroup(t *testing.T) { |
126 |
| - t.Run("v1alpha1SmbTests", func(t *testing.T) { |
127 |
| - v1alpha1SmbTests(t) |
128 |
| - }) |
129 |
| - t.Run("v1beta1SmbTests", func(t *testing.T) { |
130 |
| - v1beta1SmbTests(t) |
131 |
| - }) |
132 |
| - t.Run("v1beta2SmbTests", func(t *testing.T) { |
133 |
| - v1beta2SmbTests(t) |
134 |
| - }) |
135 |
| - t.Run("v1SmbTests", func(t *testing.T) { |
136 |
| - v1SmbTests(t) |
137 |
| - }) |
| 85 | + err = getSmbGlobalMapping(remotePath) |
| 86 | + assert.NotNil(t, err) |
| 87 | + err = writeReadFile(localPath) |
| 88 | + assert.NotNil(t, err) |
138 | 89 | }
|
0 commit comments