test_terraform.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. """
  2. unittests for terraform roster
  3. """
  4. # Import Python libs
  5. import os.path
  6. # Import Salt Libs
  7. import salt.config
  8. import salt.loader
  9. from salt.roster import terraform
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import patch
  13. from tests.support.runtests import RUNTIME_VARS
  14. from tests.support.unit import TestCase
  15. class TerraformTestCase(TestCase, LoaderModuleMockMixin):
  16. """
  17. Test cases for salt.roster.terraform
  18. """
  19. def setup_loader_modules(self):
  20. opts = salt.config.master_config(
  21. os.path.join(RUNTIME_VARS.TMP_CONF_DIR, "master")
  22. )
  23. utils = salt.loader.utils(opts, whitelist=["roster_matcher"])
  24. return {terraform: {"__utils__": utils, "__opts__": {}}}
  25. def test_default_output(self):
  26. """
  27. Test the output of a fixture tfstate file which contains libvirt
  28. resources.
  29. """
  30. tfstate = os.path.join(
  31. os.path.dirname(__file__), "terraform.data", "terraform.tfstate"
  32. )
  33. pki_dir = os.path.abspath(
  34. os.path.join(os.path.dirname(__file__), "terraform.data")
  35. )
  36. with patch.dict(
  37. terraform.__opts__, {"roster_file": tfstate, "pki_dir": pki_dir}
  38. ):
  39. expected_result = {
  40. "db0": {
  41. "host": "192.168.122.174",
  42. "user": "root",
  43. "passwd": "dbpw",
  44. "tty": True,
  45. "priv": os.path.join(pki_dir, "ssh", "salt-ssh.rsa"),
  46. },
  47. "db1": {
  48. "host": "192.168.122.190",
  49. "user": "root",
  50. "passwd": "dbpw",
  51. "tty": True,
  52. "priv": os.path.join(pki_dir, "ssh", "salt-ssh.rsa"),
  53. },
  54. "web0": {
  55. "host": "192.168.122.106",
  56. "user": "root",
  57. "passwd": "linux",
  58. "timeout": 22,
  59. "priv": os.path.join(pki_dir, "ssh", "salt-ssh.rsa"),
  60. },
  61. "web1": {
  62. "host": "192.168.122.235",
  63. "user": "root",
  64. "passwd": "linux",
  65. "timeout": 22,
  66. "priv": os.path.join(pki_dir, "ssh", "salt-ssh.rsa"),
  67. },
  68. }
  69. ret = terraform.targets("*")
  70. self.assertDictEqual(expected_result, ret)
  71. def test_default_matching(self):
  72. """
  73. Test the output of a fixture tfstate file which contains libvirt
  74. resources using matching
  75. """
  76. tfstate = os.path.join(
  77. os.path.dirname(__file__), "terraform.data", "terraform.tfstate"
  78. )
  79. pki_dir = os.path.abspath(
  80. os.path.join(os.path.dirname(__file__), "terraform.data")
  81. )
  82. with patch.dict(
  83. terraform.__opts__, {"roster_file": tfstate, "pki_dir": pki_dir}
  84. ):
  85. expected_result = {
  86. "web0": {
  87. "host": "192.168.122.106",
  88. "user": "root",
  89. "passwd": "linux",
  90. "timeout": 22,
  91. "priv": os.path.join(pki_dir, "ssh", "salt-ssh.rsa"),
  92. },
  93. "web1": {
  94. "host": "192.168.122.235",
  95. "user": "root",
  96. "passwd": "linux",
  97. "timeout": 22,
  98. "priv": os.path.join(pki_dir, "ssh", "salt-ssh.rsa"),
  99. },
  100. }
  101. ret = terraform.targets("*web*")
  102. self.assertDictEqual(expected_result, ret)