Skip to content

Commit

Permalink
Testing regions
Browse files Browse the repository at this point in the history
  • Loading branch information
gerardparis committed Nov 9, 2017
1 parent 36079f7 commit 8c6b268
Showing 1 changed file with 19 additions and 126 deletions.
145 changes: 19 additions & 126 deletions api/swift_api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from rest_framework import status
from rest_framework.test import APIRequestFactory

from .views import storage_policies, locality_list, node_list, node_detail
from swift_api.views import storage_policies, locality_list, node_list, node_detail, regions


# Tests use database=10 instead of 0.
Expand All @@ -21,20 +21,12 @@ def setUp(self):
self.r = redis.Redis(connection_pool=settings.REDIS_CON_POOL)
# initializations
self.create_storage_policies()
# self.create_proxy_sorting()
self.create_nodes()
self.create_regions_and_zones()

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

# def test_tenants_list_with_method_not_allowed(self):
# """ Test that DELETE requests to tenants_list() return METHOD_NOT_ALLOWED """
#
# request = self.api_factory.delete('/swift/tenants')
# request.META['HTTP_X_AUTH_TOKEN'] = 'fake_token'
# response = tenants_list(request)
# self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)

def test_storage_policies_with_method_not_allowed(self):
""" Test that PUT requests to storage_policies() return METHOD_NOT_ALLOWED """

Expand All @@ -44,120 +36,10 @@ def test_storage_policies_with_method_not_allowed(self):

def test_locality_list_with_method_not_allowed(self):
""" Test that POST requests to locality_list() return METHOD_NOT_ALLOWED """

request = self.api_factory.post('/swift/locality/123456789abcdef/container1/object1.txt')
response = locality_list(request, '123456789abcdef', 'container1', 'object1.txt')
self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)

# def test_sort_list_with_method_not_allowed(self):
# """ Test that DELETE requests to sort_list() return METHOD_NOT_ALLOWED """
#
# request = self.api_factory.delete('/swift/sort_nodes')
# response = sort_list(request)
# self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
#
# def test_sort_detail_with_method_not_allowed(self):
# """ Test that POST requests to sort_list() return METHOD_NOT_ALLOWED """
#
# request = self.api_factory.post('/swift/sort_nodes/5')
# response = sort_detail(request, 5)
# self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
#
# def test_get_all_proxy_sortings_ok(self):
# request = self.api_factory.get('/swift/sort_nodes')
# response = sort_list(request)
# self.assertEqual(response.status_code, status.HTTP_200_OK)
# self.assertNotEqual(response.content, "[]")
# proxy_sortings = json.loads(response.content)
# self.assertEqual(len(proxy_sortings), 1)
# self.assertEqual(proxy_sortings[0]['name'], 'FakeProxySorting')
#
# def test_create_proxy_sorting_ok(self):
# # Create a second proxy sorting
#
# proxy_sorting_data = {'name': 'SecondProxySorting', 'criterion': 'second_criterion'}
# request = self.api_factory.post('/swift/sort_nodes', proxy_sorting_data, format='json')
# response = sort_list(request)
# self.assertEqual(response.status_code, status.HTTP_201_CREATED)
#
# # Retrieve the list and check there are 2 elements
# request = self.api_factory.get('/swift/sort_nodes')
# response = sort_list(request)
# self.assertEqual(response.status_code, status.HTTP_200_OK)
# proxy_sortings = json.loads(response.content)
# self.assertEqual(len(proxy_sortings), 2)
#
# def test_create_proxy_sorting_with_empty_dict(self):
# # Create an empty proxy sorting
#
# request = self.api_factory.post('/swift/sort_nodes', {}, format='json')
# response = sort_list(request)
# self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
#
# def test_create_proxy_sorting_with_empty_data(self):
# # Create an empty proxy sorting
#
# request = self.api_factory.post('/swift/sort_nodes', '', format='json')
# response = sort_list(request)
# self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
#
# def test_create_proxy_sorting_with_unparseable_data(self):
# # Create an empty proxy sorting
#
# unparseable_data = '{{{{[[[[.....'
# request = self.simple_factory.post('/swift/sort_nodes', unparseable_data, 'application/json')
# response = sort_list(request)
# self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
#
# # TODO Add the following tests
# # def test_create_proxy_sorting_with_not_allowed_parameters(self):
# # def test_create_proxy_sorting_without_a_required_parameter(self):
#
# def test_get_proxy_sorting_ok(self):
# request = self.api_factory.get('/swift/sort_nodes/1')
# response = sort_detail(request, 1)
# self.assertEqual(response.status_code, status.HTTP_200_OK)
# proxy_sorting = json.loads(response.content)
# self.assertEqual(proxy_sorting['name'], 'FakeProxySorting')
# self.assertEqual(proxy_sorting['criterion'], 'fake_criterion')
#
# def test_update_proxy_sorting_ok(self):
# proxy_sorting_data = {'name': 'FakeProxySortingChanged', 'criterion': 'criterion changed'}
# request = self.api_factory.put('/swift/sort_nodes/1', proxy_sorting_data, format='json')
# response = sort_detail(request, 1)
# self.assertEqual(response.status_code, status.HTTP_201_CREATED)
#
# # Check it has been updated
# request = self.api_factory.get('/swift/sort_nodes/1')
# response = sort_detail(request, 1)
# self.assertEqual(response.status_code, status.HTTP_200_OK)
# proxy_sorting = json.loads(response.content)
# self.assertEqual(proxy_sorting['name'], 'FakeProxySortingChanged')
# self.assertEqual(proxy_sorting['criterion'], 'criterion changed')
#
# def test_update_proxy_sorting_with_empty_data(self):
# request = self.api_factory.put('/swift/sort_nodes/1', {}, format='json')
# response = sort_detail(request, 1)
# self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
#
# def test_update_proxy_sorting_with_unparseable_data(self):
# unparseable_data = '{{{{[[[[.....'
#
# request = self.simple_factory.put('/swift/sort_nodes/1', unparseable_data, 'application/json')
# response = sort_detail(request, 1)
# self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
#
# def test_delete_proxy_sorting_ok(self):
# request = self.api_factory.delete('/swift/sort_nodes/1')
# response = sort_detail(request, 1)
# self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
#
# # Retrieve the list and check there are 0 elements
# request = self.api_factory.get('/swift/sort_nodes')
# response = sort_list(request)
# self.assertEqual(response.status_code, status.HTTP_200_OK)
# self.assertEqual(response.content, '[]')

