def test_it_still_creates_a_profiler(self): env = {"AWS_CODEGURU_PROFILER_REPORTING_INTERVAL_MS": "12.5"} profiler_factory = MagicMock(spec=Profiler) subject = build_profiler(pg_name="my_profiling_group", env=env, profiler_factory=profiler_factory) assert subject is not None
def test_it_uses_name_from_arn(self): env = { PG_ARN_ENV: 'arn:aws:codeguru-profiler:us-west-2:003713371902:profilingGroup/name_from_arn', PG_NAME_ENV: 'different_name_here' } assert build_profiler(env=env) \ ._profiler_runner.collector.reporter.profiling_group_name == 'name_from_arn'
def test_it_creates_a_profiler_and_uses_correct_name_and_region(self): env = { PG_ARN_ENV: 'arn:aws:codeguru-profiler:us-west-2:003713371902:profilingGroup/name_from_arn' } profiler = build_profiler(env=env) assert profiler is not None assert profiler._profiler_runner.collector.reporter.profiling_group_name == 'name_from_arn' assert 'us-west-2' in profiler._profiler_runner.collector.reporter.codeguru_client_builder.codeguru_client._endpoint.host
def test_it_creates_profiler_using_the_name_and_region_from_env(self): env = { PG_ARN_ENV: 'this_arn_is_definitely_not_correct', PG_NAME_ENV: 'but_this_name_should_do', REGION_ENV: 'eu-north-1' } profiler = build_profiler(env=env) assert profiler._profiler_runner.collector.reporter.profiling_group_name == 'but_this_name_should_do' assert 'eu-north-1' in profiler._profiler_runner.collector.reporter.codeguru_client_builder.codeguru_client._endpoint.host
def test_it_creates_a_profiler_and_uses_correct_region(self): region = "eu-north-1" profiler_factory = MagicMock(spec=Profiler) subject = build_profiler(pg_name="my_profiling_group", region_name=region, env={}, profiler_factory=profiler_factory) assert subject is not None profiler_factory.assert_called_once_with(profiling_group_name=ANY, region_name=region, aws_session=ANY, environment_override=ANY)
def test_it_creates_a_profile_rand_uses_correct_credential_profile( self): credential_profile = "test-profile" mock_session = MagicMock(name="profiler", spec=boto3.session.Session) subject = build_profiler(pg_name="my_profiling_group", credential_profile=credential_profile, env={}, session_factory=mock_session) assert subject is not None mock_session.assert_called_once_with( region_name=ANY, profile_name=credential_profile)
def test_it_creates_a_profiler_and_uses_correct_name(self): pg_name = "my_profiling_group" profiler_factory = MagicMock(spec=Profiler) env = {PG_NAME_ENV: pg_name} subject = build_profiler(env=env, profiler_factory=profiler_factory) assert subject is not None profiler_factory.assert_called_once_with( profiling_group_name=pg_name, region_name=ANY, aws_session=ANY, environment_override=ANY)
def test_it_uses_correct_sampling_interval(self): env = {"AWS_CODEGURU_PROFILER_REPORTING_INTERVAL_MS": "60000"} profiler_factory = MagicMock(spec=Profiler) subject = build_profiler(pg_name="my_profiling_group", env=env, profiler_factory=profiler_factory) profiler_factory.assert_called_once_with( profiling_group_name=ANY, region_name=ANY, aws_session=ANY, environment_override={ "reporting_interval": datetime.timedelta(seconds=60) })
def _create_lambda_profiler(profiling_group_name, region_name, environment_override, context, env=os.environ): """ Calls build_profiler module to create the profiler object. If we fail to create it we return a no-op profiler so that we don't even go through this method again. """ from codeguru_profiler_agent.profiler_builder import build_profiler from codeguru_profiler_agent.agent_metadata.agent_metadata import AgentMetadata from codeguru_profiler_agent.agent_metadata.aws_lambda import AWSLambda override = {'agent_metadata': AgentMetadata(AWSLambda.look_up_metadata(context))} override.update(environment_override) profiler = build_profiler(pg_name=profiling_group_name, region_name=region_name, override=override, env=env) if profiler is None: return _EmptyProfiler() return profiler
def _start_profiler(options, env): """ This will init the profiler object and start it. :param options: options may contain profiling group name, region or credential profile if they are passed in command :param env: the environment dict from which to search for variables (usually os.environ is passed) :return: the profiler object """ from codeguru_profiler_agent.profiler_builder import build_profiler global profiler profiler = build_profiler(pg_name=options.profiling_group_name, region_name=options.region, credential_profile=options.credential_profile, env=env) if profiler is not None: profiler.start() return profiler
def test_it_creates_a_profiler_and_it_ignores_credential_profile_set_in_env( self): credential_profile = "test-profile" previous_credential_value = os.environ.get(CREDENTIAL_PATH) os.environ[CREDENTIAL_PATH] = credential_profile mock_session = MagicMock(name="profiler", spec=boto3.session.Session) subject = build_profiler(pg_name="my_profiling_group", env={}, session_factory=mock_session) if previous_credential_value is not None: os.environ[CREDENTIAL_PATH] = previous_credential_value else: del os.environ[CREDENTIAL_PATH] assert subject is not None mock_session.assert_called_once_with(region_name=ANY, profile_name=None)
def test_it_returns_none(self): assert build_profiler(env={}) is None
def test_it_returns_none(self): assert build_profiler(pg_name="my_profiling_group", env="not_a_dictionary") is None
def test_it_builds_the_profiler(self): for value in ["True", "true"]: env = {ENABLED_ENV: value} assert build_profiler(pg_name="my_profiling_group", env=env) is not None
def test_it_returns_none(self): for value in ["False", "false", "0", "Nope", "Yes" "anything"]: env = {ENABLED_ENV: value} assert build_profiler(pg_name="my_profiling_group", env=env) is None