1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use English qw(-no_match_vars);
7use Test::Deep;
8use Test::Exception;
9use Test::More;
10
11use FusionInventory::Agent::XML::Response;
12use FusionInventory::Agent::SNMP::Live;
13
14plan tests => 12;
15
16my $snmp;
17throws_ok {
18    $snmp = FusionInventory::Agent::SNMP::Live->new();
19} qr/^no hostname parameter/,
20'instanciation: no hostname parameter';
21
22throws_ok {
23    $snmp = FusionInventory::Agent::SNMP::Live->new(
24        hostname => 'localhost',
25        version  => 'foo'
26    );
27} qr/^invalid SNMP version/,
28'instanciation: invalid version parameter';
29
30throws_ok {
31    $snmp = FusionInventory::Agent::SNMP::Live->new(
32        hostname => 'localhost',
33        version  => 5
34    );
35} qr/^invalid SNMP version/,
36'instanciation: invalid version parameter';
37
38throws_ok {
39    $snmp = FusionInventory::Agent::SNMP::Live->new(
40        hostname => 'localhost',
41        version => 1
42    );
43} qr/[Cc]ommunity (is )?not defined/,
44'instanciation: undefined community';
45
46throws_ok {
47    $snmp = FusionInventory::Agent::SNMP::Live->new(
48        version   => 1,
49        community => 'public',
50        hostname  => 'none'
51    );
52} qr/^Unable to resolve the UDP\/IPv4 address "none"/,
53'instanciation: unresolvable host';
54
55throws_ok {
56    $snmp = FusionInventory::Agent::SNMP::Live->new(
57        version   => 1,
58        community => 'public',
59        hostname  => '1.1.1.1'
60    );
61} qr/no response from host 1.1.1.1/,
62'instanciation: unresponding host';
63
64SKIP: {
65skip 'live SNMP test disabled', 6 unless $ENV{TEST_LIVE_SNMP};
66
67lives_ok {
68    $snmp = FusionInventory::Agent::SNMP::Live->new(
69        version   => '1',
70        community => 'public',
71        hostname  => 'localhost'
72    );
73} 'SNMPv1: instanciation';
74
75is(
76    $snmp->get('1.3.6.1.2.1.1.9.1.3.3'),
77    'The SNMP Management Architecture MIB.',
78    'SNMPv1: simple value query'
79);
80
81cmp_deeply(
82    $snmp->walk('1.3.6.1.2.1.1.9.1.3'),
83    {
84        1  => 'The MIB for Message Processing and Dispatching.',
85        2  => 'The management information definitions for the SNMP User-based Security Model.',
86        3  => 'The SNMP Management Architecture MIB.',
87        4  => 'The MIB module for SNMPv2 entities',
88        5  => 'The MIB module for managing TCP implementations',
89        6  => 'The MIB module for managing IP and ICMP implementations',
90        7  => 'The MIB module for managing UDP implementations',
91        8  => 'View-based Access Control Model for SNMP.',
92        9  => 'The MIB modules for managing SNMP Notification, plus filtering.',
93        10 => 'The MIB module for logging SNMP Notifications.'
94    },
95    'SNMPv1: multiple value query'
96);
97
98lives_ok {
99    $snmp = FusionInventory::Agent::SNMP::Live->new(
100        version   => '2c',
101        community => 'public',
102        hostname  => 'localhost'
103    );
104} 'SNMPv2c: instanciation';
105
106is(
107    $snmp->get('1.3.6.1.2.1.1.9.1.3.3'),
108    'The SNMP Management Architecture MIB.',
109    'SNMPv2c: simple value query'
110);
111
112cmp_deeply(
113    $snmp->walk('1.3.6.1.2.1.1.9.1.3'),
114    {
115        1  => 'The MIB for Message Processing and Dispatching.',
116        2  => 'The management information definitions for the SNMP User-based Security Model.',
117        3  => 'The SNMP Management Architecture MIB.',
118        4  => 'The MIB module for SNMPv2 entities',
119        5  => 'The MIB module for managing TCP implementations',
120        6  => 'The MIB module for managing IP and ICMP implementations',
121        7  => 'The MIB module for managing UDP implementations',
122        8  => 'View-based Access Control Model for SNMP.',
123        9  => 'The MIB modules for managing SNMP Notification, plus filtering.',
124        10 => 'The MIB module for logging SNMP Notifications.'
125    },
126    'SNMPv2c: multiple value query'
127);
128}
129