def test_read_by_reference(mock_post): """ Read Device by reference """ mock_post.return_value.json.return_value = {'token': '12345qwert'} with patch('requests.get') as mock_call_get: mock_call_get.return_value.text = device_info() with patch('msa_sdk.msa_api.host_port') as mock_host_port: mock_host_port.return_value = ('api_hostname', '8080') device = Device() assert _is_valid_json(device.read('DEV_REF')) assert device.path == '/device/reference/DEV_REF' assert device.device_id == 21594 assert device.name == "Linux self MSA" assert device.manufacturer_id == 14020601 assert device.model_id == 14020601 assert device.management_address == '127.0.0.1' assert device.management_port == '22' assert device.management_interface == '' assert device.login == 'root' assert device.password == '$ubiqube' assert device.password_admin == '' assert not device.log_enabled assert not device.mail_alerting assert not device.reporting assert device.use_nat assert device.snmp_community == '' mock_call_get.assert_called_once()
def test_device_by_customer(mock_post, lookup_fixture): """Test list of device by customer ref""" mock_post.return_value.json.return_value = {'token': '12345qwert'} first_dev = { 'id': 71655, 'prefix': 'hto', 'ubiId': 'hto71655', 'externalReference': 'hto71655' } second_dev = { 'id': 73055, 'prefix': 'CKB', 'ubiId': 'CKB73055', 'externalReference': 'CKB73055' } devices = [first_dev, second_dev] with patch('requests.get') as mock_call_get: mock_call_get.return_value.text = json.dumps(devices) lookup = lookup_fixture lookup.look_list_device_by_customer_ref('cust_ref') assert lookup.path == '/lookup/customer/devices/reference/cust_ref' assert _is_valid_json(lookup.content) assert json.loads(lookup.content) == devices
def test_sec_nodes(mock_post, lookup_fixture): """Test a list of sec nodes""" mock_post.return_value.json.return_value = {'token': '12345qwert'} first_sec_node = { 'name': 'DEV-MA2', 'logAddress': '10.30.18.86', 'smsAddress': '127.0.0.1', 'repNodeName': '', 'isAlive': True, 'ipv6Addr': '', 'ipv6Mask': '64', 'ipv6Gw': '' } sec_nodes = [first_sec_node] with patch('requests.get') as mock_call_get: mock_call_get.return_value.text = json.dumps(sec_nodes) lookup = lookup_fixture lookup.look_list_sec_nodes() assert lookup.path == '/lookup/sec_nodes' assert _is_valid_json(lookup.content) assert json.loads(lookup.content)[0] == first_sec_node
def test_provision_status(device_fixture): # pylint: disable=W0621 """ Test provision status """ provision_info = ('{"errorMsg": "", "rawJSONResult": ' '{\"sms_status\": \"OK\",' ' "PROVISIONING_PROCESS\": \"OK\",' ' \"sms_result\": [{\"sms_status\": "OK\",' ' \"sms_stage\": \"Lock Provisioning\",' ' \"sms_message\": \"OK\"}, ' '{\"sms_status\": \"OK\", ' '\"sms_stage\": \"Initial Connection\", ' '"sms_message\": \"OK\"}, {\"sms_status\": \"OK\", ' '\"sms_stage\": "Initial Configuration\", ' '\"sms_message\": \"OK\"}, {\"sms_status\": "OK\", ' '\"sms_stage\": \"DNS Update\", ' '\"sms_message\": \"OK\"}, {\"sms_status\": \"OK\", ' '\"sms_stage\": \"Unlock Provisioning\",' '\"sms_message\": \"OK\"}, {\"sms_status\": \"OK\", ' '\"sms_stage\":\"Save Configuration\",' '\"sms_message\":\"OK\"}]}, "status": "OK"}') device = device_fixture device.device_id = 1234 with patch('requests.get') as mock_call_get: mock_call_get.return_value.text = provision_info assert _is_valid_json(json.dumps(device.provision_status())) mock_call_get.assert_called_once() assert device.path == '/device/provisioning/status/1234'
def test_get_service_variable_by_name(orchestration_fixture): """ Test Get Service Variables by Variable Name """ device_info = ('{"TASKINSTANCEID":"353763"}') with patch('requests.get') as mock_call_get: mock_call_get.return_value.text = device_info orch = orchestration_fixture orch.get_service_variable_by_name('1234', 'TASKINSTANCEID') assert orch.path == \ '/orchestration/service/variables/1234/TASKINSTANCEID' assert _is_valid_json(orch.response.text)
def test_read_service_instance(orchestration_fixture): """ Test Read Service Instances """ device_info = ( '{"name":"Process/Reference/Customer/Kibana/kibana_dashboard",' '"id":2231,"serviceExternalReference":"MSASID2231","state":"ACTIVE"}') with patch('requests.get') as mock_call_get: mock_call_get.return_value.text = device_info orch = orchestration_fixture orch.read_service_instance('2231') assert orch.path == '/orchestration/MSAA19224/service/instance/2231' assert _is_valid_json(orch.response.text)
def test_get_service_variables_by_service_id(orchestration_fixture): """ Test Get Service variables by Service ID """ device_info = ('[{"comment":"","name":"SERVICEINSTANCEID",' '"value":"205710"},' '{"comment":"","name":"service_id",' '"value":"205710"}]') with patch('requests.get') as mock_call_get: mock_call_get.return_value.text = device_info orch = orchestration_fixture orch.get_service_variables('1234') assert orch.path == '/orchestration/service/variables/1234' assert _is_valid_json(orch.response.text)
def test_push_configuration_status(device_fixture): """ Test push configuration """ device = device_fixture r_value = ('{"message":"[root@LINUX-FW ~]# [root@LINUX-FW ~]#\n ",' '"date":"27-06-2018 09:15:33","status":"ENDED"}') with patch('requests.get') as mock_call_get: mock_call_get.return_value.text = r_value assert _is_valid_json(device.push_configuration_status()) assert device.path == '/device/push_configuration/status/{}'.format( device.device_id) mock_call_get.assert_called_once()
def test_get_list_service_by_status(orchestration_fixture): """ Test Get list of services by status """ response = ( '{"Process/IP_CONTROLLER/Fulfilment_Dispatcher/Fulfilment_Dispatcher":' '{"RUNNING":0,"ENDED":0,"WARNING":0,"FAIL":0},"Process/IP_CONTROLLER/' 'Fulfilment_Handler/Fulfilment_Handler":{"RUNNING":0,"ENDED":0,' '"WARNING":0,"FAIL":0}}') with patch('requests.get') as mock_call_get: mock_call_get.return_value.text = response orch = orchestration_fixture orch.get_list_service_by_status(1) assert orch.path == '/orchestration/v1/services?ubiqubeId=MSAA19224&range=1' assert _is_valid_json(orch.response.text)
def test_update_config(device_fixture): """ Test update config """ device = device_fixture r_value = ('{"status":"OK","result":"","rawJSONResult":' '"{\"sms_status\":\"OK\"}",' '"rawSmsResult":null,"ok":true,"code":null,"message":null}') with patch('requests.post') as mock_call_post: mock_call_post.return_value.text = r_value assert _is_valid_json(device.update_config()) assert device.path == '/device/configuration/update/{}'.format( device.device_id) mock_call_post.assert_called_once()
def test_create(device_fixture): """ Test create """ device = device_fixture response_content = '{"id": 67015, "name": "PyASA27-b"}' with patch('requests.post') as mock_call_post: mock_call_post.return_value.text = response_content assert _is_valid_json(json.dumps(device.create())) assert device.path == '/device/v2/{}'.format(device.customer_id) assert device.device_id == 67015 assert device.fail is not None assert not device.fail mock_call_post.assert_called_once()
def test_customer_ids(mock_post, lookup_fixture): """Test a list of customers""" mock_post.return_value.json.return_value = {'token': '12345qwert'} first_costu = { 'id': 7117, 'prefix': 'MSK', 'ubiId': 'MSKA7117', 'name': 'Training', 'externalReference': 'MSKA7117', 'operatorId': 3, 'displayName': 'Training - MSKA7117', 'displayNameForJsps': 'Training - MSKA7117', 'type': 'A' } second_costu = { 'id': 3158, 'prefix': 'MSA', 'ubiId': 'MSAA3158', 'name': 'UBIqube demo', 'externalReference': 'MSAA3158', 'operatorId': 10, 'displayName': 'UBIqube demo - MSAA3158', 'displayNameForJsps': 'UBIqube demo - MSAA3158', 'type': 'A' } customers = [first_costu, second_costu] with patch('requests.get') as mock_call_get: mock_call_get.return_value.text = json.dumps(customers) lookup = lookup_fixture lookup.look_list_customer_ids() assert lookup.path == '/lookup/customers' assert _is_valid_json(lookup.content) assert json.loads(lookup.content)[0] == first_costu assert json.loads(lookup.content)[1] == second_costu
def test_operator_ids(mock_post, lookup_fixture): """Test a list of operator""" mock_post.return_value.json.return_value = {'token': '12345qwert'} first_operator = { 'id': 39311, 'prefix': 'DGT', 'ubiId': 'DGTG39311', 'name': 'mnDelegation_Test', 'externalReference': 'MDGT', 'operatorId': 418, 'displayName': 'mnDelegation_Test - MDGT', 'displayNameForJsps': 'mnDelegation_Test - MDGT', 'type': 'G' } second_operator = { 'id': 39331, 'prefix': 'RMG', 'ubiId': 'RMGG39331', 'name': 'mnDlg_TestRMG', 'externalReference': 'MRMG', 'operatorId': 420, 'displayName': 'mnDlg_TestRMG - MRMG', 'displayNameForJsps': 'mnDlg_TestRMG - MRMG', 'type': 'G' } operators = [first_operator, second_operator] with patch('requests.get') as mock_call_get: mock_call_get.return_value.text = json.dumps(operators) lookup = lookup_fixture lookup.look_list_operators_id(1234) assert lookup.path == '/lookup/operators/id/1234' assert _is_valid_json(lookup.content) assert json.loads(lookup.content) == operators
def test_list_service_instances(orchestration_fixture): """ Test List Service Instances """ device_info = ( '[{"name":"Process/Reference/Customer/Kibana/kibana_dashboard",' '"id":2102,"serviceExternalReference":"MSASID2102","state":"ACTIVE"},' '{"name":"Process/Reference/Customer/Kibana/kibana_dashboard",' '"id":2258,"serviceExternalReference":"MSASID2258","state":"ACTIVE"},' '{"name":"Process/Reference/Customer/Kibana/kibana_dashboard",' '"id":2231,"serviceExternalReference":"MSASID2231","state":"ACTIVE"},' '{"name":"Process/Reference/Device_Management/Device_Management_List",' '"id":1536,"serviceExternalReference":"MSASID1536","state":"ACTIVE"}]') with patch('requests.get') as mock_call_get: mock_call_get.return_value.text = device_info orch = orchestration_fixture orch.list_service_instances() assert orch.path == '/orchestration/MSAA19224/service/instance' assert _is_valid_json(orch.response.text)
def test_create_fail(device_fixture): """ Test fail create device """ device = device_fixture fail_return = {"errorCode": 500, "message": "Not found"} fail_response = { 'wo_status': 'FAIL', 'wo_comment': "Create device", 'wo_newparams': "Not found" } with patch('requests.post') as mock_call_post: mock_call_post.return_value = MagicMock(ok=False) mock_call_post.return_value.json.return_value = fail_return assert _is_valid_json(json.dumps(device.create())) assert device.path == '/device/v2/{}'.format(device.customer_id) assert device.content == json.dumps(fail_response) assert device.fail
def test_ping(device_fixture): """ Test ping """ device = device_fixture r_value = ('{"message":"--- ::1 ping statistics ---\\n5 packets ' 'transmitted, 5 received, 0% packet loss, ' 'time 3999ms\\nrtt min/avg/max/mdev = 0.014/0.020/0.027/0.007 ' 'ms","rawJSONResult":"{\\"sms_status\\":\\"OK\\",' '\\"sms_code\\":\\"\\",\\"sms_message\\":\\"--- ::1 ping ' 'statistics ---\\\\n5 packets transmitted, 5 received, 0% ' 'packet loss, time 3999ms\\\\nrtt min/avg/max/mdev = ' '0.014/0.020/0.027/0.007 ms\\"}","status":"OK"}') with patch('requests.get') as mock_call_get: mock_call_get.return_value.text = r_value assert _is_valid_json(device.ping('localhost')) assert device.path == '/device/ping/localhost' mock_call_get.assert_called_once()
def test_sec_nodes_fail(mock_post, lookup_fixture): """Test fail sec nodes """ mock_post.return_value.json.return_value = {'token': '12345qwert'} fail_return = {"errorCode": 500, "message": "Sec nodes not found"} fail_response = { 'wo_status': 'FAIL', 'wo_comment': "Get sec nodes", 'wo_newparams': "Sec nodes not found" } with patch('requests.get') as mock_call_get: mock_call_get.return_value = MagicMock(ok=False) mock_call_get.return_value.json.return_value = fail_return lookup = lookup_fixture lookup.look_list_sec_nodes() assert lookup.path == '/lookup/sec_nodes' assert _is_valid_json(lookup.content) assert lookup.content == json.dumps(fail_response)
def test_run_jsa_command_device_empty_params(device_fixture): """ Test run_jsa_command_device on virtual device """ device = device_fixture device_id = 133 device.device_id = device_id response_content = '{"status": "OK", "result": "", "rawJSONResult": "{\"sms_status\":\"OK\"}", "rawSmsResult": None, "code": "OK", "ok": True, "message": "Successfully processed"}' command = 'get_files' params = dict() with patch('requests.post') as mock_call_post: mock_call_post.return_value.text = response_content assert _is_valid_json( json.dumps(device.run_jsa_command_device(command, params))) path = ('/sms/cmd/{}/{}/') assert path.format(command, device_id) in device.path assert device.device_id == device_id assert not device.fail mock_call_post.assert_called_once()
def test_execute_command_on_device(device_fixture): """ Test execute_command_on_device on virtual device """ device = device_fixture device_id = 133 device.device_id = device_id response_content = '{"device_id": 133}' # "response": "{'status': 'OK', 'result': 'show version\\nCisco I...V#', 'rawJSONResult': '{\"sms_status\":\"OK\",\"sms_result\":\"show version\\\\nCisco IOS XE So...', 'rawSmsResult': 'show ...', 'code': 'OK', 'ok': True, 'message': 'Successfully processed'}" command = 'show user' with patch('requests.get') as mock_call_post: mock_call_post.return_value.text = response_content assert _is_valid_json( json.dumps(device.execute_command_on_device(command))) path = ('/device/v1/command/execute/{}') assert path.format(device_id) in device.path assert device.device_id == device_id assert not device.fail mock_call_post.assert_called_once()
def test_run_jsa_command_device(device_fixture): """ Test run_jsa_command_device on virtual device """ device = device_fixture device_id = 133 device.device_id = device_id response_content = '{"device_id": 1234, "name": "jupiner_18_3"}' command = 'get_files' params = dict(src_dir='/tmp/', file_pattern='testfile.txt', dest_dir='/tmp') with patch('requests.post') as mock_call_post: mock_call_post.return_value.text = response_content assert _is_valid_json( json.dumps(device.run_jsa_command_device(command, params))) path = ('/sms/cmd/{}/{}/') assert path.format(command, device_id) in device.path assert device.device_id == device_id assert not device.fail mock_call_post.assert_called_once()