consul/logging/syslog_test.go

81 lines
2.7 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * 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 <Blog URL>, 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 BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-11 13:12:13 +00:00
// SPDX-License-Identifier: BUSL-1.1
//go:build linux || darwin || dragonfly || freebsd || netbsd || openbsd || solaris
// +build linux darwin dragonfly freebsd netbsd openbsd solaris
package logging
import (
"testing"
gsyslog "github.com/hashicorp/go-syslog"
"github.com/stretchr/testify/require"
)
// testSyslogFunc is a wrapper for injecting WriteLevel functionality into a Syslogger
// implementation; it gives us a way to inject test assertions into a SyslogWrapper.
type testSyslogWriteLevelFunc func(p gsyslog.Priority, m []byte) error
// Write is a no-op
func (f testSyslogWriteLevelFunc) Write(p []byte) (int, error) {
return 0, nil
}
// WriteLevel is a wrapper for the identity to be called
func (f testSyslogWriteLevelFunc) WriteLevel(p gsyslog.Priority, m []byte) error {
return f(p, m)
}
// Close is a no-op
func (f testSyslogWriteLevelFunc) Close() error {
return nil
}
func TestSyslog_Wrapper(t *testing.T) {
expectedMsg := []byte("test message")
// generator for a writer that expects to be written with expectedPriority
writerForPriority := func(expectedPriority gsyslog.Priority) testSyslogWriteLevelFunc {
return func(p gsyslog.Priority, m []byte) error {
require.Equal(t, expectedPriority, p)
require.Equal(t, expectedMsg, m)
return nil
}
}
// [TRACE] is mapped to the LOG_DEBUG priority
debugw := &SyslogWrapper{l: writerForPriority(gsyslog.LOG_DEBUG)}
_, err := debugw.Write([]byte("[TRACE] " + string(expectedMsg)))
require.NoError(t, err)
// [DEBUG] is mapped to the LOG_INFO priority
infow := &SyslogWrapper{l: writerForPriority(gsyslog.LOG_INFO)}
_, err = infow.Write([]byte("[DEBUG] " + string(expectedMsg)))
require.NoError(t, err)
// [INFO] is mapped to the LOG_NOTICE priority
noticew := &SyslogWrapper{l: writerForPriority(gsyslog.LOG_NOTICE)}
_, err = noticew.Write([]byte("[INFO] " + string(expectedMsg)))
require.NoError(t, err)
// [WARN] is mapped to the LOG_WARNING priority
warnw := &SyslogWrapper{l: writerForPriority(gsyslog.LOG_WARNING)}
_, err = warnw.Write([]byte("[WARN] " + string(expectedMsg)))
require.NoError(t, err)
// [ERROR] is mapped to the LOG_ERR priority
errorw := &SyslogWrapper{l: writerForPriority(gsyslog.LOG_ERR)}
_, err = errorw.Write([]byte("[ERROR] " + string(expectedMsg)))
require.NoError(t, err)
// [CRIT] is mapped to the LOG_CRIT priority
critw := &SyslogWrapper{l: writerForPriority(gsyslog.LOG_CRIT)}
_, err = critw.Write([]byte("[CRIT] " + string(expectedMsg)))
require.NoError(t, err)
// unknown levels are written with LOG_NOTICE priority
defaultw := &SyslogWrapper{l: writerForPriority(gsyslog.LOG_NOTICE)}
_, err = defaultw.Write([]byte("[INVALID] " + string(expectedMsg)))
require.NoError(t, err)
}