From c910aa5762d1172b6d87de98a292070bee14c906 Mon Sep 17 00:00:00 2001 From: wangchong1995924 <15229716099@163.com> Date: Wed, 16 Jun 2021 17:52:21 +0800 Subject: [PATCH] modify sync_pckg_mgmt.py and add test_pckg_mgmt.py --- config/config.ini | 15 +- core/package_manager.py | 157 +++++++++++++------ core/sync_pckg_mgmt.py | 99 ++++++------ openeuler_obs.py | 5 +- test/test_pckg_mgmt.py | 332 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 503 insertions(+), 105 deletions(-) create mode 100644 test/test_pckg_mgmt.py diff --git a/config/config.ini b/config/config.ini index 19b25d0..d9372a7 100644 --- a/config/config.ini +++ b/config/config.ini @@ -14,16 +14,19 @@ name = ci_check build [package_info_file] name = package_info.csv [branch_proj] -master = openEuler:Factory openEuler:Mainline openEuler:Epol -openEuler-20.09 = openEuler:20.09 openEuler:20.09:Epol -openEuler-20.03-LTS = openEuler:20.03:LTS -openEuler-20.03-LTS-SP1 = openEuler:20.03:LTS:SP1 openEuler:20.03:LTS:SP1:Epol -openEuler-20.03-LTS-Next = openEuler:20.03:LTS:Next openEuler:20.03:LTS:Next:Epol +master = openEuler:Factory openEuler:Mainline openEuler:Epol openEuler:Extras +openEuler-20.09 = openEuler:20.09 openEuler:20.09:Epol openEuler:20.09:Extras +openEuler-20.03-LTS = openEuler:20.03:LTS openEuler:20.03:LTS:Epol +openEuler-20.03-LTS-SP1 = openEuler:20.03:LTS:SP1 openEuler:20.03:LTS:SP1:Epol openEuler:20.03:LTS:SP1:Extras +openEuler-20.03-LTS-Next = openEuler:20.03:LTS:Next openEuler:20.03:LTS:Next:Epol openEuler:20.03:LTS:Next:Extras openEuler-21.03 = openEuler:21.03 openEuler:21.03:Epol openEuler:21.03:Extras -openEuler-20.03-LTS-SP2 = openEuler:20.03:LTS:SP2 openEuler:20.03:LTS:SP2:Epol +openEuler-20.03-LTS-SP2 = openEuler:20.03:LTS:SP2 openEuler:20.03:LTS:SP2:Epol openEuler:20.03:LTS:SP2:Extras oepkg_openstack-common_oe-20.03-LTS-SP2 = openEuler:20.03:LTS:SP2:oepkg:openstack:common oepkg_openstack-queens_oe-20.03-LTS-SP2 = openEuler:20.03:LTS:SP2:oepkg:openstack:queens oepkg_openstack-rocky_oe-20.03-LTS-SP2 = openEuler:20.03:LTS:SP2:oepkg:openstack:rocky +oepkg_openstack-common_oe-20.03-LTS-Next = openEuler:20.03:LTS:Next:oepkg:openstack:common +oepkg_openstack-queens_oe-20.03-LTS-Next = openEuler:20.03:LTS:Next:oepkg:openstack:queens +oepkg_openstack-rocky_oe-20.03-LTS-Next = openEuler:20.03:LTS:Next:oepkg:openstack:rocky [gitee_repository] community = https://gitee.com/openeuler/community obs_meta = https://gitee.com/src-openeuler/obs_meta diff --git a/core/package_manager.py b/core/package_manager.py index 54c426f..49a27c7 100755 --- a/core/package_manager.py +++ b/core/package_manager.py @@ -25,6 +25,7 @@ import yaml import shutil import configparser from concurrent.futures import ThreadPoolExecutor +from collections import Counter from core.gitee_to_obs import SYNCCode current_path = os.path.join(os.path.split(os.path.realpath(__file__))[0]) sys.path.append(os.path.join(current_path, "..")) @@ -42,40 +43,32 @@ class OBSPkgManager(object): kwargs: dict include giteeUserName giteeUserPwd obs_meta_path """ self.kwargs = kwargs - self.work_dir = "/jenkins_home/workspace/obs_meta_update/openeuler_jenkins" - self.obs_meta_path = os.path.join(self.work_dir, self.kwargs["obs_meta_path"]) - self.patch_file_path = os.path.join(self.work_dir, "diff_patch") + self.init_path = self.kwargs["init_path"] + self.obs_meta_path = self.kwargs["obs_meta_path"] + self.patch_file_path = os.path.join(self.init_path, "diff_patch") self.giteeUserName = self.kwargs["gitee_user"] self.giteeUserPwd = self.kwargs["gitee_pwd"] self.sync_code = self.kwargs["sync_code"] self.multi_version_dir = '' + os.chdir(self.init_path) + p = ParserConfigIni() + self.branch_proj_dict = p.get_branch_proj() - def _pre_env(self): - """ - initialize the workdir - """ - if os.path.exists(self.work_dir): - shutil.rmtree(self.work_dir) - os.makedirs(self.work_dir) - os.chdir(self.work_dir) - def _git_clone(self, git_house): """ git clone function """ if git_house == "obs_meta": - os.chdir(self.obs_meta_path) - os.system("git diff --name-status HEAD~1 HEAD~0 > %s" - % self.patch_file_path) + os.system("cd %s && git diff --name-status HEAD~1 HEAD~0 > %s && cd -" + % (self.obs_meta_path, self.patch_file_path)) if git_house == "community": - community_path = os.path.join(self.work_dir, "community") + community_path = os.path.join(self.init_path, "community") if os.path.exists(community_path): shutil.rmtree(community_path) git_url = "https://%s:%s@gitee.com/openeuler/community.git" % ( self.giteeUserName, self.giteeUserPwd) - ret = os.popen("git lfs --depth 1 clone %s" % git_url).read() + ret = os.popen("git lfs --depth 1 clone %s %s" % (git_url, community_path)).read() log.debug(ret) - os.chdir(self.work_dir) def _add_pkg(self, proj, pkg, branch_name): """ @@ -193,7 +186,7 @@ class OBSPkgManager(object): for i in range(5): if os.system("git push") == 0: break - os.chdir(self.work_dir) + os.chdir(self.init_path) return 0 def _modify_pkg_service(self, proj, pkg, branch_name): @@ -403,7 +396,6 @@ class OBSPkgManager(object): self._check_obs_pkg() if self.kwargs["check_meta"]: self._check_obs_meta_pkg() - self._pre_env() self._git_clone("obs_meta") self._obs_pkgs_action() @@ -412,9 +404,8 @@ class OBSPkgManager(object): Preprocessing the data """ yaml_dict = {} - os.chdir(self.work_dir) - yaml_path = os.path.join(self.work_dir, "community/repository") - f1 = open("%s/src-openeuler.yaml" % yaml_path, 'r') + yaml_path = os.path.join(self.init_path, "community/repository/src-openeuler.yaml") + f1 = open(yaml_path, 'r') y = yaml.load(f1) for tmp in y['repositories']: name = tmp['name'] @@ -436,14 +427,13 @@ class OBSPkgManager(object): meta_bp_dict = {} meta_pb_dict = {} pkg_branch_dict = {} - proj_pkg_dict = {} - os.chdir(self.obs_meta_path) - cmd = "find | grep _service | grep -Ev 'OBS_PRJ_meta|openEuler-EPOL-LTS' | \ - awk -F '/' '{print $2,$3,$(NF-1)}' | sort | uniq > %s/res.txt" % self.work_dir + proj_pkg_dict = {} + cmd = "cd %s && find | grep _service | grep -Ev 'OBS_PRJ_meta|openEuler-EPOL-LTS' | \ + awk -F '/' '{print $2,$3,$(NF-1)}' | sort | uniq > %s/res.txt && cd - &>/dev/null" % ( + self.obs_meta_path, self.init_path) while os.system(cmd) != 0: continue - os.chdir(self.work_dir) - f2 = open("%s/res.txt" % self.work_dir, 'r') + f2 = open("%s/res.txt" % self.init_path, 'r') for line in f2: br = line.strip().split()[0] proj = line.strip().split()[1] @@ -457,39 +447,105 @@ class OBSPkgManager(object): f2.close() for key, value in meta_bp_dict.items(): meta_bp_dict[key] = list(set(value)) - os.remove("%s/res.txt" % self.work_dir) + os.remove("%s/res.txt" % self.init_path) data_list.append(meta_bp_dict) data_list.append(pkg_branch_dict) data_list.append(proj_pkg_dict) data_list.append(meta_pb_dict) return data_list - + + def _get_samebranchpkg_and_pkgwithoutservice(self): + """ + get same packages under same branch and packages without _service file + """ + same_pkg_dict = {} + no_service_dict = {} + proj_pkg_dict = {} + meta_pb_dict = {} + all_data = [] + for branch, projs in self.branch_proj_dict.items(): + proj_pkg = {} + tmp_list = [] + tmp = {} + for proj in projs.split(' '): + if proj.endswith(":Bak") and "RISC-V" in proj and "selfbuild" in proj: + continue + proj_path = os.path.join(self.obs_meta_path, branch, proj) + if os.path.exists(proj_path): + meta_pb_dict[proj] = branch + pkglist = os.listdir(os.path.join(self.obs_meta_path, branch, proj)) + if "README.md" in pkglist: + pkglist.remove("README.md") + if "readme.md" in pkglist: + pkglist.remove("readme.md") + if pkglist: + proj_pkg[proj] = pkglist + proj_pkg_dict[proj] = pkglist + tmp_list.extend(pkglist) + for pkg in pkglist: + pkg_service_path = os.path.join(proj_path, pkg, "_service") + if not os.path.exists(pkg_service_path): + no_service_dict.setdefault(proj, []).append(pkg) + if tmp_list: + b = dict(Counter(tmp_list)) + for key, value in b.items(): + if value > 1: + for proj, pkg in proj_pkg.items(): + if key in pkg: + tmp.setdefault(key, []).append(proj) + if tmp: + same_pkg_dict[branch] = tmp + all_data.append(same_pkg_dict) + all_data.append(no_service_dict) + all_data.append(proj_pkg_dict) + all_data.append(meta_pb_dict) + return all_data + def _check_obs_meta_pkg(self): - self._pre_env() - mylist = self._parse_meta_data() - proj_pkg_dict = mylist[2] - meta_pb_dict = mylist[3] + same_pkg_dict = {} + no_service_dict = {} + proj_pkg_dict = {} + meta_pb_dict = {} + same_pkg_dict, no_service_dict, proj_pkg_dict, meta_pb_dict = self._get_samebranchpkg_and_pkgwithoutservice() + log.info("=====Check need add or delete packages=====") for proj, pkg in proj_pkg_dict.items(): - log.info("proj:%s" % proj) + log.info("Project:%s" % proj) obs_pkg = os.popen("osc ls %s 2>/dev/null" % proj).read().strip() obs_pkg = obs_pkg.replace('\n', ',').split(',') - log.info("obs pkg total:%s" % len(obs_pkg)) - log.info("meta pkg total:%s" % len(pkg)) + log.info("Obs pkg total:%s" % len(obs_pkg)) + log.info("Meta pkg total:%s" % len(pkg)) need_add = set(pkg) - set(obs_pkg) - log.info("need add pkg total:%s" % len(need_add)) - log.info("need add pkgname:%s" % need_add) + log.info("Need add pkg total:%s" % len(need_add)) + log.info("Need add pkgname:%s" % list(need_add)) need_del = set(obs_pkg) - set(pkg) - log.info("need del pkg total:%s" % len(need_del)) - log.info("need del pkgname:%s" % need_del) - if len(need_add): - for pkgname in list(need_add): - self._add_pkg(proj, pkgname, meta_pb_dict[proj]) - if self.sync_code: - self._sync_pkg_code(proj, pkgname, meta_pb_dict[proj]) - if len(need_del): - for pkgname in list(need_del): - self._del_pkg(proj, pkgname) + log.info("Need del pkg total:%s" % len(need_del)) + log.info("Need del pkgname:%s" % list(need_del)) + if proj in no_service_dict.keys(): + log.info("Without _service file pkgname:%s" % no_service_dict[proj]) + else: + log.info("Without _service file pkgname:[]") + #if len(need_add): + # for pkgname in list(need_add): + # self._add_pkg(proj, pkgname, meta_pb_dict[proj]) + # if self.sync_code: + # self._sync_pkg_code(proj, pkgname, meta_pb_dict[proj]) + #if len(need_del): + # for pkgname in list(need_del): + # self._del_pkg(proj, pkgname) log.info("===========================") + log.info("=====Check same package under same branch report=====") + for branch, msg in same_pkg_dict.items(): + log.info("Branch : %s" % branch) + log.info("Packagename\tPorjectname") + for pkg, proj in msg.items(): + log.info("%s\t%s" % (pkg, proj)) + log.info("=====================================================") + if same_pkg_dict or no_service_dict: + log.info("Have some problems !!!") + exit(1) + else: + log.info("Have no problems !!!") + exit(0) def _check_yaml_meta_pkg(self, yaml_dict, meta_bp_dict, pkg_branch_dict): """ @@ -555,7 +611,6 @@ class OBSPkgManager(object): check the obs project and operate according to the src-openeuler.yaml file """ yaml_dict = {} - self._pre_env() self._git_clone("community") yaml_dict = self._parse_yaml_data() mylist = self._parse_meta_data() diff --git a/core/sync_pckg_mgmt.py b/core/sync_pckg_mgmt.py index e3ef559..505d495 100644 --- a/core/sync_pckg_mgmt.py +++ b/core/sync_pckg_mgmt.py @@ -32,43 +32,28 @@ class SyncPckgMgmt(object): """ def __init__(self, **kwargs): """ - current_path: init path giteeuser: gitee user name giteeuserpwd: gitee password """ self.kwargs = kwargs - self.current_path = os.getcwd() self.giteeuser = self.kwargs['gitee_user'] self.giteeuserpwd = self.kwargs['gitee_pwd'] + self.obs_meta_path = self.kwargs['obs_meta_path'] + self.release_management_path = self.kwargs['release_management_path'] - def _git_clone(self, prefix, gitee_repo, branch="master"): - """ - git clone gitee repo - """ - repo_path = os.path.join(self.current_path, gitee_repo) - if os.path.exists(repo_path): - shutil.rmtree(repo_path) - git_url = "https://%s:%s@gitee.com/%s/%s -b %s" % (self.giteeuser, \ - self.giteeuserpwd, prefix, gitee_repo, branch) - cmd = "git clone %s" % git_url - for x in range(5): - if os.system(cmd) == 0: - log.info("Git clone %s succeed!" % gitee_repo) - break - if not os.path.exists(repo_path): - log.error("Git clone %s failed!" % gitee_repo) - exit(1) - - def _get_change_file(self, gitee_repo): + def _get_change_file(self): """ get release-managemnet change file """ - os.chdir(os.path.join(self.current_path, gitee_repo)) - cmd = "git diff --name-status HEAD~1 HEAD~0 | grep pckg-mgmt.yaml" - result = os.popen(cmd).read().split('\n') - change_file = [x for x in result if x != ''] - os.chdir(self.current_path) - return change_file + if os.path.exists(self.release_management_path): + os.chdir(self.release_management_path) + cmd = "git diff --name-status HEAD~1 HEAD~0 | grep pckg-mgmt.yaml" + result = os.popen(cmd).read().split('\n') + change_file = [x for x in result if x != ''] + return change_file + else: + log.error("%s not exist!" % self.release_management_path) + exit(1) def _get_yaml_file_msg(self, file_path): """ @@ -79,6 +64,22 @@ class SyncPckgMgmt(object): with open(file_path, "r", encoding='utf-8') as f: file_msg = yaml.load(f, Loader=yaml.FullLoader) return file_msg + + def _check_pckg_yaml(self, file_msg, branch): + """ + check pckg-mgmt.yaml file + """ + proj = branch.replace("-", ":") + flag = False + for title in file_msg['packages']: + for msg in file_msg['packages'][title]: + if msg['branch_to'] != branch: + flag = True + log.error("%s branch_to is error, please check yaml." % msg['name']) + if not msg['obs_to'].startswith(proj): + flag = True + log.error("%s obs_to is error, please check yaml." % msg['name']) + return flag def _parse_yaml_msg(self, file_msg): """ @@ -174,9 +175,8 @@ class SyncPckgMgmt(object): """ add obs_meta packages _service file """ - obs_meta_path = os.path.join(self.current_path, "obs_meta") - from_pkg_path = os.path.join(obs_meta_path, tmp['branch_from'], tmp['obs_from'], tmp['pkgname']) - pkg_path = os.path.join(obs_meta_path, tmp['branch_to'], tmp['obs_to'], tmp['pkgname']) + from_pkg_path = os.path.join(self.obs_meta_path, tmp['branch_from'], tmp['obs_from'], tmp['pkgname']) + pkg_path = os.path.join(self.obs_meta_path, tmp['branch_to'], tmp['obs_to'], tmp['pkgname']) pkg_service_path = os.path.join(pkg_path, "_service") if tmp['branch_from'] == "master": branch = "openEuler" @@ -199,8 +199,7 @@ class SyncPckgMgmt(object): """ delete obs_meta packages """ - obs_meta_path = os.path.join(self.current_path, "obs_meta") - pkg_path = os.path.join(obs_meta_path, tmp['branch_to'], tmp['obs_to'], tmp['pkgname']) + pkg_path = os.path.join(self.obs_meta_path, tmp['branch_to'], tmp['obs_to'], tmp['pkgname']) if os.path.exists(pkg_path): shutil.rmtree(pkg_path) log.info("delete %s %s %s succeed!" % (tmp['branch_to'], tmp['obs_to'], tmp['pkgname'])) @@ -211,19 +210,19 @@ class SyncPckgMgmt(object): """ for proj, pkg in prj_pkg.items(): if proj != "branch": - meta_pkglist = os.listdir(os.path.join(self.current_path, "obs_meta", prj_pkg['branch'], proj)) + meta_pkglist = os.listdir(os.path.join(self.obs_meta_path, prj_pkg['branch'], proj)) need_del_pkg = set(meta_pkglist) - set(pkg) log.info("obs_meta %s %s redundant pkg:%s" % (prj_pkg['branch'], proj, list(need_del_pkg))) for del_pkg in need_del_pkg: tmp = {'pkgname': del_pkg, 'branch_to': prj_pkg['branch'], 'obs_to': proj} self._del_pkg(tmp) - def _push_code(self, gitee_repo_path): + def _push_code(self): """ push code to gitee repo """ - if os.path.exists(gitee_repo_path): - os.chdir(gitee_repo_path) + if os.path.exists(self.obs_meta_path): + os.chdir(self.obs_meta_path) cmd = "git status -s" if os.popen(cmd).read(): cmd = "git add -A && git commit -m \"synchronize with pckg-mgmt.yaml file contents\"" @@ -232,35 +231,41 @@ class SyncPckgMgmt(object): for i in range(5): if os.system(cmd) == 0: log.info("push code to gitee repo succeed!") - os.chdir(self.current_path) - return + return 0 else: log.error("push code failed, try again...") raise SystemExit("push code to gitee repo Failed!") else: log.info("No change, nothing to commit!") - os.chdir(self.current_path) + return "nothing to push" + else: + log.error("%s not exist!" % self.obs_meta_path) + return -1 def sync_yaml_meta(self): """ integration of functions """ - self._git_clone("openeuler", "release-management") - self._git_clone("src_openeuler", "obs_meta") - change_file = self._get_change_file("release-management") + change_file = self._get_change_file() yaml_dict = {} for line in change_file: log.info("line:%s" % line) name = list(line.split())[1] - file_path = os.path.join(self.current_path, "release-management", name) + branch = name.split('/')[0] + file_path = os.path.join(self.release_management_path, name) yaml_dict = self._get_yaml_file_msg(file_path) if not yaml_dict: log.info("%s file content is empty!" % name) else: + flag = self._check_pckg_yaml(yaml_dict, branch) + if flag: + return -2 + else: + log.info("The contents of %s file is no problems." % file_path) msg, del_msg, prj_pkg = self._parse_yaml_msg(yaml_dict) for tmp in msg: - prj_meta_br = os.path.join(self.current_path, - "obs_meta/OBS_PRJ_meta", tmp['branch_to']) + prj_meta_br = os.path.join(self.obs_meta_path, "OBS_PRJ_meta/%s" + % tmp['branch_to']) if not os.path.exists(prj_meta_br): os.makedirs(prj_meta_br) prj_meta_path = os.path.join(prj_meta_br, tmp['obs_to']) @@ -275,8 +280,8 @@ class SyncPckgMgmt(object): for tmp in del_msg: self._del_pkg(tmp) self._verify_meta_file(prj_pkg) - obs_meta_path = os.path.join(self.current_path, "obs_meta") - self._push_code(obs_meta_path) + ret = self._push_code() + return ret if __name__ == "__main__": diff --git a/openeuler_obs.py b/openeuler_obs.py index 44a42dd..8266ef9 100644 --- a/openeuler_obs.py +++ b/openeuler_obs.py @@ -105,6 +105,8 @@ par.add_argument("-ca", "--cc_addr", default=None, help="cc's email", required=False) par.add_argument("-pm", "--pckg_mgmt", default=False, help="synchronize the obs_meta file according to the pckg-mgmt.yaml file", required=False) +par.add_argument("-rm", "--remt", default=None, + help="Local path of release-management repository.", required=False) par.add_argument("-a", "--ALL_", default=False, help="update all obs repo rpms", required=False) args = par.parse_args() @@ -147,7 +149,8 @@ kw = { "from_addr_pwd": args.from_addr_pwd, "cc_addr": args.cc_addr, "obs_mail_notice": args.obs_mail_notice, - "pckg_mgmt": args.pckg_mgmt + "pckg_mgmt": args.pckg_mgmt, + "release_management_path": args.remt } run = Runner(**kw) diff --git a/test/test_pckg_mgmt.py b/test/test_pckg_mgmt.py new file mode 100644 index 0000000..f83a475 --- /dev/null +++ b/test/test_pckg_mgmt.py @@ -0,0 +1,332 @@ +#/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: wangchong +# Create: 2021-06-15 +# ****************************************************************************** +""" +test all: python3 -m pytest -s test_pckg_mgmt.py +test one class: python3 -m pytest -s test_pckg_mgmt.py::TestCase +test one class one case: python3 -m pytest -s test_pckg_mgmt.py::TestCase::test_1 +""" +import os +import sys +import yaml +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.sync_pckg_mgmt import SyncPckgMgmt + + +S = SetEnv() +C = CommonFunc() +obs_meta_path = None +release_management_path = None +P = None + +def verify_result(yaml_path, meta_path, status): + with open(yaml_path, "r", encoding='utf-8') as f: + file_msg = yaml.load(f, Loader=yaml.FullLoader) + for title in file_msg['packages']: + for msg in file_msg['packages'][title]: + prj_meta_path = os.path.join(meta_path, "OBS_PRJ_meta", msg['branch_to'], msg['obs_to']) + prj_path = os.path.join(meta_path, msg['branch_to'], msg['obs_to']) + pkg_path = os.path.join(meta_path, msg['branch_to'], msg['obs_to'], msg['name'], "_service") + if status == -2: + if not os.path.exists(prj_meta_path): + assert True + else: + assert False, "%s meta file is exist" % msg['obs_to'] + else: + if os.path.exists(prj_meta_path): + assert True + else: + assert False, "%s meta file is not exist" % msg['obs_to'] + if title == "delete": + if not os.path.exists(pkg_path): + assert True + else: + assert False, "%s is exist, it's in delete" % pkg_path + else: + if os.path.exists(pkg_path): + assert True + else: + assert False, "%s is not exist" % pkg_path + +class TestCase(object): + def setup_class(self): + S.set_gitee() + global obs_meta_path + global release_management_path + global P + release_management_path = C.pull_from_gitee_repo(S.gitee_info["user"], S.gitee_info["passwd"], \ + "https://gitee.com/{0}/release-management".format(S.gitee_info["user"]), "master", "release-management") + obs_meta_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") + kw = {"obs_meta_path": obs_meta_path, + "release_management_path": release_management_path, + "gitee_user": S.gitee_info["user"], + "gitee_pwd": S.gitee_info["passwd"], + "pckg_mgmt": "true" + } + P = SyncPckgMgmt(**kw) + cmd = "python3 ../tools/create_pckg_mgmt_yaml.py -fp bringInRely -fb master -tp openEuler:20.99:LTS -tb openEuler-20.99-LTS" + if os.system(cmd) == 0: + assert True + else: + assert False, "fail to exec cmd:{0}".format(cmd) + + def teardown_class(self): + S.unset_gitee() + cmd = "rm -fr {0} {1}".format(obs_meta_path, release_management_path) + if os.system(cmd) == 0: + assert True + else: + assert False, "fail to exec cmd:{0}".format(cmd) + + def test_1(self): + """ + test for add a legal yaml + """ + assert os.path.exists(release_management_path), "{0} not exist".format(release_management_path) + assert os.path.exists("pckg-mgmt.yaml"), "pckg-mgmt.yaml not exist" + cmd = "mkdir -p {0}/openEuler-20.99-LTS && mv pckg-mgmt.yaml {1}".format( + release_management_path, os.path.join(release_management_path, "openEuler-20.99-LTS")) + if os.system(cmd) == 0: + assert True + else: + assert False, "fail to exec cmd:{0}".format(cmd) + C.commit_to_gitee_repo(release_management_path, "openEuler-20.99-LTS/pckg-mgmt.yaml") + ret = P.sync_yaml_meta() + if ret == -2: + assert False, "yaml file is not legal" + else: + assert True + yaml_path = os.path.join(release_management_path, "openEuler-20.99-LTS/pckg-mgmt.yaml") + verify_result(yaml_path, obs_meta_path, ret) + + def test_2(self): + """ + test for add a error yaml + """ + assert os.path.exists(release_management_path), "{0} not exist".format(release_management_path) + cmd = "mkdir -p {0}/openEuler-mytest".format(release_management_path) + if os.system(cmd) == 0: + assert True + else: + assert False, "fail to exec cmd:{0}".format(cmd) + msg = """packages: + natural: + - name: A-Tune + branch_from: master + branch_to: openEuler-test + obs_from: openEuler:Mainline + obs_to: openEuler:mytest + date: 2021-06-23-14-25-20 + recycle: [] + delete: [] +""" + cmd = "echo \"%s\" > %s" % (msg, os.path.join(release_management_path, "openEuler-mytest/pckg-mgmt.yaml")) + if os.system(cmd) == 0: + assert True + else: + assert False, "fail to exec cmd:{0}".format(cmd) + C.commit_to_gitee_repo(release_management_path, "openEuler-mytest/pckg-mgmt.yaml") + ret = P.sync_yaml_meta() + if ret == -2: + assert True + else: + assert False + yaml_path = os.path.join(release_management_path, "openEuler-mytest/pckg-mgmt.yaml") + verify_result(yaml_path, obs_meta_path, ret) + + def test_3(self): + """ + test for del a pkg + """ + assert os.path.exists(release_management_path), "{0} not exist".format(release_management_path) + yaml_path = os.path.join(release_management_path, "openEuler-20.99-LTS/pckg-mgmt.yaml") + assert os.path.exists(yaml_path), "{0} not exist".format(yaml_path) + cmd = "sed -n '3,8p' %s" % yaml_path + ret = os.popen(cmd).read() + if ret: + assert True + else: + assert False, "fail to exec cmd:{0}".format(cmd) + cmd = "sed -i '$s/.*/ delete:/g' %s" % yaml_path + if os.system(cmd) == 0: + assert True + else: + assert False, "fail to exec cmd:{0}".format(cmd) + cmd = "echo \"%s\" >> %s && sed -i '$d' %s" % (ret, yaml_path, yaml_path) + if os.system(cmd) == 0: + assert True + else: + assert False, "fail to exec cmd:{0}".format(cmd) + cmd = "sed -i '3,8d' %s" % yaml_path + if os.system(cmd) == 0: + assert True + else: + assert False, "fail to exec cmd:{0}".format(cmd) + C.commit_to_gitee_repo(release_management_path, "openEuler-20.99-LTS/pckg-mgmt.yaml") + res = P.sync_yaml_meta() + if res == 0: + assert True + else: + assert False, "fail to push code." + for msg in ret.split('\n'): + if "- name:" in msg: + name = msg.split(': ')[1] + if "branch_to" in msg: + branch = msg.split(': ')[1] + if "obs_to" in msg: + proj = msg.split(': ')[1] + assert os.path.exists(obs_meta_path), "{0} not exist".format(obs_meta_path) + prj_path = os.path.join(obs_meta_path, branch, proj) + pkg_path = os.path.join(prj_path, name, "_service") + if os.path.exists(prj_path): + if not os.path.exists(pkg_path): + assert True + else: + assert False, "fail to delete package:%s." % name + else: + assert False, "project:%s is not exist." % proj + + def test_4(self): + """ + test for add a pkg + """ + assert os.path.exists(release_management_path), "{0} not exist".format(release_management_path) + yaml_path = os.path.join(release_management_path, "openEuler-20.99-LTS/pckg-mgmt.yaml") + assert os.path.exists(yaml_path), "{0} not exist".format(yaml_path) + cmd = "tail -n6 %s" % yaml_path + result = os.popen(cmd).read().split('\n') + if result: + assert True + else: + assert False, "fail to exec cmd:{0}".format(cmd) + msg = [x for x in result if x!= ''] + cmd = "grep -n \" natural:\" {0}".format(yaml_path) + i = int(os.popen(cmd).read()[0]) + for ret in msg: + cmd = "sed -i '%s a\\%s' %s" % (i, ret, yaml_path) + i += 1 + if os.system(cmd) == 0: + assert True + else: + assert False, "fail to exec cmd:{0}".format(cmd) + cmd = "sed -n '$=' {0}".format(yaml_path) + ret = int(os.popen(cmd).read()) + if ret: + assert True + else: + assert False, "fail to exec cmd:{0}".format(cmd) + cmd = "sed -i '{0},{1}d' {2}".format(ret - 5, ret, yaml_path) + if os.system(cmd) == 0: + assert True + else: + assert False, "fail to exec cmd:{0}".format(cmd) + cmd = "sed -i '/ delete:/c\\ delete: []' {0}".format(yaml_path) + if os.system(cmd) == 0: + assert True + else: + assert False, "fail to exec cmd:{0}".format(cmd) + C.commit_to_gitee_repo(release_management_path, "openEuler-20.99-LTS/pckg-mgmt.yaml") + res = P.sync_yaml_meta() + if res == 0: + assert True + else: + assert False, "fail to push code." + for tmp in msg: + if "- name:" in tmp: + name = tmp.split(': ')[1] + if "branch_to" in tmp: + branch = tmp.split(': ')[1] + if "obs_to" in tmp: + proj = tmp.split(': ')[1] + assert os.path.exists(obs_meta_path), "{0} not exist".format(obs_meta_path) + prj_path = os.path.join(obs_meta_path, branch, proj) + pkg_path = os.path.join(prj_path, name, "_service") + if os.path.exists(prj_path): + if os.path.exists(pkg_path): + assert True + else: + assert False, "fail to add package:%s." % name + else: + assert False, "project:%s is not exist." % proj + + def test_5(self): + """ + test for rcycle a pkg + """ + assert os.path.exists(release_management_path), "{0} not exist".format(release_management_path) + yaml_path = os.path.join(release_management_path, "openEuler-20.99-LTS/pckg-mgmt.yaml") + assert os.path.exists(yaml_path), "{0} not exist".format(yaml_path) + cmd = "sed -n '3,8p' %s" % yaml_path + result = os.popen(cmd).read() + if result: + assert True + else: + assert False, "fail to exec cmd:{0}".format(cmd) + cmd = "sed -i '/recycle:/c\\ recycle:' {0}".format(yaml_path) + if os.system(cmd) == 0: + assert True + else: + assert False, "fail to exec cmd:{0}".format(cmd) + cmd = "sed -n '/delete:/,$p' {0}".format(yaml_path) + ret = os.popen(cmd).read() + if ret: + assert True + else: + assert False, "fail to exec cmd:{0}".format(cmd) + cmd = "sed -i '/delete:/,$d' {0}".format(yaml_path) + if os.system(cmd) == 0: + assert True + else: + assert False, "fail to exec cmd:{0}".format(cmd) + msg = result + ret + cmd = "echo \"{0}\" >> {1}".format(msg, yaml_path) + if os.system(cmd) == 0: + assert True + else: + assert False, "fail to exec cmd:{0}".format(cmd) + cmd = "sed -i '$d' {0} && sed -i '3,8d' {0}".format(yaml_path) + if os.system(cmd) == 0: + assert True + else: + assert False, "fail to exec cmd:{0}".format(cmd) + C.commit_to_gitee_repo(release_management_path, "openEuler-20.99-LTS/pckg-mgmt.yaml") + ret = P.sync_yaml_meta() + if ret == "nothing to push": + assert True + else: + assert False, "fail to move a package to recycle." + + def test_6(self): + """ + test for modify other file, not is pckg-mgmt.yaml + """ + assert os.path.exists(release_management_path), "{0} not exist".format(release_management_path) + yaml_path = os.path.join(release_management_path, "openEuler-20.99-LTS/pckg-mgmt.yaml") + assert os.path.exists(yaml_path), "{0} not exist".format(yaml_path) + cmd = "echo \"test\" > {0}".format(os.path.join(release_management_path, "openEuler-20.99-LTS/testfile")) + if os.system(cmd) == 0: + assert True + else: + assert False, "fail to exec cmd:{0}".format(cmd) + C.commit_to_gitee_repo(release_management_path, "openEuler-20.99-LTS/testfile") + ret = P.sync_yaml_meta() + if ret == "nothing to push": + assert True + else: + assert False, "fail to modify other file, not is pckg-mgmt.yaml." -- Gitee