Пример #1
0
def test_server_active_custom():
    """
    Test setting zbx_server with custom value
    """
    zbx_senderprotocol = protobix.SenderProtocol()
    assert zbx_senderprotocol.server_active == '127.0.0.1'
    zbx_senderprotocol.server_active = 'myserver.domain.tld'
    assert zbx_senderprotocol.server_active == 'myserver.domain.tld'
Пример #2
0
def test_server_port_custom():
    """
    Test setting server_port with custom value
    """
    zbx_senderprotocol = protobix.SenderProtocol()
    assert zbx_senderprotocol.server_port == 10051
    zbx_senderprotocol.server_port = 10052
    assert zbx_senderprotocol.server_port == 10052
Пример #3
0
def test_debug_custom():
    """
    Test setting server_port with custom value
    """
    zbx_senderprotocol = protobix.SenderProtocol()
    assert zbx_senderprotocol.debug_level == 3
    zbx_senderprotocol.debug_level = 4
    assert zbx_senderprotocol.debug_level == 4
Пример #4
0
def test_debug_invalid_greater_than_5():
    """
    Test setting server_port with invalid value
    """
    zbx_senderprotocol = protobix.SenderProtocol()
    with pytest.raises(ValueError) as err:
        zbx_senderprotocol.debug_level = 10
    assert str(err.value) == 'DebugLevel must be between 0 and 5, 10 provided'
    assert zbx_senderprotocol.debug_level == 3
Пример #5
0
def test_server_port_invalid_lower_than_1024():
    """
    Test setting server_port with invalid value
    """
    zbx_senderprotocol = protobix.SenderProtocol()
    with pytest.raises(ValueError) as err:
        zbx_senderprotocol.server_port = 1000
    assert str(err.value) == 'ServerPort must be between 1024 and 32767'
    assert zbx_senderprotocol.server_port == 10051
Пример #6
0
def test_dryrun_custom():
    """
    Test setting dryrun with custom value
    """
    zbx_senderprotocol = protobix.SenderProtocol()
    assert zbx_senderprotocol._config.dryrun is False
    zbx_senderprotocol._config.dryrun = True
    assert zbx_senderprotocol._config.dryrun is True
    zbx_senderprotocol._config.dryrun = False
    assert zbx_senderprotocol._config.dryrun is False
Пример #7
0
def test_dryrun_invalid():
    """
    Test setting dryrun with invalid value
    """
    zbx_senderprotocol = protobix.SenderProtocol()
    assert zbx_senderprotocol._config.dryrun is False
    with pytest.raises(ValueError) as err:
        zbx_senderprotocol._config.dryrun = 'invalid'
    assert str(err.value) == 'dryrun parameter requires boolean'
    assert zbx_senderprotocol._config.dryrun is False
Пример #8
0
def test_default_params():
    """
    Default configuration
    """
    zbx_senderprotocol = protobix.SenderProtocol()

    assert zbx_senderprotocol.server_active == '127.0.0.1'
    assert zbx_senderprotocol.server_port == 10051
    assert zbx_senderprotocol._config.dryrun is False
    assert zbx_senderprotocol.items_list == []
Пример #9
0
 def test_need_backend_socket_tls_unencrypted(mock_configobj):
     """
     Test socket with no TLS
     """
     mock_configobj.side_effect = [{
         'TLSConnect': 'unencrypted',
     }]
     zbx_senderprotocol = protobix.SenderProtocol()
     _socket = zbx_senderprotocol._socket()
     assert isinstance(_socket, socket.socket)
Пример #10
0
def test_read_from_zabbix_invalid_content(mock_socket):
    """
    Test sending data to Zabbix Server
    """
    answer_payload = '{"info": "invalid content", "response": "success"}'
    answer_packet = b('ZBXD\1') + struct.pack('<Q', 93) + b(answer_payload)
    mock_socket.recv.return_value = answer_packet

    zbx_senderprotocol = protobix.SenderProtocol()
    zbx_senderprotocol.data_type = 'item'
    zbx_senderprotocol.socket = mock_socket
    with pytest.raises(IndexError):
        zbx_senderprotocol._read_from_zabbix()
