test_cloud.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 pytest
  11. # Import 3rd-party libs
  12. # pylint: disable=import-error
  13. from salt.ext.six.moves import range # pylint: disable=redefined-builtin
  14. # Import salt libs
  15. from tests.support.case import ShellCase
  16. from tests.support.mixins import ShellCaseCommonTestsMixin
  17. # Import salt testing libs
  18. from tests.support.unit import skipIf
  19. try:
  20. import libcloud # pylint: disable=unused-import
  21. HAS_LIBCLOUD = True
  22. except ImportError:
  23. HAS_LIBCLOUD = False
  24. # pylint: enable=import-error
  25. @skipIf(HAS_LIBCLOUD is False, "salt-cloud requires >= libcloud 0.11.4")
  26. class SaltCloudCliTest(ShellCase, ShellCaseCommonTestsMixin):
  27. _call_binary_ = "salt-cloud"
  28. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  29. def test_function_arguments(self):
  30. self.assertIn(
  31. "error: --function expects two arguments: " "<function-name> <provider>",
  32. "\n".join(self.run_cloud("--function show_image -h", catch_stderr=True)[1]),
  33. )
  34. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  35. def test_list_providers_accepts_no_arguments(self):
  36. self.assertIn(
  37. "error: '--list-providers' does not accept any " "arguments",
  38. "\n".join(self.run_cloud("--list-providers ec2", catch_stderr=True)[1]),
  39. )
  40. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  41. def test_mutually_exclusive_query_options(self):
  42. test_options = ["--query", "--full-query", "--select-query", "--list-providers"]
  43. while True:
  44. for idx in range(1, len(test_options)):
  45. self.assertIn(
  46. "error: The options {0}/{1} are mutually "
  47. "exclusive. Please only choose one of them".format(
  48. test_options[0], test_options[idx]
  49. ),
  50. "\n".join(
  51. self.run_cloud(
  52. "{0} {1}".format(test_options[0], test_options[idx]),
  53. catch_stderr=True,
  54. )[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. @pytest.mark.slow_test(seconds=10) # Test takes >5 and <=10 seconds
  63. def test_mutually_exclusive_list_options(self):
  64. test_options = ["--list-locations", "--list-images", "--list-sizes"]
  65. while True:
  66. for idx in range(1, len(test_options)):
  67. output = self.run_cloud(
  68. "{0} ec2 {1} ec2".format(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