forked from servalproject/serval-dna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging
executable file
·200 lines (185 loc) · 7.52 KB
/
logging
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
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/bin/bash
# Tests for Serval DNA logging.
#
# Copyright 2013 Serval Project, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
source "${0%/*}/../testframework.sh"
source "${0%/*}/../testdefs.sh"
setup() {
setup_servald
}
doc_LogStderrDefault="By default, only errors and warnings are logged to stderr"
test_LogStderrDefault() {
execute $servald log error 'hoopla'
assertExitStatus '==' 0
assertStderrGrep --matches=1 '^ERROR:.*hoopla$'
executeOk_servald log warn 'buckle'
assertStderrGrep --matches=1 '^WARN:.*buckle$'
executeOk_servald log info 'lymph'
assertStderrGrep --matches=0 'lymph'
executeOk_servald log debug 'eccles'
assertStderrGrep --matches=0 'eccles'
}
doc_LogStderrConfigAll="Configure all messages logged to stderr"
test_LogStderrConfigAll() {
executeOk_servald config set log.console.level debug
execute $servald log error 'hoopla'
assertExitStatus '==' 0
assertStderrGrep --matches=1 '^ERROR:.*hoopla$'
executeOk_servald log warn 'buckle'
assertStderrGrep --matches=1 '^WARN:.*buckle$'
executeOk_servald log info 'lymph'
assertStderrGrep --matches=1 'INFO:.*lymph$'
executeOk_servald log debug 'eccles'
assertStderrGrep --matches=1 'DEBUG:.*eccles$'
}
doc_LogStderrConfigNone="Configure no messages logged to stderr"
test_LogStderrConfigNone() {
executeOk_servald config set log.console.level none
executeOk_servald log error 'hoopla'
assertStderrIs ''
executeOk_servald log warn 'buckle'
assertStderrIs ''
executeOk_servald log info 'lymph'
assertStderrIs ''
executeOk_servald log debug 'eccles'
assertStderrIs ''
}
doc_LogFileDefault="By Default, all messages are appended to a configured file"
test_LogFileDefault() {
executeOk_servald config set log.console.level none
executeOk_servald config set log.file.path "$PWD/log.txt"
executeOk_servald log error 'hoopla'
assertGrep --matches=1 log.txt '^ERROR:.*hoopla$'
executeOk_servald log warn 'buckle'
assertGrep --matches=1 log.txt '^ERROR:.*hoopla$'
assertGrep --matches=1 log.txt '^WARN:.*buckle$'
executeOk_servald log info 'lymph'
assertGrep --matches=1 log.txt '^ERROR:.*hoopla$'
assertGrep --matches=1 log.txt '^WARN:.*buckle$'
assertGrep --matches=1 log.txt 'INFO:.*lymph$'
executeOk_servald log debug 'eccles'
assertGrep --matches=1 log.txt '^ERROR:.*hoopla$'
assertGrep --matches=1 log.txt '^WARN:.*buckle$'
assertGrep --matches=1 log.txt 'INFO:.*lymph$'
assertGrep --matches=1 log.txt 'DEBUG:.*eccles$'
}
doc_LogFileConfigLevel="Configure level of messages appended to a configured file"
test_LogFileConfigLevel() {
executeOk_servald config set log.console.level none
executeOk_servald config set log.file.level info
executeOk_servald config set log.file.path "$PWD/log.txt"
executeOk_servald log warn 'buckle'
assertGrep --matches=1 log.txt '^WARN:.*buckle$'
executeOk_servald log debug 'eccles'
assertGrep --matches=1 log.txt '^WARN:.*buckle$'
assertGrep --matches=0 log.txt 'DEBUG:.*eccles$'
executeOk_servald log error 'hoopla'
assertGrep --matches=1 log.txt '^WARN:.*buckle$'
assertGrep --matches=0 log.txt 'DEBUG:.*eccles$'
assertGrep --matches=1 log.txt '^ERROR:.*hoopla$'
executeOk_servald log info 'lymph'
assertGrep --matches=1 log.txt '^WARN:.*buckle$'
assertGrep --matches=0 log.txt 'DEBUG:.*eccles$'
assertGrep --matches=1 log.txt '^ERROR:.*hoopla$'
assertGrep --matches=1 log.txt 'INFO:.*lymph$'
}
doc_LogFileStderrFile="Log messages to stderr and a configured file"
test_LogFileStderrFile() {
executeOk_servald config set log.file.path "$PWD/log.txt"
executeOk_servald log info 'lymph'
assertGrep --matches=1 log.txt 'INFO:.*lymph$'
assertStderrIs ''
executeOk_servald log warn 'buckle'
assertGrep --matches=1 log.txt 'INFO:.*lymph$'
assertGrep --matches=1 log.txt '^WARN:.*buckle$'
assertStderrGrep --matches=1 '^WARN:.*buckle$'
executeOk_servald log debug 'eccles'
assertStderrIs ''
assertGrep --matches=1 log.txt 'INFO:.*lymph$'
assertGrep --matches=1 log.txt '^WARN:.*buckle$'
assertGrep --matches=1 log.txt 'DEBUG:.*eccles$'
execute $servald log error 'hoopla'
assertExitStatus '==' 0
assertStderrGrep --matches=1 '^ERROR:.*hoopla$'
assertGrep --matches=1 log.txt 'INFO:.*lymph$'
assertGrep --matches=1 log.txt '^WARN:.*buckle$'
assertGrep --matches=1 log.txt 'DEBUG:.*eccles$'
assertGrep --matches=1 log.txt '^ERROR:.*hoopla$'
}
doc_LogFileRotation="Log file rotation and deletion"
test_LogFileRotation() {
executeOk_servald config \
set log.file.directory_path "$PWD" \
set log.file.rotate 3 \
set log.file.duration 2s
assert --message="no log files yet" [ $(ls *.log | wc -l) -eq 0 ]
executeOk_servald log info one
assert --message="one log file" [ $(ls *.log | wc -l) -eq 1 ]
log1=*.log
sleep 2.1
executeOk_servald log info two
assert --message="two log files" [ $(ls *.log | wc -l) -eq 2 ]
log2=$(ls *.log | tail -n 1)
assert --message="ascending log file name" [ "$log2" != "$log1" ]
sleep 2.1
executeOk_servald log info three
assert --message="three log files" [ $(ls *.log | wc -l) -eq 3 ]
log3=$(ls *.log | tail -n 1)
assert --message="ascending log file name" [ "$log3" != "$log1" -a "$log3" != "$log2" ]
sleep 2.1
executeOk_servald log info four
assert --message="three log files" [ $(ls *.log | wc -l) -eq 3 ]
assert --message="first log file gone" ! [ -e $log1 ]
log4=$(ls *.log | tail -n 1)
assert --message="ascending log file name" [ "$log4" != "$log2" -a "$log4" != "$log3" -a "$log4" != "$log1" ]
}
doc_LogFileDirectoryAbsolute="Absolute log file directory path"
test_LogFileDirectoryAbsolute() {
executeOk_servald config \
set debug.verbose true \
set log.file.directory_path "$PWD/wakawaka"
executeOk_servald echo one
assert --message="exactly one log file" [ $(ls -d wakawaka/*.log | wc -l) -eq 1 ]
assertGrep wakawaka/*.log '^DEBUG:.*echo:argv\[1\]="one"$'
}
doc_LogFileDirectoryRelative="Relative log file directory path"
test_LogFileDirectoryRelative() {
executeOk_servald config \
set debug.verbose true \
set log.file.directory_path "../blah"
executeOk_servald echo one
assert --message="exactly one log file" [ $(ls -d "$SERVALINSTANCE_PATH/blah/"*.log | wc -l) -eq 1 ]
assertGrep "$SERVALINSTANCE_PATH/blah/"*.log '^DEBUG:.*echo:argv\[1\]="one"$'
}
doc_LogFileAbsolute="Absolute log file path"
test_LogFileAbsolute() {
executeOk_servald config \
set debug.verbose true \
set log.file.path "$PWD/log.txt"
executeOk_servald echo one
assertGrep log.txt '^DEBUG:.*echo:argv\[1\]="one"$'
}
doc_LogFileRelative="Relative log file path"
test_LogFileRelative() {
executeOk_servald config \
set debug.verbose true \
set log.file.directory_path "$PWD" \
set log.file.path "log.txt"
executeOk_servald echo one
assertGrep log.txt '^DEBUG:.*echo:argv\[1\]="one"$'
}
runTests "$@"