Plan 9 from Bell Labs’s /usr/web/sources/contrib/de0u/root/sys/src/cmd/divergefs/tests/test.rc

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#!/bin/rc

# test script for fs
#
# directory structure:
#   
#   mnt/  "directory on which fs will be mounted on"
#     stuff.8
#     morestuff.8
#     stuff.txt
#     mntsub/
#     readonly0/
#     readonly1/
#   layer0/
#     boot/
#   layer1/
#   layer2/
#     stuff.8
#   layer3/
#   layer4/
#


curdir=`{pwd}
remotefs=$curdir^/../8.out
tarfs=$curdir^/tapefs/8.tarfs

if(test ! -f $tarfs) {
  cd tapefs
  echo making tarfs ...
  mk 8.tarfs
  cd ..
}

patch=ape/patch
cp $remotefs /tmp
cp $tarfs /tmp
fs=/tmp/8.out
tarfs=/tmp/8.tarfs
conffile=$curdir^/.fsrc
testdir=$home^'/testtmp'
dirlist=(mnt mnt/mntsub mnt/readonly0 mnt/readonly1 layer0 layer0/boot layer1 layer2 layer3 layer4)

testlist=( \
  test_baseread \
  test_extension \
  test_fileregex \
  test_create \
  test_create_dir_ondemand \
  test_remove \
  test_massive_remove \
  test_same_dir_remove \
  test_copy_on_write \
  test_wstat \
  test_mode \
  test_patch \
  test_mkdir)
# testlist=(test_baseread)

# {{{ fn setup_dir
fn setup_dir {
  for(d in $dirlist) {
    echo mkdir $d ...
    mkdir $d
  }
}
# }}}

# {{{ fn make_file
fn make_file {
  if(test $#* -ne 2) {
    echo make_file arguments $#*, but need 2
    status='make_file argument # error'
  }
  if not {
    echo making file: $2 content: $1
    echo $1 > $2
  }
}
# }}}

# {{{ fn cmp_file_content
fn cmp_file_content {
  if(test $#* -ne 2) {
    echo cmp_file_content arguments $#*, but need 2
    status='cmp_file_content argument # error'
  }
  if not {
    cmp $1 <{echo $2}
  }
}
# }}}

# {{{ fn cmp_mnt_file_content 
fn cmp_mnt_file_content {
  if(test $#* -ne 2) {
    echo cmp_mnt_file_content arguments $#*, but need 2
    status='cmp_mnt_file_content argument # error'
  }
  if not {
    cmp_file_content mnt/$1 $2
  }
}
# }}} 

# {{{ fn populate_dir
fn populate_dir {
  echo popluating dir ...

  make_file 'mnt stuff txt' mnt/stuff.txt
  make_file 'mnt stuff 8' mnt/stuff.8
  make_file 'layer2 stuff 8' layer2/stuff.8
  make_file 'mnt morestuff 8' mnt/morestuff.8
  make_file 'hello world' hello.c
  echo mounting tarfs on mnt/readonly0 ...
  tar -cf hello.tar hello.c
  $tarfs -m mnt/readonly0 hello.tar
  echo mounting tarfs on mnt/readonly1 ...
  cp $curdir/pci.tar .
  $tarfs -m mnt/readonly1 pci.tar
}
# }}}

# {{{ fn make_fs_conf
fn make_fs_conf {
  echo making fs config file ...

  confcontent='

layer4  ext<toplayer> # a bogus layer
layer3  mode<x> || dregex<^/boot$> # see how much executables we get

layer2  ext<8> # some comment please

layer1  \
  ext<bo\>gus> && (ext<errrr> || ext<grr>) \
  # test out line continuation \
wow and even continuation inside comment ...

layer1 fregex<lex\.yy\..*> || fregex<y\.tab\..*> # for yacc/lex

layer0 all<>

'

  echo $confcontent > $conffile
  cat $conffile
}
# }}}

# {{{ fn mount_fs
fn mount_fs {
  echo $fs $testargs $fsargs mnt 
  $fs $testargs $fsargs mnt 
}
# }}}

# {{{ fn make_target_testdir
fn make_target_testdir {
  if(test -d $testdir) {
    echo $testdir exists, removing ...
    unmount $testdir/mnt
    rm -r $testdir
  }
  mkdir $testdir
}
# }}}

# {{{ fn setup
fn setup {
  echo setup
  make_fs_conf
  make_target_testdir
  echo changing dir to $testdir
  cd $testdir
  setup_dir
  populate_dir
  mount_fs
}
# }}}

# {{{ fn tear_down
fn tear_down {
  echo tear down
  cd $testdir
  unmount mnt
  unmount mnt/readonly0
  unmount mnt/readonly1
  cd $curdir
  rm -r $testdir
  rm -r $conffile
}
# }}} 


# {{{ fn test_must_succeed
fn test_must_succeed {
  echo this test must succeed ...
}
# }}}

# {{{ fn test_must_fail
fn test_must_fail {
  echo this test must fail && status='must fail'
}
# }}}

# {{{ fn test_must_error
fn test_must_error {
  # suicide does not generate a note for us
  $curdir^/8.out
}
# }}}

# {{{ fn test_baseread
fn test_baseread {
  cmp_mnt_file_content stuff.txt 'mnt stuff txt' && \
  test -f mnt/morestuff.8 && \
  cmp_mnt_file_content morestuff.8 'mnt morestuff 8' && \
  ls mnt && \
  cmp <{ls mnt} <{echo 'mnt/stuff.8
mnt/morestuff.8
mnt/stuff.txt
mnt/mntsub
mnt/readonly0
mnt/readonly1
mnt/boot' | sort}

}
# }}}

# {{{ fn test_extension
fn test_extension {
  cmp_mnt_file_content stuff.8 'layer2 stuff 8'
}
# }}}

# {{{ fn test_fileregex
fn test_fileregex {
  make_file 'yacc yacc' mnt/y.tab.c && \
  cmp_mnt_file_content y.tab.c 'yacc yacc' && \
  test -f layer1/y.tab.c && \
  cmp_file_content layer1/y.tab.c 'yacc yacc' && \
  make_file 'yacc 8' mnt/y.tab.8 && \
  ! test -f layer1/y.tab.8 && \
  test -f layer2/y.tab.8 && \
  cmp_file_content layer2/y.tab.8 'yacc 8' && \
  make_file 'lex h' mnt/lex.yy.h
}
# }}}


# {{{ fn test_create
fn test_create {
  make_file 'mnt createstuff.8' mnt/createstuff.8 && \
  make_file 'mnt createstuff.txt' mnt/createstuff.txt && \
  cmp_file_content layer2/createstuff.8 'mnt createstuff.8' && \
  cmp_mnt_file_content createstuff.txt 'mnt createstuff.txt' && \
  ! test -f layer2/createstuff.txt && \
  ! test -f layer1/createstuff.txt && \
  test -f layer0/createstuff.txt && \
  mkdir mnt/makesub && \
  mkdir mnt/confusesub.8 && \
  test -d layer4/makesub && \
  test -d layer4/confusesub.8 && \
  unmount mnt && \
  cmp_file_content layer0/createstuff.txt 'mnt createstuff.txt' && \
  ! test -f mnt/createstuff.8 && \
  ! test -d mnt/makesub && \
  ! test -d mnt/confusesub.8
}
# }}}

# {{{ fn test_create_dir_ondemand
fn test_create_dir_ondemand {
  make_file 'mnt substuff.8' mnt/mntsub/substuff.8 && \
  cmp_file_content layer2/mntsub/substuff.8 'mnt substuff.8' && \
  ! test -f layer1/mntsub/substuff.8 && \
  unmount mnt && \
  ! test -f mnt/mntsub/substuff.8
}
# }}}

# {{{ fn test_remove
fn test_remove {
  rm mnt/stuff.8 && \
  ! test -f layer2/stuff.8 && \
  ! cat mnt/stuff.8 && \
  ls mnt && \
  ! test -f mnt/stuff.8 && \
  make_file 'recreate stuff.8' mnt/stuff.8 && \
  test -f layer2/stuff.8 && \
  test -f mnt/stuff.8 && \
  cmp_mnt_file_content stuff.8 'recreate stuff.8' && \
  cmp_file_content layer2/stuff.8 'recreate stuff.8' && \
  rm mnt/stuff.txt && \
  ! test -f mnt/stuff.txt && \
  unmount mnt && \
  test -f mnt/stuff.8
}
# }}}

# {{{ fn test_massive_remove
fn test_massive_remove {
  total=500
  echo creating $total files
  i=0
  while(test $i -ne $total) {
    echo create stuff$i.8 > layer2/stuff$i.8
    i=`{echo $i + 1 | bc}
  }

  echo removing $total files
  i=0
  while(test $i -ne $total) {
    rm mnt/stuff$i.8
    i=`{echo $i + 1 | bc}
  }

  ! test -f mnt/stuff0.8 && \
  ! test -f mnt/stuff1.8 && \
  ! test -f mnt/stuff490.8
}
# }}}

# {{{ fn test_same_dir_remove
fn test_same_dir_remove {
  rm mnt/stuff.8 && \
  ! test -f mnt/stuff.8 && \
  ! echo mnt/* | grep 'mnt/stuff\.8'
}
# }}}

# {{{ fn test_copy_on_write
fn test_copy_on_write {
  echo 'morestuff 8 layer2' >> mnt/morestuff.8
  cmp_file_content layer2/morestuff.8 \
'mnt morestuff 8
morestuff 8 layer2'
}
# }}}

# {{{ fn test_wstat
fn test_wstat {
  chmod 755 mnt/stuff.8 && \
  ls -l mnt/stuff.8 && \
  test -x mnt/stuff.8 && \
  chmod 400 mnt/stuff.txt && \
  ls -l mnt/stuff.txt && \
  test ! -w mnt/stuff.txt && \
  chmod 000 mnt/morestuff.8 && \
  test ! -r mnt/morestuff.8 && \
  chmod 400 mnt/mntsub
}
# }}} 

# {{{ fn test_move
fn test_move {
  mv mnt/stuff.8 mnt/stuff.8.orig &&
  mv mnt/readonly0/hello.c mnt/readonly0/hello.c.orig &&
  echo ls mnt/readonly0 &&
  ls mnt/readonly0 &&
  test -f mnt/readonly0/hello.c.orig &&
  test ! -f mnt/readonly0/hello.c &&
  test -f layer3/readonly0/hello.c.orig
}
# }}} 

# {{{ fn test_patch
fn test_patch {
  cd mnt/readonly1 &&
  $patch -p9 < patchboot &&
  cd ../.. &&
  ls mnt/readonly1 &&
  ls layer0 &&
  ls layer0/readonly1 &&
  ls layer1 &&
  ls layer2
}
# }}} 

# {{{ fn test_mode
fn test_mode {
  echo chmod 755 mnt/stuff.8 && \
  chmod 755 mnt/stuff.8 && \
  echo test -x mnt/stuff.8 && \
  test -x mnt/stuff.8 && \
  echo mv mnt/stuff.8 mnt/mntsub/stuff.8 && \
  mv mnt/stuff.8 mnt/mntsub/stuff.8 && \
  echo ls layer3 && \
  ls layer3 && \
  echo test -x layer3/mntsub/stuff.8 && \
  test -x layer3/mntsub/stuff.8
}
# }}}

# {{{ fn test_exec
fn test_exec {
  make_file '#!/bin/rc
echo test exec' mnt/stuff.rc && \
  chmod 755 mnt/stuff.rc && \
  mnt/stuff.rc && \
  make_file '
#include <u.h>
#include <libc.h>

void main(int, char**)
{
  print("hello world!\n");
  exits(nil);
}
' mnt/hello.c && \
  8c -o mnt/hello.8 mnt/hello.c && \
  8l -o mnt/hello mnt/hello.8 && \
  for(i in `{seq 1 100}) {
    mnt/hello
  }
}
# }}}

