From 61d74b9da16ed788cfc0577b6cfb1d58ef4f90cd Mon Sep 17 00:00:00 2001
From: Yang Jiao <72076317+YangJiao0817@users.noreply.github.com>
Date: Tue, 6 Jun 2023 10:45:28 +0800
Subject: [PATCH] Add podman pull & push testcase (#18790)

Fix #18788

Signed-off-by: Yang Jiao <jiaoya@vmware.com>
---
 tests/apitests/python/library/podman.py       | 18 +++++
 .../apitests/python/test_podman_pull_push.py  | 76 +++++++++++++++++++
 tests/apitests/python/test_referrers_api.py   |  3 -
 tests/robot-cases/Group0-BAT/API_DB.robot     |  4 +
 tests/test-engine-image/Dockerfile.api_test   |  4 +-
 5 files changed, 101 insertions(+), 4 deletions(-)
 create mode 100644 tests/apitests/python/library/podman.py
 create mode 100644 tests/apitests/python/test_podman_pull_push.py

diff --git a/tests/apitests/python/library/podman.py b/tests/apitests/python/library/podman.py
new file mode 100644
index 000000000..c8d54dcdc
--- /dev/null
+++ b/tests/apitests/python/library/podman.py
@@ -0,0 +1,18 @@
+# -*- coding: utf-8 -*-
+import base
+
+def login(registry, username, password):
+    command = ["podman", "login", "-u", username, "-p", password, registry]
+    base.run_command(command)
+
+def logout(registry):
+    command = ["podman", "logout", registry]
+    base.run_command(command)
+
+def pull(artifact):
+    command = ["podman", "pull", artifact]
+    base.run_command(command)
+
+def push(source_artifact, target_artifact):
+    command = ["podman", "push", source_artifact, target_artifact]
+    base.run_command(command)
diff --git a/tests/apitests/python/test_podman_pull_push.py b/tests/apitests/python/test_podman_pull_push.py
new file mode 100644
index 000000000..dbabe17f1
--- /dev/null
+++ b/tests/apitests/python/test_podman_pull_push.py
@@ -0,0 +1,76 @@
+# -*- coding: utf-8 -*-
+
+from __future__ import absolute_import
+import unittest
+
+from testutils import harbor_server, ADMIN_CLIENT, suppress_urllib3_warning
+from library import podman
+from library.project import Project
+from library.user import User
+from library.artifact import Artifact
+from library.repository import push_self_build_image_to_project
+
+class TestPodmanPullPush(unittest.TestCase):
+
+    @suppress_urllib3_warning
+    def setUp(self):
+        self.project= Project()
+        self.user= User()
+        self.artifact = Artifact()
+        self.image = "image_test"
+        self.tag = "v1"
+        self.source_image = "ghcr.io/goharbor/harbor-core"
+        self.source_tag = "v2.8.2"
+
+    def testPodman(self):
+        """
+        Test case:
+            Podman pull and push
+        Test step and expected result:
+            1. Create a new user;
+            2. Create a new project by user;
+            3. Push a new image in project by user;
+            4. Podman login harbor;
+            5. Podman pull image from project(PA) by user;
+            6. Podman pull soure image;
+            7. Podman push soure image to project by user;
+            8. Verify the image;
+            9. Podman logout harbor;
+        """
+        url = ADMIN_CLIENT["endpoint"]
+        user_password = "Aa123456"
+
+        # 1. Create user(UA)
+        _, user_name = self.user.create_user(user_password = user_password, **ADMIN_CLIENT)
+        user_client = dict(endpoint = url, username = user_name, password = user_password, with_accessory = True)
+
+        # 2. Create private project(PA) by user(UA)
+        _, project_name = self.project.create_project(metadata = {"public": "false"}, **user_client)
+
+        # 3. Push a new image(IA) in project(PA) by user(UA)
+        push_self_build_image_to_project(project_name, harbor_server, user_name, user_password, self.image, self.tag)
+
+        # 4. Podman login harbor
+        podman.login(harbor_server, user_name, user_password)
+
+        # 5. Podman pull image from project(PA) by user
+        podman.pull("{}/{}/{}:{}".format(harbor_server, project_name, self.image, self.tag))
+
+        # 6. Podman pull soure image
+        podman.pull("{}:{}".format(self.source_image, self.source_tag))
+
+        # 7. Podman push soure image to project by user
+        podman.push("{}:{}".format(self.source_image, self.source_tag), "{}/{}/{}:{}".format(harbor_server, project_name, self.image, self.tag))
+
+        # 8. Verify the image
+        image_info = self.artifact.get_reference_info(project_name, self.image, self.tag, **user_client)
+        self.assertIsNotNone(image_info)
+        self.assertIsNotNone(image_info.digest)
+        self.assertEqual(len(image_info.tags), 1)
+        self.assertEqual(image_info.tags[0].name, self.tag)
+
+        # 9. Podman logout harbor
+        podman.logout(harbor_server)
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/tests/apitests/python/test_referrers_api.py b/tests/apitests/python/test_referrers_api.py
index f1bd69f92..a62f1dbfc 100644
--- a/tests/apitests/python/test_referrers_api.py
+++ b/tests/apitests/python/test_referrers_api.py
@@ -37,9 +37,6 @@ class TestReferrersApi(unittest.TestCase):
             6. Sign image(IA) SBOM with cosign;
             7. Call the referrers api successfully;
             8. Call the referrers api and filter artifact_type;
-        Tear down:
-            1. Delete project(PA);
-            2. Delete user(UA).
         """
         url = ADMIN_CLIENT["endpoint"]
         user_password = "Aa123456"
diff --git a/tests/robot-cases/Group0-BAT/API_DB.robot b/tests/robot-cases/Group0-BAT/API_DB.robot
index ed9da3957..c94bbfd9b 100644
--- a/tests/robot-cases/Group0-BAT/API_DB.robot
+++ b/tests/robot-cases/Group0-BAT/API_DB.robot
@@ -186,3 +186,7 @@ Test Case - Retain Image Last Pull Time
 Test Case - Referrers API
     [Tags]  referrers
     Harbor API Test  ./tests/apitests/python/test_referrers_api.py
+
+Test Case - Podman Pull And Push To Harbor
+    [Tags]  podman_pull_push
+    Harbor API Test  ./tests/apitests/python/test_podman_pull_push.py
diff --git a/tests/test-engine-image/Dockerfile.api_test b/tests/test-engine-image/Dockerfile.api_test
index ca8b39aee..d786e6cd4 100644
--- a/tests/test-engine-image/Dockerfile.api_test
+++ b/tests/test-engine-image/Dockerfile.api_test
@@ -8,7 +8,7 @@ ENV COSIGN_OCI_EXPERIMENTAL=1
 
 COPY --from=tool_builder /tool/tools.tar.gz /usr/local/bin
 
-RUN tdnf install -y \
+RUN tdnf update -y && tdnf install -y \
     wget \
     git \
     openjdk8 \
@@ -45,4 +45,6 @@ RUN chmod +x /usr/local/bin/dockerd-entrypoint.sh && \
     echo Harbor12345 > password.ca && \
     certutil -d sql:$HOME/.pki/nssdb -N -f password.ca
 
+RUN tdnf install -y podman
+
 VOLUME /var/lib/docker