class TestProjects(unittest.TestCase):
    @suppress_urllib3_warning
    def setUp(self):
        self.project = Project()
        self.user = User()
        self.artifact = Artifact()
        self.repo = Repository()
        self.repo_name = "hello-artifact"
        self.tag = "test_v2"

    @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.")
    def tearDown(self):
        #1. Delete user(UA);
        self.user.delete_user(TestProjects.user_sign_image_id, **ADMIN_CLIENT)

    def testOrasCli(self):
        """
        Test case:
            Push Artifact With ORAS CLI
        Test step and expected result:
            1. Create user-001
            2. Create a new private project(PA) by user(UA);
            3. ORAS CLI push artifacts;
            4. Get repository from Harbor successfully, and verfiy repository name is repo pushed by ORAS CLI;
            5. Get and verify artifacts by tag;
            6. ORAS CLI pull artifacts index by tag;
            7. Verfiy MD5 between artifacts pushed by ORAS and artifacts pulled by ORAS;
        Tear down:
            NA
        """
        url = ADMIN_CLIENT["endpoint"]
        user_001_password = "******"

        #1. Create user-001
        TestProjects.user_sign_image_id, user_name = self.user.create_user(user_password = user_001_password, **ADMIN_CLIENT)

        TestProjects.USER_CLIENT=dict(with_signature = True, endpoint = url, username = user_name, password = user_001_password)

        #2. Create a new private project(PA) by user(UA);
        TestProjects.project_id, TestProjects.project_name = self.project.create_project(metadata = {"public": "false"}, **TestProjects.USER_CLIENT)

        #3. ORAS CLI push artifacts;
        md5_list_push = library.oras.oras_push(harbor_server, user_name, user_001_password, TestProjects.project_name, self.repo_name, self.tag)

        #4. Get repository from Harbor successfully, and verfiy repository name is repo pushed by ORAS CLI;
        repo_data = self.repo.get_repository(TestProjects.project_name, self.repo_name, **TestProjects.USER_CLIENT)
        self.assertEqual(repo_data.name, TestProjects.project_name + "/" + self.repo_name)

        #5. Get and verify artifacts by tag;
        artifact = self.artifact.get_reference_info(TestProjects.project_name, self.repo_name, self.tag, **TestProjects.USER_CLIENT)
        self.assertEqual(artifact.tags[0].name, self.tag)

        #6. ORAS CLI pull artifacts index by tag;
        md5_list_pull = library.oras.oras_pull(harbor_server, user_name, user_001_password, TestProjects.project_name, self.repo_name, self.tag)

        #7. Verfiy MD5 between artifacts pushed by ORAS and artifacts pulled by ORAS;
        if set(md5_list_push) != set(md5_list_pull):
            raise Exception(r"MD5 check failed with {} and {}.".format(str(md5_list_push), str(md5_list_pull)))
class TestProjects(unittest.TestCase):
    @classmethod
    def setUp(self):
        self.project = Project()
        self.user = User()
        self.artifact = Artifact()
        self.repo = Repository()

    @classmethod
    def tearDown(self):
        print("Case completed")

    @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.")
    def test_ClearData(self):
        # remove the deletion as the signed image cannot be deleted.
        #1. Delete repository(RA) by user(UA);
        #self.repo.delete_repoitory(TestProjects.project_sign_image_name, TestProjects.repo_name.split('/')[1], **TestProjects.USER_sign_image_CLIENT)

        #2. Delete project(PA);
        #self.project.delete_project(TestProjects.project_sign_image_id, **TestProjects.USER_sign_image_CLIENT)

        #3. Delete user(UA);
        self.user.delete_user(TestProjects.user_sign_image_id, **ADMIN_CLIENT)

    def testPushImageWithSpecialName(self):
        """
        Test case:
            Push Image With Special Name
        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 = "******"

        #1. Create user-001
        TestProjects.user_sign_image_id, user_sign_image_name = self.user.create_user(
            user_password=user_001_password, **ADMIN_CLIENT)

        TestProjects.USER_sign_image_CLIENT = dict(
            with_signature=True,
            endpoint=url,
            username=user_sign_image_name,
            password=user_001_password)

        #2. Create a new private project(PA) by user(UA);
        TestProjects.project_sign_image_id, TestProjects.project_sign_image_name = self.project.create_project(
            metadata={"public": "false"}, **ADMIN_CLIENT)

        #3. Add user(UA) as a member of project(PA) with project-admin role;
        self.project.add_project_members(
            TestProjects.project_sign_image_id,
            user_id=TestProjects.user_sign_image_id,
            **ADMIN_CLIENT)

        #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)

        image = "hello-world"
        src_tag = "latest"
        profix = "aaa/bbb"

        #5. Create a new repository(RA) and tag(TA) in project(PA) by user(UA);
        TestProjects.repo_name, tag = push_image_to_project(
            TestProjects.project_sign_image_name,
            harbor_server,
            user_sign_image_name,
            user_001_password,
            image,
            src_tag,
            profix_for_image=profix)

        #7. Get signature of image with tag(TA), it should be exist.
        full_name = urllib.parse.quote(profix + "/" + image, 'utf-8')

        artifact = self.artifact.get_reference_info(
            TestProjects.project_sign_image_name, full_name, tag,
            **TestProjects.USER_sign_image_CLIENT)
        self.assertEqual(artifact[0].type, 'IMAGE')
Exemple #3
0
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
    """
    @suppress_urllib3_warning
    def setUp(self):
        self.user = User()
        self.system = System()
        self.repo = Repository()
        self.project = Project()
        self.retention = Retention()
        self.artifact = Artifact()
        self.repo_name_1 = "test1"
        self.repo_name_2 = "test2"

    @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.")
    def tearDown(self):
        #TODO delete_repository will fail when no tags left anymore
        resp = self.repo.list_repositories(TestProjects.project_src_repo_name,
                                           **TestProjects.USER_RA_CLIENT)
        for repo in resp:
            self.repo.delete_repository(TestProjects.project_src_repo_name,
                                        repo.name.split('/')[1],
                                        **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)
        print("Case completed")

    def testTagRetention(self):
        user_ra_password = "******"
        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)

        TestProjects.project_src_repo_id, TestProjects.project_src_repo_name = self.project.create_project(
            metadata={"public": "false"}, **TestProjects.USER_RA_CLIENT)

        # Push image test1:1.0, test1:2.0, test1:3.0,latest, test2:1.0, test2:latest, test3:1.0
        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'])
        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'])
        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'])

        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)

        tags = list_image_tags(
            harbor_server,
            TestProjects.project_src_repo_name + "/" + self.repo_name_1,
            user_ra_name, user_ra_password)
        #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)
        tags = list_image_tags(
            harbor_server,
            TestProjects.project_src_repo_name + "/" + self.repo_name_1,
            user_ra_name, user_ra_password)

        resp = self.repo.list_repositories(TestProjects.project_src_repo_name,
                                           **TestProjects.USER_RA_CLIENT)
        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)
        time.sleep(10)
        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)
        #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:-----------------------------")

        # 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)

        #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.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.digest)
