Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
netbox-awx-plugin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
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
Code review analytics
Issue 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
ICS Control System Infrastructure
netbox-awx-plugin
Merge requests
!4
add more unit tests for models
INFRA-10663
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
add more unit tests for models
INFRA-10663
feature/INFRA-10376-unit-tests
into
main
Overview
0
Commits
1
Pipelines
1
Changes
1
Merged
Fahrudin Halilovic
requested to merge
feature/INFRA-10376-unit-tests
into
main
5 months ago
Overview
0
Commits
1
Pipelines
1
Changes
1
Expand
Closes
INFRA-10376
0
0
Merge request reports
Viewing commit
b1c946c6
Show latest version
1 file
+
275
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
b1c946c6
add more unit tests for models
INFRA-10663
· b1c946c6
Fahrudin Halilovic
authored
5 months ago
netbox_awx_plugin/tests/test_models.py
+
275
−
0
Options
# netbox_awx_plugin/tests/test_models.py
from
django.test
import
TestCase
from
unittest.mock
import
patch
,
Mock
from
netbox_awx_plugin.models
import
AWX
,
AWXInventory
from
django.urls
import
reverse
from
requests.compat
import
urljoin
class
AWXModelTestCase
(
TestCase
):
@@ -53,3 +55,276 @@ class AWXInventoryModelTestCase(TestCase):
url
=
self
.
inventory
.
get_absolute_url
()
expected_url
=
reverse
(
"
plugins:netbox_awx_plugin:awxinventory
"
,
args
=
[
self
.
inventory
.
pk
])
self
.
assertEqual
(
url
,
expected_url
)
@patch
(
'
netbox_awx_plugin.models.requests.get
'
)
def
test_get_host
(
self
,
mock_get
):
"""
Test the get_host method.
"""
mock_response
=
Mock
()
mock_response
.
json
.
return_value
=
{
"
count
"
:
1
,
"
results
"
:
[{
"
id
"
:
1
,
"
name
"
:
"
test-host
"
}]
}
mock_get
.
return_value
=
mock_response
host
=
self
.
inventory
.
get_host
(
"
test-host
"
)
self
.
assertEqual
(
host
[
"
name
"
],
"
test-host
"
)
expected_url
=
urljoin
(
self
.
awx_instance
.
url
,
f
"
/api/v2/inventories/
{
self
.
inventory
.
inventory_id
}
/hosts/
"
)
mock_get
.
assert_called_once_with
(
url
=
expected_url
,
params
=
{
"
name
"
:
"
test-host
"
},
headers
=
self
.
awx_instance
.
get_headers
(),
verify
=
False
,
)
@patch
(
'
netbox_awx_plugin.models.requests.get
'
)
def
test_get_host_no_results
(
self
,
mock_get
):
"""
Test get_host when no host is found.
"""
mock_response
=
Mock
()
mock_response
.
json
.
return_value
=
{
"
count
"
:
0
,
"
results
"
:
[]}
mock_get
.
return_value
=
mock_response
host
=
self
.
inventory
.
get_host
(
"
nonexistent-host
"
)
self
.
assertIsNone
(
host
)
@patch
(
'
netbox_awx_plugin.models.requests.post
'
)
def
test_create_host
(
self
,
mock_post
):
"""
Test the create_host method.
"""
mock_response
=
Mock
()
mock_post
.
return_value
=
mock_response
data
=
{
"
name
"
:
"
new-host
"
}
self
.
inventory
.
create_host
(
data
)
expected_url
=
urljoin
(
self
.
awx_instance
.
url
,
f
"
/api/v2/inventories/
{
self
.
inventory
.
inventory_id
}
/hosts/
"
)
mock_post
.
assert_called_once_with
(
url
=
expected_url
,
headers
=
self
.
awx_instance
.
get_headers
(),
json
=
data
,
verify
=
False
,
)
@patch
(
'
netbox_awx_plugin.models.requests.put
'
)
def
test_update_host
(
self
,
mock_put
):
"""
Test the update_host method.
"""
mock_response
=
Mock
()
mock_put
.
return_value
=
mock_response
data
=
{
"
name
"
:
"
updated-host
"
}
self
.
inventory
.
update_host
(
1
,
data
)
expected_url
=
urljoin
(
self
.
awx_instance
.
url
,
"
/api/v2/hosts/1/
"
)
mock_put
.
assert_called_once_with
(
url
=
expected_url
,
headers
=
self
.
awx_instance
.
get_headers
(),
json
=
data
,
verify
=
False
,
)
@patch
(
'
netbox_awx_plugin.models.requests.delete
'
)
@patch
(
'
netbox_awx_plugin.models.requests.get
'
)
def
test_delete_host
(
self
,
mock_get
,
mock_delete
):
"""
Test the delete_host method.
"""
# Mock the get_host response
mock_get_response
=
Mock
()
mock_get_response
.
json
.
return_value
=
{
"
count
"
:
1
,
"
results
"
:
[{
"
id
"
:
1
,
"
name
"
:
"
test-host
"
}]
}
mock_get
.
return_value
=
mock_get_response
# Mock the delete response
mock_delete_response
=
Mock
()
mock_delete
.
return_value
=
mock_delete_response
self
.
inventory
.
delete_host
(
"
test-host
"
)
# Check that get_host was called correctly
expected_get_url
=
urljoin
(
self
.
awx_instance
.
url
,
f
"
/api/v2/inventories/
{
self
.
inventory
.
inventory_id
}
/hosts/
"
)
mock_get
.
assert_called_once_with
(
url
=
expected_get_url
,
params
=
{
"
name
"
:
"
test-host
"
},
headers
=
self
.
awx_instance
.
get_headers
(),
verify
=
False
,
)
# Check that delete was called correctly
expected_delete_url
=
urljoin
(
self
.
awx_instance
.
url
,
"
/api/v2/hosts/1/
"
)
mock_delete
.
assert_called_once_with
(
url
=
expected_delete_url
,
headers
=
self
.
awx_instance
.
get_headers
(),
verify
=
False
,
)
@patch
(
'
netbox_awx_plugin.models.requests.get
'
)
def
test_get_group
(
self
,
mock_get
):
"""
Test the get_group method.
"""
mock_response
=
Mock
()
mock_response
.
json
.
return_value
=
{
"
results
"
:
[{
"
id
"
:
1
,
"
name
"
:
"
test-group
"
}]
}
mock_get
.
return_value
=
mock_response
group
=
self
.
inventory
.
get_group
(
"
test-group
"
)
self
.
assertEqual
(
group
[
"
name
"
],
"
test-group
"
)
expected_url
=
urljoin
(
self
.
awx_instance
.
url
,
"
/api/v2/groups/
"
)
mock_get
.
assert_called_once_with
(
url
=
expected_url
,
params
=
{
"
name
"
:
"
test-group
"
},
headers
=
self
.
awx_instance
.
get_headers
(),
verify
=
False
,
)
@patch
(
'
netbox_awx_plugin.models.requests.get
'
)
def
test_get_group_no_results
(
self
,
mock_get
):
"""
Test get_group when no group is found.
"""
mock_response
=
Mock
()
mock_response
.
json
.
return_value
=
{
"
results
"
:
[]}
mock_get
.
return_value
=
mock_response
group
=
self
.
inventory
.
get_group
(
"
nonexistent-group
"
)
self
.
assertIsNone
(
group
)
@patch
(
'
netbox_awx_plugin.models.requests.put
'
)
@patch
(
'
netbox_awx_plugin.models.requests.get
'
)
def
test_update_group
(
self
,
mock_get
,
mock_put
):
"""
Test the update_group method.
"""
# Mock the get_group response
mock_get_response
=
Mock
()
mock_get_response
.
json
.
return_value
=
{
"
results
"
:
[{
"
id
"
:
1
,
"
name
"
:
"
test-group
"
}]
}
mock_get
.
return_value
=
mock_get_response
# Mock the put response
mock_put_response
=
Mock
()
mock_put
.
return_value
=
mock_put_response
data
=
{
"
name
"
:
"
updated-group
"
}
self
.
inventory
.
update_group
(
"
test-group
"
,
data
)
# Check that get_group was called correctly
expected_get_url
=
urljoin
(
self
.
awx_instance
.
url
,
"
/api/v2/groups/
"
)
mock_get
.
assert_called_once_with
(
url
=
expected_get_url
,
params
=
{
"
name
"
:
"
test-group
"
},
headers
=
self
.
awx_instance
.
get_headers
(),
verify
=
False
,
)
# Check that put was called correctly
expected_put_url
=
urljoin
(
self
.
awx_instance
.
url
,
"
/api/v2/groups/1/
"
)
mock_put
.
assert_called_once_with
(
url
=
expected_put_url
,
headers
=
self
.
awx_instance
.
get_headers
(),
json
=
data
,
verify
=
False
,
)
@patch
(
'
netbox_awx_plugin.models.requests.post
'
)
def
test_create_group
(
self
,
mock_post
):
"""
Test the create_group method.
"""
mock_response
=
Mock
()
mock_post
.
return_value
=
mock_response
data
=
{
"
name
"
:
"
new-group
"
}
self
.
inventory
.
create_group
(
data
)
expected_url
=
urljoin
(
self
.
awx_instance
.
url
,
f
"
/api/v2/inventories/
{
self
.
inventory
.
inventory_id
}
/groups/
"
)
mock_post
.
assert_called_once_with
(
url
=
expected_url
,
headers
=
self
.
awx_instance
.
get_headers
(),
json
=
data
,
verify
=
False
,
)
@patch
(
'
netbox_awx_plugin.models.requests.delete
'
)
@patch
(
'
netbox_awx_plugin.models.requests.get
'
)
def
test_delete_group
(
self
,
mock_get
,
mock_delete
):
"""
Test the delete_group method.
"""
# Mock the get_group response
mock_get_response
=
Mock
()
mock_get_response
.
json
.
return_value
=
{
"
results
"
:
[{
"
id
"
:
1
,
"
name
"
:
"
test-group
"
}]
}
mock_get
.
return_value
=
mock_get_response
# Mock the delete response
mock_delete_response
=
Mock
()
mock_delete
.
return_value
=
mock_delete_response
self
.
inventory
.
delete_group
(
"
test-group
"
)
# Check that get_group was called correctly
expected_get_url
=
urljoin
(
self
.
awx_instance
.
url
,
"
/api/v2/groups/
"
)
mock_get
.
assert_called_once_with
(
url
=
expected_get_url
,
params
=
{
"
name
"
:
"
test-group
"
},
headers
=
self
.
awx_instance
.
get_headers
(),
verify
=
False
,
)
# Check that delete was called correctly
expected_delete_url
=
urljoin
(
self
.
awx_instance
.
url
,
"
/api/v2/groups/1/
"
)
mock_delete
.
assert_called_once_with
(
url
=
expected_delete_url
,
headers
=
self
.
awx_instance
.
get_headers
(),
verify
=
False
,
)
@patch
(
'
netbox_awx_plugin.models.requests.post
'
)
def
test_associate_host_group
(
self
,
mock_post
):
"""
Test the associate_host_group method.
"""
mock_response
=
Mock
()
mock_post
.
return_value
=
mock_response
self
.
inventory
.
associate_host_group
(
1
,
2
)
expected_url
=
urljoin
(
self
.
awx_instance
.
url
,
"
/api/v2/hosts/1/groups/
"
)
mock_post
.
assert_called_once_with
(
url
=
expected_url
,
headers
=
self
.
awx_instance
.
get_headers
(),
json
=
{
"
id
"
:
2
},
verify
=
False
,
)
@patch
(
'
netbox_awx_plugin.models.requests.post
'
)
def
test_disassociate_host_group
(
self
,
mock_post
):
"""
Test the disassociate_host_group method.
"""
mock_response
=
Mock
()
mock_post
.
return_value
=
mock_response
self
.
inventory
.
disassociate_host_group
(
1
,
2
)
expected_url
=
urljoin
(
self
.
awx_instance
.
url
,
"
/api/v2/hosts/1/groups/
"
)
mock_post
.
assert_called_once_with
(
url
=
expected_url
,
headers
=
self
.
awx_instance
.
get_headers
(),
json
=
{
"
id
"
:
2
,
"
disassociate
"
:
"
True
"
},
verify
=
False
,
)
@patch
(
'
netbox_awx_plugin.models.requests.get
'
)
def
test_get_host_groups
(
self
,
mock_get
):
"""
Test the get_host_groups method.
"""
mock_response
=
Mock
()
mock_response
.
json
.
return_value
=
{
"
results
"
:
[{
"
id
"
:
2
,
"
name
"
:
"
group1
"
},
{
"
id
"
:
3
,
"
name
"
:
"
group2
"
}]
}
mock_get
.
return_value
=
mock_response
groups
=
self
.
inventory
.
get_host_groups
(
1
)
self
.
assertEqual
(
len
(
groups
),
2
)
self
.
assertEqual
(
groups
[
0
][
"
name
"
],
"
group1
"
)
self
.
assertEqual
(
groups
[
1
][
"
name
"
],
"
group2
"
)
expected_url
=
urljoin
(
self
.
awx_instance
.
url
,
"
/api/v2/hosts/1/groups/
"
)
mock_get
.
assert_called_once_with
(
url
=
expected_url
,
headers
=
self
.
awx_instance
.
get_headers
(),
verify
=
False
,
)
Loading