Skip to content

Commit 9a01a14

Browse files
authored
Merge pull request #258 from alexander-ding/enh/rename-api-groups
Rename Smb to SMB
2 parents 5c1a742 + d7d054f commit 9a01a14

File tree

5 files changed

+76
-76
lines changed

5 files changed

+76
-76
lines changed

integrationtests/smb_test.go

+19-19
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
smbapi "github.com/kubernetes-csi/csi-proxy/pkg/smb/api"
2121
)
2222

23-
func TestSmb(t *testing.T) {
23+
func TestSMB(t *testing.T) {
2424
fsClient, err := fs.New(fsapi.New())
2525
require.Nil(t, err)
2626
client, err := smb.New(smbapi.New(), fsClient)
@@ -34,45 +34,45 @@ func TestSmb(t *testing.T) {
3434
localPath := fmt.Sprintf("C:\\localpath%s", randomString(5))
3535

3636
if err = setupUser(username, password); err != nil {
37-
t.Fatalf("TestSmbAPIGroup %v", err)
37+
t.Fatalf("TestSMBAPIGroup %v", err)
3838
}
3939
defer removeUser(t, username)
4040

41-
if err = setupSmbShare(smbShare, sharePath, username); err != nil {
42-
t.Fatalf("TestSmbAPIGroup %v", err)
41+
if err = setupSMBShare(smbShare, sharePath, username); err != nil {
42+
t.Fatalf("TestSMBAPIGroup %v", err)
4343
}
44-
defer removeSmbShare(t, smbShare)
44+
defer removeSMBShare(t, smbShare)
4545

4646
hostname, err := os.Hostname()
4747
assert.Nil(t, err)
4848

4949
username = "domain\\" + username
5050
remotePath := "\\\\" + hostname + "\\" + smbShare
5151
// simulate Mount SMB operations around staging a volume on a node
52-
mountSmbShareReq := &smb.NewSmbGlobalMappingRequest{
52+
mountSMBShareReq := &smb.NewSMBGlobalMappingRequest{
5353
RemotePath: remotePath,
5454
Username: username,
5555
Password: password,
5656
}
57-
_, err = client.NewSmbGlobalMapping(context.Background(), mountSmbShareReq)
57+
_, err = client.NewSMBGlobalMapping(context.Background(), mountSMBShareReq)
5858
if err != nil {
59-
t.Fatalf("TestSmbAPIGroup %v", err)
59+
t.Fatalf("TestSMBAPIGroup %v", err)
6060
}
6161

62-
err = getSmbGlobalMapping(remotePath)
62+
err = getSMBGlobalMapping(remotePath)
6363
assert.Nil(t, err)
6464

6565
err = writeReadFile(remotePath)
6666
assert.Nil(t, err)
6767

68-
unmountSmbShareReq := &smb.RemoveSmbGlobalMappingRequest{
68+
unmountSMBShareReq := &smb.RemoveSMBGlobalMappingRequest{
6969
RemotePath: remotePath,
7070
}
71-
_, err = client.RemoveSmbGlobalMapping(context.Background(), unmountSmbShareReq)
71+
_, err = client.RemoveSMBGlobalMapping(context.Background(), unmountSMBShareReq)
7272
if err != nil {
73-
t.Fatalf("TestSmbAPIGroup %v", err)
73+
t.Fatalf("TestSMBAPIGroup %v", err)
7474
}
75-
err = getSmbGlobalMapping(remotePath)
75+
err = getSMBGlobalMapping(remotePath)
7676
assert.NotNil(t, err)
7777
err = writeReadFile(localPath)
7878
assert.NotNil(t, err)
@@ -118,9 +118,9 @@ func removeUser(t *testing.T, username string) {
118118
}
119119
}
120120

121-
func setupSmbShare(shareName, localPath, username string) error {
121+
func setupSMBShare(shareName, localPath, username string) error {
122122
if err := os.MkdirAll(localPath, 0755); err != nil {
123-
return fmt.Errorf("setupSmbShare failed to create local path %q: %v", localPath, err)
123+
return fmt.Errorf("setupSMBShare failed to create local path %q: %v", localPath, err)
124124
}
125125
cmdLine := fmt.Sprintf(`New-SMBShare -Name $Env:sharename -Path $Env:path -fullaccess $Env:username`)
126126
cmd := exec.Command("powershell", "/c", cmdLine)
@@ -129,24 +129,24 @@ func setupSmbShare(shareName, localPath, username string) error {
129129
fmt.Sprintf("path=%s", localPath),
130130
fmt.Sprintf("username=%s", username))
131131
if output, err := cmd.CombinedOutput(); err != nil {
132-
return fmt.Errorf("setupSmbShare failed: %v, output: %q", err, string(output))
132+
return fmt.Errorf("setupSMBShare failed: %v, output: %q", err, string(output))
133133
}
134134

135135
return nil
136136
}
137137

138-
func removeSmbShare(t *testing.T, shareName string) {
138+
func removeSMBShare(t *testing.T, shareName string) {
139139
cmdLine := fmt.Sprintf(`Remove-SMBShare -Name $Env:sharename -Force`)
140140
cmd := exec.Command("powershell", "/c", cmdLine)
141141
cmd.Env = append(os.Environ(),
142142
fmt.Sprintf("sharename=%s", shareName))
143143
if output, err := cmd.CombinedOutput(); err != nil {
144-
t.Fatalf("setupSmbShare failed: %v, output: %q", err, string(output))
144+
t.Fatalf("setupSMBShare failed: %v, output: %q", err, string(output))
145145
}
146146
return
147147
}
148148

149-
func getSmbGlobalMapping(remotePath string) error {
149+
func getSMBGlobalMapping(remotePath string) error {
150150
// use PowerShell Environment Variables to store user input string to prevent command line injection
151151
// https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-5.1
152152
cmdLine := fmt.Sprintf(`(Get-SmbGlobalMapping -RemotePath $Env:smbremotepath).Status`)

pkg/smb/api/api.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
)
99

1010
type API interface {
11-
IsSmbMapped(remotePath string) (bool, error)
12-
NewSmbLink(remotePath, localPath string) error
13-
NewSmbGlobalMapping(remotePath, username, password string) error
14-
RemoveSmbGlobalMapping(remotePath string) error
11+
IsSMBMapped(remotePath string) (bool, error)
12+
NewSMBLink(remotePath, localPath string) error
13+
NewSMBGlobalMapping(remotePath, username, password string) error
14+
RemoveSMBGlobalMapping(remotePath string) error
1515
}
1616

1717
type smbAPI struct{}
@@ -22,12 +22,12 @@ func New() API {
2222
return smbAPI{}
2323
}
2424

25-
func (smbAPI) IsSmbMapped(remotePath string) (bool, error) {
25+
func (smbAPI) IsSMBMapped(remotePath string) (bool, error) {
2626
cmdLine := `$(Get-SmbGlobalMapping -RemotePath $Env:smbremotepath -ErrorAction Stop).Status `
2727
cmdEnv := fmt.Sprintf("smbremotepath=%s", remotePath)
2828
out, err := utils.RunPowershellCmd(cmdLine, cmdEnv)
2929
if err != nil {
30-
return false, fmt.Errorf("error checking smb mapping. cmd %s, output: %s, err: %v", remotePath, string(out), err)
30+
return false, fmt.Errorf("error checking SMB mapping. cmd %s, output: %s, err: %v", remotePath, string(out), err)
3131
}
3232

3333
if len(out) == 0 || !strings.EqualFold(strings.TrimSpace(string(out)), "OK") {
@@ -36,14 +36,14 @@ func (smbAPI) IsSmbMapped(remotePath string) (bool, error) {
3636
return true, nil
3737
}
3838

39-
// NewSmbLink - creates a directory symbolic link to the remote share.
39+
// NewSMBLink - creates a directory symbolic link to the remote share.
4040
// The os.Symlink was having issue for cases where the destination was an SMB share - the container
4141
// runtime would complain stating "Access Denied". Because of this, we had to perform
4242
// this operation with powershell commandlet creating an directory softlink.
4343
// Since os.Symlink is currently being used in working code paths, no attempt is made in
4444
// alpha to merge the paths.
4545
// TODO (for beta release): Merge the link paths - os.Symlink and Powershell link path.
46-
func (smbAPI) NewSmbLink(remotePath, localPath string) error {
46+
func (smbAPI) NewSMBLink(remotePath, localPath string) error {
4747
if !strings.HasSuffix(remotePath, "\\") {
4848
// Golang has issues resolving paths mapped to file shares if they do not end in a trailing \
4949
// so add one if needed.
@@ -59,7 +59,7 @@ func (smbAPI) NewSmbLink(remotePath, localPath string) error {
5959
return nil
6060
}
6161

62-
func (smbAPI) NewSmbGlobalMapping(remotePath, username, password string) error {
62+
func (smbAPI) NewSMBGlobalMapping(remotePath, username, password string) error {
6363
// use PowerShell Environment Variables to store user input string to prevent command line injection
6464
// https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-5.1
6565
cmdLine := fmt.Sprintf(`$PWord = ConvertTo-SecureString -String $Env:smbpassword -AsPlainText -Force` +
@@ -69,15 +69,15 @@ func (smbAPI) NewSmbGlobalMapping(remotePath, username, password string) error {
6969
if output, err := utils.RunPowershellCmd(cmdLine, fmt.Sprintf("smbuser=%s", username),
7070
fmt.Sprintf("smbpassword=%s", password),
7171
fmt.Sprintf("smbremotepath=%s", remotePath)); err != nil {
72-
return fmt.Errorf("NewSmbGlobalMapping failed. output: %q, err: %v", string(output), err)
72+
return fmt.Errorf("NewSMBGlobalMapping failed. output: %q, err: %v", string(output), err)
7373
}
7474
return nil
7575
}
7676

77-
func (smbAPI) RemoveSmbGlobalMapping(remotePath string) error {
77+
func (smbAPI) RemoveSMBGlobalMapping(remotePath string) error {
7878
cmd := `Remove-SmbGlobalMapping -RemotePath $Env:smbremotepath -Force`
7979
if output, err := utils.RunPowershellCmd(cmd, fmt.Sprintf("smbremotepath=%s", remotePath)); err != nil {
80-
return fmt.Errorf("UnmountSmbShare failed. output: %q, err: %v", string(output), err)
80+
return fmt.Errorf("UnmountSMBShare failed. output: %q, err: %v", string(output), err)
8181
}
8282
return nil
8383
}

pkg/smb/smb.go

+28-28
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ import (
1010
"k8s.io/klog/v2"
1111
)
1212

13-
type Smb struct {
13+
type SMB struct {
1414
hostAPI smbapi.API
1515
fs fs.Interface
1616
}
1717

1818
type Interface interface {
19-
// NewSmbGlobalMapping creates an SMB mapping on the SMB client to an SMB share.
20-
NewSmbGlobalMapping(context.Context, *NewSmbGlobalMappingRequest) (*NewSmbGlobalMappingResponse, error)
19+
// NewSMBGlobalMapping creates an SMB mapping on the SMB client to an SMB share.
20+
NewSMBGlobalMapping(context.Context, *NewSMBGlobalMappingRequest) (*NewSMBGlobalMappingResponse, error)
2121

22-
// RemoveSmbGlobalMapping removes the SMB mapping to an SMB share.
23-
RemoveSmbGlobalMapping(context.Context, *RemoveSmbGlobalMappingRequest) (*RemoveSmbGlobalMappingResponse, error)
22+
// RemoveSMBGlobalMapping removes the SMB mapping to an SMB share.
23+
RemoveSMBGlobalMapping(context.Context, *RemoveSMBGlobalMappingRequest) (*RemoveSMBGlobalMappingResponse, error)
2424
}
2525

26-
// check that Smb implements the Interface
27-
var _ Interface = &Smb{}
26+
// check that SMB implements the Interface
27+
var _ Interface = &SMB{}
2828

2929
func normalizeWindowsPath(path string) string {
3030
normalizedPath := strings.Replace(path, "/", "\\", -1)
@@ -46,21 +46,21 @@ func getRootMappingPath(path string) (string, error) {
4646
klog.Errorf("remote path (%s) is invalid", path)
4747
return "", fmt.Errorf("remote path (%s) is invalid", path)
4848
}
49-
// parts[0] is a smb host name
50-
// parts[1] is a smb share name
49+
// parts[0] is a SMB host name
50+
// parts[1] is a SMB share name
5151
return strings.ToLower("\\\\" + parts[0] + "\\" + parts[1]), nil
5252
}
5353

54-
func New(hostAPI smbapi.API, fsClient fs.Interface) (*Smb, error) {
55-
return &Smb{
54+
func New(hostAPI smbapi.API, fsClient fs.Interface) (*SMB, error) {
55+
return &SMB{
5656
hostAPI: hostAPI,
5757
fs: fsClient,
5858
}, nil
5959
}
6060

61-
func (s *Smb) NewSmbGlobalMapping(context context.Context, request *NewSmbGlobalMappingRequest) (*NewSmbGlobalMappingResponse, error) {
62-
klog.V(2).Infof("calling NewSmbGlobalMapping with remote path %q", request.RemotePath)
63-
response := &NewSmbGlobalMappingResponse{}
61+
func (s *SMB) NewSMBGlobalMapping(context context.Context, request *NewSMBGlobalMappingRequest) (*NewSMBGlobalMappingResponse, error) {
62+
klog.V(2).Infof("calling NewSMBGlobalMapping with remote path %q", request.RemotePath)
63+
response := &NewSMBGlobalMappingResponse{}
6464
remotePath := normalizeWindowsPath(request.RemotePath)
6565
localPath := request.LocalPath
6666

@@ -74,7 +74,7 @@ func (s *Smb) NewSmbGlobalMapping(context context.Context, request *NewSmbGlobal
7474
return response, err
7575
}
7676

77-
isMapped, err := s.hostAPI.IsSmbMapped(mappingPath)
77+
isMapped, err := s.hostAPI.IsSMBMapped(mappingPath)
7878
if err != nil {
7979
isMapped = false
8080
}
@@ -89,9 +89,9 @@ func (s *Smb) NewSmbGlobalMapping(context context.Context, request *NewSmbGlobal
8989

9090
if !validResp.Valid {
9191
klog.V(4).Infof("RemotePath %s is not valid, removing now", mappingPath)
92-
err := s.hostAPI.RemoveSmbGlobalMapping(mappingPath)
92+
err := s.hostAPI.RemoveSMBGlobalMapping(mappingPath)
9393
if err != nil {
94-
klog.Errorf("RemoveSmbGlobalMapping(%s) failed with %v", mappingPath, err)
94+
klog.Errorf("RemoveSMBGlobalMapping(%s) failed with %v", mappingPath, err)
9595
return response, err
9696
}
9797
isMapped = false
@@ -102,9 +102,9 @@ func (s *Smb) NewSmbGlobalMapping(context context.Context, request *NewSmbGlobal
102102

103103
if !isMapped {
104104
klog.V(4).Infof("Remote %s not mapped. Mapping now!", mappingPath)
105-
err = s.hostAPI.NewSmbGlobalMapping(mappingPath, request.Username, request.Password)
105+
err = s.hostAPI.NewSMBGlobalMapping(mappingPath, request.Username, request.Password)
106106
if err != nil {
107-
klog.Errorf("failed NewSmbGlobalMapping %v", err)
107+
klog.Errorf("failed NewSMBGlobalMapping %v", err)
108108
return response, err
109109
}
110110
}
@@ -116,20 +116,20 @@ func (s *Smb) NewSmbGlobalMapping(context context.Context, request *NewSmbGlobal
116116
klog.Errorf("failed validate plugin path %v", err)
117117
return response, err
118118
}
119-
err = s.hostAPI.NewSmbLink(remotePath, localPath)
119+
err = s.hostAPI.NewSMBLink(remotePath, localPath)
120120
if err != nil {
121-
klog.Errorf("failed NewSmbLink %v", err)
121+
klog.Errorf("failed NewSMBLink %v", err)
122122
return response, fmt.Errorf("creating link %s to %s failed with error: %v", localPath, remotePath, err)
123123
}
124124
}
125125

126-
klog.V(2).Infof("NewSmbGlobalMapping on remote path %q is completed", request.RemotePath)
126+
klog.V(2).Infof("NewSMBGlobalMapping on remote path %q is completed", request.RemotePath)
127127
return response, nil
128128
}
129129

130-
func (s *Smb) RemoveSmbGlobalMapping(context context.Context, request *RemoveSmbGlobalMappingRequest) (*RemoveSmbGlobalMappingResponse, error) {
131-
klog.V(2).Infof("calling RemoveSmbGlobalMapping with remote path %q", request.RemotePath)
132-
response := &RemoveSmbGlobalMappingResponse{}
130+
func (s *SMB) RemoveSMBGlobalMapping(context context.Context, request *RemoveSMBGlobalMappingRequest) (*RemoveSMBGlobalMappingResponse, error) {
131+
klog.V(2).Infof("calling RemoveSMBGlobalMapping with remote path %q", request.RemotePath)
132+
response := &RemoveSMBGlobalMappingResponse{}
133133
remotePath := normalizeWindowsPath(request.RemotePath)
134134

135135
if remotePath == "" {
@@ -142,12 +142,12 @@ func (s *Smb) RemoveSmbGlobalMapping(context context.Context, request *RemoveSmb
142142
return response, err
143143
}
144144

145-
err = s.hostAPI.RemoveSmbGlobalMapping(mappingPath)
145+
err = s.hostAPI.RemoveSMBGlobalMapping(mappingPath)
146146
if err != nil {
147-
klog.Errorf("failed RemoveSmbGlobalMapping %v", err)
147+
klog.Errorf("failed RemoveSMBGlobalMapping %v", err)
148148
return response, err
149149
}
150150

151-
klog.V(2).Infof("RemoveSmbGlobalMapping on remote path %q is completed", request.RemotePath)
151+
klog.V(2).Infof("RemoveSMBGlobalMapping on remote path %q is completed", request.RemotePath)
152152
return response, nil
153153
}

pkg/smb/smb_test.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ import (
99
smbapi "github.com/kubernetes-csi/csi-proxy/pkg/smb/api"
1010
)
1111

12-
type fakeSmbAPI struct{}
12+
type fakeSMBAPI struct{}
1313

14-
var _ smbapi.API = &fakeSmbAPI{}
14+
var _ smbapi.API = &fakeSMBAPI{}
1515

16-
func (fakeSmbAPI) NewSmbGlobalMapping(remotePath, username, password string) error {
16+
func (fakeSMBAPI) NewSMBGlobalMapping(remotePath, username, password string) error {
1717
return nil
1818
}
1919

20-
func (fakeSmbAPI) RemoveSmbGlobalMapping(remotePath string) error {
20+
func (fakeSMBAPI) RemoveSMBGlobalMapping(remotePath string) error {
2121
return nil
2222
}
2323

24-
func (fakeSmbAPI) IsSmbMapped(remotePath string) (bool, error) {
24+
func (fakeSMBAPI) IsSMBMapped(remotePath string) (bool, error) {
2525
return false, nil
2626
}
2727

28-
func (fakeSmbAPI) NewSmbLink(remotePath, localPath string) error {
28+
func (fakeSMBAPI) NewSMBLink(remotePath, localPath string) error {
2929
return nil
3030
}
3131

@@ -56,7 +56,7 @@ func (fakeFileSystemAPI) IsSymlink(path string) (bool, error) {
5656
return true, nil
5757
}
5858

59-
func TestNewSmbGlobalMapping(t *testing.T) {
59+
func TestNewSMBGlobalMapping(t *testing.T) {
6060
testCases := []struct {
6161
remote string
6262
local string
@@ -82,23 +82,23 @@ func TestNewSmbGlobalMapping(t *testing.T) {
8282
t.Fatalf("FileSystem client could not be initialized for testing: %v", err)
8383
}
8484

85-
client, err := New(&fakeSmbAPI{}, fsClient)
85+
client, err := New(&fakeSMBAPI{}, fsClient)
8686
if err != nil {
87-
t.Fatalf("Smb client could not be initialized for testing: %v", err)
87+
t.Fatalf("SMB client could not be initialized for testing: %v", err)
8888
}
8989
for _, tc := range testCases {
90-
req := &NewSmbGlobalMappingRequest{
90+
req := &NewSMBGlobalMappingRequest{
9191
LocalPath: tc.local,
9292
RemotePath: tc.remote,
9393
Username: tc.username,
9494
Password: tc.password,
9595
}
96-
_, err := client.NewSmbGlobalMapping(context.TODO(), req)
96+
_, err := client.NewSMBGlobalMapping(context.TODO(), req)
9797
if tc.expectError && err == nil {
98-
t.Errorf("Expected error but NewSmbGlobalMapping returned a nil error")
98+
t.Errorf("Expected error but NewSMBGlobalMapping returned a nil error")
9999
}
100100
if !tc.expectError && err != nil {
101-
t.Errorf("Expected no errors but NewSmbGlobalMapping returned error: %v", err)
101+
t.Errorf("Expected no errors but NewSMBGlobalMapping returned error: %v", err)
102102
}
103103
}
104104
}

0 commit comments

Comments
 (0)