Skip to content

Commit

Permalink
Fix for dsl_parser and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gerardparis committed Oct 5, 2016
1 parent ae69dc6 commit d2c41df
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 10 deletions.
63 changes: 58 additions & 5 deletions api/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
from datetime import datetime, timedelta
from rest_framework.test import APIRequestFactory

from .common_utils import get_all_registered_nodes, remove_extra_whitespaces, to_json_bools, rsync_dir_with_nodes, is_valid_request
from .common_utils import get_all_registered_nodes, remove_extra_whitespaces, to_json_bools, rsync_dir_with_nodes, is_valid_request, get_project_list, \
get_keystone_admin_auth
from .exceptions import FileSynchronizationException
from .startup import run as startup_run

# Tests use database=10 instead of 0.
@override_settings(REDIS_CON_POOL=redis.ConnectionPool(host='localhost', port=6379, db=10))
Expand Down Expand Up @@ -75,7 +77,7 @@ def test_rsync_dir_with_nodes_when_rsync_fails(self, mock_os_system):

@mock.patch('api.common_utils.get_keystone_admin_auth')
def test_is_valid_request_new_valid_token(self, mock_keystone_admin_auth):
not_expired_admin_token = FakeTokenData((datetime.now() + timedelta(hours=2)).strftime('%Y-%m-%dT%H:%M:%SZ'),
not_expired_admin_token = FakeTokenData((datetime.utcnow() + timedelta(minutes=5)).strftime('%Y-%m-%dT%H:%M:%SZ'),
{'roles': [{'name': 'admin'}, {'name': '_member_'}]})
mock_keystone_admin_auth.return_value.tokens.validate.return_value = not_expired_admin_token
request = self.factory.get('/')
Expand All @@ -94,7 +96,7 @@ def test_is_valid_request_new_valid_token(self, mock_keystone_admin_auth):

@mock.patch('api.common_utils.get_keystone_admin_auth')
def test_is_valid_request_new_expired_token(self, mock_keystone_admin_auth):
not_expired_admin_token = FakeTokenData((datetime.now() - timedelta(hours=2)).strftime('%Y-%m-%dT%H:%M:%SZ'),
not_expired_admin_token = FakeTokenData((datetime.utcnow() - timedelta(minutes=5)).strftime('%Y-%m-%dT%H:%M:%SZ'),
{'roles': [{'name': 'admin'}, {'name': '_member_'}]})
mock_keystone_admin_auth.return_value.tokens.validate.return_value = not_expired_admin_token
request = self.factory.get('/')
Expand All @@ -104,14 +106,47 @@ def test_is_valid_request_new_expired_token(self, mock_keystone_admin_auth):

@mock.patch('api.common_utils.get_keystone_admin_auth')
def test_is_valid_request_not_admin(self, mock_keystone_admin_auth):
not_expired_admin_token = FakeTokenData((datetime.now() + timedelta(hours=2)).strftime('%Y-%m-%dT%H:%M:%SZ'),
not_expired_admin_token = FakeTokenData((datetime.utcnow() + timedelta(minutes=5)).strftime('%Y-%m-%dT%H:%M:%SZ'),
{'roles': [{'name': '_member_'}]})
mock_keystone_admin_auth.return_value.tokens.validate.return_value = not_expired_admin_token
request = self.factory.get('/')
request.META['HTTP_X_AUTH_TOKEN'] = 'not_admin_token'
resp = is_valid_request(request)
self.assertFalse(resp)

@mock.patch('api.common_utils.get_keystone_admin_auth')
def test_is_valid_request_raises_exception(self, mock_keystone_admin_auth):
mock_keystone_admin_auth.return_value.tokens.validate.side_effect = Exception()
request = self.factory.get('/')
request.META['HTTP_X_AUTH_TOKEN'] = 'token'
resp = is_valid_request(request)
self.assertFalse(resp)

@mock.patch('api.common_utils.get_keystone_admin_auth')
def test_get_project_list_ok(self, mock_keystone_admin_auth):
fake_tenants_list = [FakeTenantData('1234567890abcdef', 'tenantA'), FakeTenantData('abcdef1234567890', 'tenantB')]
mock_keystone_admin_auth.return_value.tenants.list.return_value = fake_tenants_list
resp = get_project_list('token')
self.assertEquals(resp['1234567890abcdef'], 'tenantA')
self.assertEquals(resp['abcdef1234567890'], 'tenantB')

@override_settings(MANAGEMENT_ACCOUNT='mng_account', MANAGEMENT_ADMIN_USERNAME='mng_username', MANAGEMENT_ADMIN_PASSWORD='mng_pw',
KEYSTONE_URL='http://localhost:35357/v2.0')
@mock.patch('api.common_utils.keystone_client.Client')
def test_get_keystone_admin_auth_ok(self, mock_keystone_client):
get_keystone_admin_auth()
mock_keystone_client.assert_called_with(username='mng_username', tenant_name='mng_account', password='mng_pw', auth_url='http://localhost:35357/v2.0')

def test_startup_run_ok(self):
self.create_startup_fixtures()
startup_run()
self.assertEquals(self.r.hget('workload_metric:1', 'enabled'), 'False')
self.assertEquals(self.r.hget('workload_metric:2', 'enabled'), 'False')
self.assertFalse(self.r.exists('metric:metric1'))
self.assertFalse(self.r.exists('metric:metric2'))
self.assertEquals(self.r.hget('policy:1', 'alive'), 'False')
self.assertEquals(self.r.hget('policy:2', 'alive'), 'False')

#
# URL tests
#
Expand Down Expand Up @@ -155,8 +190,26 @@ def configure_usernames_and_passwords_for_nodes(self):
self.r.hmset('node:storagenode1', {'ssh_username': 'user1', 'ssh_password': 's3cr3t'})
self.r.hmset('node:storagenode2', {'ssh_username': 'user1', 'ssh_password': 's3cr3t'})

def create_startup_fixtures(self):
self.r.hmset('workload_metric:1', {'metric_name': 'm1.py', 'class_name': 'Metric1', 'execution_server': 'proxy', 'out_flow':'False',
'in_flow': 'False', 'enabled': 'True', 'id': '1'})
self.r.hmset('workload_metric:2', {'metric_name': 'm2.py', 'class_name': 'Metric2', 'execution_server': 'proxy', 'out_flow':'False',
'in_flow': 'False', 'enabled': 'True', 'id': '2'})
self.r.hmset('metric:metric1', {'network_location': '?', 'type': 'integer'})
self.r.hmset('metric:metric2', {'network_location': '?', 'type': 'integer'})
self.r.hmset('policy:1',
{'alive': 'True', 'policy_description': 'FOR TENANT:0123456789abcdef DO SET compression'})
self.r.hmset('policy:2',
{'alive': 'True', 'policy_description': 'FOR TENANT:0123456789abcdef DO SET encryption'})


class FakeTokenData:
def __init__(self, expires, user):
self.expires = expires
self.user = user
self.user = user


class FakeTenantData:
def __init__(self, id, name):
self.id = id
self.name = name
4 changes: 2 additions & 2 deletions api/registry/dsl_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def parse(input_string):
# TRANSIENT
transient = Literal("TRANSIENT")
action = Group(action("action") + oneOf(sfilter)("filter") +
Optional(with_params + params_list("params") +
Optional(Suppress("ON")+server_execution("server_execution"))) +
Optional(with_params + params_list("params")) +
Optional(Suppress("ON")+server_execution("server_execution")) +
Optional(transient("transient")))

action_list = Group(delimitedList(action))
Expand Down
4 changes: 3 additions & 1 deletion api/registry/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ def test_registry_static_policy_create_set_filter_ok(self, mock_set_filter, mock
self.setup_dsl_parser_data()

# Create an instance of a POST request.
data = "FOR TENANT:1234567890abcdef DO SET compression"
data = "FOR TENANT:1234567890abcdef DO SET compression WITH bw=2 ON PROXY TO OBJECT_TYPE=DOCS"
request = self.factory.post('/registry/static_policy', data, content_type='text/plain')
request.META['HTTP_X_AUTH_TOKEN'] = 'fake_token'
response = policy_list(request)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertTrue(mock_set_filter.called)
expected_policy_data = {'object_size': '', 'execution_order': 2, 'object_type': 'DOCS', 'params': mock.ANY, 'policy_id': 2, 'execution_server': 'PROXY'}
mock_set_filter.assert_called_with(mock.ANY, '1234567890abcdef', mock.ANY, expected_policy_data, 'fake_token')

@mock.patch('registry.views.deploy_policy')
def test_registry_dynamic_policy_create_ok(self, mock_deploy_policy, mock_is_valid_request):
Expand Down
4 changes: 2 additions & 2 deletions api/registry/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,8 +1001,8 @@ def do_action(request, r, rule_parsed):
if rule_parsed.object_list.object_size:
policy_data["object_size"] = [rule_parsed.object_list.object_size.operand,
rule_parsed.object_list.object_size.object_value]
if action_info.execution_server:
policy_data["execution_server"] = action_info.execution_server
if action_info.server_execution:
policy_data["execution_server"] = action_info.server_execution
if action_info.params:
policy_data["params"] = action_info.params

Expand Down

0 comments on commit d2c41df

Please sign in to comment.