# {{{ fn test_mkdir
fn test_mkdir {
  mkdir readonly0 && \
  mkdir readonly0/a && \
  mkdir readonly0/a/b && \
  mkdir readonly0/a/b/c && \
  mkdir readonly0/a/b/c/d && \
  mkdir readonly0/a/b/c/d/e && \
  test -d readonly0/a/b/c/d/e && \
  rm -r readonly0/a && \
  ! test -d readonly0/a && \
  mkdir readonly0/a && \
  mkdir readonly0/a/b && \
  mkdir readonly0/a/b/c && \
  mkdir readonly0/a/b/c/d && \
  mkdir readonly0/a/b/c/d/e && \
  test -d readonly0/a/b/c/d/e && \
  rm -r readonly0/a
}
# }}}

totaltestsuccess=0
totaltestfailure=0
totaltesterror=0

# {{{ fn add_success
fn add_success {
  totaltestsuccess=`{echo $totaltestsuccess + 1 | bc}
}
# }}}

# {{{ fn add_failure 
fn add_failure {
  totaltestfailure=`{echo $totaltestfailure + 1 | bc}
}
# }}}

# {{{ fn add_error 
fn add_error {
  totaltesterror=`{echo $totaltesterror + 1 | bc}
}
# }}}

# {{{ fn note
fn note {
  echo ERROR occurred
  add_error
}
# }}}

# {{{ fn print_result
fn print_result {
  echo test result \
    $totaltestsuccess success \
    $totaltestfailure failures \
    $totaltesterror errors 
}
# }}}

# {{{ fn run_tests
fn run_tests {
  testargs=-f^$conffile
  for(t in $testlist) {
    if(setup) {}
    if not {
      echo setup FAILED
      exit 1
    }
    echo running $t ...
    if($t) {
      echo $t PASSED
      add_success
    }
    if not {
      echo $t FAILED: $status
      add_failure
    }
    tear_down
  }
}
# }}} 

# {{{ fn test_defaultpath {
fn test_defaultpath {
  test -d layer0/files &&
  test -f layer0/rules &&
  mkdir layer0/files/boot &&
  test_baseread
}
# }}}

# {{{ fn run_defaultarg_test {
fn run_defaultarg_test {
  testargs='-player0'
  setup
  if(test_defaultpath) {
    echo test_defaultpath for default path PASSED
    add_success
  }
  if not {
    echo test_defaultpath FAILED: $status
    add_failure
  }
  tear_down
}
# }}}


# {{{ fn main
fn main {
  run_tests
  run_defaultarg_test
  print_result
}
# }}}


fsargs=$*
main

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.