2019-02-18 06:47:16 +01: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-02-18 06:47:16 +01:00
from testutils import TEARDOWN
2020-08-07 08:56:18 +02:00
from testutils import harbor_server
2019-02-18 06:47:16 +01:00
from library . user import User
from library . project import Project
2020-11-20 06:13:12 +01:00
from library . robot import Robot
2019-02-18 06:47:16 +01:00
from library . repository import Repository
from library . repository import pull_harbor_image
from library . repository import push_image_to_project
from library . base import _assert_status_code
class TestProjects ( unittest . TestCase ) :
2020-11-04 03:13:12 +01:00
@suppress_urllib3_warning
2019-02-18 06:47:16 +01: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 . repo = Repository ( )
2020-11-20 06:13:12 +01:00
self . robot = Robot ( )
2019-02-18 06:47:16 +01:00
2020-11-04 03:13:12 +01:00
@unittest.skipIf ( TEARDOWN == False , " Test data won ' t be erased. " )
2019-02-18 06:47:16 +01:00
def tearDown ( self ) :
2020-07-30 10:04:14 +02:00
print ( " Case completed " )
2019-02-18 06:47:16 +01:00
@unittest.skipIf ( TEARDOWN == False , " Test data won ' t be erased. " )
def test_ClearData ( self ) :
#1. Delete repository(RA) by user(UA);
2020-02-21 10:08:27 +01:00
self . repo . delete_repoitory ( TestProjects . project_ra_name_a , TestProjects . repo_name_in_project_a . split ( ' / ' ) [ 1 ] , * * TestProjects . USER_RA_CLIENT )
self . repo . delete_repoitory ( TestProjects . project_ra_name_b , TestProjects . repo_name_in_project_b . split ( ' / ' ) [ 1 ] , * * TestProjects . USER_RA_CLIENT )
self . repo . delete_repoitory ( TestProjects . project_ra_name_c , TestProjects . repo_name_in_project_c . split ( ' / ' ) [ 1 ] , * * TestProjects . USER_RA_CLIENT )
self . repo . delete_repoitory ( TestProjects . project_ra_name_a , TestProjects . repo_name_pa . split ( ' / ' ) [ 1 ] , * * TestProjects . USER_RA_CLIENT )
2019-02-18 06:47:16 +01:00
#2. Delete project(PA);
self . project . delete_project ( TestProjects . project_ra_id_a , * * TestProjects . USER_RA_CLIENT )
self . project . delete_project ( TestProjects . project_ra_id_b , * * TestProjects . USER_RA_CLIENT )
self . project . delete_project ( TestProjects . project_ra_id_c , * * TestProjects . USER_RA_CLIENT )
2020-02-03 03:24:28 +01:00
#3. Delete user(UA).
2019-02-18 06:47:16 +01:00
self . user . delete_user ( TestProjects . user_ra_id , * * ADMIN_CLIENT )
def testRobotAccount ( self ) :
"""
Test case :
Robot Account
Test step and expected result :
1. Create user ( UA ) ;
2. Create private project ( PA ) , private project ( PB ) and public project ( PC ) by user ( UA ) ;
3. Push image ( ImagePA ) to project ( PA ) , image ( ImagePB ) to project ( PB ) and image ( ImagePC ) to project ( PC ) by user ( UA ) ;
4. Create a new robot account ( RA ) with pull and push privilige in project ( PA ) by user ( UA ) ;
5. Check robot account info , it should has both pull and push priviliges ;
6. Pull image ( ImagePA ) from project ( PA ) by robot account ( RA ) , it must be successful ;
7. Push image ( ImageRA ) to project ( PA ) by robot account ( RA ) , it must be successful ;
8. Push image ( ImageRA ) to project ( PB ) by robot account ( RA ) , it must be not successful ;
9. Pull image ( ImagePB ) from project ( PB ) by robot account ( RA ) , it must be not successful ;
10. Pull image from project ( PC ) , it must be successful ;
11. Push image ( ImageRA ) to project ( PC ) by robot account ( RA ) , it must be not successful ;
12. Update action property of robot account ( RA ) ;
13. Pull image ( ImagePA ) from project ( PA ) by robot account ( RA ) , it must be not successful ;
14. Push image ( ImageRA ) to project ( PA ) by robot account ( RA ) , it must be not successful ;
2020-02-03 03:24:28 +01:00
15. Delete robot account ( RA ) , it must be not successful .
2019-02-18 06:47:16 +01:00
Tear down :
2020-02-03 03:24:28 +01:00
1. Delete repository ( RA ) by user ( UA ) ;
2. Delete project ( PA ) ;
3. Delete user ( UA ) .
2019-02-18 06:47:16 +01:00
"""
url = ADMIN_CLIENT [ " endpoint " ]
admin_name = ADMIN_CLIENT [ " username " ]
admin_password = ADMIN_CLIENT [ " password " ]
user_ra_password = " Aa123456 "
2020-01-15 07:39:25 +01:00
image_project_a = " haproxy "
2019-02-18 06:47:16 +01:00
image_project_b = " hello-world "
2020-01-15 07:39:25 +01:00
image_project_c = " httpd "
2020-04-20 11:16:02 +02:00
image_robot_account = " alpine "
2019-02-18 06:47:16 +01:00
tag = " latest "
2020-07-30 10:04:14 +02:00
#1. Create user(UA);"
2019-02-18 06:47:16 +01:00
TestProjects . user_ra_id , user_ra_name = self . user . create_user ( user_password = user_ra_password , * * ADMIN_CLIENT )
TestProjects . USER_RA_CLIENT = dict ( endpoint = url , username = user_ra_name , password = user_ra_password )
2020-07-30 10:04:14 +02:00
#2. Create private project(PA), private project(PB) and public project(PC) by user(UA);
2020-02-21 10:08:27 +01:00
TestProjects . project_ra_id_a , TestProjects . project_ra_name_a = self . project . create_project ( metadata = { " public " : " false " } , * * TestProjects . USER_RA_CLIENT )
TestProjects . project_ra_id_b , TestProjects . project_ra_name_b = self . project . create_project ( metadata = { " public " : " false " } , * * TestProjects . USER_RA_CLIENT )
TestProjects . project_ra_id_c , TestProjects . project_ra_name_c = self . project . create_project ( metadata = { " public " : " true " } , * * TestProjects . USER_RA_CLIENT )
2019-02-18 06:47:16 +01:00
2020-07-30 10:04:14 +02:00
#3. Push image(ImagePA) to project(PA), image(ImagePB) to project(PB) and image(ImagePC) to project(PC) by user(UA);
2020-02-21 10:08:27 +01:00
TestProjects . repo_name_in_project_a , tag_a = push_image_to_project ( TestProjects . project_ra_name_a , harbor_server , user_ra_name , user_ra_password , image_project_a , tag )
TestProjects . repo_name_in_project_b , tag_b = push_image_to_project ( TestProjects . project_ra_name_b , harbor_server , user_ra_name , user_ra_password , image_project_b , tag )
TestProjects . repo_name_in_project_c , tag_c = push_image_to_project ( TestProjects . project_ra_name_c , harbor_server , user_ra_name , user_ra_password , image_project_c , tag )
2019-02-18 06:47:16 +01:00
2020-11-20 06:13:12 +01:00
#4. Create a new robot account(RA) with pull and push privilege in project(PA) by user(UA);
robot_id , robot_account = self . robot . create_project_robot ( TestProjects . project_ra_name_a ,
2019-12-23 08:53:11 +01:00
2441000531 , * * TestProjects . USER_RA_CLIENT )
2019-02-18 06:47:16 +01:00
2020-11-20 06:13:12 +01:00
#5. Check robot account info, it should has both pull and push privilege;
data = self . robot . get_robot_account_by_id ( robot_id , * * TestProjects . USER_RA_CLIENT )
2019-02-18 06:47:16 +01:00
_assert_status_code ( robot_account . name , data . name )
2020-07-30 10:04:14 +02:00
#6. Pull image(ImagePA) from project(PA) by robot account(RA), it must be successful;
2020-11-20 06:13:12 +01:00
pull_harbor_image ( harbor_server , robot_account . name , robot_account . secret , TestProjects . repo_name_in_project_a , tag_a )
2019-02-18 06:47:16 +01:00
2020-07-30 10:04:14 +02:00
#7. Push image(ImageRA) to project(PA) by robot account(RA), it must be successful;
2020-11-20 06:13:12 +01:00
TestProjects . repo_name_pa , _ = push_image_to_project ( TestProjects . project_ra_name_a , harbor_server , robot_account . name , robot_account . secret , image_robot_account , tag )
2019-02-18 06:47:16 +01:00
2020-07-30 10:04:14 +02:00
#8. Push image(ImageRA) to project(PB) by robot account(RA), it must be not successful;
2020-11-20 06:13:12 +01:00
push_image_to_project ( TestProjects . project_ra_name_b , harbor_server , robot_account . name , robot_account . secret , image_robot_account , tag , expected_error_message = " unauthorized to access repository " )
2019-02-18 06:47:16 +01:00
2020-07-30 10:04:14 +02:00
#9. Pull image(ImagePB) from project(PB) by robot account(RA), it must be not successful;
2020-11-20 06:13:12 +01:00
pull_harbor_image ( harbor_server , robot_account . name , robot_account . secret , TestProjects . repo_name_in_project_b , tag_b , expected_error_message = " unauthorized to access repository " )
2019-02-18 06:47:16 +01:00
2020-07-30 10:04:14 +02:00
#10. Pull image from project(PC), it must be successful;
2020-11-20 06:13:12 +01:00
pull_harbor_image ( harbor_server , robot_account . name , robot_account . secret , TestProjects . repo_name_in_project_c , tag_c )
2019-02-18 06:47:16 +01:00
2020-07-30 10:04:14 +02:00
#11. Push image(ImageRA) to project(PC) by robot account(RA), it must be not successful;
2020-11-20 06:13:12 +01:00
push_image_to_project ( TestProjects . project_ra_name_c , harbor_server , robot_account . name , robot_account . secret , image_robot_account , tag , expected_error_message = " unauthorized to access repository " )
2019-02-18 06:47:16 +01:00
2020-07-30 10:04:14 +02:00
#12. Update action property of robot account(RA);"
2020-11-20 06:13:12 +01:00
self . robot . disable_robot_account ( robot_id , True , * * TestProjects . USER_RA_CLIENT )
2019-02-18 06:47:16 +01:00
2020-07-30 10:04:14 +02:00
#13. Pull image(ImagePA) from project(PA) by robot account(RA), it must be not successful;
2020-11-20 06:13:12 +01:00
pull_harbor_image ( harbor_server , robot_account . name , robot_account . secret , TestProjects . repo_name_in_project_a , tag_a , expected_login_error_message = " unauthorized: authentication required " )
2019-02-18 06:47:16 +01:00
2020-07-30 10:04:14 +02:00
#14. Push image(ImageRA) to project(PA) by robot account(RA), it must be not successful;
2020-11-20 06:13:12 +01:00
push_image_to_project ( TestProjects . project_ra_name_a , harbor_server , robot_account . name , robot_account . secret , image_robot_account , tag , expected_login_error_message = " unauthorized: authentication required " )
2019-02-18 06:47:16 +01:00
2020-07-30 10:04:14 +02:00
#15. Delete robot account(RA), it must be not successful.
2020-11-20 06:13:12 +01:00
self . robot . delete_robot_account ( robot_id , * * TestProjects . USER_RA_CLIENT )
2019-02-18 06:47:16 +01:00
if __name__ == ' __main__ ' :
unittest . main ( )