def wrapper(*args, **kw): config = ctx.node.properties.get(VERSA_CONFIG) path = _create_path() with VersaClient(config, path) as client: kw['versa_client'] = client result = f(*args, **kw) return result
def notest_create_rule(self): with VersaClient(self.config) as client: app = 'testapp' org = 'child' pool_name = "testpool" rule_name = "testrule" source_addr = ["1.2.3.0/24"] versa_plugin.cgnat.create_rule(client, app, org, rule_name, source_addr, pool_name)
def get_configuration(url): def info(x): print x fake_ctx = mock.MagicMock() fake_ctx.logger.info = info patcher_ctx1 = mock.patch('versa_plugin.versaclient.ctx', fake_ctx) patcher_ctx1.start() with VersaClient(configuration.versa_config, '/tmp/versakey') as client: result = client.get(url, None, None, codes.ok) patcher_ctx1.stop() return result
def notest_create_pool(self): with VersaClient(self.config) as client: app = 'testapp' org = 'child' pool_name = "testpool" routing = "vr", provider = "mytestorg" addr_range = [AddressRange("range", "172.168.35.1", "175.168.35.30")] versa_plugin.cgnat.create_pool(client, app, org, pool_name, addr_range, routing, provider)
import configuration import sys import os from versa_plugin.versaclient import VersaClient if len(sys.argv) != 2: print 'Need file' quit() file_name = sys.argv[1] if not os.path.isfile(file_name): print 'Wrong file' quit() client = VersaClient(configuration.data, '') for line in open(file_name): if line.startswith('nms rbac oauth'): token = line.split()[5] print token client.access_token = token client.revoke_token()
def notest_get_all_tasks(self): with VersaClient(self.config) as client: print versa_plugin.tasks.get_all_tasks(client)
def test_get_task_info(self): with VersaClient(self.config) as client: task = '25' print versa_plugin.tasks.get_task_info(client, task)
def test_get_revoke_token(self): config = configuration.data client = VersaClient(config, '/tmp/versa.key') client.get_token() client.revoke_token()
def test_delete_rule(self): with VersaClient(self.config) as client: app = 'testapp' org = 'child' rule_name = "testrule" versa_plugin.cgnat.delete_rule(client, app, org, rule_name)
def notest_delete_pool(self): with VersaClient(self.config) as client: app = 'testapp' org = 'child' pool_name = "testpool" versa_plugin.cgnat.delete_pool(client, app, org, pool_name)
def notest_get_rules(self): with VersaClient(self.config) as client: app = 'mytestapp' org = 'mytestorg' print versa_plugin.cgnat.get_list_nat_rules(client, app, org)
def setUp(self): self.client = VersaClient(configuration, '/tmp/testkey')
class VersaPluginMockTestCase(unittest.TestCase): def setUp(self): self.client = VersaClient(configuration, '/tmp/testkey') def test_check_response(self): response = mock.MagicMock() response.status_code = 404 accept = 'json' with self.assertRaises(cfy_exc.HttpException): _check_response(response, 200, accept) response.status_code = 200 response.content = '{}' result = _check_response(response, 200, accept) self.assertIsInstance(result, dict) response.content = None result = _check_response(response, 200, accept) self.assertIsNone(result) def test_get_token(self): good_result = mock.MagicMock() good_result.content = '{"access_token": "token"}' with mock.patch('versa_plugin.versaclient.requests.post', mock.MagicMock(return_value=good_result)),\ mock.patch('versa_plugin.versaclient.VersaClient.' 'read_tokens_form_file', mock.MagicMock(return_value=False)),\ mock.patch('versa_plugin.versaclient.VersaClient.' 'save_token_to_file', mock.MagicMock(return_value=None)): self.client.get_token() self.assertTrue(self.client.access_token) bad_result = mock.MagicMock() bad_result.content = '' with mock.patch('versa_plugin.versaclient.requests.post', mock.MagicMock(return_value=bad_result)),\ mock.patch('versa_plugin.versaclient.VersaClient.' 'read_tokens_form_file', mock.MagicMock(return_value=False)),\ mock.patch('versa_plugin.versaclient.VersaClient.' 'save_token_to_file', mock.MagicMock(return_value=None)): with self.assertRaises(cfy_exc.NonRecoverableError): self.client.get_token() bad_result.content = None with mock.patch('versa_plugin.versaclient.requests.post', mock.MagicMock(return_value=bad_result)),\ mock.patch('versa_plugin.versaclient.VersaClient.' 'read_tokens_form_file', mock.MagicMock(return_value=False)),\ mock.patch('versa_plugin.versaclient.VersaClient.' 'save_token_to_file', mock.MagicMock(return_value=None)): with self.assertRaises(cfy_exc.NonRecoverableError): self.client.get_token() def test_get(self): with mock.patch('versa_plugin.versaclient.requests', mock.MagicMock( return_value={})),\ mock.patch('versa_plugin.versaclient._check_response', mock.MagicMock()): self.client.get('/path', 'data', 'json') def test_post(self): with mock.patch('versa_plugin.versaclient.requests', mock.MagicMock( return_value={})),\ mock.patch('versa_plugin.versaclient._check_response', mock.MagicMock()): self.client.post('/path', 'data', 'json') def test_delete(self): with mock.patch('versa_plugin.versaclient.requests', mock.MagicMock( return_value={})),\ mock.patch('versa_plugin.versaclient._check_response', mock.MagicMock()): self.client.delete('/path') def test_request(self): request_type = mock.MagicMock() with mock.patch('versa_plugin.versaclient._check_response', mock.MagicMock()): self.client._request(request_type, '/path', 'data', 'json', 200, 'json') request_type = mock.MagicMock( side_effect=cfy_exc.HttpException('url', '404', 'Error')) with mock.patch('versa_plugin.versaclient._check_response', mock.MagicMock()): with self.assertRaises(cfy_exc.HttpException): self.client._request(request_type, '/path', 'data', 'json', 200, 'json') def test_get_headers(self): accept = 'json' headers = self.client._get_headers('json', accept) self.assertEqual('application/json', headers['Content-type']) headers = self.client._get_headers('xml', accept) self.assertEqual('application/xml', headers['Content-type']) self.assertEqual('application/json', headers['Accept']) headers = self.client._get_headers(None, accept) self.assertNotIn('Content-type', headers) with self.assertRaises(cfy_exc.NonRecoverableError): self.client._get_headers('bad', accept) headers = self.client._get_headers('json', accept) self.assertNotIn("Authorization", headers) self.client.access_token = "Token" headers = self.client._get_headers('json', accept) self.assertIn("Authorization", headers)