Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
inventory-provider
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
geant-swd
dashboardv3
inventory-provider
Commits
d4d36d42
Commit
d4d36d42
authored
4 years ago
by
Erik Reid
Browse files
Options
Downloads
Patches
Plain Diff
added test of snmp.get_peer_state_info
parent
e8d478df
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
inventory_provider/snmp.py
+17
-2
17 additions, 2 deletions
inventory_provider/snmp.py
test/test_snmp_handling.py
+36
-3
36 additions, 3 deletions
test/test_snmp_handling.py
with
53 additions
and
5 deletions
inventory_provider/snmp.py
+
17
−
2
View file @
d4d36d42
...
@@ -46,6 +46,17 @@ def _v6address_oid2str(dotted_decimal):
...
@@ -46,6 +46,17 @@ def _v6address_oid2str(dotted_decimal):
return
"
:
"
.
join
(
hex_params
)
return
"
:
"
.
join
(
hex_params
)
def
_canonify_oid
(
oid
):
"""
Our output oid
'
s always begin with
'
.
'
.
:param oid: a thing that stringifies to a dotted oid
:return: a string like
'
.#.#.#...#
'
"""
oid
=
str
(
oid
)
return
oid
if
oid
.
startswith
(
'
.
'
)
else
f
'
.
{
oid
}
'
def
walk
(
agent_hostname
,
community
,
base_oid
):
# pragma: no cover
def
walk
(
agent_hostname
,
community
,
base_oid
):
# pragma: no cover
"""
"""
https://stackoverflow.com/a/45001921
https://stackoverflow.com/a/45001921
...
@@ -107,8 +118,10 @@ def walk(agent_hostname, community, base_oid): # pragma: no cover
...
@@ -107,8 +118,10 @@ def walk(agent_hostname, community, base_oid): # pragma: no cover
# .resolveWithMib(mibViewController)
# .resolveWithMib(mibViewController)
# for x in varBinds]
# for x in varBinds]
for
oid
,
val
in
varBinds
:
for
oid
,
val
in
varBinds
:
result
=
{
"
oid
"
:
"
.
"
+
str
(
oid
),
"
value
"
:
_cast_snmp_value
(
val
)}
result
=
{
# result = {"oid": "." + str(oid), "value": val.prettyPrint()}
"
oid
"
:
_canonify_oid
(
oid
),
"
value
"
:
_cast_snmp_value
(
val
)
}
logger
.
debug
(
result
)
logger
.
debug
(
result
)
yield
result
yield
result
...
@@ -136,8 +149,10 @@ def _v4str(l):
...
@@ -136,8 +149,10 @@ def _v4str(l):
def
get_peer_state_info
(
hostname
,
community
):
def
get_peer_state_info
(
hostname
,
community
):
oid_prefix
=
f
'
.
{
JNX_BGP_M2_PEER_STATE
}
.
'
oid_prefix
=
f
'
.
{
JNX_BGP_M2_PEER_STATE
}
.
'
for
ifc
in
walk
(
hostname
,
community
,
JNX_BGP_M2_PEER_STATE
):
for
ifc
in
walk
(
hostname
,
community
,
JNX_BGP_M2_PEER_STATE
):
assert
ifc
[
'
oid
'
].
startswith
(
oid_prefix
),
\
assert
ifc
[
'
oid
'
].
startswith
(
oid_prefix
),
\
f
'
{
ifc
[
"
oid
"
]
}
:
{
JNX_BGP_M2_PEER_STATE
}
'
f
'
{
ifc
[
"
oid
"
]
}
:
{
JNX_BGP_M2_PEER_STATE
}
'
rest
=
ifc
[
'
oid
'
][
len
(
oid_prefix
):]
rest
=
ifc
[
'
oid
'
][
len
(
oid_prefix
):]
splits
=
rest
.
split
(
'
.
'
)
splits
=
rest
.
split
(
'
.
'
)
splits
.
pop
(
0
)
# no idea what this integer is ...
splits
.
pop
(
0
)
# no idea what this integer is ...
...
...
This diff is collapsed.
Click to expand it.
test/test_snmp_handling.py
+
36
−
3
View file @
d4d36d42
...
@@ -42,7 +42,7 @@ def _gen_mocked_nextCmd(mocked_walk_data):
...
@@ -42,7 +42,7 @@ def _gen_mocked_nextCmd(mocked_walk_data):
return
_mocked_nextCmd
return
_mocked_nextCmd
def
test_snmp_interfaces
(
mocker
,
data_config
):
def
test_snmp_interfaces
(
mocker
):
expected_result_schema
=
{
expected_result_schema
=
{
"
$schema
"
:
"
http://json-schema.org/draft-07/schema#
"
,
"
$schema
"
:
"
http://json-schema.org/draft-07/schema#
"
,
...
@@ -58,11 +58,44 @@ def test_snmp_interfaces(mocker, data_config):
...
@@ -58,11 +58,44 @@ def test_snmp_interfaces(mocker, data_config):
}
}
}
}
_mocked_nextCmd
=
_gen_mocked_nextCmd
(
interfaces_walk_responses
())
test_data
=
list
(
interfaces_walk_responses
())
assert
test_data
# sanity
_mocked_nextCmd
=
_gen_mocked_nextCmd
(
test_data
)
mocker
.
patch
(
'
inventory_provider.snmp.nextCmd
'
,
_mocked_nextCmd
)
mocker
.
patch
(
'
inventory_provider.snmp.nextCmd
'
,
_mocked_nextCmd
)
mocker
.
patch
(
'
inventory_provider.snmp.UdpTransportTarget
'
,
lambda
x
:
None
)
mocker
.
patch
(
'
inventory_provider.snmp.UdpTransportTarget
'
,
lambda
x
:
None
)
interfaces
=
list
(
snmp
.
get_router_snmp_indexes
(
'
ignored
'
,
'
ignored
'
))
interfaces
=
list
(
snmp
.
get_router_snmp_indexes
(
'
ignored
'
,
'
ignored
'
))
jsonschema
.
validate
(
interfaces
,
expected_result_schema
)
jsonschema
.
validate
(
interfaces
,
expected_result_schema
)
assert
interfaces
,
"
interface list isn
'
t empty
"
assert
len
(
interfaces
)
==
len
(
test_data
)
def
test_peer_info
(
mocker
):
expected_result_schema
=
{
"
$schema
"
:
"
http://json-schema.org/draft-07/schema#
"
,
"
type
"
:
"
array
"
,
"
items
"
:
{
"
type
"
:
"
object
"
,
"
properties
"
:
{
"
local
"
:
{
"
type
"
:
"
string
"
},
"
remote
"
:
{
"
type
"
:
"
string
"
},
"
oid
"
:
{
"
type
"
:
"
string
"
}
},
"
required
"
:
[
"
local
"
,
"
remote
"
,
"
oid
"
],
"
additionalProperties
"
:
False
}
}
test_data
=
list
(
peering_walk_responses
())
assert
test_data
# sanity
_mocked_nextCmd
=
_gen_mocked_nextCmd
(
test_data
)
mocker
.
patch
(
'
inventory_provider.snmp.nextCmd
'
,
_mocked_nextCmd
)
mocker
.
patch
(
'
inventory_provider.snmp.UdpTransportTarget
'
,
lambda
x
:
None
)
peerings
=
list
(
snmp
.
get_peer_state_info
(
'
ignored
'
,
'
ignored
'
))
jsonschema
.
validate
(
peerings
,
expected_result_schema
)
assert
len
(
peerings
)
==
len
(
test_data
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment