Skip to content

Commit

Permalink
feat(storage): run tests for specific API partition
Browse files Browse the repository at this point in the history
Signed-off-by: Artsiom Koltun <artsiom.koltun@intel.com>
  • Loading branch information
artek-koltun committed Oct 31, 2023
1 parent 1f74824 commit 6abfc8e
Showing 1 changed file with 133 additions and 55 deletions.
188 changes: 133 additions & 55 deletions cmd/storage-test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,83 +11,161 @@ import (
"time"

"github.com/opiproject/godpu/grpc"

"github.com/opiproject/godpu/storage"
"github.com/spf13/cobra"
)

// NewStorageTestCommand returns the storage tests command
func NewStorageTestCommand() *cobra.Command {
var (
addr string
)
const addrCmdLineArg = "addr"

type storagePartition string

const (
storagePartitionFrontend storagePartition = "frontend"
storagePartitionBackend storagePartition = "backend"
storagePartitionMiddleend storagePartition = "middleend"
)

var allStoragePartitions = []storagePartition{
storagePartitionFrontend,
storagePartitionBackend,
storagePartitionMiddleend,
}

// NewStorageCommand tests the storage functionality
func NewStorageCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "storage",
Aliases: []string{"g"},
Short: "Tests storage functionality",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
err := c.Help()
if err != nil {
log.Fatalf("[ERROR] %s", err.Error())
}
},
}

cmd.AddCommand(newStorageTestCommand())

return cmd
}

func newStorageTestCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "test",
Aliases: []string{"s"},
Short: "Test storage functionality",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
// Set up a connection to the server.
client, err := grpc.New(addr)
if err != nil {
log.Fatalf("error creating new client: %v", err)
}
Run: func(c *cobra.Command, args []string) {
runTests(
c,
allStoragePartitions,
)
},
}

// Contact the server and print out its response.
conn, closer, err := client.NewConn()
if err != nil {
log.Fatalf("error creating gRPC connection: %v", err)
}
defer closer()
cmd.AddCommand(newStorageTestFrontendCommand())
cmd.AddCommand(newStorageTestBackendCommand())
cmd.AddCommand(newStorageTestMiddleendCommand())

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
flags := cmd.PersistentFlags()
flags.String(addrCmdLineArg, "localhost:50151", "address of OPI gRPC server")
return cmd
}

log.Printf("==============================================================================")
log.Printf("Test frontend")
log.Printf("==============================================================================")
err = storage.DoFrontend(ctx, conn)
if err != nil {
log.Panicf("DoFrontend tests failed with error: %v", err)
}
func newStorageTestFrontendCommand() *cobra.Command {
cmd := &cobra.Command{
Use: string(storagePartitionFrontend),
Short: "Tests storage frontend API",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
runTests(
c,
[]storagePartition{storagePartitionFrontend},
)
},
}

log.Printf("==============================================================================")
log.Printf("Test backend")
log.Printf("==============================================================================")
err = storage.DoBackend(ctx, conn)
if err != nil {
log.Panicf("DoBackend tests failed with error: %v", err)
}
return cmd
}

log.Printf("==============================================================================")
log.Printf("Test middleend")
log.Printf("==============================================================================")
err = storage.DoMiddleend(ctx, conn)
if err != nil {
log.Panicf("DoMiddleend tests failed with error: %v", err)
}
func newStorageTestBackendCommand() *cobra.Command {
cmd := &cobra.Command{
Use: string(storagePartitionBackend),
Short: "Tests storage backend API",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
runTests(
c,
[]storagePartition{storagePartitionBackend},
)
},
}
flags := cmd.Flags()
flags.StringVar(&addr, "addr", "localhost:50151", "address or OPI gRPC server")

return cmd
}

// NewStorageCommand tests the storage functionality
func NewStorageCommand() *cobra.Command {
func newStorageTestMiddleendCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "storage",
Aliases: []string{"g"},
Short: "Tests storage functionality",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
err := cmd.Help()
if err != nil {
log.Fatalf("[ERROR] %s", err.Error())
}
Use: string(storagePartitionMiddleend),
Short: "Tests storage middleend API",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
runTests(
c,
[]storagePartition{storagePartitionMiddleend},
)
},
}

cmd.AddCommand(NewStorageTestCommand())
return cmd
}

func runTests(
cmd *cobra.Command,
partitions []storagePartition,
) {
addr, err := cmd.Flags().GetString(addrCmdLineArg)
if err != nil {
log.Fatalf("error getting %v argument: %v", addrCmdLineArg, err)
}

// Set up a connection to the server.
client, err := grpc.New(addr)
if err != nil {
log.Fatalf("error creating new client: %v", err)
}

// Contact the server and print out its response.
conn, closer, err := client.NewConn()
if err != nil {
log.Fatalf("error creating gRPC connection: %v", err)
}
defer closer()

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

for _, partition := range partitions {
log.Printf("==============================================================================")
log.Printf("Test %v", partition)
log.Printf("==============================================================================")

var err error
switch partition {
case storagePartitionFrontend:
err = storage.DoFrontend(ctx, conn)
case storagePartitionBackend:
err = storage.DoBackend(ctx, conn)
case storagePartitionMiddleend:
err = storage.DoMiddleend(ctx, conn)
default:
log.Panicf("Unknown storage partition: %v", partition)
}

if err != nil {
log.Panicf("%v tests failed with error: %v", partition, err)
}
}
}

0 comments on commit 6abfc8e

Please sign in to comment.