summaryrefslogtreecommitdiffstats
path: root/tests/include.rc
blob: bcaafb90f6619d9afac088387306ab72f7e12432 (plain)
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
M0=${M0:=/mnt/glusterfs/0};   # 0th mount point for FUSE
M1=${M1:=/mnt/glusterfs/1};   # 1st mount point for FUSE
M2=${M2:=/mnt/glusterfs/2};   # 2nd mount point for FUSE
N0=${N0:=/mnt/nfs/0};         # 0th mount point for NFS
N1=${N1:=/mnt/nfs/1};         # 1st mount point for NFS
V0=${V0:=patchy};             # volume name to use in tests
V1=${V1:=patchy1};            # volume name to use in tests
B0=${B0:=/d/backends};        # top level of brick directories
CC=cc
OSTYPE=$(uname -s)

ENV_RC=$(dirname $0)/../env.rc
if [ ! -f $ENV_RC ]; then
   ENV_RC=$(dirname $0)/../../env.rc
fi

if [ ! -f $ENV_RC ]; then
   echo "Aborting."
   echo
   echo "env.rc not found"
   echo
   echo "Please correct the problem and try again."
   echo
   exit 1
fi
. $ENV_RC

H0=${H0:=`hostname`}; # hostname
MOUNT_TYPE_FUSE="fuse.glusterfs"
GREP_MOUNT_OPT_RO="grep (ro"
GREP_MOUNT_OPT_RW="grep (rw"

PATH=$PATH:${PWD}/tests/utils

case $OSTYPE in
Linux)
  H0=${H0:=`hostname --fqdn`}; # hostname
  ;;
NetBSD)
  MOUNT_TYPE_FUSE="puffs|perfuse|fuse.glusterfs"
  GREP_MOUNT_OPT_RO="grep (read-only"
  GREP_MOUNT_OPT_RW="grep -v (read-only"
  ;;
*)
  ;;
esac

DEBUG=${DEBUG:=0}             # turn on debugging?

PROCESS_UP_TIMEOUT=20
NFS_EXPORT_TIMEOUT=20
CHILD_UP_TIMEOUT=20
PROBE_TIMEOUT=20
REBALANCE_TIMEOUT=120
REOPEN_TIMEOUT=20
HEAL_TIMEOUT=60
MARKER_UPDATE_TIMEOUT=20
JANITOR_TIMEOUT=60
UMOUNT_TIMEOUT=5

statedumpdir=`gluster --print-statedumpdir`; # Default directory for statedump

CLI="gluster --mode=script --wignore";
GFS="glusterfs --attribute-timeout=0 --entry-timeout=0";

mkdir -p $B0;
mkdir -p $M0 $M1 $M2;
mkdir -p $N0 $N1;

case $OSTYPE in
FreeBSD | Darwin)
wc () {
   if test "x$1" = "x-l"; then
      awk '{ lines++ } END {print lines}'
   fi
   if test "x$1" = "x-w"; then
      awk '{ words += NF } END {print words}' }
   fi
   if test "x$1" = "x-c"; then
      awk '{ chars += length($0) + 1 } END {print chars}'
   fi
   if test "x$1" = "x-m"; then
      awk '{ chars += length($0) + 1 } END {print chars}'
   fi
}
;;
esac

testcnt=`egrep '^[[:space:]]*(EXPECT|EXPECT_NOT|TEST|EXPECT_WITHIN|EXPECT_KEYWORD)[[:space:]]' $0 | wc -l`
expect_tests=`egrep '^[[:space:]]*TESTS_EXPECTED_IN_LOOP[[:space:]]*' $0`

x_ifs=$IFS
IFS=$'\n'
for line in $expect_tests; do
        expect_tests=`echo $line | cut -f 2 -d =`
        testcnt=`expr $testcnt + $expect_tests`
done
IFS=$x_ifs

echo 1..$testcnt

t=1

function dbg()
{
        [ "x$DEBUG" = "x0" ] || echo "$*" >&2;
}


function test_header()
{
        dbg "=========================";
        dbg "TEST $t (line $TESTLINE): $*";
        saved_cmd="$*"
}


