From dcd34a37a933da56e1e580787aeb62ddfa92c94f Mon Sep 17 00:00:00 2001 From: danfengliu Date: Thu, 15 Nov 2018 15:18:35 +0800 Subject: [PATCH] add test case scan a not scanned image Modify for codacy issues. Signed-off-by: danfengliu --- tests/apitests/python/library/repository.py | 44 ++++++++++- tests/apitests/python/test_scan_image.py | 88 +++++++++++++++++++++ tests/robot-cases/Group0-BAT/API_DB.robot | 2 + 3 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 tests/apitests/python/test_scan_image.py diff --git a/tests/apitests/python/library/repository.py b/tests/apitests/python/library/repository.py index 9862bf59a..b96bb3b23 100644 --- a/tests/apitests/python/library/repository.py +++ b/tests/apitests/python/library/repository.py @@ -26,6 +26,10 @@ class Repository(base.Base): client = self._get_client(**kwargs) return client.repositories_repo_name_tags_get(repository) + def get_tag(self, repo_name, tag, **kwargs): + client = self._get_client(**kwargs) + return client.repositories_repo_name_tags_tag_get(repo_name, tag) + def image_exists(self, repository, tag, **kwargs): tags = self.list_tags(repository, **kwargs) exist = False @@ -65,4 +69,42 @@ class Repository(base.Base): data, status_code, _ = client.repositories_repo_name_signatures_get_with_http_info(repo_name) base._assert_status_code(expect_status_code, status_code) return data - \ No newline at end of file + + def get_not_scanned_image_init_state_success(self, repo_name, tag, **kwargs): + tag = self.get_tag(repo_name, tag, **kwargs) + if tag.scan_overview != None: + raise Exception("Image should be state!") + + def check_image_scan_result(self, repo_name, tag, expected_scan_status = "finished", **kwargs): + scan_finish = False + timeout_count = 20 + actual_scan_status = "NULL" + while not (scan_finish): + time.sleep(5) + _tag = self.get_tag(repo_name, tag, **kwargs) + scan_result = False + print "t.name:{} tag:{}, t.scan_overview.scan_status:{}".format(_tag.name, tag, _tag.scan_overview.scan_status) + if _tag.name == tag and _tag.scan_overview !=None: + if _tag.scan_overview.scan_status == expected_scan_status: + scan_finish = True + scan_result = True + break + else: + actual_scan_status = _tag.scan_overview.scan_status + timeout_count = timeout_count - 1 + if (timeout_count == 0): + scan_finish = True + if not (scan_result): + raise Exception("Scan image result is not as expected {} actual scan status is {}".format(expected_scan_status, actual_scan_status)) + + def scan_image(self, repo_name, tag, expect_status_code = 200, **kwargs): + client = self._get_client(**kwargs) + data, status_code, _ = client.repositories_repo_name_tags_tag_scan_post_with_http_info(repo_name, tag) + base._assert_status_code(expect_status_code, status_code) + return data + + def scan_not_scanned_image_success(self, repo_name, tag, **kwargs): + self.get_not_scanned_image_init_state_success(repo_name, tag, **kwargs) + self.scan_image(repo_name, tag, **kwargs) + self.check_image_scan_result(repo_name, tag, **kwargs) + diff --git a/tests/apitests/python/test_scan_image.py b/tests/apitests/python/test_scan_image.py new file mode 100644 index 000000000..eefb8514f --- /dev/null +++ b/tests/apitests/python/test_scan_image.py @@ -0,0 +1,88 @@ +from __future__ import absolute_import +import unittest + +from testutils import CLIENT +from testutils import harbor_server +from testutils import TEARDOWN +from library.project import Project +from library.user import User +from library.repository import Repository +from library.repository import create_repository + +class TestProjects(unittest.TestCase): + @classmethod + def setUp(self): + project = Project() + self.project= project + + user = User() + self.user= user + + repo = Repository() + self.repo= repo + + @classmethod + def tearDown(self): + print "Case completed" + + @unittest.skipIf(TEARDOWN == True, "Test data should be remain in the harbor.") + def test_ClearData(self): + #1. Delete repository(RA) by user(UA); + self.repo.delete_repoitory(TestProjects.repo_name, **TestProjects.USER_scan_image_CLIENT) + + #2. Delete project(PA); + self.project.delete_project(TestProjects.project_scan_image_id, **TestProjects.USER_scan_image_CLIENT) + + #3. Delete user(UA); + self.user.delete_user(TestProjects.user_scan_image_id, **TestProjects.ADMIN_CLIENT) + + def testScanImage(self): + """ + Test case: + Scan A Image + Test step & Expectation: + 1. Create a new user(UA); + 2. Create a new private project(PA) by user(UA); + 3. Add user(UA) as a member of project(PA) with project-admin role; + 4. Get private project of user(UA), user(UA) can see only one private project which is project(PA); + 5. Create a new repository(RA) and tag(TA) in project(PA) by user(UA); + 6. Send scan image command and get tag(TA) infomation to check scan result, it should be finished; + Tear down: + 1. Delete repository(RA) by user(UA); + 2. Delete project(PA); + 3. Delete user(UA); + """ + admin_user = "admin" + admin_pwd = "Harbor12345" + url = CLIENT["endpoint"] + user_001_password = "Aa123456" + TestProjects.ADMIN_CLIENT=dict(endpoint = url, username = admin_user, password = admin_pwd) + + #1. Create user-001 + TestProjects.user_scan_image_id, user_scan_image_name = self.user.create_user_success(user_password = user_001_password, **TestProjects.ADMIN_CLIENT) + + TestProjects.USER_scan_image_CLIENT=dict(endpoint = url, username = user_scan_image_name, password = user_001_password) + + #2. Create a new private project(PA) by user(UA); + TestProjects.project_scan_image_id, project_scan_image_name = self.project.create_project(metadata = {"public": "false"}, **TestProjects.ADMIN_CLIENT) + + #3. Add user(UA) as a member of project(PA) with project-admin role; + self.project.add_project_members(TestProjects.project_scan_image_id, TestProjects.user_scan_image_id, **TestProjects.ADMIN_CLIENT) + + #4. Get private project of user(UA), user(UA) can see only one private project which is project(PA); + self.project.projects_should_exist(dict(public=False), expected_count = 1, + expected_project_id = TestProjects.project_scan_image_id, **TestProjects.USER_scan_image_CLIENT) + + #Note: Please make sure that this Image has never been pulled before by any other cases, + # so it is a not-scanned image rigth after rpository creation. + #image = "tomcat" + image = "pypy" + src_tag = "latest" + #5. Create a new repository(RA) and tag(TA) in project(PA) by user(UA); + TestProjects.repo_name, tag = create_repository(project_scan_image_name, harbor_server, user_scan_image_name, user_001_password, image, src_tag) + + #6. Send scan image command and get tag(TA) infomation to check scan result, it should be finished; + self.repo.scan_not_scanned_image_success(TestProjects.repo_name, tag, **TestProjects.USER_scan_image_CLIENT) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/tests/robot-cases/Group0-BAT/API_DB.robot b/tests/robot-cases/Group0-BAT/API_DB.robot index 79507f9b6..c18d1795c 100644 --- a/tests/robot-cases/Group0-BAT/API_DB.robot +++ b/tests/robot-cases/Group0-BAT/API_DB.robot @@ -27,3 +27,5 @@ Test Case - Add Replication Rule Harbor API Test ./tests/apitests/python/test_add_replication_rule.py Test Case - Edit Project Creation Harbor API Test ./tests/apitests/python/test_edit_project_creation.py +Test Case - Scan Image + Harbor API Test ./tests/apitests/python/test_scan_image.py \ No newline at end of file