Skip to content

Commit

Permalink
refactor tests + commands (#503)
Browse files Browse the repository at this point in the history
* refactor tests + commands
* update help
  • Loading branch information
vilit1 authored Mar 22, 2022
1 parent 5fcd9a9 commit 57ec71d
Show file tree
Hide file tree
Showing 13 changed files with 1,239 additions and 1,118 deletions.
6 changes: 1 addition & 5 deletions CredScanSuppressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,10 @@
"_justification": "Completely made up keys for unit tests."
},
{
"file": "azext_iot\\tests\\dps\\test_iot_dps_int.py",
"file": "azext_iot\\tests\\dps\\enrollment_group\\test_iot_dps_enrollment_group_int.py",
"placeholder": "cT/EXZvsplPEpT//p98Pc6sKh8mY3kYgSxavHwMkl7w=",
"_justification": "Ensure made up endorsement key evaluates to the expected device key."
},
{
"file": "azext_iot\\tests\\dps\\test_iot_dps_unit.py",
"_justification": "Completely made up keys for unit tests."
},
{
"file": "azext_iot\\tests\\iothub\\core\\test_iot_ext_unit.py",
"placeholder": "+XLy+MVZ+aTeOnVzN2kLeB16O+kSxmz6g3rS6fAf6rw=",
Expand Down
23 changes: 16 additions & 7 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Release History

**General updates**

* The generic CLIErrors raised across the extension have been changed to more specific semantically correct exceptions aligning with CLI core.
* The generic CLIErrors raised across the extension have been changed to more specific semantically correct exceptions aligning with CLI core.
* Fix for issue #475 resolving `sys.excepthook` upon terminating monitor-events process in Py 3.9+ environments [IoT Hub, IoT Central].

**Digital Twin updates**
Expand All @@ -19,6 +19,12 @@ Release History

* Updated both controlplane and dataplane SDKs to now use the newer 2021-06-30-preview API version.

* Added `--no-wait` parameter to the following functions:

- az dt create
- az dt endpoint create
- az dt private-endpoint create

**IoT Central updates**

* Added commands for Edge devices and modules:
Expand All @@ -29,17 +35,20 @@ Release History

- az iot central device edge manifest
- az iot central device edge manifest show

- az iot central device edge children
- az iot central device edge children list
- az iot central device edge children add
- az iot central device edge children remove

* Added `--no-wait` parameter to the following functions:

- az dt create
- az dt endpoint create
- az dt private-endpoint create
**IoT DPS updates**

* Reorganizing command structure for enrollment-group commands:
- 'az iot dps compute-device-key' is deprecated use 'az iot dps enrollment-group compute-device-key' instead.
- 'az iot dps registration' is deprecated use 'az iot dps enrollment-group registration' instead.
- 'az iot dps registration delete' is deprecated use 'az iot dps enrollment-group registration delete' instead.
- 'az iot dps registration list' is deprecated use 'az iot dps enrollment-group registration list' instead.
- 'az iot dps registration show' is deprecated use 'az iot dps enrollment-group registration show' instead.


0.13.0
Expand Down
48 changes: 48 additions & 0 deletions azext_iot/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,54 @@
short-summary: Delete an enrollment group in an Azure IoT Hub Device Provisioning Service.
"""

helps[
"iot dps enrollment-group registration"
] = """
type: group
short-summary: Manage device registrations for an enrollment group in an Azure IoT Hub Device
Provisioning Service.
"""

helps[
"iot dps enrollment-group registration list"
] = """
type: command
short-summary: List device registrations for an enrollment group in an Azure IoT Hub Device
Provisioning Service.
"""

helps[
"iot dps enrollment-group registration show"
] = """
type: command
short-summary: Get a device registration for an enrollment group in an Azure IoT Hub Device
Provisioning Service.
"""

helps[
"iot dps enrollment-group registration delete"
] = """
type: command
short-summary: Delete a device registration for an enrollment group in an Azure IoT Hub Device
Provisioning Service.
"""

helps[
"iot dps enrollment-group compute-device-key"
] = """
type: command
short-summary: Generate a derived device SAS key for an enrollment group in an Azure IoT Hub Device
Provisioning Service.
examples:
- name: Compute the device key with the given symmetric key.
text: >
az iot dps enrollment-group compute-device-key --key {enrollement_group_symmetric_key} --registration-id {registration_id}
- name: Compute the device key with the given enrollment group.
text: >
az iot dps enrollment-group compute-device-key -g {resource_group_name} --dps-name {dps_name}
--enrollment-id {enrollment_id} --registration-id {registration_id}
"""

helps[
"iot dps registration"
] = """
Expand Down
15 changes: 15 additions & 0 deletions azext_iot/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,11 @@ def load_arguments(self, _):
"If attestation with a root CA certificate is desired then a root ca name must be provided.",
arg_group="Authentication"
)
context.argument(
"registration_id",
options_list=["--registration-id", "--rid"],
help="ID of device registration."
)

