Skip to content

Commit

Permalink
feat(CSI-308): add tests for NodeServer.ensurePublishingIsAllowed()
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyberezansky committed Nov 26, 2024
1 parent a5f88c2 commit 6387fcc
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions pkg/wekafs/nodeserver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package wekafs

import (
"context"
"github.com/wekafs/csi-wekafs/pkg/wekafs/db"
"golang.org/x/sync/semaphore"
"google.golang.org/grpc/codes"
"os"

"testing"
"time"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)

func setupTestNodeServer() *NodeServer {
// make sure to clean up the database
_ = os.Remove(db.DBPath)

database, _ := db.GetDatabase(context.Background())
return &NodeServer{
nodeID: "test-node",
config: &DriverConfig{
grpcRequestTimeout: 60 * time.Second,
maxConcurrencyPerOp: map[string]int64{
"NodePublishVolume": 10,
},
debugPath: "/tmp/wekafs",
},
semaphores: make(map[string]*semaphore.Weighted),
database: database,
}
}

func TestNodeServer_ensurePublishingIsAllowed(t *testing.T) {
ns := setupTestNodeServer()
ctx := context.Background()

volumeID := uuid.New().String()
targetPath := "/test/path"
node := "test-node"
accessMode := csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER

req := &csi.NodePublishVolumeRequest{
VolumeId: volumeID,
VolumeCapability: &csi.VolumeCapability{
AccessMode: &csi.VolumeCapability_AccessMode{
Mode: accessMode,
},
},
VolumeContext: map[string]string{
"node": node,
"pod": "test-pod",
},
TargetPath: targetPath,
}

// trivial, new attachment and nothing else
resp, err := ns.ensurePublishingIsAllowed(ctx, req)
assert.NoError(t, err)
assert.Nil(t, resp)

// duplicate attachment, same request again but after already locking, should succeed
ns.SetPvcLock(ctx, volumeID, targetPath, node, accessMode.String())

resp, err = ns.ensurePublishingIsAllowed(ctx, req)
assert.NoError(t, err)
assert.Nil(t, resp)

// duplicate request of same attachment with different access type, should fail
req.VolumeCapability.AccessMode.Mode = csi.VolumeCapability_AccessMode_MULTI_NODE_READER_ONLY

resp, err = ns.ensurePublishingIsAllowed(ctx, req)
_, expectedErr := NodePublishVolumeError(ctx, codes.FailedPrecondition, "Volume already published with a different access type")
assert.Equal(t, expectedErr, err)

// duplicate request of same attachment with different target path, should fail due to SINGLE_NODE_SINGLE_WRITER
req.VolumeCapability.AccessMode.Mode = csi.VolumeCapability_AccessMode_SINGLE_NODE_SINGLE_WRITER
req.TargetPath = "/test/path2"
resp, err = NodePublishVolumeError(ctx, codes.FailedPrecondition, "Volume already published with a different target path")
_, expectedErr = ns.ensurePublishingIsAllowed(ctx, req)
assert.Error(t, err)

}

0 comments on commit 6387fcc

Please sign in to comment.