test_cloud.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # -*- coding: utf-8 -*-
  2. '''
  3. integration.cli_test
  4. ~~~~~~~~~~~~~~~~~~~~
  5. CLI related unit testing
  6. :codeauthor: Pedro Algarvio (pedro@algarvio.me)
  7. '''
  8. # Import Python libs
  9. from __future__ import absolute_import, print_function
  10. # Import salt testing libs
  11. from tests.support.unit import skipIf
  12. # Import salt libs
  13. from tests.support.case import ShellCase
  14. from tests.support.mixins import ShellCaseCommonTestsMixin
  15. # Import 3rd-party libs
  16. # pylint: disable=import-error
  17. from salt.ext.six.moves import range # pylint: disable=redefined-builtin
  18. try:
  19. import libcloud # pylint: disable=unused-import
  20. HAS_LIBCLOUD = True
  21. except ImportError:
  22. HAS_LIBCLOUD = False
  23. # pylint: enable=import-error
  24. @skipIf(HAS_LIBCLOUD is False, 'salt-cloud requires >= libcloud 0.11.4')
  25. class SaltCloudCliTest(ShellCase,
  26. ShellCaseCommonTestsMixin):
  27. _call_binary_ = 'salt-cloud'
  28. def test_function_arguments(self):
  29. self.assertIn(
  30. 'salt-cloud: error: --function expects two arguments: '
  31. '<function-name> <provider>',
  32. self.run_cloud('--function show_image -h', catch_stderr=True)[1]
  33. )
  34. def test_list_providers_accepts_no_arguments(self):
  35. self.assertIn(
  36. 'salt-cloud: error: \'--list-providers\' does not accept any '
  37. 'arguments',
  38. self.run_cloud('--list-providers ec2', catch_stderr=True)[1]
  39. )
  40. def test_mutually_exclusive_query_options(self):
  41. test_options = [
  42. '--query', '--full-query', '--select-query', '--list-providers'
  43. ]
  44. while True:
  45. for idx in range(1, len(test_options)):
  46. self.assertIn(
  47. 'salt-cloud: error: The options {0}/{1} are mutually '
  48. 'exclusive. Please only choose one of them'.format(
  49. test_options[0], test_options[idx]
  50. ),
  51. self.run_cloud(
  52. '{0} {1}'.format(test_options[0], test_options[idx]),
  53. catch_stderr=True)[1]
  54. )
  55. # Remove the first option from the list
  56. test_options.pop(0)
  57. if len(test_options) <= 1:
  58. # Only one left? Stop iterating
  59. break
  60. def test_mutually_exclusive_list_options(self):
  61. test_options = ['--list-locations', '--list-images', '--list-sizes']
  62. while True:
  63. for idx in range(1, len(test_options)):
  64. output = self.run_cloud(
  65. '{0} ec2 {1} ec2'.format(
  66. test_options[0], test_options[idx]
  67. ), catch_stderr=True
  68. )
  69. try:
  70. self.assertIn(
  71. 'salt-cloud: error: The options {0}/{1} are mutually '
  72. 'exclusive. Please only choose one of them'.format(
  73. test_options[0], test_options[idx]
  74. ),
  75. output[1]
  76. )
  77. except AssertionError:
  78. print(output)
  79. raise
  80. # Remove the first option from the list
  81. test_options.pop(0)
  82. if len(test_options) <= 1:
  83. # Only one left? Stop iterating
  84. break