test_terraform.py 3.7 KB

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