Skip to content

Commit

Permalink
Add static/dynamic policy_detail tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gerardparis committed Oct 4, 2016
1 parent ce3ed4d commit d8ad2c2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
59 changes: 56 additions & 3 deletions api/registry/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from django.test import TestCase, override_settings
from django.conf import settings
from django.http import HttpResponse
from pyparsing import ParseException
from rest_framework import status
from rest_framework.test import APIRequestFactory
Expand All @@ -15,7 +14,7 @@
from filters.views import storlet_list, filter_deploy, StorletData
from .views import object_type_list, object_type_detail, add_tenants_group, tenants_group_detail, gtenants_tenant_detail, node_list, node_detail, \
add_metric, metric_detail, metric_module_list, metric_module_detail, MetricModuleData, list_storage_node, storage_node_detail, add_dynamic_filter, \
dynamic_filter_detail, load_metrics, load_policies
dynamic_filter_detail, load_metrics, load_policies, static_policy_detail, dynamic_policy_detail
from .dsl_parser import parse


Expand Down Expand Up @@ -850,7 +849,7 @@ def test_delete_individual_tenant_from_group_ok(self, mock_is_valid_request):
# Parse tests
#

# TODO To test dsl_parser correctly, we need to have metrics and filters in Redis.
# To test dsl_parser correctly, we need to have metrics and filters in Redis.

def test_parse_target_tenant_ok(self, mock_is_valid_request):
self.setup_dsl_parser_data()
Expand Down Expand Up @@ -951,6 +950,10 @@ def test_parse_rule_with_invalid_target(self, mock_is_valid_request):

# TODO Add tests for conditional rules

#
# load_metrics() / load_policies()
#

@mock.patch('registry.views.start_metric')
def test_load_metrics(self, mock_start_metric, mock_is_valid_request):
load_metrics()
Expand Down Expand Up @@ -979,6 +982,56 @@ def test_load_policies_alive_transient(self, mock_host, mock_is_valid_request):
load_policies()
self.assertTrue(mock_host.spawn_id.called)

#
# static_policy_detail()
#

@mock.patch('registry.views.get_project_list')
def test_registry_static_policy_detail_ok(self, mock_get_project_list, mock_is_valid_request):
mock_is_valid_request.return_value = 'fake_token'
mock_get_project_list.return_value = {'0123456789abcdef': 'tenantA', '2': 'tenantB'}

# Create an instance of a GET request.
request = self.factory.get('/registry/static_policy/0123456789abcdef:1')
request.META['HTTP_X_AUTH_TOKEN'] = 'fake_token'
response = static_policy_detail(request, '0123456789abcdef:1')
self.assertEqual(response.status_code, status.HTTP_200_OK)
json_data = json.loads(response.content)
self.assertEqual(json_data["target_name"], 'tenantA')

@mock.patch('registry.views.get_project_list')
def test_registry_static_policy_detail_delete(self, mock_get_project_list, mock_is_valid_request):
mock_is_valid_request.return_value = 'fake_token'
mock_get_project_list.return_value = {'0123456789abcdef': 'tenantA', '2': 'tenantB'}

# Create an instance of a DELETE request.
request = self.factory.delete('/registry/static_policy/0123456789abcdef:1')
request.META['HTTP_X_AUTH_TOKEN'] = 'fake_token'
response = static_policy_detail(request, '0123456789abcdef:1')
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

# Check there is no policy
request = self.factory.get('/registry/static_policy')
request.META['HTTP_X_AUTH_TOKEN'] = 'fake_token'
response = policy_list(request)
self.assertEqual(response.status_code, status.HTTP_200_OK)
json_data = json.loads(response.content)
self.assertEqual(len(json_data), 0)

#
# dynamic_policy_detail()
#

def test_registry_dynamic_policy_detail_with_method_not_allowed(self, mock_is_valid_request):
mock_is_valid_request.return_value = 'fake_token'
request = self.factory.get('/registry/dynamic_policy/123')
response = dynamic_policy_detail(request, '123')
self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)





#
# Aux methods
#
Expand Down
4 changes: 2 additions & 2 deletions api/registry/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ def policy_list(request):
@csrf_exempt
def static_policy_detail(request, policy_id):
"""
Retrieve, update or delete SLA.
Retrieve, update or delete a static policy.
"""
# Validate request: only a user with admin role can access to this method
token = is_valid_request(request)
Expand Down Expand Up @@ -935,7 +935,7 @@ def static_policy_detail(request, policy_id):
@csrf_exempt
def dynamic_policy_detail(request, policy_id):
"""
Retrieve, update or delete SLA.
Delete a dynamic policy.
"""
# Validate request: only a user with admin role can access to this method
token = is_valid_request(request)
Expand Down

0 comments on commit d8ad2c2

Please sign in to comment.