Пример #11
0
 def test_need_backend_init_tls_cert_verify_fails(mock_configobj):
     """
     Test TLS context initialization
     """
     mock_configobj.side_effect = [{
         'TLSConnect':
         'cert',
         'TLSCAFile':
         'tests/tls_ca/protobix-ca.cert.pem',
         'TLSCertFile':
         'tests/tls_ca/protobix-client.cert.pem',
         'TLSKeyFile':
         'tests/tls_ca/protobix-client.key.pem'
     }]
     zbx_senderprotocol = protobix.SenderProtocol()
     with pytest.raises(ssl.SSLError):
         zbx_senderprotocol._socket()
Пример #12
0
 def test_need_backend_socket_tls_cert(mock_configobj):
     """
     Test socket with TLS
     """
     mock_configobj.side_effect = [{
         'TLSConnect':
         'cert',
         'TLSCAFile':
         'tests/tls_ca/rogue-protobix-ca.cert.pem',
         'TLSCertFile':
         'tests/tls_ca/rogue-protobix-client.cert.pem',
         'TLSKeyFile':
         'tests/tls_ca/rogue-protobix-client.key.pem'
     }]
     zbx_senderprotocol = protobix.SenderProtocol()
     _socket = zbx_senderprotocol._socket()
     assert isinstance(_socket, ssl.SSLSocket)
Пример #13
0
 def test_init_tls_non_matching_cert_key(mock_configobj):
     """
     Test TLS context initialization
     """
     mock_configobj.side_effect = [{
         'TLSConnect':
         'cert',
         'TLSCAFile':
         'tests/tls_ca/protobix-ca.cert.pem',
         'TLSCertFile':
         'tests/tls_ca/rogue-protobix-client.cert.pem',
         'TLSKeyFile':
         'tests/tls_ca/protobix-client.key.pem'
     }]
     zbx_senderprotocol = protobix.SenderProtocol()
     with pytest.raises(ssl.SSLError) as err:
         tls_context = zbx_senderprotocol._init_tls()
Пример #14
0
def test_read_from_zabbix_valid_content(mock_socket):
    """
    Test sending data to Zabbix Server
    """
    answer_payload = '{"info": "processed: 0; failed: 1; total: 1; seconds spent: 0.000441", "response": "success"}'
    answer_packet = b('ZBXD\1') + struct.pack('<Q', 93) + b(answer_payload)
    mock_socket.recv.return_value = answer_packet

    zbx_senderprotocol = protobix.SenderProtocol()
    zbx_senderprotocol.data_type = 'item'
    zbx_senderprotocol.socket = mock_socket
    srv_response, processed, failed, total, time = zbx_senderprotocol._read_from_zabbix(
    )
    assert srv_response == 'success'
    assert processed == 0
    assert failed == 1
    assert total == 1
    assert time == 0.000441
Пример #15
0
def test_send_to_zabbix(mock_socket):
    """
    Test sending data to Zabbix Server
    """
    item = {
        'host': 'myhostname',
        'key': 'my.item.key',
        'value': 1,
        'clock': int(time.time())
    }
    payload = json.dumps({
        "data": [item],
        "request": "sender data",
        "clock": int(time.time())
    })
    packet = b('ZBXD\1') + struct.pack('<Q', 136) + b(payload)

    zbx_senderprotocol = protobix.SenderProtocol()
    zbx_senderprotocol.socket = mock_socket
    zbx_senderprotocol.data_type = 'item'
    zbx_senderprotocol._items_list.append(item)
    zbx_senderprotocol._send_to_zabbix(zbx_senderprotocol._items_list)

    zbx_senderprotocol.socket.sendall.assert_called_with(packet)
Пример #16
0
def test_clock_integer():
    """
    Test clock method
    """
    zbx_senderprotocol = protobix.SenderProtocol()
    assert isinstance(zbx_senderprotocol.clock, int) is True
Пример #17
0
def test_clock_accurate():
    """
    Test clock method
    """
    zbx_senderprotocol = protobix.SenderProtocol()
    assert zbx_senderprotocol.clock == int(time.time())