예제 #1
0
async def test_issue_2361(environment_factory: EnvironmentFactory, server,
                          client, tmpdir):
    env = await environment_factory.create_environment(main="")

    # Change the branch of the environment to a non-existing branch as such that the run method
    # of the CompileRun returns after executing the clone stage.
    result = await client.environment_modify(id=env.id,
                                             name=env.id,
                                             branch="non-existing-branch")
    assert result.code == 200

    project_work_dir = os.path.join(tmpdir, "work")
    ensure_directory_exist(project_work_dir)

    compile = data.Compile(
        remote_id=uuid.uuid4(),
        environment=env.id,
        do_export=True,
        metadata={},
        environment_variables={},
        force_update=True,
    )
    await compile.insert()

    cr = CompileRun(compile, project_work_dir)

    # This should not result in a "local variable referenced before assignment" exception
    success, compile_data = await cr.run()
    assert not success
    assert compile_data is None
예제 #2
0
async def test_git_uses_environment_variables(
        environment_factory: EnvironmentFactory, server, client, tmpdir,
        monkeypatch):
    """
    Make sure that the git clone command on the compilerservice takes into account the environment variables
    set on the system.
    """
    env = await environment_factory.create_environment(main="")
    result = await client.environment_modify(id=env.id,
                                             name=env.id,
                                             branch="master")
    assert result.code == 200

    project_work_dir = os.path.join(tmpdir, "work")
    ensure_directory_exist(project_work_dir)

    compile = data.Compile(
        remote_id=uuid.uuid4(),
        environment=env.id,
        do_export=True,
        metadata={},
        environment_variables={},
        force_update=True,
    )
    await compile.insert()

    # Make sure git clone logs trace lines
    monkeypatch.setenv("GIT_TRACE", "1")
    cr = CompileRun(compile, project_work_dir)
    await cr.run()

    report = await data.Report.get_one(compile=compile.id,
                                       name="Cloning repository")
    # Assert presence of trace lines
    assert "trace: " in report.errstream
예제 #3
0
    async def compile_and_assert(env,
                                 export=True,
                                 meta={},
                                 env_vars={},
                                 update=False):
        compile = data.Compile(
            remote_id=uuid.uuid4(),
            environment=env.id,
            do_export=export,
            metadata=meta,
            environment_variables=env_vars,
            force_update=update,
        )
        await compile.insert()

        # compile with export
        cr = CompileRun(compile, project_work_dir)
        await cr.run()

        # get and process reports
        result = await client.get_report(compile.id)
        assert result.code == 200
        stages = result.result["report"]["reports"]
        if export:
            assert cr.version > 0
            result = await client.get_version(env.id, cr.version)
            assert result.code == 200
            metadata = result.result["model"]["version_info"][
                "export_metadata"]
            for k, v in meta.items():
                assert k in metadata
                assert v == metadata[k]

        print(stages)

        stage_by_name = {stage["name"]: stage for stage in stages}

        return cr, stage_by_name
예제 #4
0
    async def request_recompile(
        self,
        env: data.Environment,
        force_update: bool,
        do_export: bool,
        remote_id: uuid.UUID,
        metadata: JsonType = {},
        env_vars: Dict[str, str] = {},
    ) -> Tuple[Optional[uuid.UUID], Warnings]:
        """
        Recompile an environment in a different thread and taking wait time into account.

        :return: the compile id of the requested compile and any warnings produced during the request
        """
        server_compile: bool = await env.get(data.SERVER_COMPILE)
        if not server_compile:
            LOGGER.info(
                "Skipping compile because server compile not enabled for this environment."
            )
            return None, [
                "Skipping compile because server compile not enabled for this environment."
            ]

        requested = datetime.datetime.now().astimezone()

        compile = data.Compile(
            environment=env.id,
            requested=requested,
            remote_id=remote_id,
            do_export=do_export,
            force_update=force_update,
            metadata=metadata,
            environment_variables=env_vars,
        )
        await compile.insert()
        await self._queue(compile)
        return compile.id, None
예제 #5
0
async def env_with_compiles(client, environment):
    compile_requested_timestamps = []
    compiles = []
    # Make sure that timestamp is never older than 7 days,
    # as such that the cleanup service doesn't delete them.
    now = datetime.datetime.now()
    for i in range(4):
        requested = now + datetime.timedelta(minutes=i)
        compile_requested_timestamps.append(requested)
        compile = data.Compile(
            id=uuid.uuid4(),
            remote_id=uuid.uuid4(),
            environment=uuid.UUID(environment),
            requested=requested,
            started=requested + datetime.timedelta(seconds=20),
            completed=requested + datetime.timedelta(seconds=40),
            do_export=True,
            force_update=False,
            metadata={"meta": 42} if i % 2 else None,
            environment_variables={"TEST_ENV_VAR": True} if i % 2 else None,
            success=True,
            handled=True,
            version=1,
            substitute_compile_id=None,
            compile_data={
                "errors": [{
                    "type": "UnexpectedException",
                    "message": "msg"
                }]
            } if i % 2 else None,
        )
        compiles.append(compile)
    compiles[1].substitute_compile_id = compiles[0].id
    compiles[2].substitute_compile_id = compiles[1].id
    for compile in compiles:
        await compile.insert()
    ids = [compile.id for compile in compiles]

    await Report(
        id=uuid.uuid4(),
        started=datetime.datetime.now(),
        completed=datetime.datetime.now(),
        command="inmanta export",
        name="name",
        errstream="error",
        outstream="success",
        returncode=0,
        compile=ids[0],
    ).insert()
    await Report(
        id=uuid.uuid4(),
        started=datetime.datetime.now(),
        completed=datetime.datetime.now(),
        command="inmanta export",
        name="another_name",
        errstream="error",
        outstream="success",
        returncode=0,
        compile=ids[0],
    ).insert()

    return environment, ids, compile_requested_timestamps