test_nexus.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt libs
  5. import salt.modules.nexus as nexus
  6. # Import Salt testing libs
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.mock import MagicMock, patch
  9. from tests.support.unit import TestCase
  10. class nexusTestCase(TestCase, LoaderModuleMockMixin):
  11. def setup_loader_modules(self):
  12. return {nexus: {}}
  13. def test_artifact_get_metadata(self):
  14. with patch(
  15. "salt.modules.nexus._get_artifact_metadata_xml",
  16. MagicMock(
  17. return_value="""<?xml version="1.0" encoding="UTF-8"?>
  18. <metadata>
  19. <groupId>com.company.sampleapp.web-module</groupId>
  20. <artifactId>web</artifactId>
  21. <versioning>
  22. <release>0.1.0</release>
  23. <versions>
  24. <version>0.0.1</version>
  25. <version>0.0.2</version>
  26. <version>0.0.3</version>
  27. <version>0.1.0</version>
  28. </versions>
  29. <lastUpdated>20171010143552</lastUpdated>
  30. </versioning>
  31. </metadata>"""
  32. ),
  33. ):
  34. metadata = nexus._get_artifact_metadata(
  35. nexus_url="http://nexus.example.com/repository",
  36. repository="libs-releases",
  37. group_id="com.company.sampleapp.web-module",
  38. artifact_id="web",
  39. headers={},
  40. )
  41. self.assertEqual(metadata["latest_version"], "0.1.0")
  42. def test_snapshot_version_get_metadata(self):
  43. with patch(
  44. "salt.modules.nexus._get_snapshot_version_metadata_xml",
  45. MagicMock(
  46. return_value="""<?xml version="1.0" encoding="UTF-8"?>
  47. <metadata modelVersion="1.1.0">
  48. <groupId>com.company.sampleapp.web-module</groupId>
  49. <artifactId>web</artifactId>
  50. <version>0.0.2-SNAPSHOT</version>
  51. <versioning>
  52. <snapshot>
  53. <timestamp>20170920.212353</timestamp>
  54. <buildNumber>3</buildNumber>
  55. </snapshot>
  56. <lastUpdated>20171112171500</lastUpdated>
  57. <snapshotVersions>
  58. <snapshotVersion>
  59. <classifier>sans-externalized</classifier>
  60. <extension>jar</extension>
  61. <value>0.0.2-20170920.212353-3</value>
  62. <updated>20170920212353</updated>
  63. </snapshotVersion>
  64. <snapshotVersion>
  65. <classifier>dist</classifier>
  66. <extension>zip</extension>
  67. <value>0.0.2-20170920.212353-3</value>
  68. <updated>20170920212353</updated>
  69. </snapshotVersion>
  70. </snapshotVersions>
  71. </versioning>
  72. </metadata>"""
  73. ),
  74. ):
  75. metadata = nexus._get_snapshot_version_metadata(
  76. nexus_url="http://nexus.example.com/repository",
  77. repository="libs-releases",
  78. group_id="com.company.sampleapp.web-module",
  79. artifact_id="web",
  80. version="0.0.2-SNAPSHOT",
  81. headers={},
  82. )
  83. self.assertEqual(
  84. metadata["snapshot_versions"]["dist"], "0.0.2-20170920.212353-3"
  85. )
  86. def test_artifact_metadata_url(self):
  87. metadata_url = nexus._get_artifact_metadata_url(
  88. nexus_url="http://nexus.example.com/repository",
  89. repository="libs-releases",
  90. group_id="com.company.sampleapp.web-module",
  91. artifact_id="web",
  92. )
  93. self.assertEqual(
  94. metadata_url,
  95. "http://nexus.example.com/repository/libs-releases/com/company/sampleapp/web-module/web/maven-metadata.xml",
  96. )
  97. def test_snapshot_version_metadata_url(self):
  98. metadata_url = nexus._get_snapshot_version_metadata_url(
  99. nexus_url="http://nexus.example.com/repository",
  100. repository="libs-snapshots",
  101. group_id="com.company.sampleapp.web-module",
  102. artifact_id="web",
  103. version="0.0.2-SNAPSHOT",
  104. )
  105. self.assertEqual(
  106. metadata_url,
  107. "http://nexus.example.com/repository/libs-snapshots/com/company/sampleapp/web-module/web/0.0.2-SNAPSHOT/maven-metadata.xml",
  108. )
  109. def test_construct_url_for_released_version(self):
  110. artifact_url, file_name = nexus._get_release_url(
  111. repository="libs-releases",
  112. group_id="com.company.sampleapp.web-module",
  113. artifact_id="web",
  114. packaging="zip",
  115. nexus_url="http://nexus.example.com/repository",
  116. version="0.1.0",
  117. )
  118. self.assertEqual(
  119. artifact_url,
  120. "http://nexus.example.com/repository/libs-releases/com/company/sampleapp/web-module/web/0.1.0/web-0.1.0.zip",
  121. )
  122. self.assertEqual(file_name, "web-0.1.0.zip")
  123. def test_construct_url_for_snapshot_version(self):
  124. with patch(
  125. "salt.modules.nexus._get_snapshot_version_metadata",
  126. MagicMock(
  127. return_value={"snapshot_versions": {"zip": "0.0.2-20170920.212353-3"}}
  128. ),
  129. ):
  130. artifact_url, file_name = nexus._get_snapshot_url(
  131. nexus_url="http://nexus.example.com/repository",
  132. repository="libs-snapshots",
  133. group_id="com.company.sampleapp.web-module",
  134. artifact_id="web",
  135. version="0.2.0-SNAPSHOT",
  136. packaging="zip",
  137. headers={},
  138. )
  139. self.assertEqual(
  140. artifact_url,
  141. "http://nexus.example.com/repository/libs-snapshots/com/company/sampleapp/web-module/web/0.2.0-SNAPSHOT/web-0.0.2-20170920.212353-3.zip",
  142. )
  143. self.assertEqual(file_name, "web-0.0.2-20170920.212353-3.zip")