2020-03-01 10:57:40 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2020-12-13 11:00:16 +01:00
|
|
|
import sys
|
2020-03-01 10:57:40 +01:00
|
|
|
import unittest
|
|
|
|
|
2020-11-04 03:13:12 +01:00
|
|
|
from testutils import ADMIN_CLIENT, suppress_urllib3_warning
|
2020-08-07 08:56:18 +02:00
|
|
|
from testutils import harbor_server
|
|
|
|
from testutils import TEARDOWN
|
2020-03-01 10:57:40 +01:00
|
|
|
import library.repository
|
|
|
|
import library.docker_api
|
2020-04-22 10:44:10 +02:00
|
|
|
import library.containerd
|
2020-03-01 10:57:40 +01:00
|
|
|
from library.base import _assert_status_code
|
|
|
|
from library.project import Project
|
|
|
|
from library.user import User
|
|
|
|
from library.repository import Repository
|
|
|
|
from library.artifact import Artifact
|
2020-12-04 11:28:29 +01:00
|
|
|
from library.repository import push_self_build_image_to_project
|
2020-03-01 10:57:40 +01:00
|
|
|
from library.repository import pull_harbor_image
|
2020-12-13 11:00:16 +01:00
|
|
|
from library.scan import Scan
|
2020-03-01 10:57:40 +01:00
|
|
|
|
2020-12-13 11:00:16 +01:00
|
|
|
class TestManifest(unittest.TestCase):
|
2020-11-04 03:13:12 +01:00
|
|
|
@suppress_urllib3_warning
|
|
|
|
def setUp(self):
|
2020-12-13 11:00:16 +01:00
|
|
|
print("Setup")
|
2020-03-01 10:57:40 +01:00
|
|
|
|
|
|
|
@unittest.skipIf(TEARDOWN == False, "Test data won't be erased.")
|
2020-12-13 11:00:16 +01:00
|
|
|
def do_tearDown(self):
|
|
|
|
"""
|
|
|
|
Tear down:
|
|
|
|
1. Delete repository(RA,RB,IA) by user(UA);
|
|
|
|
2. Delete project(PA);
|
|
|
|
3. Delete user(UA).
|
|
|
|
"""
|
2020-03-01 10:57:40 +01:00
|
|
|
#1. Delete repository(RA,RB,IA) by user(UA);
|
2020-12-13 11:00:16 +01:00
|
|
|
TestManifest.repo.delete_repoitory(TestManifest.project_name, TestManifest.index_name, **TestManifest.USER_CLIENT)
|
|
|
|
TestManifest.repo.delete_repoitory(TestManifest.project_name, TestManifest.image_a, **TestManifest.USER_CLIENT)
|
|
|
|
TestManifest.repo.delete_repoitory(TestManifest.project_name, TestManifest.image_b, **TestManifest.USER_CLIENT)
|
2020-03-01 10:57:40 +01:00
|
|
|
|
|
|
|
#2. Delete project(PA);
|
2020-12-13 11:00:16 +01:00
|
|
|
TestManifest.project.delete_project(TestManifest.project_id, **TestManifest.USER_CLIENT)
|
2020-03-01 10:57:40 +01:00
|
|
|
|
|
|
|
#3. Delete user(UA).
|
2020-12-13 11:00:16 +01:00
|
|
|
TestManifest.user.delete_user(TestManifest.user_id, **ADMIN_CLIENT)
|
2020-03-01 10:57:40 +01:00
|
|
|
|
2020-12-13 11:00:16 +01:00
|
|
|
def test_01_AddIndexByDockerManifest(self):
|
2020-03-01 10:57:40 +01:00
|
|
|
"""
|
|
|
|
Test case:
|
|
|
|
Push Index By Docker Manifest
|
|
|
|
Test step and expected result:
|
|
|
|
1. Create a new user(UA);
|
|
|
|
2. Create a new project(PA) by user(UA);
|
|
|
|
3. Create 2 new repositorys(RA,RB) in project(PA) by user(UA);
|
|
|
|
4. Push an index(IA) to Harbor by docker manifest CLI successfully;
|
2020-03-16 03:13:28 +01:00
|
|
|
5. Get Artifacts successfully;
|
|
|
|
6. Get index(IA) by reference successfully;
|
|
|
|
7. Verify harbor index is index(IA) pushed by docker manifest CLI;
|
2020-04-22 10:44:10 +02:00
|
|
|
8.1 Verify harbor index(IA) can be pulled by docker CLI successfully;
|
|
|
|
8.2 Verify harbor index(IA) can be pulled by docker CLI successfully;
|
2020-03-16 03:13:28 +01:00
|
|
|
9. Get addition successfully;
|
|
|
|
10. Unable to Delete artifact in manifest list;
|
|
|
|
11. Delete index successfully.
|
2020-03-01 10:57:40 +01:00
|
|
|
Tear down:
|
|
|
|
1. Delete repository(RA,RB,IA) by user(UA);
|
|
|
|
2. Delete project(PA);
|
|
|
|
3. Delete user(UA).
|
|
|
|
"""
|
2020-12-13 11:00:16 +01:00
|
|
|
TestManifest.project= Project()
|
|
|
|
TestManifest.user= User()
|
|
|
|
TestManifest.artifact = Artifact()
|
|
|
|
TestManifest.repo= Repository()
|
|
|
|
TestManifest.scan = Scan()
|
|
|
|
TestManifest.url = ADMIN_CLIENT["endpoint"]
|
|
|
|
TestManifest.user_push_index_password = "Aa123456"
|
|
|
|
TestManifest.index_name = "ci_test_index"
|
|
|
|
TestManifest.index_tag = "test_tag"
|
|
|
|
TestManifest.image_a = "alpine"
|
|
|
|
TestManifest.image_b = "busybox"
|
|
|
|
|
2020-03-01 10:57:40 +01:00
|
|
|
#1. Create a new user(UA);
|
2020-12-13 11:00:16 +01:00
|
|
|
TestManifest.user_id, TestManifest.user_name = TestManifest.user.create_user(user_password = TestManifest.user_push_index_password, **ADMIN_CLIENT)
|
2020-03-01 10:57:40 +01:00
|
|
|
|
2020-12-13 11:00:16 +01:00
|
|
|
TestManifest.USER_CLIENT=dict(endpoint = TestManifest.url, username = TestManifest.user_name, password = TestManifest.user_push_index_password, with_scan_overview = True)
|
2020-03-01 10:57:40 +01:00
|
|
|
|
|
|
|
#2. Create a new project(PA) by user(UA);
|
2020-12-13 11:00:16 +01:00
|
|
|
TestManifest.project_id, TestManifest.project_name = TestManifest.project.create_project(metadata = {"public": "false"}, **TestManifest.USER_CLIENT)
|
2020-03-01 10:57:40 +01:00
|
|
|
|
|
|
|
#3. Create 2 new repositorys(RA,RB) in project(PA) by user(UA);
|
2020-12-13 11:00:16 +01:00
|
|
|
repo_name_a, tag_a = push_self_build_image_to_project(TestManifest.project_name, harbor_server, 'admin', 'Harbor12345', TestManifest.image_a, "latest")
|
|
|
|
repo_name_b, tag_b = push_self_build_image_to_project(TestManifest.project_name, harbor_server, 'admin', 'Harbor12345', TestManifest.image_b, "latest")
|
2020-03-01 10:57:40 +01:00
|
|
|
|
|
|
|
#4. Push an index(IA) to Harbor by docker manifest CLI successfully;
|
|
|
|
manifests = [harbor_server+"/"+repo_name_a+":"+tag_a, harbor_server+"/"+repo_name_b+":"+tag_b]
|
2020-12-13 11:00:16 +01:00
|
|
|
index = harbor_server+"/"+TestManifest.project_name+"/"+TestManifest.index_name+":"+TestManifest.index_tag
|
|
|
|
TestManifest.index_sha256_cli_ret, TestManifest.manifests_sha256_cli_ret = library.docker_api.docker_manifest_push_to_harbor(index, manifests, harbor_server, TestManifest.user_name, TestManifest.user_push_index_password)
|
2020-03-01 10:57:40 +01:00
|
|
|
|
2020-03-16 03:13:28 +01:00
|
|
|
#5. Get Artifacts successfully;
|
2020-12-13 11:00:16 +01:00
|
|
|
artifacts = TestManifest.artifact.list_artifacts(TestManifest.project_name, TestManifest.index_name, **TestManifest.USER_CLIENT)
|
|
|
|
TestManifest.artifacts_ref_child_list = [artifacts[0].references[1].child_digest, artifacts[0].references[0].child_digest]
|
|
|
|
self.assertEqual(TestManifest.artifacts_ref_child_list.count(TestManifest.manifests_sha256_cli_ret[0]), 1)
|
|
|
|
self.assertEqual(TestManifest.artifacts_ref_child_list.count(TestManifest.manifests_sha256_cli_ret[1]), 1)
|
2020-03-16 03:13:28 +01:00
|
|
|
|
|
|
|
#6. Get index(IA) by reference successfully;
|
2020-12-13 11:00:16 +01:00
|
|
|
index_data = TestManifest.artifact.get_reference_info(TestManifest.project_name, TestManifest.index_name, TestManifest.index_tag, **TestManifest.USER_CLIENT)
|
|
|
|
print("index_data:",index_data)
|
2020-11-03 08:32:13 +01:00
|
|
|
manifests_sha256_harbor_ret = [index_data.references[1].child_digest, index_data.references[0].child_digest]
|
2020-03-01 10:57:40 +01:00
|
|
|
|
2020-03-16 03:13:28 +01:00
|
|
|
#7. Verify harbor index is index(IA) pushed by docker manifest CLI;
|
2020-12-13 11:00:16 +01:00
|
|
|
self.assertEqual(index_data.digest, TestManifest.index_sha256_cli_ret)
|
|
|
|
self.assertEqual(manifests_sha256_harbor_ret.count(TestManifest.manifests_sha256_cli_ret[0]), 1)
|
|
|
|
self.assertEqual(manifests_sha256_harbor_ret.count(TestManifest.manifests_sha256_cli_ret[1]), 1)
|
2020-03-01 10:57:40 +01:00
|
|
|
|
2020-04-22 10:44:10 +02:00
|
|
|
#8.1 Verify harbor index(IA) can be pulled by docker CLI successfully;
|
2020-12-13 11:00:16 +01:00
|
|
|
pull_harbor_image(harbor_server, TestManifest.user_name, TestManifest.user_push_index_password, TestManifest.project_name+"/"+TestManifest.index_name, TestManifest.index_tag)
|
2020-03-01 10:57:40 +01:00
|
|
|
|
2020-04-22 10:44:10 +02:00
|
|
|
#8.2 Verify harbor index(IA) can be pulled by ctr successfully;
|
2020-12-13 11:00:16 +01:00
|
|
|
oci_ref = harbor_server+"/"+TestManifest.project_name+"/"+TestManifest.index_name+":"+TestManifest.index_tag
|
|
|
|
library.containerd.ctr_images_pull(TestManifest.user_name, TestManifest.user_push_index_password, oci_ref)
|
2020-04-22 10:44:10 +02:00
|
|
|
library.containerd.ctr_images_list(oci_ref = oci_ref)
|
|
|
|
|
2020-03-16 03:13:28 +01:00
|
|
|
#9. Get addition successfully;
|
2020-12-13 11:00:16 +01:00
|
|
|
addition_v = TestManifest.artifact.get_addition(TestManifest.project_name, TestManifest.index_name, TestManifest.index_tag, "vulnerabilities", **TestManifest.USER_CLIENT)
|
2020-03-16 03:13:28 +01:00
|
|
|
self.assertEqual(addition_v[0], '{}')
|
|
|
|
#This artifact has no build history
|
|
|
|
|
2020-12-13 11:00:16 +01:00
|
|
|
addition_v = TestManifest.artifact.get_addition(TestManifest.project_name, TestManifest.index_name, TestManifest.manifests_sha256_cli_ret[0], "vulnerabilities", **TestManifest.USER_CLIENT)
|
2020-03-16 03:13:28 +01:00
|
|
|
self.assertEqual(addition_v[0], '{}')
|
2020-12-13 11:00:16 +01:00
|
|
|
addition_b = TestManifest.artifact.get_addition(TestManifest.project_name, TestManifest.index_name, TestManifest.manifests_sha256_cli_ret[0], "build_history", **TestManifest.USER_CLIENT)
|
2020-03-26 03:03:30 +01:00
|
|
|
self.assertIn("ADD file:", addition_b[0])
|
2020-12-13 11:00:16 +01:00
|
|
|
image_data = TestManifest.artifact.get_reference_info(TestManifest.project_name, TestManifest.index_name, TestManifest.manifests_sha256_cli_ret[0], **TestManifest.USER_CLIENT)
|
2020-03-16 03:13:28 +01:00
|
|
|
|
2020-12-13 11:00:16 +01:00
|
|
|
addition_v = TestManifest.artifact.get_addition(TestManifest.project_name, TestManifest.index_name, TestManifest.manifests_sha256_cli_ret[1], "vulnerabilities", **TestManifest.USER_CLIENT)
|
2020-03-16 03:13:28 +01:00
|
|
|
self.assertEqual(addition_v[0], '{}')
|
2020-12-13 11:00:16 +01:00
|
|
|
addition_b = TestManifest.artifact.get_addition(TestManifest.project_name, TestManifest.index_name, TestManifest.manifests_sha256_cli_ret[1], "build_history", **TestManifest.USER_CLIENT)
|
2020-03-26 03:03:30 +01:00
|
|
|
self.assertIn("ADD file:", addition_b[0])
|
2020-12-13 11:00:16 +01:00
|
|
|
image_data = TestManifest.artifact.get_reference_info(TestManifest.project_name, TestManifest.index_name, TestManifest.manifests_sha256_cli_ret[0], **TestManifest.USER_CLIENT)
|
2020-03-16 03:13:28 +01:00
|
|
|
|
2020-12-13 11:00:16 +01:00
|
|
|
@unittest.skipIf(TEARDOWN == True, "Test data won't be erased.")
|
|
|
|
def test_99_ManifestDeletion(self):
|
2020-03-16 03:13:28 +01:00
|
|
|
#10. Unable to Delete artifact in manifest list;
|
2020-12-13 11:00:16 +01:00
|
|
|
TestManifest.artifact.delete_artifact(TestManifest.project_name, TestManifest.index_name, TestManifest.manifests_sha256_cli_ret[0], expect_status_code = 412, **TestManifest.USER_CLIENT)
|
2020-03-16 03:13:28 +01:00
|
|
|
|
|
|
|
#11. Delete index successfully.
|
2020-12-13 11:00:16 +01:00
|
|
|
TestManifest.artifact.delete_artifact(TestManifest.project_name, TestManifest.index_name, TestManifest.index_tag, **TestManifest.USER_CLIENT)
|
|
|
|
|
|
|
|
TestManifest.do_tearDown()
|
|
|
|
|
|
|
|
def test_02_ScanManifestList(self):
|
|
|
|
"""
|
|
|
|
Test case:
|
|
|
|
Scan Manifest List
|
|
|
|
Test step and expected result:
|
|
|
|
1. Scan 1st child artifact, it should be scanned, 2nd child should be Not Scanned, index should be Not Scanned;
|
|
|
|
2. Scan 2nd child artifact, it should be scanned, index should be scanned;
|
|
|
|
"""
|
|
|
|
#1. Scan 1st child artifact, it should be scanned, 2nd child should be Not Scanned, index should be Not Scanned;
|
|
|
|
TestManifest.scan.scan_artifact(TestManifest.project_name, TestManifest.index_name, TestManifest.artifacts_ref_child_list[0], **TestManifest.USER_CLIENT)
|
|
|
|
TestManifest.artifact.check_image_scan_result(TestManifest.project_name, TestManifest.index_name, TestManifest.artifacts_ref_child_list[0], **TestManifest.USER_CLIENT)
|
|
|
|
|
|
|
|
TestManifest.artifact.check_image_scan_result(TestManifest.project_name, TestManifest.index_name, TestManifest.artifacts_ref_child_list[1], expected_scan_status = "Not Scanned", **TestManifest.USER_CLIENT)
|
|
|
|
TestManifest.artifact.check_image_scan_result(TestManifest.project_name, TestManifest.index_name, TestManifest.index_sha256_cli_ret, expected_scan_status = "Not Scanned", **TestManifest.USER_CLIENT)
|
|
|
|
|
|
|
|
#2. Scan 2nd child artifact, it should be scanned, index should be scanned;
|
|
|
|
TestManifest.scan.scan_artifact(TestManifest.project_name, TestManifest.index_name, TestManifest.artifacts_ref_child_list[1], **TestManifest.USER_CLIENT)
|
|
|
|
TestManifest.artifact.check_image_scan_result(TestManifest.project_name, TestManifest.index_name, TestManifest.artifacts_ref_child_list[1], **TestManifest.USER_CLIENT)
|
|
|
|
TestManifest.artifact.check_image_scan_result(TestManifest.project_name, TestManifest.index_name, TestManifest.index_sha256_cli_ret, **TestManifest.USER_CLIENT)
|
|
|
|
|
2020-03-16 03:13:28 +01:00
|
|
|
|
2020-03-01 10:57:40 +01:00
|
|
|
if __name__ == '__main__':
|
2020-12-13 11:00:16 +01:00
|
|
|
suite = unittest.TestSuite(unittest.makeSuite(TestManifest))
|
|
|
|
result = unittest.TextTestRunner(sys.stdout, verbosity=2, failfast=True).run(suite)
|
|
|
|
if not result.wasSuccessful():
|
|
|
|
raise Exception(r"Manifest test failed: {}".format(result))
|
2020-03-01 10:57:40 +01:00
|
|
|
|