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
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
async def test_compile_runner(environment_factory: EnvironmentFactory, server, client, tmpdir): testmarker_env = "TESTMARKER" no_marker = "__no__marker__" marker_print = "_INM_MM:" marker_print2 = "_INM_MM2:" marker_print3 = "_INM_MM3:" def make_main(marker_print): return f""" marker = std::get_env("{testmarker_env}","{no_marker}") std::print("{marker_print} {{{{marker}}}}") host = std::Host(name="test", os=std::linux) std::ConfigFile(host=host, path="/etc/motd", content="1234") """ env = await environment_factory.create_environment(make_main(marker_print)) env2 = await environment_factory.create_environment( make_main(marker_print2)) project_work_dir = os.path.join(tmpdir, "work") ensure_directory_exist(project_work_dir) 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 # with export compile, stages = await compile_and_assert(env, True, meta={"type": "Test"}) assert stages["Init"]["returncode"] == 0 assert stages["Cloning repository"]["returncode"] == 0 assert stages["Creating venv"]["returncode"] == 0 assert stages["Installing modules"]["returncode"] == 0 assert stages["Recompiling configuration model"]["returncode"] == 0 out = stages["Recompiling configuration model"]["outstream"] assert f"{marker_print} {no_marker}" in out assert len(stages) == 5 assert compile.version is not None # no export compile, stages = await compile_and_assert(env, False) assert stages["Init"]["returncode"] == 0 assert stages["Recompiling configuration model"]["returncode"] == 0 out = stages["Recompiling configuration model"]["outstream"] assert f"{marker_print} {no_marker}" in out assert len(stages) == 2 assert compile.version is None # env vars marker = str(uuid.uuid4()) compile, stages = await compile_and_assert( env, False, env_vars={testmarker_env: marker}) assert len(compile.request.environment_variables) == 1 assert stages["Init"]["returncode"] == 0 assert f"Using extra environment variables during compile TESTMARKER='{marker}'" in stages[ "Init"]["outstream"] assert stages["Recompiling configuration model"]["returncode"] == 0 out = stages["Recompiling configuration model"]["outstream"] assert f"{marker_print} {marker}" in out assert len(stages) == 2 assert compile.version is None # switch branch compile, stages = await compile_and_assert(env2, False) assert stages["Init"]["returncode"] == 0 assert stages[ f"Switching branch from {env.repo_branch} to {env2.repo_branch}"][ "returncode"] == 0 assert stages["Installing modules"]["returncode"] == 0 assert stages["Recompiling configuration model"]["returncode"] == 0 out = stages["Recompiling configuration model"]["outstream"] assert f"{marker_print2} {no_marker}" in out assert len(stages) == 4 assert compile.version is None # update with no update compile, stages = await compile_and_assert(env2, False, update=True) assert stages["Init"]["returncode"] == 0 assert stages["Pulling updates"]["returncode"] == 0 assert stages["Updating modules"]["returncode"] == 0 assert stages["Recompiling configuration model"]["returncode"] == 0 out = stages["Recompiling configuration model"]["outstream"] assert f"{marker_print2} {no_marker}" in out assert len(stages) == 4 assert compile.version is None environment_factory.write_main(make_main(marker_print3)) compile, stages = await compile_and_assert(env2, False, update=True) assert stages["Init"]["returncode"] == 0 assert stages["Pulling updates"]["returncode"] == 0 assert stages["Updating modules"]["returncode"] == 0 assert stages["Recompiling configuration model"]["returncode"] == 0 out = stages["Recompiling configuration model"]["outstream"] assert f"{marker_print3} {no_marker}" in out assert len(stages) == 4 assert compile.version is None # Ensure that the pip binary created in the venv of the compiler service works correctly pip_binary_path = os.path.join(project_work_dir, ".env", "bin", "pip") output = subprocess.check_output( [pip_binary_path, "list", "--format", "json"], encoding="utf-8") assert "inmanta-core" in output
async def prestart(self, server: server.protocol.Server) -> None: await super(CompilerService, self).prestart(server) state_dir: str = opt.state_dir.get() server_state_dir = ensure_directory_exist(state_dir, "server") self._env_folder = ensure_directory_exist(server_state_dir, "environments")