From b04f9848231f46c50ffd58a5d71c219aa630bccc Mon Sep 17 00:00:00 2001 From: Jakob Haahr Taankvist Date: Tue, 19 Mar 2024 14:36:26 +0100 Subject: [PATCH] Error mapper tests (#5795) What changed? Refactored the error test in the proto mapper Added test to make sure all fields are set in the test errors (if not, the check will pass, even though data would be lost). Added tests for the thrift mapper Why? Make sure data is preserved in the mappings How did you test it? It's a test in itself. Potential risks Release notes Documentation Changes --- common/types/mapper/proto/errors_test.go | 47 ++++++++----------- common/types/mapper/thrift/errors_test.go | 57 +++++++++++++++++++++++ common/types/testdata/common.go | 1 + common/types/testdata/error.go | 27 +++++++++++ common/types/testdata/error_test.go | 56 ++++++++++++++++++++++ 5 files changed, 160 insertions(+), 28 deletions(-) create mode 100644 common/types/mapper/thrift/errors_test.go create mode 100644 common/types/testdata/error_test.go diff --git a/common/types/mapper/proto/errors_test.go b/common/types/mapper/proto/errors_test.go index 93e7196723e..982c7200ac3 100644 --- a/common/types/mapper/proto/errors_test.go +++ b/common/types/mapper/proto/errors_test.go @@ -32,39 +32,30 @@ import ( ) func TestErrors(t *testing.T) { - for _, err := range []error{ - nil, // OK - no error - &testdata.AccessDeniedError, - &testdata.BadRequestError, - &testdata.CancellationAlreadyRequestedError, - &testdata.ClientVersionNotSupportedError, - &testdata.CurrentBranchChangedError, - &testdata.DomainAlreadyExistsError, - &testdata.DomainNotActiveError, - &testdata.EntityNotExistsError, - &testdata.WorkflowExecutionAlreadyCompletedError, - &testdata.EventAlreadyStartedError, - &testdata.InternalDataInconsistencyError, - &testdata.InternalServiceError, - &testdata.LimitExceededError, - &testdata.QueryFailedError, - &testdata.RemoteSyncMatchedError, - &testdata.RetryTaskV2Error, - &testdata.ServiceBusyError, - &testdata.ShardOwnershipLostError, - &testdata.WorkflowExecutionAlreadyStartedError, - &testdata.StickyWorkerUnavailableError, - errors.New("unknown error"), - } { - name := "OK" - if err != nil { - name = reflect.TypeOf(err).Elem().Name() - } + for _, err := range testdata.Errors { + name := reflect.TypeOf(err).Elem().Name() t.Run(name, func(t *testing.T) { + // Test that the mappings does not lose information assert.Equal(t, err, ToError(FromError(err))) }) } +} + +func TestNilMapsToOK(t *testing.T) { + protoNoError := FromError(nil) + assert.Equal(t, yarpcerrors.CodeOK, yarpcerrors.FromError(protoNoError).Code()) + assert.Nil(t, ToError(protoNoError)) +} + +func TestFromUnknownErrorMapsToUnknownError(t *testing.T) { + err := errors.New("unknown error") + protobufErr := FromError(err) + assert.True(t, yarpcerrors.IsUnknown(protobufErr)) + + assert.Equal(t, err, ToError(protobufErr)) +} +func TestToUnknownErrorMapsToItself(t *testing.T) { timeout := yarpcerrors.DeadlineExceededErrorf("timeout") assert.Equal(t, timeout, ToError(timeout)) } diff --git a/common/types/mapper/thrift/errors_test.go b/common/types/mapper/thrift/errors_test.go new file mode 100644 index 00000000000..6db606a1151 --- /dev/null +++ b/common/types/mapper/thrift/errors_test.go @@ -0,0 +1,57 @@ +// Copyright (c) 2021 Uber Technologies Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +package thrift + +import ( + "errors" + "reflect" + "testing" + + "github.com/stretchr/testify/assert" + "go.uber.org/yarpc/yarpcerrors" + + "github.com/uber/cadence/common/types/testdata" +) + +func TestErrors(t *testing.T) { + for _, err := range testdata.Errors { + name := reflect.TypeOf(err).Elem().Name() + t.Run(name, func(t *testing.T) { + // Test that the mappings does not lose information + assert.Equal(t, err, ToError(FromError(err))) + }) + } +} + +func TestNilMapsToNil(t *testing.T) { + assert.Nil(t, FromError(nil)) + assert.Nil(t, ToError(nil)) +} + +func TestFromUnknownErrorMapsToItself(t *testing.T) { + err := errors.New("unknown error") + assert.Equal(t, err, FromError(err)) +} + +func TestToUnknownErrorMapsToItself(t *testing.T) { + err := yarpcerrors.DeadlineExceededErrorf("timeout") + assert.Equal(t, err, ToError(err)) +} diff --git a/common/types/testdata/common.go b/common/types/testdata/common.go index 0abe3215b6c..643cc093778 100644 --- a/common/types/testdata/common.go +++ b/common/types/testdata/common.go @@ -56,6 +56,7 @@ const ( ClientImpl = "ClientImpl" ClientLibraryVersion = "ClientLibraryVersion" SupportedVersions = "SupportedVersions" + FeatureFlag = "FeatureFlag" Attempt = 2 PageSize = 10 diff --git a/common/types/testdata/error.go b/common/types/testdata/error.go index dd10742d400..3dad372ae45 100644 --- a/common/types/testdata/error.go +++ b/common/types/testdata/error.go @@ -44,6 +44,9 @@ var ( ClientImpl: ClientImpl, SupportedVersions: SupportedVersions, } + FeatureNotEnabledError = types.FeatureNotEnabledError{ + FeatureFlag: FeatureFlag, + } CurrentBranchChangedError = types.CurrentBranchChangedError{ Message: ErrorMessage, CurrentBranchToken: BranchToken, @@ -109,3 +112,27 @@ var ( Message: ErrorMessage, } ) + +var Errors = []error{ + &AccessDeniedError, + &BadRequestError, + &CancellationAlreadyRequestedError, + &ClientVersionNotSupportedError, + &FeatureNotEnabledError, + &CurrentBranchChangedError, + &DomainAlreadyExistsError, + &DomainNotActiveError, + &EntityNotExistsError, + &WorkflowExecutionAlreadyCompletedError, + &EventAlreadyStartedError, + &InternalDataInconsistencyError, + &InternalServiceError, + &LimitExceededError, + &QueryFailedError, + &RemoteSyncMatchedError, + &RetryTaskV2Error, + &ServiceBusyError, + &ShardOwnershipLostError, + &WorkflowExecutionAlreadyStartedError, + &StickyWorkerUnavailableError, +} diff --git a/common/types/testdata/error_test.go b/common/types/testdata/error_test.go new file mode 100644 index 00000000000..54754c9165f --- /dev/null +++ b/common/types/testdata/error_test.go @@ -0,0 +1,56 @@ +// The MIT License (MIT) + +// Copyright (c) 2017-2020 Uber Technologies Inc. + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +package testdata + +import ( + "reflect" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestAllFieldsSetInTestErrors(t *testing.T) { + for _, err := range Errors { + name := reflect.TypeOf(err).Elem().Name() + t.Run(name, func(t *testing.T) { + // Test all fields are set in the error + assert.True(t, checkAllIsSet(err)) + }) + } +} + +func checkAllIsSet(err error) bool { + // All the errors are pointers, so we get the value with .Elem + errValue := reflect.ValueOf(err).Elem() + + for i := 0; i < errValue.NumField(); i++ { + field := errValue.Field(i) + + // IsZero checks if the value is the default value (e.g. nil, "", 0 etc) + if field.IsZero() { + return false + } + } + + return true +}