Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mock unit test framework #71

Merged
merged 2 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ module github.com/opiproject/goopicsi
go 1.19

require (
github.com/go-chi/chi v1.5.4
github.com/google/uuid v1.3.0
github.com/lithammer/fuzzysearch v1.1.5
github.com/opiproject/opi-api v0.0.0-20221215155400-a49a4ea1d561
github.com/stretchr/testify v1.8.1
golang.org/x/net v0.0.0-20221002022538-bcab6841153b
golang.org/x/text v0.4.0
google.golang.org/grpc v1.51.0
google.golang.org/protobuf v1.28.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.0.0-20221002022538-bcab6841153b // indirect
golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec // indirect
golang.org/x/text v0.4.0 // indirect
google.golang.org/genproto v0.0.0-20220930163606-c98284e70a91 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
14 changes: 6 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi v1.5.4 h1:QHdzF2szwjqVV4wmByUnTcsbIg7UGaQ0tPF2t5GcAIs=
github.com/go-chi/chi v1.5.4/go.mod h1:uaf8YgoFazUOkPBG7fxPftUylNumIev9awIWOENIuEg=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/opiproject/opi-api v0.0.0-20221207164013-fd93b840b575 h1:qUuLJp4bRKxC21El2bg6rhlBLjD/HBta7ALSZLD2pMY=
github.com/opiproject/opi-api v0.0.0-20221207164013-fd93b840b575/go.mod h1:92pv4ulvvPMuxCJ9ND3aYbmBfEMLx0VCjpkiR7ZTqPY=
github.com/opiproject/opi-api v0.0.0-20221208205708-b089acb781cd h1:9jPT8oOKr5VvC2oRhuoH3YStkHCLOjZuLiQBNHLK1bY=
github.com/opiproject/opi-api v0.0.0-20221208205708-b089acb781cd/go.mod h1:92pv4ulvvPMuxCJ9ND3aYbmBfEMLx0VCjpkiR7ZTqPY=
github.com/opiproject/opi-api v0.0.0-20221208224812-65d927304471 h1:W1F/RDX/PHjRanw6aaOtxCGTsPoysGNIhMojKex+kX8=
github.com/opiproject/opi-api v0.0.0-20221208224812-65d927304471/go.mod h1:92pv4ulvvPMuxCJ9ND3aYbmBfEMLx0VCjpkiR7ZTqPY=
github.com/opiproject/opi-api v0.0.0-20221212161256-b1b2672e72be h1:lS16v9/5Vr0+ibwLRp0LJPEq3KWAEFcS81ifVS2BscI=
github.com/opiproject/opi-api v0.0.0-20221212161256-b1b2672e72be/go.mod h1:92pv4ulvvPMuxCJ9ND3aYbmBfEMLx0VCjpkiR7ZTqPY=
github.com/lithammer/fuzzysearch v1.1.5 h1:Ag7aKU08wp0R9QCfF4GoGST9HbmAIeLP7xwMrOBEp1c=
github.com/lithammer/fuzzysearch v1.1.5/go.mod h1:1R1LRNk7yKid1BaQkmuLQaHruxcC4HmAH30Dh61Ih1Q=
github.com/opiproject/opi-api v0.0.0-20221212223617-4ab228a467f7 h1:iQS6+aIBIsWmNfcUmP40rrDrKsP+xgF2Z5nhOP33U/c=
github.com/opiproject/opi-api v0.0.0-20221212223617-4ab228a467f7/go.mod h1:92pv4ulvvPMuxCJ9ND3aYbmBfEMLx0VCjpkiR7ZTqPY=
github.com/opiproject/opi-api v0.0.0-20221215155400-a49a4ea1d561 h1:0v2Lmm5zPK6Doz3cn9QCqX8vAfLw72Hvwcx1cqam6bM=
Expand All @@ -33,8 +29,10 @@ golang.org/x/net v0.0.0-20221002022538-bcab6841153b h1:6e93nYa3hNqAvLr0pD4PN1fFS
golang.org/x/net v0.0.0-20221002022538-bcab6841153b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec h1:BkDtF2Ih9xZ7le9ndzTA7KJow28VbQW3odyk/8drmuI=
golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
google.golang.org/genproto v0.0.0-20220930163606-c98284e70a91 h1:Ezh2cpcnP5Rq60sLensUsFnxh7P6513NLvNtCm9iyJ4=
Expand Down
157 changes: 117 additions & 40 deletions goopicsi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,145 @@
package goopicsi

import (
"fmt"
"log"
"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"
)

func TestNVMeControllerConnect(t *testing.T) {
err := NVMeControllerConnect("12", "", "", 44565, "")
if err != nil {
log.Println(err)
}
assert.Error(t, err)
type GoopcsiTestSuite struct {
suite.Suite
}

func TestNVMeControllerList(t *testing.T) {
resp, err := NVMeControllerList()
if err != nil {
log.Println(err)
}
log.Printf("NVMf Remote Connections: %v", resp)
func (suite *GoopcsiTestSuite) SetupSuite() {
RunServer()
}

func TestNVMeControllerGet(t *testing.T) {
resp, err := NVMeControllerGet("12")
if err != nil {
log.Println(err)
}
log.Printf("NVMf remote connection corresponding to the ID: %v", resp)
}
// RunServer launches mock grpc server
func RunServer() {
fmt.Println("RUNNING MOCK SERVER")
const (
csiAddress = "localhost:50051"
defaultStubsPath = "test/mock-server/stubs"
apiPort = "4771"
)

func TestNVMeControllerDisconnect(t *testing.T) {
err := NVMeControllerDisconnect("12")
// 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 {
log.Println(err)
fmt.Println(err, "failed to listen on address", "address", csiAddress)
os.Exit(1)
}

MockServer := grpc.NewServer()

pb.RegisterFrontendNvmeServiceServer(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 TestCreateNVMeNamespace(t *testing.T) {
resp, err := CreateNVMeNamespace("1", "nqn", "nguid", 1)
if err != nil {
log.Println(err)
}
log.Println(resp)
func (suite *GoopcsiTestSuite) TearDownTestSuite() {
suite.T().Log("Cleaning up resources..")
}

func TestDeleteNVMeNamespace(t *testing.T) {
// TODO These test cases should be reverted with mock server implementation
// func TestNVMeControllerConnect(t *testing.T) {
// err := NVMeControllerConnect("12", "", "", 44565, "")
// if err != nil {
// log.Println(err)
// }
// assert.Error(t, err)
// }

// func TestNVMeControllerList(t *testing.T) {
// resp, err := NVMeControllerList()
// if err != nil {
// log.Println(err)
// }
// log.Printf("NVMf Remote Connections: %v", resp)
// }

// func TestNVMeControllerGet(t *testing.T) {
// resp, err := NVMeControllerGet("12")
// if err != nil {
// log.Println(err)
// }
// log.Printf("NVMf remote connection corresponding to the ID: %v", resp)
// }

// func TestNVMeControllerDisconnect(t *testing.T) {
// err := NVMeControllerDisconnect("12")
// if err != nil {
// log.Println(err)
// }
// }

// func TestCreateNVMeNamespace(t *testing.T) {
// resp, err := CreateNVMeNamespace("1", "nqn", "nguid", 1)
// if err != nil {
// log.Println(err)
// }
// log.Println(resp)
// }

// func TestExposeRemoteNVMe(t *testing.T) {
// subsystemID, controllerID, err := ExposeRemoteNVMe("nqn.2022-09.io.spdk:test", 10)
// if err != nil {
// log.Println(err)
// }
// log.Printf("Subsystem ID: %s", subsystemID)
// log.Printf("Controller Id: %s", controllerID)
// }

func (suite *GoopcsiTestSuite) TestDeleteNVMeNamespace() {
// positive scenario
err := DeleteNVMeNamespace("1")
if err != nil {
log.Println(err)
}
}
assert.NoError(suite.T(), err, "DeleteNVMeNamespace success")

func TestExposeRemoteNVMe(t *testing.T) {
subsystemID, controllerID, err := ExposeRemoteNVMe("nqn.2022-09.io.spdk:test", 10)
if err != nil {
log.Println(err)
}
log.Printf("Subsystem ID: %s", subsystemID)
log.Printf("Controller Id: %s", controllerID)
// negative scenario
err = DeleteNVMeNamespace("invalid")
assert.Error(suite.T(), err, "DeleteNVMeNamespace failed")
}

func TestGenerateHostNQN(t *testing.T) {
hostNQN := GenerateHostNQN()
log.Println(hostNQN)
}

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()
}
Loading