def test_storage_policy_list_ok(self):
""" Test that GET requests to storage_policy_list() return METHOD_NOT_ALLOWED """

Expand Down Expand Up @@ -227,6 +109,16 @@ def test_delete_node_detail_ok(self):
response = node_detail(request, server_type, node_id)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

# Regions / Zones

def test_regions_get_ok(self):
request = self.api_factory.get('/swift/regions/')
response = regions(request)
self.assertEqual(response.status_code, status.HTTP_200_OK)
region_items = json.loads(response.content)
self.assertEqual(len(region_items), 1)
self.assertEqual(region_items[0]['name'], 'data_center')

#
# Aux functions
#
Expand All @@ -244,12 +136,6 @@ def create_storage_policies(self):
self.r.hmset("storage-policy:4", {'name': 's5y6', 'default': 'no', 'policy_type': 'replication',
'devices': json.dumps(devices)})

# def create_proxy_sorting(self):
# proxy_sorting_data = {'name': 'FakeProxySorting', 'criterion': 'fake_criterion'}
# request = self.api_factory.post('/swift/sort_nodes', proxy_sorting_data, format='json')
# response = sort_list(request)
# self.assertEqual(response.status_code, status.HTTP_201_CREATED)

def create_nodes(self):
self.r.hmset('proxy_node:controller',
{'ip': '192.168.2.1', 'last_ping': '1467623304.332646', 'type': 'proxy', 'name': 'controller',
Expand All @@ -260,3 +146,10 @@ def create_nodes(self):
self.r.hmset('object_node:storagenode2',
{'ip': '192.168.2.3', 'last_ping': '1467623304.332646', 'type': 'object', 'name': 'storagenode2',
'devices': '{"sdb1": {"free": 16832876544, "size": 16832880640}}', 'region_id': 1, 'zone_id': 1})

def create_regions_and_zones(self):
self.r.set('regions:id', 1)
self.r.hmset('region:1', {'name': 'data_center', 'description': 'Acme Data Center'})
self.r.set('zones:id', 1)
self.r.hmset('zone:1', {'name': 'Rack', 'description': 'Dummy Rack: GbE Switch, 2 Proxies and 7 Storage Nodes',
'regions': '1', 'zone_id': '1'})

0 comments on commit 8c6b268

Please sign in to comment.