1// Copyright (c) 2012 - Cloud Instruments Co., Ltd. 2// 3// All rights reserved. 4// 5// Redistribution and use in source and binary forms, with or without 6// modification, are permitted provided that the following conditions are met: 7// 8// 1. Redistributions of source code must retain the above copyright notice, this 9// list of conditions and the following disclaimer. 10// 2. Redistributions in binary form must reproduce the above copyright notice, 11// this list of conditions and the following disclaimer in the documentation 12// and/or other materials provided with the distribution. 13// 14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 25package seelog 26 27import ( 28 "fmt" 29 "io" 30 "os" 31 "path/filepath" 32 "strings" 33 "testing" 34) 35 36const ( 37 messageLen = 10 38) 39 40var bytesFileTest = []byte(strings.Repeat("A", messageLen)) 41 42func TestSimpleFileWriter(t *testing.T) { 43 t.Logf("Starting file writer tests") 44 NewFileWriterTester(simplefileWriterTests, simplefileWriterGetter, t).test() 45} 46 47//=============================================================== 48 49func simplefileWriterGetter(testCase *fileWriterTestCase) (io.WriteCloser, error) { 50 return NewFileWriter(testCase.fileName) 51} 52 53//=============================================================== 54type fileWriterTestCase struct { 55 files []string 56 fileName string 57 rollingType rollingType 58 fileSize int64 59 maxRolls int 60 datePattern string 61 writeCount int 62 resFiles []string 63 nameMode rollingNameMode 64 archiveType rollingArchiveType 65 archiveExploded bool 66 archivePath string 67} 68 69func createSimplefileWriterTestCase(fileName string, writeCount int) *fileWriterTestCase { 70 return &fileWriterTestCase{[]string{}, fileName, rollingTypeSize, 0, 0, "", writeCount, []string{fileName}, 0, rollingArchiveNone, false, ""} 71} 72 73var simplefileWriterTests = []*fileWriterTestCase{ 74 createSimplefileWriterTestCase("log.testlog", 1), 75 createSimplefileWriterTestCase("log.testlog", 50), 76 createSimplefileWriterTestCase(filepath.Join("dir", "log.testlog"), 50), 77} 78 79//=============================================================== 80 81type fileWriterTester struct { 82 testCases []*fileWriterTestCase 83 writerGetter func(*fileWriterTestCase) (io.WriteCloser, error) 84 t *testing.T 85} 86 87func NewFileWriterTester( 88 testCases []*fileWriterTestCase, 89 writerGetter func(*fileWriterTestCase) (io.WriteCloser, error), 90 t *testing.T) *fileWriterTester { 91 92 return &fileWriterTester{testCases, writerGetter, t} 93} 94 95func isWriterTestFile(fn string) bool { 96 return strings.Contains(fn, ".testlog") || strings.Contains(fn, ".zip") || strings.Contains(fn, ".gz") 97} 98 99func cleanupWriterTest(t *testing.T) { 100 toDel, err := getDirFilePaths(".", isWriterTestFile, true) 101 if nil != err { 102 t.Fatal("Cannot list files in test directory!") 103 } 104 105 for _, p := range toDel { 106 if err = tryRemoveFile(p); nil != err { 107 t.Errorf("cannot remove file %s in test directory: %s", p, err.Error()) 108 } 109 } 110 111 if err = os.RemoveAll("dir"); nil != err { 112 t.Errorf("cannot remove temp test directory: %s", err.Error()) 113 } 114} 115 116func getWriterTestResultFiles() ([]string, error) { 117 var p []string 118 119 visit := func(path string, f os.FileInfo, err error) error { 120 if !f.IsDir() && isWriterTestFile(path) { 121 abs, err := filepath.Abs(path) 122 if err != nil { 123 return fmt.Errorf("filepath.Abs failed for %s", path) 124 } 125 126 p = append(p, abs) 127 } 128 129 return nil 130 } 131 132 err := filepath.Walk(".", visit) 133 if nil != err { 134 return nil, err 135 } 136 137 return p, nil 138} 139 140func (tester *fileWriterTester) testCase(testCase *fileWriterTestCase, testNum int) { 141 defer cleanupWriterTest(tester.t) 142 143 tester.t.Logf("Start test [%v]\n", testNum) 144 145 for _, filePath := range testCase.files { 146 dir, _ := filepath.Split(filePath) 147 148 var err error 149 150 if 0 != len(dir) { 151 err = os.MkdirAll(dir, defaultDirectoryPermissions) 152 if err != nil { 153 tester.t.Error(err) 154 return 155 } 156 } 157 158 fi, err := os.Create(filePath) 159 if err != nil { 160 tester.t.Error(err) 161 return 162 } 163 164 err = fi.Close() 165 if err != nil { 166 tester.t.Error(err) 167 return 168 } 169 } 170 171 fwc, err := tester.writerGetter(testCase) 172 if err != nil { 173 tester.t.Error(err) 174 return 175 } 176 defer fwc.Close() 177 178 tester.performWrite(fwc, testCase.writeCount) 179 180 files, err := getWriterTestResultFiles() 181 if err != nil { 182 tester.t.Error(err) 183 return 184 } 185 186 tester.checkRequiredFilesExist(testCase, files) 187 tester.checkJustRequiredFilesExist(testCase, files) 188 189} 190 191func (tester *fileWriterTester) test() { 192 for i, tc := range tester.testCases { 193 cleanupWriterTest(tester.t) 194 tester.testCase(tc, i) 195 } 196} 197 198func (tester *fileWriterTester) performWrite(fileWriter io.Writer, count int) { 199 for i := 0; i < count; i++ { 200 _, err := fileWriter.Write(bytesFileTest) 201 202 if err != nil { 203 tester.t.Error(err) 204 return 205 } 206 } 207} 208 209func (tester *fileWriterTester) checkRequiredFilesExist(testCase *fileWriterTestCase, files []string) { 210 var found bool 211 for _, expected := range testCase.resFiles { 212 found = false 213 exAbs, err := filepath.Abs(expected) 214 if err != nil { 215 tester.t.Errorf("filepath.Abs failed for %s", expected) 216 continue 217 } 218 219 for _, f := range files { 220 if af, e := filepath.Abs(f); e == nil { 221 tester.t.Log(af) 222 if exAbs == af { 223 found = true 224 break 225 } 226 } else { 227 tester.t.Errorf("filepath.Abs failed for %s", f) 228 } 229 } 230 231 if !found { 232 tester.t.Errorf("expected file: %s doesn't exist. Got %v\n", exAbs, files) 233 } 234 } 235} 236 237func (tester *fileWriterTester) checkJustRequiredFilesExist(testCase *fileWriterTestCase, files []string) { 238 for _, f := range files { 239 found := false 240 for _, expected := range testCase.resFiles { 241 242 exAbs, err := filepath.Abs(expected) 243 if err != nil { 244 tester.t.Errorf("filepath.Abs failed for %s", expected) 245 } else { 246 if exAbs == f { 247 found = true 248 break 249 } 250 } 251 } 252 253 if !found { 254 tester.t.Errorf("unexpected file: %v", f) 255 } 256 } 257} 258