def test_snmpset_wrong_type():
    """
    When an attempt is made to set a variable of one type (an integer, 
    for example) with a value of another type (say, a string), our function 
    should raise an SNMPWriteError to let us know this is not OK.
    """
    with pytest.raises(SNMPWriteError) as excinfo:
        snmpset(ipaddress=SNMP_SRV_ADDR, oid='SNMPv2-MIB::sysName.0',
                value_type='a', value='255.255.255.255', port=SNMP_SRV_PORT)
    assert 'Bad variable type' in str(excinfo.value)
def test_snmpset_unwritable_field():
    """
    When an attempt is made to write to a field the target device doesn't 
    support writing to, net-snmp's snmpget command produces a "No Such 
    Instance" error. Our function should replicate this
    """
    with pytest.raises(SNMPWriteError) as excinfo:
        snmpset(ipaddress=SNMP_SRV_ADDR, oid='SNMPv2-MIB::sysDescr.0',
                community='public', value_type='s',
                value='Test Description', port=SNMP_SRV_PORT)
    assert 'No Such Instance' in str(excinfo.value)
def test_snmpset_non_existant_type():
    """
    There are a specific set of type codes which net-snmp's snmpset command 
    will accept. Our function should raise an SNMPWriteError if none of those 
    are specified
    """
    with pytest.raises(SNMPWriteError) as excinfo:
        snmpset(ipaddress=SNMP_SRV_ADDR, community='public',
                oid='SNMPv2-MIB::sysName.0', value_type='z',
                value='Test Description', port=SNMP_SRV_PORT)
    assert str(excinfo.value) == 'The type value you specified does not ' \
                                 'match one of the accepted type codes.\n' \
                                 'Valid type codes are one of ' \
                                 '(i|u|t|a|o|s|x|d|b)'
def test_snmpset_value_out_of_range_error():
    """
    If an attempt is made to write a value to an OID which is larger than the 
    max allowable size for that OID, an SNMPWriteError should be raised to let 
    us know 
    """
    with pytest.raises(SNMPWriteError) as excinfo:
        snmpset(ipaddress=SNMP_SRV_ADDR, oid='SNMPv2-MIB::sysName.0',
                value_type='s', value='Thiiiiiiiiiiiiiiiiiiiiiiiiiiiiis '
                'sssssttttttttrrrriiiiiiiiiiiiiiinnnnnnnnnnnnng is '
                'wwwwwwaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaayyyyyyyyyy '
                'tttoooooooooooooooooooooooooooooooooooooooooooooo '
                'lllooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnggggg'
                ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!', port=SNMP_SRV_PORT)
    assert 'Value out of range' in str(excinfo.value)
def test_snmp_timeout():
    """
    all snmp commands should implement the check_timeout function
    """
    for command in snmp_commands:
        with pytest.raises(SNMPTimeout) as excinfo:
            command(ipaddress='10.0.0.1', oid='IF-MIB::ifTable', timeout='1')
        assert 'Timeout' in str(excinfo.value)

    with pytest.raises(SNMPTimeout) as excinfo:
        snmpgetsome(ipaddress='10.0.0.1', oids=['IF-MIB::ifTable'], timeout='1')
    assert 'Timeout' in str(excinfo.value)

    with pytest.raises(SNMPTimeout) as excinfo:
        snmpset(community='public', ipaddress='10.0.0.1',
                oid='IF-MIB::ifTable', value_type='s',
                value='random string', timeout='1')
    assert 'Timeout' in str(excinfo.value)
def test_snmpset_return_structure():
    """
    On success, snmpset should return a string detailing the OID that was 
    updated, and the value that was set
    """
    result = snmpset(ipaddress=SNMP_SRV_ADDR, community='public',
                     oid='SNMPv2-MIB::sysName.0', value_type='s',
                     value='Test Description', port=SNMP_SRV_PORT)
    assert 'Test Description' in result