test_disk.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, print_function, unicode_literals
  3. import os
  4. import shutil
  5. import pytest
  6. import salt.utils.platform
  7. from salt.ext import six
  8. from tests.support.case import ModuleCase
  9. from tests.support.helpers import destructiveTest, slowTest
  10. from tests.support.unit import skipIf
  11. @destructiveTest
  12. @pytest.mark.windows_whitelisted
  13. @skipIf(salt.utils.platform.is_darwin(), "No mtab on Darwin")
  14. @skipIf(salt.utils.platform.is_freebsd(), "No mtab on FreeBSD")
  15. @skipIf(salt.utils.platform.is_windows(), "No mtab on Windows")
  16. class DiskModuleVirtualizationTest(ModuleCase):
  17. """
  18. Test to make sure we return a clean result under Docker. Refs #8976
  19. This is factored into its own class so that we can have some certainty that setUp() and tearDown() are run.
  20. """
  21. def setUp(self):
  22. # Make /etc/mtab unreadable
  23. if os.path.isfile("/etc/mtab"):
  24. shutil.move("/etc/mtab", "/tmp/mtab")
  25. def test_no_mtab(self):
  26. ret = self.run_function("disk.usage")
  27. self.assertDictEqual(ret, {})
  28. def tearDown(self):
  29. if os.path.isfile("/tmp/mtab"):
  30. shutil.move("/tmp/mtab", "/etc/mtab")
  31. @pytest.mark.windows_whitelisted
  32. class DiskModuleTest(ModuleCase):
  33. """
  34. Validate the disk module
  35. """
  36. @slowTest
  37. def test_usage(self):
  38. """
  39. disk.usage
  40. """
  41. ret = self.run_function("disk.usage")
  42. self.assertTrue(isinstance(ret, dict))
  43. if not isinstance(ret, dict):
  44. return
  45. if salt.utils.platform.is_darwin():
  46. for key, val in six.iteritems(ret):
  47. self.assertTrue("filesystem" in val)
  48. self.assertTrue("512-blocks" in val)
  49. self.assertTrue("used" in val)
  50. self.assertTrue("available" in val)
  51. self.assertTrue("capacity" in val)
  52. self.assertTrue("iused" in val)
  53. self.assertTrue("ifree" in val)
  54. self.assertTrue("%iused" in val)
  55. else:
  56. for key, val in six.iteritems(ret):
  57. self.assertTrue("filesystem" in val)
  58. self.assertTrue("1K-blocks" in val)
  59. self.assertTrue("used" in val)
  60. self.assertTrue("available" in val)
  61. self.assertTrue("capacity" in val)
  62. @skipIf(salt.utils.platform.is_windows(), "inode info not available on Windows")
  63. def test_inodeusage(self):
  64. """
  65. disk.inodeusage
  66. """
  67. ret = self.run_function("disk.inodeusage")
  68. self.assertTrue(isinstance(ret, dict))
  69. if not isinstance(ret, dict):
  70. return
  71. for key, val in six.iteritems(ret):
  72. self.assertTrue("inodes" in val)
  73. self.assertTrue("used" in val)
  74. self.assertTrue("free" in val)
  75. self.assertTrue("use" in val)
  76. self.assertTrue("filesystem" in val)