with self.argument_context("iot dps enrollment-group show") as context:
context.argument(
Expand All @@ -1078,6 +1083,16 @@ def load_arguments(self, _):
help="Include attestation keys and information in enrollment group results.",
)

with self.argument_context("iot dps enrollment-group compute-device-key") as context:
context.argument(
"symmetric_key",
options_list=["--symmetric-key", "--key"],
help="The symmetric shared access key for the enrollment group. This bypasses the "
"Device Provisioning Service registry and generates the SAS token directly "
"from the supplied symmetric key without further validation. All other command "
"parameters aside from registration ID will be ignored.",
)

with self.argument_context("iot dps registration") as context:
context.argument("registration_id", help="ID of device registration.")

Expand Down
23 changes: 20 additions & 3 deletions azext_iot/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ def load_command_table(self, _):

with self.command_group("iot dps", command_type=iotdps_ops) as cmd_group:
cmd_group.command(
"compute-device-key", "iot_dps_compute_device_key", is_preview=True
"compute-device-key",
"iot_dps_compute_device_key",
deprecate_info=self.deprecate(
redirect='iot dps enrollment-group compute-device-key', hide=True
)
)

with self.command_group("iot dps enrollment", command_type=iotdps_ops) as cmd_group:
Expand All @@ -184,15 +188,28 @@ def load_command_table(self, _):
cmd_group.show_command("show", "iot_dps_device_enrollment_group_get")
cmd_group.command("update", "iot_dps_device_enrollment_group_update")
cmd_group.command("delete", "iot_dps_device_enrollment_group_delete")
cmd_group.command(
"compute-device-key", "iot_dps_compute_device_key"
)

with self.command_group(
"iot dps enrollment-group registration", command_type=iotdps_ops
) as cmd_group:
cmd_group.command("list", "iot_dps_registration_list")
cmd_group.show_command("show", "iot_dps_registration_get")
cmd_group.command("delete", "iot_dps_registration_delete")

with self.command_group(
"iot dps registration", command_type=iotdps_ops
"iot dps registration",
command_type=iotdps_ops,
deprecate_info=self.deprecate(redirect='iot dps enrollment-group registration', hide=True)
) as cmd_group:
cmd_group.command("list", "iot_dps_registration_list")
cmd_group.show_command("show", "iot_dps_registration_get")
cmd_group.command("delete", "iot_dps_registration_delete")

with self.command_group(
"iot dps connection-string", command_type=iotdps_ops
"iot dps connection-string",
command_type=iotdps_ops,
) as cmd_group:
cmd_group.show_command("show", "iot_dps_connection_string_show")
43 changes: 43 additions & 0 deletions azext_iot/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from azext_iot.tests.generators import generate_generic_id
from azext_iot.common.shared import DeviceAuthApiType

# Patch paths
path_get_device = "azext_iot.operations.hub._iot_device_show"
path_iot_hub_service_factory = "azext_iot._factory.iot_hub_service_factory"
path_service_client = "msrest.service_client.ServiceClient.send"
Expand All @@ -33,10 +34,16 @@
path_iot_device_show = "azext_iot.operations.hub._iot_device_show"
path_update_device_twin = "azext_iot.operations.hub._iot_device_twin_update"
hub_entity = "myhub.azure-devices.net"
path_iot_service_provisioning_factory = "azext_iot._factory.iot_service_provisioning_factory"
path_gdcs = "azext_iot.dps.providers.discovery.DPSDiscovery.get_target"
path_discovery_dps_init = (
"azext_iot.dps.providers.discovery.DPSDiscovery._initialize_client"
)

