diff --git a/tests/apitests/python/library/preheat.py b/tests/apitests/python/library/preheat.py new file mode 100644 index 000000000..31754c22a --- /dev/null +++ b/tests/apitests/python/library/preheat.py @@ -0,0 +1,77 @@ +# -*- coding: utf-8 -*- + +import time +import base +import v2_swagger_client +from v2_swagger_client.rest import ApiException + +class Preheat(base.Base, object): + def __init__(self): + super(Preheat,self).__init__(api_type = "preheat") + + def create_instance(self, name = None, description="It's a dragonfly instance", vendor="dragonfly", + endpoint_url="http://20.32.244.16", auth_mode="NONE", enabled=True, insecure=True, + expect_status_code = 201, expect_response_body = None, **kwargs): + if name is None: + name = base._random_name("instance") + client = self._get_client(**kwargs) + instance = v2_swagger_client.Instance(name=name, description=description,vendor=vendor, + endpoint=endpoint_url, auth_mode=auth_mode, enabled=enabled) + print("instance:",instance) + try: + _, status_code, header = client.create_instance_with_http_info(instance) + except ApiException as e: + base._assert_status_code(expect_status_code, e.status) + if expect_response_body is not None: + base._assert_status_body(expect_response_body, e.body) + return + base._assert_status_code(expect_status_code, status_code) + base._assert_status_code(201, status_code) + print("===================header:", header) + return base._get_id_from_header(header), name + + def create_policy(self, project_name, project_id, provider_id, name = None, description="It's a dragonfly policy", + filters=r'[{"type":"repository","value":"re*"},{"type":"tag","value":"v1.0*"}]', trigger=r'{"type":"manual","trigger_setting":{"cron":""}}', enabled=True, + expect_status_code = 201, expect_response_body = None, **kwargs): + if name is None: + name = base._random_name("policy") + client = self._get_client(**kwargs) + policy = v2_swagger_client.PreheatPolicy(name=name, project_id=project_id, provider_id=provider_id, + description=description,filters=filters, + trigger=trigger, enabled=enabled) + print("policy:",policy) + try: + data, status_code, header = client.create_policy_with_http_info(project_name, policy) + print("===================policy data:", data) + except ApiException as e: + base._assert_status_code(expect_status_code, e.status) + if expect_response_body is not None: + base._assert_status_body(expect_response_body, e.body) + return + base._assert_status_code(expect_status_code, status_code) + base._assert_status_code(201, status_code) + return base._get_id_from_header(header), name + + def get_instance(self, instance_name, **kwargs): + client = self._get_client(**kwargs) + return client.get_instance(instance_name) + + def get_policy(self, project_name, preheat_policy_name, **kwargs): + client = self._get_client(**kwargs) + return client.get_policy(project_name, preheat_policy_name) + + def update_policy(self, project_name, preheat_policy_name, policy, **kwargs): + client = self._get_client(**kwargs) + return client.update_policy(project_name, preheat_policy_name, policy) + + def delete_instance(self, preheat_instance_name, expect_status_code = 200, expect_response_body = None, **kwargs): + client = self._get_client(**kwargs) + try: + _, status_code, header = _, status_code, _ = client.delete_instance_with_http_info(preheat_instance_name) + except ApiException as e: + base._assert_status_code(expect_status_code, e.status) + if expect_response_body is not None: + base._assert_status_body(expect_response_body, e.body) + else: + base._assert_status_code(expect_status_code, status_code) + base._assert_status_code(200, status_code) diff --git a/tests/apitests/python/test_p2p.py b/tests/apitests/python/test_p2p.py new file mode 100644 index 000000000..ec5efdba9 --- /dev/null +++ b/tests/apitests/python/test_p2p.py @@ -0,0 +1,106 @@ +from __future__ import absolute_import + + +import unittest +import urllib +import sys + +from testutils import ADMIN_CLIENT +from testutils import harbor_server +from testutils import TEARDOWN +from library.base import _random_name +from library.base import _assert_status_code +from library.project import Project +from library.user import User +from library.repository import Repository +from library.repository import push_image_to_project +from library.registry import Registry +from library.repository import pull_harbor_image +from library.artifact import Artifact +from library.preheat import Preheat +import library.containerd +import v2_swagger_client + +class TestP2P(unittest.TestCase): + @classmethod + def setUpClass(self): + self.url = ADMIN_CLIENT["endpoint"] + self.user_password = "Aa123456" + self.project= Project() + self.user= User() + self.repo= Repository() + self.registry = Registry() + self.artifact = Artifact() + self.preheat = Preheat() + + @classmethod + def tearDownClass(self): + print("Case completed") + + def do_validate(self, registry_type): + """ + Test case: + Proxy Cache Image From Harbor + Test step and expected result: + 1. Create a new registry; + 2. Create a new project; + 3. Add a new user as a member of project; + 4. Pull image from this project by docker CLI; + 5. Pull image from this project by ctr CLI; + 6. Pull manifest index from this project by docker CLI; + 7. Pull manifest from this project by ctr CLI; + 8. Image pulled by docker CLI should be cached; + 9. Image pulled by ctr CLI should be cached; + 10. Manifest index pulled by docker CLI should be cached; + 11. Manifest index pulled by ctr CLI should be cached; + Tear down: + 1. Delete project(PA); + 2. Delete user(UA). + """ + user_id, user_name = self.user.create_user(user_password = self.user_password, **ADMIN_CLIENT) + USER_CLIENT=dict(with_signature = True, endpoint = self.url, username = user_name, password = self.user_password) + + #2. Create a new distribution instance; + instance_id, instance_name = self.preheat.create_instance( **ADMIN_CLIENT) + + #This need to be removed once issue #13378 fixed. + instance = self.preheat.get_instance(instance_name) + print("========instance:", instance.id) + print("instance_id:", instance_id) + + #2. Create a new project; + project_id, project_name = self.project.create_project(metadata = {"public": "false"}, **USER_CLIENT) + print("project_id:",project_id) + print("project_name:",project_name) + + #This need to be removed once issue #13378 fixed. + policy_id, policy_name = self.preheat.create_policy(project_name, project_id, instance.id, **USER_CLIENT) + #policy_id, _ = self.preheat.create_policy(project_name, project_id, instance_id, **USER_CLIENT) + policy = self.preheat.get_policy(project_name, policy_name) + print("policy:", policy) + + policy_new = v2_swagger_client.PreheatPolicy(id = policy.id, name="policy_new_name", project_id=project_id, provider_id=instance.id, + description="edit this policy",filters=r'[{"type":"repository","value":"zgila/alpine*"},{"type":"tag","value":"v1.0*"},{"type":"label","value":"release"}]', + trigger=r'{"type":"scheduled","trigger_setting":{"cron":"0 8 * * * *"}}', enabled=False) + + self.preheat.update_policy(project_name, policy.name, policy_new, **USER_CLIENT) + + self.preheat.delete_instance(instance.name, expect_status_code=403, **USER_CLIENT) + + self.project.delete_project(project_id, **USER_CLIENT) + + self.preheat.delete_instance(instance.name, **ADMIN_CLIENT) + + def test_create_instance(self): + self.do_validate("harbor") + + def suite(): + suite = unittest.TestSuite(unittest.makeSuite(TestP2P)) + return suite + +if __name__ == '__main__': + result = unittest.TextTestRunner(sys.stdout, verbosity=2, failfast=True).run(TestP2P.suite()) + print("Test result:",result) + if not result.wasSuccessful(): + raise Exception(r"P2P test failed!") + diff --git a/tests/apitests/python/test_proxy_cache.py b/tests/apitests/python/test_proxy_cache.py index 69249b646..1babee9f5 100644 --- a/tests/apitests/python/test_proxy_cache.py +++ b/tests/apitests/python/test_proxy_cache.py @@ -1,139 +1,139 @@ -from __future__ import absolute_import - - -import unittest -import urllib -import sys - -from testutils import ADMIN_CLIENT, suppress_urllib3_warning -from testutils import harbor_server -from testutils import TEARDOWN -from library.base import _random_name -from library.base import _assert_status_code -from library.project import Project -from library.user import User -from library.repository import Repository -from library.repository import push_image_to_project -from library.registry import Registry -from library.repository import pull_harbor_image -from library.artifact import Artifact -import library.containerd - -class TestProxyCache(unittest.TestCase): - @suppress_urllib3_warning - def setUp(self): - self.url = ADMIN_CLIENT["endpoint"] - self.user_password = "Aa123456" - self.project= Project() - self.user= User() - self.repo= Repository() - self.registry = Registry() - self.artifact = Artifact() - - @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.") - def tearDown(self): - print("Case completed") - - def do_validate(self, registry_type): - """ - Test case: - Proxy Cache Image From Harbor - Test step and expected result: - 1. Create a new registry; - 2. Create a new project; - 3. Add a new user as a member of project; - 4. Pull image from this project by docker CLI; - 5. Pull image from this project by ctr CLI; - 6. Pull manifest index from this project by docker CLI; - 7. Pull manifest from this project by ctr CLI; - 8. Image pulled by docker CLI should be cached; - 9. Image pulled by ctr CLI should be cached; - 10. Manifest index pulled by docker CLI should be cached; - 11. Manifest index pulled by ctr CLI should be cached; - Tear down: - 1. Delete project(PA); - 2. Delete user(UA). - """ - user_id, user_name = self.user.create_user(user_password = self.user_password, **ADMIN_CLIENT) - USER_CLIENT=dict(with_signature = True, endpoint = self.url, username = user_name, password = self.user_password) - - image_for_docker = dict(image = "for_proxy", tag = "1.0") - image_for_ctr = dict(image = "redis", tag = "latest") - index_for_docker = dict(image = "index081597864867", tag = "index_tag081597864867") - access_key = "" - access_secret = "" - - #1. Create a new registry; - if registry_type == "docker-hub": - user_namespace = "danfengliu" - access_key = user_namespace - access_secret = "Aa123456" - registry = "https://hub.docker.com" - # Memo: ctr will not send image pull request if manifest list already exist, so we pull different manifest list for different registry; - index_for_ctr = dict(image = "alpine", tag = "3.12.0") - else: - user_namespace = "nightly" - registry = "https://cicd.harbor.vmwarecna.net" - index_for_ctr = dict(image = "busybox", tag = "1.32.0") - - registry_id, _ = self.registry.create_registry(registry, name=_random_name(registry_type), registry_type=registry_type, access_key = access_key, access_secret = access_secret, insecure=False, **ADMIN_CLIENT) - - print("registry_id:", registry_id) - - #2. Create a new project; - project_id, project_name = self.project.create_project(registry_id = registry_id, metadata = {"public": "false"}, **ADMIN_CLIENT) - print("project_id:",project_id) - print("project_name:",project_name) - - #3. Add a new user as a member of project; - self.project.add_project_members(project_id, user_id=user_id, **ADMIN_CLIENT) - - #4. Pull image from this project by docker CLI; - pull_harbor_image(harbor_server, USER_CLIENT["username"], USER_CLIENT["password"], project_name + "/" + user_namespace + "/" + image_for_docker["image"], image_for_docker["tag"]) - - #5. Pull image from this project by ctr CLI; - oci_ref = harbor_server + "/" + project_name + "/" + user_namespace + "/" + image_for_ctr["image"] + ":" + image_for_ctr["tag"] - library.containerd.ctr_images_pull(user_name, self.user_password, oci_ref) - library.containerd.ctr_images_list(oci_ref = oci_ref) - - #6. Pull manifest index from this project by docker CLI; - index_repo_name = user_namespace + "/" + index_for_docker["image"] - pull_harbor_image(harbor_server, user_name, self.user_password, project_name + "/" + index_repo_name, index_for_docker["tag"]) - - #7. Pull manifest from this project by ctr CLI; - index_repo_name_for_ctr = user_namespace + "/" + index_for_ctr["image"] - oci_ref = harbor_server + "/" + project_name + "/" + index_repo_name_for_ctr + ":" + index_for_ctr["tag"] - library.containerd.ctr_images_pull(user_name, self.user_password, oci_ref) - library.containerd.ctr_images_list(oci_ref = oci_ref) - - #8. Image pulled by docker CLI should be cached; - self.artifact.waiting_for_reference_exist(project_name, urllib.parse.quote(user_namespace + "/" + image_for_docker["image"],'utf-8'), image_for_docker["tag"], **USER_CLIENT) - - #9. Image pulled by ctr CLI should be cached; - self.artifact.waiting_for_reference_exist(project_name, urllib.parse.quote(user_namespace + "/" + image_for_ctr["image"],'utf-8'), image_for_ctr["tag"], **USER_CLIENT) - - #10. Manifest index pulled by docker CLI should be cached; - ret_index_by_d = self.artifact.waiting_for_reference_exist(project_name, urllib.parse.quote(index_repo_name,'utf-8'), index_for_docker["tag"], **USER_CLIENT) - print("Index's reference by docker CLI:", ret_index_by_d.references) - self.assertTrue(len(ret_index_by_d.references) == 1) - - #11. Manifest index pulled by ctr CLI should be cached; - ret_index_by_c = self.artifact.waiting_for_reference_exist(project_name, urllib.parse.quote(index_repo_name_for_ctr,'utf-8'), index_for_ctr["tag"], **USER_CLIENT) - print("Index's reference by ctr CLI:", ret_index_by_c.references) - self.assertTrue(len(ret_index_by_c.references) == 1) - - def test_proxy_cache_from_harbor(self): - self.do_validate("harbor") - - #def test_proxy_cache_from_dockerhub(self): - # self.do_validate("docker-hub") - - def suite(): - suite = unittest.TestSuite(unittest.makeSuite(TestProxyCache)) - return suite - -if __name__ == '__main__': - result = unittest.TextTestRunner(sys.stdout, verbosity=2, failfast=True).run(TestProxyCache.suite()) - if not result.wasSuccessful(): - raise Exception(r"Proxy cache test failed: ".format(result)) - +from __future__ import absolute_import + + +import unittest +import urllib +import sys + +from testutils import ADMIN_CLIENT, suppress_urllib3_warning +from testutils import harbor_server +from testutils import TEARDOWN +from library.base import _random_name +from library.base import _assert_status_code +from library.project import Project +from library.user import User +from library.repository import Repository +from library.repository import push_image_to_project +from library.registry import Registry +from library.repository import pull_harbor_image +from library.artifact import Artifact +import library.containerd + +class TestProxyCache(unittest.TestCase): + @suppress_urllib3_warning + def setUp(self): + self.url = ADMIN_CLIENT["endpoint"] + self.user_password = "Aa123456" + self.project= Project() + self.user= User() + self.repo= Repository() + self.registry = Registry() + self.artifact = Artifact() + + @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.") + def tearDown(self): + print("Case completed") + + def do_validate(self, registry_type): + """ + Test case: + Proxy Cache Image From Harbor + Test step and expected result: + 1. Create a new registry; + 2. Create a new project; + 3. Add a new user as a member of project; + 4. Pull image from this project by docker CLI; + 5. Pull image from this project by ctr CLI; + 6. Pull manifest index from this project by docker CLI; + 7. Pull manifest from this project by ctr CLI; + 8. Image pulled by docker CLI should be cached; + 9. Image pulled by ctr CLI should be cached; + 10. Manifest index pulled by docker CLI should be cached; + 11. Manifest index pulled by ctr CLI should be cached; + Tear down: + 1. Delete project(PA); + 2. Delete user(UA). + """ + user_id, user_name = self.user.create_user(user_password = self.user_password, **ADMIN_CLIENT) + USER_CLIENT=dict(with_signature = True, endpoint = self.url, username = user_name, password = self.user_password) + + image_for_docker = dict(image = "for_proxy", tag = "1.0") + image_for_ctr = dict(image = "redis", tag = "latest") + index_for_docker = dict(image = "index081597864867", tag = "index_tag081597864867") + access_key = "" + access_secret = "" + + #1. Create a new registry; + if registry_type == "docker-hub": + user_namespace = "danfengliu" + access_key = user_namespace + access_secret = "Aa123456" + registry = "https://hub.docker.com" + # Memo: ctr will not send image pull request if manifest list already exist, so we pull different manifest list for different registry; + index_for_ctr = dict(image = "alpine", tag = "3.12.0") + else: + user_namespace = "nightly" + registry = "https://cicd.harbor.vmwarecna.net" + index_for_ctr = dict(image = "busybox", tag = "1.32.0") + + registry_id, _ = self.registry.create_registry(registry, name=_random_name(registry_type), registry_type=registry_type, access_key = access_key, access_secret = access_secret, insecure=False, **ADMIN_CLIENT) + + print("registry_id:", registry_id) + + #2. Create a new project; + project_id, project_name = self.project.create_project(registry_id = registry_id, metadata = {"public": "false"}, **ADMIN_CLIENT) + print("project_id:",project_id) + print("project_name:",project_name) + + #3. Add a new user as a member of project; + self.project.add_project_members(project_id, user_id=user_id, **ADMIN_CLIENT) + + #4. Pull image from this project by docker CLI; + pull_harbor_image(harbor_server, USER_CLIENT["username"], USER_CLIENT["password"], project_name + "/" + user_namespace + "/" + image_for_docker["image"], image_for_docker["tag"]) + + #5. Pull image from this project by ctr CLI; + oci_ref = harbor_server + "/" + project_name + "/" + user_namespace + "/" + image_for_ctr["image"] + ":" + image_for_ctr["tag"] + library.containerd.ctr_images_pull(user_name, self.user_password, oci_ref) + library.containerd.ctr_images_list(oci_ref = oci_ref) + + #6. Pull manifest index from this project by docker CLI; + index_repo_name = user_namespace + "/" + index_for_docker["image"] + pull_harbor_image(harbor_server, user_name, self.user_password, project_name + "/" + index_repo_name, index_for_docker["tag"]) + + #7. Pull manifest from this project by ctr CLI; + index_repo_name_for_ctr = user_namespace + "/" + index_for_ctr["image"] + oci_ref = harbor_server + "/" + project_name + "/" + index_repo_name_for_ctr + ":" + index_for_ctr["tag"] + library.containerd.ctr_images_pull(user_name, self.user_password, oci_ref) + library.containerd.ctr_images_list(oci_ref = oci_ref) + + #8. Image pulled by docker CLI should be cached; + self.artifact.waiting_for_reference_exist(project_name, urllib.parse.quote(user_namespace + "/" + image_for_docker["image"],'utf-8'), image_for_docker["tag"], **USER_CLIENT) + + #9. Image pulled by ctr CLI should be cached; + self.artifact.waiting_for_reference_exist(project_name, urllib.parse.quote(user_namespace + "/" + image_for_ctr["image"],'utf-8'), image_for_ctr["tag"], **USER_CLIENT) + + #10. Manifest index pulled by docker CLI should be cached; + ret_index_by_d = self.artifact.waiting_for_reference_exist(project_name, urllib.parse.quote(index_repo_name,'utf-8'), index_for_docker["tag"], **USER_CLIENT) + print("Index's reference by docker CLI:", ret_index_by_d.references) + self.assertTrue(len(ret_index_by_d.references) == 1) + + #11. Manifest index pulled by ctr CLI should be cached; + ret_index_by_c = self.artifact.waiting_for_reference_exist(project_name, urllib.parse.quote(index_repo_name_for_ctr,'utf-8'), index_for_ctr["tag"], **USER_CLIENT) + print("Index's reference by ctr CLI:", ret_index_by_c.references) + self.assertTrue(len(ret_index_by_c.references) == 1) + + def test_proxy_cache_from_harbor(self): + self.do_validate("harbor") + + #def test_proxy_cache_from_dockerhub(self): + # self.do_validate("docker-hub") + + def suite(): + suite = unittest.TestSuite(unittest.makeSuite(TestProxyCache)) + return suite + +if __name__ == '__main__': + result = unittest.TextTestRunner(sys.stdout, verbosity=2, failfast=True).run(TestProxyCache.suite()) + if not result.wasSuccessful(): + raise Exception(r"Proxy cache test failed: ".format(result)) + diff --git a/tests/robot-cases/Group0-BAT/API_DB.robot b/tests/robot-cases/Group0-BAT/API_DB.robot index df3301ad3..1c5a50c7c 100644 --- a/tests/robot-cases/Group0-BAT/API_DB.robot +++ b/tests/robot-cases/Group0-BAT/API_DB.robot @@ -152,3 +152,7 @@ Test Case - Proxy Cache Test Case - Tag Immutability [Tags] tag_immutability Harbor API Test ./tests/apitests/python/test_tag_immutability.py + +Test Case - P2P + [Tags] p2p + Harbor API Test ./tests/apitests/python/test_p2p.py