Skip to content

Commit 9d9225b

Browse files
committed
Fix smb test
This PR fixes a few issues in smb test. 1. error is no longer in part of response. 2. checkerror in integration test
1 parent 2eb1ac1 commit 9d9225b

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

integrationtests/smb_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"math/rand"
88
"os"
99
"os/exec"
10-
"strings"
10+
"strings"
1111
"time"
1212

1313
"testing"
@@ -48,15 +48,15 @@ func setupUser(username, password string) error {
4848
return nil
4949
}
5050

51-
func removeUser(username string) error {
51+
func removeUser(t *testing.T, username string) error {
5252
cmdLine := fmt.Sprintf(`Remove-Localuser -name $Env:username`)
5353
cmd := exec.Command("powershell", "/c", cmdLine)
5454
cmd.Env = append(os.Environ(),
5555
fmt.Sprintf("username=%s", username))
5656
if output, err := cmd.CombinedOutput(); err != nil {
57-
return fmt.Errorf("setupUser failed: %v, output: %q", err, string(output))
57+
t.Fatalf("setupUser failed: %v, output: %q", err, string(output))
5858
}
59-
return nil
59+
return
6060
}
6161

6262
func setupSmbShare(shareName, localPath, username string) error {
@@ -76,15 +76,15 @@ func setupSmbShare(shareName, localPath, username string) error {
7676
return nil
7777
}
7878

79-
func removeSmbShare(shareName string) error {
79+
func removeSmbShare(t *testing.T, shareName string) {
8080
cmdLine := fmt.Sprintf(`Remove-SMBShare -Name $Env:sharename -Force`)
8181
cmd := exec.Command("powershell", "/c", cmdLine)
8282
cmd.Env = append(os.Environ(),
8383
fmt.Sprintf("sharename=%s", shareName))
8484
if output, err := cmd.CombinedOutput(); err != nil {
85-
return fmt.Errorf("setupSmbShare failed: %v, output: %q", err, string(output))
85+
t.Fatalf("setupSmbShare failed: %v, output: %q", err, string(output))
8686
}
87-
return nil
87+
return
8888
}
8989

9090
func getSmbGlobalMapping(remotePath string) error {
@@ -113,19 +113,20 @@ func writeReadFile(path string) error {
113113
}
114114
defer f.Close()
115115
fileContent := "Hello World"
116-
_, err = f.WriteString(fileContent)
117-
f.Sync()
118-
116+
if _, err = f.WriteString(fileContent); err != nil {
117+
return fmt.Errorf("write to file %q failed: %v", fileName, err)
118+
}
119+
if err = f.Sync(); err != nil {
120+
return fmt.Errorf("sync file %q failed: %v", fileName, err)
121+
}
119122
dat, err := ioutil.ReadFile(fileName)
120123
if err != nil {
121124
return fmt.Errorf("read file %q failed: %v", fileName, err)
122125
}
123126
if fileContent != string(dat) {
124127
return fmt.Errorf("read content of file %q failed: expected %q, got %q", fileName, fileContent, string(dat))
125128
}
126-
fmt.Print(string(dat))
127-
128-
return err
129+
return nil
129130
}
130131

131132
func TestSmbAPIGroup(t *testing.T) {
@@ -143,15 +144,15 @@ func TestSmbAPIGroup(t *testing.T) {
143144

144145
localPath := fmt.Sprintf("C:\\localpath%s", randomString(5))
145146

146-
if err := setupUser(username, password); err != nil {
147+
if err = setupUser(username, password); err != nil {
147148
t.Fatalf("TestSmbAPIGroup %v", err)
148149
}
149-
defer removeUser(username)
150+
defer removeUser(t, username)
150151

151-
if err := setupSmbShare(smbShare, sharePath, username); err != nil {
152+
if err = setupSmbShare(smbShare, sharePath, username); err != nil {
152153
t.Fatalf("TestSmbAPIGroup %v", err)
153154
}
154-
defer removeSmbShare(smbShare)
155+
defer removeSmbShare(t, smbShare)
155156

156157
hostname, err := os.Hostname()
157158
assert.Nil(t, err)
@@ -190,7 +191,6 @@ func TestSmbAPIGroup(t *testing.T) {
190191
}
191192
err = getSmbGlobalMapping(remotePath)
192193
assert.NotNil(t, err)
193-
194194
err = writeReadFile(localPath)
195195
assert.NotNil(t, err)
196196

internal/server/smb/server_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ func (fakeSmbAPI) RemoveSmbGlobalMapping(remotePath string) error {
2020

2121
func TestNewSmbGlobalMapping(t *testing.T) {
2222
v1alpha1, err := apiversion.NewVersion("v1alpha1")
23+
if err != nil {
24+
t.Fatalf("New version error: %v", err)
25+
}
2326
testCases := []struct {
2427
remote string
2528
username string
@@ -52,11 +55,11 @@ func TestNewSmbGlobalMapping(t *testing.T) {
5255
Username: tc.username,
5356
Password: tc.password,
5457
}
55-
response, _ := srv.NewSmbGlobalMapping(context.TODO(), req, tc.version)
56-
if tc.expectError && response.Error == "" {
58+
response, err := srv.NewSmbGlobalMapping(context.TODO(), req, tc.version)
59+
if tc.expectError && err == nil {
5760
t.Errorf("Expected error but NewSmbGlobalMapping returned a nil error")
5861
}
59-
if !tc.expectError && response.Error != "" {
62+
if !tc.expectError && err != nil {
6063
t.Errorf("Expected no errors but NewSmbGlobalMapping returned error: %s", response.Error)
6164
}
6265
}

0 commit comments

Comments
 (0)