1
0

test_cloud.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. 'error: --function expects two arguments: '
  31. '<function-name> <provider>',
  32. '\n'.join(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. 'error: \'--list-providers\' does not accept any '
  37. 'arguments',
  38. '\n'.join(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. '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. '\n'.join(
  52. self.run_cloud(
  53. '{0} {1}'.format(test_options[0], test_options[idx]),
  54. catch_stderr=True)[1]
  55. )
  56. )
  57. # Remove the first option from the list
  58. test_options.pop(0)
  59. if len(test_options) <= 1:
  60. # Only one left? Stop iterating
  61. break
  62. def test_mutually_exclusive_list_options(self):
  63. test_options = ['--list-locations', '--list-images', '--list-sizes']
  64. while True:
  65. for idx in range(1, len(test_options)):
  66. output = self.run_cloud(
  67. '{0} ec2 {1} ec2'.format(
  68. test_options[0], test_options[idx]
  69. ), catch_stderr=True
  70. )
  71. try:
  72. self.assertIn(
  73. 'error: The options {0}/{1} are mutually '
  74. 'exclusive. Please only choose one of them'.format(
  75. test_options[0], test_options[idx]
  76. ),
  77. '\n'.join(output[1])
  78. )
  79. except AssertionError:
  80. print(output)
  81. raise
  82. # Remove the first option from the list
  83. test_options.pop(0)
  84. if len(test_options) <= 1:
  85. # Only one left? Stop iterating
  86. break