From 70e9dd7b6f4d438834a5dda0e0d2b374a9b934ba Mon Sep 17 00:00:00 2001 From: Boris Glimcher Date: Wed, 4 Jan 2023 16:02:04 +0200 Subject: [PATCH] tests: implement Test_InventoryGet Signed-off-by: Boris Glimcher --- inventory_test.go | 81 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/inventory_test.go b/inventory_test.go index 148a9d1..29e80b3 100644 --- a/inventory_test.go +++ b/inventory_test.go @@ -4,9 +4,90 @@ package main import ( + "context" + "log" + "net" + // "reflect" "testing" + + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/credentials/insecure" + "google.golang.org/grpc/status" + "google.golang.org/grpc/test/bufconn" + + pc "github.com/opiproject/opi-api/common/v1/gen/go" ) +func dialer() func(context.Context, string) (net.Conn, error) { + listener := bufconn.Listen(1024 * 1024) + server := grpc.NewServer() + pc.RegisterInventorySvcServer(server, &PluginInventory) + + go func() { + if err := server.Serve(listener); err != nil { + log.Fatal(err) + } + }() + + return func(context.Context, string) (net.Conn, error) { + return listener.Dial() + } +} + func Test_InventoryGet(t *testing.T) { + tests := []struct { + name string + out *pc.InventoryGetResponse + errCode codes.Code + errMsg string + }{ + { + "valid request with valid responce", + &pc.InventoryGetResponse{ + Bios: &pc.BIOSInfo{Vendor: "TBD", Version: "TBD", Date: "TBD"}, + System: &pc.SystemInfo{Family: "TBD", Name: "TBD", Vendor: "TBD", SerialNumber: "TBD", Uuid: "TBD", Sku: "TBD", Version: "TBD"}, + Baseboard: &pc.BaseboardInfo{AssetTag: "TBD", SerialNumber: "TBD", Vendor: "TBD", Version: "TBD", Product: "TBD"}, + Chassis: &pc.ChassisInfo{AssetTag: "TBD", SerialNumber: "TBD", Type: "TBD", TypeDescription: "TBD", Vendor: "TBD", Version: "TBD"}, + Processor: &pc.CPUInfo{TotalCores: 8, TotalThreads: 16}, + Memory: &pc.MemoryInfo{TotalPhysicalBytes: 12, TotalUsableBytes: 55}, + }, + codes.OK, + "", + }, + } + + // start GRPC mockup server + ctx := context.Background() + conn, err := grpc.DialContext(ctx, "", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(dialer())) + if err != nil { + log.Fatal(err) + } + defer conn.Close() + client := pc.NewInventorySvcClient(conn) + + // run tests + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + request := &pc.InventoryGetRequest{} + response, err := client.InventoryGet(ctx, request) + if response != nil { + // if !reflect.DeepEqual(response, tt.out) { + if response.Bios.Vendor == "" { + t.Error("response: expected", tt.out, "received", response) + } + } + if err != nil { + if er, ok := status.FromError(err); ok { + if er.Code() != tt.errCode { + t.Error("error code: expected", codes.InvalidArgument, "received", er.Code()) + } + if er.Message() != tt.errMsg { + t.Error("error message: expected", tt.errMsg, "received", er.Message()) + } + } + } + }) + } }