Пример #1
0
    def test_generate_certificate(self, mock_requests):
        """
        Test generate certificate
        """
        DojotAPI.generate_certificate(self.jwt, self.csr)

        DojotAPI.call_api.assert_called_once_with(mock_requests.post, self.args)
Пример #2
0
    def test_revoke_certificate(self, mock_requests):
        """
        Should revoke the certificate correctly.
        """

        DojotAPI.revoke_certificate(self.jwt, self.crt['fingerprint'])

        DojotAPI.call_api.assert_called_once_with(mock_requests.delete, self.args, False)
Пример #3
0
    def test_revoke_certificate_exception(self, mock_requests):
        """
        Should not generate cert, because rose an exception.
        """
        DojotAPI.call_api.side_effect = APICallError()

        with self.assertRaises(Exception) as context:
            DojotAPI.revoke_certificate(self.jwt, self.crt['fingerprint'])

        self.assertIsNotNone(context.exception)
        self.assertIsInstance(context.exception, APICallError)

        DojotAPI.call_api.assert_called_once_with(mock_requests.delete, self.args, False)
Пример #4
0
    def test_generate_certificate_exception(self, mock_requests):
        """
        Should not generate the cert, because rose an exception.
        """
        DojotAPI.call_api.side_effect = APICallError()

        with self.assertRaises(Exception) as context:
            DojotAPI.generate_certificate(self.jwt, self.csr)

        self.assertIsNotNone(context.exception)
        self.assertIsInstance(context.exception, APICallError)

        DojotAPI.call_api.assert_called_once_with(mock_requests.post, self.args)
Пример #5
0
    def create_devices(self):
        """
        Create the devices in Dojot.

        Returns a list with device IDs.
        """
        try:
            template_id = DojotAPI.create_template(self.jwt)
            DojotAPI.create_devices(self.jwt, template_id,
                                    self.parser_args.devices,
                                    self.parser_args.batch)

        except Exception as exception:
            LOGGER.error(str(exception))
Пример #6
0
    def test_successfully_delete_templates(self, mock_requests):
        """
        Should successfully delete templates.
        """
        args = {
            "url": "{0}/template".format(MOCK_CONFIG['dojot']['url']),
            "headers": {
                "Authorization": "Bearer {0}".format(self.jwt),
            },
        }

        DojotAPI.delete_templates(self.jwt)

        DojotAPI.call_api.assert_called_once()
        DojotAPI.call_api.assert_called_with(mock_requests.delete, args, False)
Пример #7
0
    def test_successfully_create_template(self, mock_requests):
        """
        Should successfully create the template.
        """
        DojotAPI.call_api.return_value = {"template": {"id": 1}}

        args = {
            "url": "{0}/template".format(MOCK_CONFIG['dojot']['url']),
            "headers": {
                "Content-Type": "application/json",
                "Authorization": "Bearer {0}".format(self.jwt),
            },
            "data": json.dumps({
                "label": "CargoContainer",
                "attrs": [
                    {
                        "label": "timestamp",
                        "type": "dynamic",
                        "value_type": "integer"
                    },
                ]
            }),
        }

        template_id = DojotAPI.create_template(self.jwt)

        DojotAPI.call_api.assert_called_once_with(mock_requests.post, args)
        self.assertEqual(template_id, 1)
Пример #8
0
    def test_successfully_divide_loads_low_n(self):
        """
        Should successfully divide the load - n < batch.
        """
        loads = DojotAPI.divide_loads(2, 4)

        self.assertEqual(loads, [2])
Пример #9
0
    def test_successfully_create_device(self, mock_requests):
        """
        Should successfully create the device.
        """
        DojotAPI.call_api.return_value = {"devices": [{"id": 1}]}

        args = {
            "url":
            "{0}/device".format(MOCK_CONFIG['dojot']['url']),
            "headers": {
                "Content-Type": "application/json",
                "Authorization": "Bearer {0}".format(self.jwt),
            },
            "data":
            json.dumps({
                "templates": [self.template_id],
                "attrs": {},
                "label": self.label,
            }),
        }

        device_id = DojotAPI.create_device(self.jwt, self.template_id,
                                           self.label)

        DojotAPI.call_api.assert_called_once_with(mock_requests.post, args)
        self.assertEqual(device_id, 1)
