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