Skip to content
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
6 changes: 6 additions & 0 deletions gvm/protocols/gmp/_gmpnext.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ def create_agent_group(
self,
name: str,
agent_ids: list[str],
scheduler_cron_time: str,
*,
comment: str | None = None,
) -> T:
Expand All @@ -236,6 +237,7 @@ def create_agent_group(
Args:
name: Name of the new agent group.
agent_ids: List of agent UUIDs to include in the group (required).
scheduler_cron_time: Scheduler cron to use.
comment: Optional comment for the group.

Raises:
Expand All @@ -246,12 +248,14 @@ def create_agent_group(
name=name,
comment=comment,
agent_ids=agent_ids,
scheduler_cron_time=scheduler_cron_time,
)
)

def modify_agent_group(
self,
agent_group_id: EntityID,
scheduler_cron_time: str,
*,
name: str | None = None,
comment: str | None = None,
Expand All @@ -262,6 +266,7 @@ def modify_agent_group(
Args:
agent_group_id: UUID of the group to modify.
name: Optional new name for the group.
scheduler_cron_time: Scheduler cron to use.
comment: Optional comment for the group.
agent_ids: Optional list of agent UUIDs to set for the group.

Expand All @@ -274,6 +279,7 @@ def modify_agent_group(
name=name,
comment=comment,
agent_ids=agent_ids,
scheduler_cron_time=scheduler_cron_time,
)
)

Expand Down
18 changes: 17 additions & 1 deletion gvm/protocols/gmp/requests/next/_agent_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def create_agent_group(
cls,
name: str,
agent_ids: list[str],
scheduler_cron_time: str,
*,
comment: str | None = None,
) -> Request:
Expand All @@ -24,6 +25,7 @@ def create_agent_group(
Args:
name: Name of the new agent group.
agent_ids: List of agent UUIDs to include in the group (required).
scheduler_cron_time: Cron-like time to schedule new agent groups (required).
comment: Optional comment for the group.

Raises:
Expand All @@ -38,9 +40,15 @@ def create_agent_group(
raise RequiredArgument(
function=cls.create_agent_group.__name__, argument="agent_ids"
)
if not scheduler_cron_time:
raise RequiredArgument(
function=cls.create_agent_group.__name__,
argument="scheduler_cron_time",
)

cmd = XmlCommand("create_agent_group")
cmd.add_element("name", name)
cmd.add_element("scheduler_cron_time", scheduler_cron_time)

if comment:
cmd.add_element("comment", comment)
Expand Down Expand Up @@ -76,6 +84,7 @@ def clone_agent_group(cls, agent_group_id: EntityID) -> Request:
def modify_agent_group(
cls,
agent_group_id: EntityID,
scheduler_cron_time: str,
*,
name: str | None = None,
comment: str | None = None,
Expand All @@ -85,6 +94,7 @@ def modify_agent_group(

Args:
agent_group_id: UUID of the group to modify.
scheduler_cron_time: Cron-like time to schedule the agent groups.
name: Optional new name for the group.
comment: Optional comment for the group.
agent_ids: Optional list of agent UUIDs to set for the group.
Expand All @@ -98,9 +108,15 @@ def modify_agent_group(
argument="agent_group_id",
)

if not scheduler_cron_time:
raise RequiredArgument(
function=cls.modify_agent_group.__name__,
argument="scheduler_cron_time",
)

cmd = XmlCommand("modify_agent_group")
cmd.set_attribute("agent_group_id", str(agent_group_id))

cmd.add_element("scheduler_cron_time", scheduler_cron_time)
if name:
cmd.add_element("name", name)

Expand Down
4 changes: 2 additions & 2 deletions tests/connections/test_debug_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
from gvm.connections._connection import AbstractGvmConnection


class TestConnection(AbstractGvmConnection):
class DummyConnection(AbstractGvmConnection):
def connect(self) -> None:
pass


class DebugConnectionTestCase(unittest.TestCase):
def test_is_gvm_connection(self):
connection = DebugConnection(TestConnection())
connection = DebugConnection(DummyConnection())
self.assertTrue(isinstance(connection, GvmConnection))
12 changes: 6 additions & 6 deletions tests/connections/test_gvm_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@
from gvm.errors import GvmError


class TestConnection(AbstractGvmConnection):
class DummyConnection(AbstractGvmConnection):
def connect(self) -> None:
pass


class GvmConnectionTestCase(unittest.TestCase):
# pylint: disable=protected-access
def test_init_no_args(self):
connection = TestConnection()
connection = DummyConnection()

self.assertIsNone(connection._socket)
self.assertEqual(connection._timeout, DEFAULT_TIMEOUT)

def test_init_with_none(self):
connection = TestConnection(timeout=None)
connection = DummyConnection(timeout=None)

self.assertIsNone(connection._socket)
self.assertEqual(connection._timeout, DEFAULT_TIMEOUT)
Expand All @@ -37,7 +37,7 @@ def test_read_no_data(self):
read_mock = MagicMock()
read_mock.return_value = None

connection = TestConnection()
connection = DummyConnection()
connection._read = read_mock

with self.assertRaises(GvmError, msg="Remote closed the connection"):
Expand All @@ -49,7 +49,7 @@ def test_read_trigger_timeout(self):
read_mock = MagicMock()
read_mock.side_effect = [b"<foo>xyz<bar></bar>", b"</foo>"]

connection = TestConnection(timeout=0)
connection = DummyConnection(timeout=0)
connection._read = read_mock

with self.assertRaises(
Expand All @@ -58,5 +58,5 @@ def test_read_trigger_timeout(self):
connection.read()

def test_is_gvm_connection(self):
connection = TestConnection()
connection = DummyConnection()
self.assertTrue(isinstance(connection, GvmConnection))
19 changes: 16 additions & 3 deletions tests/connections/test_unix_socket_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
class DummyRequestHandler(socketserver.BaseRequestHandler):
def handle(self):
response = b'<gmp_response status="200" status_text="OK"/>'

self.request.settimeout(0.2)
try:
self.request.recv(16 * 1024)
except (TimeoutError, OSError):
# In tests, receiving request data is optional; on timeout/socket read
# issues we still return the canned response to keep behavior deterministic.
_ = None

self.request.sendall(response)


Expand All @@ -47,29 +56,33 @@ def setUp(self):
self.server_thread.start()

def tearDown(self):
self.socket_server.server_close()
self.socket_server.shutdown()
self.server_thread.join(60.0)
self.socket_server.server_close()
self.socket_path.unlink(missing_ok=True)

def test_unix_socket_connection_connect_read(self):
connection = UnixSocketConnection(
path=self.socket_name, timeout=DEFAULT_TIMEOUT
)
self.addCleanup(connection.disconnect)

connection.connect()
resp = connection.read()

self.assertEqual(resp, b'<gmp_response status="200" status_text="OK"/>')
connection.disconnect()

def test_unix_socket_connection_connect_send_bytes_read(self):
connection = UnixSocketConnection(
path=self.socket_name, timeout=DEFAULT_TIMEOUT
)
self.addCleanup(connection.disconnect)

connection.connect()
connection.send(b"<gmp/>")
resp = connection.read()

self.assertEqual(resp, b'<gmp_response status="200" status_text="OK"/>')
connection.disconnect()

def test_unix_socket_connect_file_not_found(self):
connection = UnixSocketConnection(path="foo", timeout=DEFAULT_TIMEOUT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,49 @@ def test_create_agent_group(self):
name="ExampleGroup",
agent_ids=["agent-1", "agent-2"],
comment="Sample comment",
scheduler_cron_time="0 */5 * * *",
)

self.connection.send.has_been_called_with(
b"<create_agent_group>"
b"<name>ExampleGroup</name>"
b"<scheduler_cron_time>0 */5 * * *</scheduler_cron_time>"
b"<comment>Sample comment</comment>"
b'<agents><agent id="agent-1"/><agent id="agent-2"/></agents>'
b"</create_agent_group>"
)

def test_create_agent_group_without_name(self):
with self.assertRaises(RequiredArgument):
self.gmp.create_agent_group(name="", agent_ids=["agent-1"])
self.gmp.create_agent_group(
name="",
agent_ids=["agent-1"],
scheduler_cron_time="0 */5 * * *",
)

def test_create_agent_group_without_agents(self):
with self.assertRaises(RequiredArgument):
self.gmp.create_agent_group(name="Group", agent_ids=[])
self.gmp.create_agent_group(
name="Group", agent_ids=[], scheduler_cron_time="0 */5 * * *"
)

def test_create_agent_group_without_scheduler_cron_time(self):
with self.assertRaises(RequiredArgument):
self.gmp.create_agent_group(
name="Group", agent_ids=["agent-1"], scheduler_cron_time=None
)

def test_create_agent_group_without_comment(self):
self.gmp.create_agent_group(name="GroupX", agent_ids=["a1", "a2"])
self.gmp.create_agent_group(
name="GroupX",
agent_ids=["a1", "a2"],
scheduler_cron_time="0 */5 * * *",
)

self.connection.send.has_been_called_with(
b"<create_agent_group>"
b"<name>GroupX</name>"
b"<scheduler_cron_time>0 */5 * * *</scheduler_cron_time>"
b'<agents><agent id="a1"/><agent id="a2"/></agents>'
b"</create_agent_group>"
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ def test_modify_agent_group(self):
agent_group_id="group-1",
name="NewName",
comment="Updated comment",
scheduler_cron_time="0 */5 * * *",
agent_ids=["agent-1", "agent-2"],
)

self.connection.send.has_been_called_with(
b'<modify_agent_group agent_group_id="group-1">'
b"<scheduler_cron_time>0 */5 * * *</scheduler_cron_time>"
b"<name>NewName</name>"
b"<comment>Updated comment</comment>"
b'<agents><agent id="agent-1"/><agent id="agent-2"/></agents>'
Expand All @@ -24,17 +26,27 @@ def test_modify_agent_group(self):

def test_modify_agent_group_without_id(self):
with self.assertRaises(RequiredArgument):
self.gmp.modify_agent_group(agent_group_id=None)
self.gmp.modify_agent_group(
agent_group_id=None, scheduler_cron_time="0 */5 * * *"
)

def test_modify_agent_group_without_scheduler_cron_time(self):
with self.assertRaises(RequiredArgument):
self.gmp.modify_agent_group(
agent_group_id="group-1", scheduler_cron_time=None
)

def test_modify_agent_group_without_name(self):
self.gmp.modify_agent_group(
agent_group_id="group-1",
comment="Updated comment",
scheduler_cron_time="0 */5 * * *",
agent_ids=["agent-1", "agent-2"],
)

self.connection.send.has_been_called_with(
b'<modify_agent_group agent_group_id="group-1">'
b"<scheduler_cron_time>0 */5 * * *</scheduler_cron_time>"
b"<comment>Updated comment</comment>"
b'<agents><agent id="agent-1"/><agent id="agent-2"/></agents>'
b"</modify_agent_group>"
Expand All @@ -44,11 +56,13 @@ def test_modify_agent_group_without_comment(self):
self.gmp.modify_agent_group(
agent_group_id="group-1",
name="NewName",
scheduler_cron_time="0 */5 * * *",
agent_ids=["agent-1", "agent-2"],
)

self.connection.send.has_been_called_with(
b'<modify_agent_group agent_group_id="group-1">'
b"<scheduler_cron_time>0 */5 * * *</scheduler_cron_time>"
b"<name>NewName</name>"
b'<agents><agent id="agent-1"/><agent id="agent-2"/></agents>'
b"</modify_agent_group>"
Expand All @@ -59,10 +73,12 @@ def test_modify_agent_group_without_agent_ids(self):
agent_group_id="group-1",
name="NewName",
comment="Updated comment",
scheduler_cron_time="0 */5 * * *",
)

self.connection.send.has_been_called_with(
b'<modify_agent_group agent_group_id="group-1">'
b"<scheduler_cron_time>0 */5 * * *</scheduler_cron_time>"
b"<name>NewName</name>"
b"<comment>Updated comment</comment>"
b"</modify_agent_group>"
Expand Down
Loading