Move _TimeoutExpired to utils
[ganeti-local] / test / ganeti-cleaner_unittest.bash
1 #!/bin/bash
2 #
3
4 # Copyright (C) 2010 Google Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21 set -e
22 set -o pipefail
23
24 export PYTHON=${PYTHON:=python}
25
26 GNTC=daemons/ganeti-cleaner
27 CCE=tools/check-cert-expired
28
29 err() {
30   echo "$@"
31   echo 'Aborting'
32   exit 1
33 }
34
35 upto() {
36   echo "$(date '+%F %T'):" "$@" '...'
37 }
38
39 gencert() {
40   local path=$1 validity=$2
41   VALIDITY=$validity $PYTHON \
42     ${TOP_SRCDIR:-.}/test/import-export_unittest-helper \
43     $path gencert
44 }
45
46 check_logfiles() {
47   local n=$1
48   [[ "$(find $tmpls/log/ganeti/cleaner -mindepth 1 | wc -l)" -le "$n" ]] || \
49     err "Found more than $n logfiles"
50 }
51
52 count_jobs() {
53   local n=$1
54   local count=$(find $queuedir -mindepth 1 -type f | wc -l)
55   [[ "$count" -eq "$n" ]] || err "Found $count jobs instead of $n"
56 }
57
58 count_watcher() {
59   local suffix="$1" n=$2
60   local count=$(find $watcherdir -maxdepth 1 -type f \
61                   -name "watcher.*-*-*-*.$suffix" | wc -l)
62   [[ "$count" -eq "$n" ]] || \
63     err "Found $count watcher files with suffix '$suffix' instead of $n"
64 }
65
66 count_and_check_certs() {
67   local n=$1
68   local count=$(find $cryptodir -mindepth 1 -type f -name cert | wc -l)
69   [[ "$count" -eq "$n" ]] || err "Found $count certificates instead of $n"
70
71   find $cryptodir -mindepth 1 -type d | \
72   while read dir; do
73     [[ ( -e $dir/key && -e $dir/cert ) ||
74        ( ! -e $dir/cert && ! -e $dir/key ) ]] || \
75       err 'Inconsistent cert/key directory found'
76   done
77 }
78
79 run_cleaner() {
80   CHECK_CERT_EXPIRED=$CCE LOCALSTATEDIR=$tmpls $GNTC
81 }
82
83 create_archived_jobs() {
84   local i jobdir touchargs
85   local jobarchive=$queuedir/archive
86   local old_ts=$(date -d '25 days ago' +%Y%m%d%H%M)
87
88   # Remove jobs from previous run
89   find $jobarchive -mindepth 1 -type f | xargs -r rm
90
91   i=0
92   for job_id in {1..50} 469581574 19857 1420164 494433 2448521
93   do
94     jobdir=$jobarchive/$(( job_id / 10 ))
95     test -d $jobdir || mkdir $jobdir
96
97     if (( i % 3 == 0 || i % 7 == 0 )); then
98       touchargs="-t $old_ts"
99     else
100       touchargs=
101     fi
102     touch $touchargs $jobdir/job-$job_id
103
104     let ++i
105   done
106 }
107
108 create_watcher_state() {
109   local uuids=(
110     6792a0d5-f8b6-4531-8d8c-3680c86b8a53
111     ab74da37-f5f7-44c4-83ad-074159772593
112     fced2e48-ffff-43ae-919e-2b77d37ecafa
113     6e89ac57-2eb1-4a16-85a1-94daa815d643
114     8714e8f5-59c4-47db-b2cb-196ec37978e5
115     91763d73-e1f3-47c7-a735-57025d4e2a7d
116     e27d3ff8-9546-4e86-86a4-04151223e140
117     aa3f63dd-be17-4ac8-bd01-d71790e124cb
118     05b6d7e2-003b-40d9-a6d6-ab61bf123a15
119     54c93e4c-61fe-40de-b47e-2a8e6c805d02
120     )
121
122   i=0
123   for uuid in ${uuids[@]}; do
124     touch -d "$(( 5 * i )) days ago" \
125       $watcherdir/watcher.$uuid.{data,instance-status}
126
127     let ++i
128   done
129 }
130
131 create_certdirs() {
132   local cert=$1; shift
133   local certdir
134   for name in "$@"; do
135     certdir=$cryptodir/$name
136     mkdir $certdir
137     if [[ -n "$cert" ]]; then
138       cp $cert $certdir/cert
139       cp $cert $certdir/key
140     fi
141   done
142 }
143
144 tmpdir=$(mktemp -d)
145 trap "rm -rf $tmpdir" EXIT
146
147 # Temporary localstatedir
148 tmpls=$tmpdir/var
149 queuedir=$tmpls/lib/ganeti/queue
150 cryptodir=$tmpls/run/ganeti/crypto
151 watcherdir=$tmpls/lib/ganeti
152
153 mkdir -p $tmpls/{lib,log,run}/ganeti $queuedir/archive $cryptodir
154
155 maxlog=50
156
157 upto 'Checking log directory creation'
158 test -d $tmpls/log/ganeti || err 'log/ganeti does not exist'
159 test -d $tmpls/log/ganeti/cleaner && \
160   err 'log/ganeti/cleaner should not exist yet'
161 run_cleaner
162 test -d $tmpls/log/ganeti/cleaner || err 'log/ganeti/cleaner should exist'
163 check_logfiles 1
164
165 upto 'Checking number of retained log files'
166 for (( i=0; i < (maxlog + 10); ++i )); do
167   run_cleaner
168   check_logfiles $(( (i + 2) > $maxlog?$maxlog:(i + 2) ))
169 done
170
171 upto 'Removal of archived jobs (non-master)'
172 create_archived_jobs
173 count_jobs 55
174 test -f $tmpls/lib/ganeti/ssconf_master_node && \
175   err 'ssconf_master_node should not exist'
176 run_cleaner
177 count_jobs 55
178
179 upto 'Removal of archived jobs (master node)'
180 create_archived_jobs
181 count_jobs 55
182 echo $HOSTNAME > $tmpls/lib/ganeti/ssconf_master_node
183 run_cleaner
184 count_jobs 31
185
186 upto 'Certificate expiration'
187 gencert $tmpdir/validcert 30 & vcpid=${!}
188 gencert $tmpdir/expcert -30 & ecpid=${!}
189 wait $vcpid $ecpid
190 create_certdirs $tmpdir/validcert foo{a,b,c}123 trvRMH4Wvt OfDlh6Pc2n
191 create_certdirs $tmpdir/expcert bar{x,y,z}999 fx0ljoImWr em3RBC0U8c
192 create_certdirs '' empty{1,2,3} gd2HCvRc iFG55Z0a PP28v5kg
193 count_and_check_certs 10
194 run_cleaner
195 count_and_check_certs 5
196
197 check_logfiles $maxlog
198 count_jobs 31
199
200 upto 'Watcher status files'
201 create_watcher_state
202 count_watcher data 10
203 count_watcher instance-status 10
204 run_cleaner
205 count_watcher data 5
206 count_watcher instance-status 5
207
208 exit 0