function test_footer()
{
        RET=$?
        local err=$1

        if [ $RET -eq 0 ]; then
                echo "ok $t";
        else
                echo "not ok $t $err";
                # With DEBUG, this was already printed out, so skip it.
                if [ x"$DEBUG" = x"0" ]; then
                        echo "FAILED COMMAND: $saved_cmd"
                fi
                if [ "$EXIT_EARLY" = "1" ]; then
                        exit $RET
                fi
        fi

        dbg "RESULT $t: $RET";

        t=`expr $t + 1`;
}

function test_expect_footer()
{
        local e=$1
        local a=$2
        local err=""

        if ! [[ "$a" =~ $e ]]; then
                err="Got \"$a\" instead of \"$e\""
        fi
        [[ "$a" =~ $e ]];

        test_footer "$err";
}

function _EXPECT()
{
        TESTLINE=$1;
        shift;
        local a=""

        test_header "$@";

        e="$1";
        shift;
        a=$("$@" | tail -1)

        if [ "x$e" = "x" ] ; then
                test_expect_footer "x$e" "x$a";
        else
                test_expect_footer "$e" "$a";
        fi
}

function test_expect_not_footer()
{
        local e=$1
        local a=$2
        local err=""

        if [[ "$a" =~ $e ]]; then
                err="Got \"$a\" when not expecting it"
        fi

        ! [[ "$a" =~ "$e" ]];
        test_footer "$err";
}

function _EXPECT_NOT()
{
        TESTLINE=$1;
        shift;
        local a=""

        test_header "$@";

        e="$1";
        shift;
        a=$("$@" | tail -1)

        if [ "x$e" = "x" ] ; then
               test_expect_not_footer "x$e" "x$a";
        else
               test_expect_not_footer "$e" "$a";
        fi
}

function _EXPECT_KEYWORD()
{
        TESTLINE=$1;
        shift;

        test_header "$@";

        e="$1";
        shift;
        "$@" | tail -1 | grep -q "$e"

        test_footer;
}


function _TEST()
{
        TESTLINE=$1;
        shift;
        local redirect=""

        test_header "$@";

        if [ "$1" = "!" ]; then
                redirect="2>&1"
        fi

        eval "$@" >/dev/null $redirect

        test_footer;
}

function _EXPECT_WITHIN()
{
        TESTLINE=$1
        shift;

        local timeout=$1
        shift;

        test_header "$@"

        e=$1;
        a="";
        shift;

        local endtime=$(( ${timeout}+`date +%s` ))

        while [ `date +%s` -lt $endtime ]; do
                a=$("$@" | tail -1 ; exit ${PIPESTATUS[0]})
                ## Check command success
                if [ $? -ne 0 ]; then
                        break;
                fi

                ## Check match success
                if [[ "$a" =~ "$e" ]]; then
                        break;
                fi
                sleep 1;
        done

        if [ "x$e" = "x" ] ; then
               test_expect_footer "x$e" "x$a";
        else
               test_expect_footer "$e" "$a";
        fi
}


function SKIP_TESTS()
{
        dbg "Skipping tests $t-$testcnt";
        while [ $t -le $testcnt ]; do
                true ; test_footer;
        done
}


function _TEST_IN_LOOP()
{
        testcnt=`expr $testcnt + 1`;
        _TEST $@
}

which killall > /dev/null || {
  killall() {
    pkill $@
  }
}

which pidof > /dev/null || {
  pidof() {
    pidof.py $@
  }
}

