def test_deepsea_keys_unauthorized(self): with mock.patch("requests.Session") as mock_requests_session: mock_requests_session().post.side_effect = [ self._login_resp, self._login_resp ] resp_un = mock.MagicMock() resp_un.ok = False resp_un.status_code = 401 resp_ok = mock.MagicMock() resp_ok.ok = True resp_ok.status_code = 200 resp_ok.json.return_value = { 'return': { 'minions': ['minion1', 'minion2'], 'minions_pre': ['minion3'], 'minions_denied': [], 'minions_rejected': ['minion4'] } } mock_requests_session().get.side_effect = [resp_un, resp_ok] api = DeepSea('localhost', 8000, 'auto', 'hello', 'world') res = api.key_list() self.assertTrue(mock_requests_session().post.called) self.assertTrue(mock_requests_session().get.called) self.assertEqual( res, { 'minions': ['minion1', 'minion2'], 'minions_pre': ['minion3'], 'minions_denied': [], 'minions_rejected': ['minion4'] })
def test_deepsea_get_minions_no_public_network(self): """Regression test for OP-2595: DeepSea's pillar data doesn't contain "public_network" """ with mock.patch("requests.Session") as mock_requests_session: resp_pillar = mock.MagicMock() resp_pillar.ok = True resp_pillar.status_code = 200 resp_pillar.json.return_value = { 'return': [{ 'minion1': { u'time_init': u'ntp', u'roles': [u'storage'], u'time_server': u'ses-node01', u'master_minion': u'ses-node01', u'benchmark': { u'log-file-directory': u'/var/log/ceph_bench_logs', u'job-file-directory': u'/run/ceph_bench_jobs', u'default-collection': u'simple.yml', u'work-directory': u'/run/ceph_bench' }, u'cluster': u'unassigned', u'deepsea_minions': u'*' }, }] } mock_requests_session().post.side_effect = [ self._login_resp, resp_pillar ] resp = mock.MagicMock() resp.ok = True resp.status_code = 200 resp.json.return_value = { 'return': { 'minions': ['minion1'], 'minions_pre': [], 'minions_denied': [], 'minions_rejected': [] } } mock_requests_session().get.return_value = resp api = DeepSea('localhost', 8000, 'auto', 'hello', 'world') res = api.get_minions() self.assertEqual(res, [{ 'hostname': 'minion1', 'key_status': 'accepted', u'time_init': u'ntp', u'roles': [u'storage'], u'time_server': u'ses-node01', u'master_minion': u'ses-node01', u'benchmark': { u'log-file-directory': u'/var/log/ceph_bench_logs', u'job-file-directory': u'/run/ceph_bench_jobs', u'default-collection': u'simple.yml', u'work-directory': u'/run/ceph_bench' }, u'cluster': u'unassigned', u'deepsea_minions': u'*' }])
def test_deepsea_keys_success(self): with mock.patch("requests.Session") as mock_requests_session: mock_requests_session().post.return_value = self._login_resp resp = mock.MagicMock() resp.ok = True resp.status_code = 200 resp.json.return_value = { 'return': { 'minions': ['minion1', 'minion2'], 'minions_pre': ['minion3'], 'minions_denied': [], 'minions_rejected': ['minion4'] } } mock_requests_session().get.return_value = resp api = DeepSea('localhost', 8000, 'auto', 'hello', 'world') res = api.key_list() self.assertTrue(mock_requests_session().post.called) self.assertTrue(mock_requests_session().get.called) self.assertTrue(api._is_logged_in()) self.assertEqual( res, { 'minions': ['minion1', 'minion2'], 'minions_pre': ['minion3'], 'minions_denied': [], 'minions_rejected': ['minion4'] })
def test_deepsea_get_minions(self): with mock.patch("rest_client.TimeoutRequestsSession") as mock_requests_session: resp_pillar = mock.MagicMock() resp_pillar.ok = True resp_pillar.status_code = 200 resp_pillar.json.return_value = { 'return': [{ 'minion1': { 'roles': ['storage', 'mon', 'igw'], 'public_network': '10.1.0.0/24', 'cluster_network': '10.1.0.0/24', 'fsid': 'aaabbb', }, 'minion2': { 'roles': ['storage', 'rgw'], 'public_network': '10.1.0.0/24', 'cluster_network': '10.1.0.0/24', 'fsid': 'aaabbb', } }] } mock_requests_session().post.side_effect = [self._login_resp, resp_pillar] resp = mock.MagicMock() resp.ok = True resp.status_code = 200 resp.json.return_value = { 'return': { 'minions': ['minion1'], 'minions_pre': [], 'minions_denied': [], 'minions_rejected': ['minion2'] } } mock_requests_session().get.return_value = resp api = DeepSea('localhost', 8000, 'auto', 'hello', 'world') res = api.get_minions() self.assertTrue(mock_requests_session().post.called) self.assertTrue(mock_requests_session().get.called) self.assertTrue(api._is_logged_in()) self.assertEqual(res, [ { 'roles': ['storage', 'mon', 'igw'], 'hostname': 'minion1', 'key_status': 'accepted', 'public_network': '10.1.0.0/24', 'cluster_network': '10.1.0.0/24', 'fsid': 'aaabbb', }, { 'roles': ['storage', 'rgw'], 'public_network': '10.1.0.0/24', 'hostname': 'minion2', 'key_status': 'rejected', 'cluster_network': '10.1.0.0/24', 'fsid': 'aaabbb', } ])
def test_deepsea_iscsi_interfaces(self): with mock.patch("requests.Session") as mock_requests_session: login_resp = mock.MagicMock() login_resp.ok = True login_resp.status_code = 200 login_resp.json.return_value = { 'return': [{'token': 'validtoken'}] } rest_iscsi = mock.MagicMock() rest_iscsi.ok = True rest_iscsi.status_code = 200 rest_iscsi.json.return_value = { 'return': [{ 'minion1': ['192.168.10.101', '192.168.121.41'], 'minion2': ['192.168.10.102', '192.168.121.42'] }] } mock_requests_session().post.side_effect = [login_resp, rest_iscsi] api = DeepSea('localhost', 8000, 'auto', 'admin', 'admin') res = api.iscsi_interfaces() self.assertTrue(mock_requests_session().post.called) self.assertTrue(api._is_logged_in()) self.assertEqual(res, [ {'hostname': 'minion1', 'interfaces': ['192.168.10.101', '192.168.121.41']}, {'hostname': 'minion2', 'interfaces': ['192.168.10.102', '192.168.121.42']} ])
def test_deepsea_keys_response_format_error_2(self): with mock.patch("requests.Session") as mock_requests_session: mock_requests_session().post.return_value = self._login_resp resp = mock.MagicMock() resp.ok = True resp.status_code = 200 resp.json.return_value = { 'return': { 'minions': ['minion1', 'minion2'], 'minions_pre': 'minion3', 'minions_denied': [], 'minions_rejected': ['minion4'] } } mock_requests_session().get.return_value = resp api = DeepSea('localhost', 8000, 'auto', 'hello', 'world') with self.assertRaises(BadResponseFormatException) as context: api.key_list() self.assertTrue(mock_requests_session().post.called) self.assertTrue(mock_requests_session().get.called) self.assertTrue(api._is_logged_in()) self.assertEqual(str(context.exception), "minion3 is not an array")
def get(self, request): if request.GET['eauth'] == 'auto' and \ ('username' not in request.GET or 'password' not in request.GET): raise ValidationError( '"username", and "password" params are required') if request.GET['eauth'] == 'sharedsecret' and \ ('username' not in request.GET or 'shared_secret' not in request.GET): raise ValidationError( '"username", and "shared_secret" params are required') password = request.GET['shared_secret'] \ if request.GET['eauth'] == 'sharedsecret' else request.GET['password'] deepsea = DeepSea(request.GET['host'], request.GET['port'], request.GET['eauth'], request.GET['username'], password) try: rgw_info = deepsea.get_rgw_api_credentials() if rgw_info: return Response({ "success": True, "rgw": { "host": rgw_info['host'], "port": int(rgw_info['port']), "access_key": rgw_info['access_key'], "secret_key": rgw_info['secret_key'], "admin_path": rgw_info['admin_path'], "user_id": rgw_info['user_id'], "use_ssl": rgw_info['scheme'] == 'https' } }) except RequestException: pass return Response({'success': False})
def test_deepsea_invalid_url_error(self): with mock.patch("rest_client.TimeoutRequestsSession") as mock_requests_session: mock_requests_session().post.side_effect = InvalidURL("test") api = DeepSea('', 8000, 'auto', 'hello', 'world') with self.assertRaisesRegexp(RequestException, "^test$") as context: api._login() self.assertEqual(context.exception.status_code, None) self.assertTrue(mock_requests_session().post.called)
def test_deepsea_login_connection_error(self): with mock.patch("rest_client.TimeoutRequestsSession") as mock_requests_session: mock_requests_session().post.side_effect = ConnectionError() api = DeepSea('localhost', 8000, 'auto', 'hello', 'world') with self.assertRaises(RequestException) as context: api._login() self.assertEqual(context.exception.status_code, None) self.assertTrue(mock_requests_session().post.called) self.assertEqual(mock_requests_session().post.call_args[1]['data'], {'username': '******', 'password': '******', 'eauth': 'auto'}) self.assertFalse(api._is_logged_in()) self.assertEqual(api.token, None)
def test_deepsea_service_offline_connection_error(self): with mock.patch("requests.Session") as mock_requests_session: resp_post = mock.MagicMock() resp_post.ok = True resp_post.status_code = 200 resp_post.json.return_value = {'return': [{'token': 'validtoken'}]} mock_requests_session().post.return_value = resp_post mock_requests_session().get.side_effect = ConnectionError() api = DeepSea('localhost', 8000, 'auto', 'hello', 'world') with self.assertRaises(RequestException): api.is_service_online() self.assertTrue(mock_requests_session().get.called) self.assertTrue(api._is_logged_in())
def test_deepsea_login_fail_401(self): with mock.patch("rest_client.TimeoutRequestsSession") as mock_requests_session: resp = mock.MagicMock() resp.ok = False resp.status_code = 401 mock_requests_session().post.return_value = resp api = DeepSea('localhost', 8000, 'auto', 'hello', 'world') with self.assertRaises(RequestException) as context: api._login() self.assertEqual(context.exception.status_code, 401) self.assertTrue(mock_requests_session().post.called) self.assertEqual(mock_requests_session().post.call_args[1]['data'], {'username': '******', 'password': '******', 'eauth': 'auto'}) self.assertFalse(api._is_logged_in()) self.assertEqual(api.token, None)
def test_deepsea_login_success(self): with mock.patch("rest_client.TimeoutRequestsSession") as mock_requests_session: resp = mock.MagicMock() resp.ok = True resp.status_code = 200 resp.json.return_value = { 'return': [{'token': 'validtoken'}] } mock_requests_session().post.return_value = resp api = DeepSea('localhost', 8000, 'auto', 'hello', 'world') api._login() self.assertTrue(mock_requests_session().post.called) self.assertEqual(mock_requests_session().post.call_args[1]['data'], {'username': '******', 'password': '******', 'eauth': 'auto'}) self.assertTrue(api._is_logged_in()) self.assertEqual(api.token, 'validtoken')
def test_deepsea_pillar_items_success(self): with mock.patch("requests.Session") as mock_requests_session: resp = mock.MagicMock() resp.ok = True resp.status_code = 200 resp.json.return_value = { 'return': [{ 'minion1': { 'roles': ['storage', 'mon', 'igw'], 'public_network': '10.1.0.0/24', 'cluster_network': '10.1.0.0/24', 'fsid': 'aaabbb', }, 'minion2': { 'roles': ['storage', 'rgw'], 'public_network': '10.1.0.0/24', 'cluster_network': '10.1.0.0/24', 'fsid': 'aaabbb', } }] } mock_requests_session().post.side_effect = [self._login_resp, resp] api = DeepSea('localhost', 8000, 'auto', 'hello', 'world') res = api.pillar_items() self.assertTrue(mock_requests_session().post.called) self.assertTrue(api._is_logged_in()) self.assertEqual( res, { 'minion1': { 'roles': ['storage', 'mon', 'igw'], 'public_network': '10.1.0.0/24', 'cluster_network': '10.1.0.0/24', 'fsid': 'aaabbb', }, 'minion2': { 'roles': ['storage', 'rgw'], 'public_network': '10.1.0.0/24', 'cluster_network': '10.1.0.0/24', 'fsid': 'aaabbb', } })
def test_deepsea_login_response_format_error(self): with mock.patch("rest_client.TimeoutRequestsSession") as mock_requests_session: resp = mock.MagicMock() resp.ok = True resp.status_code = 200 resp.json.return_value = { 'return': {'invalidtoken': 'validtoken'} } mock_requests_session().post.return_value = resp api = DeepSea('localhost', 8000, 'auto', 'hello', 'world') with self.assertRaises(BadResponseFormatException): api._login() self.assertTrue(mock_requests_session().post.called) self.assertEqual(mock_requests_session().post.call_args[1]['data'], {'username': '******', 'password': '******', 'eauth': 'auto'}) self.assertFalse(api._is_logged_in()) self.assertEqual(api.token, None)
def test_deepsea_service_online(self): with mock.patch("requests.Session") as mock_requests_session: resp_post = mock.MagicMock() resp_post.ok = True resp_post.status_code = 200 resp_post.json.return_value = {'return': [{'token': 'validtoken'}]} mock_requests_session().post.return_value = resp_post resp = mock.MagicMock() resp.ok = True resp.status_code = 200 resp.json.return_value = {'return': 'Welcome'} mock_requests_session().get.return_value = resp api = DeepSea('localhost', 8000, 'auto', 'hello', 'world') self.assertTrue(api.is_service_online()) self.assertTrue(mock_requests_session().get.called) self.assertTrue(api._is_logged_in())
def test_deepsea_service_offline_response_format_error(self): with mock.patch("rest_client.TimeoutRequestsSession") as mock_requests_session: resp_post = mock.MagicMock() resp_post.ok = True resp_post.status_code = 200 resp_post.json.return_value = { 'return': [{'token': 'validtoken'}] } mock_requests_session().post.return_value = resp_post resp = mock.MagicMock() resp.ok = True resp.status_code = 200 resp.json.return_value = {'no_return': 'Welcome'} mock_requests_session().get.return_value = resp api = DeepSea('localhost', 8000, 'auto', 'hello', 'world') with self.assertRaises(BadResponseFormatException): api.is_service_online() self.assertTrue(mock_requests_session().get.called) self.assertTrue(api._is_logged_in())
def test_deepsea_keys_login_error(self): with mock.patch("requests.Session") as mock_requests_session: login_resp_err = mock.MagicMock() login_resp_err.ok = False login_resp_err.status_code = 503 mock_requests_session().post.side_effect = [ self._login_resp, login_resp_err ] resp_un = mock.MagicMock() resp_un.ok = False resp_un.status_code = 401 mock_requests_session().get.side_effect = [resp_un] api = DeepSea('localhost', 8000, 'auto', 'hello', 'world') with self.assertRaises(RequestException) as context: api.key_list() self.assertTrue(mock_requests_session().post.called) self.assertTrue(mock_requests_session().get.called) self.assertEqual(context.exception.status_code, 503)
def get(self, request): if 'host' not in request.GET or \ 'port' not in request.GET or \ 'eauth' not in request.GET: raise ValidationError( '"host", "port", and "eauth" params are required') if request.GET['eauth'] == 'auto' and \ ('username' not in request.GET or 'password' not in request.GET): raise ValidationError( '"username", and "password" params are required') if request.GET['eauth'] == 'sharedsecret' and \ ('username' not in request.GET or 'shared_secret' not in request.GET): raise ValidationError( '"username", and "shared_secret" params are required') password = request.GET['shared_secret'] \ if request.GET['eauth'] == 'sharedsecret' else request.GET['password'] deepsea = DeepSea(request.GET['host'], request.GET['port'], request.GET['eauth'], request.GET['username'], password) return Response(_check_rest_client_connection(deepsea))
def test_deepsea_ipv6_host(self): api = DeepSea('fe80::5054:ff:fed3:9929', 8080, 'auto', 'hello', 'world') p = urlparse.urlparse(api.base_url) self.assertEquals(p.netloc, '[fe80::5054:ff:fed3:9929]:8080')
def test_deepsea_get_minions_no_role(self): """Regression for OP-2507: DeepSea: "roles" is missing from response""" with mock.patch("requests.Session") as mock_requests_session: resp_pillar = mock.MagicMock() resp_pillar.ok = True resp_pillar.status_code = 200 resp_pillar.json.return_value = { 'return': [{ 'minion1': { "time_init": "ntp", "rgw_configurations": { "rgw": { "users": { "email": "*****@*****.**", "system": True, "name": "Admin", "uid": "admin" } } }, "available_roles": [ "storage", "admin", "mon", "mds", "mgr", "igw", "openattic", "rgw", "ganesha", "client-cephfs", "client-radosgw", "client-iscsi", "client-nfs", "master" ], "benchmark": { "log-file-directory": "/var/log/cephfs_bench_logs", "job-file-directory": "/run/cephfs_bench_jobs", "default-collection": "simple.yml", "work-directory": "/run/cephfs_bench" }, "master_minion": "master_minion.openattic.org", "time_server": "master_minion.openattic.org", "igw_config": "default-ui", "cluster": "ceph", "public_network": "10.0.0.0/19", "cluster_network": "10.0.0.0/19", "stage_prep_master": "default", "fsid": "c0f85b6a-70d7-4c49-81fa-64ed80069e24" }, }] } mock_requests_session().post.side_effect = [ self._login_resp, resp_pillar ] resp = mock.MagicMock() resp.ok = True resp.status_code = 200 resp.json.return_value = { 'return': { 'minions': ['minion1'], 'minions_pre': [], 'minions_denied': [], 'minions_rejected': [] } } mock_requests_session().get.return_value = resp api = DeepSea('localhost', 8000, 'auto', 'hello', 'world') res = api.get_minions() self.assertEqual(res, [{ 'benchmark': { 'log-file-directory': '/var/log/cephfs_bench_logs', 'job-file-directory': '/run/cephfs_bench_jobs', 'default-collection': 'simple.yml', 'work-directory': '/run/cephfs_bench' }, 'master_minion': 'master_minion.openattic.org', 'time_server': 'master_minion.openattic.org', 'igw_config': 'default-ui', 'cluster': 'ceph', 'fsid': 'c0f85b6a-70d7-4c49-81fa-64ed80069e24', 'time_init': 'ntp', 'rgw_configurations': { 'rgw': { 'users': { 'uid': 'admin', 'email': '*****@*****.**', 'name': 'Admin', 'system': True } } }, 'available_roles': [ 'storage', 'admin', 'mon', 'mds', 'mgr', 'igw', 'openattic', 'rgw', 'ganesha', 'client-cephfs', 'client-radosgw', 'client-iscsi', 'client-nfs', 'master' ], 'hostname': 'minion1', 'key_status': 'accepted', 'public_network': '10.0.0.0/19', 'cluster_network': '10.0.0.0/19', 'stage_prep_master': 'default' }])