Пример #10
0
    def test_should_get_response_after_retry(self, mock_requests, mock_gevent):
        """
        Should successfully receive a response from a POST call in the second call.
        """
        mock_requests.post = MagicMock()
        mock_requests.post.return_value.raise_for_status = MagicMock(
            side_effect=[Exception(), None]
        )
        mock_requests.post.return_value.json = PropertyMock(return_value={"return": "testReturn"})

        args = {
            "url": "testURL",
            "data": "\"testJson\": \"testData\"",
            "headers": "testHeader"
        }

        res = DojotAPI.call_api(mock_requests.post, args)

        self.assertEqual(mock_requests.post.call_count, 2)
        mock_requests.post.assert_has_calls([
            mock.call(**args),
            mock.call(**args),
        ], any_order=True)

        mock_gevent.sleep.assert_called_once_with(5.0)

        self.assertEqual(res, {"return": "testReturn"})
Пример #11
0
    def test_should_not_get_response(self, mock_requests, mock_gevent):
        """
        Should not receive a response from a POST call - exceptions rose.
        """
        mock_requests.post = MagicMock()
        mock_requests.post.return_value.raise_for_status = MagicMock(side_effect=Exception())

        args = {
            "url": "testURL",
            "data": "\"testJson\": \"testData\"",
            "headers": "testHeader"
        }

        res = None
        with self.assertRaises(APICallError) as context:
            res = DojotAPI.call_api(mock_requests.post, args)

        self.assertIsNotNone(context.exception)
        self.assertIsInstance(context.exception, APICallError)

        self.assertEqual(mock_requests.post.call_count, 2)
        mock_requests.post.assert_has_calls([
            mock.call(**args),
            mock.call(**args),
        ], any_order=True)

        self.assertEqual(mock_gevent.sleep.call_count, 2)
        mock_gevent.sleep.assert_has_calls([mock.call(5.0), mock.call(5.0)])

        self.assertIsNone(res)
Пример #12
0
    def test_successfully_divide_loads_odd(self):
        """
        Should successfully divide the load - odd case (last load is different).
        """
        loads = DojotAPI.divide_loads(5, 2)

        self.assertEqual(loads, [2, 2, 1])
Пример #13
0
    def generate_certificate(self) -> str:
        """
        Generate the certificates.

        Returns the pem certificate.
        """
        return DojotAPI.generate_certificate(self.jwt, self.csr["pem"])
Пример #14
0
    def test_successfully_divide_loads_even(self):
        """
        Should successfully divide the load - even case (all loads are the same).
        """
        loads = DojotAPI.divide_loads(4, 2)

        self.assertEqual(loads, [2, 2])
Пример #15
0
    def test_should_not_get_response_limit_calls(self, mock_requests, mock_gevent):
        """
        Should not receive a response from a POST call - limit of calls to the API.
        """
        mock_requests.post = MagicMock()
        mock_requests.post.return_value.status_code = 429
        mock_requests.post.return_value.raise_for_status = \
            MagicMock(side_effect=requests.exceptions.HTTPError())

        args = {
            "url": "testURL",
            "data": "\"testJson\": \"testData\"",
            "headers": "testHeader"
        }

        res = None
        with self.assertRaises(SystemExit) as context:
            res = DojotAPI.call_api(mock_requests.post, args)

        self.assertIsNotNone(context.exception)
        self.assertIsInstance(context.exception, SystemExit)

        mock_requests.post.assert_called_once_with(**args)
        mock_gevent.sleep.assert_not_called()

        self.assertIsNone(res)
Пример #16
0
    def test_successfully_create_one_device_one_batch(self, mock_requests):
        """
        Should successfully create one device in one batch.
        """
        DojotAPI.divide_loads.return_value = [1]

        self.args["data"] = json.dumps({
            "templates": [self.template_id],
            "attrs": {},
            "label": "CargoContainer_0"
        })

        DojotAPI.create_devices(self.jwt, self.template_id, 1, 1)

        DojotAPI.divide_loads.assert_called_once_with(1, 1)
        self.assertEqual(DojotAPI.divide_loads.return_value, [1])
        DojotAPI.call_api.assert_called_once_with(mock_requests.post, self.args, False)
