Exemple #1
0
 def remove_readme_file(self):
     with SetDirectory(Path(self.temp_dir.name)):
         path = f'{self.chart_directory}/{self.secrets.chart_version}/src/README.md'
         try:
             os.remove(path)
         except Exception as e:
             pytest.fail(f"Failed to remove readme file : {e}")
Exemple #2
0
 def process_chart(self, is_tarball: bool):
     with SetDirectory(Path(self.temp_dir.name)):
         if is_tarball:
             # Copy the chart tar into temporary directory for PR submission
             chart_tar = self.secrets.test_chart.split('/')[-1]
             shutil.copyfile(f'{self.old_cwd}/{self.secrets.test_chart}',
                             f'{self.chart_directory}/{self.secrets.chart_version}/{chart_tar}')
         else:
             # Unzip files into temporary directory for PR submission
             extract_chart_tgz(self.secrets.test_chart, f'{self.chart_directory}/{self.secrets.chart_version}', self.secrets, logging)
Exemple #3
0
    def process_all_charts(self):
        self.setup_git_context(self.repo)
        self.setup_temp_dir()

        owners_table = dict()
        pr_number_list = list()

        # Process test charts and send PRs from temporary directory
        with SetDirectory(Path(self.temp_dir.name)):
            for vendor_type, vendor_name, chart_name, chart_version in self.secrets.submitted_charts:
                self.process_single_chart(vendor_type, vendor_name, chart_name, chart_version, pr_number_list, owners_table)

        for vendor_type, vendor_name, chart_name, chart_version, pr_number in pr_number_list:
            self.check_single_chart_result(vendor_type, vendor_name, chart_name, chart_version, pr_number, owners_table)
Exemple #4
0
    def setup_temp_dir(self):
        self.temp_dir = TemporaryDirectory(prefix='tci-')
        with SetDirectory(Path(self.temp_dir.name)):
            # Make PR's from a temporary directory
            logging.info(f'Worktree directory: {self.temp_dir.name}')
            self.repo.git.worktree('add', '--detach', self.temp_dir.name, f'HEAD')
            self.temp_repo = git.Repo(self.temp_dir.name)

            self.set_git_username_email(self.temp_repo, self.secrets.bot_name, GITHUB_ACTIONS_BOT_EMAIL)
            self.temp_repo.git.checkout('-b', self.secrets.base_branch)
            pathlib.Path(
                f'{self.chart_directory}/{self.secrets.chart_version}').mkdir(parents=True, exist_ok=True)

            self.remove_chart(self.chart_directory, self.secrets.chart_version, self.secrets.test_repo, self.secrets.base_branch, self.secrets.bot_token)
            self.remove_owners_file(self.chart_directory, self.secrets.test_repo, self.secrets.base_branch, self.secrets.bot_token)
Exemple #5
0
    def update_chart_version_in_chart_yaml(self, new_version):
        with SetDirectory(Path(self.temp_dir.name)):
            path = f'{self.chart_directory}/{self.secrets.chart_version}/src/Chart.yaml'
            with open(path, 'r') as fd:
                try:
                    chart = yaml.safe_load(fd)
                except yaml.YAMLError as err:
                    pytest.fail(f"error parsing '{path}': {err}")
            current_version = chart['version']

            if current_version != new_version:
                chart['version'] = new_version
                try:
                    with open(path, 'w') as fd:
                        fd.write(yaml.dump(chart))
                except Exception as e:
                    pytest.fail("Failed to update version in yaml file")
Exemple #6
0
    def create_and_push_owners_file(self, chart_directory, base_branch, vendor_name, vendor_type, chart_name, provider_delivery=False):
        with SetDirectory(Path(self.temp_dir.name)):
            # Create the OWNERS file from the string template
            values = {'bot_name': self.secrets.bot_name,
                    'vendor': vendor_name, 'chart_name': chart_name,
                      "provider_delivery" : provider_delivery}
            content = Template(self.secrets.owners_file_content).substitute(values)
            with open(f'{chart_directory}/OWNERS', 'w') as fd:
                fd.write(content)

            # Push OWNERS file to the test_repo
            logging.info(
                f"Push OWNERS file to '{self.secrets.test_repo}:{base_branch}'")
            self.temp_repo.git.add(f'{chart_directory}/OWNERS')
            self.temp_repo.git.commit(
                '-m', f"Add {vendor_type} {vendor_name} {chart_name} OWNERS file")
            self.temp_repo.git.push(f'https://*****:*****@github.com/{self.secrets.test_repo}',
                        f'HEAD:refs/heads/{base_branch}', '-f')
