#!/usr/bin/env python3 """Tests for fetch_batch_roles.py, on pages in the AWS Batch API reference's and user guide's markup.""" from __future__ import annotations import pytest import fetch_batch_roles as f API = f.API_REFERENCE GUIDE = f.USER_GUIDE def entry(anchor: str, member: str, description: str) -> str: return (f'
{member}{description}
' f'Type: String
') def guide(title: str, content: str) -> str: return f'' '{ "Version":"2012-10-17", ' '"Statement": [ { "Effect": "Allow", "Principal": { "Service": "' + service + '" }, ' '"Action": "sts:AssumeRole" } ] }
The AWSServiceRoleForBatch service-linked role trusts the following services " "to assume the role: batch.amazonaws.com
"), # The instance role's page lists the page holding its trust policy. GUIDE + "instance_IAM_role.html": guide("Amazon ECS instance role", 'Check your role'), GUIDE + "batch-check-ecsinstancerole.html": guide("Check your account's Amazon ECS instance role", trust("ec2.amazonaws.com")), # The spot fleet role's trust policy is the argument of the CLI command that creates it. GUIDE + "spot_fleet_IAM_role.html": guide("Amazon EC2 spot fleet role", 'With the AWS CLI'), GUIDE + "spot-fleet-roles-cli.html": guide( "Create Amazon EC2 spot fleet roles with the AWS CLI", "$ aws iam create-role --role-name AmazonEC2SpotFleetTaggingRole --assume-role-policy-document '{ "
'"Version":"2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": '
"\"spotfleet.amazonaws.com\" }, \"Action\": \"sts:AssumeRole\" } ] }'"),
}
def test_each_member_is_read_with_the_service_its_role_is_for():
built = f.build(lambda url: PAGES[url])
by_member = {(m.get("operation") or m["type"], m["member"]): m for m in built["members"]}
assert by_member[("CreateComputeEnvironment", "serviceRole")]["trustedBy"] == ["batch.amazonaws.com"]
description = by_member[("CreateComputeEnvironment", "serviceRole")]["description"]
assert description.startswith("The full Amazon Resource Name (ARN) of the IAM role that allows AWS Batch")
assert "see AWS Batch service IAM role in the AWS Batch User Guide" in description
# The later link names what the update depends on, not the role.
assert by_member[("UpdateComputeEnvironment", "serviceRole")]["rolePage"] == GUIDE + "service_IAM_role.html"
instance = by_member[("ComputeResource", "instanceRole")]
assert (instance["trustedBy"], instance["instanceProfile"]) == (["ec2.amazonaws.com"], True)
assert instance["trustPages"] == [GUIDE + "batch-check-ecsinstancerole.html"]
assert by_member[("ComputeResource", "spotIamFleetRole")]["trustedBy"] == ["spotfleet.amazonaws.com"]
assert "instanceProfile" not in by_member[("ComputeResource", "spotIamFleetRole")]
assert by_member[("ManagedInstancesProvider", "infrastructureRoleArn")]["passedToService"] == ["ecs.amazonaws.com"]
assert "rolePage" not in by_member[("ManagedInstancesProvider", "infrastructureRoleArn")]
# The update names the provider's role in the same words, so its role is that one.
update = by_member[("UpdateManagedInstancesProviderConfiguration", "infrastructureRoleArn")]
assert update["passedToService"] == ["ecs.amazonaws.com"]
assert update["sameRoleAs"] == {"type": "ManagedInstancesProvider", "member": "infrastructureRoleArn"}
def test_a_page_whose_layout_changed_is_refused():
for url, page, message in (
(API + "API_ComputeResource.html", "No policy.
"), "pages of a role"), (API + "API_CreateComputeEnvironment.html", entry("Batch-CreateComputeEnvironment-request-serviceRole", "serviceRole", "A role."), "pages of a role"), (API + "API_UpdateManagedInstancesProviderConfiguration.html", entry("Batch-Type-UpdateManagedInstancesProviderConfiguration-infrastructureRoleArn", "infrastructureRoleArn", "The updated ARN of some other role."), "no longer names")): changed = dict(PAGES, **{url: page}) with pytest.raises(ValueError, match=message): f.build(lambda u: changed[u]) def test_the_file_is_stale_when_a_page_says_something_else(): vendored = f.build(lambda url: PAGES[url]) assert not f.stale(vendored, PAGES) assert f.stale(vendored, dict(PAGES, **{GUIDE + "batch-check-ecsinstancerole.html": guide( "Check your account's Amazon ECS instance role", trust("ecs-tasks.amazonaws.com"))}))