instance_name = generate_generic_id()
hostname = "{}.subdomain.domain".format(instance_name)

# Mock Iot Hub Target
mock_target = {}
mock_target["entity"] = hub_entity
mock_target["primarykey"] = "rJx/6rJ6rmG4ak890+eW5MYGH+A0uzRvjGNjg3Ve8sfo="
Expand All @@ -47,6 +54,22 @@
mock_target["sku_tier"] = "Standard"
mock_target["resourcegroup"] = "myresourcegroup"

# Mock Iot DPS Target
mock_dps_target = {}
mock_dps_target['cs'] = 'HostName=mydps;SharedAccessKeyName=name;SharedAccessKey=value'
mock_dps_target['entity'] = 'mydps'
mock_dps_target['primarykey'] = 'rJx/6rJ6rmG4ak890+eW5MYGH+A0uzRvjGNjg3Ve8sfo='
mock_dps_target['secondarykey'] = 'aCd/6rJ6rmG4ak890+eW5MYGH+A0uzRvjGNjg3Ve8sfo='
mock_dps_target['policy'] = 'provisioningserviceowner'
mock_dps_target['subscription'] = "5952cff8-bcd1-4235-9554-af2c0348bf23"

mock_symmetric_key_attestation = {
"type": "symmetricKey",
"symmetricKey": {
"primaryKey": "primary_key",
"secondaryKey": "secondary_key"
},
}

generic_cs_template = "HostName={};SharedAccessKeyName={};SharedAccessKey={}"

Expand Down Expand Up @@ -380,3 +403,23 @@ def fixture_dt_client(mocker, fixture_cmd):

def pytest_addoption(parser):
parser.addoption("--api-version", action="store", default=None)


# DPS Fixtures
@pytest.fixture()
def fixture_gdcs(mocker):
gdcs = mocker.patch(path_gdcs)
gdcs.return_value = mock_dps_target
mocker.patch(path_iot_service_provisioning_factory)
mocker.patch(path_discovery_dps_init)

return gdcs


@pytest.fixture()
def fixture_dps_sas(mocker):
r = SasTokenAuthentication(mock_dps_target['entity'],
mock_dps_target['policy'],
mock_dps_target['primarykey'])
sas = mocker.patch(path_sas)
sas.return_value = r
8 changes: 8 additions & 0 deletions azext_iot/tests/dps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@
PREFIX_GROUP_ENROLLMENT = "test-groupenroll-"
USER_ROLE = "Device Provisioning Service Data Contributor"

TEST_ENDORSEMENT_KEY = (
"AToAAQALAAMAsgAgg3GXZ0SEs/gakMyNRqXXJP1S124GUgtk8qHaGzMUaaoABgCAAEMAEAgAAAAAAAEAibym9HQP9vxCGF5dVc1Q"
"QsAGe021aUGJzNol1/gycBx3jFsTpwmWbISRwnFvflWd0w2Mc44FAAZNaJOAAxwZvG8GvyLlHh6fGKdh+mSBL4iLH2bZ4Ry22cB3"
"CJVjXmdGoz9Y/j3/NwLndBxQC+baNvzvyVQZ4/A2YL7vzIIj2ik4y+ve9ir7U0GbNdnxskqK1KFIITVVtkTIYyyFTIR0BySjPrRI"
"Dj7r7Mh5uF9HBppGKQCBoVSVV8dI91lNazmSdpGWyqCkO7iM4VvUMv2HT/ym53aYlUrau+Qq87Tu+uQipWYgRdF11KDfcpMHqqzB"
"QQ1NpOJVhrsTrhyJzO7KNw=="
)

# Test Environment Variables
settings = DynamoSettings(
req_env_set=ENV_SET_TEST_IOTHUB_REQUIRED,
Expand Down
5 changes: 5 additions & 0 deletions azext_iot/tests/dps/enrollment/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# coding=utf-8
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
Loading

0 comments on commit 57ec71d

Please sign in to comment.