test_terraform.py 3.5 KB

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