|
7 | 7 | # -------------------------------------------------------------------------------
|
8 | 8 |
|
9 | 9 | import os
|
| 10 | +import responses |
10 | 11 |
|
11 | 12 | import capycli.common.json_support
|
12 | 13 | import capycli.common.script_base
|
13 | 14 | from capycli.bom.findsources import FindSources
|
14 | 15 | from capycli.common.capycli_bom_support import CaPyCliBom, CycloneDxSupport
|
15 | 16 | from capycli.main.result_codes import ResultCode
|
16 | 17 | from tests.test_base import AppArguments, TestBase
|
| 18 | +from unittest.mock import MagicMock, patch |
17 | 19 |
|
18 | 20 |
|
19 | 21 | class TestFindSources(TestBase):
|
@@ -100,7 +102,7 @@ def test_find_sources(self) -> None:
|
100 | 102 | self.assertTrue("Using anonymous GitHub access" in out)
|
101 | 103 | self.assertTrue("8 components read from SBOM" in out)
|
102 | 104 | self.assertTrue("1 source files were already available" in out)
|
103 |
| - self.assertTrue("5 source file URLs were found" in out) |
| 105 | + self.assertTrue("6 source file URLs were found" in out) |
104 | 106 |
|
105 | 107 | sbom = CaPyCliBom.read_sbom(args.outputfile)
|
106 | 108 | self.assertIsNotNone(sbom)
|
@@ -191,3 +193,136 @@ def test_normalize_version(self):
|
191 | 193 | actual = sut.to_semver_string(version)
|
192 | 194 | self.assertEqual(actual, expected)
|
193 | 195 | self.assertTrue(actual == expected, 'version %s is %s' % (actual, expected))
|
| 196 | + |
| 197 | + @responses.activate |
| 198 | + def test_get_release_component_id(self): |
| 199 | + # Mock the sw360 client |
| 200 | + mock_client = MagicMock() |
| 201 | + mock_client.get_release.return_value = {"_links": {"sw360:component": {"href": self.MYURL + 'components/123'}}} |
| 202 | + |
| 203 | + # Call the method and assert the result |
| 204 | + find_sources = FindSources() |
| 205 | + find_sources.client = mock_client |
| 206 | + component_id = find_sources.get_release_component_id("some_release_id") |
| 207 | + self.assertEqual(component_id, "123") |
| 208 | + |
| 209 | + @responses.activate |
| 210 | + def test_find_source_url_from_component(self): |
| 211 | + # Mock the client |
| 212 | + mock_client = MagicMock() |
| 213 | + mock_client.get_component.return_value = {"_embedded": {"sw360:releases": [{"_links": {"self": {"href": self.MYURL + 'releases/456'}}}]}} |
| 214 | + mock_client.get_release.return_value = {"_links": {"sw360:component": {"href": self.MYURL + 'components/123'}}, "sourceCodeDownloadurl": "http://github.com/some/repo/0.0.0"} |
| 215 | + |
| 216 | + # Call the method and assert the result |
| 217 | + find_sources = FindSources() |
| 218 | + find_sources.client = mock_client # Inject the mocked client |
| 219 | + source_url = find_sources.find_source_url_from_component(component_id="some_component_id") |
| 220 | + self.assertEqual(source_url, "http://github.com/some/repo/0.0.0") |
| 221 | + |
| 222 | + @patch('requests.get') |
| 223 | + @patch('bs4.BeautifulSoup') |
| 224 | + def test_get_pkg_go_repo_url_success(self, mock_beautifulsoup, mock_requests_get): |
| 225 | + # Mocking successful response |
| 226 | + mock_requests_get.return_value.text = '<div class="UnitMeta-repo"><a href="https://github.com/example/repo/1.0.0">Repo Link</a></div>' |
| 227 | + mock_beautifulsoup.return_value.find.return_value = MagicMock(get=lambda x: 'https://github.com/example/repo/1.0.0') |
| 228 | + find_sources = FindSources() |
| 229 | + repo_url = find_sources.get_pkg_go_repo_url('example/package') |
| 230 | + self.assertEqual(repo_url, 'https://github.com/example/repo/1.0.0') |
| 231 | + |
| 232 | + @patch('requests.get', side_effect=Exception('Some error')) |
| 233 | + def test_get_pkg_go_repo_url_error(self, mock_requests_get): |
| 234 | + # Mocking an exception during the request |
| 235 | + find_sources = FindSources() |
| 236 | + repo_url = find_sources.get_pkg_go_repo_url('some/package') |
| 237 | + self.assertEqual(repo_url, 'https://pkg.go.dev/some/package') |
| 238 | + |
| 239 | + @patch('capycli.bom.findsources.FindSources.get_github_info') |
| 240 | + @patch('capycli.bom.findsources.FindSources.get_matching_tag') |
| 241 | + def test_find_golang_url_github(self, mock_get_github_info, mock_get_matching_tag): |
| 242 | + # Mocking a GitHub scenario |
| 243 | + mock_get_github_info.return_value = 'https://pkg.go.dev/github.com/opencontainers/runc' |
| 244 | + mock_get_matching_tag.return_value = 'https://github.com/opencontainers/runc/archive/refs/tags/v1.0.1.zip' |
| 245 | + find_sources = FindSources() |
| 246 | + component = MagicMock() |
| 247 | + component.name = 'github.com/opencontainers/runc' |
| 248 | + component.version = 'v1.0.1' |
| 249 | + source_url = find_sources.find_golang_url(component) |
| 250 | + |
| 251 | + self.assertEqual(source_url, 'https://pkg.go.dev/github.com/opencontainers/runc') |
| 252 | + |
| 253 | + def test_find_golang_url_non_github(self): |
| 254 | + # Mocking a non-GitHub scenario |
| 255 | + find_sources = FindSources() |
| 256 | + component = MagicMock() |
| 257 | + component.name = 'example/package' |
| 258 | + component.version = 'v1.0.0' |
| 259 | + source_url = find_sources.find_golang_url(component) |
| 260 | + |
| 261 | + self.assertEqual(source_url, '') |
| 262 | + |
| 263 | + def test_no_matching_tag(self): |
| 264 | + |
| 265 | + validTag = "3.2.0" |
| 266 | + invalidTag = "0.03" |
| 267 | + emptyString = "" |
| 268 | + githubUrl = "https://github.com/apache/kafka" |
| 269 | + zipball_url = "https://api.github.com/repos/apache/kafka/zipball/refs/tags/" + validTag |
| 270 | + sourceUrl = "https://github.com/apache/kafka/archive/refs/tags/" + validTag + ".zip" |
| 271 | + findResource = capycli.bom.findsources.FindSources() |
| 272 | + # test Empty tagInfo array |
| 273 | + tagInfo = [] |
| 274 | + actual = capycli.bom.findsources.FindSources.get_matching_tag(findResource, tagInfo, validTag, githubUrl) |
| 275 | + self.assertEqual(actual, None) |
| 276 | + # test Empty tag string |
| 277 | + tagInfo = [{"name": emptyString, "zipball_url": zipball_url}] |
| 278 | + actual = capycli.bom.findsources.FindSources.get_matching_tag(findResource, tagInfo, validTag, githubUrl) |
| 279 | + self.assertEqual(actual, '') |
| 280 | + # test Empty url string |
| 281 | + tagInfo = [{"name": validTag, "zipball_url": emptyString}] |
| 282 | + actual = capycli.bom.findsources.FindSources.get_matching_tag(findResource, tagInfo, validTag, githubUrl) |
| 283 | + self.assertEqual(actual, None) |
| 284 | + # test non-matching tag |
| 285 | + tagInfo = [{"name": invalidTag, "zipball_url": zipball_url}] |
| 286 | + actual = capycli.bom.findsources.FindSources.get_matching_tag(findResource, tagInfo, validTag, githubUrl) |
| 287 | + self.assertEqual(actual, '') |
| 288 | + # test valid tag |
| 289 | + tagInfo = [{"name": validTag, "zipball_url": zipball_url}] |
| 290 | + actual = capycli.bom.findsources.FindSources.get_matching_tag(findResource, tagInfo, validTag, githubUrl) |
| 291 | + self.assertEqual(actual, sourceUrl) |
| 292 | + |
| 293 | + @patch("time.sleep") |
| 294 | + def test_get_source_url_success(self, mock_sleep): |
| 295 | + mock_client = MagicMock() |
| 296 | + mock_release_id = "123" |
| 297 | + mock_source_url = "https://example.com/source.zip" |
| 298 | + |
| 299 | + mock_client.get_release.return_value = {"sourceCodeDownloadurl": mock_source_url} |
| 300 | + |
| 301 | + findsources = FindSources() |
| 302 | + findsources.client = mock_client |
| 303 | + result = findsources.get_source_url_from_release(mock_release_id) |
| 304 | + self.assertEqual(result, mock_source_url) |
| 305 | + mock_sleep.assert_not_called() |
| 306 | + |
| 307 | + def test_get_source_url_no_source_url(self): |
| 308 | + mock_client = MagicMock() |
| 309 | + mock_release_id = "123" |
| 310 | + |
| 311 | + mock_client.get_release.return_value = {"sourceCodeDownloadurl": ""} |
| 312 | + findsources = FindSources() |
| 313 | + findsources.client = mock_client |
| 314 | + |
| 315 | + result = findsources.get_source_url_from_release(mock_release_id) |
| 316 | + self.assertIsNone(result) |
| 317 | + mock_client.get_release.assert_called_once_with(mock_release_id) |
| 318 | + |
| 319 | + def test_get_source_url_exception(self): |
| 320 | + mock_client = MagicMock() |
| 321 | + mock_release_id = "123" |
| 322 | + |
| 323 | + mock_client.get_release.side_effect = Exception("Unexpected error") |
| 324 | + findsources = FindSources() |
| 325 | + findsources.client = mock_client |
| 326 | + with self.assertRaises(Exception): |
| 327 | + findsources.get_source_url_from_release(mock_release_id) |
| 328 | + mock_client.get_release.assert_called_once_with(mock_release_id) |
0 commit comments