Beispiel #1
0
 def test_config_device_deploy(self, device_id):
     user_id = str(uuid.uuid4())
     client = management_api_with_params(user_id=user_id, tenant_id="tenant-id")
     #
     # set the initial configuration
     configuration = {
         "key": "value",
         "another-key": "another-value",
         "dollar-key": "$",
     }
     r = client.set_device_configuration(
         device_id, request_body=configuration, _preload_content=False
     )
     assert r.status == 204
     #
     # deploy the configuration
     request = {
         "retries": 1,
     }
     r = client.deploy_device_configuration(
         device_id, new_configuration_deployment=request, _preload_content=False
     )
     assert r.status == 200
     assert "deployment_id" in str(r.data)
     #
     # get the deployment ID
     r = client.get_device_configuration(device_id)
     data = r.to_dict()
     assert "deployment_id" in data.keys()
     assert "" != data["deployment_id"]
Beispiel #2
0
 def test_config_device_key_too_long(self, device_id):
     user_id = str(uuid.uuid4())
     client = management_api_with_params(user_id=user_id, tenant_id="tenant-id")
     configuration = {
         "k" * 4097: "value",
     }
     with pytest.raises(ManagementApiException) as excinfo:
         client.set_device_configuration(
             device_id, request_body=configuration, _preload_content=False
         )
     assert excinfo.value.status == 400
Beispiel #3
0
 def test_config_device_replace_key_with_empty_value(self, device_id):
     user_id = str(uuid.uuid4())
     client = management_api_with_params(user_id=user_id, tenant_id="tenant-id")
     #
     # get the configuration (empty)
     r = client.get_device_configuration(device_id)
     data = r.to_dict()
     assert {"id": device_id, "reported": {}, "configured": {}} == {
         k: data[k] for k in ("id", "reported", "configured")
     }
     assert "updated_ts" in data.keys()
     #
     # set the initial configuration
     configuration = {
         "key": "value",
         "another-key": "another-value",
     }
     r = client.set_device_configuration(
         device_id, request_body=configuration, _preload_content=False
     )
     assert r.status == 204
     #
     # get the configuration
     r = client.get_device_configuration(device_id)
     data = r.to_dict()
     assert {
         "id": device_id,
         "reported": {},
         "configured": {"key": "value", "another-key": "another-value"},
     } == {k: data[k] for k in ("id", "reported", "configured")}
     assert "updated_ts" in data.keys()
     #
     # replace the configuration
     configuration = {
         "key": "value",
         "another-key": "",
     }
     r = client.set_device_configuration(
         device_id, request_body=configuration, _preload_content=False
     )
     assert r.status == 204
     #
     # get the configuration
     r = client.get_device_configuration(device_id)
     data = r.to_dict()
     assert {
         "id": device_id,
         "reported": {},
         "configured": {"key": "value", "another-key": ""},
     } == {k: data[k] for k in ("id", "reported", "configured")}
     assert "updated_ts" in data.keys()
 def test_config_device_set_auditlog(self, device_id, mmock_url):
     user_id = str(uuid.uuid4())
     client = management_api_with_params(user_id=user_id, tenant_id="tenant-id")
     #
     # set the initial configuration
     configuration = {
         "key": "value",
         "another-key": "another-value",
         "dollar-key": "$",
     }
     r = client.set_device_configuration(
         device_id, request_body=configuration, _preload_content=False
     )
     assert r.status == 204
     #
     # verify the auditlogs
     res = requests.get(mmock_url + "/api/request/all")
     assert res.status_code == 200
     response = res.json()
     assert len(response) == 1
     expected = {
         "request": {
             "scheme": "http",
             "host": "mender-workflows-server",
             "port": "8080",
             "method": "POST",
             "path": "/api/v1/workflow/emit_auditlog",
             "queryStringParameters": {},
             "fragment": "",
             "headers": {
                 "Content-Type": ["application/json"],
                 "Accept-Encoding": ["gzip"],
                 "User-Agent": ["Go-http-client/1.1"],
             },
             "cookies": {},
         },
     }
     body = response[0]["request"].pop("body")
     del response[0]["request"]["headers"]["Content-Length"]
     assert expected["request"] == response[0]["request"]
     assert "set_configuration" in body
 def test_config_device_get(self, device_id):
     user_id = str(uuid.uuid4())
     management_client = management_api_with_params(user_id=user_id,
                                                    tenant_id="tenant-id")
     client = devices_api_with_params(device_id=device_id,
                                      tenant_id="tenant-id")
     #
     # set the initial configuration
     configuration = {
         "key": "value",
         "another-key": "another-value",
         "dollar-key": "$",
     }
     r = management_client.set_device_configuration(
         device_id, request_body=configuration, _preload_content=False)
     assert r.status == 204
     #
     # get the configuration
     data = client.get_device_configuration()
     assert data == {
         "key": "value",
         "another-key": "another-value",
         "dollar-key": "$",
     }
