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