Exemple #4
0
class TestProjects(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.project= Project()
        self.user= User()
        self.artifact = Artifact()
        self.repo= Repository()
        self.url = ADMIN_CLIENT["endpoint"]
        self.user_push_chart_password = "******"
        self.chart_file = "https://storage.googleapis.com/harbor-builds/helm-chart-test-files/harbor-0.2.0.tgz"
        self.archive = "harbor/"
        self.verion = "0.2.0"
        self.repo_name = "harbor_api_test"

    @classmethod
    def tearDownClass(self):
        print "Case completed"

    @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.")
    def test_ClearData(self):
        #1. Delete repository chart(CA) by user(UA);
        self.repo.delete_repoitory(TestProjects.project_push_chart_name, self.repo_name, **TestProjects.USER_CLIENT)

        #2. Delete project(PA);
        self.project.delete_project(TestProjects.project_push_chart_id, **TestProjects.USER_CLIENT)

        #3. Delete user(UA).
        self.user.delete_user(TestProjects.user_id, **ADMIN_CLIENT)

    def testPushChartByHelmChartCLI(self):
        """
        Test case:
            Push Chart File By Helm Chart CLI
        Test step and expected result:
            1. Create a new user(UA);
            2. Create a new project(PA) by user(UA);
            3. Push an chart(CA) to Harbor by helm3 registry/chart CLI successfully;
            4. List artifacts successfully;
            5. Get chart(CA) by reference successfully;
            6. Get addtion successfully;
            7. Delete chart by reference successfully.
        Tear down:
            1. Delete repository chart(CA) by user(UA);
            2. Delete project(PA);
            3. Delete user(UA).
        """
        #1. Create a new user(UA);
        TestProjects.user_id, user_name = self.user.create_user(user_password = self.user_push_chart_password, **ADMIN_CLIENT)
        TestProjects.USER_CLIENT=dict(endpoint = self.url, username = user_name, password = self.user_push_chart_password)

        #2. Create a new project(PA) by user(UA);
        TestProjects.project_push_chart_id, TestProjects.project_push_chart_name = self.project.create_project(metadata = {"public": "false"}, **TestProjects.USER_CLIENT)

        #3. Push an chart(CA) to Harbor by helm3 registry/chart CLI successfully;
        chart_cli_ret = library.helm.helm_chart_push_to_harbor(self.chart_file, self.archive,  harbor_server, TestProjects.project_push_chart_name, self.repo_name, self.verion, user_name, self.user_push_chart_password)
        print "chart_cli_ret:", chart_cli_ret

        #4. List artifacts successfully;
        artifacts = self.artifact.list_artifacts(TestProjects.project_push_chart_name, self.repo_name, **TestProjects.USER_CLIENT)
        self.assertEqual(artifacts[0].type, 'CHART')
        self.assertEqual(artifacts[0].tags[0].name, self.verion)

        #5. Get chart(CA) by reference successfully;
        artifact = self.artifact.get_reference_info(TestProjects.project_push_chart_name, self.repo_name, self.verion, **TestProjects.USER_CLIENT)
        self.assertEqual(artifact[0].type, 'CHART')
        self.assertEqual(artifact[0].tags[0].name, self.verion)

        #6. Get addtion successfully;
        addition_r = self.artifact.get_addition(TestProjects.project_push_chart_name, self.repo_name, self.verion, "readme.md", **TestProjects.USER_CLIENT)
        self.assertIn("Helm Chart for Harbor", addition_r[0])
        addition_d = self.artifact.get_addition(TestProjects.project_push_chart_name, self.repo_name, self.verion, "dependencies", **TestProjects.USER_CLIENT)
        self.assertIn("https://kubernetes-charts.storage.googleapis.com", addition_d[0])
        addition_v = self.artifact.get_addition(TestProjects.project_push_chart_name, self.repo_name, self.verion, "values.yaml", **TestProjects.USER_CLIENT)
        self.assertIn("adminserver", addition_v[0])

        #7. Delete chart by reference successfully.
        self.artifact.delete_artifact(TestProjects.project_push_chart_name, self.repo_name, self.verion, **TestProjects.USER_CLIENT)
Exemple #5
0
class TestProjects(unittest.TestCase):
    @suppress_urllib3_warning
    def setUp(self):
        self.project = Project()
        self.user = User()
        self.artifact = Artifact()
        self.repo = Repository()
        self.repo_name_1 = "test1_sign"

    @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.")
    def tearDown(self):
        # remove the deletion as the signed image cannot be deleted.
        #1. Delete repository(RA) by user(UA);
        #self.repo.delete_repoitory(TestProjects.project_sign_image_name, TestProjects.repo_name.split('/')[1], **TestProjects.USER_sign_image_CLIENT)

        #2. Delete project(PA);
        #self.project.delete_project(TestProjects.project_sign_image_id, **TestProjects.USER_sign_image_CLIENT)

        #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 = "******"

        #1. Create user-001
        TestProjects.user_sign_image_id, user_sign_image_name = self.user.create_user(
            user_password=user_001_password, **ADMIN_CLIENT)

        TestProjects.USER_sign_image_CLIENT = dict(
            with_signature=True,
            endpoint=url,
            username=user_sign_image_name,
            password=user_001_password)

        #2. Create a new private project(PA) by user(UA);
        TestProjects.project_sign_image_id, TestProjects.project_sign_image_name = self.project.create_project(
            metadata={"public": "false"}, **ADMIN_CLIENT)

        #3. Add user(UA) as a member of project(PA) with project-admin role;
        self.project.add_project_members(
            TestProjects.project_sign_image_id,
            user_id=TestProjects.user_sign_image_id,
            **ADMIN_CLIENT)

        #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)

        #Note:busybox is pulled in setup phase, and setup is a essential phase.
        image = "busybox"
        tag = "latest"
        #5. Create a new repository(RA) and tag(TA) in project(PA) by user(UA);
        #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)

        #6. Sign image with tag(TA) which was tagged by step #5;
        sign_image(harbor_server, TestProjects.project_sign_image_name, image,
                   tag)

        #7. Get signature of image with tag(TA), it should be exist.
        artifact = self.artifact.get_reference_info(
            TestProjects.project_sign_image_name, image, tag,
            **TestProjects.USER_sign_image_CLIENT)
        self.assertEqual(artifact.tags[0].signed, True)

        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'])
        self.repo.delete_repoitory(TestProjects.project_sign_image_name,
                                   self.repo_name_1,
                                   **TestProjects.USER_sign_image_CLIENT)

        self.repo.delete_repoitory(
            TestProjects.project_sign_image_name,
            image,
            expect_status_code=412,
            expect_response_body="with signature cannot be deleted",
            **TestProjects.USER_sign_image_CLIENT)
Exemple #6
0
class TestProjects(unittest.TestCase):
    @suppress_urllib3_warning
    def setUp(self):
        self.project= Project()
        self.user= User()
        self.artifact = Artifact()
        self.repo= Repository()
        self.url = ADMIN_CLIENT["endpoint"]
        self.user_password = "******"
        self.repo_name = "hello-world"

    @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.")
    def tearDown(self):
        #1. Delete repository(RA,IA) by user(UA);
        self.repo.delete_repoitory(TestProjects.project_name, self.repo_name, **TestProjects.USER_CLIENT)

        #2. Delete project(PA);
        self.project.delete_project(TestProjects.project_id, **TestProjects.USER_CLIENT)

        #3. Delete user(UA).
        self.user.delete_user(TestProjects.user_id, **ADMIN_CLIENT)
        print("Case completed")

    def testCreateDeleteTag(self):
        """
        Test case:
            Create/Delete tag
        Test step and expected result:
            1. Create a new user(UA);
            2. Create a new project(PA) by user(UA);
            3. Push an image(IA) to Harbor by docker successfully;
            4. Create a tag(1.0) for the image(IA);
            5. Get the image(latest) from Harbor successfully;
            6. Verify the image(IA) contains tag named 1.0;
            7. Delete the tag(1.0) from image(IA);
            8. Get the image(IA) from Harbor successfully;
            9. Verify the image(IA) contains no tag named 1.0;
        Tear down:
            1. Delete repository(RA,IA) by user(UA);
            2. Delete project(PA);
            3. Delete user(UA).
        """
        #1. Create a new user(UA);
        TestProjects.user_id, user_name = self.user.create_user(user_password = self.user_password, **ADMIN_CLIENT)

        TestProjects.USER_CLIENT=dict(with_tag = True, endpoint = self.url, username = user_name, password = self.user_password)

        #2. Create a new project(PA) by user(UA);
        TestProjects.project_id, TestProjects.project_name = self.project.create_project(metadata = {"public": "false"}, **TestProjects.USER_CLIENT)

        #3. Push an image(IA) to Harbor by docker successfully;
        repo_name, tag = push_self_build_image_to_project(TestProjects.project_name, harbor_server, 'admin', 'Harbor12345', self.repo_name, "latest")

        #4. Create a tag(1.0) for the image(IA)
        self.artifact.create_tag(TestProjects.project_name, self.repo_name, tag, "1.0",**TestProjects.USER_CLIENT)

        #5. Get the image(IA) from Harbor successfully;
        artifact = self.artifact.get_reference_info(TestProjects.project_name, self.repo_name, tag, **TestProjects.USER_CLIENT)

        #6. Verify the image(IA) contains tag named 1.0;
        self.assertEqual(artifact.tags[0].name, "1.0")
        self.assertEqual(artifact.tags[1].name, tag)

        #7. Delete the tag(1.0) from image(IA);
        self.artifact.delete_tag(TestProjects.project_name, self.repo_name, tag, "1.0",**TestProjects.USER_CLIENT)

        #8. Get the image(latest) from Harbor successfully;
        artifact = self.artifact.get_reference_info(TestProjects.project_name, self.repo_name, tag, **TestProjects.USER_CLIENT)

        #9. Verify the image(IA) contains no tag named 1.0;
        self.assertEqual(artifact.tags[0].name, tag)
class TestProjects(unittest.TestCase):
    @suppress_urllib3_warning
    def setUp(self):
        self.project = Project()
        self.user = User()
        self.replication = Replication()
        self.replication_v2 = ReplicationV2()
        self.registry = Registry()
        self.artifact = Artifact()
        self.repo = Repository()
        self.image = "alpine"
        self.tag = "latest"

    @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.")
    def tearDown(self):
        #1. Delete rule(RA);
        self.replication.delete_replication_rule(TestProjects.rule_id,
                                                 **ADMIN_CLIENT)

        #2. Delete registry(TA);
        self.registry.delete_registry(TestProjects.registry_id, **ADMIN_CLIENT)

        #1. Delete repository(RA);
        self.repo.delete_repoitory(TestProjects.project_name, self.image,
                                   **TestProjects.USER_add_rule_CLIENT)

        #3. Delete project(PA);
        self.project.delete_project(TestProjects.project_add_rule_id,
                                    **TestProjects.USER_add_rule_CLIENT)

        #4. Delete user(UA);
        self.user.delete_user(TestProjects.user_add_rule_id, **ADMIN_CLIENT)

    def testReplicationFromDockerhub(self):
        """
        Test case:
            Replication From Dockerhub
        Test step and expected result:
            1. Create a new user(UA);
            2. Create a new private project(PA) by user(UA);
            3. Create a new registry;
            4. Create a new rule for this registry;
            5. Check rule should be exist;
            6. Trigger the rule;
            7. Wait for completion of this replication job;
            8. Check image is replicated into target project successfully.
        Tear down:
            1. Delete rule(RA);
            2. Delete registry(TA);
            3. Delete project(PA);
            4. Delete user(UA).
        """
        url = ADMIN_CLIENT["endpoint"]
        user_add_rule_password = "******"

        #1. Create user(UA)
        TestProjects.user_add_rule_id, user_add_rule_name = self.user.create_user(
            user_password=user_add_rule_password, **ADMIN_CLIENT)

        TestProjects.USER_add_rule_CLIENT = dict(
            endpoint=url,
            username=user_add_rule_name,
            password=user_add_rule_password)

        #2.1. Create private project(PA) by user(UA)
        TestProjects.project_add_rule_id, TestProjects.project_name = self.project.create_project(
            metadata={"public": "false"}, **TestProjects.USER_add_rule_CLIENT)

        #2.2. Get private project of uesr-001, uesr-001 can see only one private project which is project-001
        self.project.projects_should_exist(
            dict(public=False),
            expected_count=1,
            expected_project_id=TestProjects.project_add_rule_id,
            **TestProjects.USER_add_rule_CLIENT)

        #3. Create a new registry;
        TestProjects.registry_id, _ = self.registry.create_registry(
            "https://hub.docker.com",
            registry_type="docker-hub",
            access_key=DOCKER_USER,
            access_secret=DOCKER_PWD,
            insecure=False,
            **ADMIN_CLIENT)

        #4. Create a pull-based rule for this registry;
        TestProjects.rule_id, rule_name = self.replication.create_replication_policy(
            src_registry=swagger_client.Registry(
                id=int(TestProjects.registry_id)),
            dest_namespace=TestProjects.project_name,
            filters=[
                swagger_client.ReplicationFilter(type="name",
                                                 value="library/" +
                                                 self.image),
                swagger_client.ReplicationFilter(type="tag", value=self.tag)
            ],
            **ADMIN_CLIENT)

        #5. Check rule should be exist;
        self.replication.check_replication_rule_should_exist(
            TestProjects.rule_id, rule_name, **ADMIN_CLIENT)

        #6. Trigger the rule;
        self.replication_v2.trigger_replication_executions(
            TestProjects.rule_id, **ADMIN_CLIENT)

        #7. Wait for completion of this replication job;
        self.replication_v2.wait_until_jobs_finish(TestProjects.rule_id,
                                                   interval=30,
                                                   **ADMIN_CLIENT)

        #8. Check image is replicated into target project successfully.
        artifact = self.artifact.get_reference_info(TestProjects.project_name,
                                                    self.image, self.tag,
                                                    **ADMIN_CLIENT)
class TestProjects(unittest.TestCase):

    user_id = None
    project_push_chart_id = None
    USER_CLIENT = None
    project_push_chart_name = None

    @suppress_urllib3_warning
    def setUp(self):
        self.project = Project()
        self.user = User()
        self.artifact = Artifact()
        self.repo = Repository()
        self.url = ADMIN_CLIENT["endpoint"]
        self.user_push_chart_password = "******"
        self.chart_file_name = "harbor-helm-1.7.3"
        self.chart_file_package_name = "harbor-1.7.3.tgz"
        self.chart_file_path = files_directory + "harbor-helm-1.7.3.tar.gz"
        self.version = "1.7.3"
        self.repo_name = "harbor"

    @unittest.skipIf(TEARDOWN is False, "Test data won't be erased.")
    def tearDown(self):
        # 1. Delete repository chart(CA) by user(UA);
        self.repo.delete_repository(TestProjects.project_push_chart_name,
                                    self.repo_name, **TestProjects.USER_CLIENT)

        # 2. Delete project(PA);
        self.project.delete_project(TestProjects.project_push_chart_id,
                                    **TestProjects.USER_CLIENT)

        # 3. Delete user(UA).
        self.user.delete_user(TestProjects.user_id, **ADMIN_CLIENT)

    def testPushChartByHelmChartCLI(self):
        """
        Test case:
            Push Chart File By Helm3.7 CLI
        Test step and expected result:
            1. Create a new user(UA);
            2. Create a new project(PA) by user(UA);
            3. Push an chart(CA) to Harbor by helm3.7 CLI successfully;
            4. List artifacts successfully;
            5. Get chart(CA) by reference successfully;
            6. Get addition successfully;
            7. Delete chart by reference successfully.
        Tear down:
            1. Delete repository chart(CA) by user(UA);
            2. Delete project(PA);
            3. Delete user(UA).
        """
        # 1. Create a new user(UA);
        TestProjects.user_id, user_name = self.user.create_user(
            user_password=self.user_push_chart_password, **ADMIN_CLIENT)
        TestProjects.USER_CLIENT = dict(endpoint=self.url,
                                        username=user_name,
                                        password=self.user_push_chart_password)

        # 2. Create a new project(PA) by user(UA);
        TestProjects.project_push_chart_id, TestProjects.project_push_chart_name = self.project.create_project(
            metadata={"public": "false"}, **TestProjects.USER_CLIENT)

        # 3 Push an chart(CA) to Harbor by helm3.7 CLI successfully;
        command = ["tar", "zxf", self.chart_file_path]
        base.run_command(command)
        # 3.1 helm3_7_registry_login;
        helm.helm3_7_registry_login(ip=harbor_server,
                                    user=user_name,
                                    password=self.user_push_chart_password)
        # 3.2 helm3_7_package;
        helm.helm3_7_package(file_path=self.chart_file_name)
        # 3.2 helm3_7_push;
        helm.helm3_7_push(file_path=self.chart_file_package_name,
                          ip=harbor_server,
                          project_name=TestProjects.project_push_chart_name)

        # 4. List artifacts successfully;
        artifacts = self.artifact.list_artifacts(
            TestProjects.project_push_chart_name, self.repo_name,
            **TestProjects.USER_CLIENT)
        self.assertEqual(artifacts[0].type, 'CHART')
        self.assertEqual(artifacts[0].tags[0].name, self.version)

        # 5.1 Get chart(CA) by reference successfully;
        artifact = self.artifact.get_reference_info(
            TestProjects.project_push_chart_name, self.repo_name, self.version,
            **TestProjects.USER_CLIENT)
        self.assertEqual(artifact.type, 'CHART')
        self.assertEqual(artifact.tags[0].name, self.version)

        # 6. Get addition successfully;
        addition_r = self.artifact.get_addition(
            TestProjects.project_push_chart_name, self.repo_name, self.version,
            "readme.md", **TestProjects.USER_CLIENT)
        self.assertIn("Helm Chart for Harbor", addition_r[0])
        addition_v = self.artifact.get_addition(
            TestProjects.project_push_chart_name, self.repo_name, self.version,
            "values.yaml", **TestProjects.USER_CLIENT)
        self.assertIn("expose", addition_v[0])

        # 7. Delete chart by reference successfully.
        self.artifact.delete_artifact(TestProjects.project_push_chart_name,
                                      self.repo_name, self.version,
                                      **TestProjects.USER_CLIENT)
class TestTagImmutability(unittest.TestCase):
    @suppress_urllib3_warning
    def setUp(self):
        self.url = ADMIN_CLIENT["endpoint"]
        self.user_password = "******"
        self.project= Project()
        self.user= User()
        self.repo= Repository()
        self.registry = Registry()
        self.artifact = Artifact()
        self.tag_immutability = Tag_Immutability()
        self.project_id, self.project_name, self.user_id, self.user_name = [None] * 4
        self.user_id, self.user_name = self.user.create_user(user_password = self.user_password, **ADMIN_CLIENT)
        self.USER_CLIENT = dict(with_signature = True, with_immutable_status = True, endpoint = self.url, username = self.user_name, password = self.user_password)
        self.exsiting_rule = dict(selector_repository="rel*", selector_tag="v2.*")
        self.project_id, self.project_name = self.project.create_project(metadata = {"public": "false"}, **self.USER_CLIENT)

    @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.")
    def tearDown(self):
        print("Case completed")

    def check_tag_immutability(self, artifact, tag_name, status = True):
        for tag in artifact.tags:
            if tag.name == tag_name:
                self.assertTrue(tag.immutable == status)
                return
        raise Exception("No tag {} found in artifact {}".format(tag, artifact))

    def test_disability_of_rules(self):
        """
        Test case:
            Test Disability Of Rules
        Test step and expected result:
            1. Create a new project;
            2. Push image A to the project with 2 tags A and B;
            3. Create a disabled rule matched image A with tag A;
            4. Both tags of image A should not be immutable;
            5. Enable this rule;
            6. image A with tag A should be immutable.
        """
        image_a = dict(name="image_disability_a", tag1="latest", tag2="6.2.2")

        #1. Create a new project;
        project_id, project_name = self.project.create_project(metadata = {"public": "false"}, **self.USER_CLIENT)

        #2. Push image A to the project with 2 tags;
        push_special_image_to_project(project_name, harbor_server, self.user_name, self.user_password, image_a["name"], [image_a["tag1"], image_a["tag2"]])

        #3. Create a disabled rule matched image A;
        rule_id = self.tag_immutability.create_rule(project_id, disabled = True, selector_repository=image_a["name"], selector_tag=str(image_a["tag1"])[0:2] + "*", **self.USER_CLIENT)

        #4. Both tags of image A should not be immutable;
        artifact_a = self.artifact.get_reference_info(project_name, image_a["name"], image_a["tag2"], **self.USER_CLIENT)
        print("[test_disability_of_rules] - artifact:{}".format(artifact_a))
        self.assertTrue(artifact_a)
        self.check_tag_immutability(artifact_a, image_a["tag1"], status = False)
        self.check_tag_immutability(artifact_a, image_a["tag2"], status = False)

        #5. Enable this rule;
        self.tag_immutability.update_tag_immutability_policy_rule(project_id, rule_id, disabled = False, **self.USER_CLIENT)

        #6. image A with tag A should be immutable.
        artifact_a = self.artifact.get_reference_info(project_name, image_a["name"], image_a["tag2"], **self.USER_CLIENT)
        print("[test_disability_of_rules] - artifact:{}".format(artifact_a))
        self.assertTrue(artifact_a)
        self.check_tag_immutability(artifact_a, image_a["tag1"], status = True)
        self.check_tag_immutability(artifact_a, image_a["tag2"], status = False)

    def test_artifact_and_repo_is_undeletable(self):
        """
        Test case:
            Test Artifact And Repo is Undeleteable
        Test step and expected result:
            1. Create a new project;
            2. Push image A to the project with 2 tags A and B;
            3. Create a enabled rule matched image A with tag A;
            4. Tag A should be immutable;
            5. Artifact is undeletable;
            6. Repository is undeletable.
        """
        image_a = dict(name="image_repo_undeletable_a", tag1="latest", tag2="1.3.2")

        #1. Create a new project;
        project_id, project_name = self.project.create_project(metadata = {"public": "false"}, **self.USER_CLIENT)

        #2. Push image A to the project with 2 tags A and B;
        push_special_image_to_project(project_name, harbor_server, self.user_name, self.user_password, image_a["name"], [image_a["tag1"], image_a["tag2"]])

        #3. Create a enabled rule matched image A with tag A;
        self.tag_immutability.create_rule(project_id, selector_repository=image_a["name"], selector_tag=str(image_a["tag1"])[0:2] + "*", **self.USER_CLIENT)

        #4. Tag A should be immutable;
        artifact_a = self.artifact.get_reference_info(project_name, image_a["name"], image_a["tag2"], **self.USER_CLIENT)
        print("[test_artifact_and_repo_is_undeletable] - artifact:{}".format(artifact_a))
        self.assertTrue(artifact_a)
        self.check_tag_immutability(artifact_a, image_a["tag1"], status = True)
        self.check_tag_immutability(artifact_a, image_a["tag2"], status = False)

        #5. Artifact is undeletable;
        self.artifact.delete_artifact(project_name, image_a["name"], image_a["tag1"], expect_status_code = 412,expect_response_body = "configured as immutable, cannot be deleted", **self.USER_CLIENT)

        #6. Repository is undeletable.
        self.repo.delete_repository(project_name, image_a["name"], expect_status_code = 412, expect_response_body = "configured as immutable, cannot be deleted", **self.USER_CLIENT)

    def test_tag_is_undeletable(self):
        """
        Test case:
            Test Tag is Undeleteable
        Test step and expected result:
            1. Push image A to the project with 2 tags A and B;
            2. Create a enabled rule matched image A with tag A;
            3. Tag A should be immutable;
            4. Tag A is undeletable;
            5. Tag B is deletable.
        """
        image_a = dict(name="image_undeletable_a", tag1="latest", tag2="9.3.2")

        #1. Push image A to the project with 2 tags A and B;
        push_special_image_to_project(self.project_name, harbor_server, self.user_name, self.user_password, image_a["name"], [image_a["tag1"], image_a["tag2"]])

        #2. Create a enabled rule matched image A with tag A;
        self.tag_immutability.create_rule(self.project_id, selector_repository=image_a["name"], selector_tag=str(image_a["tag2"])[0:2] + "*", **self.USER_CLIENT)

        #3. Tag A should be immutable;
        artifact_a = self.artifact.get_reference_info(self.project_name, image_a["name"], image_a["tag2"], **self.USER_CLIENT)
        print("[test_tag_is_undeletable] - artifact:{}".format(artifact_a))
        self.assertTrue(artifact_a)
        self.check_tag_immutability(artifact_a, image_a["tag2"], status = True)

        #4. Tag A is undeletable;
        self.artifact.delete_tag(self.project_name, image_a["name"], image_a["tag1"], image_a["tag2"], expect_status_code = 412, **self.USER_CLIENT)

        #5. Tag B is deletable.
        self.artifact.delete_tag(self.project_name, image_a["name"], image_a["tag1"], image_a["tag1"], **self.USER_CLIENT)

    def test_image_is_unpushable(self):
        """
        Test case:
            Test Image is Unpushable
        Test step and expected result:
            1. Create a new project;
            2. Push image A to the project with 2 tags A and B;
            3. Create a enabled rule matched image A with tag A;
            4. Tag A should be immutable;
            5. Can not push image with the same image name and with the same tag name.
        """
        image_a = dict(name="image_unpushable_a", tag1="latest", tag2="1.3.2")

        #1. Create a new project;
        project_id, project_name = self.project.create_project(metadata = {"public": "false"}, **self.USER_CLIENT)

        #2. Push image A to the project with 2 tags A and B;
        push_special_image_to_project(project_name, harbor_server, self.user_name, self.user_password, image_a["name"], [image_a["tag1"], image_a["tag2"]])

        #3. Create a enabled rule matched image A with tag A;
        self.tag_immutability.create_rule(project_id, selector_repository=image_a["name"], selector_tag=str(image_a["tag1"])[0:2] + "*", **self.USER_CLIENT)

        #4. Tag A should be immutable;
        artifact_a = self.artifact.get_reference_info(project_name, image_a["name"], image_a["tag2"], **self.USER_CLIENT)
        print("[test_image_is_unpushable] - artifact:{}".format(artifact_a))
        self.assertTrue(artifact_a)
        self.check_tag_immutability(artifact_a, image_a["tag1"], status = True)
        self.check_tag_immutability(artifact_a, image_a["tag2"], status = False)

        #5. Can not push image with the same image name and with the same tag name.
        push_special_image_to_project(project_name, harbor_server, self.user_name, self.user_password, image_a["name"], [image_a["tag1"]], size=10
                                      , expected_error_message = "configured as immutable")

    def test_copy_disability(self):
        """
        Test case:
            Test Copy Disability
        Test step and expected result:
            1. Create 2 projects;
            2. Push image A with tag A and B to project A, push image B which has the same image name and tag name to project B;
            3. Create a enabled rule matched image A with tag A;
            4. Tag A should be immutable;
            5. Can not copy artifact from project A to project B with the same repository name.
        """
        image_a = dict(name="image_copy_disability_a", tag1="latest", tag2="1.3.2")

        #1. Create 2 projects;
        project_id, project_name = self.project.create_project(metadata = {"public": "false"}, **self.USER_CLIENT)
        _, project_name_src = self.project.create_project(metadata = {"public": "false"}, **self.USER_CLIENT)

        #2. Push image A with tag A and B to project A, push image B which has the same image name and tag name to project B;
        push_special_image_to_project(project_name, harbor_server, self.user_name, self.user_password, image_a["name"], [image_a["tag1"], image_a["tag2"]])
        push_special_image_to_project(project_name_src, harbor_server, self.user_name, self.user_password, image_a["name"], [image_a["tag1"], image_a["tag2"]])

        #3. Create a enabled rule matched image A with tag A;
        self.tag_immutability.create_rule(project_id, selector_repository=image_a["name"], selector_tag=str(image_a["tag1"])[0:2] + "*", **self.USER_CLIENT)

        #4. Tag A should be immutable;
        artifact_a = self.artifact.get_reference_info(project_name, image_a["name"], image_a["tag2"], **self.USER_CLIENT)
        print("[test_copy_disability] - artifact:{}".format(artifact_a))
        self.assertTrue(artifact_a)
        self.check_tag_immutability(artifact_a, image_a["tag1"], status = True)
        self.check_tag_immutability(artifact_a, image_a["tag2"], status = False)

        #5. Can not copy artifact from project A to project B with the same repository name.
        artifact_a_src = self.artifact.get_reference_info(project_name_src, image_a["name"], image_a["tag2"], **self.USER_CLIENT)
        print("[test_copy_disability] - artifact_a_src:{}".format(artifact_a_src))
        self.artifact.copy_artifact(project_name, image_a["name"], project_name_src+"/"+ image_a["name"] + "@" + artifact_a_src.digest, expect_status_code=412, expect_response_body = "configured as immutable, cannot be updated", **self.USER_CLIENT)

    #def test_replication_disability(self):
    #    pass

    def test_priority_of_rules(self):
        """
        Test case:
            Test Priority Of Rules(excluding rule will not affect matching rule)
        Test step and expected result:
            1. Push image A, B and C, image A has only 1 tag named tag1;
            2. Create a matching rule that matches image A and tag named tag2 which is not exist;
            3. Create a excluding rule to exlude image A and B;
            4. Add a tag named tag2 to image A, tag2 should be immutable;
            5. Tag2 should be immutable;
            6. All tags in image B should be immutable;
            7. All tags in image C should not be immutable;
            8. Disable all rules.
        """
        image_a = dict(name="image_priority_a", tag1="latest", tag2="6.3.2")
        image_b = dict(name="image_priority_b", tag1="latest", tag2="0.12.0")
        image_c = dict(name="image_priority_c", tag1="latest", tag2="3.12.0")

        #1. Push image A, B and C, image A has only 1 tag named tag1;
        push_special_image_to_project(self.project_name, harbor_server, self.user_name, self.user_password, image_a["name"], [image_a["tag1"]])
        push_special_image_to_project(self.project_name, harbor_server, self.user_name, self.user_password, image_b["name"], [image_b["tag1"],image_b["tag2"]])
        push_special_image_to_project(self.project_name, harbor_server, self.user_name, self.user_password, image_c["name"], [image_c["tag1"],image_c["tag2"]])

        #2. Create a matching rule that matches image A and tag named tag2 which is not exist;
        rule_id_1 = self.tag_immutability.create_rule(self.project_id, selector_repository=image_a["name"], selector_tag=image_a["tag2"], **self.USER_CLIENT)

        #3. Create a excluding rule to exlude image A and B;
        rule_id_2 = self.tag_immutability.create_rule(self.project_id, selector_repository_decoration = "repoExcludes",
                                          selector_repository="{image_priority_a,image_priority_b}", selector_tag="**", **self.USER_CLIENT)

        #4. Add a tag named tag2 to image A, tag2 should be immutable;
        self.artifact.create_tag(self.project_name, image_a["name"], image_a["tag1"], image_a["tag2"], **self.USER_CLIENT)

        #5. Tag2 should be immutable;
        artifact_a = self.artifact.get_reference_info(self.project_name, image_a["name"], image_a["tag2"], **self.USER_CLIENT)
        print("[test_priority_of_rules] - artifact:{}".format(artifact_a))
        self.assertTrue(artifact_a)
        self.check_tag_immutability(artifact_a, image_a["tag2"], status = True)
        self.check_tag_immutability(artifact_a, image_a["tag1"], status = False)

        #6. All tags in image B should be immutable;
        artifact_b = self.artifact.get_reference_info(self.project_name, image_b["name"], image_b["tag2"], **self.USER_CLIENT)
        print("[test_priority_of_rules] - artifact:{}".format(artifact_b))
        self.assertTrue(artifact_b)
        self.check_tag_immutability(artifact_b, image_b["tag2"], status = False)
        self.check_tag_immutability(artifact_b, image_b["tag1"], status = False)

        #7. All tags in image C should not be immutable;
        artifact_c = self.artifact.get_reference_info(self.project_name, image_c["name"], image_c["tag2"], **self.USER_CLIENT)
        print("[test_priority_of_rules] - artifact:{}".format(artifact_c))
        self.assertTrue(artifact_c)
        self.check_tag_immutability(artifact_c, image_c["tag2"], status = True)
        self.check_tag_immutability(artifact_c, image_c["tag1"], status = True)

        #8. Disable all rules.
        self.tag_immutability.update_tag_immutability_policy_rule(self.project_id, rule_id_1, disabled = True, **self.USER_CLIENT)
        self.tag_immutability.update_tag_immutability_policy_rule(self.project_id, rule_id_2, disabled = True, **self.USER_CLIENT)

    def test_add_exsiting_rule(self):
        """
        Test case:
            Test Priority Of Rules(excluding rule will not affect matching rule)
        Test step and expected result:
            1. Push image A and B with no tag;
            2. Create a immutability policy rule A;
            3. Fail to create rule B which has the same config as rule A;
        """
        self.tag_immutability.create_tag_immutability_policy_rule(self.project_id, **self.exsiting_rule, **self.USER_CLIENT)
        self.tag_immutability.create_tag_immutability_policy_rule(self.project_id, **self.exsiting_rule, expect_status_code = 409, **self.USER_CLIENT)
class TestProjects(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.project = Project()
        self.user = User()
        self.artifact = Artifact()
        self.repo = Repository()
        self.url = ADMIN_CLIENT["endpoint"]
        self.user_push_index_password = "******"
        self.index_name = "ci_test_index"
        self.index_tag = "test_tag"
        self.image_a = "alpine"
        self.image_b = "busybox"

    @classmethod
    def tearDownClass(self):
        print("Case completed")

    @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.")
    def test_ClearData(self):
        #1. Delete repository(RA,RB,IA) by user(UA);
        self.repo.delete_repoitory(TestProjects.project_push_index_name,
                                   self.index_name, **TestProjects.USER_CLIENT)
        self.repo.delete_repoitory(TestProjects.project_push_index_name,
                                   self.image_a, **TestProjects.USER_CLIENT)
        self.repo.delete_repoitory(TestProjects.project_push_index_name,
                                   self.image_b, **TestProjects.USER_CLIENT)

        #2. Delete project(PA);
        self.project.delete_project(TestProjects.project_push_index_id,
                                    **TestProjects.USER_CLIENT)

        #3. Delete user(UA).
        self.user.delete_user(TestProjects.user_id, **ADMIN_CLIENT)

    def testAddIndexByDockerManifest(self):
        """
        Test case:
            Push Index By Docker Manifest
        Test step and expected result:
            1. Create a new user(UA);
            2. Create a new project(PA) by user(UA);
            3. Create 2 new repositorys(RA,RB) in project(PA) by user(UA);
            4. Push an index(IA) to Harbor by docker manifest CLI successfully;
            5. Get Artifacts successfully;
            6. Get index(IA) by reference successfully;
            7. Verify harbor index is index(IA) pushed by docker manifest CLI;
            8.1 Verify harbor index(IA) can be pulled by docker CLI successfully;
            8.2 Verify harbor index(IA) can be pulled by docker CLI successfully;
            9. Get addition successfully;
            10. Unable to Delete artifact in manifest list;
            11. Delete index successfully.
        Tear down:
            1. Delete repository(RA,RB,IA) by user(UA);
            2. Delete project(PA);
            3. Delete user(UA).
        """
        #1. Create a new user(UA);
        TestProjects.user_id, user_name = self.user.create_user(
            user_password=self.user_push_index_password, **ADMIN_CLIENT)

        TestProjects.USER_CLIENT = dict(endpoint=self.url,
                                        username=user_name,
                                        password=self.user_push_index_password)

        #2. Create a new project(PA) by user(UA);
        TestProjects.project_push_index_id, TestProjects.project_push_index_name = self.project.create_project(
            metadata={"public": "false"}, **TestProjects.USER_CLIENT)

        #3. Create 2 new repositorys(RA,RB) in project(PA) by user(UA);
        repo_name_a, tag_a = push_image_to_project(
            TestProjects.project_push_index_name, harbor_server, 'admin',
            'Harbor12345', self.image_a, "latest")
        repo_name_b, tag_b = push_image_to_project(
            TestProjects.project_push_index_name, harbor_server, 'admin',
            'Harbor12345', self.image_b, "latest")

        #4. Push an index(IA) to Harbor by docker manifest CLI successfully;
        manifests = [
            harbor_server + "/" + repo_name_a + ":" + tag_a,
            harbor_server + "/" + repo_name_b + ":" + tag_b
        ]
        index = harbor_server + "/" + TestProjects.project_push_index_name + "/" + self.index_name + ":" + self.index_tag
        index_sha256_cli_ret, manifests_sha256_cli_ret = library.docker_api.docker_manifest_push_to_harbor(
            index, manifests, harbor_server, user_name,
            self.user_push_index_password)

        #5. Get Artifacts successfully;
        artifacts = self.artifact.list_artifacts(
            TestProjects.project_push_index_name, self.index_name,
            **TestProjects.USER_CLIENT)
        artifacts_ref_child_list = [
            artifacts[0].references[1].child_digest,
            artifacts[0].references[0].child_digest
        ]
        self.assertEqual(
            artifacts_ref_child_list.count(manifests_sha256_cli_ret[0]), 1)
        self.assertEqual(
            artifacts_ref_child_list.count(manifests_sha256_cli_ret[1]), 1)

        #6. Get index(IA) by reference successfully;
        index_data = self.artifact.get_reference_info(
            TestProjects.project_push_index_name, self.index_name,
            self.index_tag, **TestProjects.USER_CLIENT)
        manifests_sha256_harbor_ret = [
            index_data[0].references[1].child_digest,
            index_data[0].references[0].child_digest
        ]

        #7. Verify harbor index is index(IA) pushed by docker manifest CLI;
        self.assertEqual(index_data[0].digest, index_sha256_cli_ret)
        self.assertEqual(
            manifests_sha256_harbor_ret.count(manifests_sha256_cli_ret[0]), 1)
        self.assertEqual(
            manifests_sha256_harbor_ret.count(manifests_sha256_cli_ret[1]), 1)

        #8.1 Verify harbor index(IA) can be pulled by docker CLI successfully;
        pull_harbor_image(
            harbor_server, user_name, self.user_push_index_password,
            TestProjects.project_push_index_name + "/" + self.index_name,
            self.index_tag)

        #8.2 Verify harbor index(IA) can be pulled by ctr successfully;
        oci_ref = harbor_server + "/" + TestProjects.project_push_index_name + "/" + self.index_name + ":" + self.index_tag
        library.containerd.ctr_images_pull(user_name,
                                           self.user_push_index_password,
                                           oci_ref)
        library.containerd.ctr_images_list(oci_ref=oci_ref)

        #9. Get addition successfully;
        addition_v = self.artifact.get_addition(
            TestProjects.project_push_index_name, self.index_name,
            self.index_tag, "vulnerabilities", **TestProjects.USER_CLIENT)
        self.assertEqual(addition_v[0], '{}')
        #This artifact has no build history

        addition_v = self.artifact.get_addition(
            TestProjects.project_push_index_name, self.index_name,
            manifests_sha256_cli_ret[0], "vulnerabilities",
            **TestProjects.USER_CLIENT)
        self.assertEqual(addition_v[0], '{}')
        addition_b = self.artifact.get_addition(
            TestProjects.project_push_index_name, self.index_name,
            manifests_sha256_cli_ret[0], "build_history",
            **TestProjects.USER_CLIENT)
        self.assertIn("ADD file:", addition_b[0])
        image_data = self.artifact.get_reference_info(
            TestProjects.project_push_index_name, self.index_name,
            manifests_sha256_cli_ret[0], **TestProjects.USER_CLIENT)

        addition_v = self.artifact.get_addition(
            TestProjects.project_push_index_name, self.index_name,
            manifests_sha256_cli_ret[1], "vulnerabilities",
            **TestProjects.USER_CLIENT)
        self.assertEqual(addition_v[0], '{}')
        addition_b = self.artifact.get_addition(
            TestProjects.project_push_index_name, self.index_name,
            manifests_sha256_cli_ret[1], "build_history",
            **TestProjects.USER_CLIENT)
        self.assertIn("ADD file:", addition_b[0])
        image_data = self.artifact.get_reference_info(
            TestProjects.project_push_index_name, self.index_name,
            manifests_sha256_cli_ret[0], **TestProjects.USER_CLIENT)

        #10. Unable to Delete artifact in manifest list;
        self.artifact.delete_artifact(TestProjects.project_push_index_name,
                                      self.index_name,
                                      manifests_sha256_cli_ret[0],
                                      expect_status_code=412,
                                      **TestProjects.USER_CLIENT)

        #11. Delete index successfully.
        self.artifact.delete_artifact(TestProjects.project_push_index_name,
                                      self.index_name, self.index_tag,
                                      **TestProjects.USER_CLIENT)
Exemple #11
0
class TestProjects(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.project= Project()
        self.user= User()
        self.artifact = Artifact()
        self.repo= Repository()
        self.url = ADMIN_CLIENT["endpoint"]
        self.user_push_cnab_password = "******"
        self.cnab_repo_name = "test_cnab"
        self.cnab_tag = "test_cnab_tag"

    @classmethod
    def tearDownClass(self):
        print("Case completed")

    @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.")
    def test_ClearData(self):
        #1. Delete repository(RA) by user(UA);
        self.repo.delete_repoitory(TestProjects.project_push_bundle_name, self.cnab_repo_name, **TestProjects.USER_CLIENT)

        #2. Delete project(PA);
        self.project.delete_project(TestProjects.project_push_bundle_id, **TestProjects.USER_CLIENT)

        #3. Delete user(UA).
        self.user.delete_user(TestProjects.user_id, **ADMIN_CLIENT)

    def testPushBundleByCnab(self):
        """
        Test case:
            Push Bundle By Cnab
        Test step and expected result:
            1. Create a new user(UA);
            2. Create a new project(PA) by user(UA);
            3. Pull images for bundle;
            4. Push bundle to harbor as repository(RA);
            5. Get repository from Harbor successfully;
            6. Verfiy bundle name;
            7. Get artifact by sha256;
            8. Verify artifact information.
        Tear down:
            1. Delete repository(RA) by user(UA);
            2. Delete project(PA);
            3. Delete user(UA).
        """
        #1. Create a new user(UA);
        TestProjects.user_id, user_name = self.user.create_user(user_password = self.user_push_cnab_password, **ADMIN_CLIENT)
        TestProjects.USER_CLIENT=dict(endpoint = self.url, username = user_name, password = self.user_push_cnab_password)


        #2. Create a new project(PA) by user(UA);
        TestProjects.project_push_bundle_id, TestProjects.project_push_bundle_name = self.project.create_project(metadata = {"public": "false"}, **TestProjects.USER_CLIENT)

        #3. Pull images for bundle;
        _docker_api = DockerAPI()
        _docker_api.docker_image_pull("alpine", tag = "latest")
        _docker_api.docker_image_pull("haproxy", tag = "latest")

        #4. Push bundle to harbor as repository(RA);
        target = harbor_server + "/" + TestProjects.project_push_bundle_name  + "/" + self.cnab_repo_name  + ":" + self.cnab_tag
        reference_sha256 = library.cnab.push_cnab_bundle(harbor_server, user_name, self.user_push_cnab_password, "alpine:latest", "haproxy:latest", target)

        #5. Get repository from Harbor successfully;
        index_data = self.repo.get_repository(TestProjects.project_push_bundle_name, self.cnab_repo_name, **TestProjects.USER_CLIENT)

        #5.2 Cnab bundle can be pulled by ctr successfully;
        # This step might not successful since ctr does't support cnab fully, it might be uncomment sometime in future.
        # Please keep them in comment!
        #library.containerd.ctr_images_pull(user_name, self.user_push_cnab_password, target)
        #library.containerd.ctr_images_list(oci_ref = target)

        #6. Verfiy bundle name;
        self.assertEqual(index_data.name, TestProjects.project_push_bundle_name + "/" + self.cnab_repo_name)

        #7. Get artifact by sha256;
        artifact = self.artifact.get_reference_info(TestProjects.project_push_bundle_name, self.cnab_repo_name, reference_sha256, **TestProjects.USER_CLIENT)

        #8. Verify artifact information;
        self.assertEqual(artifact[0].type, 'CNAB')
        self.assertEqual(artifact[0].digest, reference_sha256)
class TestProjects(unittest.TestCase):
    @classmethod
    def setUp(self):
        self.project = Project()
        self.user = User()
        self.artifact = Artifact()
        self.repo = Repository()
        self.repo_name = "busybox"
        self.tag = "1.28"

    @classmethod
    def tearDown(self):
        print("Case completed")

    @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.")
    def test_ClearData(self):
        #1. Delete user(UA);
        self.user.delete_user(TestProjects.user_sign_image_id, **ADMIN_CLIENT)

    def testPushSingularity(self):
        """
        Test case:
            Push Singularity file With Singularity CLI
        Test step and expected result:
            1. Create user-001
            2. Create a new private project(PA) by user(UA);
            3. Push a sif file to harbor by singularity;
            4. Get repository from Harbor successfully, and verfiy repository name is repo pushed by singularity CLI;
            5. Get and verify artifacts by tag;
            6. Pull sif file from harbor by singularity;
        Tear down:
            NA
        """
        url = ADMIN_CLIENT["endpoint"]
        user_001_password = "******"

        #1. Create user-001
        TestProjects.user_sign_image_id, user_name = self.user.create_user(
            user_password=user_001_password, **ADMIN_CLIENT)

        TestProjects.USER_CLIENT = dict(with_signature=True,
                                        endpoint=url,
                                        username=user_name,
                                        password=user_001_password)

        #2. Create a new private project(PA) by user(UA);
        TestProjects.project_id, TestProjects.project_name = self.project.create_project(
            metadata={"public": "false"}, **TestProjects.USER_CLIENT)

        #3. Push a sif file to harbor by singularity;
        library.singularity.push_singularity_to_harbor(
            "library:", "library/default/", harbor_server, user_name,
            user_001_password, TestProjects.project_name, self.repo_name,
            self.tag)

        #4. Get repository from Harbor successfully, and verfiy repository name is repo pushed by singularity CLI;
        repo_data = self.repo.get_repository(TestProjects.project_name,
                                             self.repo_name,
                                             **TestProjects.USER_CLIENT)
        self.assertEqual(repo_data.name,
                         TestProjects.project_name + "/" + self.repo_name)

        #5. Get and verify artifacts by tag;
        artifact = self.artifact.get_reference_info(TestProjects.project_name,
                                                    self.repo_name, self.tag,
                                                    **TestProjects.USER_CLIENT)
        self.assertEqual(artifact[0].tags[0].name, self.tag)

        #6. Pull sif file from harbor by singularity;
        library.singularity.singularity_pull(
            TestProjects.project_name + ".sif",
            "oras://" + harbor_server + "/" + TestProjects.project_name + "/" +
            self.repo_name + ":" + self.tag)
Exemple #13
0
class TestProjects(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.project = Project()
        self.user = User()
        self.artifact = Artifact(api_type='artifact')
        self.repo = Repository(api_type='repository')
        self.url = ADMIN_CLIENT["endpoint"]
        self.user_push_chart_password = "******"
        self.chart_file = "https://storage.googleapis.com/harbor-builds/helm-chart-test-files/harbor-0.2.0.tgz"
        self.archive = "harbor/"
        self.verion = "0.2.0"
        self.repo_name = "harbor_api_test"

    @classmethod
    def tearDownClass(self):
        print "Case completed"

    @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.")
    def test_ClearData(self):
        #1. Delete repository chart(CA) by user(UA);
        self.repo.delete_repoitory(TestProjects.project_push_chart_name,
                                   self.repo_name, **TestProjects.USER_CLIENT)

        #2. Delete project(PA);
        self.project.delete_project(TestProjects.project_push_chart_id,
                                    **TestProjects.USER_CLIENT)

        #3. Delete user(UA).
        self.user.delete_user(TestProjects.user_id, **ADMIN_CLIENT)

    def testPushChartByHelmChartCLI(self):
        """
        Test case:
            Push Chart File By Helm Chart CLI
        Test step and expected result:
            1. Create a new user(UA);
            2. Create a new project(PA) by user(UA);
            3. Push an chart(CA) to Harbor by helm3 registry/chart CLI successfully;
            4. Get chart(CA) from Harbor successfully;
            5. TO_DO: Verify this chart artifact information, like digest.
        Tear down:
            1. Delete repository chart(CA) by user(UA);
            2. Delete project(PA);
            3. Delete user(UA).
        """
        #1. Create a new user(UA);
        TestProjects.user_id, user_name = self.user.create_user(
            user_password=self.user_push_chart_password, **ADMIN_CLIENT)
        TestProjects.USER_CLIENT = dict(endpoint=self.url,
                                        username=user_name,
                                        password=self.user_push_chart_password)

        #2. Create a new project(PA) by user(UA);
        TestProjects.project_push_chart_id, TestProjects.project_push_chart_name = self.project.create_project(
            metadata={"public": "false"}, **TestProjects.USER_CLIENT)

        #3. Push an chart(CA) to Harbor by helm3 registry/chart CLI successfully;
        chart_cli_ret = library.helm.helm_chart_push_to_harbor(
            self.chart_file, self.archive, harbor_server,
            TestProjects.project_push_chart_name, self.repo_name, self.verion,
            user_name, self.user_push_chart_password)
        print "chart_cli_ret:", chart_cli_ret

        #4. Get chart(CA) from Harbor successfully;
        artifact = self.artifact.get_reference_info(
            TestProjects.project_push_chart_name, self.repo_name, self.verion,
            **TestProjects.USER_CLIENT)
        print "artifact:", artifact

        #5. TO_DO: Verify this chart artifact information, like digest;
        self.assertEqual(artifact[0].type, 'CHART')
        self.assertEqual(artifact[0].tags[0].name, self.verion)
Exemple #14
0
class TestProjects(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.project = Project()
        self.user = User()
        self.artifact = Artifact(api_type='artifact')
        self.repo = Repository(api_type='repository')
        self.url = ADMIN_CLIENT["endpoint"]
        self.user_push_index_password = "******"
        self.index_name = "ci_test_index"
        self.index_tag = "test_tag"
        self.image_a = "alpine"
        self.image_b = "busybox"

    @classmethod
    def tearDownClass(self):
        print "Case completed"

    @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.")
    def test_ClearData(self):
        #1. Delete repository(RA,RB,IA) by user(UA);
        self.repo.delete_repoitory(TestProjects.project_push_index_name,
                                   self.index_name, **TestProjects.USER_CLIENT)
        self.repo.delete_repoitory(TestProjects.project_push_index_name,
                                   self.image_a, **TestProjects.USER_CLIENT)
        self.repo.delete_repoitory(TestProjects.project_push_index_name,
                                   self.image_b, **TestProjects.USER_CLIENT)

        #2. Delete project(PA);
        self.project.delete_project(TestProjects.project_push_index_id,
                                    **TestProjects.USER_CLIENT)

        #3. Delete user(UA).
        self.user.delete_user(TestProjects.user_id, **ADMIN_CLIENT)

    def testAddIndexByDockerManifest(self):
        """
        Test case:
            Push Index By Docker Manifest
        Test step and expected result:
            1. Create a new user(UA);
            2. Create a new project(PA) by user(UA);
            3. Create 2 new repositorys(RA,RB) in project(PA) by user(UA);
            4. Push an index(IA) to Harbor by docker manifest CLI successfully;
            5. Get index(IA) from Harbor successfully;
            6. Verify harbor index is index(IA) pushed by docker manifest CLI;
            7. Verify harbor index(IA) can be pulled by docker CLI successfully.
        Tear down:
            1. Delete repository(RA,RB,IA) by user(UA);
            2. Delete project(PA);
            3. Delete user(UA).
        """
        #1. Create a new user(UA);
        TestProjects.user_id, user_name = self.user.create_user(
            user_password=self.user_push_index_password, **ADMIN_CLIENT)

        TestProjects.USER_CLIENT = dict(endpoint=self.url,
                                        username=user_name,
                                        password=self.user_push_index_password)

        #2. Create a new project(PA) by user(UA);
        TestProjects.project_push_index_id, TestProjects.project_push_index_name = self.project.create_project(
            metadata={"public": "false"}, **TestProjects.USER_CLIENT)

        #3. Create 2 new repositorys(RA,RB) in project(PA) by user(UA);
        repo_name_a, tag_a = push_image_to_project(
            TestProjects.project_push_index_name, harbor_server, 'admin',
            'Harbor12345', self.image_a, "latest")
        repo_name_b, tag_b = push_image_to_project(
            TestProjects.project_push_index_name, harbor_server, 'admin',
            'Harbor12345', self.image_b, "latest")

        #4. Push an index(IA) to Harbor by docker manifest CLI successfully;
        manifests = [
            harbor_server + "/" + repo_name_a + ":" + tag_a,
            harbor_server + "/" + repo_name_b + ":" + tag_b
        ]
        index = harbor_server + "/" + TestProjects.project_push_index_name + "/" + self.index_name + ":" + self.index_tag
        index_sha256_cli_ret, manifests_sha256_cli_ret = library.docker_api.docker_manifest_push_to_harbor(
            index, manifests, harbor_server, user_name,
            self.user_push_index_password)

        #5. Get index(IA) from Harbor successfully;
        index_data = self.artifact.get_reference_info(
            TestProjects.project_push_index_name, self.index_name,
            self.index_tag, **TestProjects.USER_CLIENT)
        manifests_sha256_harbor_ret = [
            index_data[0].references[1].child_digest,
            index_data[0].references[0].child_digest
        ]

        #6. Verify harbor index is index(IA) pushed by docker manifest CLI;
        self.assertEqual(index_data[0].digest, index_sha256_cli_ret)
        self.assertEqual(
            manifests_sha256_harbor_ret.count(manifests_sha256_cli_ret[0]), 1)
        self.assertEqual(
            manifests_sha256_harbor_ret.count(manifests_sha256_cli_ret[1]), 1)

        #7. Verify harbor index(IA) can be pulled by docker CLI successfully;
        pull_harbor_image(
            harbor_server, user_name, self.user_push_index_password,
            TestProjects.project_push_index_name + "/" + self.index_name,
            self.index_tag)
Exemple #15
0
class TestProjects(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.project = Project()
        self.user = User()
        self.artifact = Artifact(api_type='artifact')
        self.repo = Repository(api_type='repository')

    @classmethod
    def tearDownClass(self):
        print "Case completed"

    @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.")
    def test_ClearData(self):
        #1. Delete repository(RA);
        self.repo.delete_repoitory(TestProjects.project_src_repo_name,
                                   (TestProjects.src_repo_name).split('/')[1],
                                   **TestProjects.USER_RETAG_CLIENT)

        #2. Delete repository by retag;
        self.repo.delete_repoitory(TestProjects.project_dst_repo_name,
                                   (TestProjects.dst_repo_name).split('/')[1],
                                   **TestProjects.USER_RETAG_CLIENT)

        #3. Delete project(PA);
        self.project.delete_project(TestProjects.project_src_repo_id,
                                    **TestProjects.USER_RETAG_CLIENT)
        self.project.delete_project(TestProjects.project_dst_repo_id,
                                    **TestProjects.USER_RETAG_CLIENT)

        #4. Delete user(UA).
        self.user.delete_user(TestProjects.user_retag_id, **ADMIN_CLIENT)

    def testRetag(self):
        """
        Test case:
            Retag Image
        Test step and expected result:
            1. Create a new user(UA);
            2. Create a new project(PA) by user(UA);
            3. Create a new project(PB) by user(UA);
            4. Update role of user-retag as guest member of project(PB);
            5. Create a new repository(RA) in project(PA) by user(UA);
            6. Get repository in project(PA), there should be one repository which was created by user(UA);
            7. Get repository(RA)'s image tag detail information;
            8. Retag image in project(PA) to project(PB), it should be forbidden;
            9. Update role of user-retag as admin member of project(PB);
            10. Retag image in project(PA) to project(PB), it should be successful;
            11. Get repository(RB)'s image tag detail information;
            12. Read digest of retaged image, it must be the same with the image in repository(RA);
            13. Pull image from project(PB) by user_retag, it must be successful;
        Tear down:
            1. Delete repository(RA);
            2. Delete repository by retag;
            3. Delete project(PA);
            4. Delete user(UA).
        """
        url = ADMIN_CLIENT["endpoint"]
        user_retag_password = "******"
        pull_tag_name = "latest"
        dst_repo_sub_name = "repo"

        #1. Create a new user(UA);
        TestProjects.user_retag_id, user_retag_name = self.user.create_user(
            user_password=user_retag_password, **ADMIN_CLIENT)

        TestProjects.USER_RETAG_CLIENT = dict(endpoint=url,
                                              username=user_retag_name,
                                              password=user_retag_password)

        #2. Create a new project(PA) by user(UA);
        TestProjects.project_src_repo_id, TestProjects.project_src_repo_name = self.project.create_project(
            metadata={"public": "false"}, **TestProjects.USER_RETAG_CLIENT)

        #3. Create a new project(PB) by user(UA);
        TestProjects.project_dst_repo_id, TestProjects.project_dst_repo_name = self.project.create_project(
            metadata={"public": "false"}, **TestProjects.USER_RETAG_CLIENT)

        retag_member_id = self.project.get_project_member_id(
            TestProjects.project_dst_repo_id, user_retag_name,
            **TestProjects.USER_RETAG_CLIENT)

        #4. Update role of user-retag as guest member of project(PB);
        self.project.update_project_member_role(
            TestProjects.project_dst_repo_id, retag_member_id, 3,
            **ADMIN_CLIENT)

        #5. Create a new repository(RA) in project(PA) by user(UA);
        TestProjects.src_repo_name, tag_name = push_image_to_project(
            TestProjects.project_src_repo_name, harbor_server, 'admin',
            'Harbor12345', "hello-world", pull_tag_name)

        #6. Get repository in project(PA), there should be one repository which was created by user(UA);
        src_repo_data = self.repo.get_repository(
            TestProjects.project_src_repo_name,
            **TestProjects.USER_RETAG_CLIENT)
        _assert_status_code(TestProjects.src_repo_name, src_repo_data[0].name)

        #7. Get repository(RA)'s image tag detail information;
        src_tag_data = self.artifact.get_reference_info(
            TestProjects.project_src_repo_name,
            TestProjects.src_repo_name.split('/')[1], tag_name,
            **TestProjects.USER_RETAG_CLIENT)
        TestProjects.dst_repo_name = TestProjects.project_dst_repo_name + "/" + dst_repo_sub_name
        #8. Retag image in project(PA) to project(PB), it should be forbidden;
        self.artifact.copy_artifact(TestProjects.project_dst_repo_name,
                                    dst_repo_sub_name,
                                    TestProjects.src_repo_name + "@" +
                                    src_tag_data[0].digest,
                                    expect_status_code=403,
                                    **TestProjects.USER_RETAG_CLIENT)

        #9. Update role of user-retag as admin member of project(PB);
        self.project.update_project_member_role(
            TestProjects.project_dst_repo_id, retag_member_id, 1,
            **ADMIN_CLIENT)

        #10. Retag image in project(PA) to project(PB), it should be successful;
        self.artifact.copy_artifact(
            TestProjects.project_dst_repo_name, dst_repo_sub_name,
            TestProjects.src_repo_name + "@" + src_tag_data[0].digest,
            **TestProjects.USER_RETAG_CLIENT)

        #11. Get repository(RB)'s image tag detail information;
        dst_tag_data = self.artifact.get_reference_info(
            TestProjects.project_dst_repo_name, dst_repo_sub_name, tag_name,
            **TestProjects.USER_RETAG_CLIENT)

        #12. Read digest of retaged image, it must be the same with the image in repository(RA);
        self.assertEqual(src_tag_data[0].digest, dst_tag_data[0].digest)

        #13. Pull image from project(PB) by user_retag, it must be successful;"
        pull_harbor_image(harbor_server, user_retag_name, user_retag_password,
                          TestProjects.dst_repo_name, tag_name)
Exemple #16
0
class TestProjects(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.project = Project()
        self.user = User()
        self.artifact = Artifact(api_type='artifact')
        self.repo = Repository(api_type='repository')
        self.url = ADMIN_CLIENT["endpoint"]
        self.user_push_chart_password = "******"
        self.cnab_repo_name = "test_cnab"

    @classmethod
    def tearDownClass(self):
        print "Case completed"

    @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.")
    def test_ClearData(self):
        #1. Delete repository(RA) by user(UA);
        self.repo.delete_repoitory(TestProjects.project_push_bundle_name,
                                   self.cnab_repo_name,
                                   **TestProjects.USER_CLIENT)

        #2. Delete project(PA);
        self.project.delete_project(TestProjects.project_push_bundle_id,
                                    **TestProjects.USER_CLIENT)

        #3. Delete user(UA).
        self.user.delete_user(TestProjects.user_id, **ADMIN_CLIENT)

    def testPushBundleByCnab(self):
        """
        Test case:
            Push Bundle By Cnab
        Test step and expected result:
            1. Create a new user(UA);
            2. Create a new project(PA) by user(UA);
            3. Pull images for bundle;
            4. Push bundle to harbor as repository(RA);
            5. Get repository from Harbor successfully;
            6. Verfiy bundle name;
            7. Get artifact by sha256;
            8. Verify artifact information.
        Tear down:
            1. Delete repository(RA) by user(UA);
            2. Delete project(PA);
            3. Delete user(UA).
        """
        #1. Create a new user(UA);
        TestProjects.user_id, user_name = self.user.create_user(
            user_password=self.user_push_chart_password, **ADMIN_CLIENT)
        TestProjects.USER_CLIENT = dict(endpoint=self.url,
                                        username=user_name,
                                        password=self.user_push_chart_password)

        #2. Create a new project(PA) by user(UA);
        TestProjects.project_push_bundle_id, TestProjects.project_push_bundle_name = self.project.create_project(
            metadata={"public": "false"}, **TestProjects.USER_CLIENT)

        #3. Pull images for bundle;
        _docker_api = DockerAPI()
        _docker_api.docker_image_pull("hello-world", tag="latest")
        _docker_api.docker_image_pull("busybox", tag="latest")

        #4. Push bundle to harbor as repository(RA);
        target = harbor_server + "/" + TestProjects.project_push_bundle_name + "/" + self.cnab_repo_name
        reference_sha256 = library.cnab.push_cnab_bundle(
            harbor_server, user_name, self.user_push_chart_password,
            "hello-world:latest", "busybox:latest", target)

        #5. Get repository from Harbor successfully;
        index_data = self.repo.get_repository(
            TestProjects.project_push_bundle_name, self.cnab_repo_name,
            **TestProjects.USER_CLIENT)
        print "index_data:", index_data

        #6. Verfiy bundle name;
        self.assertEqual(
            index_data.name,
            TestProjects.project_push_bundle_name + "/" + self.cnab_repo_name)

        #7. Get artifact by sha256;
        artifact = self.artifact.get_reference_info(
            TestProjects.project_push_bundle_name, self.cnab_repo_name,
            reference_sha256, **TestProjects.USER_CLIENT)

        #8. Verify artifact information;
        self.assertEqual(artifact[0].type, 'CNAB')
        self.assertEqual(artifact[0].digest, reference_sha256)
Exemple #17
0
class TestProjects(unittest.TestCase):
    @suppress_urllib3_warning
    def setUp(self):
        self.project = Project()
        self.user = User()
        self.artifact = Artifact()
        self.repo = Repository()

    @unittest.skipIf(TEARDOWN == True, "Test data won't be erased.")
    def tearDown(self):
        #1. Delete repository(RA) by user(UA);
        self.repo.delete_repository(TestProjects.project_content_trust_name,
                                    TestProjects.repo_name.split('/')[1],
                                    **TestProjects.USER_CONTENT_TRUST_CLIENT)

        #2. Delete project(PA);
        self.project.delete_project(TestProjects.project_content_trust_id,
                                    **TestProjects.USER_CONTENT_TRUST_CLIENT)

        #3. Delete user(UA);
        self.user.delete_user(TestProjects.user_content_trust_id,
                              **ADMIN_CLIENT)

    def testProjectLevelPolicyContentTrust(self):
        """
        Test case:
            Project Level Policy Content Trust
        Test step and expected result:
            1. Create a new user(UA);
            2. Create a new project(PA) by user(UA);
            3. Push a new image(IA) in project(PA) by admin;
            4. Image(IA) should exist;
            5. Pull image(IA) successfully;
            6. Enable content trust in project(PA) configuration;
            7. Pull image(IA) failed and the reason is "The image is not signed in Notary".
        Tear down:
            1. Delete repository(RA) by user(UA);
            2. Delete project(PA);
            3. Delete user(UA);
        """
        url = ADMIN_CLIENT["endpoint"]
        image = "test_content_trust"
        user_content_trust_password = "******"

        #1. Create a new user(UA);
        TestProjects.user_content_trust_id, user_content_trust_name = self.user.create_user(
            user_password=user_content_trust_password, **ADMIN_CLIENT)

        TestProjects.USER_CONTENT_TRUST_CLIENT = dict(
            endpoint=url,
            username=user_content_trust_name,
            password=user_content_trust_password)

        #2. Create a new project(PA) by user(UA);
        TestProjects.project_content_trust_id, TestProjects.project_content_trust_name = self.project.create_project(
            metadata={"public": "false"},
            **TestProjects.USER_CONTENT_TRUST_CLIENT)

        #3. Push a new image(IA) in project(PA) by admin;
        TestProjects.repo_name, tag = push_self_build_image_to_project(
            TestProjects.project_content_trust_name, harbor_server,
            ADMIN_CLIENT["username"], ADMIN_CLIENT["password"], image,
            "latest")

        #4. Image(IA) should exist;
        artifact = self.artifact.get_reference_info(
            TestProjects.project_content_trust_name, image, tag,
            **TestProjects.USER_CONTENT_TRUST_CLIENT)
        self.assertEqual(artifact.tags[0].name, tag)

        docker_image_clean_all()
        #5. Pull image(IA) successfully;
        pull_harbor_image(harbor_server, ADMIN_CLIENT["username"],
                          ADMIN_CLIENT["password"], TestProjects.repo_name,
                          tag)

        self.project.get_project(TestProjects.project_content_trust_id)
        #6. Enable content trust in project(PA) configuration;
        self.project.update_project(TestProjects.project_content_trust_id,
                                    metadata={"enable_content_trust": "true"},
                                    **TestProjects.USER_CONTENT_TRUST_CLIENT)
        self.project.get_project(TestProjects.project_content_trust_id)

        #7. Pull image(IA) failed and the reason is "The image is not signed in Notary".
        docker_image_clean_all()
        pull_harbor_image(
            harbor_server,
            ADMIN_CLIENT["username"],
            ADMIN_CLIENT["password"],
            TestProjects.repo_name,
            tag,
            expected_error_message="The image is not signed in Notary")
class TestCosign(unittest.TestCase):
    @suppress_urllib3_warning
    def setUp(self):
        self.project = Project()
        self.user = User()
        self.artifact = Artifact()
        self.repo = Repository()
        self.image = "alpine"
        self.tag = "latest"
        self.expect_accessory_type = "signature.cosign"

    @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.")
    def tearDown(self):
        #1. Delete repository by user(UA);
        self.repo.delete_repository(TestCosign.project_name, self.image,
                                    **TestCosign.user_client)
        #2. Delete project(PA);
        self.project.delete_project(TestCosign.project_id,
                                    **TestCosign.user_client)
        #3. Delete user(UA).
        self.user.delete_user(TestCosign.user_id, **ADMIN_CLIENT)

    def testCosignArtifact(self):
        """
        Test case:
            Cosign Artifact API
        Test step and expected result:
            1. Create a new user(UA);
            2. Create a new project(PA) by user(UA);
            3. Push a new image(IA) in project(PA) by user(UA);
            4. Verify that the image (IA) is not signed by cosign;
            5. Sign image(IA) with cosign;
            6. Verify that the image (IA) is signed by cosign;
        Tear down:
            1. Delete project(PA);
            2. Delete user(UA).
        """
        url = ADMIN_CLIENT["endpoint"]
        user_password = "******"

        # 1. Create user(UA)
        TestCosign.user_id, user_name = self.user.create_user(
            user_password=user_password, **ADMIN_CLIENT)
        TestCosign.user_client = dict(endpoint=url,
                                      username=user_name,
                                      password=user_password,
                                      with_accessory=True)

        # 2.1. Create private project(PA) by user(UA)
        TestCosign.project_id, TestCosign.project_name = self.project.create_project(
            metadata={"public": "false"}, **TestCosign.user_client)
        # 2.2. Get private project of uesr-001, uesr-001 can see only one private project which is project-001
        self.project.projects_should_exist(
            dict(public=False),
            expected_count=1,
            expected_project_id=TestCosign.project_id,
            **TestCosign.user_client)

        # 3. Push a new image(IA) in project(PA) by user(UA)
        TestCosign.repo_name, tag = push_self_build_image_to_project(
            TestCosign.project_name, harbor_server, user_name, user_password,
            self.image, self.tag)

        # 4.1. Verify list_artifacts API;
        artifact_list = self.artifact.list_artifacts(TestCosign.project_name,
                                                     self.image,
                                                     **TestCosign.user_client)
        first_artifact = artifact_list[0]
        artifact_reference = first_artifact.digest
        self.assertTrue(len(artifact_list) == 1)
        self.assertIsNone(artifact_list[0].accessories)
        # 4.2. Verify get_reference_info API;
        artifact_info = self.artifact.get_reference_info(
            TestCosign.project_name, self.image, artifact_reference,
            **TestCosign.user_client)
        self.assertIsNone(artifact_info.accessories)
        # 4.3. Verify list_accessories API;
        accessory_list = self.artifact.list_accessories(
            TestCosign.project_name, self.image, artifact_reference,
            **TestCosign.user_client)
        self.assertTrue(len(accessory_list) == 0)

        # 5.1. Generate cosign key pair;
        cosign.generate_key_pair()
        # 5.2. Generate cosign key pair;
        docker_api.docker_login_cmd(harbor_server,
                                    user_name,
                                    user_password,
                                    enable_manifest=False)
        cosign.sign_artifact("{}/{}/{}:{}".format(harbor_server,
                                                  TestCosign.project_name,
                                                  self.image, self.tag))

        # 6.1. Verify list_artifacts API;
        artifact_list = self.artifact.list_artifacts(TestCosign.project_name,
                                                     self.image,
                                                     **TestCosign.user_client)
        self.assertTrue(len(artifact_list) == 1)
        first_artifact = artifact_list[0]
        self.assertTrue(len(first_artifact.accessories) == 1)
        first_accessory = first_artifact.accessories[0]
        self.assertEqual(first_accessory.type, self.expect_accessory_type)
        accessory_reference = first_accessory.digest
        # 6.2. Verify get_reference_info API;
        artifact_info = self.artifact.get_reference_info(
            TestCosign.project_name, self.image, artifact_reference,
            **TestCosign.user_client)
        self.assertEqual(artifact_info.accessories[0].type,
                         self.expect_accessory_type)
        # 6.3. Verify list_accessories API;
        accessory_list = self.artifact.list_accessories(
            TestCosign.project_name, self.image, artifact_reference,
            **TestCosign.user_client)
        self.assertTrue(len(accessory_list) == 1)
        self.assertEqual(accessory_list[0].type, self.expect_accessory_type)
        # 6.4. Verify list_accessories API;
        accessory_info = self.artifact.get_reference_info(
            TestCosign.project_name, self.image, accessory_reference,
            **TestCosign.user_client)
        self.assertEqual(accessory_info.digest, accessory_reference)