Generate import-export unittest certs in parallel
[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_and_check_certs() {
59   local n=$1
60   local count=$(find $cryptodir -mindepth 1 -type f -name cert | wc -l)
61   [[ "$count" -eq "$n" ]] || err "Found $count certificates instead of $n"
62
63   find $cryptodir -mindepth 1 -type d | \
64   while read dir; do
65     [[ ( -e $dir/key && -e $dir/cert ) ||
66        ( ! -e $dir/cert && ! -e $dir/key ) ]] || \
67       err 'Inconsistent cert/key directory found'
68   done
69 }
70
71 run_cleaner() {
72   CHECK_CERT_EXPIRED=$CCE LOCALSTATEDIR=$tmpls $GNTC
73 }
74
75 create_archived_jobs() {
76   local i jobdir touchargs
77   local jobarchive=$queuedir/archive
78   local large=$(( RANDOM * RANDOM ))
79   local old_ts=$(date -d '25 days ago' +%Y%m%d%H%M)
80
81   # Remove jobs from previous run
82   find $jobarchive -mindepth 1 -type f | xargs -r rm
83
84   i=0
85   for job_id in {1..50} $(( large % RANDOM )) $RANDOM \
86                 $(( large - 1 )) $large $(( large + 1 ))
87   do
88     jobdir=$jobarchive/$(( job_id / 10 ))
89     test -d $jobdir || mkdir $jobdir
90
91     if (( i % 3 == 0 || i % 7 == 0 )); then
92       touchargs="-t $old_ts"
93     else
94       touchargs=
95     fi
96     touch $touchargs $jobdir/job-$job_id
97
98     let ++i
99   done
100 }
101
102 create_certdirs() {
103   local cert=$1; shift
104   local certdir
105   for name in "$@"; do
106     certdir=$cryptodir/$name
107     mkdir $certdir
108     if [[ -n "$cert" ]]; then
109       cp $cert $certdir/cert
110       cp $cert $certdir/key
111     fi
112   done
113 }
114
115 tmpdir=$(mktemp -d)
116 trap "rm -rf $tmpdir" EXIT
117
118 # Temporary localstatedir
119 tmpls=$tmpdir/var
120 queuedir=$tmpls/lib/ganeti/queue
121 cryptodir=$tmpls/run/ganeti/crypto
122
123 mkdir -p $tmpls/{lib,log,run}/ganeti $queuedir/archive $cryptodir
124
125 maxlog=50
126
127 upto 'Checking log directory creation'
128 test -d $tmpls/log/ganeti || err 'log/ganeti does not exist'
129 test -d $tmpls/log/ganeti/cleaner && \
130   err 'log/ganeti/cleaner should not exist yet'
131 run_cleaner
132 test -d $tmpls/log/ganeti/cleaner || err 'log/ganeti/cleaner should exist'
133 check_logfiles 1
134
135 upto 'Checking number of retained log files'
136 for (( i=0; i < (maxlog + 10); ++i )); do
137   run_cleaner
138   check_logfiles $(( (i + 2) > $maxlog?$maxlog:(i + 2) ))
139 done
140
141 upto 'Removal of archived jobs (non-master)'
142 create_archived_jobs
143 count_jobs 55
144 test -f $tmpls/lib/ganeti/ssconf_master_node && \
145   err 'ssconf_master_node should not exist'
146 run_cleaner
147 count_jobs 55
148
149 upto 'Removal of archived jobs (master node)'
150 create_archived_jobs
151 count_jobs 55
152 echo $HOSTNAME > $tmpls/lib/ganeti/ssconf_master_node
153 run_cleaner
154 count_jobs 31
155
156 upto 'Certificate expiration'
157 gencert $tmpdir/validcert 30 & vcpid=${!}
158 gencert $tmpdir/expcert -30 & ecpid=${!}
159 wait $vcpid $ecpid
160 create_certdirs $tmpdir/validcert foo{a,b,c}123 trvRMH4Wvt OfDlh6Pc2n
161 create_certdirs $tmpdir/expcert bar{x,y,z}999 fx0ljoImWr em3RBC0U8c
162 create_certdirs '' empty{1,2,3} gd2HCvRc iFG55Z0a PP28v5kg
163 count_and_check_certs 10
164 run_cleaner
165 count_and_check_certs 5
166
167 check_logfiles $maxlog
168 count_jobs 31
169
170 exit 0