|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +from uas_standards.astm.f3548.v21.api import EntityID, SubscriptionID |
| 4 | + |
| 5 | +from monitoring.monitorlib.fetch import QueryError |
| 6 | +from monitoring.uss_qualifier.resources.astm.f3548.v21.dss import DSSInstance |
| 7 | +from monitoring.uss_qualifier.scenarios.scenario import TestScenarioType |
| 8 | + |
| 9 | +# The InterUSS DSS implementation will set an OIR's subscription ID to 00000000-0000-4000-8000-000000000000 |
| 10 | +# when the OIR is not attached to any subscription, as the OpenAPI spec does not allow the value to be empty. |
| 11 | +# Other implementations may use a different value. One way to check that an OIR is not attached to any subscription |
| 12 | +# is to attempt to retrieve the subscription reportedly attached to it: if a 404 is returned then we may assume |
| 13 | +# no subscription is attached. |
| 14 | +# Note that this is only allowed for OIRs in the ACCEPTED state. |
| 15 | +NULL_SUBSCRIPTION_ID = "00000000-0000-4000-8000-000000000000" |
| 16 | + |
| 17 | + |
| 18 | +def step_oir_has_correct_subscription( |
| 19 | + scenario: TestScenarioType, |
| 20 | + dss: DSSInstance, |
| 21 | + oir_id: EntityID, |
| 22 | + expected_sub_id: Optional[SubscriptionID], |
| 23 | +): |
| 24 | + """ |
| 25 | + Ensure that an OIR is currently attached to the specified subscription, |
| 26 | + or not attached to any subscription if the passed subscription ID is None. |
| 27 | +
|
| 28 | + This fragment will fetch the OIR from the DSS. |
| 29 | + """ |
| 30 | + |
| 31 | + step_name = ( |
| 32 | + "OIR is not attached to any subscription" |
| 33 | + if expected_sub_id is None |
| 34 | + else "OIR is attached to expected subscription" |
| 35 | + ) |
| 36 | + scenario.begin_test_step(step_name) |
| 37 | + check_oir_has_correct_subscription( |
| 38 | + scenario, |
| 39 | + dss, |
| 40 | + oir_id, |
| 41 | + expected_sub_id, |
| 42 | + ) |
| 43 | + scenario.end_test_step() |
| 44 | + |
| 45 | + |
| 46 | +def check_oir_has_correct_subscription( |
| 47 | + scenario: TestScenarioType, |
| 48 | + dss: DSSInstance, |
| 49 | + oir_id: EntityID, |
| 50 | + expected_sub_id: Optional[SubscriptionID], |
| 51 | +): |
| 52 | + with scenario.check( |
| 53 | + "Get operational intent reference by ID", dss.participant_id |
| 54 | + ) as check: |
| 55 | + try: |
| 56 | + oir, q = dss.get_op_intent_reference(oir_id) |
| 57 | + scenario.record_query(q) |
| 58 | + except QueryError as qe: |
| 59 | + scenario.record_queries(qe.queries) |
| 60 | + check.record_failed( |
| 61 | + summary="Could not get OIR", |
| 62 | + details=f"Failed to get OIR with error code {qe.cause_status_code}: {qe.msg}", |
| 63 | + query_timestamps=qe.query_timestamps, |
| 64 | + ) |
| 65 | + |
| 66 | + sub_is_as_expected = False |
| 67 | + referenced_sub_was_found_when_non_expected = False |
| 68 | + if expected_sub_id is None: |
| 69 | + # See comment on NULL_SUBSCRIPTION_ID |
| 70 | + # ASTM may at some point decide to tolerate accepting empty returned values here, |
| 71 | + # but in the meantime we simply attempt to obtain the subscription and check that it does not exist |
| 72 | + if oir.subscription_id == NULL_SUBSCRIPTION_ID: |
| 73 | + # Sub ID explicitly set to the value representing the null subscription: all good |
| 74 | + sub_is_as_expected = True |
| 75 | + elif oir.subscription_id is None: |
| 76 | + # Sub ID not set at all: not compliant with the spec, but not wrong with regard to which subscription should be attached to the OIR |
| 77 | + sub_is_as_expected = True |
| 78 | + else: |
| 79 | + # If the subscription ID is defined and not set to the known 'null' value, we assume that the DSS used another |
| 80 | + # placeholder for the non-existing subscription, and we check that it does not exist. |
| 81 | + with scenario.check( |
| 82 | + "Subscription referenced by the OIR does not exist" |
| 83 | + ) as check: |
| 84 | + sub = dss.get_subscription(oir.subscription_id) |
| 85 | + scenario.record_query(sub) |
| 86 | + if sub.status_code not in [200, 404]: |
| 87 | + check.record_failed( |
| 88 | + summary="Failed to try to obtain the subscription referenced by the OIR", |
| 89 | + details=f"Failed in an unexpected way while querying subscription with ID {oir.subscription_id}: expected a 404 or 200, but got {sub.status_code}", |
| 90 | + query_timestamps=[sub.request.timestamp], |
| 91 | + ) |
| 92 | + if sub.status_code == 200: |
| 93 | + referenced_sub_was_found_when_non_expected = True |
| 94 | + else: |
| 95 | + sub_is_as_expected = oir.subscription_id == expected_sub_id |
| 96 | + |
| 97 | + attached_check_name = ( |
| 98 | + "OIR is not attached to a subscription" |
| 99 | + if expected_sub_id is None |
| 100 | + else f"OIR is attached to expected subscription" |
| 101 | + ) |
| 102 | + |
| 103 | + with scenario.check(attached_check_name, dss.participant_id) as check: |
| 104 | + if referenced_sub_was_found_when_non_expected: |
| 105 | + check.record_failed( |
| 106 | + summary="OIR is attached to a subscription although it should not be", |
| 107 | + details=f"Expected OIR to not be attached to any subscription, but the referenced subscription {oir.subscription_id} does exist.", |
| 108 | + query_timestamps=[sub.request.timestamp], |
| 109 | + ) |
| 110 | + if not sub_is_as_expected: |
| 111 | + check.record_failed( |
| 112 | + summary="OIR is not attached to the correct subscription", |
| 113 | + details=f"Expected OIR to be attached to subscription {expected_sub_id}, but it is attached to {oir.subscription_id}", |
| 114 | + query_timestamps=[q.request.timestamp], |
| 115 | + ) |
0 commit comments