Exemple #7
0
    def setup_temp_dir(self):
        self.temp_dir = TemporaryDirectory(prefix='tci-')
        with SetDirectory(Path(self.temp_dir.name)):
            # Make PR's from a temporary directory
            logging.info(f'Worktree directory: {self.temp_dir.name}')
            self.repo.git.worktree('add', '--detach', self.temp_dir.name, f'HEAD')
            self.temp_repo = git.Repo(self.temp_dir.name)

            # Run submission flow test with charts in PROD_REPO:PROD_BRANCH
            self.set_git_username_email(self.temp_repo, self.secrets.bot_name, GITHUB_ACTIONS_BOT_EMAIL)
            self.temp_repo.git.fetch(
                f'https://github.com/{PROD_REPO}.git', f'{PROD_BRANCH}:{PROD_BRANCH}', '-f')
            self.temp_repo.git.checkout(PROD_BRANCH, 'charts')
            self.temp_repo.git.restore('--staged', 'charts')
            self.secrets.submitted_charts = get_all_charts(
                'charts', self.secrets.vendor_type)
            logging.info(
                f"Found charts for {self.secrets.vendor_type}: {self.secrets.submitted_charts}")
            self.temp_repo.git.checkout('-b', 'tmp')
Exemple #8
0
 def add_non_chart_related_file(self):
     with SetDirectory(Path(self.temp_dir.name)):
         path = f'{self.chart_directory}/Notes.txt'
         with open(path, 'w') as fd:
             fd.write("This is a test file")
Exemple #9
0
    def process_report(self, update_chart_sha=False, update_url=False, url=None,
                       update_versions=False,supported_versions=None,tested_version=None,kube_version=None,
                       update_provider_delivery=False, provider_delivery=False, missing_check=None):

        with SetDirectory(Path(self.temp_dir.name)):
            # Copy report to temporary location and push to test_repo:pr_branch
            logging.info(
                f"Push report to '{self.secrets.test_repo}:{self.secrets.pr_branch}'")
            tmpl = open(self.secrets.test_report).read()
            values = {'repository': self.secrets.test_repo,
                    'branch': self.secrets.base_branch}
            content = Template(tmpl).substitute(values)

            report_path = f'{self.chart_directory}/{self.secrets.chart_version}/' + self.secrets.test_report.split('/')[-1]

            with open(report_path, 'w') as fd:
                fd.write(content)

            if update_chart_sha or update_url or update_versions or update_provider_delivery:

                with open(report_path, 'r') as fd:
                    try:
                        report = yaml.safe_load(fd)
                    except yaml.YAMLError as err:
                        pytest.fail(f"error parsing '{report_path}': {err}")

                #For updating the report.yaml, for chart sha mismatch scenario
                if update_chart_sha:
                    new_sha_value = 'sha256:5b85ae00b9ca2e61b2d70a59f98fd72136453b1a185676b29d4eb862981c1xyz'
                    logging.info(f"Current SHA Value in report: {report['metadata']['tool']['digests']['chart']}")
                    report['metadata']['tool']['digests']['chart'] = new_sha_value
                    logging.info(f"Updated sha value in report: {new_sha_value}")

                #For updating the report.yaml, for invalid_url sceanrio
                if update_url:
                    logging.info(f"Current chart-uri in report: {report['metadata']['tool']['chart-uri']}")
                    report['metadata']['tool']['chart-uri'] = url
                    logging.info(f"Updated chart-uri value in report: {url}")

                if update_versions:
                    report['metadata']['tool']['testedOpenShiftVersion'] = tested_version
                    report['metadata']['tool']['supportedOpenShiftVersions'] = supported_versions
                    report['metadata']['chart']['kubeversion'] = kube_version
                    logging.info(f"Updated testedOpenShiftVersion value in report: {tested_version}")
                    logging.info(f"Updated supportedOpenShiftVersions value in report: {supported_versions}")
                    logging.info(f"Updated kubeversion value in report: {kube_version}")

                if update_provider_delivery:
                    report['metadata']['tool']['providerControlledDelivery'] = provider_delivery

                with open(report_path, 'w') as fd:
                    try:
                        fd.write(yaml.dump(report))
                        logging.info("Report updated with new values")
                    except Exception as e:
                        pytest.fail("Failed to update report yaml with new values")            

            #For removing the check for missing check scenario
            if missing_check:
                logging.info(f"Updating report with {missing_check}")
                with open(report_path, 'r+') as fd:
                    report_content = yaml.safe_load(fd)
                    results = report_content["results"]
                    new_results = filter(lambda x: x['check'] != missing_check, results)
                    report_content["results"] = list(new_results)
                    fd.seek(0)
                    yaml.dump(report_content, fd)
                    fd.truncate()

        self.temp_repo.git.add(report_path)
        self.temp_repo.git.commit(
                '-m', f"Add {self.secrets.vendor} {self.secrets.chart_name} {self.secrets.chart_version} report")
        self.temp_repo.git.push(f'https://*****:*****@github.com/{self.secrets.test_repo}',
                f'HEAD:refs/heads/{self.secrets.pr_branch}', '-f')