From b40f63d9dbf558412eeaaf86a1a9dc78b753355b Mon Sep 17 00:00:00 2001 From: yaokai13 Date: Tue, 27 Apr 2021 11:42:51 +0800 Subject: [PATCH] add support for multi version and fix net bug and add test --- core/gitee_to_obs.py | 35 ++++--- test/test_gitee_to_obs.py | 191 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 215 insertions(+), 11 deletions(-) create mode 100644 test/test_gitee_to_obs.py diff --git a/core/gitee_to_obs.py b/core/gitee_to_obs.py index 7182107..d367340 100644 --- a/core/gitee_to_obs.py +++ b/core/gitee_to_obs.py @@ -99,23 +99,34 @@ class SYNCCode(object): log.info("Git clone the %s in %s" % (rpm_dir, gitee_branch)) self.cmd.ssh_cmd("rm -rf %s " % (path)) repo_url = "https://%s:%s@gitee.com/src-openEuler/%s" % (self.giteeuser, self.giteeuserpwd, rpm_dir) - clone_result = self.cmd.ssh_cmd("git lfs clone --depth=1 %s -b %s %s" - % (repo_url, gitee_branch, path), 600) - pull_result_last = str(self.cmd.ssh_cmd('git -C %s pull' % path)[1].strip()).split("'")[1] - if "Already" in pull_result_last: - log.info(pull_result_last) - log.info("At now %s the branch is in %s" % (rpm_dir, gitee_branch)) - else: - raise SystemExit('Git clone error') + for i in range(5): + clone_result = self.cmd.ssh_cmd("git lfs clone --depth=1 %s -b %s %s" + % (repo_url, gitee_branch, path), 600) + pull_result_last = str(self.cmd.ssh_cmd('git -C %s pull' % path)[1].strip()).split("'")[1] + if "Already" in pull_result_last: + log.info(pull_result_last) + log.info("At now %s the branch is in %s" % (rpm_dir, gitee_branch)) + return + else: + log.info("_GIT_CLONE: %s" % i) + clear_repo = self.cmd.ssh_cmd("rm -rf %s" % path) + log.info("clear_repo:%s" % clear_repo) + continue + raise SystemExit('Git clone error') def _get_obs_project(self): """ Get the obs project from gitee_branch """ log.info("Start get the obs_project") - path = self.meta_path + '/' + self.gitee_branch - log.info(path) - cmd = "find %s -name %s | awk -F '/' '{print $4}'" % (path, self.repository) + if "multi" in self.gitee_branch: + path = self.meta_path + '/' + 'multi_version/' + self.gitee_branch + log.info(path) + cmd = "find %s -name %s | awk -F '/' '{print $5}'" % (path, self.repository) + else: + path = self.meta_path + '/' + self.gitee_branch + log.info(path) + cmd = "find %s -name %s | awk -F '/' '{print $4}'" % (path, self.repository) all_project = os.popen(cmd).readlines() log.info(all_project) obs_project = None @@ -139,6 +150,8 @@ class SYNCCode(object): """ if self.gitee_branch == "master": source_path = "/srv/cache/obs/tar_scm/repo/next/openEuler" + elif "multi" in self.gitee_branch: + source_path = "/srv/cache/obs/tar_scm/repo/next/openEuler/multi_version/" + self.gitee_branch else: source_path = "/srv/cache/obs/tar_scm/repo/next/" + self.gitee_branch if self.repository == "CreateImage": diff --git a/test/test_gitee_to_obs.py b/test/test_gitee_to_obs.py new file mode 100644 index 0000000..db21f53 --- /dev/null +++ b/test/test_gitee_to_obs.py @@ -0,0 +1,191 @@ +#/bin/env python3 +# -*- encoding=utf8 -*- +#****************************************************************************** +# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. +# licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. +# Author: yaokai13 +# Create: 2021-04-27 +# ****************************************************************************** +""" +test all: python3 -m pytest -s test_project.py +test one class: python3 -m pytest -s test_project.py::TestCase +test one class one case: python3 -m pytest -s test_project.py::TestCase::test_1 +""" +import os +import sys +import pytest +current_path = os.path.join(os.path.split(os.path.realpath(__file__))[0]) +sys.path.append(os.path.join(current_path, "..")) +from func import SetEnv, CommonFunc +from core.gitee_to_obs import SYNCCode +from common.common import Pexpect + +S = SetEnv() +C = CommonFunc() +repo_path = None +P = None +source_path = "/srv/cache/obs/tar_scm/repo/next/" +scmd = Pexpect("root","127.0.0.1","112233","2222") + +class TestCase(object): + def setup_class(self): + S.set_oscrc() + S.set_gitee() + global repo_path + repo_path = C.pull_from_gitee_repo(S.gitee_info["user"], S.gitee_info["passwd"], \ + "https://gitee.com/{0}/obs_meta".format(S.gitee_info["user"]), "master", "obs_meta") + + def teardown_class(self): + S.unset_oscrc() + S.unset_gitee() + cmd = "rm -rf %s" % repo_path + if os.system(cmd) == 0: + assert True + else: + assert False, "fail to clear {0} after testing".format(repo_path) + + def test_1(self): + """ + test for sync all the pkg in obs_project + """ + assert os.path.exists(repo_path), "{0} not exist".format(repo_path) + kw = { + "branch": "***", + "gitee_user": "***", + "gitee_pwd": "***", + "obs_meta_path": repo_path, + "project": "***", + "source_server_user": "***", + "source_server_ip": "***", + "source_server_pwd": "***", + "source_server_port": "***", + "repository":"", + "pkglist":"" + } + check1 = SYNCCode(**kw) + check1.sync_code_to_obs() + cmd = "osc ls %s" % kw['project'] + pkgs = os.popen(cmd).readlines() + print(pkgs) + for pkg in pkgs: + if kw['branch'] == "master": + my_cmd = "ls %s | grep %s" % (os.path.join(source_path,"openEuler"), pkg.replace('\n', '')) + if pkg.replace('\n', '') != scmd.ssh_cmd(my_cmd)[1].strip().decode('utf-8'): + assert False, "{0} sync fail".format(pkg.replace('\n', '')) + elif "multi" in kw['branch']: + my_cmd = "ls %s | grep %s" % (os.path.join(source_path,"multi_version",kw['branch']), pkg.replace('\n', '')) + if pkg.replace('\n', '') != scmd.ssh_cmd(my_cmd)[1].strip().decode('utf-8'): + assert False, "{0} sync fail".format(pkg.replace('\n', '')) + else: + my_cmd = "ls %s | grep %s" % (os.path.join(source_path,kw['branch']), pkg.replace('\n', '')) + if pkg.replace('\n', '') != scmd.ssh_cmd(my_cmd)[1].strip().decode('utf-8'): + assert False, "{0} sync fail".format(pkg.replace('\n', '')) + + + def test_2(self): + """ + test for sync one pkg + """ + assert os.path.exists(repo_path), "{0} not exist".format(repo_path) + kw = { + "branch": "***", + "gitee_user": "***", + "gitee_pwd": "***", + "obs_meta_path": repo_path, + "repository": "***", + "source_server_user": "***", + "source_server_ip": "***", + "source_server_pwd": "***", + "source_server_port": "***", + "pkglist":"", + "project":"" + } + check2 = SYNCCode(**kw) + check2.sync_code_to_obs() + if kw['branch'] == "master": + my_cmd = "ls %s | grep %s" % (os.path.join(source_path,"openEuler"), kw['repository']) + if kw['repository'] != scmd.ssh_cmd(my_cmd)[1].strip().decode('utf-8'): + assert False, "{0} sync fail".format(kw['repository']) + elif "multi" in kw['branch']: + my_cmd = "ls %s | grep %s" % (os.path.join(source_path,"multi_version",kw['branch']), kw['repository']) + if kw['repository'] != scmd.ssh_cmd(my_cmd)[1].strip().decode('utf-8'): + assert False, "{0} sync fail".format(kw['repository']) + else: + my_cmd = "ls %s | grep %s" % (os.path.join(source_path,kw['branch']), kw['repository']) + if kw['repository'] != scmd.ssh_cmd(my_cmd)[1].strip().decode('utf-8'): + assert False, "{0} sync fail".format(kw['repository']) + + def test_3(self): + """ + Have the project name help you more fast to sync + """ + assert os.path.exists(repo_path), "{0} not exist".format(repo_path) + kw = { + "branch": "***", + "gitee_user": "***", + "gitee_pwd": "***", + "obs_meta_path": repo_path, + "repository": "***", + "source_server_user": "***", + "source_server_ip": "***", + "source_server_pwd": "***", + "source_server_port": "***", + "pkglist":"", + "project":"***" + } + check3 = SYNCCode(**kw) + check3.sync_code_to_obs() + if kw['branch'] == "master": + my_cmd = "ls %s | grep %s" % (os.path.join(source_path,"openEuler"), kw['repository']) + if kw['repository'] != scmd.ssh_cmd(my_cmd)[1].strip().decode('utf-8'): + assert False, "{0} sync fail".format(kw['repository']) + elif "multi" in kw['branch']: + my_cmd = "ls %s | grep %s" % (os.path.join(source_path,"multi_version",kw['branch']), kw['repository']) + if kw['repository'] != scmd.ssh_cmd(my_cmd)[1].strip().decode('utf-8'): + assert False, "{0} sync fail".format(kw['repository']) + else: + my_cmd = "ls %s | grep %s" % (os.path.join(source_path,kw['branch']), kw['repository']) + if kw['repository'] != scmd.ssh_cmd(my_cmd)[1].strip().decode('utf-8'): + assert False, "{0} sync fail".format(kw['repository']) + + + def test_4(self): + """ + use the pkglist help you can sync not only one pkg at the same time + """ + assert os.path.exists(repo_path), "{0} not exist".format(repo_path) + kw = { + "branch": "***", + "gitee_user": "***", + "gitee_pwd": "***", + "obs_meta_path": repo_path, + "repository": "", + "source_server_user": "***", + "source_server_ip": "***", + "source_server_pwd": "***", + "source_server_port": "***", + "pkglist": ["***", "***"], + "project": "***" + } + check4 = SYNCCode(**kw) + check4.sync_code_to_obs() + for pkg in kw['pkglist']: + if kw['branch'] == "master": + my_cmd = "ls %s | grep %s" % (os.path.join(source_path,"openEuler"), pkg.replace('\n', '')) + if pkg.replace('\n', '') != scmd.ssh_cmd(my_cmd)[1].strip().decode('utf-8'): + assert False, "{0} sync fail".format(pkg.replace('\n', '')) + elif "multi" in kw['branch']: + my_cmd = "ls %s | grep %s" % (os.path.join(source_path,"multi_version",kw['branch']), pkg.replace('\n', '')) + if pkg.replace('\n', '') != scmd.ssh_cmd(my_cmd)[1].strip().decode('utf-8'): + assert False, "{0} sync fail".format(pkg.replace('\n', '')) + else: + my_cmd = "ls %s | grep %s" % (os.path.join(source_path,kw['branch']), pkg.replace('\n', '')) + if pkg.replace('\n', '') != scmd.ssh_cmd(my_cmd)[1].strip().decode('utf-8'): + assert False, "{0} sync fail".format(pkg.replace('\n', '')) -- Gitee