Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
GÉANT Service Orchestrator
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
GÉANT Orchestration and Automation Team
GAP
GÉANT Service Orchestrator
Commits
edd77610
Commit
edd77610
authored
1 year ago
by
Hakan Calim
Browse files
Options
Downloads
Patches
Plain Diff
NAT-286: Changed test to use mock of pynetbox with patch
parent
46d70aa4
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!89
Feature/nat 286 create unit tests for netbox client
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
test/services/test_netbox.py
+43
-64
43 additions, 64 deletions
test/services/test_netbox.py
with
43 additions
and
64 deletions
test/services/test_netbox.py
+
43
−
64
View file @
edd77610
"""
Unit tests for testing the netbox client
"""
import
uuid
import
os
import
json
from
os
import
PathLike
from
unittest.mock
import
patch
,
Mock
import
responses
from
unittest.mock
import
patch
from
gso.services.netbox_client
import
NetBoxClient
from
gso.products.product_blocks.site
import
SiteTier
BASE_URL
=
"
https://127.0.0.1:8000
"
def
_create_device
(
id
:
int
,
name
:
str
)
->
dict
:
device
=
{
"
id
"
:
id
,
"
name
"
:
name
}
return
device
def
_create_interface
(
id
:
int
,
name
:
str
)
->
dict
:
return
{
"
id
"
:
id
,
"
name
"
:
name
}
def
_load_fixture
(
filename
):
current_directory
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
# Build the full path to the fixture file
fixture_path
=
os
.
path
.
join
(
current_directory
,
'
fixtures
'
,
filename
)
# Load and return the JSON data
with
open
(
fixture_path
,
'
r
'
)
as
f
:
return
json
.
load
(
f
)
def
_create_list_interfaces
(
num_iface
:
int
)
->
list
:
list_iface
=
list
()
for
i
in
range
(
num_iface
):
name
=
f
"
et-0/0/
{
i
}
"
id
=
i
+
1
iface
=
_create_interface
(
id
,
name
)
list_iface
.
append
(
iface
)
return
list_iface
def
_get_mock_device
():
return
_load_fixture
(
"
device.json
"
)
@responses.activate
def
test_create_device
(
data_config_filename
:
PathLike
):
device_name
=
"
mx1.lab.office.geant.net
"
device_id
=
10
# device_manufacturer = "Nokia"
device_site
=
"
GEANT
"
# device_role = "Router"
# Create a
mock
device
device
=
_create_devic
e
(
device_
id
,
device_name
)
def
_get_
mock
_
device
_type
():
return
_load_fixtur
e
(
"
device_
type.json
"
)
# Define response
endpoint
=
f
"
{
BASE_URL
}
/api/dcim/devices/?limit=0
"
responses
.
add
(
responses
.
POST
,
endpoint
,
json
=
device
,
status
=
200
)
def
_get_mock_device_role
():
return
_load_fixture
(
"
device_role.json
"
)
result
=
NetBoxClient
().
create_device
(
device_name
,
device_site
)
assert
result
is
not
None
def
_get_mock_site
():
return
_load_fixture
(
"
site.json
"
)
@responses.activate
def
test_get_all_interfaces
(
data_config_filename
:
PathLike
):
num_iface
=
3
list_iface
=
_create_list_interfaces
(
num_iface
)
endpoint
=
f
"
{
BASE_URL
}
/api/dcim/devices/?limit=0
"
responses
.
add
(
responses
.
GET
,
endpoint
,
json
=
list_iface
,
status
=
200
)
def
_get_mock_device_bay
():
return
_load_fixture
(
"
device_bay.json
"
)
result
=
NetBoxClient
().
get_all_devices
()
def
test_create_device
(
data_config_filename
:
PathLike
):
device_name
=
"
mx1.lab.geant.net
"
site_tier
=
SiteTier
.
TIER1
mock_device
=
_get_mock_device
()
mock_device
[
"
name
"
]
=
device_name
mock_device_role
=
_get_mock_device_role
()
mock_device_type
=
_get_mock_device_type
()
mock_site
=
_get_mock_site
()
mock_device_bay
=
_get_mock_device_bay
()
# Define mock calls
with
patch
(
'
gso.services.netbox_client.pynetbox.api
'
)
as
mock_api
:
mock_api
.
return_value
.
dcim
.
device_types
.
get
.
return_value
=
mock_device_type
mock_api
.
return_value
.
dcim
.
device_roles
.
get
.
return_value
=
mock_device_role
mock_api
.
return_value
.
dcim
.
sites
.
get
.
return_value
=
mock_site
mock_api
.
return_value
.
dcim
.
devices
.
create
.
return_value
=
mock_device
mock_api
.
return_value
.
dcim
.
module_bays
.
filter
.
return_value
=
mock_device_bay
mock_api
.
return_value
.
dcim
.
module_types
.
get
.
return_value
=
{
"
id
"
:
1
,
"
name
"
:
"
eth-0/0/1
"
}
mock_api
.
return_value
.
dcim
.
module_types
.
create
.
return_value
=
{
"
id
"
:
1
,
"
name
"
:
"
eth-0/0/1
"
}
result
=
NetBoxClient
().
create_device
(
device_name
,
site_tier
)
assert
result
is
not
None
assert
len
(
result
)
==
num_iface
@patch
(
"
gso.services.netbox_client.Router.from_subscription
"
)
@patch
(
"
gso.services.netbox_client.pynetbox.api
"
)
def
test_get_available_lags
(
mock_api
,
mock_from_subscription
,
data_config_filename
:
PathLike
):
router_id
=
uuid
.
uuid4
()
feasible_lags
=
[
f
"
LAG-
{
i
}
"
for
i
in
range
(
1
,
10
)]
# Mock the pynetbox API instance
mock_netbox
=
mock_api
.
return_value
mock_filter
=
mock_netbox
.
dcim
.
interfaces
.
filter
mock_filter
.
return_value
=
[{
"
name
"
:
f
"
LAG-
{
i
}
"
,
"
type
"
:
"
lag
"
}
for
i
in
range
(
1
,
4
)]
# Mock the Router.from_subscription method
mock_subscription
=
mock_from_subscription
.
return_value
mock_router
=
mock_subscription
.
router
mock_router
.
router_fqdn
=
"
test_router
"
netbox_client
=
NetBoxClient
()
result
=
netbox_client
.
get_available_lags
(
router_id
)
# Check the result of the function
assert
result
==
[
lag
for
lag
in
feasible_lags
if
lag
not
in
[
f
"
LAG-
{
i
}
"
for
i
in
range
(
1
,
4
)]]
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