2018-12-03 10:05:06 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from testutils import harbor_server
|
|
|
|
from testutils import TEARDOWN
|
|
|
|
from testutils import ADMIN_CLIENT
|
2019-03-22 10:52:21 +01:00
|
|
|
from library.system import System
|
2018-12-03 10:05:06 +01:00
|
|
|
from library.project import Project
|
|
|
|
from library.user import User
|
|
|
|
from library.repository import Repository
|
|
|
|
from library.repository import push_image_to_project
|
2020-03-10 07:55:55 +01:00
|
|
|
from library.artifact import Artifact
|
2020-03-16 03:13:28 +01:00
|
|
|
from library.scanner import Scanner
|
2018-12-03 10:05:06 +01:00
|
|
|
|
|
|
|
class TestProjects(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUp(self):
|
2020-02-25 03:40:29 +01:00
|
|
|
self.system = System()
|
|
|
|
self.project= Project()
|
|
|
|
self.user= User()
|
2020-03-16 03:13:28 +01:00
|
|
|
self.artifact = Artifact()
|
|
|
|
self.repo = Repository()
|
|
|
|
self.scanner = Scanner()
|
2018-12-03 10:05:06 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDown(self):
|
2020-07-30 10:04:14 +02:00
|
|
|
print("Case completed")
|
2018-12-03 10:05:06 +01:00
|
|
|
|
2018-12-04 05:26:12 +01:00
|
|
|
@unittest.skipIf(TEARDOWN == False, "Test data won't be erased.")
|
2018-12-03 10:05:06 +01:00
|
|
|
def test_ClearData(self):
|
|
|
|
#1. Delete Alice's repository and Luca's repository;
|
2020-03-10 07:55:55 +01:00
|
|
|
self.repo.delete_repoitory(TestProjects.project_Alice_name, TestProjects.repo_Alice_name.split('/')[1], **ADMIN_CLIENT)
|
|
|
|
self.repo.delete_repoitory(TestProjects.project_Luca_name, TestProjects.repo_Luca_name.split('/')[1], **ADMIN_CLIENT)
|
2018-12-03 10:05:06 +01:00
|
|
|
|
|
|
|
#2. Delete Alice's project and Luca's project;
|
|
|
|
self.project.delete_project(TestProjects.project_Alice_id, **ADMIN_CLIENT)
|
|
|
|
self.project.delete_project(TestProjects.project_Luca_id, **ADMIN_CLIENT)
|
|
|
|
|
|
|
|
#3. Delete user Alice and Luca.
|
|
|
|
self.user.delete_user(TestProjects.user_Alice_id, **ADMIN_CLIENT)
|
|
|
|
self.user.delete_user(TestProjects.user_Luca_id, **ADMIN_CLIENT)
|
|
|
|
|
2020-03-10 07:55:55 +01:00
|
|
|
def testSystemLevelScanALL(self):
|
2018-12-03 10:05:06 +01:00
|
|
|
"""
|
|
|
|
Test case:
|
2020-03-10 07:55:55 +01:00
|
|
|
System level Scan All
|
2018-12-04 05:26:12 +01:00
|
|
|
Test step and expected result:
|
2018-12-03 10:05:06 +01:00
|
|
|
1. Create user Alice and Luca;
|
|
|
|
2. Create 2 new private projects project_Alice and project_Luca;
|
|
|
|
3. Push a image to project_Alice and push another image to project_Luca;
|
|
|
|
4. Trigger scan all event;
|
|
|
|
5. Check if image in project_Alice and another image in project_Luca were both scanned.
|
|
|
|
Tear down:
|
|
|
|
1. Delete Alice's repository and Luca's repository;
|
|
|
|
2. Delete Alice's project and Luca's project;
|
|
|
|
3. Delete user Alice and Luca.
|
|
|
|
"""
|
|
|
|
url = ADMIN_CLIENT["endpoint"]
|
|
|
|
user_common_password = "Aa123456"
|
|
|
|
|
|
|
|
#1. Create user Alice and Luca;
|
|
|
|
TestProjects.user_Alice_id, user_Alice_name = self.user.create_user(user_password = user_common_password, **ADMIN_CLIENT)
|
|
|
|
TestProjects.user_Luca_id, user_Luca_name = self.user.create_user(user_password = user_common_password, **ADMIN_CLIENT)
|
|
|
|
|
2020-03-10 07:55:55 +01:00
|
|
|
USER_ALICE_CLIENT=dict(endpoint = url, username = user_Alice_name, password = user_common_password, with_scan_overview = True)
|
|
|
|
USER_LUCA_CLIENT=dict(endpoint = url, username = user_Luca_name, password = user_common_password, with_scan_overview = True)
|
2018-12-03 10:05:06 +01:00
|
|
|
|
|
|
|
#2. Create 2 new private projects project_Alice and project_Luca;
|
2020-03-10 07:55:55 +01:00
|
|
|
TestProjects.project_Alice_id, TestProjects.project_Alice_name = self.project.create_project(metadata = {"public": "false"}, **USER_ALICE_CLIENT)
|
|
|
|
TestProjects.project_Luca_id, TestProjects.project_Luca_name = self.project.create_project(metadata = {"public": "false"}, **USER_LUCA_CLIENT)
|
2018-12-03 10:05:06 +01:00
|
|
|
|
|
|
|
#3. Push a image to project_Alice and push another image to project_Luca;
|
|
|
|
|
|
|
|
#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 repository creation.
|
|
|
|
#image = "tomcat"
|
2020-03-10 07:55:55 +01:00
|
|
|
image_a = "mariadb"
|
2018-12-03 10:05:06 +01:00
|
|
|
src_tag = "latest"
|
|
|
|
#3.1 Push a image to project_Alice;
|
2020-03-10 07:55:55 +01:00
|
|
|
TestProjects.repo_Alice_name, tag_Alice = push_image_to_project(TestProjects.project_Alice_name, harbor_server, user_Alice_name, user_common_password, image_a, src_tag)
|
2018-12-03 10:05:06 +01:00
|
|
|
|
|
|
|
#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 repository creation.
|
2020-03-10 07:55:55 +01:00
|
|
|
image_b = "httpd"
|
2018-12-03 10:05:06 +01:00
|
|
|
src_tag = "latest"
|
|
|
|
#3.2 push another image to project_Luca;
|
2020-03-10 07:55:55 +01:00
|
|
|
TestProjects.repo_Luca_name, tag_Luca = push_image_to_project(TestProjects.project_Luca_name, harbor_server, user_Luca_name, user_common_password, image_b, src_tag)
|
2018-12-03 10:05:06 +01:00
|
|
|
|
|
|
|
#4. Trigger scan all event;
|
2019-03-22 10:52:21 +01:00
|
|
|
self.system.scan_now(**ADMIN_CLIENT)
|
2018-12-03 10:05:06 +01:00
|
|
|
|
|
|
|
#5. Check if image in project_Alice and another image in project_Luca were both scanned.
|
2020-03-10 07:55:55 +01:00
|
|
|
self.artifact.check_image_scan_result(TestProjects.project_Alice_name, image_a, tag_Alice, **USER_ALICE_CLIENT)
|
|
|
|
self.artifact.check_image_scan_result(TestProjects.project_Luca_name, image_b, tag_Luca, **USER_LUCA_CLIENT)
|
2018-12-03 10:05:06 +01:00
|
|
|
|
2020-03-16 03:13:28 +01:00
|
|
|
#6. Swith Scanner;
|
|
|
|
uuid = self.scanner.scanners_get_uuid(**ADMIN_CLIENT)
|
|
|
|
self.scanner.scanners_registration_id_patch(uuid, **ADMIN_CLIENT)
|
|
|
|
|
|
|
|
#7. Trigger scan all event;
|
|
|
|
self.system.scan_now(**ADMIN_CLIENT)
|
|
|
|
|
|
|
|
#8. Check if image in project_Alice and another image in project_Luca were both scanned.
|
|
|
|
self.artifact.check_image_scan_result(TestProjects.project_Alice_name, image_a, tag_Alice, **USER_ALICE_CLIENT)
|
|
|
|
self.artifact.check_image_scan_result(TestProjects.project_Luca_name, image_b, tag_Luca, **USER_LUCA_CLIENT)
|
|
|
|
|
|
|
|
|
2018-12-03 10:05:06 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|