Skip to content

Commit

Permalink
Fix floatation of error/message in the backup/restore result
Browse files Browse the repository at this point in the history
Signed-off-by: Shashank Singh <[email protected]>
  • Loading branch information
Shashank1306s committed Nov 29, 2023
1 parent 4d21e29 commit d473898
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/util/logging/log_counter_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ func (h *LogHook) Fire(entry *logrus.Entry) error {

namespace, isNamespacePresent := entry.Data["namespace"]
errorField, isErrorFieldPresent := entry.Data["error"]
// When JSON logging format is enabled, error message is placed at "error.message" instead of "error"
errorMsgField, isErrorMsgFieldPresent := entry.Data["error.message"]
resourceField, isResourceFieldPresent := entry.Data["resource"]
nameField, isNameFieldPresent := entry.Data["name"]
msgField, isMsgFieldPresent := entry.Message, true

entryMessage := ""
if isResourceFieldPresent {
Expand All @@ -73,6 +76,12 @@ func (h *LogHook) Fire(entry *logrus.Entry) error {
if isErrorFieldPresent {
entryMessage = fmt.Sprintf("%s error: /%v", entryMessage, errorField)
}
if isErrorMsgFieldPresent {
entryMessage = fmt.Sprintf("%s error.message: /%v", entryMessage, errorMsgField)
}
if isMsgFieldPresent {
entryMessage = fmt.Sprintf("%s msg: /%v", entryMessage, msgField)
}

if isNamespacePresent {
h.entries[entry.Level].Add(namespace.(string), errors.New(entryMessage))
Expand Down
154 changes: 154 additions & 0 deletions pkg/util/logging/log_counter_hook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package logging

import (
"reflect"
"sync"
"testing"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/vmware-tanzu/velero/pkg/util/results"
)

type fields struct {
mu sync.RWMutex
counts map[logrus.Level]int
entries map[logrus.Level]*results.Result
}

func TestLogHook_Fire(t *testing.T) {
type args struct {
entry *logrus.Entry
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "test",
fields: fields{
mu: sync.RWMutex{},
counts: map[logrus.Level]int{
logrus.ErrorLevel: 1,
},
entries: map[logrus.Level]*results.Result{
logrus.ErrorLevel: {
Velero: []string{"test error"},
},
},
},
args: args{
entry: &logrus.Entry{
Level: logrus.ErrorLevel,
Data: map[string]interface{}{
"namespace": "test",
"error": errors.New("test error"),
},
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := &LogHook{
mu: tt.fields.mu,
counts: tt.fields.counts,
entries: tt.fields.entries,
}
if err := h.Fire(tt.args.entry); (err != nil) != tt.wantErr {
t.Errorf("LogHook.Fire() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestLogHook_GetCount(t *testing.T) {
type args struct {
level logrus.Level
}
tests := []struct {
name string
fields fields
args args
want int
}{
{
name: "test",
fields: fields{
mu: sync.RWMutex{},
counts: map[logrus.Level]int{
logrus.ErrorLevel: 1,
},
entries: map[logrus.Level]*results.Result{
logrus.ErrorLevel: {
Velero: []string{"test error"},
},
},
},
args: args{
level: logrus.ErrorLevel,
},
want: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := &LogHook{
mu: tt.fields.mu,
counts: tt.fields.counts,
entries: tt.fields.entries,
}
if got := h.GetCount(tt.args.level); got != tt.want {
t.Errorf("LogHook.GetCount() = %v, want %v", got, tt.want)
}
})
}
}

func TestLogHook_GetEntries(t *testing.T) {
type args struct {
level logrus.Level
}
tests := []struct {
name string
fields fields
args args
want results.Result
}{
{
name: "test",
fields: fields{
mu: sync.RWMutex{},
counts: map[logrus.Level]int{
logrus.ErrorLevel: 1,
},
entries: map[logrus.Level]*results.Result{
logrus.ErrorLevel: {
Velero: []string{"test error"},
},
},
},
args: args{
level: logrus.ErrorLevel,
},
want: results.Result{
Velero: []string{"test error"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := &LogHook{
mu: tt.fields.mu,
counts: tt.fields.counts,
entries: tt.fields.entries,
}
if got := h.GetEntries(tt.args.level); !reflect.DeepEqual(got, tt.want) {
t.Errorf("LogHook.GetEntries() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit d473898

Please sign in to comment.