File: nwfilter_concurrent.sh

package info (click to toggle)
libvirt-tck 0.1.0~2.git890d1c-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,128 kB
  • sloc: perl: 2,885; sh: 1,180; xml: 992; makefile: 6
file content (406 lines) | stat: -rw-r--r-- 10,065 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/bin/sh

VIRSH=virsh

# For each line starting with uri=, remove the prefix and set the hold
# space to the rest of the line.  Then at file end, print the hold
# space, which is effectively the last uri= line encountered.
uri=$(sed -n '/^uri[     ]*=[     ]*/ {
  s///
  h
}
$ {
  x
  p
}' < "$LIBVIRT_TCK_CONFIG")
: "${uri:=qemu:///system}"

LIBVIRT_URI=${uri}

FLAG_WAIT="$((1<<0))"
FLAG_ATTACH="$((1<<1))"
FLAG_VERBOSE="$((1<<2))"
FLAG_LIBVIRT_TEST="$((1<<3))"
FLAG_TAP_TEST="$((1<<4))"
FLAG_FORCE_CLEAN="$((1<<5))"

failctr=0
passctr=0
attachfailctr=0
attachctr=0

TAP_FAIL_LIST=""
TAP_FAIL_CTR=0
TAP_TOT_CTR=0

usage() {
  cmd="$0"
cat <<EOF
Usage: ${cmd} [--help|-h|-?] [--noattach] [--wait] [--verbose]
              [--libvirt-test] [--tap-test]

Options:
 --help,-h,-?   : Display this help screen.
 --noattach     : Skip tests that attach and detach a network interface
 --wait         : Wait for the user to press the enter key once an error
                  was detected
 --verbose      : Verbose output
 --libvirt-test : Use the libvirt test output format
 --tap-test     : TAP format output
 --force        : Allow the automatic cleaning of VMs and nwfilters
                  previously created by the TCK test suite

This test will create two virtual machines. The one virtual machine
will use a filter called '${TESTFILTERNAME}', and reference the filter
'clean-traffic' which should be available by default with every install.
The other virtual machine will reference the filter 'tck-testcase' and will
have its filter permanently updated.
EOF
}


tap_fail() {
  txt=$(echo "$2" | gawk '{print substr($0,1,66)}')
  echo "not ok $1 - ${txt}"
  TAP_FAIL_LIST="$TAP_FAIL_LIST $1 "
  TAP_FAIL_CTR=$(($TAP_FAIL_CTR + 1))
  TAP_TOT_CTR=$(($TAP_TOT_CTR + 1))
}

tap_pass() {
  txt=$(echo "$2" | gawk '{print substr($0,1,70)}')
  echo "ok $1 - ${txt}"
  TAP_TOT_CTR=$(($TAP_TOT_CTR + 1))
}

tap_final() {
  [ -n "${TAP_FAIL_LIST}" ] && echo "FAILED tests ${TAP_FAIL_LIST}"

  okay=`echo "($TAP_TOT_CTR-$TAP_FAIL_CTR)*100/$TAP_TOT_CTR" | bc -l`
  txt=$(echo $okay | gawk '{print substr($0,1,5)}')
  echo "Failed ${TAP_FAIL_CTR}/${TAP_TOT_CTR} tests, ${txt}% okay"
}

# A wrapper for mktemp in case it does not exist
# Echos the name of a temporary file.
mktmpdir() {
  {
    tmp=$( (umask 077 && mktemp -d ./nwfvmtest.XXXXXX) 2>/dev/null) &&
    test -n "$tmp" && test -d "$tmp"
  } ||
  {
    tmp=./nwfvmtest$$-$RANDOM
    (umask 077 && mkdir "$tmp")
  } || { echo "failed to create secure temporary directory" >&2; exit 1; }
  echo "${tmp}"
  return 0
}


PRGDIR="./concurrency"
CREATE_DES_VM1="${PRGDIR}/start-destroy-vm.sh 1"
CREATE_DES_VM2="${PRGDIR}/start-destroy-vm.sh 2"
CHG_FILTER_VM1="${PRGDIR}/chg-vm-filter.sh 1"
CHG_FILTER_VM2="${PRGDIR}/chg-vm-filter.sh 2"


startPrgs()
{
  flags="$1"

  sh ${CHG_FILTER_VM1} "$4" 2>&1 >/dev/null &
  [ $? -ne 0 ] && \
    ( killPrgs "${flags}" "Could not start program 3" ; return 1; )
  CHG_FILTER_VM1_THR=$!

  sh ${CHG_FILTER_VM2} "$5" 2>&1 >/dev/null &
  [ $? -ne 0 ] && \
    ( killPrgs "${flags}" "Could not start program 4" ; return 1; )
  CHG_FILTER_VM2_THR=$!

  # Give some time for the filters to be created
  sleep 2

  sh ${CREATE_DES_VM1} "$2" 2>&1 >/dev/null &
  [ $? -ne 0 ] && \
    ( killPrgs "${flags}" "Could not start program 1" ; return 1; )
  CREATE_DES_VM1_THR=$!

  sh ${CREATE_DES_VM2} "$3" 2>&1 >/dev/null &
  [ $? -ne 0 ] && \
    ( killPrgs "${flags}" "Could not start program 2" ; return 1; )
  CREATE_DES_VM2_THR=$!

}


