github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/logconfig/collector_test.go (about) 1 /* 2 * Copyright (C) 2019 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package logconfig 19 20 import ( 21 "os" 22 "path" 23 "testing" 24 "time" 25 26 "github.com/rs/zerolog" 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestCollector_List_ListsAllLogFilesMatchingPattern(t *testing.T) { 31 assert := assert.New(t) 32 33 // given 34 baseName := "mysterium-test" 35 dn1 := NewTempDirName(t, "") 36 logFilename := baseName + ".log" 37 f1, err := os.Create(path.Join(dn1, logFilename)) 38 assert.NoError(err) 39 defer os.Remove(f1.Name()) 40 41 fn2 := NewTempFileName(t, dn1, logFilename+".gz") 42 defer os.Remove(fn2) 43 44 fn3 := NewTempFileName(t, dn1, logFilename+".gz") 45 defer os.Remove(fn3) 46 47 // ensure this is the file with the most recent modified time 48 time.Sleep(time.Millisecond * 10) 49 fn4 := NewTempFileName(t, dn1, logFilename+".gz") 50 defer os.Remove(fn4) 51 52 opts := LogOptions{ 53 LogLevel: zerolog.DebugLevel, 54 Filepath: path.Join(path.Dir(f1.Name()), baseName), 55 } 56 collector := NewCollector(&opts) 57 58 // when 59 logFiles, err := collector.logFilepaths() 60 61 // then 62 assert.NoError(err) 63 assert.Contains(logFiles, f1.Name()) 64 assert.Contains(logFiles, fn4) 65 assert.Len(logFiles, 2) 66 } 67 68 func TestCollector_Archive(t *testing.T) { 69 assert := assert.New(t) 70 71 // given 72 baseName := "mysterium-test" 73 dn1 := NewTempDirName(t, "") 74 logFilename := baseName + ".log" 75 f1, err := os.Create(path.Join(dn1, logFilename)) 76 assert.NoError(err) 77 defer os.Remove(f1.Name()) 78 79 fn2 := NewTempFileName(t, dn1, logFilename+".gz") 80 defer os.Remove(fn2) 81 82 opts := LogOptions{ 83 LogLevel: zerolog.DebugLevel, 84 Filepath: path.Join(path.Dir(f1.Name()), baseName), 85 } 86 collector := NewCollector(&opts) 87 88 // when 89 zipFilename, err := collector.Archive() 90 defer os.Remove(zipFilename) 91 92 // then 93 assert.NoError(err) 94 assert.NotEmpty(zipFilename) 95 } 96 97 func NewTempFileName(t *testing.T, dir, pattern string) string { 98 file, err := os.CreateTemp(dir, pattern) 99 assert.NoError(t, err) 100 return file.Name() 101 } 102 103 func NewTempDirName(t *testing.T, pattern string) string { 104 dir, err := os.MkdirTemp("", pattern) 105 assert.NoError(t, err) 106 return dir 107 }