stat -c %s /dev/null > /dev/null 2>&1 || {
  stat() {
    local format=""
    local f=""

    if [ "x$1" = "x-c" ] ; then
      argc=3
      format=$2
      f=$3
    else
      argc=1
      f=$1
    fi

    if [ $# -ne $argc ] ; then
      echo 'Usage: stat [-c format] file'
      exit 2;
    fi

    if [ "x${format}" = "x" ] ; then
      `which stat` $f
    else
      echo ${format} | sed "
        s/%u/`$( which stat ) -f %u $f`/g;
        s/%g/`$( which stat ) -f %g $f`/g;
        s/%a/`$( which stat ) -f %p $f |sed 's/^..//; s/^0//'`/g;
        s/%A/`ls -l $f|awk '{print $1}'`/g;
        s/%s/`$( which stat ) -f %z $f`/g;
        s/%h/`$( which stat ) -f %l $f`/g;
        s/%F/`$( which stat ) -f %HT $f | sed '
          s/Directory/directory/;
          s/Fifo file/fifo/;
          s/Symbolic Link/symbolic link/;
          s/Regular File/regular file/;
          s/Block Device/block special file/;
          s/Character Device/character special file/;
        ' | sed \"$(
          test -s $f && echo 's/regular file/regular empty file/g'
        )\"`/g;
        s|%n|`$( which stat ) -f %N $f`|g;
        s/%Y/`$( which stat ) -f %m $f`/g;
        s/%b/`$( which stat ) -f %b $f`/g;
        s/%B/512/g;
      "
    fi
  }
}

function cleanup()
{
        # unmount filesystems before killing daemons to avoid deadllocks
        MOUNTPOINTS=`mount | grep "$B0/" | awk '{print $3}'`
        for m in $MOUNTPOINTS;
        do
                umount $m
        done

        killall -15 glusterfs glusterfsd glusterd 2>/dev/null || true;
        test x"$OSTYPE" = x"NetBSD" && pkill -15 perfused || true
        # allow completion of signal handlers for SIGTERM before issue SIGKILL
        sleep 1
        killall -9 glusterfs glusterfsd glusterd 2>/dev/null || true;
        test x"$OSTYPE" = x"NetBSD" && pkill -9 perfused || true

        # unregister nfs and related services from portmapper/rpcbind
        ## nfs
        rpcinfo -d 100003 3 2>/dev/null || true;
        ## mountd
        rpcinfo -d 100005 1 2>/dev/null || true;
        rpcinfo -d 100005 3 2>/dev/null || true;
        ## nlockmgr
        rpcinfo -d 100021 1 2>/dev/null || true;
        rpcinfo -d 100021 4 2>/dev/null || true;
        ## nfs_acl
        rpcinfo -d 100227 3 2>/dev/null || true;

        type cleanup_lvm &>/dev/null && cleanup_lvm || true;

        case `uname -s` in
        Linux)
                LOOPDEVICES=`losetup -a | grep "$B0/" | \
                             awk '{print $1}' | tr -d :`
                for l in $LOOPDEVICES;
                do
                        losetup -d $l
                done
                ;;
        NetBSD)
                vnd=`vnconfig -l | \
                     awk '!/not in use/{printf("%s%s:%d ", $1, $2, $5);}'`
                for l in ${vnd} ; do
                        dev=${l%%:*}
                        tmp=${l#*:}
                        fs=${tmp%%:*}
                        inode=${tmp#*:}
                        file=`find -x ${fs} -inum ${inode} -print -exit`
                        echo ${file} | grep "$B0/" && \
                            LOOPDEVICES="${LOOPDEVICES} $dev"
                done
                for l in $LOOPDEVICES;
                do
                        vnconfig -u $l
                done
                ;;
        *)
                echo "`uname -s` loopback device supportmissing"
                ;;
        esac

        if [ -n "${GLUSTERD_WORKDIR}" ] ; then
            rm -rf $GLUSTERD_WORKDIR/* $B0/* /etc/glusterd/*;
        fi

        # unmount all stale mounts from /tmp, This is a temporary work around
        # till the stale mount in /tmp is found.
        umount -l /tmp/mnt* 2>/dev/null
        umount -l $M0 2>/dev/null || umount -f $M0 2>/dev/null || true;
        umount -l $M1 2>/dev/null || umount -f $M1 2>/dev/null || true;
        umount -l $N0 2>/dev/null || umount -f $N0 2>/dev/null || true;
        umount -l $N1 2>/dev/null || umount -f $N1 2>/dev/null || true;

}

function volinfo_field()
{
    local vol=$1;
    local field=$2;

    $CLI volume info $vol | grep "^$field: " | sed 's/.*: //';
}

function cleanup_tester ()
{
    local exe=$1
    rm -f $exe
}

function build_tester ()
{
    local cfile=$1
    local fname=$(basename "$cfile")
    local ext="${fname##*.}"
    local execname="${fname%.*}"
    shift
    local cflags=$*
    $CC -g -o $(dirname $cfile)/$execname $cfile $cflags
}

function process_leak_count ()
{
    local pid=$1;
    return $(ls -lh /proc/$pid/fd | grep "(deleted)"| wc -l)
}

which truncate > /dev/null || {
  truncate() {
    local nocreate=0
    local ioblocks=0
    local fileref=""
    local newsize=""

    args=`getopt xor:s: $*`
    if [ $? -ne 0 ]; then
      echo 'Usage: truncate [-co](-r file | -s size) file ...'
      exit 2
    fi
    set -- $args
    while [ $# -gt 0 ]; do
      case "$1" in
      -c)
        nocreate=1;
        ;;
      -o)
        ioblocks=1;
        echo "Unimplemented -o option"
        exit 2
        ;;
      -r)
        fileref=$2;
        shift;
        ;;
      -s)
        newsize=$2;
        shift;
        ;;
      --)
        shift;
        break;
        ;;
      *)
        echo 'Usage: truncate [-co](-r file | -s size) file ...'
        exit 2;
        ;;
      esac
      shift
    done

    if [ "x$newsize" = "x" -a "x$fileref" = "x" ] ; then
      echo 'Usage: truncate [-co](-r file | -s size) file ...'
      exit 2;
    fi

    if [ "x$newsize" != "x" -a "x$fileref" != "x" ] ; then
      echo 'Usage: truncate [-co](-r file | -s size) file ...'
      exit 2;
    fi

    if [ "x$newsize" != "x" ] ; then
      echo $newsize | grep -q '^[-_<>%/]' && {
        echo "Unimplemented prefix in ${newsize}"
        exit 2;
      }

      echo $newsize | egrep -q '[GTPEZY]B?$' && {
        echo "Unit not implemented for ${newsize}"
        exit 2;
      }

      case $newsize in
      *KB)
        newsize=$(( ${newsize/KB/} * 1000 ))
        ;;
      *K)
        newsize=$(( ${newsize/K/} * 1024 ))
        ;;
      *MB)
        newsize=$(( ${newsize/MB/} * 1000 * 1000 ))
        ;;
      *M)
        newsize=$(( ${newsize/M/} * 1024 * 1024 ))
        ;;
      esac

    fi

    if [ "x$fileref" != "x" ] ; then
       if [ ! -f $fileref ] ; then
         echo "File does not exists: ${fileref}"
         exit 2;
       fi
       newsize=`ls -l ${fileref}|awk '{print $5}'`
    fi

    if [ $# -eq 0 ]; then
      echo 'Usage: truncate [-co](-r file | -s size) file ...'
      exit 2;
    fi

    for f in $* ; do
      if [ "x$nocreate" = "x1" -a ! -f $f ] ; then
        continue;
      fi

      dd bs=1 seek=$newsize if=/dev/null of=$f msgfmt=quiet
    done
  }
}

which md5sum > /dev/null || {
  md5sum() {
    for f in $* ; do
      md5 $f | awk -F'[() ]' '{printf("%s  %s\n", $6, $3)}'
    done
  }
}

which setfattr > /dev/null || {
  setfattr() {
    setfattr.py $@
  }
}

which getfattr > /dev/null || {
  getfattr() {
    getfattr.py $@
  }
}

which sha1sum > /dev/null || {
  sha1sum() {
  case $OSTYPE in
  Darwin)
    for f in $* ; do
      openssl sha1 $f | awk -F'[() ]' '{printf("%s %s\n", $4, $2)}'
    done
    ;;
  NetBSD | FreeBSD)
    for f in $* ; do
      sha1 $f | awk -F'[() ]' '{printf("%s  %s\n", $6, $3)}'
    done
    ;;
  esac
  }
}

alias EXPECT='_EXPECT $LINENO'
alias EXPECT_NOT='_EXPECT_NOT $LINENO'
alias TEST='_TEST $LINENO'
alias EXPECT_WITHIN='_EXPECT_WITHIN $LINENO'
alias EXPECT_KEYWORD='_EXPECT_KEYWORD $LINENO'
alias TEST_IN_LOOP='_TEST_IN_LOOP $LINENO'
shopt -s expand_aliases

if [ x"$OSTYPE" = x"Linux" ]; then
        alias dd="dd status=none"
elif [ x"$OSTYPE" = x"NetBSD" ]; then
        alias dd="dd msgfmt=quiet"
fi
# MacOS doesn't seem to support either option.  Doing nothing at all is
# probably the safest option there and on anything we don't recognize, but
# if you want to reduce the noise level and know the correct option for
# your favorite platform please feel free to add it here.