Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/Crystal-SDS/controller into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
JosepSampe committed Nov 9, 2017
2 parents 1e7e5da + 8c6b268 commit cc6cf23
Show file tree
Hide file tree
Showing 17 changed files with 1,510 additions and 1,699 deletions.
63 changes: 24 additions & 39 deletions api/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.test import TestCase, override_settings
from rest_framework.test import APIRequestFactory

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

Expand Down Expand Up @@ -50,31 +50,16 @@ def test_to_json_bools_ok(self):
self.assertEqual(bdict['d'], 'False')
self.assertNotEqual(bdict['d'], False)

@mock.patch('api.common_utils.os.system')
def test_rsync_dir_with_nodes_ok(self, mock_os_system):
mock_os_system.return_value = 0 # return value when rsync succeeds

@mock.patch('api.common.threading.Thread')
def test_rsync_dir_with_nodes_ok(self, mock_thread):
self.configure_usernames_and_passwords_for_nodes()
rsync_dir_with_nodes(settings.WORKLOAD_METRICS_DIR)

# test that rsync_dir_with_nodes() called os.system with the right parameters
calls = [mock.call("sshpass -p s3cr3t rsync --progress --delete -avrz -e ssh /opt/crystal/workload_metrics user1@192.168.2.1:/opt/crystal"),
mock.call("sshpass -p s3cr3t rsync --progress --delete -avrz -e ssh /opt/crystal/workload_metrics user1@192.168.2.2:/opt/crystal"),
mock.call("sshpass -p s3cr3t rsync --progress --delete -avrz -e ssh /opt/crystal/workload_metrics user1@192.168.2.3:/opt/crystal")]
mock_os_system.assert_has_calls(calls, any_order=True)
self.assertEqual(mock_thread.call_count, 3)

def test_rsync_dir_with_nodes_when_username_and_password_not_present(self):
with self.assertRaises(FileSynchronizationException):
rsync_dir_with_nodes(settings.WORKLOAD_METRICS_DIR)

@mock.patch('api.common_utils.os.system')
def test_rsync_dir_with_nodes_when_rsync_fails(self, mock_os_system):
mock_os_system.return_value = 1 # return value when rsync fails

self.configure_usernames_and_passwords_for_nodes()
with self.assertRaises(FileSynchronizationException):
rsync_dir_with_nodes(settings.WORKLOAD_METRICS_DIR)

# @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.utcnow() + timedelta(minutes=5)).strftime('%Y-%m-%dT%H:%M:%SZ'),
Expand Down Expand Up @@ -122,7 +107,7 @@ def test_rsync_dir_with_nodes_when_rsync_fails(self, mock_os_system):
# resp = get_token_connection(request)
# self.assertFalse(resp)

