Beispiel #1
0
class TestBasic(unittest.TestCase):
    def setUp(self):
        self.art = Artifactory(travis_env["ARTIFACTORY_URL"],
                               travis_env["ARTIFACTORY_USER"],
                               travis_env["ARTIFACTORY_PASSWORD"])
        self.repo_develop = self.art.create_repo("develop")
        self.travis = TravisMock()
        self.github = GithubMock(self.travis)
        try:
            self.repo_meta = self.art.create_repo("meta")
        except:
            meta = self.art.get_repo("meta")
            meta.remove()
            self.repo_meta = self.art.create_repo("meta")

    def tearDown(self):
        self.repo_develop.remove()
        repos = self.art.list_repos()
        for r in repos:
            if r.name.startswith("company_"):
                r.remove()

    def _store_meta(self, profiles, project_refs):
        for name, contents in profiles.items():
            self.repo_meta.deploy_contents("profiles/{}".format(name),
                                           contents)
        p_json = {"projects": project_refs}
        self.repo_meta.deploy_contents("projects.json", json.dumps(p_json))

    @staticmethod
    def _complete_ref(name):
        if "/" not in name:
            return "{}/1.0@conan/stable".format(name)
        return name

    def _complete_refs(self, tree):
        new_tree = {}
        for ref, reqs in tree.items():
            new_tree[self._complete_ref(ref)] = [
                self._complete_ref(r) for r in reqs
            ]
        return new_tree

    def get_slug(self, name):
        slug = "company/{}".format(name)
        return slug

    def create_gh_repo(self, tree, ref, upload_recipe=True):
        name, version = ref.split("@")[0].split("/")
        slug = self.get_slug(name)
        if self.github.repos.get(slug):
            return
        rand = uuid.uuid4()
        reqs = tree.get(ref, [])
        reqs_str = ",".join('"{}"'.format(r) for r in reqs)
        reqs_line = 'requires = {}'.format(reqs_str) if reqs else ""
        cf = conanfile.format(name, version, reqs_line, rand)
        files = {
            "conanfile.py": cf,
            "myfile.txt": "Original content: {}".format(ref)
        }

        # Register the repo on Github
        repo = self.github.create_repository(slug, files)

        # Register the repo on travis
        self.travis.register_env_vars(slug, travis_env)

        def main_action():
            """
            This simulates the yml script of a repository of a library
            :return:
            """
            ci_adapter = TravisCIAdapter()
            ci_caller = TravisAPICallerMultiThreadMock(self.travis)
            main_job = MainJob(ci_adapter, ci_caller)
            with environment_append({"CONAN_USER_HOME": os.getcwd()}):
                main_job.run()

        self.travis.register_repo(slug, repo, main_action)

        tmp = tempfile.mkdtemp()
        for name, contents in files.items():
            path = os.path.join(tmp, name)
            with open(path, "w") as f:
                f.write(contents)

        if upload_recipe:
            with environment_append({
                    "CONAN_USER_HOME":
                    tmp,
                    "CONAN_REVISIONS_ENABLED":
                    "1",
                    "CONAN_LOGIN_USERNAME":
                    travis_env["CONAN_LOGIN_USERNAME"],
                    "CONAN_PASSWORD":
                    travis_env["CONAN_PASSWORD"],
                    "CONAN_NON_INTERACTIVE":
                    "1"
            }):
                with chdir(tmp):
                    run_command("conan remote add develop {}".format(
                        self.repo_develop.url))
                    run_command("conan export . {}".format(ref))
                    run_command(
                        "conan upload {} -r develop -c --all".format(ref))

        for req in reqs:
            self.create_gh_repo(tree, req, upload_recipe=upload_recipe)

    def register_build_repo(self):
        slug = "company/build_node"
        repo = self.github.create_repository(slug, {"foo": "bar"})
        repo.checkout_copy("master")  # By default the mock creates develop

        def action():
            """
            This simulates the yml script of a repository of a library
            :return:
            """
            main_job = BuildPackageJob()
            with environment_append({"CONAN_USER_HOME": os.getcwd()}):
                main_job.run()

        self.travis.register_env_vars(slug, travis_env)
        self.travis.register_repo(slug, repo, action)

    def test_basic(self):
        projects = ["P1", "P2"]
        profiles = {
            "linux_gcc7_64": linux_gcc7_64,
            "linux_gcc7_32": linux_gcc7_32
        }
        tree = {
            "P1": ["FF", "CC", "DD"],
            "P2": ["FF"],
            "CC": ["BB"],
            "DD": ["BB"],
            "BB": ["AA"],
            "FF": ["AA"],
            "AA": []
        }
        """tree = {"P1": ["AA"],
                "P2": ["AA"]}"""

        tree = self._complete_refs(tree)
        projects = [self._complete_ref(p) for p in projects]

        self._store_meta(profiles, projects)

        for p in projects:
            self.create_gh_repo(tree, p, upload_recipe=True)

        # Register a repo in travis that will be the one building single jobs
        self.register_build_repo()

        # Create a branch on AA an open pull request
        repo = self.github.repos[self.get_slug("AA")]
        repo.checkout_copy("feature/cool1")
        message_commit = "Here we go!"
        repo.commit_files({"myfile.txt": "Modified myfile: pepe"},
                          message_commit)

        # Generate binary for EE merging the PR
        # pr_number = self.github.open_pull_request("lasote/EE", "company/EE")
        # self.github.merge_pull_request(pr_number)

        # We don't use forks because the env vars wouldn't be available
        pr_number = self.github.open_pull_request("company/AA", "develop",
                                                  "company/AA",
                                                  "feature/cool1")

        self.github.merge_pull_request(pr_number)
        # TODO: asserts
        print("Breakpoint")
