2018-11-15 08:18:35 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from testutils import harbor_server
|
|
|
|
from testutils import TEARDOWN
|
2018-11-20 07:24:13 +01:00
|
|
|
from testutils import ADMIN_CLIENT
|
2018-11-15 08:18:35 +01:00
|
|
|
from library.project import Project
|
|
|
|
from library.user import User
|
|
|
|
from library.repository import Repository
|
2018-11-20 07:24:13 +01:00
|
|
|
from library.repository import push_image_to_project
|
2020-03-10 07:55:55 +01:00
|
|
|
from library.artifact import Artifact
|
|
|
|
from library.scan import Scan
|
2020-03-16 03:13:28 +01:00
|
|
|
from library.scanner import Scanner
|
2018-11-15 08:18:35 +01:00
|
|
|
class TestProjects(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUp(self):
|
2020-02-25 03:40:29 +01:00
|
|
|
self.project= Project()
|
|
|
|
self.user= User()
|
2020-03-16 03:13:28 +01:00
|
|
|
self.artifact = Artifact()
|
|
|
|
self.repo = Repository()
|
|
|
|
self.scan = Scan()
|
|
|
|
self.scanner = Scanner()
|
2018-11-15 08:18:35 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDown(self):
|
2020-07-30 10:04:14 +02:00
|
|
|
print("Case completed")
|
2018-11-15 08:18:35 +01:00
|
|
|
|
2020-03-16 03:13:28 +01:00
|
|
|
@unittest.skipIf(TEARDOWN == True, "Test data won't be erased.")
|
2018-11-15 08:18:35 +01:00
|
|
|
def test_ClearData(self):
|
|
|
|
#1. Delete repository(RA) by user(UA);
|
2020-03-10 07:55:55 +01:00
|
|
|
self.repo.delete_repoitory(TestProjects.project_scan_image_name, TestProjects.repo_name.split('/')[1], **TestProjects.USER_SCAN_IMAGE_CLIENT)
|
2018-11-15 08:18:35 +01:00
|
|
|
|
|
|
|
#2. Delete project(PA);
|
2018-12-03 10:05:06 +01:00
|
|
|
self.project.delete_project(TestProjects.project_scan_image_id, **TestProjects.USER_SCAN_IMAGE_CLIENT)
|
2018-11-15 08:18:35 +01:00
|
|
|
|
|
|
|
#3. Delete user(UA);
|
2018-11-20 07:24:13 +01:00
|
|
|
self.user.delete_user(TestProjects.user_scan_image_id, **ADMIN_CLIENT)
|
2018-11-15 08:18:35 +01:00
|
|
|
|
2020-03-10 07:55:55 +01:00
|
|
|
def testScanImageArtifact(self):
|
2018-11-15 08:18:35 +01:00
|
|
|
"""
|
|
|
|
Test case:
|
2020-03-10 07:55:55 +01:00
|
|
|
Scan An Image Artifact
|
2018-12-04 05:26:12 +01:00
|
|
|
Test step and expected result:
|
2018-11-15 08:18:35 +01:00
|
|
|
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);
|
2019-01-31 14:49:06 +01:00
|
|
|
6. Send scan image command and get tag(TA) information to check scan result, it should be finished;
|
2020-03-16 03:13:28 +01:00
|
|
|
7. Swith Scanner;
|
|
|
|
8. Send scan another image command and get tag(TA) information to check scan result, it should be finished.
|
2018-11-15 08:18:35 +01:00
|
|
|
Tear down:
|
|
|
|
1. Delete repository(RA) by user(UA);
|
|
|
|
2. Delete project(PA);
|
|
|
|
3. Delete user(UA);
|
|
|
|
"""
|
2018-11-20 07:24:13 +01:00
|
|
|
url = ADMIN_CLIENT["endpoint"]
|
2018-11-15 08:18:35 +01:00
|
|
|
user_001_password = "Aa123456"
|
|
|
|
|
|
|
|
#1. Create user-001
|
2018-11-29 11:27:53 +01:00
|
|
|
TestProjects.user_scan_image_id, user_scan_image_name = self.user.create_user(user_password = user_001_password, **ADMIN_CLIENT)
|
2018-11-15 08:18:35 +01:00
|
|
|
|
2020-03-10 07:55:55 +01:00
|
|
|
TestProjects.USER_SCAN_IMAGE_CLIENT=dict(endpoint = url, username = user_scan_image_name, password = user_001_password, with_scan_overview = True)
|
2018-11-15 08:18:35 +01:00
|
|
|
|
|
|
|
#2. Create a new private project(PA) by user(UA);
|
2020-03-10 07:55:55 +01:00
|
|
|
TestProjects.project_scan_image_id, TestProjects.project_scan_image_name = self.project.create_project(metadata = {"public": "false"}, **ADMIN_CLIENT)
|
2018-11-15 08:18:35 +01:00
|
|
|
|
|
|
|
#3. Add user(UA) as a member of project(PA) with project-admin role;
|
2018-11-20 07:24:13 +01:00
|
|
|
self.project.add_project_members(TestProjects.project_scan_image_id, TestProjects.user_scan_image_id, **ADMIN_CLIENT)
|
2018-11-15 08:18:35 +01:00
|
|
|
|
|
|
|
#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,
|
2018-12-03 10:05:06 +01:00
|
|
|
expected_project_id = TestProjects.project_scan_image_id, **TestProjects.USER_SCAN_IMAGE_CLIENT)
|
2018-11-15 08:18:35 +01:00
|
|
|
|
|
|
|
#Note: Please make sure that this Image has never been pulled before by any other cases,
|
2020-03-16 03:13:28 +01:00
|
|
|
# so it is a not-scanned image right after repository creation.
|
2018-12-18 03:21:03 +01:00
|
|
|
image = "docker"
|
|
|
|
src_tag = "1.13"
|
2018-11-15 08:18:35 +01:00
|
|
|
#5. Create a new repository(RA) and tag(TA) in project(PA) by user(UA);
|
2020-03-10 07:55:55 +01:00
|
|
|
TestProjects.repo_name, tag = push_image_to_project(TestProjects.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) information to check scan result, it should be finished;
|
|
|
|
self.scan.scan_artifact(TestProjects.project_scan_image_name, TestProjects.repo_name.split('/')[1], tag, **TestProjects.USER_SCAN_IMAGE_CLIENT)
|
2020-03-16 03:13:28 +01:00
|
|
|
self.artifact.check_image_scan_result(TestProjects.project_scan_image_name, image, tag, **TestProjects.USER_SCAN_IMAGE_CLIENT)
|
2018-11-15 08:18:35 +01:00
|
|
|
|
2020-03-16 03:13:28 +01:00
|
|
|
#7. Swith Scanner;
|
|
|
|
uuid = self.scanner.scanners_get_uuid(**ADMIN_CLIENT)
|
|
|
|
self.scanner.scanners_registration_id_patch(uuid, **ADMIN_CLIENT)
|
|
|
|
|
|
|
|
image = "tomcat"
|
|
|
|
src_tag = "latest"
|
|
|
|
TestProjects.repo_name, tag = push_image_to_project(TestProjects.project_scan_image_name, harbor_server, user_scan_image_name, user_001_password, image, src_tag)
|
|
|
|
#8. Send scan another image command and get tag(TA) information to check scan result, it should be finished.
|
|
|
|
self.scan.scan_artifact(TestProjects.project_scan_image_name, TestProjects.repo_name.split('/')[1], tag, **TestProjects.USER_SCAN_IMAGE_CLIENT)
|
2020-03-10 07:55:55 +01:00
|
|
|
self.artifact.check_image_scan_result(TestProjects.project_scan_image_name, image, tag, **TestProjects.USER_SCAN_IMAGE_CLIENT)
|
2018-11-15 08:18:35 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|