@mock.patch('api.common_utils.get_keystone_admin_auth')
@mock.patch('api.common.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.projects.list.return_value = fake_tenants_list
Expand All @@ -132,9 +117,9 @@ def test_get_project_list_ok(self, mock_keystone_admin_auth):

@override_settings(MANAGEMENT_ACCOUNT='mng_account', MANAGEMENT_ADMIN_USERNAME='mng_username', MANAGEMENT_ADMIN_PASSWORD='mng_pw',
KEYSTONE_ADMIN_URL='http://localhost:35357/v3')
@mock.patch('api.common_utils.client.Client')
@mock.patch('api.common_utils.session.Session')
@mock.patch('api.common_utils.v3.Password')
@mock.patch('api.common.client.Client')
@mock.patch('api.common.session.Session')
@mock.patch('api.common.v3.Password')
def test_get_keystone_admin_auth_ok(self, mock_password, mock_session, mock_keystone_client):
get_keystone_admin_auth()
mock_password.assert_called_with(auth_url='http://localhost:35357/v3', username='mng_username', password='mng_pw',
Expand All @@ -148,12 +133,12 @@ def test_startup_run_ok(self, mock_startup_redis):
# Mocking redis to use DB=10 (in startup.py, settings are imported directly from ./settings.py instead of using django.conf)
mock_startup_redis.return_value = self.r
startup_run()
self.assertEquals(self.r.hget('workload_metric:1', 'enabled'), 'False')
self.assertEquals(self.r.hget('workload_metric:2', 'enabled'), 'False')
self.assertEquals(self.r.hget('workload_metric:1', 'status'), 'Stopped')
self.assertEquals(self.r.hget('workload_metric:2', 'status'), 'Stopped')
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')
self.assertEquals(self.r.hget('policy:1', 'status'), 'Stopped')
self.assertEquals(self.r.hget('policy:2', 'status'), 'Stopped')

#
# URL tests
Expand All @@ -172,10 +157,10 @@ def test_urls(self):
self.assertEqual(resolver.kwargs, {'filter_id': '123'})

resolver = resolve('/swift/nodes/')
self.assertEqual(resolver.view_name, 'swift.views.node_list')
self.assertEqual(resolver.view_name, 'swift_api.views.node_list')

resolver = resolve('/swift/nodes/object/node1')
self.assertEqual(resolver.view_name, 'swift.views.node_detail')
self.assertEqual(resolver.view_name, 'swift_api.views.node_detail')
self.assertEqual(resolver.kwargs, {'server_type': 'object', 'node_id': 'node1'})

#
Expand All @@ -185,30 +170,30 @@ def test_urls(self):
def create_nodes(self):
self.r.hmset('proxy_node:controller',
{'ip': '192.168.2.1', 'last_ping': str(calendar.timegm(time.gmtime())), 'type': 'proxy', 'name': 'controller',
'devices': '{"sdb1": {"free": 16832876544, "size": 16832880640}}'})
'devices': '{"sdb1": {"free": 16832876544, "size": 16832880640}}', 'ssh_access': 'False'})
self.r.hmset('object_node:storagenode1',
{'ip': '192.168.2.2', 'last_ping': str(calendar.timegm(time.gmtime())), 'type': 'object', 'name': 'storagenode1',
'devices': '{"sdb1": {"free": 16832876544, "size": 16832880640}}'})
'devices': '{"sdb1": {"free": 16832876544, "size": 16832880640}}', 'ssh_access': 'False'})
self.r.hmset('object_node:storagenode2',
{'ip': '192.168.2.3', 'last_ping': str(calendar.timegm(time.gmtime())), 'type': 'object', 'name': 'storagenode2',
'devices': '{"sdb1": {"free": 16832876544, "size": 16832880640}}'})
'devices': '{"sdb1": {"free": 16832876544, "size": 16832880640}}', 'ssh_access': 'False'})

def configure_usernames_and_passwords_for_nodes(self):
self.r.hmset('proxy_node:controller', {'ssh_username': 'user1', 'ssh_password': 's3cr3t'})
self.r.hmset('object_node:storagenode1', {'ssh_username': 'user1', 'ssh_password': 's3cr3t'})
self.r.hmset('object_node:storagenode2', {'ssh_username': 'user1', 'ssh_password': 's3cr3t'})
self.r.hmset('proxy_node:controller', {'ssh_username': 'user1', 'ssh_password': 's3cr3t', 'ssh_access': 'True'})
self.r.hmset('object_node:storagenode1', {'ssh_username': 'user1', 'ssh_password': 's3cr3t', 'ssh_access': 'True'})
self.r.hmset('object_node:storagenode2', {'ssh_username': 'user1', 'ssh_password': 's3cr3t', 'ssh_access': 'True'})

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'})
'in_flow': 'False', 'status': 'Running', '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'})
'in_flow': 'False', 'status': 'Running', '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'})
{'status': 'Alive', 'policy_description': 'FOR TENANT:0123456789abcdef DO SET compression'})
self.r.hmset('policy:2',
{'alive': 'True', 'policy_description': 'FOR TENANT:0123456789abcdef DO SET encryption'})
{'status': 'Alive', 'policy_description': 'FOR TENANT:0123456789abcdef DO SET encryption'})


class FakeTokenData:
Expand Down

0 comments on commit cc6cf23

Please sign in to comment.