ganeti-cleaner_unittest.bash 5.9 KB
Newer Older
1
2
3
#!/bin/bash
#

4
# Copyright (C) 2010, 2012 Google Inc.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

21
set -e -u
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
set -o pipefail

export PYTHON=${PYTHON:=python}

GNTC=daemons/ganeti-cleaner
CCE=tools/check-cert-expired

err() {
  echo "$@"
  echo 'Aborting'
  exit 1
}

upto() {
  echo "$(date '+%F %T'):" "$@" '...'
}

gencert() {
  local path=$1 validity=$2
  VALIDITY=$validity $PYTHON \
    ${TOP_SRCDIR:-.}/test/import-export_unittest-helper \
    $path gencert
}

check_logfiles() {
47
  local n=$1 p=$2 path
48
49
  if [[ "$p" = master ]]; then
    path=$tmpls/log/ganeti/master-cleaner
50
51
52
  else
    path=$tmpls/log/ganeti/cleaner
  fi
53
54
55
56

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

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

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

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

75
76
77
78
79
80
81
82
83
84
85
86
87
88
count_and_check_certs() {
  local n=$1
  local count=$(find $cryptodir -mindepth 1 -type f -name cert | wc -l)
  [[ "$count" -eq "$n" ]] || err "Found $count certificates instead of $n"

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

run_cleaner() {
89
  CHECK_CERT_EXPIRED=$CCE LOCALSTATEDIR=$tmpls $GNTC $1
90
91
92
93
94
95
96
97
98
99
100
}

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

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

  i=0
101
  for job_id in {1..50} 469581574 19857 1420164 494433 2448521
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  do
    jobdir=$jobarchive/$(( job_id / 10 ))
    test -d $jobdir || mkdir $jobdir

    if (( i % 3 == 0 || i % 7 == 0 )); then
      touchargs="-t $old_ts"
    else
      touchargs=
    fi
    touch $touchargs $jobdir/job-$job_id

    let ++i
  done
}

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

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

    let ++i
  done
}

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

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

# Temporary localstatedir
tmpls=$tmpdir/var
queuedir=$tmpls/lib/ganeti/queue
cryptodir=$tmpls/run/ganeti/crypto
160
watcherdir=$tmpls/lib/ganeti
161
162
163
164
165
166
167
168
169

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

maxlog=50

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

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

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

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

upto 'Removal of archived jobs (non-master)'
create_archived_jobs
count_jobs 55
test -f $tmpls/lib/ganeti/ssconf_master_node && \
  err 'ssconf_master_node should not exist'
197
run_cleaner node
198
count_jobs 55
199
200
run_cleaner master
count_jobs 55
201
202
203
204
205

upto 'Removal of archived jobs (master node)'
create_archived_jobs
count_jobs 55
echo $HOSTNAME > $tmpls/lib/ganeti/ssconf_master_node
206
run_cleaner node
207
208
count_jobs 55
run_cleaner master
209
210
211
212
213
214
215
216
217
218
count_jobs 31

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

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

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

239
exit 0