Beispiel #6
0
    def test_connect(self, clean_mongo, tenant_id=None):
        """
        Tests end-to-end connection between user and device and checks
        for basic expected responses.
        """

        dev = Device(tenant_id=tenant_id)
        user_id = str(uuid.uuid4())
        api_mgmt = management_api_with_params(user_id=user_id,
                                              tenant_id=tenant_id)
        try:
            api_mgmt.connect(
                "00000000-0000-0000-0000-000000000000",
                connection="Upgrade",
                upgrade="websocket",
                sec_websocket_key="mumbojumbo==",
                sec_websocket_version=13,
            )
        except management_api.ApiException as e:
            assert e.status == 404
            assert "device not found" in str(e.body)
        else:
            raise Exception("Expected status code 404")

        try:
            api_mgmt.connect(
                dev.id,
                connection="Upgrade",
                upgrade="websocket",
                sec_websocket_key="mumbojumbo==",
                sec_websocket_version=13,
            )
        except management_api.ApiException as e:
            assert e.status == 404
            assert "device not connected" in str(e.body)
        else:
            raise Exception("Expected status code 404")

        with dev.connect() as dev_conn:
            try:
                api_mgmt.connect(dev.id)
            except management_api.ApiException as e:
                assert e.status == 400
                assert "the client is not using the websocket protocol" in e.body
            else:
                raise Exception("Expected status code 400")

            try:
                api_mgmt.connect(
                    dev.id,
                    connection="Upgrade",
                    upgrade="websocket",
                    sec_websocket_key="mumbojumbo==",
                    sec_websocket_version=13,
                )
            except management_api.ApiException as e:
                assert e.status == 101
            else:
                raise Exception("Expected status code 101")

            with management_api_connect(dev.id,
                                        user_id=user_id,
                                        tenant_id=tenant_id) as user_conn:
                user_conn.send(
                    msgpack.dumps({
                        "hdr": {
                            "proto": 1,
                            "typ": "start",
                            "props": {
                                "status": "ok"
                            },
                        },
                    }))
                msg = dev_conn.recv()
                rsp = msgpack.loads(msg)
                assert "hdr" in rsp, "Message does not contain header"
                assert ("sid" in rsp["hdr"]
                        ), "Forwarded message should contain session ID"
                assert rsp == {
                    "hdr": {
                        "proto": 1,
                        "typ": "start",
                        "props": {
                            "status": "ok",
                            "user_id": user_id,
                        },
                        "sid": rsp["hdr"]["sid"],
                    },
                }
                dev_conn.send(
                    msgpack.dumps({
                        "hdr": {
                            "proto": 1,
                            "typ": "shell",
                            "props": {
                                "status": "ok"
                            },
                            "sid": rsp["hdr"]["sid"],
                        },
                        "body": b"sh-5.0$ ",
                    }))
                msg = user_conn.recv()
                assert msgpack.loads(msg) == {
                    "hdr": {
                        "proto": 1,
                        "typ": "shell",
                        "props": {
                            "status": "ok"
                        },
                        "sid": rsp["hdr"]["sid"],
                    },
                    "body": b"sh-5.0$ ",
                }

        try:
            api_mgmt.connect(
                dev.id,
                connection="Upgrade",
                upgrade="websocket",
                sec_websocket_key="mumbojumbo==",
                sec_websocket_version=13,
            )
        except management_api.ApiException as e:
            assert e.status == 404
        else:
            raise Exception("Expected status code 404")
    def test_connect(self, clean_mongo, tenant_id=None):
        """
        Tests end-to-end connection between user and device and checks
        for basic expected responses.
        """

        dev = Device(tenant_id=tenant_id)
        api_mgmt = management_api_with_params(user_id=str(uuid.uuid4()),
                                              tenant_id=tenant_id)
        try:
            api_mgmt.connect(
                "00000000-0000-0000-0000-000000000000",
                connection="Upgrade",
                upgrade="websocket",
                sec_websocket_key="mumbojumbo==",
                sec_websocket_version=13,
            )
        except management_api.ApiException as e:
            assert e.status == 404
            assert "device not found" in str(e.body)
        else:
            raise Exception("Expected status code 404")

        try:
            api_mgmt.connect(
                dev.id,
                connection="Upgrade",
                upgrade="websocket",
                sec_websocket_key="mumbojumbo==",
                sec_websocket_version=13,
            )
        except management_api.ApiException as e:
            assert e.status == 404
            assert "device not connected" in str(e.body)
        else:
            raise Exception("Expected status code 404")

        with dev.connect() as dev_conn:
            try:
                api_mgmt.connect(dev.id)
            except management_api.ApiException as e:
                assert e.status == 400
                assert "the client is not using the websocket protocol" in e.body
            else:
                raise Exception("Expected status code 400")

            try:
                api_mgmt.connect(
                    dev.id,
                    connection="Upgrade",
                    upgrade="websocket",
                    sec_websocket_key="mumbojumbo==",
                    sec_websocket_version=13,
                )
            except management_api.ApiException as e:
                assert e.status == 101
            else:
                raise Exception("Expected status code 101")

            with management_api_connect(dev.id,
                                        tenant_id=tenant_id) as user_conn:
                user_conn.send(msgpack.dumps({
                    "type": "shell",
                    "data": None,
                }))
                msg = dev_conn.recv()
                assert msgpack.loads(msg) == {
                    "type": "shell",
                    "data": None,
                    "session_id": "",
                    "status_code": 0
                }
                dev_conn.send(
                    msgpack.dumps({
                        "type": "shell",
                        "data": b"sh-5.0$ "
                    }))
                msg = user_conn.recv()
                assert msgpack.loads(msg) == {
                    "type": "shell",
                    "data": b"sh-5.0$ ",
                    "session_id": "",
                    "status_code": 0
                }

        try:
            api_mgmt.connect(
                dev.id,
                connection="Upgrade",
                upgrade="websocket",
                sec_websocket_key="mumbojumbo==",
                sec_websocket_version=13,
            )
        except management_api.ApiException as e:
            assert e.status == 404
        else:
            raise Exception("Expected status code 404")