test_mdata.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: :email:`Jorge Schrauwen <sjorge@blackdot.be>`
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.grains.mdata as mdata
  9. from tests.support.mock import Mock, patch
  10. # Import Salt Testing Libs
  11. from tests.support.unit import TestCase
  12. class MdataGrainsTestCase(TestCase):
  13. """
  14. Test cases for mdata grains
  15. """
  16. def setup_loader_modules(self):
  17. return {
  18. mdata: {"__salt__": {}},
  19. }
  20. def test_user_mdata_missing_cmd_both(self):
  21. """
  22. When both or either of the commands is missing there should
  23. be no grain output.
  24. """
  25. grains_exp_res = {}
  26. which_mock = Mock(side_effect=[None, None])
  27. with patch("salt.utils.path.which", which_mock):
  28. grains_res = mdata._user_mdata()
  29. self.assertEqual(grains_exp_res, grains_res)
  30. def test_user_mdata_missing_cmd_one(self):
  31. """
  32. When both or either of the commands is missing there should
  33. be no grain output.
  34. """
  35. grains_exp_res = {}
  36. which_mock = Mock(side_effect=["/usr/sbin/mdata-list", None])
  37. with patch("salt.utils.path.which", which_mock):
  38. grains_res = mdata._user_mdata()
  39. self.assertEqual(grains_exp_res, grains_res)
  40. def test_user_mdata_empty_list(self):
  41. """
  42. When there are no user grains, there are no mdata-get calls
  43. so there are also no grains.
  44. """
  45. grains_exp_res = {}
  46. which_mock = Mock(side_effect=["/usr/sbin/mdata-list", "/usr/sbin/mdata-get"])
  47. cmd_mock = Mock(side_effect=[""])
  48. with patch("salt.utils.path.which", which_mock), patch.dict(
  49. mdata.__salt__, {"cmd.run": cmd_mock}
  50. ):
  51. grains_res = mdata._user_mdata()
  52. self.assertEqual(grains_exp_res, grains_res)
  53. def test_user_mdata(self):
  54. """
  55. We have a list of two grains, so there should be two mdata-get
  56. calls, resulting in 2 grains.
  57. """
  58. grains_exp_res = {
  59. "mdata": {
  60. "multi_text_data": "multi\nline\ntext",
  61. "simple_text_data": "some text data",
  62. },
  63. }
  64. which_mock = Mock(side_effect=["/usr/sbin/mdata-list", "/usr/sbin/mdata-get"])
  65. cmd_mock = Mock(
  66. side_effect=[
  67. "simple_text_data\nmulti_text_data",
  68. "some text data",
  69. "multi\nline\ntext",
  70. ]
  71. )
  72. with patch("salt.utils.path.which", which_mock), patch.dict(
  73. mdata.__salt__, {"cmd.run": cmd_mock}
  74. ):
  75. grains_res = mdata._user_mdata()
  76. self.assertEqual(grains_exp_res, grains_res)
  77. def test_sdc_mdata_missing_cmd_both(self):
  78. """
  79. When both or either of the commands is missing there should
  80. be no grain output.
  81. """
  82. which_mock = Mock(side_effect=[None, None])
  83. with patch("salt.utils.path.which", which_mock):
  84. grains = mdata._sdc_mdata()
  85. assert grains == {}
  86. def test_sdc_mdata_missing_cmd_one(self):
  87. """
  88. When both or either of the commands is missing there should
  89. be no grain output.
  90. """
  91. grains_exp_res = {}
  92. which_mock = Mock(side_effect=["/usr/sbin/mdata-list", None])
  93. with patch("salt.utils.path.which", which_mock):
  94. grains_res = mdata._sdc_mdata()
  95. self.assertEqual(grains_exp_res, grains_res)
  96. def test_sdc_mdata(self):
  97. """
  98. Simulate all mdata_get calls from a test zone.
  99. """
  100. grains_exp_res = {
  101. "mdata": {
  102. "sdc": {
  103. "alias": "test",
  104. "dns_domain": "example.org",
  105. "hostname": "test_salt",
  106. "nics": [
  107. {
  108. "gateway": "10.12.3.1",
  109. "gateways": ["10.12.3.1"],
  110. "interface": "net0",
  111. "ip": "10.12.3.123",
  112. "ips": ["10.12.3.123/24", "2001:ffff:ffff:123::123/64"],
  113. "mac": "00:00:00:00:00:01",
  114. "mtu": 1500,
  115. "netmask": "255.255.255.0",
  116. "nic_tag": "trunk",
  117. "primary": True,
  118. "vlan_id": 123,
  119. }
  120. ],
  121. "resolvers": ["10.12.3.1", "2001:ffff:ffff:123::1"],
  122. "routes": [],
  123. "server_uuid": "00000000-0000-0000-0000-000123456789",
  124. "uuid": "bae504b1-4594-47de-e2ed-e4f454776689",
  125. },
  126. },
  127. }
  128. which_mock = Mock(side_effect=["/usr/sbin/mdata-list", "/usr/sbin/mdata-get"])
  129. cmd_mock = Mock(
  130. side_effect=[
  131. "bae504b1-4594-47de-e2ed-e4f454776689",
  132. "00000000-0000-0000-0000-000123456789",
  133. "No metadata for 'sdc:datacenter_name'",
  134. "test_salt",
  135. "example.org",
  136. "test",
  137. '["10.12.3.1","2001:ffff:ffff:123::1"]',
  138. '[{"interface":"net0","mac":"00:00:00:00:00:01","vlan_id":123,"nic_tag":"trunk","gateway":"10.12.3.1","gateways":["10.12.3.1"],"netmask":"255.255.255.0","ip":"10.12.3.123","ips":["10.12.3.123/24","2001:ffff:ffff:123::123/64"],"mtu":1500,"primary":true}]',
  139. "[]",
  140. ]
  141. )
  142. with patch("salt.utils.path.which", which_mock), patch.dict(
  143. mdata.__salt__, {"cmd.run": cmd_mock}
  144. ):
  145. grains_res = mdata._sdc_mdata()
  146. self.assertEqual(grains_exp_res, grains_res)