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