Skip to content

Commit

Permalink
Add test for dynamic policies
Browse files Browse the repository at this point in the history
  • Loading branch information
gerardparis committed Sep 22, 2016
1 parent 76f4fe8 commit 82724d7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
9 changes: 2 additions & 7 deletions api/docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.'))
sys.path.insert(0, os.path.abspath('..'))

# dist_packages_path = os.path.join(os.sep, 'usr', 'lib', 'python2.7', 'dist-packages')
# sys.path.insert(0, dist_packages_path)
# local_dist_packages_path = os.path.join(os.sep, 'usr', 'local', 'lib', 'python2.7', 'dist-packages')
# sys.path.insert(0, local_dist_packages_path)

from django.conf import settings
settings.configure()

Expand Down Expand Up @@ -297,7 +291,8 @@
# If true, do not generate a @detailmenu in the "Top" node's menu.
#texinfo_no_detailmenu = False

# -- Mocks
# -- Mocks: mock objects are added for those modules that are not declared in requirements.txt
# Note: when using a submodule like pyactive.controller, both module and submodule must be declared.

class Mock(MagicMock):
@classmethod
Expand Down
6 changes: 4 additions & 2 deletions api/registry/dynamic_policies/rules/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def __init__(self, rule_parsed, action, target, host):
:param rule_parsed: The rule parsed by the dsl_parser.
:type rule_parsed: **any** PyParsing type
:param action: The action assigned to this rule.
:type action: **any** PyParsing type
:param target: The target assigned to this rule.
:type target: **any** String type
:param host: The proxy host provided by the PyActive Middleware.
Expand Down Expand Up @@ -87,7 +89,7 @@ def stop_actor(self):
def start_rule(self):
"""
Method called afeter init to start the rule. Basically this method
allows to be called remotelly and calls the internal method
allows to be called remotely and calls the internal method
**check_metrics()** which subscribes the rule to all the workload
metrics necessaries.
"""
Expand Down Expand Up @@ -177,7 +179,7 @@ def _check_conditions(self, condition_list):

def get_target(self):
"""
Retrun the target assigned to this rule.
Return the target assigned to this rule.
:return: Return the target id assigned to this rule
:rtype: String type.
Expand Down
60 changes: 60 additions & 0 deletions api/registry/tests_dynamic_policies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import os
import mock
import redis

from django.conf import settings
from django.test import TestCase, override_settings

from .dsl_parser import parse
from registry.dynamic_policies.rules.rule import Rule


# Tests use database=10 instead of 0.
@override_settings(REDIS_CON_POOL=redis.ConnectionPool(host='localhost', port=6379, db=10),
STORLET_FILTERS_DIR=os.path.join("/tmp", "crystal", "storlet_filters"),
WORKLOAD_METRICS_DIR=os.path.join("/tmp", "crystal", "native_metrics"))
class DynamicPoliciesTestCase(TestCase):

def setUp(self):
self.r = redis.Redis(connection_pool=settings.REDIS_CON_POOL)

def tearDown(self):
self.r.flushdb()

def test_get_target_ok(self):
self.setup_dsl_parser_data()
has_condition_list, parsed_rule = parse('FOR TENANT:4f0279da74ef4584a29dc72c835fe2c9 WHEN metric1 > 5 DO SET compression')
target = '4f0279da74ef4584a29dc72c835fe2c9'
host = None
rule = Rule(parsed_rule, parsed_rule.action_list[0], target, host)
self.assertEqual(rule.get_target(), '4f0279da74ef4584a29dc72c835fe2c9')

@mock.patch('registry.dynamic_policies.rules.rule.Rule._do_action')
def test_action_is_not_triggered(self, mock_do_action):
self.setup_dsl_parser_data()
has_condition_list, parsed_rule = parse('FOR TENANT:4f0279da74ef4584a29dc72c835fe2c9 WHEN metric1 > 5 DO SET compression')
target = '4f0279da74ef4584a29dc72c835fe2c9'
host = None
rule = Rule(parsed_rule, parsed_rule.action_list[0], target, host)
rule.update('metric1', 3)
self.assertFalse(mock_do_action.called)

@mock.patch('registry.dynamic_policies.rules.rule.Rule._do_action')
def test_action_is_triggered(self, mock_do_action):
self.setup_dsl_parser_data()
has_condition_list, parsed_rule = parse('FOR TENANT:4f0279da74ef4584a29dc72c835fe2c9 WHEN metric1 > 5 DO SET compression')
target = '4f0279da74ef4584a29dc72c835fe2c9'
host = None
rule = Rule(parsed_rule, parsed_rule.action_list[0], target, host)
rule.update('metric1', 6)
self.assertTrue(mock_do_action.called)

#
# Aux methods
#

def setup_dsl_parser_data(self):
self.r.hmset('dsl_filter:compression', {'valid_parameters': '{"cparam1": "integer", "cparam2": "integer", "cparam3": "integer"}'})
self.r.hmset('dsl_filter:encryption', {'valid_parameters': '{"eparam1": "integer", "eparam2": "bool", "eparam3": "string"}'})
self.r.hmset('metric:metric1', {'network_location': '?', 'type': 'integer'})
self.r.hmset('metric:metric2', {'network_location': '?', 'type': 'integer'})

0 comments on commit 82724d7

Please sign in to comment.