Skip to content

Commit d02fc2e

Browse files
committed
Add pkg/mounter tests
1 parent eb1997d commit d02fc2e

File tree

7 files changed

+437
-4
lines changed

7 files changed

+437
-4
lines changed

pkg/mounter/interceptors/alinas_secret.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import (
1010
"k8s.io/klog/v2"
1111
)
1212

13+
var credDir = os.TempDir()
1314
var _ mounter.MountInterceptor = AlinasSecretInterceptor
1415

1516
func AlinasSecretInterceptor(ctx context.Context, op *mounter.MountOperation, handler mounter.MountHandler) error {
1617
if op == nil || op.Secrets == nil {
1718
return handler(ctx, op)
1819
}
1920

20-
tmpCredFile, err := os.CreateTemp("", op.VolumeID+"-*.credentials")
21+
tmpCredFile, err := os.CreateTemp(credDir, op.VolumeID+"-*.credentials")
2122
if err != nil {
2223
return err
2324
}
@@ -35,7 +36,7 @@ func AlinasSecretInterceptor(ctx context.Context, op *mounter.MountOperation, ha
3536
return err
3637
}
3738

38-
credFilePath := path.Join(os.TempDir(), op.VolumeID+".credentials")
39+
credFilePath := path.Join(credDir, op.VolumeID+".credentials")
3940
if err = os.Rename(tmpCredFile.Name(), credFilePath); err != nil {
4041
return err
4142
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package interceptors
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"path"
8+
"sync"
9+
"testing"
10+
"time"
11+
12+
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
var (
17+
successMountHandler = func(context.Context, *mounter.MountOperation) error {
18+
return nil
19+
}
20+
failureMountHandler = func(context.Context, *mounter.MountOperation) error {
21+
return fmt.Errorf("failed")
22+
}
23+
)
24+
25+
func TestAlinasSecretInterceptor(t *testing.T) {
26+
credDir = t.TempDir()
27+
print(credDir)
28+
tests := []struct {
29+
name string
30+
concurrent bool
31+
handler mounter.MountHandler
32+
op *mounter.MountOperation
33+
expectErr bool
34+
}{
35+
{
36+
name: "nil operation",
37+
handler: successMountHandler,
38+
},
39+
{
40+
name: "nil secrets",
41+
handler: successMountHandler,
42+
op: &mounter.MountOperation{},
43+
},
44+
{
45+
name: "mount error reservation",
46+
handler: failureMountHandler,
47+
op: &mounter.MountOperation{},
48+
expectErr: true,
49+
},
50+
{
51+
name: "normal operation",
52+
handler: successMountHandler,
53+
op: &mounter.MountOperation{
54+
Secrets: map[string]string{
55+
"akId": "akid",
56+
"akSecret": "aksecret",
57+
},
58+
VolumeID: "volume-id",
59+
},
60+
},
61+
{
62+
name: "concurrent operations",
63+
concurrent: true,
64+
handler: successMountHandler,
65+
op: &mounter.MountOperation{
66+
Secrets: map[string]string{
67+
"akId": "akid",
68+
"akSecret": "aksecret",
69+
},
70+
VolumeID: "volume-id",
71+
},
72+
},
73+
}
74+
75+
for _, tt := range tests {
76+
t.Run(tt.name, func(t *testing.T) {
77+
var errs []error
78+
var mutex sync.Mutex
79+
80+
if !tt.concurrent {
81+
if err := AlinasSecretInterceptor(context.Background(), tt.op, tt.handler); err != nil {
82+
errs = append(errs, err)
83+
}
84+
} else {
85+
var wg sync.WaitGroup
86+
for range 10 {
87+
wg.Add(1)
88+
go func() {
89+
defer wg.Done()
90+
time.Sleep(time.Millisecond)
91+
err := AlinasSecretInterceptor(context.Background(), tt.op, tt.handler)
92+
mutex.Lock()
93+
if err != nil {
94+
errs = append(errs, err)
95+
}
96+
mutex.Unlock()
97+
}()
98+
}
99+
wg.Wait()
100+
}
101+
102+
if tt.expectErr {
103+
assert.NotEmpty(t, errs)
104+
return
105+
}
106+
107+
assert.Nil(t, errs)
108+
if tt.op == nil || tt.op.Secrets == nil {
109+
return
110+
}
111+
112+
content, err := os.ReadFile(path.Join(credDir, tt.op.VolumeID+".credentials"))
113+
assert.NoError(t, err)
114+
assert.Equal(
115+
t,
116+
fmt.Sprintf("[NASCredentials]\naccessKeyID=%s\naccessKeySecret=%s", tt.op.Secrets["akId"], tt.op.Secrets["akSecret"]),
117+
string(content))
118+
})
119+
}
120+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package interceptors
2+
3+
import (
4+
"context"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
9+
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter"
10+
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter/proxy/server"
11+
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/utils"
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func TestOssfsMonitorInterceptor(t *testing.T) {
16+
metricsDir := t.TempDir()
17+
tests := []struct {
18+
name string
19+
handler mounter.MountHandler
20+
op *mounter.MountOperation
21+
expectErr bool
22+
}{
23+
{
24+
name: "nil operation",
25+
handler: successMountHandler,
26+
},
27+
{
28+
name: "nil metrics path",
29+
handler: successMountHandler,
30+
op: &mounter.MountOperation{},
31+
},
32+
{
33+
name: "mount error reservation",
34+
handler: failureMountHandler,
35+
op: &mounter.MountOperation{
36+
MetricsPath: metricsDir,
37+
},
38+
expectErr: true,
39+
},
40+
{
41+
name: "nil mount result",
42+
handler: successMountHandler,
43+
op: &mounter.MountOperation{
44+
MetricsPath: metricsDir,
45+
Target: "target1",
46+
},
47+
},
48+
{
49+
name: "invalid mount result",
50+
handler: successMountHandler,
51+
op: &mounter.MountOperation{
52+
MetricsPath: metricsDir,
53+
MountResult: "invalid",
54+
Target: "target2",
55+
},
56+
},
57+
}
58+
59+
for _, tt := range tests {
60+
t.Run(tt.name, func(t *testing.T) {
61+
err := OssfsMonitorInterceptor(context.Background(), tt.op, tt.handler)
62+
if tt.expectErr {
63+
assert.Error(t, err)
64+
return
65+
}
66+
assert.NoError(t, err)
67+
if tt.op == nil || tt.op.MetricsPath == "" {
68+
return
69+
}
70+
71+
monitor, found := monitorManager.GetMountMonitor(tt.op.Target, tt.op.MetricsPath, raw, false)
72+
assert.True(t, found)
73+
assert.NotNil(t, monitor)
74+
75+
})
76+
}
77+
78+
monitorManager.StopAllMonitoring()
79+
monitorManager.WaitForAllMonitoring()
80+
monitorManager = server.NewMountMonitorManager()
81+
defer func() {
82+
monitorManager.StopAllMonitoring()
83+
monitorManager.WaitForAllMonitoring()
84+
}()
85+
86+
op := &mounter.MountOperation{
87+
Target: "volume1",
88+
MetricsPath: metricsDir,
89+
MountResult: server.OssfsMountResult{
90+
PID: 123,
91+
ExitChan: make(chan error),
92+
},
93+
}
94+
err := OssfsMonitorInterceptor(context.Background(), op, successMountHandler)
95+
assert.NoError(t, err)
96+
monitor, found := monitorManager.GetMountMonitor(op.Target, op.MetricsPath, raw, false)
97+
assert.True(t, found)
98+
assert.NotNil(t, monitor)
99+
assertMountMetricValue(t, op.MetricsPath, utils.MetricsMountPointStatus, "0")
100+
assertMountMetricValue(t, op.MetricsPath, utils.MetricsMountRetryCount, "0")
101+
102+
err = OssfsMonitorInterceptor(context.Background(), op, failureMountHandler)
103+
assert.Error(t, err)
104+
assertMountMetricValue(t, op.MetricsPath, utils.MetricsMountPointStatus, "1")
105+
assertMountMetricValue(t, op.MetricsPath, utils.MetricsMountRetryCount, "1")
106+
}
107+
108+
func assertMountMetricValue(t *testing.T, metricsDir, metricsFile string, expected string) {
109+
actual, err := os.ReadFile(filepath.Join(metricsDir, metricsFile))
110+
assert.NoError(t, err)
111+
assert.Equal(t, expected, string(actual))
112+
}

pkg/mounter/interceptors/ossfs_secret.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ func Ossfs2SecretInterceptor(ctx context.Context, op *mounter.MountOperation, ha
2222
}
2323

2424
func ossfsSecretInterceptor(ctx context.Context, op *mounter.MountOperation, handler mounter.MountHandler, fuseType string) error {
25+
if op == nil || op.Secrets == nil {
26+
return handler(ctx, op)
27+
}
28+
2529
passwdFile, err := utils.SaveOssSecretsToFile(op.Secrets, op.FsType)
2630
if err != nil {
2731
return err
@@ -41,7 +45,7 @@ func ossfsSecretInterceptor(ctx context.Context, op *mounter.MountOperation, han
4145
if passwdFile == "" || op.MountResult == nil {
4246
return nil
4347
}
44-
result, ok := op.MountResult.(*server.OssfsMountResult)
48+
result, ok := op.MountResult.(server.OssfsMountResult)
4549
if !ok {
4650
klog.ErrorS(
4751
errors.New("failed to assert ossfs mount result"),

0 commit comments

Comments
 (0)