2019-09-03 09:52:41 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import time
|
|
|
|
|
|
|
|
from testutils import ADMIN_CLIENT
|
|
|
|
from testutils import TEARDOWN
|
|
|
|
from testutils import harbor_server
|
|
|
|
from library.repository import push_special_image_to_project
|
2020-04-22 10:44:10 +02:00
|
|
|
from library.docker_api import list_image_tags
|
2019-09-03 09:52:41 +02:00
|
|
|
|
|
|
|
from library.retention import Retention
|
|
|
|
from library.project import Project
|
|
|
|
from library.repository import Repository
|
|
|
|
from library.user import User
|
|
|
|
from library.system import System
|
2020-04-22 10:44:10 +02:00
|
|
|
from library.artifact import Artifact
|
2019-09-03 09:52:41 +02:00
|
|
|
|
|
|
|
class TestProjects(unittest.TestCase):
|
|
|
|
"""
|
|
|
|
Test case:
|
|
|
|
Tag Retention
|
|
|
|
Setup:
|
|
|
|
Create Project test-retention
|
|
|
|
Push image test1:1.0, test1:2.0, test1:3.0,latest, test2:1.0, test2:latest, test3:1.0, test4:1.0
|
|
|
|
Test Steps:
|
|
|
|
1. Create Retention Policy
|
|
|
|
2. Add rule "For the repositories matching **, retain always with tags matching latest*"
|
|
|
|
3. Add rule "For the repositories matching test3*, retain always with tags matching **"
|
|
|
|
4. Dry run, check execution and tasks
|
|
|
|
5. Real run, check images retained
|
|
|
|
Tear Down:
|
|
|
|
1. Delete project test-retention
|
|
|
|
"""
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(self):
|
|
|
|
self.user = User()
|
|
|
|
self.system = System()
|
2020-03-16 03:13:28 +01:00
|
|
|
self.repo = Repository()
|
2019-09-03 09:52:41 +02:00
|
|
|
self.project = Project()
|
2020-02-25 03:40:29 +01:00
|
|
|
self.retention = Retention()
|
2020-04-22 10:44:10 +02:00
|
|
|
self.artifact = Artifact()
|
|
|
|
self.repo_name_1 = "test1"
|
2020-05-08 05:52:42 +02:00
|
|
|
self.repo_name_2 = "test2"
|
2019-09-03 09:52:41 +02:00
|
|
|
|
|
|
|
def testTagRetention(self):
|
|
|
|
user_ra_password = "Aa123456"
|
|
|
|
user_ra_id, user_ra_name = self.user.create_user(user_password=user_ra_password, **ADMIN_CLIENT)
|
|
|
|
print("Created user: %s, id: %s" % (user_ra_name, user_ra_id))
|
|
|
|
TestProjects.USER_RA_CLIENT = dict(endpoint=ADMIN_CLIENT["endpoint"],
|
|
|
|
username=user_ra_name,
|
|
|
|
password=user_ra_password)
|
|
|
|
TestProjects.user_ra_id = int(user_ra_id)
|
|
|
|
|
2020-02-21 10:08:27 +01:00
|
|
|
TestProjects.project_src_repo_id, TestProjects.project_src_repo_name = self.project.create_project(metadata = {"public": "false"}, **TestProjects.USER_RA_CLIENT)
|
2019-09-03 09:52:41 +02:00
|
|
|
|
|
|
|
# Push image test1:1.0, test1:2.0, test1:3.0,latest, test2:1.0, test2:latest, test3:1.0
|
2020-04-22 10:44:10 +02:00
|
|
|
push_special_image_to_project(TestProjects.project_src_repo_name, harbor_server, user_ra_name, user_ra_password, self.repo_name_1, ['1.0'])
|
|
|
|
push_special_image_to_project(TestProjects.project_src_repo_name, harbor_server, user_ra_name, user_ra_password, self.repo_name_1, ['2.0'])
|
|
|
|
push_special_image_to_project(TestProjects.project_src_repo_name, harbor_server, user_ra_name, user_ra_password, self.repo_name_1, ['3.0','latest'])
|
2020-05-08 05:52:42 +02:00
|
|
|
push_special_image_to_project(TestProjects.project_src_repo_name, harbor_server, user_ra_name, user_ra_password, self.repo_name_2, ['1.0'])
|
|
|
|
push_special_image_to_project(TestProjects.project_src_repo_name, harbor_server, user_ra_name, user_ra_password, self.repo_name_2, ['latest'])
|
2020-02-21 10:08:27 +01:00
|
|
|
push_special_image_to_project(TestProjects.project_src_repo_name, harbor_server, user_ra_name, user_ra_password, "test3", ['1.0'])
|
|
|
|
push_special_image_to_project(TestProjects.project_src_repo_name, harbor_server, user_ra_name, user_ra_password, "test4", ['1.0'])
|
2019-09-03 09:52:41 +02:00
|
|
|
|
2020-05-08 05:52:42 +02:00
|
|
|
tag_data_artifact3_image1 = self.artifact.get_reference_info(TestProjects.project_src_repo_name, self.repo_name_1, "3.0", **TestProjects.USER_RA_CLIENT)
|
|
|
|
|
|
|
|
tag_data_artifact2_image2 = self.artifact.get_reference_info(TestProjects.project_src_repo_name, self.repo_name_2, "latest", **TestProjects.USER_RA_CLIENT)
|
|
|
|
|
2020-04-22 10:44:10 +02:00
|
|
|
tags = list_image_tags(harbor_server, TestProjects.project_src_repo_name+"/"+self.repo_name_1, user_ra_name, user_ra_password)
|
2020-05-08 05:52:42 +02:00
|
|
|
#Delete all 2 tags of "artifact3" in repostory "image1";
|
|
|
|
self.artifact.delete_tag(TestProjects.project_src_repo_name, self.repo_name_1, "3.0", "latest", **TestProjects.USER_RA_CLIENT)
|
|
|
|
self.artifact.delete_tag(TestProjects.project_src_repo_name, self.repo_name_1, "3.0", "3.0", **TestProjects.USER_RA_CLIENT)
|
2020-04-22 10:44:10 +02:00
|
|
|
tags = list_image_tags(harbor_server, TestProjects.project_src_repo_name+"/"+self.repo_name_1, user_ra_name, user_ra_password)
|
|
|
|
|
2020-03-07 04:15:19 +01:00
|
|
|
resp=self.repo.list_repositories(TestProjects.project_src_repo_name, **TestProjects.USER_RA_CLIENT)
|
2019-09-03 09:52:41 +02:00
|
|
|
self.assertEqual(len(resp), 4)
|
|
|
|
|
|
|
|
# Create Retention Policy
|
|
|
|
retention_id = self.retention.create_retention_policy(TestProjects.project_src_repo_id, selector_repository="**", selector_tag="latest*", expect_status_code = 201, **TestProjects.USER_RA_CLIENT)
|
|
|
|
|
|
|
|
# Add rule
|
|
|
|
self.retention.update_retention_add_rule(retention_id,selector_repository="test3*", selector_tag="**", expect_status_code = 200, **TestProjects.USER_RA_CLIENT)
|
|
|
|
|
|
|
|
# Dry run
|
|
|
|
self.retention.trigger_retention_policy(retention_id, dry_run=True, **TestProjects.USER_RA_CLIENT)
|
2019-09-20 09:57:45 +02:00
|
|
|
time.sleep(10)
|
2019-09-03 09:52:41 +02:00
|
|
|
resp=self.retention.get_retention_executions(retention_id, **TestProjects.USER_RA_CLIENT)
|
|
|
|
self.assertTrue(len(resp)>0)
|
|
|
|
execution=resp[0]
|
|
|
|
resp=self.retention.get_retention_exec_tasks(retention_id,execution.id, **TestProjects.USER_RA_CLIENT)
|
|
|
|
self.assertEqual(len(resp), 4)
|
|
|
|
resp=self.retention.get_retention_exec_task_log(retention_id,execution.id,resp[0].id, **TestProjects.USER_RA_CLIENT)
|
2020-04-22 10:44:10 +02:00
|
|
|
#For Debug:
|
|
|
|
print("Task 0 log begin:-----------------------------")
|
|
|
|
i=0
|
|
|
|
for line in resp.split("\n"):
|
|
|
|
print("Line"+str(i)+": "+line)
|
|
|
|
i=i+1
|
|
|
|
print("Task 0 log end:-----------------------------")
|
2019-09-03 09:52:41 +02:00
|
|
|
|
|
|
|
# Real run
|
|
|
|
self.retention.trigger_retention_policy(retention_id, dry_run=False, **TestProjects.USER_RA_CLIENT)
|
|
|
|
time.sleep(10)
|
|
|
|
resp=self.retention.get_retention_executions(retention_id, **TestProjects.USER_RA_CLIENT)
|
|
|
|
self.assertTrue(len(resp)>1)
|
|
|
|
execution=resp[0]
|
|
|
|
resp=self.retention.get_retention_exec_tasks(retention_id,execution.id, **TestProjects.USER_RA_CLIENT)
|
|
|
|
self.assertEqual(len(resp), 4)
|
|
|
|
resp=self.retention.get_retention_exec_task_log(retention_id,execution.id,resp[0].id, **TestProjects.USER_RA_CLIENT)
|
|
|
|
print(resp)
|
2020-05-08 05:52:42 +02:00
|
|
|
|
|
|
|
#List artifacts successfully, and untagged artifact in test1 should be the only one retained;
|
|
|
|
artifacts_1 = self.artifact.list_artifacts(TestProjects.project_src_repo_name, self.repo_name_1, **TestProjects.USER_RA_CLIENT)
|
|
|
|
self.assertTrue(len(artifacts_1)==1)
|
|
|
|
self.assertEqual(artifacts_1[0].digest, tag_data_artifact3_image1[0].digest)
|
|
|
|
|
|
|
|
#List artifacts successfully, and artifact with latest tag in test2 should be the only one retained;
|
|
|
|
artifacts_2 = self.artifact.list_artifacts(TestProjects.project_src_repo_name, self.repo_name_2, **TestProjects.USER_RA_CLIENT)
|
|
|
|
self.assertTrue(len(artifacts_2)==1)
|
|
|
|
self.assertEqual(artifacts_2[0].digest, tag_data_artifact2_image2[0].digest)
|
2019-09-03 09:52:41 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(self):
|
2020-07-30 10:04:14 +02:00
|
|
|
print("Case completed")
|
2019-09-03 09:52:41 +02:00
|
|
|
|
2020-02-17 03:53:09 +01:00
|
|
|
# TODO delete_repoitory will fail when no tags left anymore
|
|
|
|
# @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.")
|
|
|
|
# def test_ClearData(self):
|
2020-03-07 04:15:19 +01:00
|
|
|
# resp=self.repo.list_repositories(TestProjects.project_src_repo_id, **TestProjects.USER_RA_CLIENT)
|
2020-02-17 03:53:09 +01:00
|
|
|
# for repo in resp:
|
|
|
|
# self.repo.delete_repoitory(repo.name, **TestProjects.USER_RA_CLIENT)
|
|
|
|
# self.project.delete_project(TestProjects.project_src_repo_id, **TestProjects.USER_RA_CLIENT)
|
|
|
|
# self.user.delete_user(TestProjects.user_ra_id, **ADMIN_CLIENT)
|
2019-09-03 09:52:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|