forked from templexxx/logro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_test.go
187 lines (154 loc) · 3.67 KB
/
backup_test.go
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright (c) 2019. Temple3x (temple3x@gmail.com)
* Copyright (c) 2014 Nate Finch
*
* Use of this source code is governed by the MIT License
* that can be found in the LICENSE file.
*/
package logro
import (
"container/heap"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"testing"
"time"
)
func TestBackups_Heap(t *testing.T) {
s := make([]Backup, 0, 3)
b := &Backups{bs: s}
if heap.Pop(b) != nil || b.Len() != 0 {
t.Fatal("should be empty")
}
for i := 4; i >= 0; i-- {
heap.Push(b, Backup{ts: int64(i), fp: strconv.Itoa(i)})
}
if b.Len() != 5 {
t.Fatal("len mismatch")
}
i := 0
for {
val := heap.Pop(b) // Pop min.
if val == nil {
break
}
v := val.(Backup)
if v.ts != int64(i) || v.fp != strconv.Itoa(i) {
t.Fatal("value mismatch", v.ts, i)
}
i++
}
}
func TestListBackups(t *testing.T) {
testListBackupsPathError(t, 2)
for i := 0; i < 3*2; i++ {
testListBackups(t, i, 2)
}
}
func testListBackupsPathError(t *testing.T, maxBackups int) {
dir, err := ioutil.TempDir(os.TempDir(), "")
if err != nil {
t.Fatal(err)
}
os.RemoveAll(dir)
fn := "logro-test.log"
output := filepath.Join(dir, fn)
b, err := listBackups(output, maxBackups)
if err == nil || b != nil {
t.Fatal("should raise path error")
}
}
func makeBackups(output string, n int) (TSs []int64, err error) {
TSs = make([]int64, n)
now := time.Now()
for i := 0; i < n; i++ {
fn, ts := makeBackupFP(output, false, now.Add(time.Second*time.Duration(int64(i))))
TSs[i] = ts
_, err = os.Create(fn)
if err != nil {
return
}
}
// Create some illegal backup log file/dir.
// listBackups should ignore them.
os.Mkdir(filepath.Join(filepath.Dir(output), "dir"), 0755)
os.Create(filepath.Join(filepath.Dir(output), "c.log"))
os.Create(filepath.Join(filepath.Dir(output), "a-c"))
os.Create(filepath.Join(filepath.Dir(output), "a-c.log"))
os.Create(filepath.Join(filepath.Dir(output), "logro-test-a.logx"))
os.Create(filepath.Join(filepath.Dir(output), "logro-test-a.log"))
return
}
func testListBackups(t *testing.T, i, maxBackups int) {
dir, err := ioutil.TempDir(os.TempDir(), "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
fn := "logro-test.log"
output := filepath.Join(dir, fn)
TSs, err := makeBackups(output, i)
if err != nil {
t.Fatal(err)
}
b, err := listBackups(output, maxBackups)
if err != nil {
t.Fatal(err)
}
cnt := maxBackups
if len(TSs) < maxBackups {
cnt = len(TSs)
}
if b.Len() != cnt {
t.Fatal("mismatch backups len")
}
TSs = TSs[len(TSs)-cnt:]
for _, ts := range TSs {
val := heap.Pop(b)
if val == nil {
break
}
if val.(Backup).ts != ts {
t.Fatal("mismatch backup ts")
}
}
}
func TestGetPrefixAndExt(t *testing.T) {
output := "a/b.log"
prefix, ext := getPrefixAndExt(output)
if prefix != "b-" {
t.Fatal("prefix mismatch")
}
if ext != ".log" {
t.Fatal("ext mismatch")
}
}
func TestMakeBackupFP(t *testing.T) {
now := time.Now()
fnBase := "logro-test"
fnExt := ".log"
fn := fnBase + fnExt
// Test Make.
utc := fmt.Sprintf("%s-%s%s", fnBase, now.UTC().Format(backupTimeFmt), fnExt)
actFP, actTS := makeBackupFP(fn, false, now)
if actFP != utc || actTS != now.Unix() {
t.Fatal("make: mismatch UTC time")
}
local := fmt.Sprintf("%s-%s%s", fnBase, now.Format(backupTimeFmt), fnExt)
actFP, actTS = makeBackupFP(fn, true, now)
if actFP != local || actTS != now.Unix() {
t.Fatal("make: mismatch Local time")
}
// Test Parse Time
prefix, ext := getPrefixAndExt(fn)
actTS = parseTime(utc, prefix, ext)
if actTS != now.Unix() {
t.Fatal("parse: mismatch UTC time")
}
actTS = parseTime(local, prefix, ext)
if actTS != now.Unix() {
t.Fatal("parse: mismatch Local time")
}
}