From 41a497ab8a5a2ad580e3e41c3bd6a5780835fec9 Mon Sep 17 00:00:00 2001 From: Boris Glimcher Date: Tue, 26 Sep 2023 01:22:11 +0300 Subject: [PATCH] refactor(utils): split proto to a new file Signed-off-by: Boris Glimcher --- pkg/utils/proto.go | 66 ++++++++++++++++++++++ pkg/utils/{utils_test.go => proto_test.go} | 0 pkg/utils/{utils.go => server.go} | 57 ------------------- 3 files changed, 66 insertions(+), 57 deletions(-) create mode 100644 pkg/utils/proto.go rename pkg/utils/{utils_test.go => proto_test.go} (100%) rename pkg/utils/{utils.go => server.go} (69%) diff --git a/pkg/utils/proto.go b/pkg/utils/proto.go new file mode 100644 index 00000000..57cab70a --- /dev/null +++ b/pkg/utils/proto.go @@ -0,0 +1,66 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries. +// Copyright (C) 2023 Intel Corporation + +// Package utils contails useful helper functions +package utils + +import ( + "google.golang.org/protobuf/proto" +) + +// ProtoClone creates a deep copy of a provided gRPC structure +func ProtoClone[T proto.Message](protoStruct T) T { + return proto.Clone(protoStruct).(T) +} + +// EqualProtoSlices reports if 2 slices containing proto structures are equal +func EqualProtoSlices[T proto.Message](x, y []T) bool { + if len(x) != len(y) { + return false + } + + for i := 0; i < len(x); i++ { + if !proto.Equal(x[i], y[i]) { + return false + } + } + + return true +} + +// ProtoObjChangedReporter used by CheckTestProtoObjectsNotChangedInTestFunc +// to report errors if a test object changed +type ProtoObjChangedReporter interface { + Fatalf(format string, args ...any) +} + +// CheckTestProtoObjectsNotChangedInTestFunc checks test proto objects +// have not changed in a given test case. Accepts an interface to report +// a failure and the name of a test, returns a dedicated function to +// Cleanup method of *testing.T +type CheckTestProtoObjectsNotChangedInTestFunc = func( + r ProtoObjChangedReporter, testName string) func() + +// CheckTestProtoObjectsNotChanged can be used to check if test objects +// describing a resource used in multiple cases changed by a test or not. +// This function return another one for usage in a given test case. +func CheckTestProtoObjectsNotChanged( + msgs ...proto.Message, +) CheckTestProtoObjectsNotChangedInTestFunc { + origMsgs := []proto.Message{} + for _, m := range msgs { + origMsgs = append(origMsgs, ProtoClone(m)) + } + + return func(r ProtoObjChangedReporter, testName string) func() { + return func() { + for i, current := range msgs { + if !proto.Equal(current, origMsgs[i]) { + r.Fatalf("Global test object %v was changed to %v by test in %s", + origMsgs[i], current, testName) + } + } + } + } +} diff --git a/pkg/utils/utils_test.go b/pkg/utils/proto_test.go similarity index 100% rename from pkg/utils/utils_test.go rename to pkg/utils/proto_test.go diff --git a/pkg/utils/utils.go b/pkg/utils/server.go similarity index 69% rename from pkg/utils/utils.go rename to pkg/utils/server.go index 77519ac3..e7adea08 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/server.go @@ -18,7 +18,6 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "google.golang.org/protobuf/proto" "github.com/opiproject/gospdk/spdk" ) @@ -142,63 +141,7 @@ func spdkMockServerCommunicate(rpc spdk.JSONRPC, l net.Listener, toSend []string } } -// ProtoClone creates a deep copy of a provided gRPC structure -func ProtoClone[T proto.Message](protoStruct T) T { - return proto.Clone(protoStruct).(T) -} - -// EqualProtoSlices reports if 2 slices containing proto structures are equal -func EqualProtoSlices[T proto.Message](x, y []T) bool { - if len(x) != len(y) { - return false - } - - for i := 0; i < len(x); i++ { - if !proto.Equal(x[i], y[i]) { - return false - } - } - - return true -} - // ResourceIDToVolumeName creates name of volume resource based on ID func ResourceIDToVolumeName(resourceID string) string { return fmt.Sprintf("//storage.opiproject.org/volumes/%s", resourceID) } - -// ProtoObjChangedReporter used by CheckTestProtoObjectsNotChangedInTestFunc -// to report errors if a test object changed -type ProtoObjChangedReporter interface { - Fatalf(format string, args ...any) -} - -// CheckTestProtoObjectsNotChangedInTestFunc checks test proto objects -// have not changed in a given test case. Accepts an interface to report -// a failure and the name of a test, returns a dedicated function to -// Cleanup method of *testing.T -type CheckTestProtoObjectsNotChangedInTestFunc = func( - r ProtoObjChangedReporter, testName string) func() - -// CheckTestProtoObjectsNotChanged can be used to check if test objects -// describing a resource used in multiple cases changed by a test or not. -// This function return another one for usage in a given test case. -func CheckTestProtoObjectsNotChanged( - msgs ...proto.Message, -) CheckTestProtoObjectsNotChangedInTestFunc { - origMsgs := []proto.Message{} - for _, m := range msgs { - origMsgs = append(origMsgs, ProtoClone(m)) - } - - return func(r ProtoObjChangedReporter, testName string) func() { - return func() { - for i, current := range msgs { - if !proto.Equal(current, origMsgs[i]) { - r.Fatalf("Global test object %v was changed to %v by test in %s", - origMsgs[i], current, testName) - } - } - } - } -}