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) - } - } - } - } -}