| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otelcol |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
"go.uber.org/zap/zapcore" |
| 12 |
) |
| 13 |
|
| 14 |
func Test_collectorCore_Enabled(t *testing.T) { |
| 15 |
cc := newCollectorCore(newBufferedCore(zapcore.InfoLevel)) |
| 16 |
assert.True(t, cc.Enabled(zapcore.ErrorLevel)) |
| 17 |
assert.False(t, cc.Enabled(zapcore.DebugLevel)) |
| 18 |
} |
| 19 |
|
| 20 |
func Test_collectorCore_Check(t *testing.T) { |
| 21 |
t.Run("check passed", func(t *testing.T) { |
| 22 |
bc := newBufferedCore(zapcore.InfoLevel) |
| 23 |
cc := newCollectorCore(bc) |
| 24 |
e := zapcore.Entry{ |
| 25 |
Level: zapcore.InfoLevel, |
| 26 |
} |
| 27 |
expected := &zapcore.CheckedEntry{} |
| 28 |
expected = expected.AddCore(e, bc) |
| 29 |
assert.Equal(t, expected, cc.Check(e, nil)) |
| 30 |
}) |
| 31 |
|
| 32 |
t.Run("check did not pass", func(t *testing.T) { |
| 33 |
cc := newCollectorCore(newBufferedCore(zapcore.InfoLevel)) |
| 34 |
e := zapcore.Entry{ |
| 35 |
Level: zapcore.DebugLevel, |
| 36 |
} |
| 37 |
assert.Nil(t, cc.Check(e, nil)) |
| 38 |
}) |
| 39 |
} |
| 40 |
|
| 41 |
func Test_collectorCore_With(t *testing.T) { |
| 42 |
cc := newCollectorCore(newBufferedCore(zapcore.InfoLevel)) |
| 43 |
cc.loadDelegate().(*bufferedCore).context = []zapcore.Field{ |
| 44 |
{Key: "original", String: "context"}, |
| 45 |
} |
| 46 |
inputs := []zapcore.Field{ |
| 47 |
{Key: "test", String: "passed"}, |
| 48 |
} |
| 49 |
expected := []zapcore.Field{ |
| 50 |
{Key: "original", String: "context"}, |
| 51 |
{Key: "test", String: "passed"}, |
| 52 |
} |
| 53 |
newCC := cc.With(inputs) |
| 54 |
assert.Equal(t, expected, newCC.(*collectorCore).loadDelegate().(*bufferedCore).context) |
| 55 |
} |
| 56 |
|
| 57 |
func Test_collectorCore_Write(t *testing.T) { |
| 58 |
cc := newCollectorCore(newBufferedCore(zapcore.InfoLevel)) |
| 59 |
e := zapcore.Entry{ |
| 60 |
Level: zapcore.DebugLevel, |
| 61 |
Message: "test", |
| 62 |
} |
| 63 |
fields := []zapcore.Field{ |
| 64 |
{Key: "field1", String: "value1"}, |
| 65 |
} |
| 66 |
err := cc.Write(e, fields) |
| 67 |
require.NoError(t, err) |
| 68 |
|
| 69 |
expected := loggedEntry{ |
| 70 |
e, |
| 71 |
fields, |
| 72 |
} |
| 73 |
require.Len(t, cc.loadDelegate().(*bufferedCore).logs, 1) |
| 74 |
require.Equal(t, expected, cc.loadDelegate().(*bufferedCore).logs[0]) |
| 75 |
} |
| 76 |
|
| 77 |
func Test_collectorCore_Sync(t *testing.T) { |
| 78 |
cc := newCollectorCore(newBufferedCore(zapcore.InfoLevel)) |
| 79 |
assert.NoError(t, cc.Sync()) |
| 80 |
} |
| 81 |
|
| 82 |
func Test_collectorCore_SetCore(t *testing.T) { |
| 83 |
cc := newCollectorCore(newBufferedCore(zapcore.InfoLevel)) |
| 84 |
newCore := newBufferedCore(zapcore.DebugLevel) |
| 85 |
cc.SetCore(newCore) |
| 86 |
assert.Equal(t, zapcore.DebugLevel, cc.loadDelegate().(*bufferedCore).LevelEnabler) |
| 87 |
} |