TestAlertDiskSpace.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env python
  2. '''
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. '''
  17. import alert_disk_space
  18. from mock.mock import patch, MagicMock
  19. from ambari_commons.os_check import OSCheck
  20. from stacks.utils.RMFTestCase import *
  21. from only_for_platform import get_platform, not_for_platform, only_for_platform, PLATFORM_LINUX, PLATFORM_WINDOWS
  22. if get_platform() != PLATFORM_WINDOWS:
  23. os_distro_value = ('Suse','11','Final')
  24. from pwd import getpwnam
  25. else:
  26. #No Windows tests for now, but start getting prepared
  27. os_distro_value = ('win2012serverr2','6.3','WindowsServer')
  28. class TestAlertDiskSpace(RMFTestCase):
  29. @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))
  30. @patch('alert_disk_space._get_disk_usage')
  31. @patch("os.path.isdir")
  32. @patch.object(OSCheck, "get_os_family", new = MagicMock(return_value = 'redhat'))
  33. def test_linux_flow(self, isdir_mock, disk_usage_mock):
  34. isdir_mock.return_value = False
  35. # / OK, /usr/hdp OK
  36. disk_usage_mock.return_value = alert_disk_space.DiskInfo(
  37. total = 21673930752L, used = 5695861760L,
  38. free = 15978068992L)
  39. res = alert_disk_space.execute()
  40. self.assertEqual(res,
  41. ('OK', ['Capacity Used: [26.28%, 5.7 GB], Capacity Total: [21.7 GB]']))
  42. # / WARNING, /usr/hdp OK
  43. disk_usage_mock.return_value = alert_disk_space.DiskInfo(
  44. total = 21673930752L, used = 14521533603L,
  45. free = 7152397149L)
  46. res = alert_disk_space.execute()
  47. self.assertEqual(res, (
  48. 'WARNING',
  49. ['Capacity Used: [67.00%, 14.5 GB], Capacity Total: [21.7 GB]']))
  50. # / CRITICAL, /usr/hdp OK
  51. disk_usage_mock.return_value = alert_disk_space.DiskInfo(
  52. total = 21673930752L, used = 20590234214L,
  53. free = 1083696538)
  54. res = alert_disk_space.execute()
  55. self.assertEqual(res, ('CRITICAL',
  56. ['Capacity Used: [95.00%, 20.6 GB], Capacity Total: [21.7 GB]']))
  57. # / < 5GB, /usr/hdp OK
  58. disk_usage_mock.return_value = alert_disk_space.DiskInfo(
  59. total = 5418482688L, used = 1625544806L,
  60. free = 3792937882L)
  61. res = alert_disk_space.execute()
  62. self.assertEqual(res, ('WARNING', [
  63. 'Capacity Used: [30.00%, 1.6 GB], Capacity Total: [5.4 GB]. Total free space is less than 5.0 GB']))
  64. # / OK, /usr/hdp WARNING
  65. disk_usage_mock.side_effect = [
  66. alert_disk_space.DiskInfo(total = 21673930752L, used = 5695861760L,
  67. free = 15978068992L),
  68. alert_disk_space.DiskInfo(total = 21673930752L, used = 14521533603L,
  69. free = 7152397149L)]
  70. # trigger isdir(/usr/hdp) to True
  71. isdir_mock.return_value = True
  72. res = alert_disk_space.execute()
  73. self.assertEqual(res, (
  74. 'WARNING', ["Capacity Used: [26.28%, 5.7 GB], Capacity Total: [21.7 GB]. "
  75. "Insufficient space at /usr/hdp: Capacity Used: [67.00%, 14.5 GB], Capacity Total: [21.7 GB]"]))
  76. # / OK, /usr/hdp CRITICAL
  77. disk_usage_mock.side_effect = [
  78. alert_disk_space.DiskInfo(total = 21673930752L, used = 5695861760L,
  79. free = 15978068992L),
  80. alert_disk_space.DiskInfo(total = 21673930752L, used = 20590234214L,
  81. free = 1083696538L)]
  82. res = alert_disk_space.execute()
  83. self.assertEqual(res, (
  84. 'WARNING', ["Capacity Used: [26.28%, 5.7 GB], Capacity Total: [21.7 GB]. "
  85. "Insufficient space at /usr/hdp: Capacity Used: [95.00%, 20.6 GB], Capacity Total: [21.7 GB]"]))
  86. # / OK, /usr/hdp < 5GB
  87. disk_usage_mock.side_effect = [
  88. alert_disk_space.DiskInfo(total = 21673930752L, used = 5695861760L,
  89. free = 15978068992L),
  90. alert_disk_space.DiskInfo(total = 5418482688L, used = 1625544806L,
  91. free = 3792937882L)]
  92. res = alert_disk_space.execute()
  93. self.assertEqual(res, (
  94. 'WARNING', ["Capacity Used: [26.28%, 5.7 GB], Capacity Total: [21.7 GB]. "
  95. "Insufficient space at /usr/hdp: Capacity Used: [30.00%, 1.6 GB], Capacity Total: [5.4 GB]. Total free space is less than 5.0 GB"]))