Пример #1
0
def test_determine_plans_to_remove_different_service_same_regions():
    old_service = "OLD SERVICE"
    new_service = "NEW SERVICE"
    regions = frozenset(["IAD", "PDX"])

    assert determine_plans_to_remove(old_service, regions, new_service, regions) == frozenset([
        Plan(old_service, "IAD"),
        Plan(old_service, "PDX")
    ])
def handle_blueprint_record(blueprint_record: BlueprintRecord,
                            plans_to_update: PlansToUpdate) -> BuildablesBlueprintRecord:
    buildables_blueprint_record = BuildablesBlueprintRecord.get_if_present(blueprint_record.uid)
    plans_to_add: FrozenSet[Plan] = Plan.generate_plans(blueprint_record.service_rip_short_name,
                                                        blueprint_record.get_airport_codes())

    if not buildables_blueprint_record:
        logger.info(f"Could not find a matching blueprint record in buildables for {blueprint_record} from blueprint")
        buildables_blueprint_record = BuildablesBlueprintRecord.create(blueprint_record.uid)
        set_all_blueprint_record_fields(buildables_blueprint_record, blueprint_record)
        buildables_blueprint_record.save()
        plans_to_update.update_or_add_uid_to_plans(blueprint_record.uid, plans_to_add)
        logger.info(f"Created {buildables_blueprint_record} in buildables")
        return buildables_blueprint_record

    logger.info(f"The matching blueprint record in buildables is {buildables_blueprint_record} for {blueprint_record}")
    set_blueprint_record_fields_except_service_and_regions(buildables_blueprint_record, blueprint_record)
    buildables_blueprint_record.save()
    logger.info(f"Updated all regions exception for service and region for {buildables_blueprint_record}")

    plan_change_args = dict(
        old_service=buildables_blueprint_record.service_rip_short_name,
        old_regions=frozenset(buildables_blueprint_record.regions),
        new_service=blueprint_record.service_rip_short_name,
        new_regions=frozenset(blueprint_record.get_airport_codes())
    )

    plans_to_update.update_or_add_uid_to_plans(blueprint_record.uid, plans_to_add)
    plans_to_update.remove_uid_from_plans(blueprint_record.uid, determine_plans_to_remove(**plan_change_args))

    return buildables_blueprint_record
Пример #3
0
def test_determine_plans_to_remove_same_service_different_region():
    service = "TEST"
    old_regions = frozenset(["IAD", "PDX"])
    new_regions = frozenset(["PDX", "SFO"])

    assert determine_plans_to_remove(service, old_regions, service, new_regions) == frozenset([
        Plan(service, "IAD")
    ])
def test_handle_blueprint_record_with_match(
        mocked_buildables_blueprint_record):
    mocked_matching_record = Mock()
    mocked_matching_record.service_rip_short_name = BLUEPRINT_RECORD.service_rip_short_name
    mocked_matching_record.regions = {"IAD", "CMH", "PDX",
                                      "SFO"}  # NRT added and SFO removed
    mocked_buildables_blueprint_record.get_if_present.return_value = mocked_matching_record
    plans_to_update = Mock()

    handle_blueprint_record(BLUEPRINT_RECORD, plans_to_update)

    assert plans_to_update.update_or_add_uid_to_plans.call_args[0][
        0] == BLUEPRINT_RECORD.uid
    assert Plan(
        "myservice",
        "NRT") in plans_to_update.update_or_add_uid_to_plans.call_args[0][1]
    assert frozenset(
        [Plan("myservice",
              "SFO")]) == plans_to_update.remove_uid_from_plans.call_args[0][1]
    mocked_matching_record.save.assert_called_once()
Пример #5
0
def handle_buildables_blueprint_received_plans(buildables_blueprint_plans: Iterable[BlueprintPlan],
                                               plans_to_update: PlansToUpdate):
    for buildables_blueprint_plan in buildables_blueprint_plans:
        plan = Plan(buildables_blueprint_plan.get_service(), buildables_blueprint_plan.get_region())
        uids_to_add_or_update = plans_to_update.get_update_or_add_uids(plan)
        uids_to_remove = plans_to_update.get_uids_to_remove(plan)
        logger.info(f"Updating uids for {buildables_blueprint_plans}, adding or updating {uids_to_add_or_update} "
                    f"and removing {uids_to_remove}")
        buildables_blueprint_plan.add_uids(uids_to_add_or_update)
        buildables_blueprint_plan.remove_uids(uids_to_remove)
        logger.info(f"Recalculating the launch date for {buildables_blueprint_plan}")
        buildables_blueprint_plan.recalculate()
def test_handle_blueprint_record_without_match(
        mocked_buildables_blueprint_record):
    mocked_buildables_blueprint_record.get_if_present.return_value = None
    created_record = Mock()
    mocked_buildables_blueprint_record.create.return_value = created_record
    plans_to_update = Mock()

    handle_blueprint_record(BLUEPRINT_RECORD, plans_to_update)

    assert plans_to_update.update_or_add_uid_to_plans.call_args[0][
        0] == BLUEPRINT_RECORD.uid
    assert Plan(
        "myservice",
        "NRT") in plans_to_update.update_or_add_uid_to_plans.call_args[0][1]
    created_record.save.assert_called_once()
Пример #7
0
def convert_buildables_blueprint_plans_to_plans(
        buildables_blueprint_plans: Iterable[BlueprintPlan]) -> Set[Plan]:
    return {Plan(plan.get_service(), plan.get_region()) for plan in buildables_blueprint_plans}
Пример #8
0
from regions_recon_lambda.blueprint_ingestor.data_records.plans_update import PlansToUpdate
from regions_recon_lambda.blueprint_ingestor.utils.handle_blueprint_plan_change import \
    handle_buildables_blueprint_received_plans, convert_buildables_blueprint_plans_to_plans, handle_unmatched_plans, \
    change_blueprint_plans
from regions_recon_python_common.data_models.plan import Plan


def create_mocked_buildables_blueprint_plan(plan: Plan):
    mocked_plan = Mock()
    mocked_plan.get_service.return_value = plan.service
    mocked_plan.get_region.return_value = plan.region
    return mocked_plan


LAMBDA_PLAN = Plan("lambda", "IAD")
SAGEMAKER_PLAN = Plan("sagemaker", "PDX")
DDB_PLAN = Plan("dynamodb", "SFO")

UID_TO_REMOVE_FROM_LAMBDA = "123"
UID_TO_REMOVE_FROM_SAGEMAKER = "234"
UID_TO_ADD_TO_DDB = "456"


@pytest.fixture
def test_buildables_blueprint_plans():
    return [
        create_mocked_buildables_blueprint_plan(LAMBDA_PLAN),
        create_mocked_buildables_blueprint_plan(SAGEMAKER_PLAN)
    ]

def test_handle_uid_remove():
    test_update = Update()
    test_update.handle_uid("123", True)
    assert "123" in test_update.remove_uids


def test_handle_uid_add():
    test_update = Update()
    test_update.handle_uid("123", False)
    assert "123" in test_update.update_or_add_uids


TEST_PLANS = [
    Plan("SERVICE_ONE", "IAD"),
    Plan("SERVICE_TWO", "PDX")
]


@pytest.fixture
def test_plans_to_update():
    return PlansToUpdate()


def test_plans_to_update_remove(test_plans_to_update):
    test_plans_to_update.remove_uid_from_plans("123", TEST_PLANS)
    assert "123" in test_plans_to_update.get_uids_to_remove(TEST_PLANS[0])
    assert "123" in test_plans_to_update.get_uids_to_remove(TEST_PLANS[1])