github.com/valexz/goofys@v0.24.0/test/fuse-test.sh (about)

     1  #!/bin/bash
     2  
     3  set -o xtrace
     4  set -o errexit
     5  
     6  : ${CATFS:="false"}
     7  
     8  #COMMON=integration-test-common.sh
     9  #source $COMMON
    10  
    11  # Configuration
    12  TEST_TEXT="HELLO WORLD"
    13  TEST_TEXT_FILE=test-s3fs.txt
    14  TEST_DIR=testdir
    15  ALT_TEST_TEXT_FILE=test-s3fs-ALT.txt
    16  TEST_TEXT_FILE_LENGTH=15
    17  BIG_FILE=big-file-s3fs.txt
    18  BIG_FILE_LENGTH=$((25 * 1024 * 1024))
    19  
    20  function mk_test_file {
    21      if [ $# == 0 ]; then
    22          TEXT=$TEST_TEXT
    23      else
    24          TEXT=$1
    25      fi
    26      echo $TEXT > $TEST_TEXT_FILE
    27      if [ ! -e $TEST_TEXT_FILE ]
    28      then
    29          echo "Could not create file ${TEST_TEXT_FILE}, it does not exist"
    30          exit 1
    31      fi
    32  }
    33  
    34  function rm_test_file {
    35      if [ $# == 0 ]; then
    36          FILE=$TEST_TEXT_FILE
    37      else
    38          FILE=$1
    39      fi
    40      rm -f $FILE
    41  
    42      if [ -e $FILE ]
    43      then
    44          echo "Could not cleanup file ${TEST_TEXT_FILE}"
    45          exit 1
    46      fi
    47  }
    48  
    49  function mk_test_dir {
    50      mkdir ${TEST_DIR}
    51  
    52      if [ ! -d ${TEST_DIR} ]; then
    53          echo "Directory ${TEST_DIR} was not created"
    54          exit 1
    55      fi
    56  }
    57  
    58  function rm_test_dir {
    59      rmdir ${TEST_DIR}
    60      if [ -e $TEST_DIR ]; then
    61          echo "Could not remove the test directory, it still exists: ${TEST_DIR}"
    62          exit 1
    63      fi
    64  }
    65  
    66  function test_append_file {
    67      echo "Testing append to file ..."
    68      # Write a small test file
    69      for x in `seq 1 $TEST_TEXT_FILE_LENGTH`
    70      do
    71         echo "echo ${TEST_TEXT} to ${TEST_TEXT_FILE}"
    72      done > ${TEST_TEXT_FILE}
    73  
    74      # Verify contents of file
    75      echo "Verifying length of test file"
    76      FILE_LENGTH=`wc -l $TEST_TEXT_FILE | awk '{print $1}'`
    77      if [ "$FILE_LENGTH" -ne "$TEST_TEXT_FILE_LENGTH" ]
    78      then
    79         echo "error: expected $TEST_TEXT_FILE_LENGTH , got $FILE_LENGTH"
    80         exit 1
    81      fi
    82  
    83      rm_test_file
    84  }
    85  
    86  function test_truncate_file {
    87      echo "Testing truncate file ..."
    88      # Write a small test file
    89      echo "${TEST_TEXT}" > ${TEST_TEXT_FILE}
    90   
    91      # Truncate file to 0 length.  This should trigger open(path, O_RDWR | O_TRUNC...)
    92      : > ${TEST_TEXT_FILE}
    93    
    94      # Verify file is zero length
    95      if [ -s ${TEST_TEXT_FILE} ]
    96      then
    97          echo "error: expected ${TEST_TEXT_FILE} to be zero length"
    98          exit 1
    99      fi
   100      rm_test_file
   101  }
   102  
   103  function test_truncate_empty_file {
   104      echo "Testing truncate empty file ..."
   105      # Write an empty test file
   106      touch ${TEST_TEXT_FILE}
   107  
   108      # Truncate the file to 1024 length
   109      t_size=1024
   110      truncate ${TEST_TEXT_FILE} -s $t_size
   111  
   112      # Verify file is zero length
   113      size=$(stat -c %s ${TEST_TEXT_FILE})
   114      if [ $t_size -ne $size ]
   115      then
   116          echo "error: expected ${TEST_TEXT_FILE} to be $t_size length, got $size"
   117          exit 1
   118      fi
   119      rm_test_file
   120  }
   121  
   122  function test_mv_file {
   123      echo "Testing mv file function ..."
   124  
   125      # if the rename file exists, delete it
   126      if [ -e $ALT_TEST_TEXT_FILE ]
   127      then
   128         rm $ALT_TEST_TEXT_FILE
   129      fi
   130  
   131      if [ -e $ALT_TEST_TEXT_FILE ]
   132      then
   133         echo "Could not delete file ${ALT_TEST_TEXT_FILE}, it still exists"
   134         exit 1
   135      fi
   136  
   137      # create the test file again
   138      mk_test_file
   139  
   140      #rename the test file
   141      mv $TEST_TEXT_FILE $ALT_TEST_TEXT_FILE
   142      if [ ! -e $ALT_TEST_TEXT_FILE ]
   143      then
   144         echo "Could not move file"
   145         exit 1
   146      fi
   147  
   148      # Check the contents of the alt file
   149      ALT_TEXT_LENGTH=`echo $TEST_TEXT | wc -c | awk '{print $1}'`
   150      ALT_FILE_LENGTH=`wc -c $ALT_TEST_TEXT_FILE | awk '{print $1}'`
   151      if [ "$ALT_FILE_LENGTH" -ne "$ALT_TEXT_LENGTH" ]
   152      then
   153         echo "moved file length is not as expected expected: $ALT_TEXT_LENGTH  got: $ALT_FILE_LENGTH"
   154         exit 1
   155      fi
   156  
   157      # clean up
   158      rm_test_file $ALT_TEST_TEXT_FILE
   159  }
   160  
   161  function test_mv_directory {
   162      echo "Testing mv directory function ..."
   163      if [ -e $TEST_DIR ]; then
   164         echo "Unexpected, this file/directory exists: ${TEST_DIR}"
   165         exit 1
   166      fi
   167  
   168      mk_test_dir
   169  
   170      mv ${TEST_DIR} ${TEST_DIR}_rename
   171  
   172      if [ ! -d "${TEST_DIR}_rename" ]; then
   173         echo "Directory ${TEST_DIR} was not renamed"
   174         exit 1
   175      fi
   176  
   177      rmdir ${TEST_DIR}_rename
   178      if [ -e "${TEST_DIR}_rename" ]; then
   179         echo "Could not remove the test directory, it still exists: ${TEST_DIR}_rename"
   180         exit 1
   181      fi
   182  }
   183  
   184  function test_redirects {
   185      echo "Testing redirects ..."
   186  
   187      mk_test_file ABCDEF
   188  
   189      CONTENT=`cat $TEST_TEXT_FILE`
   190  
   191      if [ "${CONTENT}" != "ABCDEF" ]; then
   192         echo "CONTENT read is unexpected, got ${CONTENT}, expected ABCDEF"
   193         exit 1
   194      fi
   195  
   196      echo XYZ > $TEST_TEXT_FILE
   197  
   198      CONTENT=`cat $TEST_TEXT_FILE`
   199  
   200      if [ ${CONTENT} != "XYZ" ]; then
   201         echo "CONTENT read is unexpected, got ${CONTENT}, expected XYZ"
   202         exit 1
   203      fi
   204  
   205      echo 123456 >> $TEST_TEXT_FILE
   206  
   207      LINE1=`sed -n '1,1p' $TEST_TEXT_FILE`
   208      LINE2=`sed -n '2,2p' $TEST_TEXT_FILE`
   209  
   210      if [ ${LINE1} != "XYZ" ]; then
   211         echo "LINE1 was not as expected, got ${LINE1}, expected XYZ"
   212         exit 1
   213      fi
   214  
   215      if [ ${LINE2} != "123456" ]; then
   216         echo "LINE2 was not as expected, got ${LINE2}, expected 123456"
   217         exit 1
   218      fi
   219  
   220      # clean up
   221      rm_test_file
   222  }
   223  
   224  function test_mkdir_rmdir {
   225      echo "Testing creation/removal of a directory"
   226  
   227      if [ -e $TEST_DIR ]; then
   228         echo "Unexpected, this file/directory exists: ${TEST_DIR}"
   229         exit 1
   230      fi
   231  
   232      mk_test_dir
   233      rm_test_dir
   234  }
   235  
   236  function test_chmod {
   237      echo "Testing chmod file function ..."
   238  
   239      # create the test file again
   240      mk_test_file
   241  
   242      ORIGINAL_PERMISSIONS=$(stat --format=%a $TEST_TEXT_FILE)
   243  
   244      chmod 777 $TEST_TEXT_FILE;
   245  
   246      # if they're the same, we have a problem.
   247      if [ $(stat --format=%a $TEST_TEXT_FILE) == $ORIGINAL_PERMISSIONS ]
   248      then
   249        echo "Could not modify $TEST_TEXT_FILE permissions"
   250        exit 1
   251      fi
   252  
   253      # clean up
   254      rm_test_file
   255  }
   256  
   257  function test_chown {
   258      echo "Testing chown file function ..."
   259  
   260      # create the test file again
   261      mk_test_file
   262  
   263      ORIGINAL_PERMISSIONS=$(stat --format=%u:%g $TEST_TEXT_FILE)
   264  
   265      chown 1000:1000 $TEST_TEXT_FILE;
   266  
   267      # if they're the same, we have a problem.
   268      if [ $(stat --format=%a $TEST_TEXT_FILE) == $ORIGINAL_PERMISSIONS ]
   269      then
   270        echo "Could not modify $TEST_TEXT_FILE ownership"
   271        exit 1
   272      fi
   273  
   274      # clean up
   275      rm_test_file
   276  }
   277  
   278  function test_list {
   279      echo "Testing list"
   280      mk_test_file
   281      mk_test_dir
   282  
   283      file_cnt=$(ls -1 | wc -l)
   284      if [ $file_cnt != 2 ]; then
   285          echo "Expected 2 file but got $file_cnt"
   286          exit 1
   287      fi
   288  
   289      rm_test_file
   290      rm_test_dir
   291  }
   292  
   293  function test_remove_nonempty_directory {
   294      echo "Testing removing a non-empty directory"
   295      mk_test_dir
   296      touch "${TEST_DIR}/file"
   297      rmdir "${TEST_DIR}" 2>&1 | grep -q "Directory not empty"
   298      rm "${TEST_DIR}/file"
   299      rm_test_dir
   300  }
   301  
   302  function test_rename_before_close {
   303      echo "Testing rename before close ..."
   304      (
   305          echo foo
   306          mv $TEST_TEXT_FILE ${TEST_TEXT_FILE}.new
   307      ) > $TEST_TEXT_FILE
   308  
   309      if ! cmp <(echo foo) ${TEST_TEXT_FILE}.new; then
   310          echo "rename before close failed"
   311          exit 1
   312      fi
   313  
   314      rm_test_file ${TEST_TEXT_FILE}.new
   315      rm -f ${TEST_TEXT_FILE}
   316  }
   317  
   318  function test_multipart_upload {
   319      echo "Testing multi-part upload ..."
   320      dd if=/dev/urandom of="/tmp/${BIG_FILE}" bs=$BIG_FILE_LENGTH count=1
   321      dd if="/tmp/${BIG_FILE}" of="${BIG_FILE}" bs=$BIG_FILE_LENGTH count=1
   322  
   323      # Verify contents of file
   324      echo "Comparing test file"
   325      if ! cmp "/tmp/${BIG_FILE}" "${BIG_FILE}"
   326      then
   327         exit 1
   328      fi
   329  
   330      rm -f "/tmp/${BIG_FILE}"
   331      rm_test_file "${BIG_FILE}"
   332  }
   333  
   334  function test_multipart_copy {
   335      echo "Testing multi-part copy ..."
   336      dd if=/dev/urandom of="/tmp/${BIG_FILE}" bs=$BIG_FILE_LENGTH count=1
   337      dd if="/tmp/${BIG_FILE}" of="${BIG_FILE}" bs=$BIG_FILE_LENGTH count=1
   338      mv "${BIG_FILE}" "${BIG_FILE}-copy"
   339  
   340      # Verify contents of file
   341      echo "Comparing test file"
   342      if ! cmp "/tmp/${BIG_FILE}" "${BIG_FILE}-copy"
   343      then
   344         exit 1
   345      fi
   346  
   347      rm -f "/tmp/${BIG_FILE}"
   348      rm_test_file "${BIG_FILE}-copy"
   349  }
   350  
   351  function test_special_characters {
   352      echo "Testing special characters ..."
   353  
   354      ls 'special' 2>&1 | grep -q 'No such file or directory'
   355      ls 'special?' 2>&1 | grep -q 'No such file or directory'
   356      ls 'special*' 2>&1 | grep -q 'No such file or directory'
   357      ls 'special~' 2>&1 | grep -q 'No such file or directory'
   358      ls 'special&' 2>&1 | grep -q 'No such file or directory'
   359      ls 'special@' 2>&1 | grep -q 'No such file or directory'
   360      ls 'specialĀµ' 2>&1 | grep -q 'No such file or directory'
   361  }
   362  
   363  function test_symlink {
   364      echo "Testing symlinks ..."
   365  
   366      rm -f $TEST_TEXT_FILE
   367      rm -f $ALT_TEST_TEXT_FILE
   368      echo foo > $TEST_TEXT_FILE
   369  
   370      ln -s $TEST_TEXT_FILE $ALT_TEST_TEXT_FILE
   371      cmp $TEST_TEXT_FILE $ALT_TEST_TEXT_FILE
   372  
   373      rm -f $TEST_TEXT_FILE
   374  
   375      [ -L $ALT_TEST_TEXT_FILE ]
   376      [ ! -f $ALT_TEST_TEXT_FILE ]
   377  }
   378  
   379  function test_extended_attributes {
   380      command -v setfattr >/dev/null 2>&1 || \
   381          { echo "Skipping extended attribute tests" ; return; }
   382  
   383      echo "Testing extended attributes ..."
   384  
   385      rm -f $TEST_TEXT_FILE
   386      touch $TEST_TEXT_FILE
   387  
   388      # set value
   389      setfattr -n user.key1 -v value1 $TEST_TEXT_FILE
   390      getfattr -n user.key1 --only-values $TEST_TEXT_FILE | grep -q '^value1$'
   391  
   392      # append value
   393      setfattr -n user.key2 -v value2 $TEST_TEXT_FILE
   394      getfattr -n user.key1 --only-values $TEST_TEXT_FILE | grep -q '^value1$'
   395      getfattr -n user.key2 --only-values $TEST_TEXT_FILE | grep -q '^value2$'
   396  
   397      # remove value
   398      setfattr -x user.key1 $TEST_TEXT_FILE
   399      ! getfattr -n user.key1 --only-values $TEST_TEXT_FILE
   400      getfattr -n user.key2 --only-values $TEST_TEXT_FILE | grep -q '^value2$'
   401  }
   402  
   403  function test_mtime_file {
   404      echo "Testing mtime preservation function ..."
   405  
   406      # if the rename file exists, delete it
   407      if [ -e $ALT_TEST_TEXT_FILE -o -L $ALT_TEST_TEXT_FILE ]
   408      then
   409         rm $ALT_TEST_TEXT_FILE
   410      fi
   411  
   412      if [ -e $ALT_TEST_TEXT_FILE ]
   413      then
   414         echo "Could not delete file ${ALT_TEST_TEXT_FILE}, it still exists"
   415         exit 1
   416      fi
   417  
   418      # create the test file again
   419      mk_test_file
   420      sleep 2 # allow for some time to pass to compare the timestamps between test & alt
   421  
   422      #copy the test file with preserve mode
   423      cp -p $TEST_TEXT_FILE $ALT_TEST_TEXT_FILE
   424      testmtime=`stat -c %Y $TEST_TEXT_FILE`
   425      altmtime=`stat -c %Y $ALT_TEST_TEXT_FILE`
   426      if [ "$testmtime" -ne "$altmtime" ]
   427      then
   428         echo "File times do not match:  $testmtime != $altmtime"
   429         exit 1
   430      fi
   431  }
   432  
   433  function test_rm_rf_dir {
   434     echo "Test that rm -rf will remove directory with contents"
   435     # Create a dir with some files and directories
   436     mkdir dir1
   437     mkdir dir1/dir2
   438     touch dir1/file1
   439     touch dir1/dir2/file2
   440  
   441     # Remove the dir with recursive rm
   442     rm -rf dir1
   443  
   444     if [ -e dir1 ]; then
   445         echo "rm -rf did not remove $PWD/dir1"
   446         exit 1
   447     fi
   448  }
   449  
   450  function test_write_after_seek_ahead {
   451     echo "Test writes succeed after a seek ahead"
   452     dd if=/dev/zero of=testfile seek=1 count=1 bs=1024
   453     rm testfile
   454  }
   455  
   456  function run_all_tests {
   457      test_append_file
   458      #test_truncate_file
   459      #test_truncate_empty_file
   460      test_mv_file
   461      test_mv_directory
   462      #test_redirects
   463      test_mkdir_rmdir
   464      #test_chmod
   465      #test_chown
   466      test_list
   467      test_remove_nonempty_directory
   468      # TODO: broken: https://github.com/s3fs-fuse/s3fs-fuse/issues/145
   469      #test_rename_before_close
   470      test_multipart_upload
   471      test_multipart_copy
   472      test_special_characters
   473      #test_symlink
   474      if [ "$CATFS" != "true" ]; then        
   475          test_extended_attributes
   476      fi
   477      #test_mtime_file
   478      test_rm_rf_dir
   479      if [ "$CATFS" == "true" ]; then
   480          test_write_after_seek_ahead
   481      fi
   482  }
   483  
   484  # Mount the bucket
   485  CUR_DIR=`pwd`
   486  TEST_BUCKET_MOUNT_POINT_1=$1
   487  if [ "$TEST_BUCKET_MOUNT_POINT_1" == "" ]; then
   488      echo "Mountpoint missing"
   489      exit 1
   490  fi
   491  
   492  pushd $TEST_BUCKET_MOUNT_POINT_1
   493  mkdir test_dir
   494  cd test_dir
   495  
   496  if [ -e $TEST_TEXT_FILE ]
   497  then
   498    rm -f $TEST_TEXT_FILE
   499  fi
   500  
   501  run_all_tests
   502  
   503  # Unmount the bucket
   504  popd
   505  echo "All tests complete."