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

Uniformly check returned error on OK response in kvm #522

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
114 changes: 74 additions & 40 deletions pkg/kvm/blk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ package kvm
import (
"bytes"
"context"
"errors"
"testing"

"github.com/opiproject/gospdk/spdk"
pc "github.com/opiproject/opi-api/common/v1/gen/go"
pb "github.com/opiproject/opi-api/storage/v1alpha1/gen/go"
"github.com/opiproject/opi-spdk-bridge/pkg/frontend"
"github.com/opiproject/opi-spdk-bridge/pkg/server"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
)

Expand All @@ -36,7 +37,8 @@ func TestCreateVirtioBlk(t *testing.T) {

tests := map[string]struct {
jsonRPC spdk.JSONRPC
expectError error
errCode codes.Code
errMsg string
nonDefaultQmpAddress string
buses []string

Expand All @@ -55,21 +57,24 @@ func TestCreateVirtioBlk(t *testing.T) {
ExpectQueryPci(testVirtioBlkID),
},
"spdk failed to create virtio-blk": {
in: testCreateVirtioBlkRequest,
jsonRPC: alwaysFailingJSONRPC,
expectError: errStub,
in: testCreateVirtioBlkRequest,
jsonRPC: alwaysFailingJSONRPC,
errCode: status.Convert(errStub).Code(),
errMsg: status.Convert(errStub).Message(),
},
"qemu chardev add failed": {
in: testCreateVirtioBlkRequest,
jsonRPC: alwaysSuccessfulJSONRPC,
expectError: errAddChardevFailed,
in: testCreateVirtioBlkRequest,
jsonRPC: alwaysSuccessfulJSONRPC,
errCode: status.Convert(errAddChardevFailed).Code(),
errMsg: status.Convert(errAddChardevFailed).Message(),
mockQmpCalls: newMockQmpCalls().
ExpectAddChardev(testVirtioBlkID).WithErrorResponse(),
},
"qemu device add failed": {
in: testCreateVirtioBlkRequest,
jsonRPC: alwaysSuccessfulJSONRPC,
expectError: errAddDeviceFailed,
in: testCreateVirtioBlkRequest,
jsonRPC: alwaysSuccessfulJSONRPC,
errCode: status.Convert(errAddDeviceFailed).Code(),
errMsg: status.Convert(errAddDeviceFailed).Message(),
mockQmpCalls: newMockQmpCalls().
ExpectAddChardev(testVirtioBlkID).
ExpectAddVirtioBlk(testVirtioBlkID, testVirtioBlkID).WithErrorResponse().
Expand All @@ -79,7 +84,8 @@ func TestCreateVirtioBlk(t *testing.T) {
in: testCreateVirtioBlkRequest,
nonDefaultQmpAddress: "/dev/null",
jsonRPC: alwaysSuccessfulJSONRPC,
expectError: errMonitorCreation,
errCode: status.Convert(errMonitorCreation).Code(),
errMsg: status.Convert(errMonitorCreation).Message(),
},
"valid virtio-blk creation with on first bus location": {
in: &pb.CreateVirtioBlkRequest{VirtioBlk: &pb.VirtioBlk{
Expand Down Expand Up @@ -111,32 +117,35 @@ func TestCreateVirtioBlk(t *testing.T) {
ExpectQueryPci(testVirtioBlkID),
},
"virtio-blk creation with physical function goes out of buses": {
in: testCreateVirtioBlkRequest,
out: nil,
expectError: errDeviceEndpoint,
jsonRPC: alwaysSuccessfulJSONRPC,
buses: []string{"pci.opi.0"},
in: testCreateVirtioBlkRequest,
out: nil,
errCode: status.Convert(errDeviceEndpoint).Code(),
errMsg: status.Convert(errDeviceEndpoint).Message(),
jsonRPC: alwaysSuccessfulJSONRPC,
buses: []string{"pci.opi.0"},
},
"negative physical function": {
in: &pb.CreateVirtioBlkRequest{VirtioBlk: &pb.VirtioBlk{
PcieId: &pb.PciEndpoint{PhysicalFunction: -1},
VolumeId: &pc.ObjectKey{Value: "Malloc42"},
MaxIoQps: 1,
}, VirtioBlkId: testVirtioBlkID},
out: nil,
expectError: errDeviceEndpoint,
jsonRPC: alwaysSuccessfulJSONRPC,
buses: []string{"pci.opi.0"},
out: nil,
errCode: status.Convert(errDeviceEndpoint).Code(),
errMsg: status.Convert(errDeviceEndpoint).Message(),
jsonRPC: alwaysSuccessfulJSONRPC,
buses: []string{"pci.opi.0"},
},
"nil pcie endpoint": {
in: &pb.CreateVirtioBlkRequest{VirtioBlk: &pb.VirtioBlk{
PcieId: nil,
VolumeId: &pc.ObjectKey{Value: "Malloc42"},
MaxIoQps: 1,
}, VirtioBlkId: testVirtioBlkID},
out: nil,
expectError: errNoPcieEndpoint,
jsonRPC: alwaysSuccessfulJSONRPC,
out: nil,
errCode: status.Convert(errNoPcieEndpoint).Code(),
errMsg: status.Convert(errNoPcieEndpoint).Message(),
jsonRPC: alwaysSuccessfulJSONRPC,
},
}

Expand All @@ -154,9 +163,18 @@ func TestCreateVirtioBlk(t *testing.T) {
request := server.ProtoClone(test.in)

out, err := kvmServer.CreateVirtioBlk(context.Background(), request)
if !errors.Is(err, test.expectError) {
t.Errorf("Expected error %v, got %v", test.expectError, err)

if er, ok := status.FromError(err); ok {
if er.Code() != test.errCode {
t.Error("error code: expected", test.errCode, "received", er.Code())
}
if er.Message() != test.errMsg {
t.Error("error message: expected", test.errMsg, "received", er.Message())
}
} else {
t.Errorf("expected grpc error status")
}

gotOut, _ := proto.Marshal(out)
wantOut, _ := proto.Marshal(test.out)
if !bytes.Equal(gotOut, wantOut) {
Expand All @@ -172,7 +190,8 @@ func TestCreateVirtioBlk(t *testing.T) {
func TestDeleteVirtioBlk(t *testing.T) {
tests := map[string]struct {
jsonRPC spdk.JSONRPC
expectError error
errCode codes.Code
errMsg string
nonDefaultQmpAddress string

mockQmpCalls *mockQmpCalls
Expand All @@ -184,44 +203,50 @@ func TestDeleteVirtioBlk(t *testing.T) {
ExpectDeleteChardev(testVirtioBlkID),
},
"qemu device delete failed": {
jsonRPC: alwaysSuccessfulJSONRPC,
expectError: errDevicePartiallyDeleted,
jsonRPC: alwaysSuccessfulJSONRPC,
errCode: status.Convert(errDevicePartiallyDeleted).Code(),
errMsg: status.Convert(errDevicePartiallyDeleted).Message(),
mockQmpCalls: newMockQmpCalls().
ExpectDeleteVirtioBlk(testVirtioBlkID).WithErrorResponse().
ExpectDeleteChardev(testVirtioBlkID),
},
"qemu device delete failed by timeout": {
jsonRPC: alwaysSuccessfulJSONRPC,
expectError: errDevicePartiallyDeleted,
jsonRPC: alwaysSuccessfulJSONRPC,
errCode: status.Convert(errDevicePartiallyDeleted).Code(),
errMsg: status.Convert(errDevicePartiallyDeleted).Message(),
mockQmpCalls: newMockQmpCalls().
ExpectDeleteVirtioBlk(testVirtioBlkID).
ExpectDeleteChardev(testVirtioBlkID),
},
"qemu chardev delete failed": {
jsonRPC: alwaysSuccessfulJSONRPC,
expectError: errDevicePartiallyDeleted,
jsonRPC: alwaysSuccessfulJSONRPC,
errCode: status.Convert(errDevicePartiallyDeleted).Code(),
errMsg: status.Convert(errDevicePartiallyDeleted).Message(),
mockQmpCalls: newMockQmpCalls().
ExpectDeleteVirtioBlkWithEvent(testVirtioBlkID).
ExpectDeleteChardev(testVirtioBlkID).WithErrorResponse(),
},
"spdk failed to delete virtio-blk": {
jsonRPC: alwaysFailingJSONRPC,
expectError: errDevicePartiallyDeleted,
jsonRPC: alwaysFailingJSONRPC,
errCode: status.Convert(errDevicePartiallyDeleted).Code(),
errMsg: status.Convert(errDevicePartiallyDeleted).Message(),
mockQmpCalls: newMockQmpCalls().
ExpectDeleteVirtioBlkWithEvent(testVirtioBlkID).
ExpectDeleteChardev(testVirtioBlkID),
},
"all qemu and spdk calls failed": {
jsonRPC: alwaysFailingJSONRPC,
expectError: errDeviceNotDeleted,
jsonRPC: alwaysFailingJSONRPC,
errCode: status.Convert(errDeviceNotDeleted).Code(),
errMsg: status.Convert(errDeviceNotDeleted).Message(),
mockQmpCalls: newMockQmpCalls().
ExpectDeleteVirtioBlk(testVirtioBlkID).WithErrorResponse().
ExpectDeleteChardev(testVirtioBlkID).WithErrorResponse(),
},
"failed to create monitor": {
nonDefaultQmpAddress: "/dev/null",
jsonRPC: alwaysSuccessfulJSONRPC,
expectError: errMonitorCreation,
errCode: status.Convert(errMonitorCreation).Code(),
errMsg: status.Convert(errMonitorCreation).Message(),
},
}

Expand All @@ -242,9 +267,18 @@ func TestDeleteVirtioBlk(t *testing.T) {
request := server.ProtoClone(testDeleteVirtioBlkRequest)

_, err := kvmServer.DeleteVirtioBlk(context.Background(), request)
if !errors.Is(err, test.expectError) {
t.Errorf("Expected %v, got %v", test.expectError, err)

if er, ok := status.FromError(err); ok {
if er.Code() != test.errCode {
t.Error("error code: expected", test.errCode, "received", er.Code())
}
if er.Message() != test.errMsg {
t.Error("error message: expected", test.errMsg, "received", er.Message())
}
} else {
t.Errorf("expected grpc error status")
}

if !qmpServer.WereExpectedCallsPerformed() {
t.Errorf("Not all expected calls were performed")
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/kvm/kvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package kvm

import (
"errors"
"fmt"
"log"
"net"
Expand All @@ -18,10 +17,12 @@ import (
"time"

"github.com/opiproject/gospdk/spdk"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

var (
errStub = errors.New("stub error")
errStub = status.Error(codes.Internal, "stub error")
alwaysSuccessfulJSONRPC = stubJSONRRPC{nil}
alwaysFailingJSONRPC = stubJSONRRPC{errStub}

Expand Down
Loading
Loading