Skip to content

Commit

Permalink
feat: add context in tests as well
Browse files Browse the repository at this point in the history
Signed-off-by: Boris Glimcher <[email protected]>
  • Loading branch information
glimchb committed Oct 6, 2023
1 parent 8edbef6 commit 15e8f28
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 50 deletions.
7 changes: 5 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,28 @@
package main

import (
"context"
"flag"
"log"

"github.com/opiproject/gospdk/spdk"
)

func main() {
ctx := context.Background()

var spdkAddress string
flag.StringVar(&spdkAddress, "spdk_addr", "/var/tmp/spdk.sock", "Points to SPDK unix socket/tcp socket to interact with")
flag.Parse()

// use like this:
jsonRPC := spdk.NewSpdkJSONRPC(spdkAddress)
version := jsonRPC.GetVersion()
version := jsonRPC.GetVersion(ctx)
log.Printf("Received from SPDK: %v", version)

// or like this:
var ver spdk.GetVersionResult
err := jsonRPC.Call("spdk_get_version", nil, &ver)
err := jsonRPC.Call(ctx, "spdk_get_version", nil, &ver)
if err != nil {
log.Fatalf("failed to get SPDK version: %v", err)
}
Expand Down
12 changes: 8 additions & 4 deletions spdk/accel.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
// Package spdk implements the spdk json-rpc protocol
package spdk

import (
"context"
)

type AccelService interface {

Check warning on line 11 in spdk/accel.service.go

View workflow job for this annotation

GitHub Actions / call / golangci

exported: exported type AccelService should have comment or be unexported (revive)
CryptoKeyCreate(name string, cipher string, key []byte) (*AccelCryptoKeyCreateResult, error)
CryptoKeyDestroy(*AccelCryptoKeyDestroyParams) (*AccelCryptoKeyDestroyResult, error)
CryptoKeyList(*AccelCryptoKeyGetParams) (*AccelCryptoKeyGetResult, error)
GetStats(*NvmfCreateSubsystemParams) (*NvmfCreateSubsystemResult, error)
CryptoKeyCreate(ctx context.Context, name string, cipher string, key []byte) (*AccelCryptoKeyCreateResult, error)
CryptoKeyDestroy(context.Context, *AccelCryptoKeyDestroyParams) (*AccelCryptoKeyDestroyResult, error)
CryptoKeyList(context.Context, *AccelCryptoKeyGetParams) (*AccelCryptoKeyGetResult, error)
GetStats(context.Context, *NvmfCreateSubsystemParams) (*NvmfCreateSubsystemResult, error)
}
15 changes: 7 additions & 8 deletions spdk/accel.service.impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ import (
)

type AccelServiceImpl struct {

Check warning on line 14 in spdk/accel.service.impl.go

View workflow job for this annotation

GitHub Actions / call / golangci

exported: exported type AccelServiceImpl should have comment or be unexported (revive)
ctx context.Context
client JSONRPC
}

func NewAccelService(ctx context.Context) AccelService {
func NewAccelService() AccelService {

Check warning on line 18 in spdk/accel.service.impl.go

View workflow job for this annotation

GitHub Actions / call / golangci

exported: exported function NewAccelService should have comment or be unexported (revive)

Check warning on line 18 in spdk/accel.service.impl.go

View check run for this annotation

Codecov / codecov/patch

spdk/accel.service.impl.go#L18

Added line #L18 was not covered by tests
// client := spdk.NewSpdkJSONRPC(spdkAddress)
return &AccelServiceImpl{ctx, nil}
return &AccelServiceImpl{nil}

Check warning on line 20 in spdk/accel.service.impl.go

View check run for this annotation

Codecov / codecov/patch

spdk/accel.service.impl.go#L20

Added line #L20 was not covered by tests
}

func (p *AccelServiceImpl) CryptoKeyCreate(name string, cipher string, key []byte) (*AccelCryptoKeyCreateResult, error) {
func (p *AccelServiceImpl) CryptoKeyCreate(ctx context.Context, name string, cipher string, key []byte) (*AccelCryptoKeyCreateResult, error) {

Check warning on line 23 in spdk/accel.service.impl.go

View workflow job for this annotation

GitHub Actions / call / golangci

exported: exported method AccelServiceImpl.CryptoKeyCreate should have comment or be unexported (revive)

Check warning on line 23 in spdk/accel.service.impl.go

View check run for this annotation

Codecov / codecov/patch

spdk/accel.service.impl.go#L23

Added line #L23 was not covered by tests
// TBD
keyHalf := len(key) / 2
params := AccelCryptoKeyCreateParams{
Expand All @@ -31,7 +30,7 @@ func (p *AccelServiceImpl) CryptoKeyCreate(name string, cipher string, key []byt
Name: name,
}
var result AccelCryptoKeyCreateResult
err := p.client.Call("accel_crypto_key_create", &params, &result)
err := p.client.Call(ctx, "accel_crypto_key_create", &params, &result)

Check warning on line 33 in spdk/accel.service.impl.go

View check run for this annotation

Codecov / codecov/patch

spdk/accel.service.impl.go#L33

Added line #L33 was not covered by tests
if err != nil {
log.Printf("error: %v", err)
return nil, err
Expand All @@ -45,17 +44,17 @@ func (p *AccelServiceImpl) CryptoKeyCreate(name string, cipher string, key []byt
return nil, nil
}

func (p *AccelServiceImpl) CryptoKeyDestroy(params *AccelCryptoKeyDestroyParams) (*AccelCryptoKeyDestroyResult, error) {
func (p *AccelServiceImpl) CryptoKeyDestroy(_ context.Context, params *AccelCryptoKeyDestroyParams) (*AccelCryptoKeyDestroyResult, error) {

Check warning on line 47 in spdk/accel.service.impl.go

View workflow job for this annotation

GitHub Actions / call / golangci

unused-parameter: parameter 'params' seems to be unused, consider removing or renaming it as _ (revive)

Check warning on line 47 in spdk/accel.service.impl.go

View check run for this annotation

Codecov / codecov/patch

spdk/accel.service.impl.go#L47

Added line #L47 was not covered by tests
// TBD
return nil, nil
}

func (p *AccelServiceImpl) CryptoKeyList(params *AccelCryptoKeyGetParams) (*AccelCryptoKeyGetResult, error) {
func (p *AccelServiceImpl) CryptoKeyList(_ context.Context, params *AccelCryptoKeyGetParams) (*AccelCryptoKeyGetResult, error) {

Check warning on line 52 in spdk/accel.service.impl.go

View workflow job for this annotation

GitHub Actions / call / golangci

unused-parameter: parameter 'params' seems to be unused, consider removing or renaming it as _ (revive)

Check warning on line 52 in spdk/accel.service.impl.go

View check run for this annotation

Codecov / codecov/patch

spdk/accel.service.impl.go#L52

Added line #L52 was not covered by tests
// TBD
return nil, nil
}

func (p *AccelServiceImpl) GetStats(params *NvmfCreateSubsystemParams) (*NvmfCreateSubsystemResult, error) {
func (p *AccelServiceImpl) GetStats(_ context.Context, params *NvmfCreateSubsystemParams) (*NvmfCreateSubsystemResult, error) {

Check warning on line 57 in spdk/accel.service.impl.go

View workflow job for this annotation

GitHub Actions / call / golangci

unused-parameter: parameter 'params' seems to be unused, consider removing or renaming it as _ (revive)

Check warning on line 57 in spdk/accel.service.impl.go

View check run for this annotation

Codecov / codecov/patch

spdk/accel.service.impl.go#L57

Added line #L57 was not covered by tests
// TBD
return nil, nil
}
14 changes: 9 additions & 5 deletions spdk/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package spdk

import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
Expand All @@ -29,9 +30,9 @@ var (
// JSONRPC represents an interface to execute JSON RPC to SPDK
type JSONRPC interface {
GetID() uint64
GetVersion() string
GetVersion(context.Context) string
StartUnixListener() net.Listener
Call(method string, args, result interface{}) error
Call(ctx context.Context, method string, args, result interface{}) error
}

type SpdkJSONRPC struct {

Check warning on line 38 in spdk/jsonrpc.go

View workflow job for this annotation

GitHub Actions / call / golangci

exported: exported type SpdkJSONRPC should have comment or be unexported (revive)
Expand All @@ -40,6 +41,9 @@ type SpdkJSONRPC struct {
id uint64
}

// build time check that struct implements interface
var _ JSONRPC = (*SpdkJSONRPC)(nil)

// NewSpdkJSONRPC creates a new instance of JSONRPC which is capable to
// interact with either unix domain socket, e.g.: /var/tmp/spdk.sock
// or with tcp connection ip and port tuple, e.g.: 10.1.1.2:1234
Expand All @@ -65,9 +69,9 @@ func (r *SpdkJSONRPC) GetID() uint64 {
}

// GetVersion implements low level rpc request/response handling
func (r *SpdkJSONRPC) GetVersion() string {
func (r *SpdkJSONRPC) GetVersion(ctx context.Context) string {

Check warning on line 72 in spdk/jsonrpc.go

View check run for this annotation

Codecov / codecov/patch

spdk/jsonrpc.go#L72

Added line #L72 was not covered by tests
var ver GetVersionResult
err := r.Call("spdk_get_version", nil, &ver)
err := r.Call(ctx, "spdk_get_version", nil, &ver)

Check warning on line 74 in spdk/jsonrpc.go

View check run for this annotation

Codecov / codecov/patch

spdk/jsonrpc.go#L74

Added line #L74 was not covered by tests
if err != nil {
msg := fmt.Sprintf("Could not get spdk version: %v", err)
log.Print(msg)
Expand All @@ -90,7 +94,7 @@ func (r *SpdkJSONRPC) StartUnixListener() net.Listener {
}

// Call implements low level rpc request/response handling
func (r *SpdkJSONRPC) Call(method string, args, result interface{}) error {
func (r *SpdkJSONRPC) Call(_ context.Context, method string, args, result interface{}) error {

Check warning on line 97 in spdk/jsonrpc.go

View check run for this annotation

Codecov / codecov/patch

spdk/jsonrpc.go#L97

Added line #L97 was not covered by tests
id := atomic.AddUint64(&r.id, 1)
request := RPCRequest{
RPCVersion: JSONRPCVersion,
Expand Down
26 changes: 15 additions & 11 deletions spdk/lvol.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
// Package spdk implements the spdk json-rpc protocol
package spdk

import (
"context"
)

type LvolService interface {

Check warning on line 11 in spdk/lvol.service.go

View workflow job for this annotation

GitHub Actions / call / golangci

exported: exported type LvolService should have comment or be unexported (revive)
CreateLvstore(*NvmfCreateSubsystemParams) (*NvmfCreateSubsystemResult, error)
DeleteLvstore(*NvmfDeleteSubsystemParams) (*NvmfDeleteSubsystemResult, error)
GetLvstores(page int, limit int) (*NvmfGetSubsystemsResult, error)
RenameLvstore(*NvmfDeleteSubsystemParams) (*NvmfDeleteSubsystemResult, error)
GrowLvstore(*NvmfDeleteSubsystemParams) (*NvmfDeleteSubsystemResult, error)
CreateLvstore(context.Context, *NvmfCreateSubsystemParams) (*NvmfCreateSubsystemResult, error)
DeleteLvstore(context.Context, *NvmfDeleteSubsystemParams) (*NvmfDeleteSubsystemResult, error)
GetLvstores(ctx context.Context, page int, limit int) (*NvmfGetSubsystemsResult, error)
RenameLvstore(context.Context, *NvmfDeleteSubsystemParams) (*NvmfDeleteSubsystemResult, error)
GrowLvstore(context.Context, *NvmfDeleteSubsystemParams) (*NvmfDeleteSubsystemResult, error)

CreateLvol(*NvmfCreateSubsystemParams) (*NvmfCreateSubsystemResult, error)
SnapshotLvol(*NvmfDeleteSubsystemParams) (*NvmfDeleteSubsystemResult, error)
CloneLvol(*NvmfDeleteSubsystemParams) (*NvmfDeleteSubsystemResult, error)
RenameLvol(*NvmfDeleteSubsystemParams) (*NvmfDeleteSubsystemResult, error)
ResizeLvol(*NvmfDeleteSubsystemParams) (*NvmfDeleteSubsystemResult, error)
DeleteLvol(*NvmfDeleteSubsystemParams) (*NvmfDeleteSubsystemResult, error)
CreateLvol(context.Context, *NvmfCreateSubsystemParams) (*NvmfCreateSubsystemResult, error)
SnapshotLvol(context.Context, *NvmfDeleteSubsystemParams) (*NvmfDeleteSubsystemResult, error)
CloneLvol(context.Context, *NvmfDeleteSubsystemParams) (*NvmfDeleteSubsystemResult, error)
RenameLvol(context.Context, *NvmfDeleteSubsystemParams) (*NvmfDeleteSubsystemResult, error)
ResizeLvol(context.Context, *NvmfDeleteSubsystemParams) (*NvmfDeleteSubsystemResult, error)
DeleteLvol(context.Context, *NvmfDeleteSubsystemParams) (*NvmfDeleteSubsystemResult, error)
}
20 changes: 12 additions & 8 deletions spdk/nvmf.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
// Package spdk implements the spdk json-rpc protocol
package spdk

import (
"context"
)

type NvmfService interface {

Check warning on line 11 in spdk/nvmf.service.go

View workflow job for this annotation

GitHub Actions / call / golangci

exported: exported type NvmfService should have comment or be unexported (revive)
CreateSubsystem(nqn string, serial string, model string, ns int) (*NvmfCreateSubsystemResult, error)
DeleteSubsystem(*NvmfDeleteSubsystemParams) (*NvmfDeleteSubsystemResult, error)
GetSubsystems(page int, limit int) (*NvmfGetSubsystemsResult, error)
GetStats(page int, limit int) (*NvmfGetSubsystemStatsResult, error)
AddListener(*NvmfSubsystemAddListenerParams) (*NvmfSubsystemAddListenerResult, error)
RemoveListener(*NvmfSubsystemAddListenerParams) (*NvmfSubsystemAddListenerResult, error)
AddNamespace(*NvmfSubsystemAddNsParams) (*NvmfSubsystemAddNsResult, error)
RemoveNamespace(*NvmfSubsystemRemoveNsParams) (*NvmfSubsystemRemoveNsResult, error)
CreateSubsystem(ctx context.Context, nqn string, serial string, model string, ns int) (*NvmfCreateSubsystemResult, error)
DeleteSubsystem(context.Context, *NvmfDeleteSubsystemParams) (*NvmfDeleteSubsystemResult, error)
GetSubsystems(ctx context.Context, page int, limit int) (*NvmfGetSubsystemsResult, error)
GetStats(ctx context.Context, page int, limit int) (*NvmfGetSubsystemStatsResult, error)
AddListener(context.Context, *NvmfSubsystemAddListenerParams) (*NvmfSubsystemAddListenerResult, error)
RemoveListener(context.Context, *NvmfSubsystemAddListenerParams) (*NvmfSubsystemAddListenerResult, error)
AddNamespace(context.Context, *NvmfSubsystemAddNsParams) (*NvmfSubsystemAddNsResult, error)
RemoveNamespace(context.Context, *NvmfSubsystemRemoveNsParams) (*NvmfSubsystemRemoveNsResult, error)
}
23 changes: 11 additions & 12 deletions spdk/nvmf.service.impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ import (
)

type NvmfServiceImpl struct {
ctx context.Context
client JSONRPC
}

func NewNvmfService(ctx context.Context) NvmfService {
func NewNvmfService() NvmfService {

Check warning on line 17 in spdk/nvmf.service.impl.go

View check run for this annotation

Codecov / codecov/patch

spdk/nvmf.service.impl.go#L17

Added line #L17 was not covered by tests
// client := spdk.NewSpdkJSONRPC(spdkAddress)
return &NvmfServiceImpl{ctx, nil}
return &NvmfServiceImpl{nil}

Check warning on line 19 in spdk/nvmf.service.impl.go

View check run for this annotation

Codecov / codecov/patch

spdk/nvmf.service.impl.go#L19

Added line #L19 was not covered by tests
}

func (p *NvmfServiceImpl) CreateSubsystem(nqn string, serial string, model string, ns int) (*NvmfCreateSubsystemResult, error) {
func (p *NvmfServiceImpl) CreateSubsystem(ctx context.Context, nqn string, serial string, model string, ns int) (*NvmfCreateSubsystemResult, error) {

Check warning on line 22 in spdk/nvmf.service.impl.go

View check run for this annotation

Codecov / codecov/patch

spdk/nvmf.service.impl.go#L22

Added line #L22 was not covered by tests
// TBD
params := NvmfCreateSubsystemParams{
Nqn: nqn,
Expand All @@ -30,7 +29,7 @@ func (p *NvmfServiceImpl) CreateSubsystem(nqn string, serial string, model strin
MaxNamespaces: ns,
}
var result NvmfCreateSubsystemResult
err := p.client.Call("nvmf_create_subsystem", &params, &result)
err := p.client.Call(ctx, "nvmf_create_subsystem", &params, &result)

Check warning on line 32 in spdk/nvmf.service.impl.go

View check run for this annotation

Codecov / codecov/patch

spdk/nvmf.service.impl.go#L32

Added line #L32 was not covered by tests
if err != nil {
log.Printf("error: %v", err)
return nil, err
Expand All @@ -44,37 +43,37 @@ func (p *NvmfServiceImpl) CreateSubsystem(nqn string, serial string, model strin
return nil, nil
}

func (p *NvmfServiceImpl) DeleteSubsystem(*NvmfDeleteSubsystemParams) (*NvmfDeleteSubsystemResult, error) {
func (p *NvmfServiceImpl) DeleteSubsystem(context.Context, *NvmfDeleteSubsystemParams) (*NvmfDeleteSubsystemResult, error) {

Check warning on line 46 in spdk/nvmf.service.impl.go

View check run for this annotation

Codecov / codecov/patch

spdk/nvmf.service.impl.go#L46

Added line #L46 was not covered by tests
// TBD
return nil, nil
}

func (p *NvmfServiceImpl) GetSubsystems(page int, limit int) (*NvmfGetSubsystemsResult, error) {
func (p *NvmfServiceImpl) GetSubsystems(_ context.Context, page int, limit int) (*NvmfGetSubsystemsResult, error) {

Check warning on line 51 in spdk/nvmf.service.impl.go

View check run for this annotation

Codecov / codecov/patch

spdk/nvmf.service.impl.go#L51

Added line #L51 was not covered by tests
// TBD
return nil, nil
}

func (p *NvmfServiceImpl) GetStats(page int, limit int) (*NvmfGetSubsystemStatsResult, error) {
func (p *NvmfServiceImpl) GetStats(_ context.Context, page int, limit int) (*NvmfGetSubsystemStatsResult, error) {

Check warning on line 56 in spdk/nvmf.service.impl.go

View check run for this annotation

Codecov / codecov/patch

spdk/nvmf.service.impl.go#L56

Added line #L56 was not covered by tests
// TBD
return nil, nil
}

func (p *NvmfServiceImpl) AddListener(*NvmfSubsystemAddListenerParams) (*NvmfSubsystemAddListenerResult, error) {
func (p *NvmfServiceImpl) AddListener(context.Context, *NvmfSubsystemAddListenerParams) (*NvmfSubsystemAddListenerResult, error) {

Check warning on line 61 in spdk/nvmf.service.impl.go

View check run for this annotation

Codecov / codecov/patch

spdk/nvmf.service.impl.go#L61

Added line #L61 was not covered by tests
// TBD
return nil, nil
}

func (p *NvmfServiceImpl) RemoveListener(*NvmfSubsystemAddListenerParams) (*NvmfSubsystemAddListenerResult, error) {
func (p *NvmfServiceImpl) RemoveListener(context.Context, *NvmfSubsystemAddListenerParams) (*NvmfSubsystemAddListenerResult, error) {

Check warning on line 66 in spdk/nvmf.service.impl.go

View check run for this annotation

Codecov / codecov/patch

spdk/nvmf.service.impl.go#L66

Added line #L66 was not covered by tests
// TBD
return nil, nil
}

func (p *NvmfServiceImpl) AddNamespace(*NvmfSubsystemAddNsParams) (*NvmfSubsystemAddNsResult, error) {
func (p *NvmfServiceImpl) AddNamespace(context.Context, *NvmfSubsystemAddNsParams) (*NvmfSubsystemAddNsResult, error) {

Check warning on line 71 in spdk/nvmf.service.impl.go

View check run for this annotation

Codecov / codecov/patch

spdk/nvmf.service.impl.go#L71

Added line #L71 was not covered by tests
// TBD
return nil, nil
}

func (p *NvmfServiceImpl) RemoveNamespace(*NvmfSubsystemRemoveNsParams) (*NvmfSubsystemRemoveNsResult, error) {
func (p *NvmfServiceImpl) RemoveNamespace(context.Context, *NvmfSubsystemRemoveNsParams) (*NvmfSubsystemRemoveNsResult, error) {

Check warning on line 76 in spdk/nvmf.service.impl.go

View check run for this annotation

Codecov / codecov/patch

spdk/nvmf.service.impl.go#L76

Added line #L76 was not covered by tests
// TBD
return nil, nil
}

0 comments on commit 15e8f28

Please sign in to comment.