test_svn.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Tests for the SVN state
  4. '''
  5. # Import python libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. import os
  8. import shutil
  9. import socket
  10. # Import Salt Testing libs
  11. from tests.support.case import ModuleCase
  12. from tests.support.paths import TMP
  13. from tests.support.mixins import SaltReturnAssertsMixin
  14. class SvnTest(ModuleCase, SaltReturnAssertsMixin):
  15. '''
  16. Validate the svn state
  17. '''
  18. def setUp(self):
  19. super(SvnTest, self).setUp()
  20. if not self.run_function('cmd.has_exec', ['svn']):
  21. self.skipTest("The executable 'svn' is not available.")
  22. self.__domain = 'svn.apache.org'
  23. try:
  24. if hasattr(socket, 'setdefaulttimeout'):
  25. # 10 second dns timeout
  26. socket.setdefaulttimeout(10)
  27. socket.gethostbyname(self.__domain)
  28. except socket.error:
  29. msg = 'error resolving {0}, possible network issue?'
  30. self.skipTest(msg.format(self.__domain))
  31. self.target = os.path.join(TMP, 'apache_http_test_repo')
  32. self.name = 'http://{0}/repos/asf/httpd/httpd/trunk/test/'.format(
  33. self.__domain
  34. )
  35. self.new_rev = '1456987'
  36. def tearDown(self):
  37. shutil.rmtree(self.target, ignore_errors=True)
  38. # Reset the dns timeout after the test is over
  39. socket.setdefaulttimeout(None)
  40. def test_latest(self):
  41. '''
  42. svn.latest
  43. '''
  44. ret = self.run_state(
  45. 'svn.latest',
  46. name=self.name,
  47. rev=self.new_rev,
  48. target=self.target,
  49. )
  50. self.assertSaltTrueReturn(ret)
  51. self.assertTrue(os.path.isdir(os.path.join(self.target, '.svn')))
  52. self.assertSaltStateChangesEqual(
  53. ret, self.name, keys=['new']
  54. )
  55. self.assertSaltStateChangesEqual(
  56. ret, self.new_rev, keys=['revision']
  57. )
  58. def test_latest_failure(self):
  59. '''
  60. svn.latest
  61. '''
  62. ret = self.run_state(
  63. 'svn.latest',
  64. name='https://youSpelledApacheWrong.com/repo/asf/httpd/trunk/',
  65. rev=self.new_rev,
  66. target=self.target,
  67. )
  68. self.assertSaltFalseReturn(ret)
  69. self.assertFalse(os.path.isdir(os.path.join(self.target, '.svn')))
  70. def test_latest_empty_dir(self):
  71. '''
  72. svn.latest
  73. '''
  74. if not os.path.isdir(self.target):
  75. os.mkdir(self.target)
  76. ret = self.run_state(
  77. 'svn.latest',
  78. name=self.name,
  79. rev=self.new_rev,
  80. target=self.target,
  81. )
  82. self.assertSaltTrueReturn(ret)
  83. self.assertTrue(os.path.isdir(os.path.join(self.target, '.svn')))
  84. def no_test_latest_existing_repo(self):
  85. '''
  86. svn.latest against existing repository
  87. '''
  88. current_rev = '1442865'
  89. cwd, basename = os.path.split(self.target)
  90. opts = ('-r', current_rev)
  91. out = self.run_function('svn.checkout',
  92. [cwd, self.name, basename, None, None, opts])
  93. assert out
  94. ret = self.run_state(
  95. 'svn.latest',
  96. name=self.name,
  97. rev=self.new_rev,
  98. target=self.target,
  99. )
  100. self.assertSaltTrueReturn(ret)
  101. self.assertSaltStateChangesEqual(
  102. ret,
  103. '{0} => {1}'.format(current_rev, self.new_rev),
  104. keys=['revision']
  105. )
  106. self.assertTrue(os.path.isdir(os.path.join(self.target, '.svn')))
  107. def no_test_latest_existing_repo_no_rev_change(self):
  108. '''
  109. svn.latest against existing repository
  110. '''
  111. current_rev = self.new_rev
  112. cwd, basename = os.path.split(self.target)
  113. opts = ('-r', current_rev)
  114. out = self.run_function('svn.checkout',
  115. [cwd, self.name, basename, None, None, opts])
  116. assert out
  117. ret = self.run_state(
  118. 'svn.latest',
  119. name=self.name,
  120. rev=self.new_rev,
  121. target=self.target,
  122. )
  123. self.assertSaltTrueReturn(ret)
  124. self.assertSaltStateChangesEqual(ret, {})
  125. self.assertTrue(os.path.isdir(os.path.join(self.target, '.svn')))