class BaseTest(unittest.TestCase):

    repo_meta: MetaRepo

    def init(self,
             pre_develop_repo_name,
             develop_repo_name,
             meta_repo_name,
             slug_prefix="company",
             arti_url=None,
             arti_user=None,
             arti_password=None):

        self.slug_prefix = slug_prefix
        self.arti_url = arti_url or "http://*****:*****@")[0].split("/")[0]
        return "{}/{}".format(self.slug_prefix, name)

    def tearDown(self):
        self.repo_develop.remove()
        self.repo_pre_develop.remove()
        repos = self.art.list_repos()
        for r in repos:
            if r.name.startswith("{}_".format(self.slug_prefix)):
                r.remove()

    def get_travis_env(self):
        travis_env = {
            "CONAN_LOGIN_USERNAME": self.arti_user,
            "CONAN_PASSWORD": self.arti_password,
            "ARTIFACTORY_URL": self.arti_url,
            "ARTIFACTORY_USER": self.arti_user,
            "ARTIFACTORY_PASSWORD": os.getenv("ARTIFACTORY_PASSWORD",
                                              "password"),
            "CONAN_REVISIONS_ENABLED": os.getenv("CONAN_REVISIONS_ENABLED",
                                                 "1")
        }

        return travis_env

    def populate_meta_repo(self, profiles, project_refs):
        for name, contents in profiles.items():
            self.repo_meta.deploy_contents("profiles/{}".format(name),
                                           contents)
        p_json = {
            "projects": project_refs,
            "repos_branches": {
                "develop": self.develop_repo_name
            }
        }
        self.repo_meta.deploy_contents("config.json", json.dumps(p_json))

    @staticmethod
    def _complete_ref(name):
        if "/" not in name:
            return "{}/1.0@conan/stable".format(name)
        return name

    def _complete_refs(self, tree):
        new_tree = {}
        for ref, reqs in tree.items():
            new_tree[self._complete_ref(ref)] = [
                self._complete_ref(r) for r in reqs
            ]
        return new_tree

    def register_build_repo(self):
        slug = "company/build_node"
        repo = self.github.create_repository(slug, {"foo": "bar"})
        repo.checkout_copy("master")  # By default the mock creates develop

        def action():
            """
            This simulates the yml script of a repository of a library
            :return:
            """
            main_job = ConanCreateJob()
            with environment_append({"CONAN_USER_HOME": os.getcwd()}):
                main_job.run()

        self.travis.register_env_vars(slug, self.get_travis_env())
        self.travis.register_repo(slug, repo, action)

    def create_gh_repo(self, slug, files):
        if self.github.repos.get(slug):
            return

        # Register the repo on Github
        repo = self.github.create_repository(slug, files)

        # Register the repo on travis
        self.travis.register_env_vars(slug, self.get_travis_env())

        def main_action():
            """
            This simulates the yml script of a repository of a library
            :return:
            """
            ci_adapter = TravisCIAdapter()
            # TravisAPICallerMock(self.travis)
            ci_caller = TravisAPICallerMultiThreadMock(self.travis)
            art = Artifactory(os.getenv("ARTIFACTORY_URL"),
                              os.getenv("ARTIFACTORY_USER"),
                              os.getenv("ARTIFACTORY_PASSWORD"))
            repos = ReposBuild(art.get_repo(self.develop_repo_name),
                               art.get_repo(self.pre_develop_repo_name),
                               art.get_repo(self.meta_repo_name).as_meta())
            main_job = CoordinatorJob(ci_adapter, ci_caller, self.logger,
                                      repos)
            with environment_append({"CONAN_USER_HOME": os.getcwd()}):
                main_job.run()

        self.travis.register_repo(slug, repo, main_action)

        tmp = tempfile.mkdtemp()
        for name, contents in files.items():
            path = os.path.join(tmp, name)
            with open(path, "w") as f:
                f.write(contents)

        return tmp

    def run_conan_commands(self, commands, package_id_mode, folder=None):
        output = ""
        tmp = folder or tempfile.mkdtemp()

        with environment_append({
                "CONAN_USER_HOME": tmp,
                "CONAN_REVISIONS_ENABLED": "1",
                "CONAN_LOGIN_USERNAME": self.arti_user,
                "CONAN_PASSWORD": self.arti_password,
                "CONAN_NON_INTERACTIVE": "1"
        }):
            with chdir(tmp):
                run("conan config set general.default_package_id_mode={}".
                    format(package_id_mode))
                for c in commands:
                    output += run(c)

        return tmp, output