forked from opiproject/godpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goopicsi_test.go
145 lines (121 loc) · 3.88 KB
/
goopicsi_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2022 Dell Inc, or its subsidiaries.
package goopicsi
import (
"fmt"
"net"
"os"
"strings"
"testing"
"github.com/opiproject/goopicsi/test/mock-server/server"
"github.com/opiproject/goopicsi/test/mock-server/stub"
pb "github.com/opiproject/opi-api/storage/v1alpha1/gen/go"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
"github.com/stretchr/testify/assert"
)
type GoopcsiTestSuite struct {
suite.Suite
}
func (suite *GoopcsiTestSuite) SetupSuite() {
RunServer()
}
// RunServer launches mock grpc server
func RunServer() {
fmt.Println("RUNNING MOCK SERVER")
const (
csiAddress = "localhost:50051"
defaultStubsPath = "test/mock-server/stubs"
apiPort = "4771"
)
// run admin stub server
stub.RunStubServer(stub.Options{
StubPath: defaultStubsPath,
Port: apiPort,
BindAddr: "0.0.0.0",
})
var protocol string
if strings.Contains(csiAddress, ":") {
protocol = "tcp"
} else {
protocol = "unix"
}
lis, err := net.Listen(protocol, csiAddress)
if err != nil {
fmt.Println(err, "failed to listen on address", "address", csiAddress)
os.Exit(1)
}
MockServer := grpc.NewServer()
pb.RegisterFrontendNvmeServiceServer(MockServer, &server.GoopCSI{})
pb.RegisterNVMfRemoteControllerServiceServer(MockServer, &server.GoopCSI{})
pb.RegisterNullDebugServiceServer(MockServer, &server.GoopCSI{})
fmt.Printf("Serving gRPC on %s\n", csiAddress)
errChan := make(chan error)
// run blocking call in a separate goroutine, report errors via channel
go func() {
if err := MockServer.Serve(lis); err != nil {
errChan <- err
}
}()
}
func (suite *GoopcsiTestSuite) TearDownTestSuite() {
suite.T().Log("Cleaning up resources..")
}
func (suite *GoopcsiTestSuite) TestExposeRemoteNVMe() {
// Negative scenario
subsystemID, controllerID, err := ExposeRemoteNVMe("nqn.2022-09.io.spdk:test", 10)
assert.Error(suite.T(), err)
assert.Empty(suite.T(), subsystemID, "ExposeRemoteNVMe failed")
assert.Empty(suite.T(), controllerID, "ExposeRemoteNVMe failed")
}
func (suite *GoopcsiTestSuite) TestCreateNVMeNamespace() {
// scenario: when volume ID not found
resp, err := CreateNVMeNamespace("1", "nqn", "nguid", 1)
assert.Error(suite.T(), err)
assert.Empty(suite.T(), resp, "CreateNVMeNamespace failed with invalid volume ID")
}
func (suite *GoopcsiTestSuite) TestNVMeControllerDisconnect() {
// scenario: when connection already exists
err := NVMeControllerDisconnect("12")
assert.NoError(suite.T(), err)
}
func (suite *GoopcsiTestSuite) TestNVMeControllerConnect() {
// scenario: when connection already exists
err := NVMeControllerConnect("12", "", "", 44565, "")
assert.NoError(suite.T(), err)
}
func (suite *GoopcsiTestSuite) TestNVMeControllerList() {
resp, err := NVMeControllerList()
assert.NoError(suite.T(), err)
assert.NotNil(suite.T(), resp, "ListControllers success")
}
func (suite *GoopcsiTestSuite) TestNVMeControllerGet() {
// positive scenario
resp, err := NVMeControllerGet("12")
assert.NoError(suite.T(), err)
assert.NotNil(suite.T(), resp, "GetController success")
// negative scenario
resp, err = NVMeControllerGet("invalid")
assert.Error(suite.T(), err, "GetController failed")
assert.Empty(suite.T(), resp, "GetController failed")
}
func (suite *GoopcsiTestSuite) TestDeleteNVMeNamespace() {
// positive scenario
err := DeleteNVMeNamespace("1")
assert.NoError(suite.T(), err, "DeleteNVMeNamespace success")
// negative scenario
err = DeleteNVMeNamespace("invalid")
assert.Error(suite.T(), err, "DeleteNVMeNamespace failed")
}
func (suite *GoopcsiTestSuite) TestGenerateHostNQN() {
hostNQN := GenerateHostNQN()
assert.NotNil(suite.T(), hostNQN, "GenerateHostNQN success")
}
func TestGoopcsiTestSuite(t *testing.T) {
if testing.Short() {
t.Skip("Skipping as requested by short flag")
}
testSuite := new(GoopcsiTestSuite)
suite.Run(t, testSuite)
testSuite.TearDownTestSuite()
}