Skip to content

Commit

Permalink
Merge pull request #3 from danlesser/asn1_data_types
Browse files Browse the repository at this point in the history
Add asn1 data type to response
  • Loading branch information
zenonas committed Jan 14, 2015
2 parents 0d15b70 + 85acd82 commit 927e1c6
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 39 deletions.
9 changes: 7 additions & 2 deletions lib/snmpjr/response.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
class Snmpjr
class Response
attr_reader :error, :oid
attr_reader :error, :oid, :type

def initialize response = {}
@error = response[:error] || ''
@value = response[:value] || ''
@oid = response[:oid] || ''
@type = response[:type] || ''
end

def error?
Expand All @@ -16,13 +17,17 @@ def error?
end
end

def to_h
{ oid: @oid, value: @value, type: @type }
end

def to_s
@value
end

def ==(other)
return false unless other.instance_of?(self.class)
@error == other.error && to_s == other.to_s && @oid == other.oid
@error == other.error && to_h == other.to_h
end
end
end
2 changes: 1 addition & 1 deletion lib/snmpjr/session_v2c.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def construct_response variable_binding
if variable_binding.is_exception
Snmpjr::Response.new(oid: variable_binding.oid.to_s, error: variable_binding.variable.to_s)
else
Snmpjr::Response.new(oid: variable_binding.oid.to_s, value: variable_binding.variable.to_s)
Snmpjr::Response.new(oid: variable_binding.oid.to_s, value: variable_binding.variable.to_s, type: variable_binding.variable.syntax_string)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/snmpjr/walker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def walk oid

def extract_variable_bindings variable_bindings
variable_bindings.flat_map {|vb|
Snmpjr::Response.new(oid: vb.oid.to_s, value: vb.variable.to_s)
Snmpjr::Response.new(oid: vb.oid.to_s, value: vb.variable.to_s, type: vb.variable.syntax_string)
}
end

Expand Down
11 changes: 6 additions & 5 deletions spec/integration/snmp_v2c/snmpjr_get_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@
end

it 'can perform a simple synchronous get request on an snmp agent' do
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')
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', type: 'OCTET STRING')
end

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'),
Snmpjr::Response.new(oid: '1.3.6.1.2.1.1.5.0', value: 'zeus.snmplabs.com')] }
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', type: 'OCTET STRING'),
Snmpjr::Response.new(oid: '1.3.6.1.2.1.1.5.0', value: 'zeus.snmplabs.com', type: 'OCTET STRING'),
Snmpjr::Response.new(oid: '1.3.6.1.2.1.1.7.0', value: '72', type: 'Integer32')] }
it 'can perform a series of gets if passed an array of oids' do
expect(subject.get ['1.3.6.1.2.1.1.1.0', '1.3.6.1.2.1.1.5.0']).to eq expected
expect(subject.get ['1.3.6.1.2.1.1.1.0', '1.3.6.1.2.1.1.5.0', '1.3.6.1.2.1.1.7.0']).to eq expected
end

context "when an invalid oid is requested" do

let(:expected) { [Snmpjr::Response.new(oid: '1.3.6.1.2.1.1.5', error: 'noSuchInstance'),
Snmpjr::Response.new(oid: '1.3.6.1.2.1.1.5.0', value: 'zeus.snmplabs.com')] }
Snmpjr::Response.new(oid: '1.3.6.1.2.1.1.5.0', value: 'zeus.snmplabs.com', type: 'OCTET STRING')] }

it 'returns an error' do
expect(subject.get ['1.3.6.1.2.1.1.5', '1.3.6.1.2.1.1.5.0']).to eq expected
Expand Down
5 changes: 3 additions & 2 deletions spec/integration/snmp_v2c/snmpjr_walk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
it 'can perform a simple synchronous walk request on an snmp agent' do
response = subject.walk '1.3.6.1.2.1.1'
expect(response.count).to eq 11
expect(response.first.to_s).to eq 'SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m'
expect(response.last.to_s).to match(/^\d+\:\d+:\d+\.\d+$/)
expect(response.first.to_h).to eq(
{ oid: '1.3.6.1.2.1.1.1.0', value: 'SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m', type: 'OCTET STRING' }
)
end

context "when a non existent subtree is walked" do
Expand Down
11 changes: 6 additions & 5 deletions spec/integration/snmp_v3/snmpjr_get_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
end

it 'can perform a simple synchronous get request on an snmp agent' do
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')
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', type: 'OCTET STRING')
end

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'),
Snmpjr::Response.new(oid: '1.3.6.1.2.1.1.5.0', value: 'zeus.snmplabs.com')] }
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', type: 'OCTET STRING'),
Snmpjr::Response.new(oid: '1.3.6.1.2.1.1.5.0', value: 'zeus.snmplabs.com', type: 'OCTET STRING'),
Snmpjr::Response.new(oid: '1.3.6.1.2.1.1.7.0', value: '72', type: 'Integer32')] }
it 'can perform a series of gets if passed an array of oids' do
expect(subject.get ['1.3.6.1.2.1.1.1.0', '1.3.6.1.2.1.1.5.0']).to eq expected
expect(subject.get ['1.3.6.1.2.1.1.1.0', '1.3.6.1.2.1.1.5.0', '1.3.6.1.2.1.1.7.0']).to eq expected
end

context "when an invalid oid is requested" do
Expand All @@ -38,7 +39,7 @@
end

let(:expected) { [Snmpjr::Response.new(oid: '1.3.6.1.2.1.1.5', error: 'noSuchInstance'),
Snmpjr::Response.new(oid: '1.3.6.1.2.1.1.5.0', value: 'zeus.snmplabs.com')] }
Snmpjr::Response.new(oid: '1.3.6.1.2.1.1.5.0', value: 'zeus.snmplabs.com', type: 'OCTET STRING')] }

