mirror of https://github.com/hashicorp/boundary
Event log adpaters (#1434)
parent
68dd2a66ba
commit
137729b0c0
@ -0,0 +1,113 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/observability/event"
|
||||
"github.com/hashicorp/eventlogger/formatter_filters/cloudevents"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_oidcLogger_Errorf(t *testing.T) {
|
||||
// this test and its subtests cannot be run in parallel because of it's
|
||||
// dependency on the sysEventer
|
||||
|
||||
c := event.TestEventerConfig(t, "TestoidcLogger_Errorf")
|
||||
testLock := &sync.Mutex{}
|
||||
testLogger := hclog.New(&hclog.LoggerOptions{
|
||||
Mutex: testLock,
|
||||
Name: "test",
|
||||
})
|
||||
require.NoError(t, event.InitSysEventer(testLogger, testLock, "use-TestoidcLogger_Errorf", event.WithEventerConfig(&c.EventerConfig)))
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
fmt string
|
||||
args []interface{}
|
||||
}{
|
||||
{
|
||||
name: "no-args",
|
||||
fmt: "simple",
|
||||
},
|
||||
{
|
||||
name: "with-args",
|
||||
fmt: "%s: simple",
|
||||
args: []interface{}{"error"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
l := &oidcLogger{}
|
||||
l.Errorf(tt.fmt, tt.args...)
|
||||
sinkFileName := c.AllEvents.Name()
|
||||
defer func() { _ = os.WriteFile(sinkFileName, nil, 0o666) }()
|
||||
b, err := ioutil.ReadFile(sinkFileName)
|
||||
require.NoError(err)
|
||||
gotEvent := &cloudevents.Event{}
|
||||
err = json.Unmarshal(b, gotEvent)
|
||||
require.NoErrorf(err, "json: %s", string(b))
|
||||
expected := gotEvent.Data.(map[string]interface{})
|
||||
expected["error"] = fmt.Sprintf(tt.fmt, tt.args...)
|
||||
assert.Equal(expected, gotEvent.Data.(map[string]interface{}))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_oidcLogger_Infof(t *testing.T) {
|
||||
// this test and its subtests cannot be run in parallel because of it's
|
||||
// dependency on the sysEventer
|
||||
|
||||
c := event.TestEventerConfig(t, "TestoidcLogger_Errorf")
|
||||
testLock := &sync.Mutex{}
|
||||
testLogger := hclog.New(&hclog.LoggerOptions{
|
||||
Mutex: testLock,
|
||||
Name: "test",
|
||||
})
|
||||
require.NoError(t, event.InitSysEventer(testLogger, testLock, "use-TestoidcLogger_Errorf", event.WithEventerConfig(&c.EventerConfig)))
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
fmt string
|
||||
args []interface{}
|
||||
}{
|
||||
{
|
||||
name: "no-args",
|
||||
fmt: "simple",
|
||||
},
|
||||
{
|
||||
name: "with-args",
|
||||
fmt: "%s: simple",
|
||||
args: []interface{}{"info"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
l := &oidcLogger{}
|
||||
l.Infof(tt.fmt, tt.args...)
|
||||
sinkFileName := c.AllEvents.Name()
|
||||
defer func() { _ = os.WriteFile(sinkFileName, nil, 0o666) }()
|
||||
b, err := ioutil.ReadFile(sinkFileName)
|
||||
require.NoError(err)
|
||||
gotEvent := &cloudevents.Event{}
|
||||
err = json.Unmarshal(b, gotEvent)
|
||||
require.NoErrorf(err, "json: %s", string(b))
|
||||
expected := gotEvent.Data.(map[string]interface{})
|
||||
expected["msg"] = fmt.Sprintf(tt.fmt, tt.args...)
|
||||
assert.Equal(expected, gotEvent.Data.(map[string]interface{}))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_oidcLogger_FailNow(t *testing.T) {
|
||||
l := &oidcLogger{}
|
||||
assert.Panics(t, func() { l.FailNow() }, "sys eventer failed, see logs for output (if any)")
|
||||
}
|
||||
Loading…
Reference in new issue