-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.sh
executable file
Β·192 lines (155 loc) Β· 3.78 KB
/
tests.sh
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
#!/usr/bin/env bash
args=$@
IDLELOCK_DO_NOT_RUN=1 source ./idlelock.sh
test_window_process_name='glxgears'
test_window_title='Gears'
test_audio_url='https://www.youtube.com/watch?v=s-cAcqsFJWY'
test_download_url='http://ipv4.download.thinkbroadband.com/1GB.zip'
test_is_window_fullscreen() {
#
# Test is_window_fullscreen does not pass when no window is fullscreen.
#
trap 'kill $(jobs -p)' RETURN
sh -c "exec $test_window_process_name" &
sleep 0.5
wmctrl -r $test_window_title -b add,fullscreen
is_window_fullscreen
}
test_is_window_not_fullscreen() {
#
# Test is_window_fullscreen does not pass when no window is fullscreen.
#
! is_window_fullscreen
}
test_is_audio_playing() {
#
# Test is_audio_playing passes when audio is playing.
#
trap 'kill $(jobs -p)' RETURN
sh -c "exec mpv '$test_audio_url' --no-video" &
sleep 5
is_audio_playing
}
test_is_audio_not_playing() {
#
# Test is_audio_playing fails when audio is not playing.
#
! is_audio_playing
}
test_is_network_busy() {
#
# Test is_network_busy passes when downloading a file.
#
trap 'kill $(jobs -p); rm -f /tmp/test_file' RETURN
sh -c "wget --limit-rate=501k '$test_download_url' -O /tmp/test_file" &
sleep 2
is_network_busy enp5s0 500
}
test_is_network_not_busy() {
#
# Test is_network_busy fails when not downloading.
#
! is_network_busy enp5s0 500
}
test_is_cpu_busy() {
#
# Test is_cpu_busy passes when there is cpu load.
#
trap 'pkill stress' RETURN
sh -c "stress -c 16" &
sleep 10
is_cpu_busy 3
}
test_is_cpu_not_busy() {
#
# Test is_cpu_busy fails when there is no cpu load.
#
! is_cpu_busy 5
}
test_is_paused() {
#
# Test is_paused passes when file with unix time is greater than now.
#
echo "$(( $(printf '%(%s)T\n') + 60 ))" > /tmp/test_pause_file
trap 'rm -f /tmp/test_pause_file' RETURN
is_paused /tmp/test_pause_file
}
test_is_paused_with_unix_time_less_than_now() {
#
# Test is_paused fails when file with unix time is less than now.
#
echo "$(( $(printf '%(%s)T\n') - 60 ))" > /tmp/test_pause_file
trap 'rm -f /tmp/test_pause_file' RETURN
! is_paused /tmp/test_pause_file
}
test_is_paused_without_file() {
#
# Test is_paused fails without pause file.
#
rm -f /tmp/test_pause_file
! is_paused /tmp/test_pause_file
}
test_run_command() {
#
# Test run_command runs a temporary timer command.
#
commands=( [temp]='touch /tmp/test_file' )
run_command temp
sleep 0.5
rm /tmp/test_file
}
test_run_command_with_invalid_timer() {
#
# Test run_command fails on invalid timer key.
#
! run_command invalid
}
test_run_restore() {
#
# Test run_restore runs a restore command.
#
restores=( [temp]='touch /tmp/test_file' )
run_restore temp
sleep 0.2
rm /tmp/test_file
}
test_run_restore_with_invalid_timer() {
#
# Test run_restore fails when no restores are specified.
#
! run_restore invalid
}
validate() {
#
# Validate test environment.
#
which wmctrl &> /dev/null || { echo 'Missing wmctrl.' && exit 1; }
which glxgears &> /dev/null || { echo 'Missing glxgears.' && exit 1; }
which mpv &> /dev/null || { echo 'Missing mpv.' && exit 1; }
which youtube-dl &> /dev/null || { echo 'Missing youtube-dl.' && exit 1; }
which stress &> /dev/null || { echo 'Missing stress.' && exit 1; }
}
main() {
#
# Run test functions.
#
test_functions=${args:-$(declare -F | grep -oP ' test_.*')}
tests_total=$(echo $test_functions | wc -w)
tests_completed=0; tests_passed=0; tests_failed=0
for test_function in $test_functions; do
((tests_completed++))
if $test_function; then
echo "$test_function: Passed ($tests_completed/$tests_total)"
((tests_passed++))
else
echo "$test_function: Failed ($tests_completed/$tests_total)"
((tests_failed++))
fi
done
echo
echo 'Tests: ' $tests_completed
echo 'Passed:' $tests_passed
echo 'Failed:' $tests_failed
}
validate
main