2018-11-27 12:17:41 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from testutils import ADMIN_CLIENT
|
|
|
|
from testutils import TEARDOWN
|
|
|
|
from library.user import User
|
|
|
|
from library.system import System
|
|
|
|
from library.project import Project
|
|
|
|
from library.repository import Repository
|
|
|
|
from library.repository import push_image_to_project
|
|
|
|
from testutils import harbor_server
|
|
|
|
from library.base import _assert_status_code
|
2020-06-16 11:34:30 +02:00
|
|
|
from library.repository import push_special_image_to_project
|
|
|
|
from library.artifact import Artifact
|
2018-11-27 12:17:41 +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.repo = Repository()
|
2020-06-16 11:34:30 +02:00
|
|
|
self.artifact = Artifact()
|
|
|
|
self.repo_name = "test_repo"
|
|
|
|
self.repo_name_untag = "test_untag"
|
|
|
|
self.tag = "v1.0"
|
2018-11-27 12:17:41 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDown(self):
|
|
|
|
print "Case completed"
|
|
|
|
|
2020-06-16 11:34:30 +02:00
|
|
|
@unittest.skipIf(TEARDOWN == True, "Test data won't be erased.")
|
2018-11-27 12:17:41 +01:00
|
|
|
def test_ClearData(self):
|
|
|
|
#2. Delete project(PA);
|
|
|
|
self.project.delete_project(TestProjects.project_gc_id, **TestProjects.USER_GC_CLIENT)
|
|
|
|
|
|
|
|
#3. Delete user(UA);
|
|
|
|
self.user.delete_user(TestProjects.user_gc_id, **ADMIN_CLIENT)
|
|
|
|
|
|
|
|
def testGarbageCollection(self):
|
|
|
|
"""
|
|
|
|
Test case:
|
|
|
|
Garbage Collection
|
|
|
|
Test step and expected result:
|
|
|
|
1. Create a new user(UA);
|
2020-06-16 11:34:30 +02:00
|
|
|
2. Create project(PA) and project(PB) by user(UA);
|
|
|
|
3. Push a image in project(PA) and then delete repository by admin;
|
|
|
|
4. Get repository by user(UA), it should get nothing;
|
|
|
|
5. Tigger garbage collection operation;
|
|
|
|
6. Check garbage collection job was finished;
|
|
|
|
7. Get garbage collection log, check there is a number of files was deleted;
|
|
|
|
8. Push a image in project(PB) by admin and delete the only tag;
|
|
|
|
9. Tigger garbage collection operation;
|
|
|
|
10. Check garbage collection job was finished;
|
|
|
|
11. Repository with untag image should be still there;
|
|
|
|
12. But no any artifact in repository anymore.
|
2018-11-27 12:17:41 +01:00
|
|
|
Tear down:
|
|
|
|
1. Delete project(PA);
|
|
|
|
2. Delete user(UA).
|
|
|
|
"""
|
|
|
|
url = ADMIN_CLIENT["endpoint"]
|
|
|
|
admin_name = ADMIN_CLIENT["username"]
|
|
|
|
admin_password = ADMIN_CLIENT["password"]
|
|
|
|
user_gc_password = "Aa123456"
|
|
|
|
|
|
|
|
#1. Create a new user(UA);
|
2018-11-29 11:27:53 +01:00
|
|
|
TestProjects.user_gc_id, user_gc_name = self.user.create_user(user_password = user_gc_password, **ADMIN_CLIENT)
|
2018-11-27 12:17:41 +01:00
|
|
|
|
|
|
|
TestProjects.USER_GC_CLIENT=dict(endpoint = url, username = user_gc_name, password = user_gc_password)
|
|
|
|
|
2020-06-16 11:34:30 +02:00
|
|
|
#2. Create project(PA) and project(PB) by user(UA);
|
2020-02-21 10:08:27 +01:00
|
|
|
TestProjects.project_gc_id, TestProjects.project_gc_name = self.project.create_project(metadata = {"public": "false"}, **TestProjects.USER_GC_CLIENT)
|
2020-06-16 11:34:30 +02:00
|
|
|
TestProjects.project_gc_untag_id, TestProjects.project_gc_untag_name = self.project.create_project(metadata = {"public": "false"}, **TestProjects.USER_GC_CLIENT)
|
2018-11-27 12:17:41 +01:00
|
|
|
|
2020-06-16 11:34:30 +02:00
|
|
|
#3. Push a image in project(PA) and then delete repository by admin;
|
|
|
|
push_special_image_to_project(TestProjects.project_gc_name, harbor_server, admin_name, admin_password, self.repo_name, ["latest", "v1.2.3"])
|
|
|
|
self.repo.delete_repoitory(TestProjects.project_gc_name, self.repo_name, **TestProjects.USER_GC_CLIENT)
|
2018-11-27 12:17:41 +01:00
|
|
|
|
2020-06-16 11:34:30 +02:00
|
|
|
#4. Get repository by user(UA), it should get nothing;
|
2020-03-07 04:15:19 +01:00
|
|
|
repo_data = self.repo.list_repositories(TestProjects.project_gc_name, **TestProjects.USER_GC_CLIENT)
|
2018-11-27 12:17:41 +01:00
|
|
|
_assert_status_code(len(repo_data), 0)
|
|
|
|
|
2020-06-16 11:34:30 +02:00
|
|
|
#5. Tigger garbage collection operation;
|
2018-11-27 12:17:41 +01:00
|
|
|
gc_id = self.system.gc_now(**ADMIN_CLIENT)
|
|
|
|
|
2020-06-16 11:34:30 +02:00
|
|
|
#6. Check garbage collection job was finished;
|
2018-11-27 12:17:41 +01:00
|
|
|
self.system.validate_gc_job_status(gc_id, "finished", **ADMIN_CLIENT)
|
|
|
|
|
2020-06-16 11:34:30 +02:00
|
|
|
#7. Get garbage collection log, check there is a number of files was deleted;
|
2018-11-27 12:17:41 +01:00
|
|
|
self.system.validate_deletion_success(gc_id, **ADMIN_CLIENT)
|
|
|
|
|
2020-06-16 11:34:30 +02:00
|
|
|
#8. Push a image in project(PB) by admin and delete the only tag;
|
|
|
|
push_special_image_to_project(TestProjects.project_gc_untag_name, harbor_server, admin_name, admin_password, self.repo_name_untag, [self.tag])
|
|
|
|
self.artifact.delete_tag(TestProjects.project_gc_untag_name, self.repo_name_untag, self.tag, self.tag, **ADMIN_CLIENT)
|
|
|
|
|
|
|
|
#9. Tigger garbage collection operation;
|
|
|
|
gc_id = self.system.gc_now(**ADMIN_CLIENT)
|
|
|
|
|
|
|
|
#10. Check garbage collection job was finished;
|
|
|
|
self.system.validate_gc_job_status(gc_id, "finished", **ADMIN_CLIENT)
|
|
|
|
|
|
|
|
#11. Repository with untag image should be still there;
|
|
|
|
repo_data_untag = self.repo.list_repositories(TestProjects.project_gc_untag_name, **TestProjects.USER_GC_CLIENT)
|
|
|
|
print "repo_data_untag:", repo_data_untag
|
|
|
|
_assert_status_code(len(repo_data_untag), 1)
|
|
|
|
self.assertEqual(TestProjects.project_gc_untag_name + "/" + self.repo_name_untag , repo_data_untag[0].name)
|
|
|
|
|
|
|
|
#12. But no any artifact in repository anymore.
|
|
|
|
artifacts = self.artifact.list_artifacts(TestProjects.project_gc_untag_name, self.repo_name_untag, **TestProjects.USER_GC_CLIENT)
|
|
|
|
self.assertEqual(artifacts,[])
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-27 12:17:41 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|