| 
									
										
										
										
											2018-11-29 18:27:53 +08:00
										 |  |  | from __future__ import absolute_import | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2020-05-29 16:17:10 +08:00
										 |  |  | import time | 
					
						
							| 
									
										
										
										
											2018-11-29 18:27:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-04 10:13:12 +08:00
										 |  |  | from testutils import ADMIN_CLIENT, suppress_urllib3_warning | 
					
						
							| 
									
										
										
										
											2018-11-29 18:27:53 +08:00
										 |  |  | from testutils import TEARDOWN | 
					
						
							|  |  |  | from testutils import TestResult | 
					
						
							|  |  |  | from library.user import User | 
					
						
							| 
									
										
										
										
											2020-03-19 14:56:37 +08:00
										 |  |  | from library.projectV2 import ProjectV2 | 
					
						
							| 
									
										
										
										
											2018-11-29 18:27:53 +08:00
										 |  |  | from library.project import Project | 
					
						
							|  |  |  | from library.repository import Repository | 
					
						
							| 
									
										
										
										
											2020-12-04 18:28:29 +08:00
										 |  |  | from library.repository import push_self_build_image_to_project | 
					
						
							| 
									
										
										
										
											2018-11-29 18:27:53 +08:00
										 |  |  | from testutils import harbor_server | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TestProjects(unittest.TestCase): | 
					
						
							| 
									
										
										
										
											2020-11-04 10:13:12 +08:00
										 |  |  |     @suppress_urllib3_warning | 
					
						
							| 
									
										
										
										
											2018-11-29 18:27:53 +08:00
										 |  |  |     def setUp(self): | 
					
						
							| 
									
										
										
										
											2020-11-04 10:13:12 +08:00
										 |  |  |         self.project= Project() | 
					
						
							|  |  |  |         self.user= User() | 
					
						
							|  |  |  |         self.repo= Repository() | 
					
						
							|  |  |  |         self.projectv2= ProjectV2() | 
					
						
							| 
									
										
										
										
											2018-11-29 18:27:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-04 10:13:12 +08:00
										 |  |  |     @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.") | 
					
						
							| 
									
										
										
										
											2018-11-29 18:27:53 +08:00
										 |  |  |     def tearDown(self): | 
					
						
							| 
									
										
										
										
											2020-07-30 16:04:14 +08:00
										 |  |  |         print("Case completed") | 
					
						
							| 
									
										
										
										
											2018-11-29 18:27:53 +08:00
										 |  |  |         #1. Delete project(PA); | 
					
						
							|  |  |  |         self.project.delete_project(TestProjects.project_user_view_logs_id, **TestProjects.USER_USER_VIEW_LOGS_CLIENT) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         #2. Delete user(UA); | 
					
						
							|  |  |  |         self.user.delete_user(TestProjects.user_user_view_logs_id, **ADMIN_CLIENT) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def testUserViewLogs(self): | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         Test case: | 
					
						
							|  |  |  |             User View Logs | 
					
						
							|  |  |  |         Test step and expected result: | 
					
						
							|  |  |  |             1. Create a new user(UA); | 
					
						
							|  |  |  |             2. Create a new project(PA) by user(UA), in project(PA), there should be 1 'create' log record;; | 
					
						
							|  |  |  |             3. Push a new image(IA) in project(PA) by admin, in project(PA), there should be 1 'push' log record;; | 
					
						
							|  |  |  |             4. Delete repository(RA) by user(UA), in project(PA), there should be 1 'delete' log record;; | 
					
						
							|  |  |  |         Tear down: | 
					
						
							|  |  |  |             1. Delete project(PA); | 
					
						
							|  |  |  |             2. Delete user(UA). | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2020-11-04 10:13:12 +08:00
										 |  |  |         test_result= TestResult() | 
					
						
							| 
									
										
										
										
											2018-11-29 18:27:53 +08:00
										 |  |  |         url = ADMIN_CLIENT["endpoint"] | 
					
						
							|  |  |  |         admin_name = ADMIN_CLIENT["username"] | 
					
						
							|  |  |  |         admin_password = ADMIN_CLIENT["password"] | 
					
						
							|  |  |  |         user_content_trust_password = "Aa123456" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         #1. Create a new user(UA); | 
					
						
							|  |  |  |         TestProjects.user_user_view_logs_id, user_user_view_logs_name = self.user.create_user(user_password = user_content_trust_password, **ADMIN_CLIENT) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         TestProjects.USER_USER_VIEW_LOGS_CLIENT=dict(endpoint = url, username = user_user_view_logs_name, password = user_content_trust_password) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         #2.1 Create a new project(PA) by user(UA); | 
					
						
							|  |  |  |         TestProjects.project_user_view_logs_id, project_user_view_logs_name = self.project.create_project(metadata = {"public": "false"}, **TestProjects.USER_USER_VIEW_LOGS_CLIENT) | 
					
						
							| 
									
										
										
										
											2020-05-29 16:17:10 +08:00
										 |  |  |         time.sleep(2) | 
					
						
							| 
									
										
										
										
											2018-11-29 18:27:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         #2.2 In project(PA), there should be 1 'create' log record; | 
					
						
							|  |  |  |         operation = "create" | 
					
						
							| 
									
										
										
										
											2020-03-19 14:56:37 +08:00
										 |  |  |         log_count = self.projectv2.filter_project_logs(project_user_view_logs_name, user_user_view_logs_name, project_user_view_logs_name, "project", operation, **TestProjects.USER_USER_VIEW_LOGS_CLIENT) | 
					
						
							| 
									
										
										
										
											2018-11-29 18:27:53 +08:00
										 |  |  |         if log_count != 1: | 
					
						
							| 
									
										
										
										
											2020-11-04 10:13:12 +08:00
										 |  |  |             test_result.add_test_result("1 - Failed to get log with user:{}, resource:{}, resource_type:{} and operation:{}, expect count 1, but actual is {}.". | 
					
						
							| 
									
										
										
										
											2020-05-29 16:17:10 +08:00
										 |  |  |                                              format(user_user_view_logs_name, project_user_view_logs_name, "project", operation, log_count)) | 
					
						
							| 
									
										
										
										
											2018-11-29 18:27:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         #3.1 Push a new image(IA) in project(PA) by admin; | 
					
						
							| 
									
										
										
										
											2020-12-04 18:28:29 +08:00
										 |  |  |         repo_name, tag = push_self_build_image_to_project(project_user_view_logs_name, harbor_server, admin_name, admin_password, "tomcat", "latest") | 
					
						
							| 
									
										
										
										
											2020-05-29 16:17:10 +08:00
										 |  |  |         time.sleep(2) | 
					
						
							| 
									
										
										
										
											2018-11-29 18:27:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         #3.2 In project(PA), there should be 1 'push' log record; | 
					
						
							| 
									
										
										
										
											2020-03-19 14:56:37 +08:00
										 |  |  |         operation = "create" | 
					
						
							|  |  |  |         log_count = self.projectv2.filter_project_logs(project_user_view_logs_name,  admin_name, r'{}:{}'.format(repo_name, tag), "artifact", operation, **TestProjects.USER_USER_VIEW_LOGS_CLIENT) | 
					
						
							| 
									
										
										
										
											2018-11-29 18:27:53 +08:00
										 |  |  |         if log_count != 1: | 
					
						
							| 
									
										
										
										
											2020-11-04 10:13:12 +08:00
										 |  |  |             test_result.add_test_result("2 - Failed to get log with user:{}, resource:{}, resource_type:{} and operation:{}, expect count 1, but actual is {}.". | 
					
						
							| 
									
										
										
										
											2020-05-29 16:17:10 +08:00
										 |  |  |                                              format(user_user_view_logs_name, project_user_view_logs_name, "artifact", operation, log_count)) | 
					
						
							| 
									
										
										
										
											2018-11-29 18:27:53 +08:00
										 |  |  |         #4.1 Delete repository(RA) by user(UA); | 
					
						
							| 
									
										
										
										
											2021-01-16 19:34:43 +08:00
										 |  |  |         self.repo.delete_repository(project_user_view_logs_name, repo_name.split('/')[1], **TestProjects.USER_USER_VIEW_LOGS_CLIENT) | 
					
						
							| 
									
										
										
										
											2020-06-16 17:34:30 +08:00
										 |  |  |         time.sleep(6) | 
					
						
							| 
									
										
										
										
											2018-11-29 18:27:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         #4.2 In project(PA), there should be 1 'delete' log record; | 
					
						
							|  |  |  |         operation = "delete" | 
					
						
							| 
									
										
										
										
											2020-03-19 14:56:37 +08:00
										 |  |  |         log_count = self.projectv2.filter_project_logs(project_user_view_logs_name, user_user_view_logs_name, repo_name, "repository", operation, **TestProjects.USER_USER_VIEW_LOGS_CLIENT) | 
					
						
							| 
									
										
										
										
											2018-11-29 18:27:53 +08:00
										 |  |  |         if log_count != 1: | 
					
						
							| 
									
										
										
										
											2020-11-04 10:13:12 +08:00
										 |  |  |             test_result.add_test_result("5 - Failed to get log with user:{}, resource:{}, resource_type:{} and operation:{}, expect count 1, but actual is {}.". | 
					
						
							| 
									
										
										
										
											2020-05-29 16:17:10 +08:00
										 |  |  |                                              format(user_user_view_logs_name, project_user_view_logs_name, "repository", operation, log_count)) | 
					
						
							| 
									
										
										
										
											2018-11-29 18:27:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-04 10:13:12 +08:00
										 |  |  |         test_result.get_final_result() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-29 18:27:53 +08:00
										 |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											2021-01-16 19:34:43 +08:00
										 |  |  |     unittest.main() |