test_terraform.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 Testing Libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import TestCase, skipIf
  11. from tests.support.runtests import RUNTIME_VARS
  12. from tests.support.mock import (
  13. patch,
  14. NO_MOCK,
  15. NO_MOCK_REASON
  16. )
  17. # Import Salt Libs
  18. import salt.config
  19. import salt.loader
  20. from salt.roster import terraform
  21. @skipIf(NO_MOCK, NO_MOCK_REASON)
  22. class TerraformTestCase(TestCase, LoaderModuleMockMixin):
  23. '''
  24. Test cases for salt.roster.terraform
  25. '''
  26. def setup_loader_modules(self):
  27. opts = salt.config.master_config(os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'master'))
  28. utils = salt.loader.utils(opts, whitelist=['roster_matcher'])
  29. return {
  30. terraform: {
  31. '__utils__': utils,
  32. '__opts__': {},
  33. }
  34. }
  35. def test_default_output(self):
  36. '''
  37. Test the output of a fixture tfstate file wich contains libvirt
  38. resources.
  39. '''
  40. tfstate = os.path.join(os.path.dirname(__file__), 'terraform.data', 'terraform.tfstate')
  41. pki_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'terraform.data'))
  42. with patch.dict(terraform.__opts__, {'roster_file': tfstate, 'pki_dir': pki_dir}):
  43. expected_result = {
  44. 'db0': {
  45. 'host': '192.168.122.174',
  46. 'user': 'root',
  47. 'passwd': 'dbpw',
  48. 'tty': True,
  49. 'priv': os.path.join(pki_dir, 'ssh', 'salt-ssh.rsa')},
  50. 'db1': {
  51. 'host': '192.168.122.190',
  52. 'user': 'root',
  53. 'passwd': 'dbpw',
  54. 'tty': True,
  55. 'priv': os.path.join(pki_dir, 'ssh', 'salt-ssh.rsa')},
  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. 'web1': {
  63. 'host': '192.168.122.235',
  64. 'user': 'root',
  65. 'passwd': 'linux',
  66. 'timeout': 22,
  67. 'priv': os.path.join(pki_dir, 'ssh', 'salt-ssh.rsa')}
  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 wich contains libvirt
  74. resources using matching
  75. '''
  76. tfstate = os.path.join(os.path.dirname(__file__), 'terraform.data', 'terraform.tfstate')
  77. pki_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'terraform.data'))
  78. with patch.dict(terraform.__opts__, {'roster_file': tfstate, 'pki_dir': pki_dir}):
  79. expected_result = {
  80. 'web0': {
  81. 'host': '192.168.122.106',
  82. 'user': 'root',
  83. 'passwd': 'linux',
  84. 'timeout': 22,
  85. 'priv': os.path.join(pki_dir, 'ssh', 'salt-ssh.rsa')},
  86. 'web1': {
  87. 'host': '192.168.122.235',
  88. 'user': 'root',
  89. 'passwd': 'linux',
  90. 'timeout': 22,
  91. 'priv': os.path.join(pki_dir, 'ssh', 'salt-ssh.rsa')}
  92. }
  93. ret = terraform.targets('*web*')
  94. self.assertDictEqual(expected_result, ret)