test_mine.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Rupesh Tare <rupesht@saltstack.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. patch,
  12. )
  13. # Import Salt Libs
  14. import salt.modules.mine as mine
  15. class MineTestCase(TestCase, LoaderModuleMockMixin):
  16. '''
  17. Test cases for salt.modules.mine
  18. '''
  19. def setup_loader_modules(self):
  20. return {mine: {}}
  21. def test_get_docker(self):
  22. '''
  23. Test for Get all mine data for 'docker.ps' and run an
  24. aggregation.
  25. '''
  26. ps_response = {
  27. 'localhost': {
  28. 'host': {
  29. 'interfaces': {
  30. 'docker0': {
  31. 'hwaddr': '88:99:00:00:99:99',
  32. 'inet': [{'address': '172.17.42.1',
  33. 'broadcast': None,
  34. 'label': 'docker0',
  35. 'netmask': '255.255.0.0'}],
  36. 'inet6': [{'address': 'ffff::eeee:aaaa:bbbb:8888',
  37. 'prefixlen': '64'}],
  38. 'up': True},
  39. 'eth0': {'hwaddr': '88:99:00:99:99:99',
  40. 'inet': [{'address': '192.168.0.1',
  41. 'broadcast': '192.168.0.255',
  42. 'label': 'eth0',
  43. 'netmask': '255.255.255.0'}],
  44. 'inet6': [{'address':
  45. 'ffff::aaaa:aaaa:bbbb:8888',
  46. 'prefixlen': '64'}],
  47. 'up': True},
  48. }},
  49. 'abcdefhjhi1234567899': { # container Id
  50. 'Ports': [{'IP': '0.0.0.0', # we bind on every interfaces
  51. 'PrivatePort': 80,
  52. 'PublicPort': 80,
  53. 'Type': 'tcp'}],
  54. 'Image': 'image:latest',
  55. 'Info': {'Id': 'abcdefhjhi1234567899'},
  56. },
  57. }}
  58. with patch.object(mine, 'get', return_value=ps_response):
  59. ret = mine.get_docker()
  60. # Sort ifaces since that will change between py2 and py3
  61. ret['image:latest']['ipv4'][80] = sorted(ret['image:latest']['ipv4'][80])
  62. self.assertEqual(ret,
  63. {'image:latest': {
  64. 'ipv4': {80: sorted([
  65. '172.17.42.1:80',
  66. '192.168.0.1:80',
  67. ])}}})
  68. def test_get_docker_with_container_id(self):
  69. '''
  70. Test for Get all mine data for 'docker.ps' and run an
  71. aggregation.
  72. '''
  73. ps_response = {
  74. 'localhost': {
  75. 'host': {
  76. 'interfaces': {
  77. 'docker0': {
  78. 'hwaddr': '88:99:00:00:99:99',
  79. 'inet': [{'address': '172.17.42.1',
  80. 'broadcast': None,
  81. 'label': 'docker0',
  82. 'netmask': '255.255.0.0'}],
  83. 'inet6': [{'address': 'ffff::eeee:aaaa:bbbb:8888',
  84. 'prefixlen': '64'}],
  85. 'up': True},
  86. 'eth0': {'hwaddr': '88:99:00:99:99:99',
  87. 'inet': [{'address': '192.168.0.1',
  88. 'broadcast': '192.168.0.255',
  89. 'label': 'eth0',
  90. 'netmask': '255.255.255.0'}],
  91. 'inet6': [{'address':
  92. 'ffff::aaaa:aaaa:bbbb:8888',
  93. 'prefixlen': '64'}],
  94. 'up': True},
  95. }},
  96. 'abcdefhjhi1234567899': { # container Id
  97. 'Ports': [{'IP': '0.0.0.0', # we bind on every interfaces
  98. 'PrivatePort': 80,
  99. 'PublicPort': 80,
  100. 'Type': 'tcp'}],
  101. 'Image': 'image:latest',
  102. 'Info': {'Id': 'abcdefhjhi1234567899'},
  103. },
  104. }}
  105. with patch.object(mine, 'get', return_value=ps_response):
  106. ret = mine.get_docker(with_container_id=True)
  107. # Sort ifaces since that will change between py2 and py3
  108. ret['image:latest']['ipv4'][80] = sorted(ret['image:latest']['ipv4'][80])
  109. self.assertEqual(ret,
  110. {'image:latest': {
  111. 'ipv4': {80: sorted([
  112. ('172.17.42.1:80', 'abcdefhjhi1234567899'),
  113. ('192.168.0.1:80', 'abcdefhjhi1234567899'),
  114. ])}}})