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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
|
import boto3
import pytest
from botocore.exceptions import ClientError
from moto import mock_aws
def generate_thing_group_tree(iot_client, tree_dict, _parent=None):
"""
Generates a thing group tree given the input tree structure.
:param iot_client: the iot client for boto3
:param tree_dict: dictionary with the key being the group_name, and the value being a sub tree.
tree_dict = {
"group_name_1a":{
"group_name_2a":{
"group_name_3a":{} or None
},
},
"group_name_1b":{}
}
:return: a dictionary of created groups, keyed by group name
"""
if tree_dict is None:
tree_dict = {}
created_dict = {}
for group_name in tree_dict.keys():
params = {"thingGroupName": group_name}
if _parent:
params["parentGroupName"] = _parent
created_group = iot_client.create_thing_group(**params)
created_dict[group_name] = created_group
subtree_dict = generate_thing_group_tree(
iot_client=iot_client, tree_dict=tree_dict[group_name], _parent=group_name
)
created_dict.update(created_dict)
created_dict.update(subtree_dict)
return created_dict
class TestListThingGroup:
group_name_1a = "my-group:name-1a"
group_name_1b = "my-group:name-1b"
group_name_2a = "my-group-name-2a"
group_name_2b = "my-group-name-2b"
group_name_3a = "my-group-name-3a"
group_name_3b = "my-group-name-3b"
group_name_3c = "my-group-name-3c"
group_name_3d = "my-group-name-3d"
tree_dict = {
group_name_1a: {
group_name_2a: {group_name_3a: {}, group_name_3b: {}},
group_name_2b: {group_name_3c: {}, group_name_3d: {}},
},
group_name_1b: {},
}
@mock_aws
def test_should_list_all_groups(self):
# setup
client = boto3.client("iot", region_name="ap-northeast-1")
generate_thing_group_tree(client, self.tree_dict)
# test
resp = client.list_thing_groups()
assert "thingGroups" in resp
assert len(resp["thingGroups"]) == 8
@mock_aws
def test_should_list_all_groups_non_recursively(self):
# setup
client = boto3.client("iot", region_name="ap-northeast-1")
generate_thing_group_tree(client, self.tree_dict)
# test
resp = client.list_thing_groups(recursive=False)
assert "thingGroups" in resp
assert len(resp["thingGroups"]) == 2
@mock_aws
def test_should_list_all_groups_filtered_by_parent(self):
# setup
client = boto3.client("iot", region_name="ap-northeast-1")
generate_thing_group_tree(client, self.tree_dict)
# test
resp = client.list_thing_groups(parentGroup=self.group_name_1a)
assert "thingGroups" in resp
assert len(resp["thingGroups"]) == 6
resp = client.list_thing_groups(parentGroup=self.group_name_2a)
assert "thingGroups" in resp
assert len(resp["thingGroups"]) == 2
resp = client.list_thing_groups(parentGroup=self.group_name_1b)
assert "thingGroups" in resp
assert len(resp["thingGroups"]) == 0
with pytest.raises(ClientError) as e:
client.list_thing_groups(parentGroup="inexistant-group-name")
assert e.value.response["Error"]["Code"] == "ResourceNotFoundException"
@mock_aws
def test_should_list_all_groups_filtered_by_parent_non_recursively(self):
# setup
client = boto3.client("iot", region_name="ap-northeast-1")
generate_thing_group_tree(client, self.tree_dict)
# test
resp = client.list_thing_groups(parentGroup=self.group_name_1a, recursive=False)
assert "thingGroups" in resp
assert len(resp["thingGroups"]) == 2
resp = client.list_thing_groups(parentGroup=self.group_name_2a, recursive=False)
assert "thingGroups" in resp
assert len(resp["thingGroups"]) == 2
@mock_aws
def test_should_list_all_groups_filtered_by_name_prefix(self):
# setup
client = boto3.client("iot", region_name="ap-northeast-1")
generate_thing_group_tree(client, self.tree_dict)
# test
resp = client.list_thing_groups(namePrefixFilter="my-group:name-1")
assert "thingGroups" in resp
assert len(resp["thingGroups"]) == 2
resp = client.list_thing_groups(namePrefixFilter="my-group-name-3")
assert "thingGroups" in resp
assert len(resp["thingGroups"]) == 4
resp = client.list_thing_groups(namePrefixFilter="prefix-which-doesn-not-match")
assert "thingGroups" in resp
assert len(resp["thingGroups"]) == 0
@mock_aws
def test_should_list_all_groups_filtered_by_name_prefix_non_recursively(self):
# setup
client = boto3.client("iot", region_name="ap-northeast-1")
generate_thing_group_tree(client, self.tree_dict)
# test
resp = client.list_thing_groups(
namePrefixFilter="my-group:name-1", recursive=False
)
assert "thingGroups" in resp
assert len(resp["thingGroups"]) == 2
resp = client.list_thing_groups(
namePrefixFilter="my-group-name-3", recursive=False
)
assert "thingGroups" in resp
assert len(resp["thingGroups"]) == 0
@mock_aws
def test_should_list_all_groups_filtered_by_name_prefix_and_parent(self):
# setup
client = boto3.client("iot", region_name="ap-northeast-1")
generate_thing_group_tree(client, self.tree_dict)
# test
resp = client.list_thing_groups(
namePrefixFilter="my-group-name-2", parentGroup=self.group_name_1a
)
assert "thingGroups" in resp
assert len(resp["thingGroups"]) == 2
resp = client.list_thing_groups(
namePrefixFilter="my-group-name-3", parentGroup=self.group_name_1a
)
assert "thingGroups" in resp
assert len(resp["thingGroups"]) == 4
resp = client.list_thing_groups(
namePrefixFilter="prefix-which-doesn-not-match",
parentGroup=self.group_name_1a,
)
assert "thingGroups" in resp
assert len(resp["thingGroups"]) == 0
@mock_aws
def test_delete_thing_group():
client = boto3.client("iot", region_name="ap-northeast-1")
group_name_1a = "my-group:name-1a"
group_name_2a = "my-group-name-2a"
tree_dict = {group_name_1a: {group_name_2a: {}}}
generate_thing_group_tree(client, tree_dict)
# delete group with child
try:
client.delete_thing_group(thingGroupName=group_name_1a)
except client.exceptions.InvalidRequestException as exc:
error_code = exc.response["Error"]["Code"]
assert error_code == "InvalidRequestException"
else:
raise Exception("Should have raised error")
# delete child group
client.delete_thing_group(thingGroupName=group_name_2a)
res = client.list_thing_groups()
assert len(res["thingGroups"]) == 1
assert group_name_2a not in res["thingGroups"]
# now that there is no child group, we can delete the previous group safely
client.delete_thing_group(thingGroupName=group_name_1a)
res = client.list_thing_groups()
assert len(res["thingGroups"]) == 0
# Deleting an invalid thing group does not raise an error.
res = client.delete_thing_group(thingGroupName="non-existent-group-name")
assert res["ResponseMetadata"]["HTTPStatusCode"] == 200
@mock_aws
def test_describe_thing_group_metadata_hierarchy():
client = boto3.client("iot", region_name="ap-northeast-1")
group_name_1a = "my-group:name-1a"
group_name_1b = "my-group:name-1b"
group_name_2a = "my-group-name-2a"
group_name_2b = "my-group-name-2b"
group_name_3a = "my-group-name-3a"
group_name_3b = "my-group-name-3b"
group_name_3c = "my-group-name-3c"
group_name_3d = "my-group-name-3d"
tree_dict = {
group_name_1a: {
group_name_2a: {group_name_3a: {}, group_name_3b: {}},
group_name_2b: {group_name_3c: {}, group_name_3d: {}},
},
group_name_1b: {},
}
group_catalog = generate_thing_group_tree(client, tree_dict)
# describe groups
# groups level 1
# 1a
desc1a = client.describe_thing_group(thingGroupName=group_name_1a)
assert desc1a["thingGroupName"] == group_name_1a
assert "thingGroupProperties" in desc1a
assert "creationDate" in desc1a["thingGroupMetadata"]
assert "version" in desc1a
# 1b
desc1b = client.describe_thing_group(thingGroupName=group_name_1b)
assert desc1b["thingGroupName"] == group_name_1b
assert "thingGroupProperties" in desc1b
assert len(desc1b["thingGroupMetadata"]) == 1
assert "creationDate" in desc1b["thingGroupMetadata"]
assert "version" in desc1b
# groups level 2
# 2a
desc2a = client.describe_thing_group(thingGroupName=group_name_2a)
assert desc2a["thingGroupName"] == group_name_2a
assert "thingGroupProperties" in desc2a
assert len(desc2a["thingGroupMetadata"]) == 3
assert desc2a["thingGroupMetadata"]["parentGroupName"] == group_name_1a
desc2a_groups = desc2a["thingGroupMetadata"]["rootToParentThingGroups"]
assert len(desc2a_groups) == 1
assert desc2a_groups[0]["groupName"] == group_name_1a
assert desc2a_groups[0]["groupArn"] == group_catalog[group_name_1a]["thingGroupArn"]
assert "version" in desc2a
# 2b
desc2b = client.describe_thing_group(thingGroupName=group_name_2b)
assert desc2b["thingGroupName"] == group_name_2b
assert "thingGroupProperties" in desc2b
assert len(desc2b["thingGroupMetadata"]) == 3
assert desc2b["thingGroupMetadata"]["parentGroupName"] == group_name_1a
desc2b_groups = desc2b["thingGroupMetadata"]["rootToParentThingGroups"]
assert len(desc2b_groups) == 1
assert desc2b_groups[0]["groupName"] == group_name_1a
assert desc2b_groups[0]["groupArn"] == group_catalog[group_name_1a]["thingGroupArn"]
assert "version" in desc2b
# groups level 3
# 3a
desc3a = client.describe_thing_group(thingGroupName=group_name_3a)
assert desc3a["thingGroupName"] == group_name_3a
assert "thingGroupProperties" in desc3a
assert len(desc3a["thingGroupMetadata"]) == 3
assert desc3a["thingGroupMetadata"]["parentGroupName"] == group_name_2a
desc3a_groups = desc3a["thingGroupMetadata"]["rootToParentThingGroups"]
assert len(desc3a_groups) == 2
assert desc3a_groups[0]["groupName"] == group_name_1a
assert desc3a_groups[0]["groupArn"] == group_catalog[group_name_1a]["thingGroupArn"]
assert desc3a_groups[1]["groupName"] == group_name_2a
assert desc3a_groups[1]["groupArn"] == group_catalog[group_name_2a]["thingGroupArn"]
assert "version" in desc3a
# 3b
desc3b = client.describe_thing_group(thingGroupName=group_name_3b)
assert desc3b["thingGroupName"] == group_name_3b
assert "thingGroupProperties" in desc3b
assert len(desc3b["thingGroupMetadata"]) == 3
assert desc3b["thingGroupMetadata"]["parentGroupName"] == group_name_2a
desc3b_groups = desc3b["thingGroupMetadata"]["rootToParentThingGroups"]
assert len(desc3b_groups) == 2
assert desc3b_groups[0]["groupName"] == group_name_1a
assert desc3b_groups[0]["groupArn"] == group_catalog[group_name_1a]["thingGroupArn"]
assert desc3b_groups[1]["groupName"] == group_name_2a
assert desc3b_groups[1]["groupArn"] == group_catalog[group_name_2a]["thingGroupArn"]
assert "version" in desc3b
# 3c
desc3c = client.describe_thing_group(thingGroupName=group_name_3c)
assert desc3c["thingGroupName"] == group_name_3c
assert "thingGroupProperties" in desc3c
assert len(desc3c["thingGroupMetadata"]) == 3
assert desc3c["thingGroupMetadata"]["parentGroupName"] == group_name_2b
desc3c_groups = desc3c["thingGroupMetadata"]["rootToParentThingGroups"]
assert len(desc3c_groups) == 2
assert desc3c_groups[0]["groupName"] == group_name_1a
assert desc3c_groups[0]["groupArn"] == group_catalog[group_name_1a]["thingGroupArn"]
assert desc3c_groups[1]["groupName"] == group_name_2b
assert desc3c_groups[1]["groupArn"] == group_catalog[group_name_2b]["thingGroupArn"]
assert "version" in desc3c
# 3d
desc3d = client.describe_thing_group(thingGroupName=group_name_3d)
assert desc3d["thingGroupName"] == group_name_3d
assert "thingGroupProperties" in desc3d
assert len(desc3d["thingGroupMetadata"]) == 3
assert desc3d["thingGroupMetadata"]["parentGroupName"] == group_name_2b
desc3d_groups = desc3d["thingGroupMetadata"]["rootToParentThingGroups"]
assert len(desc3d_groups) == 2
assert desc3d_groups[0]["groupName"] == group_name_1a
assert desc3d_groups[0]["groupArn"] == group_catalog[group_name_1a]["thingGroupArn"]
assert desc3d_groups[1]["groupName"] == group_name_2b
assert desc3d_groups[1]["groupArn"] == group_catalog[group_name_2b]["thingGroupArn"]
assert "version" in desc3d
@mock_aws
def test_thing_groups():
client = boto3.client("iot", region_name="ap-northeast-1")
group_name = "my-group:name"
# thing group
thing_group = client.create_thing_group(thingGroupName=group_name)
assert thing_group["thingGroupName"] == group_name
assert "thingGroupArn" in thing_group
assert group_name in thing_group["thingGroupArn"]
res = client.list_thing_groups()
assert len(res["thingGroups"]) == 1
for thing_group in res["thingGroups"]:
assert thing_group["groupName"] is not None
assert thing_group["groupArn"] is not None
thing_group = client.describe_thing_group(thingGroupName=group_name)
assert thing_group["thingGroupName"] == group_name
assert "thingGroupProperties" in thing_group
assert "thingGroupMetadata" in thing_group
assert "version" in thing_group
assert "thingGroupArn" in thing_group
assert group_name in thing_group["thingGroupArn"]
# delete thing group
client.delete_thing_group(thingGroupName=group_name)
res = client.list_thing_groups()
assert len(res["thingGroups"]) == 0
# props create test
props = {
"thingGroupDescription": "my first thing group",
"attributePayload": {"attributes": {"key1": "val01", "Key02": "VAL2"}},
}
thing_group = client.create_thing_group(
thingGroupName=group_name, thingGroupProperties=props
)
assert thing_group["thingGroupName"] == group_name
assert "thingGroupArn" in thing_group
thing_group = client.describe_thing_group(thingGroupName=group_name)
assert "attributes" in thing_group["thingGroupProperties"]["attributePayload"]
res_props = thing_group["thingGroupProperties"]["attributePayload"]["attributes"]
assert res_props["key1"] == "val01"
assert res_props["Key02"] == "VAL2"
# props update test with merge
new_props = {"attributePayload": {"attributes": {"k3": "v3"}, "merge": True}}
client.update_thing_group(thingGroupName=group_name, thingGroupProperties=new_props)
thing_group = client.describe_thing_group(thingGroupName=group_name)
assert "attributes" in thing_group["thingGroupProperties"]["attributePayload"]
res_props = thing_group["thingGroupProperties"]["attributePayload"]["attributes"]
assert res_props["key1"] == "val01"
assert res_props["Key02"] == "VAL2"
assert res_props["k3"] == "v3"
# props update test
new_props = {"attributePayload": {"attributes": {"k4": "v4"}}}
client.update_thing_group(thingGroupName=group_name, thingGroupProperties=new_props)
thing_group = client.describe_thing_group(thingGroupName=group_name)
assert "attributes" in thing_group["thingGroupProperties"]["attributePayload"]
res_props = thing_group["thingGroupProperties"]["attributePayload"]["attributes"]
assert res_props["k4"] == "v4"
assert "key1" not in res_props
@mock_aws
def test_thing_group_relations():
client = boto3.client("iot", region_name="ap-northeast-1")
name = "my-thing"
group_name = "my-group-name"
# thing group
thing_group = client.create_thing_group(thingGroupName=group_name)
assert thing_group["thingGroupName"] == group_name
assert "thingGroupArn" in thing_group
# thing
thing = client.create_thing(thingName=name)
assert thing["thingName"] == name
assert "thingArn" in thing
# add in 4 way
client.add_thing_to_thing_group(thingGroupName=group_name, thingName=name)
client.add_thing_to_thing_group(
thingGroupArn=thing_group["thingGroupArn"], thingArn=thing["thingArn"]
)
client.add_thing_to_thing_group(
thingGroupName=group_name, thingArn=thing["thingArn"]
)
client.add_thing_to_thing_group(
thingGroupArn=thing_group["thingGroupArn"], thingName=name
)
things = client.list_things_in_thing_group(thingGroupName=group_name)
assert "things" in things
assert len(things["things"]) == 1
thing_groups = client.list_thing_groups_for_thing(thingName=name)
assert "thingGroups" in thing_groups
assert len(thing_groups["thingGroups"]) == 1
# remove in 4 way
client.remove_thing_from_thing_group(thingGroupName=group_name, thingName=name)
client.remove_thing_from_thing_group(
thingGroupArn=thing_group["thingGroupArn"], thingArn=thing["thingArn"]
)
client.remove_thing_from_thing_group(
thingGroupName=group_name, thingArn=thing["thingArn"]
)
client.remove_thing_from_thing_group(
thingGroupArn=thing_group["thingGroupArn"], thingName=name
)
things = client.list_things_in_thing_group(thingGroupName=group_name)
assert "things" in things
assert len(things["things"]) == 0
# update thing group for thing
client.update_thing_groups_for_thing(thingName=name, thingGroupsToAdd=[group_name])
things = client.list_things_in_thing_group(thingGroupName=group_name)
assert "things" in things
assert len(things["things"]) == 1
client.update_thing_groups_for_thing(
thingName=name, thingGroupsToRemove=[group_name]
)
things = client.list_things_in_thing_group(thingGroupName=group_name)
assert "things" in things
assert len(things["things"]) == 0
@mock_aws
def test_thing_group_already_exists_with_different_properties_raises():
client = boto3.client("iot", region_name="ap-northeast-1")
thing_group_name = "my-group-name"
client.create_thing_group(
thingGroupName=thing_group_name,
thingGroupProperties={"thingGroupDescription": "Current description"},
)
with pytest.raises(
client.exceptions.ResourceAlreadyExistsException,
match=f"Thing Group {thing_group_name} already exists in current account with different properties",
):
client.create_thing_group(thingGroupName=thing_group_name)
@mock_aws
def test_thing_group_already_exists_with_same_properties_returned():
client = boto3.client("iot", region_name="ap-northeast-1")
thing_group_name = "my-group-name"
thing_group_properties = {"thingGroupDescription": "Current description"}
current_thing_group = client.create_thing_group(
thingGroupName=thing_group_name, thingGroupProperties=thing_group_properties
)
current_thing_group.pop("ResponseMetadata")
thing_group = client.create_thing_group(
thingGroupName=thing_group_name, thingGroupProperties=thing_group_properties
)
thing_group.pop("ResponseMetadata")
assert thing_group == current_thing_group
@mock_aws
def test_thing_group_updates_description():
client = boto3.client("iot", region_name="ap-northeast-1")
name = "my-thing-group"
new_description = "new description"
client.create_thing_group(
thingGroupName=name,
thingGroupProperties={"thingGroupDescription": "initial-description"},
)
client.update_thing_group(
thingGroupName=name,
thingGroupProperties={"thingGroupDescription": new_description},
)
thing_group = client.describe_thing_group(thingGroupName=name)
assert (
thing_group["thingGroupProperties"]["thingGroupDescription"] == new_description
)
@mock_aws
def test_thing_group_update_with_no_previous_attributes_no_merge():
client = boto3.client("iot", region_name="ap-northeast-1")
name = "my-group-name"
client.create_thing_group(thingGroupName=name)
client.update_thing_group(
thingGroupName=name,
thingGroupProperties={
"attributePayload": {
"attributes": {
"key1": "val01",
},
"merge": False,
}
},
)
updated_thing_group = client.describe_thing_group(thingGroupName=name)
assert updated_thing_group["thingGroupProperties"]["attributePayload"][
"attributes"
] == {"key1": "val01"}
@mock_aws
def test_thing_group_update_with_no_previous_attributes_with_merge():
client = boto3.client("iot", region_name="ap-northeast-1")
name = "my-group-name"
client.create_thing_group(thingGroupName=name)
client.update_thing_group(
thingGroupName=name,
thingGroupProperties={
"attributePayload": {
"attributes": {
"key1": "val01",
},
"merge": True,
}
},
)
updated_thing_group = client.describe_thing_group(thingGroupName=name)
assert updated_thing_group["thingGroupProperties"]["attributePayload"][
"attributes"
] == {"key1": "val01"}
@mock_aws
def test_thing_group_updated_with_empty_attributes_no_merge_no_attributes_added():
client = boto3.client("iot", region_name="ap-northeast-1")
name = "my-group-name"
client.create_thing_group(thingGroupName=name)
client.update_thing_group(
thingGroupName=name,
thingGroupProperties={
"attributePayload": {
"attributes": {},
"merge": False,
}
},
)
updated_thing_group = client.describe_thing_group(thingGroupName=name)
assert "attributePayload" not in updated_thing_group["thingGroupProperties"]
|