Files
velero/pkg/util/logging/error_location_hook_test.go
T
Xun Jiang/Bruce JiangandGitHub 25402b6209
Run the E2E test on kind / get-go-version (push) Successful in 59s
Run the E2E test on kind / setup-test-matrix (push) Successful in 4s
Main CI / get-go-version (push) Successful in 14s
Run the E2E test on kind / build (push) Failing after 1m48s
Run the E2E test on kind / run-e2e-test (push) Has been skipped
Main CI / Build (push) Failing after 25s
[1.18] Deprecate inactive maintained packages (#9912)
* Replace github.com/robfig/cron/v3 by github.com/netresearch/go-cron

Replace k8s.io/utils/pointer with k8s.io/utils/ptr

Signed-off-by: Xun Jiang <xun.jiang@broadcom.com>

* Replace gopkg.in/yaml.v3 by go.yaml.in/yaml/v3

Signed-off-by: Xun Jiang <xun.jiang@broadcom.com>

* Replace github.com/joho/godotenv.

Move the needed code into Velero repository.

Signed-off-by: Xun Jiang <xun.jiang@broadcom.com>

* Replace github.com/pkg/errors by github.com/cockroachdb/errors

Change errors.Cause to errors.Is, because github.com/cockroachdb/errors
New() function create a error with error stack with depth 1, but
github.com/pkg/errors's New() function create error with no depth.

Signed-off-by: Xun Jiang <xun.jiang@broadcom.com>

---------

Signed-off-by: Xun Jiang <xun.jiang@broadcom.com>
Signed-off-by: Xun Jiang/Bruce Jiang <59276555+blackpiglet@users.noreply.github.com>
2026-07-10 11:20:34 +08:00

112 lines
3.3 KiB
Go

/*
Copyright 2017, 2019 the Velero contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package logging
import (
"errors"
"testing"
pkgerrs "github.com/cockroachdb/errors"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFire(t *testing.T) {
tests := []struct {
name string
preEntryFields map[string]any
expectedEntryFields map[string]any
expectedErr bool
}{
{
name: "no error",
preEntryFields: map[string]any{"foo": "bar"},
expectedEntryFields: map[string]any{"foo": "bar"},
},
{
name: "basic (non-pkg/errors) error",
preEntryFields: map[string]any{logrus.ErrorKey: errors.New("a normal error")},
expectedEntryFields: map[string]any{logrus.ErrorKey: errors.New("a normal error")},
},
{
name: "non-error logged in error field",
preEntryFields: map[string]any{logrus.ErrorKey: "not an error"},
expectedEntryFields: map[string]any{logrus.ErrorKey: "not an error"},
expectedErr: false,
},
{
name: "pkg/errors error",
preEntryFields: map[string]any{logrus.ErrorKey: pkgerrs.New("a pkg/errors error")},
expectedEntryFields: map[string]any{
logrus.ErrorKey: pkgerrs.New("a pkg/errors error"),
errorFileField: "",
errorFunctionField: "github.com/vmware-tanzu/velero/pkg/util/logging.TestFire",
},
},
{
name: "already have error file and function fields",
preEntryFields: map[string]any{
logrus.ErrorKey: pkgerrs.New("a pkg/errors error"),
errorFileField: "some_file.go:123",
errorFunctionField: "SomeFunction",
},
expectedEntryFields: map[string]any{
logrus.ErrorKey: pkgerrs.New("a pkg/errors error"),
errorFileField: "some_file.go:123",
errorFunctionField: "SomeFunction",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
hook := &ErrorLocationHook{}
entry := &logrus.Entry{
Data: logrus.Fields(test.preEntryFields),
}
// method under test
err := hook.Fire(entry)
require.Equal(t, test.expectedErr, err != nil)
require.Len(t, entry.Data, len(test.expectedEntryFields))
for key, expectedValue := range test.expectedEntryFields {
actualValue, found := entry.Data[key]
assert.True(t, found, "expected key not found: %s", key)
switch key {
// test existence of this field only since testing the value
// is fragile
case errorFileField:
case errorFunctionField:
case logrus.ErrorKey:
if err, ok := expectedValue.(error); ok {
assert.Equal(t, err.Error(), actualValue.(error).Error())
} else {
assert.Equal(t, expectedValue, actualValue)
}
default:
assert.Equal(t, expectedValue, actualValue)
}
}
})
}
}