github.com/drone/runner-go@v1.12.0/logger/history/history_test.go (about)

     1  // Copyright 2019 Drone.IO Inc. All rights reserved.
     2  // Use of this source code is governed by the Polyform License
     3  // that can be found in the LICENSE file.
     4  
     5  package history
     6  
     7  import (
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/google/go-cmp/cmp"
    12  	"github.com/sirupsen/logrus"
    13  )
    14  
    15  func TestLevels(t *testing.T) {
    16  	hook := New()
    17  	if diff := cmp.Diff(hook.Levels(), logrus.AllLevels); diff != "" {
    18  		t.Errorf("Hook should return all levels")
    19  		t.Log(diff)
    20  	}
    21  }
    22  
    23  func TestConvertLevels(t *testing.T) {
    24  	tests := []struct {
    25  		before logrus.Level
    26  		after  Level
    27  	}{
    28  		{logrus.PanicLevel, LevelError},
    29  		{logrus.FatalLevel, LevelError},
    30  		{logrus.ErrorLevel, LevelError},
    31  		{logrus.WarnLevel, LevelWarn},
    32  		{logrus.InfoLevel, LevelInfo},
    33  		{logrus.DebugLevel, LevelDebug},
    34  		{logrus.TraceLevel, LevelTrace},
    35  	}
    36  	if len(tests) != len(logrus.AllLevels) {
    37  		t.Errorf("missing unit tests for all logrus levels")
    38  	}
    39  	for _, test := range tests {
    40  		if got, want := convertLevel(test.before), test.after; got != want {
    41  			t.Errorf("Want entry level %v, got %v", want, got)
    42  		}
    43  	}
    44  }
    45  
    46  func TestLimit(t *testing.T) {
    47  	hook := NewLimit(4)
    48  	hook.Fire(&logrus.Entry{})
    49  	hook.Fire(&logrus.Entry{})
    50  	hook.Fire(&logrus.Entry{})
    51  	hook.Fire(&logrus.Entry{})
    52  	hook.Fire(&logrus.Entry{})
    53  	if got, want := len(hook.entries), 4; got != want {
    54  		t.Errorf("Expect entries pruned to %d, got %d", want, got)
    55  	}
    56  }
    57  
    58  func TestHistory(t *testing.T) {
    59  	hook := New()
    60  
    61  	now := time.Now()
    62  	hook.Fire(&logrus.Entry{
    63  		Level:   logrus.DebugLevel,
    64  		Message: "foo",
    65  		Data:    logrus.Fields{"foo": "bar"},
    66  		Time:    now,
    67  	})
    68  
    69  	hook.Fire(&logrus.Entry{
    70  		Level:   logrus.InfoLevel,
    71  		Message: "bar",
    72  		Data:    logrus.Fields{"baz": "qux"},
    73  		Time:    now,
    74  	})
    75  
    76  	if len(hook.entries) != 2 {
    77  		t.Errorf("Expected 2 hooks added to history")
    78  	}
    79  
    80  	entries := hook.Entries()
    81  	if len(entries) != 2 {
    82  		t.Errorf("Expected 2 hooks returned")
    83  	}
    84  	if entries[0] == hook.entries[0] {
    85  		t.Errorf("Expect copy of entries, got a reference")
    86  	}
    87  	if entries[1] == hook.entries[1] {
    88  		t.Errorf("Expect copy of entries, got a reference")
    89  	}
    90  
    91  	expect := []*Entry{
    92  		{
    93  			Level:   LevelDebug,
    94  			Message: "foo",
    95  			Data:    logrus.Fields{"foo": "bar"},
    96  			Unix:    now.Unix(),
    97  		},
    98  		{
    99  			Level:   LevelInfo,
   100  			Message: "bar",
   101  			Data:    logrus.Fields{"baz": "qux"},
   102  			Unix:    now.Unix(),
   103  		},
   104  	}
   105  	if diff := cmp.Diff(entries, expect); diff != "" {
   106  		t.Errorf("Entries should return an exact copy of all entries")
   107  		t.Log(diff)
   108  	}
   109  }
   110  
   111  func TestFilter(t *testing.T) {
   112  	hook := New()
   113  
   114  	now := time.Now()
   115  	hook.Fire(&logrus.Entry{
   116  		Level:   logrus.DebugLevel,
   117  		Message: "foo",
   118  		Data:    logrus.Fields{"foo": "bar"},
   119  		Time:    now,
   120  	})
   121  
   122  	hook.Fire(&logrus.Entry{
   123  		Level:   logrus.InfoLevel,
   124  		Message: "bar",
   125  		Data:    logrus.Fields{"baz": "qux"},
   126  		Time:    now,
   127  	})
   128  
   129  	expect := []*Entry{
   130  		{
   131  			Level:   LevelDebug,
   132  			Message: "foo",
   133  			Data:    logrus.Fields{"foo": "bar"},
   134  			Unix:    now.Unix(),
   135  		},
   136  	}
   137  	entries := hook.Filter(func(entry *Entry) bool {
   138  		return entry.Data["foo"] == "bar"
   139  	})
   140  	if diff := cmp.Diff(entries, expect); diff != "" {
   141  		t.Errorf("Entries should return an exact copy of all entries")
   142  		t.Log(diff)
   143  	}
   144  }