From 9914f722013dd8916c04c503e07484fbbbc88805 Mon Sep 17 00:00:00 2001 From: Mikhail Montsev Date: Tue, 23 Jan 2024 15:59:03 +0000 Subject: [PATCH] [Disk Manager] fix facade/image_service_*test --- .../disk_service_test/disk_service_test.go | 12 +------ .../image_service_test/image_service_test.go | 20 +++++------- .../internal/pkg/facade/testcommon/common.go | 31 +++++++++++++++++++ 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/cloud/disk_manager/internal/pkg/facade/disk_service_test/disk_service_test.go b/cloud/disk_manager/internal/pkg/facade/disk_service_test/disk_service_test.go index 2a975ec9506..420949f8152 100644 --- a/cloud/disk_manager/internal/pkg/facade/disk_service_test/disk_service_test.go +++ b/cloud/disk_manager/internal/pkg/facade/disk_service_test/disk_service_test.go @@ -78,17 +78,7 @@ func TestDiskServiceShouldFailCreateDiskFromNonExistingImage(t *testing.T) { require.Error(t, err) require.Contains(t, err.Error(), "not found") - status, ok := grpc_status.FromError(err) - require.True(t, ok) - - statusDetails := status.Details() - require.Equal(t, 1, len(statusDetails)) - - errorDetails, ok := statusDetails[0].(*disk_manager.ErrorDetails) - require.True(t, ok) - - require.Equal(t, int64(codes.BadSource), errorDetails.Code) - require.False(t, errorDetails.Internal) + testcommon.CheckErrorDetails(t, err, codes.BadSource, "", false /* internal */) testcommon.CheckConsistency(t, ctx) */ diff --git a/cloud/disk_manager/internal/pkg/facade/image_service_test/image_service_test.go b/cloud/disk_manager/internal/pkg/facade/image_service_test/image_service_test.go index d41ae841b6c..bd519e3bf31 100644 --- a/cloud/disk_manager/internal/pkg/facade/image_service_test/image_service_test.go +++ b/cloud/disk_manager/internal/pkg/facade/image_service_test/image_service_test.go @@ -14,7 +14,6 @@ import ( "github.com/ydb-platform/nbs/cloud/disk_manager/internal/pkg/types" sdk_client "github.com/ydb-platform/nbs/cloud/disk_manager/pkg/client" "github.com/ydb-platform/nbs/cloud/disk_manager/pkg/client/codes" - grpc_status "google.golang.org/grpc/status" ) //////////////////////////////////////////////////////////////////////////////// @@ -651,18 +650,13 @@ func testShouldFailCreateImageFromImageFileURL( err = internal_client.WaitOperation(ctx, client, operation.Id) require.Error(t, err) - status, ok := grpc_status.FromError(err) - require.True(t, ok) - - statusDetails := status.Details() - require.Equal(t, 1, len(statusDetails)) - - errorDetails, ok := statusDetails[0].(*disk_manager.ErrorDetails) - require.True(t, ok) - - require.Equal(t, int64(codes.BadSource), errorDetails.Code) - require.False(t, errorDetails.Internal) - require.Equal(t, errorDetailsMessage, errorDetails.Message) + testcommon.CheckErrorDetails( + t, + err, + codes.BadSource, + errorDetailsMessage, + true, // internal + ) testcommon.CheckConsistency(t, ctx) } diff --git a/cloud/disk_manager/internal/pkg/facade/testcommon/common.go b/cloud/disk_manager/internal/pkg/facade/testcommon/common.go index abdee8dc8a1..2653bfa5734 100644 --- a/cloud/disk_manager/internal/pkg/facade/testcommon/common.go +++ b/cloud/disk_manager/internal/pkg/facade/testcommon/common.go @@ -13,6 +13,7 @@ import ( "testing" "time" + "github.com/golang/protobuf/proto" "github.com/golang/protobuf/ptypes/empty" "github.com/google/uuid" "github.com/stretchr/testify/require" @@ -674,3 +675,33 @@ func GetEncryptionKeyHash(encryptionDesc *types.EncryptionDesc) ([]byte, error) return nil, errors.NewNonRetriableErrorf("unknown key %s", key) } } + +//////////////////////////////////////////////////////////////////////////////// + +func CheckErrorDetails( + t *testing.T, + err error, + code int, + message string, + internal bool, +) { + + status, ok := grpc_status.FromError(err) + require.True(t, ok) + + statusDetails := status.Details() + require.Equal(t, 1, len(statusDetails)) + + protoMessage, err := proto.Marshal(statusDetails[0].(proto.Message)) + require.NoError(t, err) + + var errorDetails disk_manager.ErrorDetails + err = proto.Unmarshal(protoMessage, &errorDetails) + require.NoError(t, err) + + require.Equal(t, int64(code), errorDetails.Code) + require.False(t, errorDetails.Internal) + if len(message) != 0 { + require.Equal(t, message, errorDetails.Message) + } +}