it 'returns an error' do
expect(subject.get ['1.3.6.1.2.1.1.5', '1.3.6.1.2.1.1.5.0']).to eq expected
Expand Down
5 changes: 3 additions & 2 deletions spec/integration/snmp_v3/snmpjr_walk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
it 'can perform a simple synchronous walk request on an snmp agent' do
response = subject.walk '1.3.6.1.2.1.1'
expect(response.count).to eq 11
expect(response.first.to_s).to eq 'SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m'
expect(response.last.to_s).to match(/^\d+\:\d+:\d+\.\d+$/)
expect(response.first.to_h).to eq(
{ oid: '1.3.6.1.2.1.1.1.0', value: 'SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m', type: 'OCTET STRING' }
)
end

context "when a non existent subtree is walked" do
Expand Down
33 changes: 14 additions & 19 deletions spec/snmpjr/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
describe '.new' do
context 'when initialized with a value' do
it 'assigns that value' do
response = described_class.new(oid: 'some oid', value: 'Some value')
expect(response.to_s).to eq 'Some value'
response = described_class.new(oid: 'some oid', value: 'Some value', type: 'Some type')
expect(response.to_h).to eq({ oid: 'some oid', value: 'Some value', type: 'Some type' })
end

it 'sets the error to an empty string' do
response = described_class.new(oid: 'some oid', value: 'Some value')
response = described_class.new(oid: 'some oid', value: 'Some value', type: 'Some type')
expect(response.error).to eq ''
end
end
Expand Down Expand Up @@ -42,43 +42,38 @@
end

it 'returns false if there isnt an error' do
response = described_class.new(oid: 'some oid', value: 'Some value')
response = described_class.new(oid: 'some oid', value: 'Some value', type: 'Some type')
expect(response.error?).to be_falsey
end
end

describe '#==' do
context 'when the objects are equal' do
let(:other) { Snmpjr::Response.new(oid: 'some oid', value: 'some value') }
let(:other) { Snmpjr::Response.new(oid: 'some oid', value: 'some value', type: 'Some type') }
it 'returns true' do
expect(described_class.new(oid: 'some oid', value: 'some value')).to eq other
expect(described_class.new(oid: 'some oid', value: 'some value', type: 'Some type')).to eq other
end
end

context 'when the objects are not equal' do
let(:other) { Snmpjr::Response.new(oid: 'some oid', error: 'some value') }
it 'returns true' do
expect(described_class.new(oid: 'some oid', error: 'some error')).to_not eq other
end
end

context 'when the oids are different' do
let(:other) { Snmpjr::Response.new(oid: 'some oid', error: 'some error') }
it 'returns true' do
expect(described_class.new(oid: 'another oid', error: 'some error')).to_not eq other
context 'when the objects are different' do
context 'when the objects are equal' do
let(:other) { Snmpjr::Response.new(oid: 'some oid', error: 'some error') }
it 'returns false' do
expect(described_class.new(oid: 'some oid', error: 'another error')).to_not eq other
end
end
end

context 'when the objects are not of the same class' do
let(:other) { double :response }
before do
allow(other).to receive(:error).and_return ''
allow(other).to receive(:to_s).and_return 'some value'
allow(other).to receive(:to_h).and_return({ value: 'some value', type: 'Some type' })
allow(other).to receive(:oid).and_return 'some oid'
end

it 'returns false' do
expect(described_class.new(oid: 'some oid', value: 'some value')).to_not eq other
expect(described_class.new(oid: 'some oid', value: 'some value', type: 'Some type')).to_not eq other
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/snmpjr/session_v2c_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@

before do
allow(snmp_session).to receive(:send).and_return response
allow(vb1).to receive_message_chain('variable.syntax_string')
allow(vb1).to receive_message_chain('variable.to_s')
allow(vb1).to receive_message_chain('oid.to_s') { "1.2.3" }
allow(vb1).to receive(:is_exception)
allow(vb2).to receive_message_chain('variable.syntax_string')
allow(vb2).to receive_message_chain('variable.to_s')
allow(vb2).to receive_message_chain('oid.to_s') { "4.5.6" }
allow(vb2).to receive(:is_exception)
Expand Down
7 changes: 5 additions & 2 deletions spec/snmpjr/walker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
before do
allow(tree_util).to receive(:getSubtree).with(target, oid).and_raise Exception.new 'noAccess'
end

it 'raises a runtime error' do
expect{
subject.walk oid
Expand All @@ -49,6 +50,7 @@

context 'when a target times out' do
let(:tree_event_1) { double :tree_event_1 }

before do
allow(tree_event_1).to receive(:is_error?).and_return true
allow(tree_event_1).to receive(:error_message).and_return 'Request timed out.'
Expand All @@ -63,6 +65,7 @@

context 'when a random error occurs' do
let(:tree_event_1) { double :tree_event_1 }

before do
allow(tree_event_1).to receive(:is_error?).and_return true
allow(tree_event_1).to receive(:error_message).and_return 'noAccess'
Expand All @@ -85,8 +88,8 @@
end

it 'performs a synchronous walk' do
expect(subject.walk oid).to match_array [Snmpjr::Response.new(oid: vb1.oid.to_s, value: vb1.variable.to_s),
Snmpjr::Response.new(oid: vb2.oid.to_s, value: vb2.variable.to_s)]
expect(subject.walk oid).to match_array [Snmpjr::Response.new(oid: vb1.oid.to_s, value: vb1.variable.to_s, type: vb1.variable.syntax_string),
Snmpjr::Response.new(oid: vb2.oid.to_s, value: vb2.variable.to_s, type: vb1.variable.syntax_string)]
end

it 'closes the snmp session' do
Expand Down

0 comments on commit 927e1c6

Please sign in to comment.