2019-08-09 04:50:06 +02:00
from __future__ import absolute_import
import unittest
2020-11-04 03:13:12 +01:00
from testutils import ADMIN_CLIENT , suppress_urllib3_warning
2019-08-09 04:50:06 +02:00
from testutils import harbor_server
from testutils import TEARDOWN
2020-08-07 08:56:18 +02:00
from library . sign import sign_image
2020-02-25 03:40:29 +01:00
from library . artifact import Artifact
2019-08-09 04:50:06 +02:00
from library . project import Project
from library . user import User
from library . repository import Repository
2020-06-30 09:56:35 +02:00
from library . repository import push_special_image_to_project
2019-08-09 04:50:06 +02:00
class TestProjects ( unittest . TestCase ) :
2020-11-04 03:13:12 +01:00
@suppress_urllib3_warning
2019-08-09 04:50:06 +02:00
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 ( )
2020-06-30 09:56:35 +02:00
self . repo_name_1 = " test1_sign "
2019-08-09 04:50:06 +02:00
2020-02-25 03:40:29 +01:00
@unittest.skipIf ( TEARDOWN == False , " Test data won ' t be erased. " )
2020-11-04 03:13:12 +01:00
def tearDown ( self ) :
2020-02-27 10:02:11 +01:00
# remove the deletion as the signed image cannot be deleted.
2019-08-09 04:50:06 +02:00
#1. Delete repository(RA) by user(UA);
2021-01-16 12:34:43 +01:00
#self.repo.delete_repository(TestProjects.project_sign_image_name, TestProjects.repo_name.split('/')[1], **TestProjects.USER_sign_image_CLIENT)
2019-08-09 04:50:06 +02:00
#2. Delete project(PA);
2020-02-27 10:02:11 +01:00
#self.project.delete_project(TestProjects.project_sign_image_id, **TestProjects.USER_sign_image_CLIENT)
2019-08-09 04:50:06 +02:00
#3. Delete user(UA);
self . user . delete_user ( TestProjects . user_sign_image_id , * * ADMIN_CLIENT )
def testSignImage ( self ) :
"""
Test case :
Sign A Image
Test step and expected result :
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. Sign image with tag ( TA ) which was tagged by step #5;
7. Get signature of image with tag ( TA ) , it should be exist .
Tear down :
NA
"""
url = ADMIN_CLIENT [ " endpoint " ]
user_001_password = " Aa123456 "
#1. Create user-001
TestProjects . user_sign_image_id , user_sign_image_name = self . user . create_user ( user_password = user_001_password , * * ADMIN_CLIENT )
2020-02-25 03:40:29 +01:00
TestProjects . USER_sign_image_CLIENT = dict ( with_signature = True , endpoint = url , username = user_sign_image_name , password = user_001_password )
2019-08-09 04:50:06 +02:00
#2. Create a new private project(PA) by user(UA);
2020-02-21 10:08:27 +01:00
TestProjects . project_sign_image_id , TestProjects . project_sign_image_name = self . project . create_project ( metadata = { " public " : " false " } , * * ADMIN_CLIENT )
2019-08-09 04:50:06 +02:00
#3. Add user(UA) as a member of project(PA) with project-admin role;
2020-08-17 08:51:18 +02:00
self . project . add_project_members ( TestProjects . project_sign_image_id , user_id = TestProjects . user_sign_image_id , * * ADMIN_CLIENT )
2019-08-09 04:50:06 +02: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 ,
expected_project_id = TestProjects . project_sign_image_id , * * TestProjects . USER_sign_image_CLIENT )
2020-12-04 11:28:29 +01:00
#Note:busybox is pulled in setup phase, and setup is a essential phase.
image = " busybox "
tag = " latest "
2019-08-09 04:50:06 +02:00
#5. Create a new repository(RA) and tag(TA) in project(PA) by user(UA);
2020-12-04 11:28:29 +01:00
#TestProjects.repo_name, tag = push_self_build_image_to_project(TestProjects.project_sign_image_name, harbor_server, user_sign_image_name, user_001_password, image, src_tag)
2019-08-09 04:50:06 +02:00
#6. Sign image with tag(TA) which was tagged by step #5;
2020-02-21 10:08:27 +01:00
sign_image ( harbor_server , TestProjects . project_sign_image_name , image , tag )
2019-08-09 04:50:06 +02:00
#7. Get signature of image with tag(TA), it should be exist.
2020-02-25 03:40:29 +01:00
artifact = self . artifact . get_reference_info ( TestProjects . project_sign_image_name , image , tag , * * TestProjects . USER_sign_image_CLIENT )
2020-11-03 08:32:13 +01:00
self . assertEqual ( artifact . tags [ 0 ] . signed , True )
2019-08-09 04:50:06 +02:00
2020-06-30 09:56:35 +02:00
push_special_image_to_project ( TestProjects . project_sign_image_name , harbor_server , user_sign_image_name , user_001_password , self . repo_name_1 , [ ' 1.0 ' ] )
2021-01-16 12:34:43 +01:00
self . repo . delete_repository ( TestProjects . project_sign_image_name , self . repo_name_1 , * * TestProjects . USER_sign_image_CLIENT )
2020-06-30 09:56:35 +02:00
2021-01-16 12:34:43 +01:00
self . repo . delete_repository ( TestProjects . project_sign_image_name , image , expect_status_code = 412 , expect_response_body = " with signature cannot be deleted " , * * TestProjects . USER_sign_image_CLIENT )
2020-06-30 09:56:35 +02:00
2019-08-09 04:50:06 +02:00
if __name__ == ' __main__ ' :
unittest . main ( )