コード例 #1
0
    def __init__(self, *args, **kwargs):
        super(Locust, self).__init__(*args, **kwargs)

        # Connects to Redis database that stores the device_id for each client
        cache = RedisClient()

        revoke = cache.has_to_revoke()
        should_revoke = False
        should_renew = False

        device_id = None
        # We need to differentiate the device IDs to be revogated/renewed from the other ones
        # The revogated/renewed ones will not be stored in Redis; instead, they will be created
        # at runtime
        if revoke:
            should_revoke = revoke["should_revoke"]
            device_id = revoke["device_id"]
        else:
            renew = cache.has_to_renew()

            if renew:
                should_renew = renew["should_renew"]
                device_id = renew["device_id"]
            else:
                device_id = cache.next_device_id()

        # UUID to identify the client run
        run_id = str(uuid.uuid4())

        self.client = MQTTClient(device_id, run_id, should_revoke,
                                 should_renew)
        self.client.setup()
        self.client.connect()
コード例 #2
0
    def test_exception(self, mock_utils, mock_redis):
        """
        Should raise an exception.
        """
        mock_redis.Redis.return_value = MagicMock()
        client = RedisClient()
        client.mapped.eval.side_effect = Exception()

        should_renew = client.has_to_renew()

        client.mapped.eval.assert_called_once()
        mock_utils.create_logger().error.assert_called_once()
        self.assertIsNone(should_renew)
コード例 #3
0
    def test_should_not_renew(self, mock_utils, mock_redis):
        """
        Should not renew because the script returned 0.
        """
        mock_redis.Redis.return_value = MagicMock()
        client = RedisClient()
        client.mapped.eval.return_value = 0

        should_renew = client.has_to_renew()

        client.mapped.eval.assert_called_once()
        mock_utils.create_logger().error.assert_not_called()
        self.assertIsNone(should_renew)
コード例 #4
0
    def test_should_not_renew_config(self, mock_utils, mock_redis):
        """
        Should not renew.
        """
        mock_redis.Redis.return_value = MagicMock()
        client = RedisClient()
        mock_config = {'security': {'renew_devices': False}}
        patch.dict('src.mqtt_locust.redis_client.CONFIG', mock_config)

        should_renew = client.has_to_renew()

        client.mapped.eval.assert_not_called()
        mock_utils.create_logger().error.assert_not_called()
        self.assertIsNone(should_renew)
コード例 #5
0
    def test_should_renew(self, mock_utils, mock_redis):
        """
        Should renew.
        """
        mock_redis.Redis.return_value = MagicMock()
        client = RedisClient()
        client.mapped.eval.return_value = 1
        client.get_device_id = MagicMock(return_value="testID")

        should_renew = client.has_to_renew()

        client.mapped.eval.assert_called_once()
        mock_utils.create_logger().error.assert_not_called()
        self.assertIsNotNone(should_renew)
        self.assertEqual(should_renew['device_id'], "testID")
        self.assertTrue(should_renew['should_renew'])