eject-tests.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0-or-later
  3. # Copyright (c) International Business Machines Corp., 2001
  4. # Copyright (c) 2016 Cyril Hrubis <chrubis@suse.cz>
  5. # Author: Manoj Iyer <manjo@mail.utexas.edu>
  6. #
  7. # Tests basic functionality of eject command.
  8. TST_CNT=4
  9. TST_SETUP=setup
  10. TST_CLEANUP=cleanup
  11. TST_TESTFUNC=test
  12. TST_NEEDS_TMPDIR=1
  13. TST_NEEDS_ROOT=1
  14. TST_NEEDS_CMDS="eject"
  15. . tst_test.sh
  16. setup()
  17. {
  18. CD_DRIVE="/dev/cdrom"
  19. if ! [ -e "$CD_DRIVE" ]; then
  20. tst_brk TCONF "There is no "$CD_DRIVE""
  21. fi
  22. if grep -q "$CD_DRIVE" /proc/mounts; then
  23. tst_brk TCONF "$CD_DRIVE is already mounted"
  24. fi
  25. ROD mkdir "cdrom"
  26. }
  27. cleanup()
  28. {
  29. # We have to use the mount point since /dev/cdrom may be link to
  30. # /dev/sr0 and because of that /dev/cdrom is not listed in /proc/mounts
  31. tst_umount "$PWD/cdrom"
  32. }
  33. test1()
  34. {
  35. EXPECT_PASS eject -d \> eject.out
  36. if grep -q "eject: default device:" eject.out; then
  37. tst_res TPASS "Eject listed default device"
  38. else
  39. tst_res TFAIL "Eject failed to list default device"
  40. cat eject.out
  41. fi
  42. }
  43. test2()
  44. {
  45. EXPECT_PASS eject -v $CD_DRIVE \> eject.out
  46. if grep -q "CD-ROM eject command succeeded" eject.out; then
  47. # Close the tray if it is supported.
  48. eject -t $CD_DRIVE > /dev/null 2>&1
  49. tst_res TPASS "Drive successfully ejected"
  50. else
  51. tst_res TFAIL "Eject failed"
  52. cat eject.out
  53. fi
  54. }
  55. mount_cdrom()
  56. {
  57. local tries=100
  58. # Wait for the drive to spin up the disk
  59. while [ $tries -gt 0 ]; do
  60. eject_check_tray $CD_DRIVE
  61. if [ $? -eq 4 ]; then
  62. break
  63. fi
  64. tst_sleep 100ms
  65. tries=$((tries-1))
  66. done
  67. mount "$CD_DRIVE" cdrom/ > mount.out 2>&1
  68. if [ $? -eq 32 ]; then
  69. tst_res TCONF "Failed to mount $CD_DRIVE, no disk in drive?"
  70. cat mount.out
  71. return 0
  72. fi
  73. tst_res TINFO "$CD_DRIVE mounted sucessfully"
  74. return 1
  75. }
  76. test3()
  77. {
  78. if mount_cdrom; then
  79. return
  80. fi
  81. test2
  82. if grep -q "$CD_DRIVE" /proc/mounts; then
  83. tst_res TFAIL "$CD_DRIVE is stil moutned"
  84. else
  85. tst_res TPASS "$CD_DRIVE umounted succesfully"
  86. fi
  87. }
  88. test4()
  89. {
  90. if mount_cdrom; then
  91. return
  92. fi
  93. EXPECT_PASS eject -a on $CD_DRIVE
  94. eject_check_tray $CD_DRIVE
  95. if [ $? -eq 2 ]; then
  96. tst_brk TBROK "$CD_DRIVE is mounted but tray is open"
  97. fi
  98. EXPECT_PASS umount $CD_DRIVE
  99. eject_check_tray $CD_DRIVE
  100. if [ $? -eq 2 ]; then
  101. tst_res TPASS "$CD_DRIVE was auto-ejected"
  102. else
  103. tst_res TFAIL "$CD_DRIVE was not auto-ejected"
  104. fi
  105. EXPECT_PASS eject -a off $CD_DRIVE
  106. eject -t $CD_DRIVE > /dev/null 2>&1
  107. if mount_cdrom; then
  108. return
  109. fi
  110. EXPECT_PASS umount $CD_DRIVE
  111. eject_check_tray $CD_DRIVE
  112. if [ $? -eq 2 ]; then
  113. tst_res TFAIL "$CD_DRIVE was auto-ejected"
  114. else
  115. tst_res TPASS "$CD_DRIVE was not auto-ejected"
  116. fi
  117. }
  118. tst_run