async def test_scheduler_stop(caplog): sched = util.Scheduler("stop") i = [] async def action(): i.append(0) return "A" sched.add_action(action, 0.05, 0) while len(i) == 0: await asyncio.sleep(0.01) sched.stop() length = len(i) await asyncio.sleep(0.1) assert len(i) == length no_error_in_logs(caplog) caplog.clear() sched.add_action(action, 0.05, 0) assert "Scheduling action 'action', while scheduler is stopped" in caplog.messages assert not sched._executing_tasks[action]
def test_CRUD_handler_purged_response(purged_desired, purged_actual, excn, create, delete, updated, caplog): """ purged_actual and excn are conceptually equivalent, this test case serves to prove that they are in fact, equivalent """ caplog.set_level(logging.DEBUG) class DummyCrud(CRUDHandler): def __init__(self): self.updated = False self.created = False self.deleted = False def read_resource(self, ctx: HandlerContext, resource: resources.PurgeableResource) -> None: resource.purged = purged_actual if updated: resource.value = "b" if excn: raise ResourcePurged() def update_resource(self, ctx: HandlerContext, changes: dict, resource: resources.PurgeableResource) -> None: self.updated = True def create_resource(self, ctx: HandlerContext, resource: resources.PurgeableResource) -> None: self.created = True def delete_resource(self, ctx: HandlerContext, resource: resources.PurgeableResource) -> None: self.deleted = True @resource("aa::Aa", "aa", "aa") class TestResource(PurgeableResource): fields = ("value", ) res = TestResource(Id("aa::Aa", "aa", "aa", "aa", 1)) res.purged = purged_desired res.value = "a" ctx = HandlerContext(res, False) handler = DummyCrud() handler.execute(ctx, res, False) assert handler.updated == ((not (create or delete)) and updated and not purged_desired) assert handler.created == create assert handler.deleted == delete no_error_in_logs(caplog) log_contains(caplog, "inmanta.agent.handler", logging.DEBUG, "resource aa::Aa[aa,aa=aa],v=1: Calling read_resource")
async def test_scheduler_remove(caplog): sched = util.Scheduler("remove") i = [] async def action(): i.append(0) sched.add_action(action, 0.05, 0) while len(i) == 0: await asyncio.sleep(0.01) sched.remove(action) length = len(i) await asyncio.sleep(0.1) assert len(i) == length no_error_in_logs(caplog)
async def test_get_facts(resource_container, client, clienthelper, environment, agent, caplog): """ Test retrieving facts from the agent """ env_id = environment resource_container.Provider.set("agent1", "key", "value") version = await clienthelper.get_version() resource_id_wov = "test::Resource[agent1,key=key]" resource_id = "%s,v=%d" % (resource_id_wov, version) resources = [{ "key": "key", "value": "value", "id": resource_id, "requires": [], "purged": False, "send_event": False }] await clienthelper.put_version_simple(resources, version) result = await client.release_version( env_id, version, True, const.AgentTriggerMethod.push_full_deploy) assert result.code == 200 result = await client.get_param(env_id, "length", resource_id_wov) assert result.code == 503 env_uuid = uuid.UUID(env_id) params = await data.Parameter.get_list(environment=env_uuid, resource_id=resource_id_wov) while len(params) < 3: params = await data.Parameter.get_list(environment=env_uuid, resource_id=resource_id_wov) await asyncio.sleep(0.1) result = await client.get_param(env_id, "key1", resource_id_wov) assert result.code == 200 no_error_in_logs(caplog)
async def test_scheduler_run_async(caplog): sched = util.Scheduler("xxx") i = [] async def action(): i.append(0) sched.add_action(action, 0.05, 0) while len(i) == 0: await asyncio.sleep(0.01) sched.stop() length = len(i) await asyncio.sleep(0.1) assert len(i) == length assert not sched._executing_tasks[action] no_error_in_logs(caplog)
async def test_purged_facts(resource_container, client, clienthelper, agent, environment, no_agent_backoff, caplog): """ Test if facts are purged when the resource is purged. """ resource_container.Provider.set("agent1", "key", "value") version = await clienthelper.get_version() resource_id_wov = "test::Resource[agent1,key=key]" resource_id = "%s,v=%d" % (resource_id_wov, version) resources = [{ "key": "key", "value": "value", "id": resource_id, "requires": [], "purged": False, "send_event": False }] await clienthelper.put_version_simple(resources, version) result = await client.release_version( environment, version, True, const.AgentTriggerMethod.push_full_deploy) assert result.code == 200 result = await client.get_param(environment, "length", resource_id_wov) assert result.code == 503 env_uuid = uuid.UUID(environment) params = await data.Parameter.get_list(environment=env_uuid, resource_id=resource_id_wov) while len(params) < 3: params = await data.Parameter.get_list(environment=env_uuid, resource_id=resource_id_wov) await asyncio.sleep(0.1) result = await client.get_param(environment, "key1", resource_id_wov) assert result.code == 200 # Purge the resource version = await clienthelper.get_version() resources[0]["id"] = "%s,v=%d" % (resource_id_wov, version) resources[0]["purged"] = True result = await client.put_version( tid=environment, version=version, resources=resources, unknowns=[], version_info={}, compiler_version=get_compiler_version(), ) assert result.code == 200 result = await client.release_version( environment, version, True, const.AgentTriggerMethod.push_full_deploy) assert result.code == 200 result = await client.get_version(environment, version) assert result.code == 200 await _wait_until_deployment_finishes(client, environment, version) result = await client.get_version(environment, version) assert result.result["model"]["done"] == len(resources) # The resource facts should be purged result = await client.get_param(environment, "length", resource_id_wov) assert result.code == 503 no_error_in_logs(caplog)