Пример #17
0
    def run(self):
        """
        Runs the commands for each parser.
        """
        if self.parser_args.topic == "cert":
            self.jwt = DojotAPI.get_jwt()
            self.cert_commands()

        elif self.parser_args.topic == "dojot":
            self.jwt = DojotAPI.get_jwt()
            if self.parser_args.dojot == "create":
                self.dojot_create_commands()
            if self.parser_args.dojot == "clear":
                self.dojot_clear_commands()

        elif self.parser_args.topic == "redis":
            self.redis_commands()
Пример #18
0
    def test_retrieve_ca_cert(self, mock_requests):
        """
        Should retrieve the CA certificate correctly.
        """
        DojotAPI.call_api.return_value = {"caPem": "testPem"}

        res = DojotAPI.retrieve_ca_cert(self.jwt)

        self.assertEqual(res, "testPem")
        DojotAPI.call_api.assert_called_once_with(mock_requests.get, self.args)
Пример #19
0
    def cert_commands(self):
        """
        Certificate creation commands.
        """
        if self.parser_args.remove and os.path.exists(
                CONFIG['security']['cert_dir']):
            LOGGER.info("Removing certificates...")
            # We don't remove the cert directory because it can be mounted as a volume and it will
            # throw the 'Resource busy' error, we remove everything inside it instead
            for path in glob.glob(
                    os.path.join(CONFIG['security']['cert_dir'], '*')):
                if os.path.isfile(path):
                    os.remove(path)
                if os.path.isdir(path):
                    shutil.rmtree(path)

            LOGGER.info("... Removed certificates")

            LOGGER.info("Creating certificates directories...")
            revoke_dir = os.path.join(CONFIG['security']['cert_dir'],
                                      CONFIG['security']['revoke_cert_dir'])
            renew_dir = os.path.join(CONFIG['security']['cert_dir'],
                                     CONFIG['security']['renew_cert_dir'])
            os.makedirs(renew_dir, exist_ok=True)
            os.makedirs(revoke_dir, exist_ok=True)
            LOGGER.info("... Created certificates directories")

        if self.parser_args.devices is not None:
            if self.parser_args.processes > self.parser_args.devices:
                LOGGER.error(
                    "The number of certificates must be greather than the number of processes!"
                )
                sys.exit(1)
            # Generating the random IDs
            ids = [
                str(uuid.uuid4().hex) for _ in range(self.parser_args.devices)
            ]
            # Begins the certificate generation for random devices IDs
            self.generate_certs(ids)

        if self.parser_args.ids is not None:
            # Begins the certificate generation
            self.generate_certs(self.parser_args.ids)

        if self.parser_args.dojot:
            devices_ids = DojotAPI.get_devices(self.jwt)
            self.generate_certs(devices_ids)

        # Exports the certificates' files
        self.export_certs()
        # Retrieving the CA certificate
        self.retrieve_ca_cert()
        # Mapping the certificates
        self.map_device_ids()
Пример #20
0
    def test_successfully_create_two_devices_two_batches(self, mock_requests):
        """
        Should successfully create two devices in two batches.
        """
        DojotAPI.divide_loads.return_value = [1, 1]

        self.args["data"] = json.dumps({
            "templates": [self.template_id],
            "attrs": {},
            "label": "CargoContainer_1"
        })

        DojotAPI.create_devices(self.jwt, self.template_id, 2, 2)

        DojotAPI.divide_loads.assert_called_once_with(2, 2)
        self.assertEqual(DojotAPI.divide_loads.return_value, [1, 1])
        self.assertEqual(DojotAPI.call_api.call_count, 2)
        DojotAPI.call_api.assert_has_calls([
            mock.call(mock_requests.post, self.args, False),
            mock.call(mock_requests.post, self.args, False)
        ])
Пример #21
0
    def get_template_id(self):
        """
        Retrieves the test template ID from the database or create a new one.
        """
        template_id = self.mapped.get('template_id').decode('utf-8')

        if template_id == "-1":
            jwt = self.get_jwt()
            template_id = DojotAPI.create_template(jwt)
            self.mapped.set('template_id', template_id)

        return template_id
Пример #22
0
    def get_jwt(self):
        """
        Retrieves the JWT from the dadtabase or create a new one.
        """
        jwt = self.mapped.get('jwt')

        if jwt:
            return jwt.decode('utf-8')

        jwt = DojotAPI.get_jwt()
        self.mapped.setex('jwt', CONFIG['locust']['redis']['jwt_expire_time'], jwt)

        return jwt
