summaryrefslogtreecommitdiffstats
path: root/test/test_logging.sh
blob: ef6060fa7113de51e197c987b17fe6908530bda5 (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/bin/bash
#
# Copyright (c) 2014, Red Hat, Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are
# those of the authors and should not be interpreted as representing official
# policies, either expressed or implied, of the FreeBSD Project.
#
# Authors: Michal Minar <miminar@redhat.com>

. ./base.sh

# Set the full test name
TEST="openlmi-scripts/test/test_logging.sh"

# Package being tested
PACKAGE="openlmi-scripts"

# array of supported log levels
LEVELS=( critical error warn info debug )

DEBUG_CONFIG_TEMPLATE="
[Main]
Trace = True
Verbosity = 2

[Log]
Level = DEBUG
LogToConsole = True
FileFormat = %%(levelname)s: %%(message)s
OutputFile = %s
"

rlJournalStart

rlPhaseStartSetup
    rlLogInfo "Creating temporary python sandbox"
    sandbox=`mktemp -d`
    export PYTHONPATH="$sandbox"
    pushd ..
    rlLogInfo "Installing lmi meta-command"
    rlRun "python setup.py develop --install-dir=$sandbox" 0
    popd
    pushd subcmd
    rlRun "python setup.py develop --install-dir=$sandbox" 0
    popd
    export PATH="$sandbox:$PATH"
rlPhaseEnd

rlPhaseStartTest
    rlLogInfo "Test logging with default settings"

    LMICMD="$LMI -c logging/defaults.conf"

    stdout=`mktemp stdoutXXXX`
    stderr=`mktemp stderrXXXX`
    rlRun "$LMICMD test log all >$stdout 2>$stderr" 0
    rlAssertNotDiffer logging/log_all_default.stdout $stdout
    rlAssertNotDiffer logging/log_all_warn.stderr $stderr

    rlRun "$LMICMD test log all --with-traceback >$stdout 2>$stderr" 0
    rlAssertNotDiffer logging/log_all_default.stdout $stdout
    tmpout=`mktemp tmpoutXXXX`
    grep '^\(warning\|error\|critical\)' $stderr > $tmpout
    rlAssertNotDiffer logging/log_all_warn.stderr $tmpout
    rlAssertEquals "Correct number of exceptions were raised" 3 \
        `grep '^RuntimeError: S\*\*t happens!' $stderr | wc -l`
    rm $tmpout

    expected_out=`mktemp expected_stdoutXXXX`
    expected_err=`mktemp expected_stderrXXXX`
    for verbosity in 0 1 2; do
        if [[ $verbosity -gt 0 ]]; then
            option=`printf -- "-v %.0s" $(seq $verbosity)`
        else
            option=''
        fi
        CMD="$LMICMD $option test log level"
        for level in `seq 0 $((${#LEVELS[@]} - 1))`; do
            rlRun "$CMD --${LEVELS[$level]} >$stdout 2>$stderr" 0
            level_name=${LEVELS[$level]}
            log_level_name=$level_name
            [ $level_name = warn ] && log_level_name=warning
            ( \
                if [ $verbosity = 2 ]; then \
                    echo 'Running command "test".'; \
                    echo 'Found registered command "test".'; \
                fi; \
                echo -n 'warning : Command "lmi.scripts.cmdtest.Show"'; \
                echo -n " is missing usage string. It will be"; \
                echo " inherited from parent command."; \
                if [ $verbosity = 2 ]; then \
                    for opt_name in '_with_traceback' '_lmi_failed'; do \
                        echo -n "Option \"$opt_name\" not handled in function"; \
                        echo ' "level", ignoring.'; \
                    done; \
                    echo "Connected to $HOSTNAME"; \
                fi; \
                if [ $(($verbosity + 2)) -ge $level ]; then \
                    if [ $level -lt 3 ]; then \
                        printf "%-8s: " $log_level_name; \
                    fi; \
                    echo "This is $level_name message."; \
                fi \
            ) >$expected_err
            rlAssertNotDiffer $expected_err $stderr
            rlAssertEquals "Nothing is written to stdout" 0 `cat $stdout | wc -c`
        done
    done

    rm $stdout $stderr $expected_out $expected_err

rlPhaseEnd

rlPhaseStartTest
    rlLogInfo "Test logging with debug settings"

    config_file=`mktemp configXXXX`
    log_file=`mktemp log_fileXXXX`
    printf "$DEBUG_CONFIG_TEMPLATE" $log_file >$config_file

    LMICMD="$LMI -c $config_file"

    stdout=`mktemp stdoutXXXX`
    stderr=`mktemp stderrXXXX`
    filter_debug=" | grep -v '^Option' > $stderr"
    rlRun "$LMICMD test log all 2>&1 >$stdout $filter_debug" 0
    rlAssertNotDiffer logging/log_all_debug.stdout $stdout
    rlAssertNotDiffer logging/log_all_debug.stderr $stderr
    cat $log_file | grep -v '^DEBUG: Option' >${log_file}.tmp
    mv ${log_file}{.tmp,}
    rlAssertNotDiffer logging/log_all_debug.file $log_file
    rm $log_file

    rlRun "$LMICMD test log all --with-traceback 2>&1 >$stdout $filter_debug" 0
    rlAssertNotDiffer logging/log_all_debug.stdout $stdout
    tmpout=`mktemp tmpoutXXXX`
    grep '^\(warning\|error\|critical\)' $stderr > $tmpout
    rlAssertNotDiffer logging/log_all_warn.stderr $tmpout
    rlAssertEquals "Correct number of exceptions were raised" 5 \
        `grep '^RuntimeError: S\*\*t happens!' $stderr | wc -l`
    rlAssertEquals "Correct number of exceptions were raised" 5 \
        `grep '^RuntimeError: S\*\*t happens!' $log_file | wc -l`
    rm $tmpout $log_file

    rlLogInfo "Try override log-file"
    new_log_file=`mktemp new_log_fileXXXX`
    rlRun "$LMICMD --log-file $new_log_file test log all 2>&1 >$stdout $filter_debug" 0
    rlAssertNotDiffer logging/log_all_debug.stdout $stdout
    rlAssertNotDiffer logging/log_all_debug.stderr $stderr
    cat $new_log_file | grep -v '^DEBUG: Option' >${new_log_file}.tmp
    mv ${new_log_file}{.tmp,}
    rlAssertNotDiffer logging/log_all_debug.file $new_log_file
    rlRun "[ -e $log_file ]" 1 "Log file from config file shall not be written"
    
    [ -e $new_log_file ] && rm $new_log_file
    [ -e $log_file ] && rm $log_file

    rlLogInfo "Try to disable logging to a file"
    rlRun "$LMICMD --log-file '' test log all 2>&1 >$stdout $filter_debug" 0
    rlAssertNotDiffer logging/log_all_debug.stdout $stdout
    rlAssertNotDiffer logging/log_all_debug.stderr $stderr
    rlRun "[ -e $log_file ]" 1 "Log file from config file shall not be written"
    [ -e $log_file ] && rm $log_file

    rlLogInfo "Try override verbosity"
    rlRun "$LMICMD -q test log all 2>&1 >$stdout $filter_debug" 0
    rlAssertNotDiffer logging/log_all_silent.stdout $stdout
    rlAssertNotDiffer logging/log_all_error.stderr $stderr
    cat $log_file | grep -v '^DEBUG: Option' >${log_file}.tmp
    mv ${log_file}{.tmp,}
    rlAssertNotDiffer logging/log_all_debug.file $log_file
    rm $log_file

    rlRun "$LMICMD -v test log all >$stdout 2>$stderr" 0
    rlAssertNotDiffer logging/log_all_info.stdout $stdout
    rlAssertNotDiffer logging/log_all_info.stderr $stderr
    cat $log_file | grep -v '^DEBUG: Option' >${log_file}.tmp
    mv ${log_file}{.tmp,}
    rlAssertNotDiffer logging/log_all_debug.file $log_file
    rm $log_file

    rlLogInfo "Change file log level"
    sed -i 's/DEBUG/WARNING/' $config_file
    rlAssertGrep 'Level = WARNING' $config_file
    rlRun "$LMICMD test log all 2>&1 >$stdout $filter_debug" 0
    rlAssertNotDiffer logging/log_all_debug.stdout $stdout
    rlAssertNotDiffer logging/log_all_debug.stderr $stderr
    rlAssertNotDiffer logging/log_all_warn.file $log_file
    [ -e $log_file ] && rm $log_file

    rlLogInfo "Disable logging to console"
    sed -i 's/\(LogToConsole.*\)True/\1False/' $config_file
    rlAssertGrep 'LogToConsole = False' $config_file
    rlRun "$LMICMD test log all >$stdout 2>$stderr" 0
    rlAssertNotDiffer logging/log_all_debug.stdout $stdout
    rlAssertEquals "Nothing is written to stderr" `cat $stderr | wc -c` 0
    rlAssertNotDiffer logging/log_all_warn.file $log_file
    [ -e $log_file ] && rm $log_file

    rlLogInfo "Override tracing"
    rlRun "$LMICMD --notrace test log all >$stdout 2>$stderr" 0
    rlAssertNotDiffer logging/log_all_debug_notrace.stdout $stdout
    rlAssertEquals "Nothing is written to stderr" `cat $stderr | wc -c` 0
    rlAssertNotDiffer logging/log_all_warn.file $log_file

    for f in $log_file $new_log_file $config_file $stdout $stderr; do
        [ -e $f ] && rm $f
    done

rlPhaseEnd

rlPhaseStartTest
    rlLogInfo "Test logging with settings"

    stdout=`mktemp stdoutXXXX`
    stderr=`mktemp stderrXXXX`
    rlRun "$LMI -c logging/defaults.conf test log all >$stdout 2>$stderr" 0
    rlAssertNotDiffer $stdout logging/log_all_default.stdout
    rlAssertNotDiffer $stderr logging/log_all_warn.stderr
    rm $stdout $stderr
rlPhaseEnd


rlPhaseStartCleanup
    rlLogInfo "Removing temporary python sandbox"
    rm -rf "$sandbox"
rlPhaseEnd