File: log_test.go

package info (click to toggle)
nats-server 2.12.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,452 kB
  • sloc: sh: 720; makefile: 3
file content (131 lines) | stat: -rw-r--r-- 2,892 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package test

import (
	"fmt"
	"os"
	"path/filepath"
	"runtime"
	"testing"
	"time"

	"github.com/nats-io/nats-server/v2/server"
)

func RunServerWithLogging(opts *server.Options) *server.Server {
	if opts == nil {
		opts = &DefaultTestOptions
	}
	opts.NoLog = false
	opts.Cluster.PoolSize = -1
	opts.Cluster.Compression.Mode = server.CompressionOff
	opts.LeafNode.Compression.Mode = server.CompressionOff
	s, err := server.NewServer(opts)
	if err != nil || s == nil {
		panic(fmt.Sprintf("No NATS Server object returned: %v", err))
	}
	s.ConfigureLogger()
	go s.Start()
	if !s.ReadyForConnections(10 * time.Second) {
		panic("Unable to start NATS Server in Go Routine")
	}
	return s
}

func TestLogMaxArchives(t *testing.T) {
	// With logfile_size_limit set to small 100 characters, plain startup rotates 8 times
	for _, test := range []struct {
		name               string
		config             string
		totEntriesExpected int
	}{
		{
			"Default implicit, no max logs, expect 0 purged logs",
			`
				port: -1
				log_file: %s
				logfile_size_limit: 100
			`,
			9,
		},
		{
			"Default explicit, no max logs, expect 0 purged logs",
			`
				port: -1
				log_file: %s
				logfile_size_limit: 100
				logfile_max_num: 0
			`,
			9,
		},
		{
			"Default explicit - negative val, no max logs, expect 0 purged logs",
			`
				port: -1
				log_file: %s
				logfile_size_limit: 100
				logfile_max_num: -42
			`,
			9,
		},
		{
			"1-max num, expect 8 purged logs",
			`
				port: -1
				log_file: %s
				logfile_size_limit: 100
				logfile_max_num: 1
			`,
			1,
		},
		{
			"5-max num, expect 4 purged logs; use opt alias",
			`
				port: -1
				log_file: %s
				log_size_limit: 100
				log_max_num: 5
			`,
			5,
		},
		{
			"100-max num, expect 0 purged logs",
			`
				port: -1
				log_file: %s
				logfile_size_limit: 100
				logfile_max_num: 100
			`,
			9,
		},
	} {
		t.Run(test.name, func(t *testing.T) {
			d, err := os.MkdirTemp("", "logtest")
			if err != nil {
				t.Fatalf("Error creating temp dir: %v", err)
			}
			content := fmt.Sprintf(test.config, filepath.Join(d, "nats-server.log"))
			// server config does not like plain windows backslash
			if runtime.GOOS == "windows" {
				content = filepath.ToSlash(content)
			}
			opts, err := server.ProcessConfigFile(createConfFile(t, []byte(content)))
			if err != nil {
				t.Fatalf("Error processing config file: %v", err)
			}
			s := RunServerWithLogging(opts)
			if s == nil {
				t.Fatalf("No NATS Server object returned")
			}
			s.Shutdown()
			// Windows filesystem can be a little pokey on the flush, so wait a bit after shutdown...
			time.Sleep(500 * time.Millisecond)
			entries, err := os.ReadDir(d)
			if err != nil {
				t.Fatalf("Error reading dir: %v", err)
			}
			if len(entries) != test.totEntriesExpected {
				t.Fatalf("Expected %d log files, got %d", test.totEntriesExpected, len(entries))
			}
		})
	}
}