killPrgs()
{
  msg="$1"

  # terminate all process
  [ "x${CREATE_DES_VM1_THR}x" != "xx" ] && \
    kill -2 ${CREATE_DES_VM1_THR}
  [ "x${CREATE_DES_VM2_THR}x" != "xx" ] && \
    kill -2 ${CREATE_DES_VM2_THR}
  [ "x${CHG_FILTER_VM1_THR}x" != "xx" ] && \
    kill -2 ${CHG_FILTER_VM1_THR}
  [ "x${CHG_FILTER_VM2_THR}x" != "xx" ] && \
    kill -2 ${CHG_FILTER_VM2_THR}

  wait
}


testFail()
{
  flags="$1"
  msg="$2"

  failctr=$(($failctr + 1))
  if [ $(($flags & $FLAG_VERBOSE)) -ne 0 ]; then
    echo "FAIL : ${msg}"
  fi
  if [ $(($flags & $FLAG_WAIT)) -ne 0 ]; then
    echo "Press enter"
    read enter
  fi
  [ $(($flags & $FLAG_LIBVIRT_TEST)) -ne 0 ] && \
    test_result $(($passctr + $failctr)) "" 1
  [ $(($flags & $FLAG_TAP_TEST)) -ne 0 ] && \
    tap_fail $(($passctr + $failctr)) "${msg}"
}


testPass()
{
  flags="$1"
  msg="$2"

  passctr=$(($passctr + 1))
  if [ $(($flags & $FLAG_VERBOSE)) -ne 0 ]; then
    echo "PASS : ${msg}"
  fi
  [ $(($flags & $FLAG_LIBVIRT_TEST)) -ne 0 ] && \
    test_result $(($passctr + $failctr)) "" 0
  [ $(($flags & $FLAG_TAP_TEST)) -ne 0 ] && \
    tap_pass $(($passctr + $failctr)) "${msg}"
}

cleanup()
{
  rm -rf "$1"
  killPrgs
  exit $2
}

missedSteps()
{
  cur="$1"
  exp="$2"
  flags="$3"

  while [ $cur -lt $exp ]; do
    cur=$(($cur + 1))
    testFail "${flags}" "$4 ${cur}"
  done
}

runTest()
{
  flags="$1"

  passctr=0
  failctr=0

  tmpdir=`mktmpdir`
  failctr=0
  passctr=0
  logvm1="${PWD}/${tmpdir}/logvm1"
  logvm2="${PWD}/${tmpdir}/logvm2"
  logfivm1="${PWD}/${tmpdir}/logfivm1"
  logfivm2="${PWD}/${tmpdir}/logfivm2"

  # exp. number of steps each 'thread' has to do
  steps=10

  steps_vm1=0
  steps_vm2=0
  steps_fivm1=0
  steps_fivm2=0

  trap "cleanup ${tmpdir} 1" INT

  if [ $(($flags & $FLAG_TAP_TEST)) -ne 0 ]; then
    # Need to display the number of total tests
    tap_total=$((4 * $steps))
    echo "1..${tap_total}"
  fi

  startPrgs "${flags}" "${logvm1}" "${logvm2}" \
    "${logfivm1}" "${logfivm2}"

  [ $? -ne 0 ] && rm -rf "${tmpdir}" && return 1;

  # Test runs for a maximum of 3 minutes
  now=`date +%s`
  test_end=$(($now + 3 * 60))

  while :;
  do
    # The logs give us the number of cycles the VMs were created
    # and destroyed.
    val=$(tail -n 1 "${logvm1}" 2>/dev/null )
    while [ -n "${val}" ] && \
          [ $steps_vm1 -lt $steps ] && [ $steps_vm1 -lt $val ]; do
      steps_vm1=$(($steps_vm1 + 1))
      testPass "${flags}" \
        "VM1 log - step ${steps_vm1}"
    done

    val=$(tail -n 1 "${logvm1}" 2>/dev/null )
    while [ -n "${val}" ] && \
          [ $steps_vm2 -lt $steps ] && [ $steps_vm2 -lt $val ]; do
      steps_vm2=$(($steps_vm2 + 1))
      testPass "${flags}" \
        "VM2 log - step ${steps_vm2}"
    done

    # The changing of the filters is expected to work much faster
    val=$(cat "${logfivm1}" 2>/dev/null | tail -n 1)
    while [ -n "${val}" ] && \
          [ $steps_fivm1 -lt $steps ] && [ $steps_fivm1 -lt $(($val / 50)) ];
    do
      steps_fivm1=$(($steps_fivm1 + 1))
      testPass "${flags}" \
        "VM1 filter log - step ${steps_fivm1}"
    done

    val=$(cat "${logfivm2}" 2>/dev/null | tail -n 1)
    while [ -n "${val}" ] && \
          [ $steps_fivm2 -lt $steps ] && [ $steps_fivm2 -lt $(($val / 50)) ];
    do
      steps_fivm2=$(($steps_fivm2 + 1))
      testPass "${flags}" \
        "VM2 filter log - step ${steps_fivm2}"
    done

    [ $steps_vm1 -ge $steps ] && \
       [ $steps_vm2   -ge $steps ] && \
       [ $steps_fivm1 -ge $steps ] && \
       [ $steps_fivm2 -ge $steps ] && \
       break

    now=`date +%s`
    [ $now -gt $test_end ] && break

    sleep 4
  done

  missedSteps $steps_vm1   $steps $flags "VM1 log - step "
  missedSteps $steps_vm2   $steps $flags "VM2 log - step "
  missedSteps $steps_fivm1 $steps $flags "VM1 filter log - step "
  missedSteps $steps_fivm1 $steps $flags "VM2 filter log - step "

  [ $now -gt $test_end ] && \
    echo "Bail out! Not all tests finished before test expired. Busy system?"

  cleanup "${tmpdir}" 0
}