Пример #23
0
    def test_successfully_get_devices(self, mock_requests):
        """
        Should successfully get devices.
        """
        DojotAPI.call_api.side_effect = [{
            "devices": [{"id": 0}, {"id": 1}],
            "pagination": {
                "total": 2
            }
        }, [0], [1]]

        devices = DojotAPI.get_devices("testJWT")

        self.assertEqual(DojotAPI.call_api.call_count, 3)
        DojotAPI.call_api.assert_called_with(mock_requests.get, ANY)
        self.assertEqual(devices, [0, 1])
Пример #24
0
    def test_successfully_no_devices(self, mock_requests):
        """
        Should successfully get an empty list of devices.
        """
        DojotAPI.call_api.return_value = {
            "devices": [],
            "pagination": {
                "total": 0
            }
        }

        devices = DojotAPI.get_devices("testJWT")

        DojotAPI.call_api.assert_called_once()
        DojotAPI.call_api.assert_called_with(mock_requests.get, ANY)
        self.assertEqual(devices, [])
Пример #25
0
    def get_device_id(self):
        """
        Get the device ID.
        """
        device_id = None

        if CONFIG['dojot']['env']:
            jwt = self.get_jwt()
            template_id = self.get_template_id()
            device_id = DojotAPI.create_device(
                jwt, template_id,
                "CargoContainer_{0}".format(str(uuid4()).replace("-", "")))

        else:
            device_id = str(uuid4()).replace("-", "")

        return device_id
Пример #26
0
    def test_should_get_response(self, mock_requests, mock_gevent):
        """
        Should successfully receive a response from a POST call.
        """
        mock_requests.post = MagicMock()
        mock_requests.post.return_value.json = PropertyMock(return_value={"return": "testReturn"})

        args = {
            "url": "testURL",
            "data": "\"testJson\": \"testData\"",
            "headers": "testHeader"
        }

        res = DojotAPI.call_api(mock_requests.post, args)

        mock_requests.post.assert_called_once_with(**args)
        mock_gevent.sleep.assert_not_called()
        mock_requests.post.return_value.json.assert_called_once()
        self.assertEqual(res, {"return": "testReturn"})
Пример #27
0
    def test_should_not_return_json(self, mock_requests, mock_gevent):
        """
        Should successfully make a POST call but do not return a JSON.
        """
        mock_requests.post = MagicMock()
        mock_requests.post.return_value.json = PropertyMock(return_value={"return": "testReturn"})

        args = {
            "url": "testURL",
            "data": "\"testJson\": \"testData\"",
            "headers": "testHeader"
        }

        res = DojotAPI.call_api(mock_requests.post, args, False)

        mock_requests.post.assert_called_once_with(**args)
        mock_gevent.sleep.assert_not_called()
        mock_requests.post.return_value.json.assert_not_called()
        self.assertIsNone(res)
Пример #28
0
    def redis_commands(self):
        """
        Redis commands execution.
        """
        if self.parser_args.restore:
            self.restore_db_state()

        elif self.parser_args.clear:
            self.clear_db()
            self.restore_db_state()

        elif self.parser_args.map:
            self.map_device_ids()

        elif self.parser_args.export:
            # Retrieve JWT token
            self.jwt = DojotAPI.get_jwt()
            # Exports the certificates' files
            self.export_certs()
            # Retrieving the CA certificate
            self.retrieve_ca_cert()
Пример #29
0
    def test_successfully_get_jwt(self, mock_requests):
        """
        Should successfully get a JWT from Dojot.
        """
        args = {
            "url": "{0}/auth".format(MOCK_CONFIG['dojot']['url']),
            "data": json.dumps({
                "username": MOCK_CONFIG['dojot']['user'],
                "passwd": MOCK_CONFIG['dojot']['passwd'],
            }),
            "headers": {
                "Accept": "application/json",
                "Content-Type": "application/json"
            },
        }
        DojotAPI.call_api.return_value = {"jwt": "testJWT"}

        jwt = DojotAPI.get_jwt()

        DojotAPI.call_api.assert_called_once()
        DojotAPI.call_api.assert_called_with(mock_requests.post, args)
        self.assertEqual(jwt, "testJWT")
Пример #30
0
 def delete_templates(self, ) -> None:
     """
     Deletes all devices.
     """
     DojotAPI.delete_templates(self.jwt)