Consul is a distributed, highly available, and data center aware solution to connect and configure applications across dynamic, distributed infrastructure.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.5 KiB

// 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>
1 year ago
// SPDX-License-Identifier: BUSL-1.1
package logging
import (
"os"
"path/filepath"
"sort"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/hashicorp/consul/sdk/testutil"
)
func TestLogFile_Rotation_MaxDuration(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}
tempDir := testutil.TempDir(t, "")
logFile := LogFile{
fileName: "consul.log",
logPath: tempDir,
duration: 50 * time.Millisecond,
}
logFile.Write([]byte("Hello World"))
time.Sleep(3 * logFile.duration)
logFile.Write([]byte("Second File"))
require.Len(t, listDir(t, tempDir), 2)
}
func TestLogFile_openNew(t *testing.T) {
logFile := LogFile{
fileName: "consul.log",
logPath: testutil.TempDir(t, ""),
duration: defaultRotateDuration,
}
err := logFile.openNew()
require.NoError(t, err)
msg := "[INFO] Something"
_, err = logFile.Write([]byte(msg))
require.NoError(t, err)
content, err := os.ReadFile(logFile.FileInfo.Name())
require.NoError(t, err)
require.Contains(t, string(content), msg)
}
func TestLogFile_renameCurrentFile(t *testing.T) {
logFile := LogFile{
fileName: "consul.log",
logPath: testutil.TempDir(t, ""),
duration: defaultRotateDuration,
}
err := logFile.openNew()
require.NoError(t, err)
err = logFile.renameCurrentFile()
require.NoError(t, err)
_, err = os.ReadFile(logFile.FileInfo.Name())
require.Contains(t, err.Error(), "no such file or directory")
}
func TestLogFile_Rotation_MaxBytes(t *testing.T) {
tempDir := testutil.TempDir(t, "LogWriterBytes")
logFile := LogFile{
fileName: "somefile.log",
logPath: tempDir,
MaxBytes: 10,
duration: defaultRotateDuration,
}
logFile.Write([]byte("Hello World"))
logFile.Write([]byte("Second File"))
require.Len(t, listDir(t, tempDir), 2)
}
func TestLogFile_PruneFiles(t *testing.T) {
tempDir := testutil.TempDir(t, t.Name())
logFile := LogFile{
fileName: "consul.log",
logPath: tempDir,
MaxBytes: 10,
duration: defaultRotateDuration,
MaxFiles: 1,
}
logFile.Write([]byte("[INFO] Hello World"))
logFile.Write([]byte("[INFO] Second File"))
logFile.Write([]byte("[INFO] Third File"))
logFiles := listDir(t, tempDir)
sort.Strings(logFiles)
require.Len(t, logFiles, 2)
content, err := os.ReadFile(filepath.Join(tempDir, logFiles[0]))
require.NoError(t, err)
require.Contains(t, string(content), "Second File")
content, err = os.ReadFile(filepath.Join(tempDir, logFiles[1]))
require.NoError(t, err)
require.Contains(t, string(content), "Third File")
}
func TestLogFile_PruneFiles_Disabled(t *testing.T) {
tempDir := testutil.TempDir(t, t.Name())
logFile := LogFile{
fileName: "somename.log",
logPath: tempDir,
MaxBytes: 10,
duration: defaultRotateDuration,
MaxFiles: 0,
}
logFile.Write([]byte("[INFO] Hello World"))
logFile.Write([]byte("[INFO] Second File"))
logFile.Write([]byte("[INFO] Third File"))
require.Len(t, listDir(t, tempDir), 3)
}
func TestLogFile_FileRotation_Disabled(t *testing.T) {
tempDir := testutil.TempDir(t, t.Name())
logFile := LogFile{
fileName: "consul.log",
logPath: tempDir,
MaxBytes: 10,
MaxFiles: -1,
}
logFile.Write([]byte("[INFO] Hello World"))
logFile.Write([]byte("[INFO] Second File"))
logFile.Write([]byte("[INFO] Third File"))
require.Len(t, listDir(t, tempDir), 1)
}
func listDir(t *testing.T, name string) []string {
t.Helper()
fh, err := os.Open(name)
require.NoError(t, err)
files, err := fh.Readdirnames(100)
require.NoError(t, err)
return files
}