Example #1
0
def test_close_raises_commerror_on_socket_close_exception():
    with mock.patch.object(Socket, 'close') as mock_close:
        mock_close.side_effect = Exception
        with pytest.raises(CommError):
            driver = CIPDriver(CONNECT_PATH)
            driver._sock = Socket()
            driver.close()
Example #2
0
def test__forward_close_returns_true_if_response():
    driver = CIPDriver(CONNECT_PATH)
    driver._session = 1
    response = (
        b"o\x00\x1e\x00\x02\x16\x02\x0b\x00\x00\x00\x00_pycomm_"
        b"\x00\x00\x00\x00\x00\x00\x00\x00\n\x00\x02\x00\x00\x00"
        b"\x00\x00\xb2\x00\x0e\x00\xce\x00\x00\x00'\x04\t\x10\xd6\x9c\x06=\x00\x00"
    )
    driver._sock = Mocket(response)
    assert driver._forward_close()
Example #3
0
def test_get_module_info_returns_expected_identity_dict():
    EXPECTED_DICT = {
        'vendor': 'Rockwell Automation/Allen-Bradley',
        'product_type': 'Programmable Logic Controller',
        'product_code': 89,
        'revision': {
            'major': 20,
            'minor': 19
        },
        'status': b'`0',
        'serial': 'c00fa09b',
        'product_name': '1769-L23E-QBFC1 LOGIX5323E-QBFC1'
    }

    RESPONSE_BYTES = (
        b'o\x00C\x00\x02\x13\x02\x0b\x00\x00\x00\x00_pycomm_\x00\x00\x00\x00\x00\x00\x00\x00\n'
        b'\x00\x02\x00\x00\x00\x00\x00\xb2\x003\x00\x81\x00\x00\x00\x01\x00\x0e\x00Y\x00\x14\x13'
        b'`0\x9b\xa0\x0f\xc0 1769-L23E-QBFC1 LOGIX5323E-QBFC1')

    driver = CIPDriver(CONNECT_PATH)
    driver._sock = Mocket(RESPONSE_BYTES)
    actual_response = driver.get_module_info(1)
    assert actual_response == EXPECTED_DICT
Example #4
0
def test_get_module_info_returns_expected_identity_dict():
    EXPECTED_DICT = {
        "vendor": "Rockwell Automation/Allen-Bradley",
        "product_type": "Programmable Logic Controller",
        "product_code": 89,
        "revision": {
            "major": 20,
            "minor": 19
        },
        "status": b"`0",
        "serial": "c00fa09b",
        "product_name": "1769-L23E-QBFC1 LOGIX5323E-QBFC1",
    }

    RESPONSE_BYTES = (
        b"o\x00C\x00\x02\x13\x02\x0b\x00\x00\x00\x00_pycomm_\x00\x00\x00\x00\x00\x00\x00\x00\n"
        b"\x00\x02\x00\x00\x00\x00\x00\xb2\x003\x00\x81\x00\x00\x00\x01\x00\x0e\x00Y\x00\x14\x13"
        b"`0\x9b\xa0\x0f\xc0 1769-L23E-QBFC1 LOGIX5323E-QBFC1")

    driver = CIPDriver(CONNECT_PATH)
    driver._sock = Mocket(RESPONSE_BYTES)
    actual_response = driver.get_module_info(1)
    assert actual_response == EXPECTED_DICT
Example #5
0
def test__forward_open_returns_false_if_falsy_response():
    driver = CIPDriver(CONNECT_PATH)
    driver._sock = Mocket()
    driver._session = 1
    assert not driver._forward_open()
Example #6
0
def test__register_session_returns_none_if_no_response():
    driver = CIPDriver(CONNECT_PATH)
    driver._sock = Mocket()
    assert driver._register_session() is None
Example #7
0
def test__register_session_returns_configured_session(conf_session):
    driver = CIPDriver(CONNECT_PATH)
    driver._sock = Mocket(bytes(4) + UDINT.encode(conf_session) + bytes(20))
    assert conf_session == driver._register_session()
Example #8
0
def test_open_returns_true_if_register_session_truthy():
    with mock.patch.object(CIPDriver, '_register_session') as mock_register:
        mock_register.return_value = 1
        driver = CIPDriver(CONNECT_PATH)
        driver._sock = Mocket()
        assert driver.open()
Example #9
0
def test_open_returns_false_if_register_session_falsy():
    driver = CIPDriver(CONNECT_PATH)
    driver._sock = Mocket()
    assert not driver.open()
Example #10
0
def test_close_calls_socket_close_if_socket():
    with mock.patch.object(Mocket, 'close') as mock_close:
        driver = CIPDriver(CONNECT_PATH)
        driver._sock = Mocket()
        driver.close()
        assert mock_close.called
Example #11
0
def test_close_raises_no_error_on_close_with_registered_session():
    driver = CIPDriver(CONNECT_PATH)
    driver._session = 1
    driver._sock = Mocket()
    driver.close()