netns_sysfs.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/sh
  2. #==============================================================================
  3. # Copyright (c) 2015 Red Hat, Inc.
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of version 2 the GNU General Public License as
  7. # published by the Free Software Foundation.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #
  17. # Written by Matus Marhefka <mmarhefk@redhat.com>
  18. #
  19. #==============================================================================
  20. #
  21. # Tests that a separate network namespace cannot affect sysfs contents
  22. # of the main namespace.
  23. #==============================================================================
  24. TCID="netns_sysfs"
  25. TST_TOTAL=3
  26. NS_TYPE="net,mnt"
  27. DUMMYDEV_HOST="dummy_test0"
  28. DUMMYDEV="dummy_test1"
  29. . test.sh
  30. if tst_kvcmp -lt "2.6.35"; then
  31. tst_brkm TCONF "sysfs is not mount namespace aware for kernels older than 2.6.35"
  32. fi
  33. setns_check
  34. if [ $? -eq 32 ]; then
  35. tst_brkm TCONF "setns not supported"
  36. fi
  37. cleanup()
  38. {
  39. tst_rmdir
  40. ip link del $DUMMYDEV_HOST 2>/dev/null
  41. ip link del $DUMMYDEV 2>/dev/null
  42. kill -9 $NS_HANDLE 2>/dev/null
  43. }
  44. tst_tmpdir
  45. NS_HANDLE=$(ns_create $NS_TYPE)
  46. if [ $? -eq 1 ]; then
  47. tst_resm TINFO "$NS_HANDLE"
  48. tst_brkm TBROK "unable to create a new network namespace"
  49. fi
  50. TST_CLEANUP=cleanup
  51. ip link add $DUMMYDEV_HOST type dummy || \
  52. tst_brkm TBROK "failed to add a new (host) dummy device"
  53. ns_exec $NS_HANDLE $NS_TYPE mount --make-rprivate /sys
  54. ns_exec $NS_HANDLE $NS_TYPE ip link add $DUMMYDEV type dummy || \
  55. tst_brkm TBROK "failed to add a new dummy device"
  56. ns_exec $NS_HANDLE $NS_TYPE mount -t sysfs none /sys 2>/dev/null
  57. # TEST CASE #1
  58. ns_exec $NS_HANDLE $NS_TYPE test -e /sys/class/net/$DUMMYDEV
  59. if [ $? -eq 0 ]; then
  60. tst_resm TPASS "sysfs in new namespace has $DUMMYDEV interface"
  61. else
  62. tst_resm TFAIL "sysfs in new namespace does not have $DUMMYDEV interface"
  63. fi
  64. # TEST CASE #2
  65. ns_exec $NS_HANDLE $NS_TYPE test -e /sys/class/net/$DUMMYDEV_HOST
  66. if [ $? -ne 0 ]; then
  67. tst_resm TPASS "sysfs in new namespace does not have $DUMMYDEV_HOST interface"
  68. else
  69. tst_resm TFAIL "sysfs in new namespace contains $DUMMYDEV_HOST interface"
  70. fi
  71. # TEST CASE #3
  72. test -e /sys/class/net/$DUMMYDEV
  73. if [ $? -ne 0 ]; then
  74. tst_resm TPASS "sysfs not affected by a separate namespace"
  75. else
  76. tst_resm TFAIL "sysfs affected by a separate namespace"
  77. fi
  78. tst_exit