1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
from uuid import uuid4
import boto3
import pytest
from botocore.exceptions import ClientError
from moto import mock_aws
from tests import EXAMPLE_AMI_ID
def quick_instance_creation():
conn_ec2 = boto3.resource("ec2", "us-east-1")
test_instance = conn_ec2.create_instances(
ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1
)
# We only need instance id for this tests
return test_instance[0].id
def quick_instance_profile_creation(name):
conn_iam = boto3.resource("iam", "us-east-1")
test_instance_profile = conn_iam.create_instance_profile(
InstanceProfileName=name, Path="/"
)
return test_instance_profile.arn, test_instance_profile.name
@mock_aws
def test_associate():
client = boto3.client("ec2", region_name="us-east-1")
instance_id = quick_instance_creation()
instance_profile_arn, instance_profile_name = quick_instance_profile_creation(
str(uuid4())
)
association = client.associate_iam_instance_profile(
IamInstanceProfile={
"Arn": instance_profile_arn,
"Name": instance_profile_name,
},
InstanceId=instance_id,
)
assert association["IamInstanceProfileAssociation"]["InstanceId"] == instance_id
assert (
association["IamInstanceProfileAssociation"]["IamInstanceProfile"]["Arn"]
== instance_profile_arn
)
assert association["IamInstanceProfileAssociation"]["State"] == "associating"
@mock_aws
def test_invalid_associate():
client = boto3.client("ec2", region_name="us-east-1")
instance_id = quick_instance_creation()
instance_profile_arn, instance_profile_name = quick_instance_profile_creation(
str(uuid4())
)
client.associate_iam_instance_profile(
IamInstanceProfile={
"Arn": instance_profile_arn,
"Name": instance_profile_name,
},
InstanceId=instance_id,
)
# Duplicate
with pytest.raises(ClientError) as ex:
client.associate_iam_instance_profile(
IamInstanceProfile={
"Arn": instance_profile_arn,
"Name": instance_profile_name,
},
InstanceId=instance_id,
)
assert ex.value.response["Error"]["Code"] == "IncorrectState"
assert (
"There is an existing association for" in ex.value.response["Error"]["Message"]
)
# Wrong instance profile
with pytest.raises(ClientError) as ex:
client.associate_iam_instance_profile(
IamInstanceProfile={"Arn": "fake", "Name": "fake"}, InstanceId=instance_id
)
assert ex.value.response["Error"]["Code"] == "NoSuchEntity"
assert "not found" in ex.value.response["Error"]["Message"]
# Wrong instance id
with pytest.raises(ClientError) as ex:
client.associate_iam_instance_profile(
IamInstanceProfile={
"Arn": instance_profile_arn,
"Name": instance_profile_name,
},
InstanceId="fake",
)
assert ex.value.response["Error"]["Code"] == "InvalidInstanceID.NotFound"
assert "does not exist" in ex.value.response["Error"]["Message"]
@mock_aws
def test_describe():
client = boto3.client("ec2", region_name="us-east-1")
instance_id1 = quick_instance_creation()
instance_profile_arn1, instance_profile_name1 = quick_instance_profile_creation(
str(uuid4())
)
client.associate_iam_instance_profile(
IamInstanceProfile={
"Arn": instance_profile_arn1,
"Name": instance_profile_name1,
},
InstanceId=instance_id1,
)
associations = client.describe_iam_instance_profile_associations()
associations = associations["IamInstanceProfileAssociations"]
assert instance_profile_arn1 in [
a["IamInstanceProfile"]["Arn"] for a in associations
]
my_assoc = [
a
for a in associations
if a["IamInstanceProfile"]["Arn"] == instance_profile_arn1
][0]
assert my_assoc["InstanceId"] == instance_id1
assert my_assoc["State"] == "associated"
instance_id2 = quick_instance_creation()
instance_profile_arn2, instance_profile_name2 = quick_instance_profile_creation(
str(uuid4())
)
client.associate_iam_instance_profile(
IamInstanceProfile={
"Arn": instance_profile_arn2,
"Name": instance_profile_name2,
},
InstanceId=instance_id2,
)
associations = client.describe_iam_instance_profile_associations()
associations = associations["IamInstanceProfileAssociations"]
assert instance_profile_arn1 in [
a["IamInstanceProfile"]["Arn"] for a in associations
]
assert instance_profile_arn2 in [
a["IamInstanceProfile"]["Arn"] for a in associations
]
my_assoc = [
a
for a in associations
if a["IamInstanceProfile"]["Arn"] == instance_profile_arn1
][0]
associations = client.describe_iam_instance_profile_associations(
AssociationIds=[my_assoc["AssociationId"]]
)
assert len(associations["IamInstanceProfileAssociations"]) == 1
assert (
associations["IamInstanceProfileAssociations"][0]["IamInstanceProfile"]["Arn"]
== my_assoc["IamInstanceProfile"]["Arn"]
)
associations = client.describe_iam_instance_profile_associations(
Filters=[
{"Name": "instance-id", "Values": [my_assoc["InstanceId"]]},
{"Name": "state", "Values": ["associated"]},
]
)
assert len(associations["IamInstanceProfileAssociations"]) == 1
assert (
associations["IamInstanceProfileAssociations"][0]["IamInstanceProfile"]["Arn"]
== my_assoc["IamInstanceProfile"]["Arn"]
)
@mock_aws
def test_replace():
client = boto3.client("ec2", region_name="us-east-1")
instance_id1 = quick_instance_creation()
instance_profile_arn1, instance_profile_name1 = quick_instance_profile_creation(
str(uuid4())
)
instance_profile_arn2, instance_profile_name2 = quick_instance_profile_creation(
str(uuid4())
)
association = client.associate_iam_instance_profile(
IamInstanceProfile={
"Arn": instance_profile_arn1,
"Name": instance_profile_name1,
},
InstanceId=instance_id1,
)
association = client.replace_iam_instance_profile_association(
IamInstanceProfile={
"Arn": instance_profile_arn2,
"Name": instance_profile_name2,
},
AssociationId=association["IamInstanceProfileAssociation"]["AssociationId"],
)
assert (
association["IamInstanceProfileAssociation"]["IamInstanceProfile"]["Arn"]
== instance_profile_arn2
)
assert association["IamInstanceProfileAssociation"]["State"] == "associating"
@mock_aws
def test_invalid_replace():
client = boto3.client("ec2", region_name="us-east-1")
instance_id = quick_instance_creation()
instance_profile_arn, instance_profile_name = quick_instance_profile_creation(
str(uuid4())
)
instance_profile_arn2, instance_profile_name2 = quick_instance_profile_creation(
str(uuid4())
)
association = client.associate_iam_instance_profile(
IamInstanceProfile={
"Arn": instance_profile_arn,
"Name": instance_profile_name,
},
InstanceId=instance_id,
)
# Wrong id
with pytest.raises(ClientError) as ex:
client.replace_iam_instance_profile_association(
IamInstanceProfile={
"Arn": instance_profile_arn2,
"Name": instance_profile_name2,
},
AssociationId="fake",
)
assert ex.value.response["Error"]["Code"] == "InvalidAssociationID.NotFound"
assert "An invalid association-id of" in ex.value.response["Error"]["Message"]
# Wrong instance profile
with pytest.raises(ClientError) as ex:
client.replace_iam_instance_profile_association(
IamInstanceProfile={"Arn": "fake", "Name": "fake"},
AssociationId=association["IamInstanceProfileAssociation"]["AssociationId"],
)
assert ex.value.response["Error"]["Code"] == "NoSuchEntity"
assert "not found" in ex.value.response["Error"]["Message"]
@mock_aws
def test_disassociate():
client = boto3.client("ec2", region_name="us-east-1")
instance_id = quick_instance_creation()
instance_profile_arn, instance_profile_name = quick_instance_profile_creation(
str(uuid4())
)
association = client.associate_iam_instance_profile(
IamInstanceProfile={
"Arn": instance_profile_arn,
"Name": instance_profile_name,
},
InstanceId=instance_id,
)
associations = client.describe_iam_instance_profile_associations()
associations = associations["IamInstanceProfileAssociations"]
assert instance_profile_arn in [
a["IamInstanceProfile"]["Arn"] for a in associations
]
reservations = client.describe_instances(InstanceIds=[instance_id])["Reservations"]
instances = reservations[0]["Instances"]
assert "IamInstanceProfile" in instances[0]
disassociation = client.disassociate_iam_instance_profile(
AssociationId=association["IamInstanceProfileAssociation"]["AssociationId"]
)
assert (
disassociation["IamInstanceProfileAssociation"]["IamInstanceProfile"]["Arn"]
== instance_profile_arn
)
assert disassociation["IamInstanceProfileAssociation"]["State"] == "disassociating"
associations = client.describe_iam_instance_profile_associations()
associations = associations["IamInstanceProfileAssociations"]
assert instance_profile_arn not in [
a["IamInstanceProfile"]["Arn"] for a in associations
]
reservations = client.describe_instances(InstanceIds=[instance_id])["Reservations"]
instances = reservations[0]["Instances"]
assert "IamInstanceProfile" not in instances[0]
@mock_aws
def test_invalid_disassociate():
client = boto3.client("ec2", region_name="us-east-1")
# Wrong id
with pytest.raises(ClientError) as ex:
client.disassociate_iam_instance_profile(AssociationId="fake")
assert ex.value.response["Error"]["Code"] == "InvalidAssociationID.NotFound"
assert "An invalid association-id of" in ex.value.response["Error"]["Message"]
|