main() {
  prgname="$0"
  xmldir="nwfilterxml2xmlin"
  fwalldir="nwfilterxml2fwallout"
  found=0
  filtername="tck-testcase"
  libvirtdpid=-1

  flags=${FLAG_ATTACH}

  while [ $# -ne 0 ]; do
    case "$1" in
    --help|-h|-\?) usage ${prgname}; exit 0;;
    --noattach)     flags=$(($flags & ~$FLAG_ATTACH));;
    --wait)         flags=$(($flags | $FLAG_WAIT    ));;
    --verbose)      flags=$(($flags | $FLAG_VERBOSE ));;
    --libvirt-test) flags=$(($flags | $FLAG_LIBVIRT_TEST ));;
    --tap-test)     flags=$(($flags | $FLAG_TAP_TEST ));;
    --force)        flags=$(($flags | $FLAG_FORCE_CLEAN ));;
    *) usage ${prgname}; exit 1;;
    esac
    shift 1
  done

  if [ `uname` != "Linux" ]; then
    if [ $(($flags & $FLAG_TAP_TEST)) -ne 0 ]; then
      echo "1..0 # Skipped: Only valid on Linux hosts"
    else
      echo "This script will only run on Linux."
    fi
    exit 1;
  fi

  if [ $(($flags & $FLAG_TAP_TEST)) -ne 0 ]; then
    if [ "${LIBVIRT_URI}" != "qemu:///system" ]; then
        echo "1..0 # Skipped: Only valid for Qemu system driver"
        exit 0
    fi

    for name in `virsh list --all | awk '{print $2}'`
    do
      case ${name} in
      tck*)
        if [ "x${LIBVIRT_TCK_AUTOCLEAN}" = "x1" ] || \
           [ $(($flags & $FLAG_FORCE_CLEAN)) -ne 0 ]; then
          res=$(virsh destroy  ${name} 2>&1)
          rc1=$?
          res=$(virsh undefine ${name} 2>&1)
          rc2=$?
          if [ $rc1 -ne 0 ] && [ $rc2 -ne 0 ]; then
            echo "Bail out! Could not destroy VM ${name}: ${res}"
            exit 0
          fi
        else
          echo "Bail out! VM ${name} already exists, use --force to clean"
          exit 1
        fi
      esac
    done

    for name in `virsh nwfilter-list | awk '{print $2}'`
    do
      case ${name} in
      tck*)
        if [ "x${LIBVIRT_TCK_AUTOCLEAN}" = "x1" ] || \
           [ $(($flags & $FLAG_FORCE_CLEAN)) -ne 0 ]; then
          res=$(virsh nwfilter-undefine ${name} 2>&1)
          if [ $? -ne 0 ]; then
            echo "Bail out! Could not undefine filter ${name}: ${res}"
            exit 1
          fi
        else
          echo "Bail out! Filter ${name} already exists, use --force to clean"
          exit 1
        fi
      esac
    done
  fi

  if [ $(($flags & $FLAG_LIBVIRT_TEST)) -ne 0 ]; then
    curdir="${PWD}"
    . test-lib.sh
    if [ $? -ne 0 ]; then
        exit 1
    fi
    test_intro $this_test
    cd "${curdir}" || { echo "cd failed" >&2; exit 1;}
  fi

  runTest "${flags}"

  return 0
}

main "$@"