Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
Resource Manager
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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
GÉANT Orchestration and Automation Team
GAP
Resource Manager
Commits
1ed30709
Commit
1ed30709
authored
2 years ago
by
Erik Reid
Browse files
Options
Downloads
Patches
Plain Diff
fixed mocking with test data
parent
0b4cd3df
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
test/test_parse_router.py
+65
-17
65 additions, 17 deletions
test/test_parse_router.py
with
65 additions
and
17 deletions
test/test_parse_router.py
+
65
−
17
View file @
1ed30709
import
json
import
os
from
unittest.mock
import
patch
,
MagicMock
from
jsonschema
import
validate
,
ValidationError
from
resource_management.hardware.router
import
load_line_cards
,
LINE_CARDS_LIST_SCHEMA
# This is not yet working ... try to find the right mocks that will
# allow us to run all lines of code in router.py
#
# another possible approach: try to mock the netconf responses and use
# the code from https://github.com/GIC-de/Juniper-PyEZ-Unit-Testing
@patch
(
'
resource_management.hardware.router.PhyPortTable
'
)
@patch
(
'
resource_management.hardware.router.FpcHwTable
'
)
@patch
(
'
resource_management.hardware.router.Device
'
)
def
test_load_line_cards
(
Device
,
FpcHwTable
,
PhyPortTable
):
PhyPortTable
.
return_value
=
MagicMock
()
PhyPortTable
.
return_value
.
return_value
=
'
aaaa
'
fpcs
=
load_line_cards
(
hostname
=
'
bogus
'
,
username
=
'
bogus
'
,
key_filename
=
'
no file
'
)
from
jsonschema
import
validate
import
pytest
from
resource_management.hardware.router
\
import
load_line_cards
,
LINE_CARDS_LIST_SCHEMA
# another possible approach: try to mock the netconf responses
# directly and use the code from:
# https://github.com/GIC-de/Juniper-PyEZ-Unit-Testing
DATA_DIR
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'
data
'
)
def
_load_json_data
(
filename
):
with
open
(
os
.
path
.
join
(
DATA_DIR
,
filename
))
as
f
:
return
json
.
loads
(
f
.
read
())
class
_namespace
(
object
):
pass
@pytest.fixture
def
mocked_pyez_vmx
():
def
_table_data_to_iter
(
json_data_filename
):
# simple simulation of how pyez renders
# a table as an iterable
#
# datafile should be something formatted as
# if (or actually captured) from a pyez table
# object's to_json() method
for
name
,
data
in
_load_json_data
(
json_data_filename
).
items
():
o
=
_namespace
()
o
.
name
=
o
.
key
=
name
for
k
,
v
in
data
.
items
():
setattr
(
o
,
k
,
v
)
yield
o
m
=
'
resource_management.hardware.router
'
with
patch
(
f
'
{
m
}
.PhyPortTable
'
)
as
PhyPortTable
:
with
patch
(
f
'
{
m
}
.FpcHwTable
'
)
as
FpcHwTable
:
with
patch
(
f
'
{
m
}
.Device
'
)
as
Device
:
phy_port_table
=
MagicMock
()
phy_port_table
.
__iter__
.
return_value
\
=
_table_data_to_iter
(
'
ports.json
'
)
PhyPortTable
.
return_value
=
phy_port_table
fpc_hw_table
=
MagicMock
()
fpc_hw_table
.
__iter__
.
return_value
\
=
_table_data_to_iter
(
'
line_cards.json
'
)
FpcHwTable
.
return_value
=
fpc_hw_table
yield
# stay in this block until caller context exits
def
test_load_line_cards
(
mocked_pyez_vmx
):
fpcs
=
load_line_cards
(
hostname
=
'
bogus
'
,
username
=
'
bogus
'
,
key_filename
=
'
no file
'
)
fpcs
=
list
(
fpcs
)
validate
(
fpcs
,
LINE_CARDS_LIST_SCHEMA
)
assert
len
(
fpcs
)
==
1
assert
len
(
fpcs
)
>
0
# because we know the test data is non-empty
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