From 2016d32497111f656da4596c0d066c5c8a467828 Mon Sep 17 00:00:00 2001
From: Yang Jiao <72076317+YangJiao0817@users.noreply.github.com>
Date: Fri, 19 May 2023 14:18:21 +0800
Subject: [PATCH] Add Retain image last pull time API test case (#18689)

Fix #18618

Signed-off-by: Yang Jiao <jiaoya@vmware.com>
---
 .../apitests/python/library/configurations.py |  7 ++
 tests/apitests/python/test_p2p.py             |  1 -
 .../test_retain_image_last_pull_time.py       | 75 +++++++++++++++++++
 tests/robot-cases/Group0-BAT/API_DB.robot     |  4 +
 4 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100644 tests/apitests/python/test_retain_image_last_pull_time.py

diff --git a/tests/apitests/python/library/configurations.py b/tests/apitests/python/library/configurations.py
index 7d2ba8fba..fd82f93c5 100644
--- a/tests/apitests/python/library/configurations.py
+++ b/tests/apitests/python/library/configurations.py
@@ -28,6 +28,8 @@ def set_configurations(client, expect_status_code = 200, expect_response_body =
         conf["audit_log_forward_endpoint"] = config.get("audit_log_forward_endpoint")
     if "skip_audit_log_database" in config and config.get("skip_audit_log_database") is not None:
         conf["skip_audit_log_database"] = config.get("skip_audit_log_database")
+    if "scanner_skip_update_pulltime" in config and config.get("scanner_skip_update_pulltime") is not None:
+        conf["scanner_skip_update_pulltime"] = config.get("scanner_skip_update_pulltime")
 
     try:
         _, status_code, _ = client.update_configurations_with_http_info(conf)
@@ -88,3 +90,8 @@ class Configurations(base.Base, object):
         client = self._get_client(**kwargs)
         config=dict(audit_log_forward_endpoint=audit_log_forward_endpoint, skip_audit_log_database=skip_audit_log_database)
         set_configurations(client, expect_status_code = expect_status_code, **config)
+
+    def set_configurations_of_retain_image_last_pull_time(self, is_skip, expect_status_code = 200, **kwargs):
+        client = self._get_client(**kwargs)
+        config=dict(scanner_skip_update_pulltime=is_skip)
+        set_configurations(client, expect_status_code = expect_status_code, **config)
diff --git a/tests/apitests/python/test_p2p.py b/tests/apitests/python/test_p2p.py
index e9756b777..c1d6168b8 100644
--- a/tests/apitests/python/test_p2p.py
+++ b/tests/apitests/python/test_p2p.py
@@ -12,7 +12,6 @@ from library.project import Project
 from library.user import User
 from library.repository import Repository
 from library.registry import Registry
-from library.repository import pull_harbor_image
 from library.artifact import Artifact
 from library.preheat import Preheat
 import v2_swagger_client
diff --git a/tests/apitests/python/test_retain_image_last_pull_time.py b/tests/apitests/python/test_retain_image_last_pull_time.py
new file mode 100644
index 000000000..4460df2f7
--- /dev/null
+++ b/tests/apitests/python/test_retain_image_last_pull_time.py
@@ -0,0 +1,75 @@
+from __future__ import absolute_import
+import time
+import unittest
+
+from testutils import ADMIN_CLIENT, harbor_server, suppress_urllib3_warning
+from library.user import User
+from library.configurations import Configurations
+from library.project import Project
+from library.artifact import Artifact
+from library.scan import Scan
+from library.repository import push_self_build_image_to_project
+
+class TestRetainImageLastPullTime(unittest.TestCase, object):
+
+    @suppress_urllib3_warning
+    def setUp(self):
+        self.user = User()
+        self.conf = Configurations()
+        self.project = Project()
+        self.artifact = Artifact()
+        self.scan = Scan()
+        self.image = "alpine"
+        self.tag = "latest"
+        self.default_time = "1-01-01 00:00:00"
+
+    def testRetainImageLastPullTime(self):
+        """
+        Test case:
+            RetainImageLastPullTime
+        Test step and expected result:
+            1. Create a new user(UA);
+            2. Create a new private project(PA) by user(UA);
+            3. Push a new image(IA) in project(PA) by user(UA);
+            4. Enable the retain image last pull time on scanning;
+            5. Scan image(IA);
+            6. Check the last pull time of image(IA) is default time;
+            7. Disable the retain image last pull time on scanning;
+            8. Scan image(IA);
+            9. Check the last pull time of image(IA) is not default time;
+        """
+        url = ADMIN_CLIENT["endpoint"]
+        user_password = "Aa123456"
+        # 1. Create a new 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 a new private project(PA) by user(UA);
+        _, project_name = self.project.create_project(metadata = {"public": "false"}, **USER_CLIENT)
+        # 3. Push a new image(IA) in repository(RA) by user(UA);
+        push_self_build_image_to_project(project_name, harbor_server, user_name, user_password, self.image, self.tag)
+        # 4. Enable the retain image last pull time on scanning;
+        self.conf.set_configurations_of_retain_image_last_pull_time(True)
+        # 5. Scan image(IA);
+        self.scan.scan_artifact(project_name, self.image, self.tag, **USER_CLIENT)
+        time.sleep(15)
+        # 6. Check the last pull time of image(IA) is default time;
+        artifact_info = self.artifact.get_reference_info(project_name, self.image, self.tag, **USER_CLIENT)
+        self.assertEqual(artifact_info.pull_time.strftime("%Y-%m-%d %H:%M:%S"), self.default_time)
+        # 7. Disable the retain image last pull time on scanning;
+        self.conf.set_configurations_of_retain_image_last_pull_time(False)
+        # 8. Scan image(IA);
+        self.scan.scan_artifact(project_name, self.image, self.tag, **USER_CLIENT)
+        # 9. Check the last pull time of image(IA) is not default time;
+        pull_time = self.default_time
+        for _ in range(6):
+            artifact_info = self.artifact.get_reference_info(project_name, self.image, self.tag, **USER_CLIENT)
+            pull_time = artifact_info.pull_time.strftime("%Y-%m-%d %H:%M:%S")
+            if pull_time != self.default_time:
+                break
+            else:
+                time.sleep(5)
+        self.assertNotEqual(pull_time, self.default_time)
+
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/tests/robot-cases/Group0-BAT/API_DB.robot b/tests/robot-cases/Group0-BAT/API_DB.robot
index 77d17b0d7..d46b5b946 100644
--- a/tests/robot-cases/Group0-BAT/API_DB.robot
+++ b/tests/robot-cases/Group0-BAT/API_DB.robot
@@ -178,3 +178,7 @@ Test Case - Scan Data Export
 Test Case - Job Service Dashboard
     [Tags]  job_service_dashboard
     Harbor API Test  ./tests/apitests/python/test_job_service_dashboard.py
+
+Test Case - Retain Image Last Pull Time
+    [Tags]  retain_image_last_pull_time
+    Harbor API Test  ./tests/apitests/python/test_retain_image_last_pull_time.py