You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
3.0 KiB

  1. #!/usr/bin/env bash
  2. #
  3. # Unit test for symlinks
  4. # Run test on a test set e.g. an artificially created rootfs
  5. #
  6. SYMLINKS_BIN=../symlinks
  7. ROOTFS_FOLDER=rootfs
  8. ROOTFS_DANGLING_LINKS=3
  9. TEST_FOLDER=$(dirname $0)
  10. echo "* Running unit tests on $TEST_FOLDER/$ROOTFS_FOLDER"
  11. ###################################################################################################
  12. # Simple test which should cover all test cases implemented in (generate-rootfs.sh):
  13. #
  14. # Test case #1: normal chained / cascaded symlinks in the case of software versioning
  15. # Expected Result: do nothing, everything is fine here!
  16. #
  17. # Test case #2: absolute symlinks
  18. # Expected result: change absolute symlinks into relative ones
  19. #
  20. # Test case #3: detect dangling symlinks as in the case of messed up library versioning
  21. # Expected result: detect and delete all symlinks which are involved
  22. #
  23. # Test case #4: recursive mess of absolute symlinks
  24. # Expected result: resolve all symlinks, at least after second run!
  25. ###################################################################################################
  26. # let's run symlink against the just generated rootfs folder and see how it does:
  27. # cd into rootfs folder first
  28. cd $TEST_FOLDER/$ROOTFS_FOLDER
  29. # call 'symlinks' to convert all absolute symlinks to relative ones with the following options:
  30. # -v verbose
  31. # -r recursive
  32. # -c change absolute links into relative links
  33. COUNT=0
  34. while [ "$(../$SYMLINKS_BIN -verc . | grep absolute)" ]; do
  35. COUNT=$((COUNT+1))
  36. echo "Test run #$COUNT"
  37. done
  38. # call 'symlinks' again to get rid of the remaining dangling symlinks which could not be fixed
  39. echo "Last run to fix the remaining dangling symlinks"
  40. DANGLING_SYMLINKS_COUNT=$(../$SYMLINKS_BIN -verd . | grep dangling | wc -l)
  41. echo "Removed $DANGLING_SYMLINKS_COUNT dangling links!"
  42. if [ $DANGLING_SYMLINKS_COUNT -lt $ROOTFS_DANGLING_LINKS ]; then
  43. echo "Detected and removed too less broken symlinks!"
  44. elif [ $DANGLING_SYMLINKS_COUNT -gt $ROOTFS_DANGLING_LINKS ]; then
  45. echo "Detected and removed too many broken symlinks!"
  46. fi
  47. # now let's look if there are broken symlinks left, 'find' offers some easy way to do that
  48. BROKEN_SYMLINKS=$(find -L -type l)
  49. # and also if there are still existing (maybe even working) absolute symlinks left
  50. ABSOLUTE_SYMLINKS=$(find -type l -exec readlink {} \; | grep "^/")
  51. if [ "$BROKEN_SYMLINKS" ] || [ "$ABSOLUTE_SYMLINKS" ]; then
  52. echo "Test failed, program was not able to fix all symlink problems..."
  53. if [ "$BROKEN_SYMLINKS" ]; then
  54. echo "The following symlinks have not been fixed:"
  55. for link in $BROKEN_SYMLINKS; do
  56. echo -e "\t * $link -> $(readlink $link)"
  57. done
  58. fi
  59. if [ "$ABSOLUTE_SYMLINKS" ]; then
  60. echo "There are still (maybe even working) absolute symlinks left:"
  61. for link in $ABSOLUTE_SYMLINKS; do
  62. echo -e "\t * $link -> $(readlink $link)"
  63. done
  64. fi
  65. else
  66. echo "Test succeeded, it seems symlinks was able to solve all symlink problems!"
  67. fi