| 1 |
package test |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
"path/filepath" |
| 7 |
"runtime" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/nats-io/nats-server/v2/server" |
| 12 |
) |
| 13 |
|
| 14 |
func RunServerWithLogging(opts *server.Options) *server.Server { |
| 15 |
if opts == nil { |
| 16 |
dto := DefaultTestOptions |
| 17 |
opts = &dto |
| 18 |
} |
| 19 |
opts.NoLog = false |
| 20 |
opts.Cluster.PoolSize = -1 |
| 21 |
opts.Cluster.Compression.Mode = server.CompressionOff |
| 22 |
opts.LeafNode.Compression.Mode = server.CompressionOff |
| 23 |
s, err := server.NewServer(opts) |
| 24 |
if err != nil || s == nil { |
| 25 |
panic(fmt.Sprintf("No NATS Server object returned: %v", err)) |
| 26 |
} |
| 27 |
s.ConfigureLogger() |
| 28 |
go s.Start() |
| 29 |
if !s.ReadyForConnections(10 * time.Second) { |
| 30 |
panic("Unable to start NATS Server in Go Routine") |
| 31 |
} |
| 32 |
return s |
| 33 |
} |
| 34 |
|
| 35 |
func TestLogMaxArchives(t *testing.T) { |
| 36 |
// With logfile_size_limit set to small 100 characters, plain startup rotates 8 times |
| 37 |
for _, test := range []struct { |
| 38 |
name string |
| 39 |
config string |
| 40 |
totEntriesExpected int |
| 41 |
}{ |
| 42 |
{ |
| 43 |
"Default implicit, no max logs, expect 0 purged logs", |
| 44 |
` |
| 45 |
port: -1 |
| 46 |
log_file: %s |
| 47 |
logfile_size_limit: 100 |
| 48 |
`, |
| 49 |
9, |
| 50 |
}, |
| 51 |
{ |
| 52 |
"Default explicit, no max logs, expect 0 purged logs", |
| 53 |
` |
| 54 |
port: -1 |
| 55 |
log_file: %s |
| 56 |
logfile_size_limit: 100 |
| 57 |
logfile_max_num: 0 |
| 58 |
`, |
| 59 |
9, |
| 60 |
}, |
| 61 |
{ |
| 62 |
"Default explicit - negative val, no max logs, expect 0 purged logs", |
| 63 |
` |
| 64 |
port: -1 |
| 65 |
log_file: %s |
| 66 |
logfile_size_limit: 100 |
| 67 |
logfile_max_num: -42 |
| 68 |
`, |
| 69 |
9, |
| 70 |
}, |
| 71 |
{ |
| 72 |
"1-max num, expect 8 purged logs", |
| 73 |
` |
| 74 |
port: -1 |
| 75 |
log_file: %s |
| 76 |
logfile_size_limit: 100 |
| 77 |
logfile_max_num: 1 |
| 78 |
`, |
| 79 |
1, |
| 80 |
}, |
| 81 |
{ |
| 82 |
"5-max num, expect 4 purged logs; use opt alias", |
| 83 |
` |
| 84 |
port: -1 |
| 85 |
log_file: %s |
| 86 |
log_size_limit: 100 |
| 87 |
log_max_num: 5 |
| 88 |
`, |
| 89 |
5, |
| 90 |
}, |
| 91 |
{ |
| 92 |
"100-max num, expect 0 purged logs", |
| 93 |
` |
| 94 |
port: -1 |
| 95 |
log_file: %s |
| 96 |
logfile_size_limit: 100 |
| 97 |
logfile_max_num: 100 |
| 98 |
`, |
| 99 |
9, |
| 100 |
}, |
| 101 |
} { |
| 102 |
t.Run(test.name, func(t *testing.T) { |
| 103 |
d, err := os.MkdirTemp("", "logtest") |
| 104 |
if err != nil { |
| 105 |
t.Fatalf("Error creating temp dir: %v", err) |
| 106 |
} |
| 107 |
content := fmt.Sprintf(test.config, filepath.Join(d, "nats-server.log")) |
| 108 |
// server config does not like plain windows backslash |
| 109 |
if runtime.GOOS == "windows" { |
| 110 |
content = filepath.ToSlash(content) |
| 111 |
} |
| 112 |
opts, err := server.ProcessConfigFile(createConfFile(t, []byte(content))) |
| 113 |
if err != nil { |
| 114 |
t.Fatalf("Error processing config file: %v", err) |
| 115 |
} |
| 116 |
s := RunServerWithLogging(opts) |
| 117 |
if s == nil { |
| 118 |
t.Fatalf("No NATS Server object returned") |
| 119 |
} |
| 120 |
s.Shutdown() |
| 121 |
// Windows filesystem can be a little pokey on the flush, so wait a bit after shutdown... |
| 122 |
time.Sleep(500 * time.Millisecond) |
| 123 |
entries, err := os.ReadDir(d) |
| 124 |
if err != nil { |
| 125 |
t.Fatalf("Error reading dir: %v", err) |
| 126 |
} |
| 127 |
if len(entries) != test.totEntriesExpected { |
| 128 |
t.Fatalf("Expected %d log files, got %d", test.totEntriesExpected, len(entries)) |
| 129 |
} |
| 130 |
}) |
| 131 |
} |
| 132 |
} |