mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-14 10:37:00 +02:00
* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License. Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUS-1.1 * Fix test that expected exact offset on hcl file --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Sarah Thompson <sthompson@hashicorp.com> Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
141 lines
2.7 KiB
Go
141 lines
2.7 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package monitor
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestMonitor_Start(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
logger := log.NewInterceptLogger(&log.LoggerOptions{
|
|
Level: log.Error,
|
|
})
|
|
|
|
m, _ := NewMonitor(512, logger, &log.LoggerOptions{
|
|
Level: log.Debug,
|
|
})
|
|
|
|
logCh := m.Start()
|
|
defer m.Stop()
|
|
|
|
go func() {
|
|
logger.Debug("test log")
|
|
time.Sleep(10 * time.Millisecond)
|
|
}()
|
|
|
|
select {
|
|
case l := <-logCh:
|
|
require.Contains(t, string(l), "[DEBUG] test log")
|
|
return
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("Expected to receive from log channel")
|
|
}
|
|
}
|
|
|
|
func TestMonitor_JSONFormat(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
logger := log.NewInterceptLogger(&log.LoggerOptions{
|
|
Level: log.Error,
|
|
})
|
|
|
|
m, _ := NewMonitor(512, logger, &log.LoggerOptions{
|
|
Level: log.Debug,
|
|
JSONFormat: true,
|
|
})
|
|
|
|
type jsonlog struct {
|
|
Level string `json:"@level"`
|
|
Message string `json:"@message"`
|
|
TimeStamp string `json:"@timestamp"`
|
|
}
|
|
jsonLog := &jsonlog{}
|
|
|
|
logCh := m.Start()
|
|
defer m.Stop()
|
|
|
|
go func() {
|
|
logger.Debug("test json log")
|
|
time.Sleep(10 * time.Millisecond)
|
|
}()
|
|
|
|
select {
|
|
case l := <-logCh:
|
|
err := json.Unmarshal(l, jsonLog)
|
|
if err != nil {
|
|
t.Fatal("Expected JSON log from channel")
|
|
}
|
|
require.Contains(t, jsonLog.Message, "test json log")
|
|
return
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("Expected to receive from log channel")
|
|
}
|
|
}
|
|
|
|
func TestMonitor_Start_Unbuffered(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
logger := log.NewInterceptLogger(&log.LoggerOptions{
|
|
Level: log.Error,
|
|
})
|
|
|
|
_, err := NewMonitor(0, logger, &log.LoggerOptions{
|
|
Level: log.Debug,
|
|
})
|
|
|
|
if err == nil {
|
|
t.Fatal("expected to get an error, but didn't")
|
|
} else {
|
|
if !strings.Contains(err.Error(), "greater than zero") {
|
|
t.Fatal("expected an error about buf being greater than zero")
|
|
}
|
|
}
|
|
}
|
|
|
|
// Ensure number of dropped messages are logged
|
|
func TestMonitor_DroppedMessages(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
logger := log.NewInterceptLogger(&log.LoggerOptions{
|
|
Level: log.Warn,
|
|
})
|
|
|
|
m, _ := newMonitor(5, logger, &log.LoggerOptions{
|
|
Level: log.Debug,
|
|
})
|
|
m.dropCheckInterval = 5 * time.Millisecond
|
|
|
|
logCh := m.Start()
|
|
defer m.Stop()
|
|
|
|
for i := 0; i <= 100; i++ {
|
|
logger.Debug(fmt.Sprintf("test message %d", i))
|
|
}
|
|
|
|
passed := make(chan struct{})
|
|
go func() {
|
|
for recv := range logCh {
|
|
if strings.Contains(string(recv), "Monitor dropped") {
|
|
close(passed)
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
select {
|
|
case <-passed:
|
|
case <-time.After(2 * time.Second):
|
|
require.Fail(t, "expected to see warn dropped messages")
|
|
}
|
|
}
|