Statistics
| Branch: | Tag: | Revision:

root / test / ganeti-cleaner_unittest.bash @ 46118ed2

History | View | Annotate | Download (5.9 kB)

1
#!/bin/bash
2
#
3

    
4
# Copyright (C) 2010, 2012 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 -u
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 p=$2 path
48
  if [[ "$p" = master ]]; then
49
    path=$tmpls/log/ganeti/master-cleaner
50
  else
51
    path=$tmpls/log/ganeti/cleaner
52
  fi
53

    
54
  test -d $path || \
55
    err "Log file directory '$path' not created"
56

    
57
  [[ "$(find $path -mindepth 1 | wc -l)" -le "$n" ]] || \
58
    err "Found more than $n logfiles"
59
}
60

    
61
count_jobs() {
62
  local n=$1
63
  local count=$(find $queuedir -mindepth 1 -type f | wc -l)
64
  [[ "$count" -eq "$n" ]] || err "Found $count jobs instead of $n"
65
}
66

    
67
count_watcher() {
68
  local suffix="$1" n=$2
69
  local count=$(find $watcherdir -maxdepth 1 -type f \
70
                  -name "watcher.*-*-*-*.$suffix" | wc -l)
71
  [[ "$count" -eq "$n" ]] || \
72
    err "Found $count watcher files with suffix '$suffix' instead of $n"
73
}
74

    
75
count_and_check_certs() {
76
  local n=$1
77
  local count=$(find $cryptodir -mindepth 1 -type f -name cert | wc -l)
78
  [[ "$count" -eq "$n" ]] || err "Found $count certificates instead of $n"
79

    
80
  find $cryptodir -mindepth 1 -type d | \
81
  while read dir; do
82
    [[ ( -e $dir/key && -e $dir/cert ) ||
83
       ( ! -e $dir/cert && ! -e $dir/key ) ]] || \
84
      err 'Inconsistent cert/key directory found'
85
  done
86
}
87

    
88
run_cleaner() {
89
  CHECK_CERT_EXPIRED=$CCE LOCALSTATEDIR=$tmpls $GNTC $1
90
}
91

    
92
create_archived_jobs() {
93
  local i jobdir touchargs
94
  local jobarchive=$queuedir/archive
95
  local old_ts=$(date -d '25 days ago' +%Y%m%d%H%M)
96

    
97
  # Remove jobs from previous run
98
  find $jobarchive -mindepth 1 -type f | xargs -r rm
99

    
100
  i=0
101
  for job_id in {1..50} 469581574 19857 1420164 494433 2448521
102
  do
103
    jobdir=$jobarchive/$(( job_id / 10 ))
104
    test -d $jobdir || mkdir $jobdir
105

    
106
    if (( i % 3 == 0 || i % 7 == 0 )); then
107
      touchargs="-t $old_ts"
108
    else
109
      touchargs=
110
    fi
111
    touch $touchargs $jobdir/job-$job_id
112

    
113
    let ++i
114
  done
115
}
116

    
117
create_watcher_state() {
118
  local uuids=(
119
    6792a0d5-f8b6-4531-8d8c-3680c86b8a53
120
    ab74da37-f5f7-44c4-83ad-074159772593
121
    fced2e48-ffff-43ae-919e-2b77d37ecafa
122
    6e89ac57-2eb1-4a16-85a1-94daa815d643
123
    8714e8f5-59c4-47db-b2cb-196ec37978e5
124
    91763d73-e1f3-47c7-a735-57025d4e2a7d
125
    e27d3ff8-9546-4e86-86a4-04151223e140
126
    aa3f63dd-be17-4ac8-bd01-d71790e124cb
127
    05b6d7e2-003b-40d9-a6d6-ab61bf123a15
128
    54c93e4c-61fe-40de-b47e-2a8e6c805d02
129
    )
130

    
131
  i=0
132
  for uuid in ${uuids[@]}; do
133
    touch -d "$(( 5 * i )) days ago" \
134
      $watcherdir/watcher.$uuid.{data,instance-status}
135

    
136
    let ++i
137
  done
138
}
139

    
140
create_certdirs() {
141
  local cert=$1; shift
142
  local certdir
143
  for name in "$@"; do
144
    certdir=$cryptodir/$name
145
    mkdir $certdir
146
    if [[ -n "$cert" ]]; then
147
      cp $cert $certdir/cert
148
      cp $cert $certdir/key
149
    fi
150
  done
151
}
152

    
153
tmpdir=$(mktemp -d)
154
trap "rm -rf $tmpdir" EXIT
155

    
156
# Temporary localstatedir
157
tmpls=$tmpdir/var
158
queuedir=$tmpls/lib/ganeti/queue
159
cryptodir=$tmpls/run/ganeti/crypto
160
watcherdir=$tmpls/lib/ganeti
161

    
162
mkdir -p $tmpls/{lib,log,run}/ganeti $queuedir/archive $cryptodir
163

    
164
maxlog=50
165

    
166
upto 'Checking log directory creation'
167
test -d $tmpls/log/ganeti || err 'log/ganeti does not exist'
168
test -d $tmpls/log/ganeti/cleaner && \
169
  err 'log/ganeti/cleaner should not exist yet'
