NAME
Test::Mock::Net::SNMP - Perl extension for mocking Net::SNMP in your unit tests.
SYNOPSIS
use Test::Mock::Net::SNMP;
my $mock_snmp = Test::Mock::Net::SNMP->new();
DESCRIPTION
Test::Mock::Net::SNMP is a simple way to mock a Net::SNMP object and allows you to test your modules behaviour when retrieving SNMP data or sending SNMP traps.
METHODS
new
my $mock_net_snmp = Test::Mock::Net::SNMP->new();
Generates the mock object required to mock Net::SNMP
set_varbindlist
$mock_net_snmp->set_varbindlist( [ {'1.3.6.1.2.1.2.2.1' => 1, '1.3.6.1.2.1.2.2.2' => 2, '1.3.6.1.2.1.2.2.3' => 3, '1.3.6.1.2.1.2.2.4' => 4}, {'1.3.6.1.2.1.2.2.5' => 5, '1.3.6.1.2.1.2.3.1' => 1, '1.3.6.1.2.1.2.3.2' => 2, '1.3.6.1.2.1.2.3.3' => 5} ] );
set_varbindlist is the main way of returning values in Net::SNMP the most important part of setting up the mock is setting this correctly takes an array reference of varbindlist hashes and returns true
This will also set up varbindnames, but you can overwrite this by calling set_varbindnames (see below). set_varbindlist will overwrite varbindnames so it's best to call this first.
To force a failed return for a request method or a call to var_bind_list assign a value of undef to the array element that represents that call.
i.e. if we have a blocking get_request that is performed after two set request, and we want that request to fail to make sure that our code is handling a the failure correctly, we could set it up like this:
$mock_net_snmp->set_varbindlist( [ { '1.3.6.1.2.1.1.4.0' => 'Help Desk x911', '1.3.6.1.2.1.1.6.0' => 'Building 1, First Floor' }, { '1.3.6.1.2.1.1.4.0' => 'Help Desk x911', '1.3.6.1.2.1.1.6.0' => 'Building 1, Second Floor' }, undef, { '1.3.6.1.2.1.1.3.0' => 600 } ] );
set_varbindnames
$mock_net_snmp->set_varbindnames([[qw( 2.2.1 2.2.3 2.2.4 )]]);
varbindnames is a list of names for each oid, it should match the keys of the hash that the call to var_bind_list returns.
set_varbindnames takes an array reference of arrays of oids.
set_varbindtypes
$mock_net_snmp->set_varbindtypes( [ { '1.2.1.1' => OCTET_STRING, '1.2.1.2' => OCTET_STRING, '1.2.1.3' => OCTET_STRING }, { '1.2.2.1' => OCTET_STRING, '1.2.2.2' => OCTET_STRING, '1.2.2.3' => OCTET_STRING } ] );
varbindtypes is a hash of types for each oid
set_varbindtypes takes an array reference of varbindtypes
set_session_failure
$mock_net_snmp->set_session_failure()
calling this method will mean that all calls to Net::SNMP->session will fail.
To revert this you need to call reset_values (see below)
set_error
$mock_net_snmp->set_error('Error message');
This method allows you to override the error message that will be returned if an error occurs.
set_error_status
$mock_net_snmp->set_error_status($status);
This lets you set the return value of an $snmp->error_status() call.
set_error_index
$mock_net_snmp->set_error_index($index);
This lets you set the return value of an $snmp->error_index() call.
get_option_val
is($mock_net_snmp->get_option_val('session','-hostname'),q{myhost.myserver.com},q{correct hostname passed to session});
is_deeply($mock_net_snmp->get_option_val('get_request','-varbindlist',0),['1.2.2.1'],q{first call to get_request is for 1.2.2.1});
is($mock_net_snmp->get_option_val($method,$option,$position), $expected, qq{$option passed to $method in call $postition is $expected});
where: $method is the mocked method, $option is the option passed into the method, $position is the position in the call stack (the last call is returned if no position is given)
it returns the value for that option.
Net::SNMP lets you specify options in a style such as -varbindlist or Varbindlist. Test::Mock::Net::SNMP expects you to retrieve the option values using the same style as the option passed in. So if your method call uses Varbindlist then $option should equal Varbindlist.
get_num_method_calls
$mock_net_snmp->get_num_method_calls('get_request');
returns the number of times that the requested method was called.
reset_values
$mock_net_snmp->reset_values();
Sets all the values to their original state.
clear_error
$mock_net_snmp->clear_error();
Test::Mock::Net::SNMP will only update the error string if it hasn't already been set. This means that sometimes it is useful to clear the error string
set_trap_failure
$mock_net_snmp->set_trap_failure();
force a trap method to fail.
ADDITIONAL CHECKS
Some additional checks are performed to make sure that:
1.) The methods that expect a -varbindlist option are passed one 2.) The first two oids of an snmpv2_trap and inform_request are there 3.) those functions that should pass multiples of three do. 4.) checks that some arguments are within range.
It's important to not rely on these checks and instead check them through your unit tests, but you may find your tests dying if the input values are known to be incorrect.
SEE ALSO
Net::SNMP, Test::MockObject::Extends
AUTHOR
Rob Halliday, <robh@cpan.org>
COPYRIGHT AND LICENSE
Copyright (C) 2013 by Rob Halliday
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.14.2 or, at your option, any later version of Perl 5 you may have available.