bdev: Add functions to verify file storage paths
[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 GNTMC=daemons/ganeti-master-cleaner
28 CCE=tools/check-cert-expired
29
30 err() {
31   echo "$@"
32   echo 'Aborting'
33   exit 1
34 }
35
36 upto() {
37   echo "$(date '+%F %T'):" "$@" '...'
38 }
39
40 gencert() {
41   local path=$1 validity=$2
42   VALIDITY=$validity $PYTHON \
43     ${TOP_SRCDIR:-.}/test/import-export_unittest-helper \
44     $path gencert
45 }
46
47 check_logfiles() {
48   local n=$1 p=$2 path
49   if [[ "$p2" = master ]]; then
50     path=$tmpls/log/ganeti/mastercleaner
51   else
52     path=$tmpls/log/ganeti/cleaner
53   fi
54   [[ "$(find $path -mindepth 1 | wc -l)" -le "$n" ]] || \
55     err "Found more than $n logfiles"
56 }
57
58 count_jobs() {
59   local n=$1
60   local count=$(find $queuedir -mindepth 1 -type f | wc -l)
61   [[ "$count" -eq "$n" ]] || err "Found $count jobs instead of $n"
62 }
63
64 count_watcher() {
65   local suffix="$1" n=$2
66   local count=$(find $watcherdir -maxdepth 1 -type f \
67                   -name "watcher.*-*-*-*.$suffix" | wc -l)
68   [[ "$count" -eq "$n" ]] || \
69     err "Found $count watcher files with suffix '$suffix' instead of $n"
70 }
71
72 count_and_check_certs() {
73   local n=$1
74   local count=$(find $cryptodir -mindepth 1 -type f -name cert | wc -l)
75   [[ "$count" -eq "$n" ]] || err "Found $count certificates instead of $n"
76
77   find $cryptodir -mindepth 1 -type d | \
78   while read dir; do
79     [[ ( -e $dir/key && -e $dir/cert ) ||
80        ( ! -e $dir/cert && ! -e $dir/key ) ]] || \
81       err 'Inconsistent cert/key directory found'
82   done
83 }
84
85 run_cleaner() {
86   local cmd
87
88   if [[ "$1" = master ]]; then
89     cmd=$GNTMC
90   else
91     cmd=$GNTC
92   fi
93
94   CHECK_CERT_EXPIRED=$CCE LOCALSTATEDIR=$tmpls $cmd
95 }
96
97 create_archived_jobs() {
98   local i jobdir touchargs
99   local jobarchive=$queuedir/archive
100   local old_ts=$(date -d '25 days ago' +%Y%m%d%H%M)
101
102   # Remove jobs from previous run
103   find $jobarchive -mindepth 1 -type f | xargs -r rm
104
105   i=0
106   for job_id in {1..50} 469581574 19857 1420164 494433 2448521
107   do
108     jobdir=$jobarchive/$(( job_id / 10 ))
109     test -d $jobdir || mkdir $jobdir
110
111     if (( i % 3 == 0 || i % 7 == 0 )); then
112       touchargs="-t $old_ts"
113     else
114       touchargs=
115     fi
116     touch $touchargs $jobdir/job-$job_id
117
118     let ++i
119   done
120 }
121
122 create_watcher_state() {
123   local uuids=(
124     6792a0d5-f8b6-4531-8d8c-3680c86b8a53
125     ab74da37-f5f7-44c4-83ad-074159772593
126     fced2e48-ffff-43ae-919e-2b77d37ecafa
127     6e89ac57-2eb1-4a16-85a1-94daa815d643
128     8714e8f5-59c4-47db-b2cb-196ec37978e5
129     91763d73-e1f3-47c7-a735-57025d4e2a7d
130     e27d3ff8-9546-4e86-86a4-04151223e140
131     aa3f63dd-be17-4ac8-bd01-d71790e124cb
132     05b6d7e2-003b-40d9-a6d6-ab61bf123a15
133     54c93e4c-61fe-40de-b47e-2a8e6c805d02
134     )
135
136   i=0
137   for uuid in ${uuids[@]}; do
138     touch -d "$(( 5 * i )) days ago" \
139       $watcherdir/watcher.$uuid.{data,instance-status}
140
141     let ++i
142   done
143 }
144
145 create_certdirs() {
146   local cert=$1; shift
147   local certdir
148   for name in "$@"; do
149     certdir=$cryptodir/$name
150     mkdir $certdir
151     if [[ -n "$cert" ]]; then
152       cp $cert $certdir/cert
153       cp $cert $certdir/key
154     fi
155   done
156 }
157
158 tmpdir=$(mktemp -d)
159 trap "rm -rf $tmpdir" EXIT
160
161 # Temporary localstatedir
162 tmpls=$tmpdir/var
163 queuedir=$tmpls/lib/ganeti/queue
164 cryptodir=$tmpls/run/ganeti/crypto
165 watcherdir=$tmpls/lib/ganeti
166
167 mkdir -p $tmpls/{lib,log,run}/ganeti $queuedir/archive $cryptodir
168
169 maxlog=50
170
171 upto 'Checking log directory creation'
172 test -d $tmpls/log/ganeti || err 'log/ganeti does not exist'
173 test -d $tmpls/log/ganeti/cleaner && \
174   err 'log/ganeti/cleaner should not exist yet'
175 run_cleaner
176 test -d $tmpls/log/ganeti/cleaner || err 'log/ganeti/cleaner should exist'
177 check_logfiles 1
178
179 test -d $tmpls/log/ganeti/master-cleaner && \
180   err 'log/ganeti/master-cleaner should not exist yet'
181 run_cleaner master
182 test -d $tmpls/log/ganeti/master-cleaner || \
183   err 'log/ganeti/master-cleaner should exist'
184 check_logfiles 1 master
185
186 upto 'Checking number of retained log files (master)'
187 for (( i=0; i < (maxlog + 10); ++i )); do
188   run_cleaner master
189   check_logfiles 1
190   check_logfiles $(( (i + 2) > $maxlog?$maxlog:(i + 2) )) master
191 done
192
193 upto 'Checking number of retained log files (node)'
194 for (( i=0; i < (maxlog + 10); ++i )); do
195   run_cleaner
196   check_logfiles $(( (i + 2) > $maxlog?$maxlog:(i + 2) ))
197   check_logfiles $maxlog master
198 done
199
200 upto 'Removal of archived jobs (non-master)'
201 create_archived_jobs
202 count_jobs 55
203 test -f $tmpls/lib/ganeti/ssconf_master_node && \
204   err 'ssconf_master_node should not exist'
205 run_cleaner
206 count_jobs 55
207 run_cleaner master
208 count_jobs 55
209
210 upto 'Removal of archived jobs (master node)'
211 create_archived_jobs
212 count_jobs 55
213 echo $HOSTNAME > $tmpls/lib/ganeti/ssconf_master_node
214 run_cleaner
215 count_jobs 55
216 run_cleaner master
217 count_jobs 31
218
219 upto 'Certificate expiration'
220 gencert $tmpdir/validcert 30 & vcpid=${!}
221 gencert $tmpdir/expcert -30 & ecpid=${!}
222 wait $vcpid $ecpid
223 create_certdirs $tmpdir/validcert foo{a,b,c}123 trvRMH4Wvt OfDlh6Pc2n
224 create_certdirs $tmpdir/expcert bar{x,y,z}999 fx0ljoImWr em3RBC0U8c
225 create_certdirs '' empty{1,2,3} gd2HCvRc iFG55Z0a PP28v5kg
226 count_and_check_certs 10
227 run_cleaner master
228 count_and_check_certs 10
229 run_cleaner
230 count_and_check_certs 5
231
232 check_logfiles $maxlog
233 check_logfiles $maxlog master
234 count_jobs 31
235
236 upto 'Watcher status files'
237 create_watcher_state
238 count_watcher data 10
239 count_watcher instance-status 10
240 run_cleaner master
241 count_watcher data 10
242 count_watcher instance-status 10
243 run_cleaner
244 count_watcher data 5
245 count_watcher instance-status 5
246
247 exit 0