def test_fetch_artifacts_with_task_with_null_artifacts(self):
        revision = "a22"
        build_id = "project_variant_patch_a22_date"
        build = MagicMock(id=build_id)

        task_response = MagicMock(status="failure", display_name="archive_dist_test")
        task_response.is_success.return_value = False
        build.get_tasks.return_value = [task_response]

        with self.assertRaises(ValueError):
            under_test.fetch_artifacts(build, revision)
    def test_fetch_artifacts_with_task_with_null_artifacts(self):
        revision = "a22"
        build_id = "project_variant_patch_a22_date"

        task_response = MagicMock(status="failure")
        task_response.is_success.return_value = False
        evergreen_api = MagicMock()
        evergreen_api.task_by_id.return_value = task_response

        with self.assertRaises(ValueError):
            under_test.fetch_artifacts(evergreen_api, build_id, revision)
    def test_fetch_artifacts_with_task_with_no_artifacts(self):
        revision = "a22"
        build_id = "project_variant_patch_a22_date"
        build = MagicMock(id=build_id)

        artifacts_mock = []

        task_response = MagicMock(status="success", display_name="archive_dist_test")
        task_response.artifacts = artifacts_mock
        build.get_tasks.return_value = [task_response]

        artifact_files = under_test.fetch_artifacts(build, revision)
        self.assertEqual(len(artifact_files), 0)
    def test_fetch_artifacts_with_task_with_no_artifacts(self):
        revision = "a22"
        build_id = "project_variant_patch_a22_date"

        artifacts_mock = []

        task_response = MagicMock(status="success")
        task_response.artifacts = artifacts_mock
        evergreen_api = MagicMock()
        evergreen_api.task_by_id.return_value = task_response

        artifact_files = under_test.fetch_artifacts(evergreen_api, build_id,
                                                    revision)
        self.assertEqual(len(artifact_files), 0)
    def test_fetch_artifacts_with_task_with_artifact(self):
        revision = "a22"
        build_id = "project_variant_patch_a22_date"
        build = MagicMock(id=build_id)

        artifact_mock = MagicMock()
        artifact_mock.name = "Binaries"
        artifact_mock.url = "http://s3.amazon.com/mciuploads/mongodb/build_var//binaries/mongo-test.tgz"
        artifacts_mock = [artifact_mock]

        task_response = MagicMock(status="success", display_name="archive_dist_test")
        task_response.artifacts = artifacts_mock
        build.get_tasks.return_value = [task_response]

        artifact_files = under_test.fetch_artifacts(build, revision)
        expected_file = {
            "name": artifact_mock.name,
            "link": artifact_mock.url,
            "visibility": "private",
        }
        self.assertIn(expected_file, artifact_files)
    def test_fetch_artifacts_with_task_with_artifact(self):
        revision = "a22"
        build_id = "project_variant_patch_a22_date"

        artifact_mock = MagicMock()
        artifact_mock.name = "Binaries"
        artifact_mock.url = "http://s3.amazon.com/mciuploads/mongodb/build_var//binaries/mongo-test.tgz"
        artifacts_mock = [artifact_mock]

        task_response = MagicMock(status="success")
        task_response.artifacts = artifacts_mock
        evergreen_api = MagicMock()
        evergreen_api.task_by_id.return_value = task_response

        artifact_files = under_test.fetch_artifacts(evergreen_api, build_id,
                                                    revision)
        expected_file = {
            "name": artifact_mock.name,
            "link": artifact_mock.url,
            "visibility": "private",
        }
        self.assertIn(expected_file, artifact_files)
def main(  # pylint: disable=too-many-arguments,too-many-locals
        project, build_variant, revision, out_file, version_id, json_artifact):
    """
    Create a file with expansions that can be used to bypass compile.

    This is used for dynamically generated build variants that can use a base build variants
    compile artifacts to run against.
    \f

    :param project: The evergreen project.
    :param build_variant: The build variant whose artifacts we want to use.
    :param revision: The base revision being run against.
    :param out_file: File to write expansions to.
    :param version_id: Evergreen version id being run against.
    """
    logging.basicConfig(
        format="[%(asctime)s - %(name)s - %(levelname)s] %(message)s",
        level=logging.DEBUG,
        stream=sys.stdout,
    )

    evg_api = RetryingEvergreenApi.get_api(config_file=EVG_CONFIG_FILE)

    version = evg_api.version_by_id(version_id)
    build_id = _retrieve_used_build_id(version.build_by_variant(build_variant))
    artifacts = fetch_artifacts(evg_api, build_id, revision)

    write_out_artifacts(json_artifact, artifacts)

    LOGGER.info("Creating expansions files",
                project=project,
                build_variant=build_variant,
                revision=revision,
                build_id=build_id)

    expansions = generate_bypass_expansions(project, build_variant, revision,
                                            build_id)
    write_out_bypass_compile_expansions(out_file, **expansions)