From 4b5080d44ed98ada816177e6811e0341372a6b9b Mon Sep 17 00:00:00 2001 From: danfengliu Date: Tue, 9 Jun 2020 15:33:18 +0800 Subject: [PATCH] Add singularity py-test Signed-off-by: danfengliu --- .github/workflows/CI.yml | 15 ++++ tests/apitests/python/library/singularity.py | 24 ++++++ ...oras_cli.py => test_push_files_by_oras.py} | 4 +- .../python/test_push_sif_by_singularity.py | 78 +++++++++++++++++++ tests/robot-cases/Group0-BAT/API_DB.robot | 5 +- 5 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 tests/apitests/python/library/singularity.py rename tests/apitests/python/{test_oras_cli.py => test_push_files_by_oras.py} (97%) create mode 100644 tests/apitests/python/test_push_sif_by_singularity.py diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 6f16b6257..70107ba16 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -147,6 +147,21 @@ jobs: mkdir -p oras-install/ tar -zxf oras_0.8.1_*.tar.gz -C oras-install/ sudo mv oras-install/oras /usr/local/bin/ + sudo apt-get update && sudo apt-get install -y \ + build-essential \ + uuid-dev \ + libgpgme-dev \ + squashfs-tools \ + libseccomp-dev \ + pkg-config \ + cryptsetup-bin + export VERSION=3.5.3 && \ + wget https://github.com/sylabs/singularity/releases/download/v${VERSION}/singularity-${VERSION}.tar.gz && \ + tar -xzf singularity-${VERSION}.tar.gz && \ + cd singularity + ./mconfig && \ + make -C builddir && \ + sudo make -C builddir install - name: install run: | cd src/github.com/goharbor/harbor diff --git a/tests/apitests/python/library/singularity.py b/tests/apitests/python/library/singularity.py new file mode 100644 index 000000000..b5bc9fd5b --- /dev/null +++ b/tests/apitests/python/library/singularity.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- + +import os +import base +from datetime import datetime + +singularity_cmd = "singularity" +timestamp = datetime.now().strftime(r'%m%s') + +def set_singularity_login_env(user, password): + os.environ.setdefault('SINGULARITY_DOCKER_USERNAME', user) + os.environ.setdefault('SINGULARITY_DOCKER_PASSWORD', password) + +def singularity_push_to_harbor(harbor_server, sif_file, project, image, tag): + ret = base.run_command( [singularity_cmd, "push", sif_file, "oras://"+harbor_server + "/" + project + "/" + image+":"+ tag] ) + +def singularity_pull(out_file, from_sif_file): + ret = base.run_command( [singularity_cmd, "pull", out_file, from_sif_file] ) + +def push_singularity_to_harbor(from_URI, from_namespace, harbor_server, user, password, project, image, tag): + tmp_sif_file = image+timestamp+".sif" + set_singularity_login_env(user, password) + singularity_pull(tmp_sif_file, from_URI+"//"+from_namespace + image+":" + tag) + singularity_push_to_harbor(harbor_server, tmp_sif_file, project, image, tag) diff --git a/tests/apitests/python/test_oras_cli.py b/tests/apitests/python/test_push_files_by_oras.py similarity index 97% rename from tests/apitests/python/test_oras_cli.py rename to tests/apitests/python/test_push_files_by_oras.py index 790aedcb8..3d974fc11 100644 --- a/tests/apitests/python/test_oras_cli.py +++ b/tests/apitests/python/test_push_files_by_oras.py @@ -40,7 +40,7 @@ class TestProjects(unittest.TestCase): 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;; + 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; @@ -62,7 +62,7 @@ class TestProjects(unittest.TestCase): md5_list_push = library.oras.oras_push(harbor_server, user_name, user_001_password, TestProjects.project_name, self.repo_name, self.tag) print "md5_list_push:",md5_list_push - #4. Get repository from Harbor successfully, and verfiy repository name is repo pushed by ORAS CLI;; + #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) print "repo_data:", repo_data self.assertEqual(repo_data.name, TestProjects.project_name + "/" + self.repo_name) diff --git a/tests/apitests/python/test_push_sif_by_singularity.py b/tests/apitests/python/test_push_sif_by_singularity.py new file mode 100644 index 000000000..ab67ebf61 --- /dev/null +++ b/tests/apitests/python/test_push_sif_by_singularity.py @@ -0,0 +1,78 @@ +from __future__ import absolute_import +import unittest +import urllib + +import library.singularity +from library.sign import sign_image +from testutils import ADMIN_CLIENT +from testutils import harbor_server +from testutils import TEARDOWN +from library.user import User +from library.project import Project +from library.repository import Repository +from library.artifact import Artifact + + +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 = "Aa123456" + + #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) + print "repo_data:", repo_data + 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) + print "artifact:", artifact + 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) + +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 425487d80..d1af1ec72 100644 --- a/tests/robot-cases/Group0-BAT/API_DB.robot +++ b/tests/robot-cases/Group0-BAT/API_DB.robot @@ -100,4 +100,7 @@ Test Case - Push Image With Special Name Harbor API Test ./tests/apitests/python/test_push_image_with_special_name.py Test Case - Push Artifact With ORAS CLI [Tags] oras - Harbor API Test ./tests/apitests/python/test_oras_cli.py + Harbor API Test ./tests/apitests/python/test_push_files_by_oras.py +Test Case - Push Singularity file With Singularity CLI + [Tags] singularity + Harbor API Test ./tests/apitests/python/test_push_sif_by_singularity.py