snmpjr 0.2.2-java → 0.3.0-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.semver +2 -2
- data/README.md +33 -10
- data/history.rdoc +2 -0
- data/lib/snmpjr/configuration.rb +13 -0
- data/lib/snmpjr/configuration_v2c.rb +22 -0
- data/lib/snmpjr/configuration_v3.rb +51 -0
- data/lib/snmpjr/getter.rb +18 -19
- data/lib/snmpjr/{pdu.rb → pdu_v2c.rb} +8 -5
- data/lib/snmpjr/pdu_v3.rb +14 -0
- data/lib/snmpjr/{session.rb → session_v2c.rb} +4 -2
- data/lib/snmpjr/session_v3.rb +63 -0
- data/lib/snmpjr/target_v2c.rb +18 -0
- data/lib/snmpjr/target_v3.rb +18 -0
- data/lib/snmpjr/version.rb +4 -1
- data/lib/snmpjr/walker.rb +3 -2
- data/lib/snmpjr/wrappers/mp.rb +9 -0
- data/lib/snmpjr/wrappers/security.rb +9 -0
- data/lib/snmpjr.rb +29 -31
- data/snmpjr.gemspec +1 -1
- data/spec/integration/{snmpjr_get_spec.rb → snmp_v2c/snmpjr_get_spec.rb} +18 -5
- data/spec/integration/{snmpjr_walk_spec.rb → snmp_v2c/snmpjr_walk_spec.rb} +20 -5
- data/spec/integration/snmp_v3/snmpjr_get_spec.rb +67 -0
- data/spec/integration/snmp_v3/snmpjr_walk_spec.rb +54 -0
- data/spec/snmpjr/configuration_v2c_spec.rb +30 -0
- data/spec/snmpjr/configuration_v3_spec.rb +90 -0
- data/spec/snmpjr/getter_spec.rb +24 -27
- data/spec/snmpjr/{pdu_spec.rb → pdu_v2c_spec.rb} +2 -2
- data/spec/snmpjr/pdu_v3_spec.rb +17 -0
- data/spec/snmpjr/{session_spec.rb → session_v2c_spec.rb} +1 -1
- data/spec/snmpjr/session_v3_spec.rb +22 -0
- data/spec/snmpjr/{target_spec.rb → target_v2c_spec.rb} +16 -7
- data/spec/snmpjr/target_v3_spec.rb +50 -0
- data/spec/snmpjr/walker_spec.rb +1 -2
- data/spec/snmpjr_spec.rb +68 -35
- metadata +37 -16
- data/lib/snmpjr/target.rb +0 -17
- data/lib/snmpjr/target_timeout_error.rb +0 -5
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'snmpjr'
|
2
|
+
require 'snmpjr/response'
|
3
|
+
require 'snmpjr/session_v3'
|
4
|
+
|
5
|
+
describe "snmpjr for snmp v3" do
|
6
|
+
|
7
|
+
describe 'GET' do
|
8
|
+
subject { Snmpjr.new(Snmpjr::Version::V3) }
|
9
|
+
|
10
|
+
context 'when the host is reachable' do
|
11
|
+
before do
|
12
|
+
subject.configure do |config|
|
13
|
+
config.host = 'demo.snmplabs.com'
|
14
|
+
config.user = 'usr-sha-aes128'
|
15
|
+
config.authentication 'SHA', 'authkey1'
|
16
|
+
config.privacy 'AES128', 'privkey1'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'can perform a simple synchronous get request on an snmp agent' do
|
21
|
+
expect(subject.get '1.3.6.1.2.1.1.1.0').to eq Snmpjr::Response.new(oid: '1.3.6.1.2.1.1.1.0', value: 'SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m')
|
22
|
+
end
|
23
|
+
|
24
|
+
let(:expected) { [Snmpjr::Response.new(oid: '1.3.6.1.2.1.1.1.0', value: 'SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m'),
|
25
|
+
Snmpjr::Response.new(oid: '1.3.6.1.2.1.1.5.0', value: 'zeus.snmplabs.com')] }
|
26
|
+
it 'can perform a series of gets if passed an array of oids' do
|
27
|
+
expect(subject.get ['1.3.6.1.2.1.1.1.0', '1.3.6.1.2.1.1.5.0']).to eq expected
|
28
|
+
end
|
29
|
+
|
30
|
+
context "when an invalid oid is requested" do
|
31
|
+
before do
|
32
|
+
subject.configure do |config|
|
33
|
+
config.host = 'demo.snmplabs.com'
|
34
|
+
config.user = 'usr-sha-des'
|
35
|
+
config.authentication 'SHA', 'authkey1'
|
36
|
+
config.privacy 'DES', 'privkey1'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
let(:expected) { [Snmpjr::Response.new(oid: '1.3.6.1.2.1.1.5', error: 'noSuchInstance'),
|
41
|
+
Snmpjr::Response.new(oid: '1.3.6.1.2.1.1.5.0', value: 'zeus.snmplabs.com')] }
|
42
|
+
|
43
|
+
it 'returns an error' do
|
44
|
+
expect(subject.get ['1.3.6.1.2.1.1.5', '1.3.6.1.2.1.1.5.0']).to eq expected
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'when the host is unreachable' do
|
50
|
+
before do
|
51
|
+
subject.configure do |config|
|
52
|
+
config.host = 'demo.snmplabs.com'
|
53
|
+
config.port = 161
|
54
|
+
config.user = 'usr-sha-des'
|
55
|
+
config.authentication 'SHA', 'authkey1'
|
56
|
+
config.timeout = 50
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'the request times out after 5 seconds' do
|
61
|
+
expect{
|
62
|
+
subject.get '1.3.6.1.2.1.1.1.0'
|
63
|
+
}.to raise_error(Snmpjr::TargetTimeoutError)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'snmpjr'
|
2
|
+
require 'snmpjr/response'
|
3
|
+
require 'snmpjr/session_v3'
|
4
|
+
|
5
|
+
describe "snmpjr for snmp v3" do
|
6
|
+
|
7
|
+
describe 'WALK' do
|
8
|
+
subject { Snmpjr.new(Snmpjr::Version::V3) }
|
9
|
+
|
10
|
+
context 'when the host is reachable' do
|
11
|
+
before do
|
12
|
+
subject.configure do |config|
|
13
|
+
config.host = 'demo.snmplabs.com'
|
14
|
+
config.port = 161
|
15
|
+
config.context = '80004fb805636c6f75644dab22cc'
|
16
|
+
config.user = 'usr-none-none'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'can perform a simple synchronous walk request on an snmp agent' do
|
21
|
+
response = subject.walk '1.3.6.1.2.1.1'
|
22
|
+
expect(response.count).to eq 11
|
23
|
+
expect(response.first.to_s).to eq 'SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m'
|
24
|
+
expect(response.last.to_s).to match(/^\d+\:\d+:\d+\.\d+$/)
|
25
|
+
end
|
26
|
+
|
27
|
+
context "when a non existent subtree is walked" do
|
28
|
+
it 'returns an empty array' do
|
29
|
+
expect(subject.walk '1.3.6.1.5').to eq []
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'when the host is unreachable' do
|
35
|
+
before do
|
36
|
+
subject.configure do |config|
|
37
|
+
config.host = 'demo.snmplabs.com'
|
38
|
+
config.port = 161
|
39
|
+
config.context = '80004fb805636c6f75644dab22cc'
|
40
|
+
config.user = 'usr-sha-aes256'
|
41
|
+
config.authentication 'SHA', 'authkey1'
|
42
|
+
config.privacy 'AES256', 'privkey1'
|
43
|
+
config.timeout = 50
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'the request times out after 5 seconds' do
|
48
|
+
expect{
|
49
|
+
subject.walk '1.3.6.1.2.1.1'
|
50
|
+
}.to raise_error(Snmpjr::TargetTimeoutError)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require production_code
|
2
|
+
|
3
|
+
describe Snmpjr::ConfigurationV2C do
|
4
|
+
describe '#create_pdu' do
|
5
|
+
it 'creates a new PduV2 instance' do
|
6
|
+
expect(subject.create_pdu).to be_a Snmpjr::PduV2C
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#create_target' do
|
11
|
+
let(:target) { double :target }
|
12
|
+
let(:created_target) { double :created_target }
|
13
|
+
|
14
|
+
before do
|
15
|
+
allow(Snmpjr::TargetV2C).to receive(:new).and_return target
|
16
|
+
allow(target).to receive(:create).with(subject).and_return created_target
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'creates a target for snmp v2' do
|
20
|
+
subject.create_target
|
21
|
+
expect(subject.create_target).to eq created_target
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#create_session' do
|
26
|
+
it 'creates a session for snmp v2' do
|
27
|
+
expect(subject.create_session).to be_a Snmpjr::SessionV2C
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require production_code
|
2
|
+
|
3
|
+
describe Snmpjr::ConfigurationV3 do
|
4
|
+
describe '#authentication' do
|
5
|
+
it 'sets the authentication protocol' do
|
6
|
+
subject.authentication 'MD5', 'test1234'
|
7
|
+
expect(subject.authentication_protocol).to eq 'MD5'
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'sets the authentication key' do
|
11
|
+
subject.authentication 'MD5', 'test1234'
|
12
|
+
expect(subject.authentication_key).to eq 'test1234'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#privacy' do
|
17
|
+
it 'sets the privacy protocol' do
|
18
|
+
subject.privacy 'AES256', 'test1234'
|
19
|
+
expect(subject.privacy_protocol).to eq 'AES256'
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'sets the privacy key' do
|
23
|
+
subject.privacy 'AES256', 'test1234'
|
24
|
+
expect(subject.privacy_key).to eq 'test1234'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#create_pdu' do
|
29
|
+
it 'creates a new PduV3 instance' do
|
30
|
+
expect(subject.create_pdu).to be_a Snmpjr::PduV3
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'assigns a context to it' do
|
34
|
+
subject.context = 'some_context'
|
35
|
+
expect(Snmpjr::PduV3).to receive(:new).with('some_context')
|
36
|
+
subject.create_pdu
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#security_level' do
|
41
|
+
context 'noAuthNoPriv' do
|
42
|
+
it 'sets the security level to NoAuthNoPriv if no authentication or privacy are set' do
|
43
|
+
expect(subject.security_level).to eq Snmpjr::ConfigurationV3::SecurityLevels::NoAuthNoPriv
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'authNoPriv' do
|
48
|
+
it 'sets the security level to no authNoPriv if authentication but no privacy is set' do
|
49
|
+
subject.authentication 'MD5', 'test1234'
|
50
|
+
expect(subject.security_level).to eq Snmpjr::ConfigurationV3::SecurityLevels::AuthNoPriv
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'authPriv' do
|
55
|
+
it 'sets the security level to authPriv if both authentication and privacy are set' do
|
56
|
+
subject.authentication 'MD5', 'test1234'
|
57
|
+
subject.privacy 'AES256', 'test1234'
|
58
|
+
expect(subject.security_level).to eq Snmpjr::ConfigurationV3::SecurityLevels::AuthPriv
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe '#create_target' do
|
64
|
+
let(:target) { double :target }
|
65
|
+
let(:created_target) { double :created_target }
|
66
|
+
|
67
|
+
before do
|
68
|
+
allow(Snmpjr::TargetV3).to receive(:new).and_return target
|
69
|
+
allow(target).to receive(:create).with(subject).and_return created_target
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'creates a target for snmp v3' do
|
73
|
+
subject.create_target
|
74
|
+
expect(subject.create_target).to eq created_target
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe '#create_session' do
|
79
|
+
let(:created_session) { double :created_session }
|
80
|
+
|
81
|
+
before do
|
82
|
+
allow(Snmpjr::SessionV3).to receive(:new).with(subject).and_return created_session
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'creates a session for snmp v3' do
|
86
|
+
subject.create_session
|
87
|
+
expect(subject.create_session).to eq created_session
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/spec/snmpjr/getter_spec.rb
CHANGED
@@ -1,63 +1,60 @@
|
|
1
1
|
require production_code
|
2
|
+
require 'snmpjr/configuration'
|
2
3
|
|
3
4
|
describe Snmpjr::Getter do
|
4
5
|
let(:target) { double :target }
|
5
|
-
let(:session) { double
|
6
|
-
let(:pdu) { double
|
6
|
+
let(:session) { double :session }
|
7
|
+
let(:pdu) { double :pdu }
|
7
8
|
let(:created_pdu_single) { double :created_pdu_single }
|
8
9
|
|
9
|
-
|
10
|
+
let(:configuration) { Snmpjr::Configuration.new }
|
11
|
+
|
12
|
+
subject { described_class.new(session: session, target: target, pdu: pdu, config: configuration) }
|
10
13
|
|
11
14
|
before do
|
12
|
-
allow(Snmpjr::Pdu).to receive(:new).and_return pdu
|
13
|
-
allow(pdu).to receive(:create).with(['1.2.3.4.5.6']).and_return created_pdu_single
|
14
|
-
allow(Snmpjr::Session).to receive(:new).and_return session
|
15
15
|
allow(session).to receive(:start)
|
16
16
|
allow(session).to receive(:send).with(created_pdu_single, target).and_return ['Foo']
|
17
17
|
allow(session).to receive(:close)
|
18
18
|
end
|
19
19
|
|
20
|
-
describe '#
|
20
|
+
describe '#get' do
|
21
21
|
let(:created_pdu_multiple_1) { double :created_pdu_multiple_1 }
|
22
22
|
let(:created_pdu_multiple_2) { double :created_pdu_multiple_2 }
|
23
23
|
|
24
24
|
before do
|
25
|
+
configuration.max_oids_per_request = 1
|
25
26
|
allow(pdu).to receive(:create).with(['1.2.3.4.5.6']).and_return created_pdu_multiple_1
|
26
27
|
allow(pdu).to receive(:create).with(['6.5.4.3.2.1']).and_return created_pdu_multiple_2
|
27
28
|
allow(session).to receive(:send).with(created_pdu_multiple_1, target).and_return ['Foo']
|
28
29
|
allow(session).to receive(:send).with(created_pdu_multiple_2, target).and_return ['Bar']
|
29
30
|
end
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
32
|
+
context 'when there is a single oid' do
|
33
|
+
before do
|
34
|
+
allow(pdu).to receive(:create).with(['1.2.3.4.5.6']).and_return created_pdu_single
|
35
|
+
end
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
37
|
+
it 'performs a synchronous get' do
|
38
|
+
expect(subject.get ['1.2.3.4.5.6']).to eq 'Foo'
|
39
|
+
expect(session).to have_received(:send).with(created_pdu_single, target)
|
40
|
+
end
|
40
41
|
end
|
41
42
|
|
42
|
-
|
43
|
-
|
44
|
-
|
43
|
+
context 'when there are multiple oids' do
|
44
|
+
it 'performs multiple gets for each oid' do
|
45
|
+
expect(subject.get ['1.2.3.4.5.6', '6.5.4.3.2.1']).to eq(['Foo', 'Bar'])
|
46
|
+
expect(session).to have_received(:send).with(created_pdu_multiple_1, target)
|
47
|
+
expect(session).to have_received(:send).with(created_pdu_multiple_2, target)
|
48
|
+
end
|
45
49
|
end
|
46
|
-
end
|
47
50
|
|
48
|
-
describe '#get' do
|
49
51
|
it 'starts an snmp session' do
|
50
|
-
subject.get '1.2.3.4.5.6'
|
52
|
+
subject.get ['1.2.3.4.5.6', '6.5.4.3.2.1']
|
51
53
|
expect(session).to have_received(:start)
|
52
54
|
end
|
53
55
|
|
54
|
-
it 'performs a synchronous get' do
|
55
|
-
expect(subject.get '1.2.3.4.5.6').to eq 'Foo'
|
56
|
-
expect(session).to have_received(:send).with(created_pdu_single, target)
|
57
|
-
end
|
58
|
-
|
59
56
|
it 'closes the snmp session' do
|
60
|
-
subject.get '1.2.3.4.5.6'
|
57
|
+
subject.get ['1.2.3.4.5.6', '6.5.4.3.2.1']
|
61
58
|
expect(session).to have_received(:close)
|
62
59
|
end
|
63
60
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require production_code
|
2
2
|
|
3
|
-
describe Snmpjr::
|
3
|
+
describe Snmpjr::PduV2C do
|
4
4
|
describe '#create' do
|
5
5
|
|
6
6
|
let(:pdu) { double Snmpjr::Wrappers::PDU }
|
@@ -19,7 +19,7 @@ describe Snmpjr::Pdu do
|
|
19
19
|
allow(pdu).to receive(:add)
|
20
20
|
end
|
21
21
|
it 'creates a GET Pdu' do
|
22
|
-
expect(pdu).to receive(:type=).with(Snmpjr::
|
22
|
+
expect(pdu).to receive(:type=).with(Snmpjr::PduV2C::Constants::GET)
|
23
23
|
subject.create ['1.2.3.4']
|
24
24
|
end
|
25
25
|
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require production_code
|
2
|
+
|
3
|
+
describe Snmpjr::PduV3 do
|
4
|
+
subject { Snmpjr::PduV3.new 'context' }
|
5
|
+
|
6
|
+
describe '.new' do
|
7
|
+
it 'assigns scoped context name' do
|
8
|
+
expect(subject.create(['1.3.6']).context_name.to_s).to eq 'context'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#create' do
|
13
|
+
it 'creates a scoped Pdu' do
|
14
|
+
expect(subject.create ['1.3.6']).to be_a(Snmpjr::Wrappers::ScopedPDU)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require production_code
|
2
|
+
require 'snmpjr/configuration_v3'
|
3
|
+
|
4
|
+
describe Snmpjr::SessionV3 do
|
5
|
+
describe '.new' do
|
6
|
+
it 'creates and attaches the correct user security model (usm)' do
|
7
|
+
config = Snmpjr::ConfigurationV3.new
|
8
|
+
config.host = 'demo.snmplabs.com'
|
9
|
+
config.port = 161
|
10
|
+
config.user = 'usr-sha-des'
|
11
|
+
config.authentication 'SHA', 'authkey1'
|
12
|
+
config.privacy 'DES', 'privkey1'
|
13
|
+
|
14
|
+
session = described_class.new config
|
15
|
+
|
16
|
+
expect(session.snmp.usm.user_table.user_entries.first.usm_user.security_name.to_s).to eq 'usr-sha-des'
|
17
|
+
expect(session.snmp.usm.user_table.user_entries.first.usm_user.privacy_passphrase.to_s).to eq 'privkey1'
|
18
|
+
expect(session.snmp.usm.user_table.user_entries.first.usm_user.authentication_passphrase.to_s).to eq 'authkey1'
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -1,18 +1,27 @@
|
|
1
|
+
require 'snmpjr/configuration_v2c'
|
1
2
|
require production_code
|
2
3
|
|
3
|
-
describe Snmpjr::
|
4
|
+
describe Snmpjr::TargetV2C do
|
4
5
|
|
5
6
|
describe '#create' do
|
6
|
-
let(:
|
7
|
+
let(:configuration) do
|
8
|
+
config = Snmpjr::ConfigurationV2C.new
|
9
|
+
config.host = '127.0.0.1'
|
10
|
+
config.port = 161
|
11
|
+
config.community = 'some community'
|
12
|
+
config.retries = 2
|
13
|
+
config.timeout = 50
|
14
|
+
config
|
15
|
+
end
|
7
16
|
|
8
17
|
it 'creates an octet string for the community string' do
|
9
|
-
expect(Snmpjr::Wrappers::SMI::OctetString).to receive(:new).with(
|
10
|
-
subject.create(
|
18
|
+
expect(Snmpjr::Wrappers::SMI::OctetString).to receive(:new).with(configuration.community)
|
19
|
+
subject.create(configuration)
|
11
20
|
end
|
12
21
|
|
13
22
|
it 'creates an smi address based on the ip and port name' do
|
14
23
|
expect(Snmpjr::Wrappers::SMI::GenericAddress).to receive(:parse).with("udp:127.0.0.1/161")
|
15
|
-
subject.create(
|
24
|
+
subject.create(configuration)
|
16
25
|
end
|
17
26
|
|
18
27
|
let(:community_target) { double Snmpjr::Wrappers::CommunityTarget }
|
@@ -30,11 +39,11 @@ describe Snmpjr::Target do
|
|
30
39
|
expect(community_target).to receive(:version=).with(1)
|
31
40
|
expect(community_target).to receive(:timeout=).with(50)
|
32
41
|
expect(community_target).to receive(:retries=).with(2)
|
33
|
-
subject.create(
|
42
|
+
subject.create(configuration)
|
34
43
|
end
|
35
44
|
|
36
45
|
it 'returns the SNMP4J community target' do
|
37
|
-
expect(subject.create(
|
46
|
+
expect(subject.create(configuration)).to eq(community_target)
|
38
47
|
end
|
39
48
|
end
|
40
49
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'snmpjr/configuration_v3'
|
2
|
+
require production_code
|
3
|
+
|
4
|
+
describe Snmpjr::TargetV3 do
|
5
|
+
|
6
|
+
describe '#create' do
|
7
|
+
let(:configuration) do
|
8
|
+
config = Snmpjr::ConfigurationV3.new
|
9
|
+
config.host = '127.0.0.1'
|
10
|
+
config.port = 161
|
11
|
+
config.retries = 2
|
12
|
+
config.context = '80004fb805636c6f75644dab22cc'
|
13
|
+
config.user = 'username'
|
14
|
+
config.authentication 'SHA', 'authkey1'
|
15
|
+
config.privacy 'DES', 'privkey1'
|
16
|
+
config.timeout = 50
|
17
|
+
config
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'returns the SNMP4J user target' do
|
21
|
+
expect(subject.create(configuration)).to be_a(Snmpjr::Wrappers::UserTarget)
|
22
|
+
end
|
23
|
+
|
24
|
+
let(:target) { subject.create(configuration) }
|
25
|
+
|
26
|
+
specify 'the user target has the correct host and port' do
|
27
|
+
expect(target.address.to_s).to eq('127.0.0.1/161')
|
28
|
+
end
|
29
|
+
|
30
|
+
specify 'the user target has the correct retries' do
|
31
|
+
expect(target.retries).to eq 2
|
32
|
+
end
|
33
|
+
|
34
|
+
specify 'the user target has the correct timeout' do
|
35
|
+
expect(target.timeout).to eq 50
|
36
|
+
end
|
37
|
+
|
38
|
+
specify 'the user was created with the correct user' do
|
39
|
+
expect(target.security_name.to_s).to eq 'username'
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'sets the snmp version to 3' do
|
43
|
+
expect(target.version).to eq Snmpjr::Version::V3
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'sets the security level' do
|
47
|
+
expect(target.security_level).to eq Snmpjr::ConfigurationV3::SecurityLevels::AuthPriv
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/spec/snmpjr/walker_spec.rb
CHANGED
@@ -8,7 +8,7 @@ describe Snmpjr::Walker do
|
|
8
8
|
let(:pdu_factory) { double Snmpjr::Wrappers::Util::DefaultPDUFactory }
|
9
9
|
let(:tree_util) { double Snmpjr::Wrappers::Util::TreeUtils }
|
10
10
|
|
11
|
-
subject { described_class.new(target: target) }
|
11
|
+
subject { described_class.new(target: target, session: session) }
|
12
12
|
|
13
13
|
let(:vb1) { Snmpjr::Wrappers::SMI::VariableBinding.new(
|
14
14
|
Snmpjr::Wrappers::SMI::OID.new('1.3.6'),
|
@@ -25,7 +25,6 @@ describe Snmpjr::Walker do
|
|
25
25
|
|
26
26
|
before do
|
27
27
|
allow(Snmpjr::Wrappers::Util::DefaultPDUFactory).to receive(:new).and_return pdu_factory
|
28
|
-
allow(Snmpjr::Session).to receive(:new).and_return session
|
29
28
|
|
30
29
|
allow(Snmpjr::Wrappers::Util::TreeUtils).to receive(:new).with(session.snmp, pdu_factory).and_return tree_util
|
31
30
|
allow(tree_util).to receive(:getSubtree).with(target, oid).and_return example_event_responses
|
data/spec/snmpjr_spec.rb
CHANGED
@@ -1,47 +1,88 @@
|
|
1
1
|
require production_code
|
2
2
|
|
3
3
|
describe Snmpjr do
|
4
|
-
let(:target) { double
|
5
|
-
let(:
|
6
|
-
let(:
|
4
|
+
let(:target) { double :target }
|
5
|
+
let(:configuration) { double :configuration }
|
6
|
+
let(:created_target) { double :created_target }
|
7
|
+
let(:created_pdu) { double :created_pdu }
|
8
|
+
let(:created_session) { double :created_session }
|
7
9
|
|
8
|
-
subject { described_class.new(
|
10
|
+
subject { described_class.new(Snmpjr::Version::V2C) }
|
9
11
|
|
10
12
|
before do
|
11
|
-
allow(
|
12
|
-
allow(
|
13
|
+
allow(configuration).to receive(:create_target).and_return created_target
|
14
|
+
allow(configuration).to receive(:create_pdu).and_return created_pdu
|
15
|
+
allow(configuration).to receive(:create_session).and_return created_session
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#configuration" do
|
19
|
+
context "when we are working with SNMP V2" do
|
20
|
+
it "returns a Configuration for SNMP V2" do
|
21
|
+
expect(described_class.new(Snmpjr::Version::V2C).configuration).to be_a(Snmpjr::ConfigurationV2C)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "when we are working with SNMP V3" do
|
26
|
+
it "returns a Configuration for SNMP V3" do
|
27
|
+
expect(described_class.new(Snmpjr::Version::V3).configuration).to be_a(Snmpjr::ConfigurationV3)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "#configure" do
|
33
|
+
context "when we are working with SNMP V2" do
|
34
|
+
it "yields an Snmpjr V2C configuration" do
|
35
|
+
expect {|block|
|
36
|
+
Snmpjr.new(Snmpjr::Version::V2C).configure(&block)
|
37
|
+
}.to yield_control
|
38
|
+
Snmpjr.new(Snmpjr::Version::V2C).configure do |config|
|
39
|
+
expect(config).to be_a(Snmpjr::ConfigurationV2C)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "when we are working with SNMP V3" do
|
45
|
+
it "yields an Snmpjr V3 configuration" do
|
46
|
+
expect {|block|
|
47
|
+
Snmpjr.new(Snmpjr::Version::V3).configure(&block)
|
48
|
+
}.to yield_control
|
49
|
+
Snmpjr.new(Snmpjr::Version::V3).configure do |config|
|
50
|
+
expect(config).to be_a(Snmpjr::ConfigurationV3)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'returns an instance of Snmpjr' do
|
56
|
+
result = Snmpjr.new(Snmpjr::Version::V3).configure do |config|
|
57
|
+
config.user = 'some_user'
|
58
|
+
end
|
59
|
+
expect(result).to be_a(Snmpjr)
|
60
|
+
end
|
13
61
|
end
|
14
62
|
|
15
63
|
describe "#get" do
|
16
64
|
let(:getter) { double Snmpjr::Getter }
|
17
65
|
|
18
66
|
before do
|
19
|
-
allow(Snmpjr::
|
67
|
+
allow(Snmpjr::ConfigurationV2C).to receive(:new).and_return configuration
|
68
|
+
allow(Snmpjr::ConfigurationV3).to receive(:new).and_return configuration
|
69
|
+
allow(Snmpjr::Getter).to receive(:new).with(session: created_session, target: created_target, pdu: created_pdu, config: configuration).and_return getter
|
70
|
+
allow(getter).to receive(:get)
|
20
71
|
end
|
21
72
|
|
22
|
-
subject { described_class.new(agent_details.merge({max_oids_per_request: 20})) }
|
23
|
-
|
24
73
|
context 'when passed a single oid' do
|
25
|
-
it '
|
26
|
-
expect(getter).to receive(:get).with('1.2.3.4.5.6')
|
74
|
+
it 'coerces the single oid to an array and gives it to the getter' do
|
75
|
+
expect(getter).to receive(:get).with(['1.2.3.4.5.6'])
|
27
76
|
subject.get '1.2.3.4.5.6'
|
28
77
|
end
|
29
78
|
end
|
30
79
|
|
31
80
|
context 'when passed multiple oids' do
|
32
|
-
it 'performs a get
|
33
|
-
expect(getter).to receive(:
|
81
|
+
it 'performs a get with the getter' do
|
82
|
+
expect(getter).to receive(:get).with(['1.2.3.4.5.6', '6.5.4.3.2.1'])
|
34
83
|
subject.get ['1.2.3.4.5.6', '6.5.4.3.2.1']
|
35
84
|
end
|
36
85
|
end
|
37
|
-
|
38
|
-
context 'when an invalid argument was passed in' do
|
39
|
-
it 'raises an ArgumentError' do
|
40
|
-
expect {
|
41
|
-
subject.get({'oid_value' => '1.3.4.5.6'})
|
42
|
-
}.to raise_error ArgumentError
|
43
|
-
end
|
44
|
-
end
|
45
86
|
end
|
46
87
|
|
47
88
|
describe '#walk' do
|
@@ -49,23 +90,15 @@ describe Snmpjr do
|
|
49
90
|
let(:oid) { double :oid }
|
50
91
|
|
51
92
|
before do
|
52
|
-
allow(Snmpjr::
|
93
|
+
allow(Snmpjr::ConfigurationV2C).to receive(:new).and_return configuration
|
94
|
+
allow(Snmpjr::Walker).to receive(:new).with(session: created_session, target: created_target, pdu: created_pdu).and_return walker
|
53
95
|
allow(Snmpjr::Wrappers::SMI::OID).to receive(:new).with('1.3.6.1.1').and_return oid
|
54
|
-
|
55
|
-
context 'when a string is passed' do
|
56
|
-
it 'performs a synchronous walk' do
|
57
|
-
expect(walker).to receive(:walk).with(oid)
|
58
|
-
subject.walk '1.3.6.1.1'
|
59
|
-
end
|
96
|
+
allow(walker).to receive(:walk)
|
60
97
|
end
|
61
98
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
subject.walk({'oid_value' => '1.3.4.5.6'})
|
66
|
-
}.to raise_error ArgumentError
|
67
|
-
end
|
99
|
+
it 'performs a synchronous walk' do
|
100
|
+
expect(walker).to receive(:walk).with(oid)
|
101
|
+
subject.walk '1.3.6.1.1'
|
68
102
|
end
|
69
|
-
|
70
103
|
end
|
71
104
|
end
|