Merge "Protect PoolStats table from negative values."

This commit is contained in:
Jenkins 2013-06-11 15:19:50 +00:00 committed by Gerrit Code Review
commit 67b36b7761
2 changed files with 26 additions and 0 deletions

View File

@ -19,6 +19,7 @@ import sqlalchemy as sa
from sqlalchemy import exc as sa_exc from sqlalchemy import exc as sa_exc
from sqlalchemy import orm from sqlalchemy import orm
from sqlalchemy.orm import exc from sqlalchemy.orm import exc
from sqlalchemy.orm import validates
from quantum.api.v2 import attributes from quantum.api.v2 import attributes
from quantum.common import exceptions as q_exc from quantum.common import exceptions as q_exc
@ -59,6 +60,16 @@ class PoolStatistics(model_base.BASEV2):
active_connections = sa.Column(sa.Integer, nullable=False) active_connections = sa.Column(sa.Integer, nullable=False)
total_connections = sa.Column(sa.Integer, nullable=False) total_connections = sa.Column(sa.Integer, nullable=False)
@validates('bytes_in', 'bytes_out',
'active_connections', 'total_connections')
def validate_non_negative_int(self, key, value):
if value < 0:
data = {'key': key, 'value': value}
raise ValueError(_('The %(key)s field can not have '
'negative value. '
'Current value is %(value)d.') % data)
return value
class Vip(model_base.BASEV2, models_v2.HasId, models_v2.HasTenant): class Vip(model_base.BASEV2, models_v2.HasId, models_v2.HasTenant):
"""Represents a v2 quantum loadbalancer vip.""" """Represents a v2 quantum loadbalancer vip."""

View File

@ -936,6 +936,21 @@ class TestLoadBalancer(LoadBalancerPluginDbTestCase):
for key in keys: for key in keys:
self.assertEqual(pool_obj.stats.__dict__[key], 0) self.assertEqual(pool_obj.stats.__dict__[key], 0)
def test_update_pool_stats_with_negative_values(self):
stats_data = {"bytes_in": -1,
"bytes_out": -2,
"active_connections": -3,
"total_connections": -4}
for k, v in stats_data.items():
self._test_update_pool_stats_with_negative_value(k, v)
def _test_update_pool_stats_with_negative_value(self, k, v):
with self.pool() as pool:
pool_id = pool['pool']['id']
ctx = context.get_admin_context()
self.assertRaises(ValueError, self.plugin._update_pool_stats,
ctx, pool_id, {k: v})
def test_update_pool_stats(self): def test_update_pool_stats(self):
stats_data = {"bytes_in": 1, stats_data = {"bytes_in": 1,
"bytes_out": 2, "bytes_out": 2,