Skip to content

xenopsd: Fix pytype warning on get_words: Name string is not defined #5921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions ocaml/xenopsd/scripts/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,28 +192,33 @@ def get_external_ids(self):
results["xs-network-uuid"] = self.json["extra_private_keys"]["network-uuid"]
results["attached-mac"] = self.get_mac()
return results

def get_locking_mode(self):
def get_words(value, separator):
if string.strip(value) == "":
return []
else:
return string.split(value, separator)
"""
Get the locking mode configuration for the VIF.

:returns dict: A dictionary containing the locking mode configuration with keys:
- mac: The MAC address
- locking_mode: The locking mode
- ipv4_allowed: List of IPv4 addresses allowed
- ipv6_allowed: List of IPv6 addresses allowed
"""
results = {
"mac": self.get_mac(),
"locking_mode": "",
"ipv4_allowed": [],
"ipv6_allowed": []
"ipv6_allowed": [],
}
if "locking_mode" in self.json:
if type(self.json["locking_mode"]) is list:
# Must be type=locked here
if isinstance(self.json["locking_mode"], list):
# Must be type=locked and have keys for allowed ipv4 and ipv6 addresses
results["locking_mode"] = self.json["locking_mode"][0].lower()
locked_params=self.json["locking_mode"][1]
locked_params = self.json["locking_mode"][1]
results["ipv4_allowed"] = locked_params["ipv4"]
results["ipv6_allowed"] = locked_params["ipv6"]
else:
results["locking_mode"] = self.json["locking_mode"].lower()
send_to_syslog("Got locking config: %s" % (repr(results)))
send_to_syslog("Got locking config: " + repr(results))
return results

class Interface:
Expand Down
78 changes: 78 additions & 0 deletions ocaml/xenopsd/scripts/test_common_class_vif.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""Test ocaml/xenopsd/scripts/common.VIF.get_locking_mode()"""

from unittest.mock import patch # to check the arguments passed to send_to_syslog()

import pytest # for pytest.parametrize to run the same test with different parameters

import common # Tested module


# Mock class to simulate the object containing the get_locking_mode method
class VifMockSubclass(common.VIF):
"""Mock class to simulate a VIF object containing the get_locking_mode method"""

def __init__(self, json): # pylint: disable=super-init-not-called
"""Do not call the parent constructor, it would open a file"""
self.json = json

def get_mac(self):
return "00:11:22:33:44:55" # Expected MAC address


@pytest.mark.parametrize(
# Call the test case 3 times with two args:
# inp: input for VIF.get_locking_mode()
# expected_output: expected output of the get_locking_mode method
# Asserted with:
# assert expected_output == get_locking_mode(input)
"input_params, expected_output",
[
# Happy path tests
(
# locked
{ # input
"locking_mode": [
"locked",
{"ipv4": ["1.1.1.1"], "ipv6": ["fe80::1"]},
]
}, # expected output
{
"mac": "00:11:22:33:44:55",
"locking_mode": "locked",
"ipv4_allowed": ["1.1.1.1"],
"ipv6_allowed": ["fe80::1"],
},
),
(
# unlocked
{"locking_mode": "unlocked"},
{
"mac": "00:11:22:33:44:55",
"locking_mode": "unlocked",
"ipv4_allowed": [],
"ipv6_allowed": [],
},
),
(
{}, # no locking_mode
{
"mac": "00:11:22:33:44:55",
"locking_mode": "",
"ipv4_allowed": [],
"ipv6_allowed": [],
},
),
],
)
def test_get_locking_mode(input_params, expected_output):
"""Test VIF.get_locking_mode() using the VIF class test parameters defined above."""

# Act: Get the locking mode configuration for the input params from the VIF object:
with patch("common.send_to_syslog") as send_to_syslog:
test_result = VifMockSubclass(input_params).get_locking_mode()

# Assert the expected output and the expected call to send_to_syslog():
assert test_result == expected_output
send_to_syslog.assert_called_once_with(
"Got locking config: " + repr(expected_output)
)
Loading