예제 #1
0
    def test_config_create_adm(
        self,
        fixture_cmd,
        serviceclient,
        sample_config_adm,
        sample_config_metrics,
        config_id,
        hub_name,
        target_condition,
        priority,
        labels,
    ):

        contentKey = (
            "moduleContent"
            if sample_config_adm[0].startswith("module")
            else "deviceContent"
        )

        if contentKey == "moduleContent":
            # Enforce the query prefix for success the case
            target_condition = "FROM devices.modules WHERE {}".format(target_condition)

        subject.iot_hub_configuration_create(
            cmd=fixture_cmd,
            config_id=config_id,
            hub_name=hub_name,
            content=sample_config_adm[1],
            target_condition=target_condition,
            priority=priority,
            labels=labels,
            metrics=sample_config_metrics[1],
        )

        args = serviceclient.call_args
        url = args[0][0].url
        method = args[0][0].method
        body = json.loads(args[0][0].body)

        assert "{}/configurations/{}?".format(hub_name, config_id.lower()) in url
        assert method == "PUT"
        assert body["id"] == config_id.lower()
        assert body.get("targetCondition") == target_condition
        assert body.get("priority") == priority
        assert body.get("labels") == evaluate_literal(labels, dict)

        if sample_config_adm[0].endswith("Inline"):
            assert (
                body["content"][contentKey]
                == json.loads(sample_config_adm[1])["content"][contentKey]
            )
        elif sample_config_adm[0].endswith("File"):
            assert (
                body["content"][contentKey]
                == json.loads(read_file_content(sample_config_adm[1]))["content"][
                    contentKey
                ]
            )

        self._assert_config_metrics_request(sample_config_metrics, body)
예제 #2
0
    def test_config_create_adm_invalid(
        self,
        fixture_cmd2,
        serviceclient,
        config_id,
        hub_name,
        target_condition,
        priority,
        labels,
    ):
        with pytest.raises(CLIError) as exc1:
            subject.iot_hub_configuration_create(
                cmd=fixture_cmd2,
                config_id=config_id,
                hub_name=hub_name,
                content=get_context_path(__file__,
                                         "test_edge_deployment.json"),
                target_condition=target_condition,
                priority=priority,
                labels=labels,
            )

        # API does not support both deviceContent and moduleContent at the same time.
        content = json.dumps({"deviceContent": {}, "moduleContent": {}})
        with pytest.raises(CLIError) as exc2:
            subject.iot_hub_configuration_create(
                cmd=fixture_cmd2,
                config_id=config_id,
                hub_name=hub_name,
                content=content,
                target_condition=target_condition,
                priority=priority,
                labels=labels,
            )

        for exc in [exc1, exc2]:
            assert (
                str(exc.value) ==
                "Automatic device configuration payloads require property: deviceContent or moduleContent"
            )

        # Module configurations target condition needs to start with 'from devices.modules where'
        content = json.dumps({"moduleContent": {"key": "value"}})
        with pytest.raises(CLIError) as exc3:
            subject.iot_hub_configuration_create(
                cmd=fixture_cmd2,
                config_id=config_id,
                hub_name=hub_name,
                content=content,
                target_condition=target_condition,
                priority=priority,
                labels=labels,
            )

        assert (
            str(exc3.value) ==
            "The target condition for a module configuration must start with 'from devices.modules where'"
        )