170
run_cleaner node
171
check_logfiles 1 node
172

    
173
test -d $tmpls/log/ganeti/master-cleaner && \
174
  err 'log/ganeti/master-cleaner should not exist yet'
175
run_cleaner master
176
check_logfiles 1 master
177

    
178
upto 'Checking number of retained log files (master)'
179
for (( i=0; i < (maxlog + 10); ++i )); do
180
  run_cleaner master
181
  check_logfiles 1 node
182
  check_logfiles $(( (i + 2) > $maxlog?$maxlog:(i + 2) )) master
183
done
184

    
185
upto 'Checking number of retained log files (node)'
186
for (( i=0; i < (maxlog + 10); ++i )); do
187
  run_cleaner node
188
  check_logfiles $(( (i + 2) > $maxlog?$maxlog:(i + 2) )) node
189
  check_logfiles $maxlog master
190
done
191

    
192
upto 'Removal of archived jobs (non-master)'
193
create_archived_jobs
194
count_jobs 55
195
test -f $tmpls/lib/ganeti/ssconf_master_node && \
196
  err 'ssconf_master_node should not exist'
197
run_cleaner node
198
count_jobs 55
199
run_cleaner master
200
count_jobs 55
201

    
202
upto 'Removal of archived jobs (master node)'
203
create_archived_jobs
204
count_jobs 55
205
echo $HOSTNAME > $tmpls/lib/ganeti/ssconf_master_node
206
run_cleaner node
207
count_jobs 55
208
run_cleaner master
209
count_jobs 31
210

    
211
upto 'Certificate expiration'
212
gencert $tmpdir/validcert 30 & vcpid=${!}
213
gencert $tmpdir/expcert -30 & ecpid=${!}
214
wait $vcpid $ecpid
215
create_certdirs $tmpdir/validcert foo{a,b,c}123 trvRMH4Wvt OfDlh6Pc2n
216
create_certdirs $tmpdir/expcert bar{x,y,z}999 fx0ljoImWr em3RBC0U8c
217
create_certdirs '' empty{1,2,3} gd2HCvRc iFG55Z0a PP28v5kg
218
count_and_check_certs 10
219
run_cleaner master
220
count_and_check_certs 10
221
run_cleaner node
222
count_and_check_certs 5
223

    
224
check_logfiles $maxlog node
225
check_logfiles $maxlog master
226
count_jobs 31
227

    
228
upto 'Watcher status files'
229
create_watcher_state
230
count_watcher data 10
231
count_watcher instance-status 10
232
run_cleaner master
233
count_watcher data 10
234
count_watcher instance-status 10
235
run_cleaner node
236
count_watcher data 5
237
count_watcher instance-status 5
238

    
239
exit 0