Skip to content

Commit

Permalink
add code coverage for salt/payload.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Phipps authored and s0undt3ch committed Oct 25, 2023
1 parent 6cb08d6 commit 841486c
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
14 changes: 13 additions & 1 deletion tests/pytests/functional/test_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,16 @@ def test_destroy(sreq, echo_server):
"""
# ensure no exceptions when we go to destroy the sreq, since __del__
# swallows exceptions, we have to call destroy directly
sreq.destroy()
assert sreq.send("clear", "foo") == {"enc": "clear", "load": "foo"}
try:
sreq.destroy()
except Exception as exc: # pylint: disable=broad-except
pytest.fail(f"sreq.destroy threw an exception {exc}")


@pytest.mark.slow_test
def test_clear_socket(sreq, echo_server):
assert sreq.send("clear", "foo") == {"enc": "clear", "load": "foo"}
assert hasattr(sreq, "_socket")
sreq.clear_socket()
assert hasattr(sreq, "_socket") is False
93 changes: 93 additions & 0 deletions tests/pytests/unit/test_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import datetime
import logging

import zmq

import salt.exceptions
import salt.payload
import salt.utils.msgpack
from salt.defaults import _Constant
from salt.utils import immutabletypes
from salt.utils.odict import OrderedDict
Expand Down Expand Up @@ -210,3 +213,93 @@ def test_constants():
sdata = salt.payload.dumps(constant)
odata = salt.payload.loads(sdata)
assert odata == constant


def test_package():
value = salt.utils.msgpack.dumps("test")
assert value == salt.payload.package("test")


def test_unpackage():
value = [b"test"]
packed = salt.utils.msgpack.dumps(value)
assert value == salt.payload.unpackage(packed)


def test_format_payload():
expected = salt.utils.msgpack.dumps(
{"enc": [b"test"], "load": {"kwargs": {"foo": "bar"}}}
)
enc = [b"test"]
kwargs = {"foo": "bar"}
payload = salt.payload.format_payload(enc=enc, kwargs=kwargs)
assert expected == payload


def test_SREQ_init():
req = salt.payload.SREQ(
"tcp://salt:3434", id_=b"id", serial="msgpack", linger=1, opts=None
)
assert req.master == "tcp://salt:3434"
assert req.id_ == b"id"
assert req.linger == 1
assert req.opts is None
assert isinstance(req.context, zmq.Context)
assert isinstance(req.poller, zmq.Poller)


def test_SREQ_socket():
req = salt.payload.SREQ(
"tcp://salt:3434", id_=b"id", serial="msgpack", linger=1, opts=None
)
# socket() is a property that auto creates a socket if a socket is wanted.
socket = req.socket
assert isinstance(socket, zmq.Socket)

req = salt.payload.SREQ(
"tcp://[2001:db8:85a3:8d3:1319:8a2e:370:7348]:3434",
id_=b"id",
serial="msgpack",
linger=1,
opts=None,
)
# socket() is a property that auto creates a socket if a socket is wanted.
socket = req.socket
assert isinstance(socket, zmq.Socket)

req = salt.payload.SREQ(
"tcp://salt:3434", id_=None, serial="msgpack", linger=1, opts=None
)
# socket() is a property that auto creates a socket if a socket is wanted.
socket = req.socket
assert isinstance(socket, zmq.Socket)


def test_SREQ_set_tcp_keepalive():
opts = {"tcp_keepalive": True}
req = salt.payload.SREQ(
"tcp://salt:3434", id_=b"id", serial="msgpack", linger=1, opts=opts
)
socket = req.socket
assert req._socket.getsockopt(zmq.TCP_KEEPALIVE)

opts = {"tcp_keepalive_idle": 100}
req = salt.payload.SREQ(
"tcp://salt:3434", id_=b"id", serial="msgpack", linger=1, opts=opts
)
socket = req.socket
assert req._socket.getsockopt(zmq.TCP_KEEPALIVE_IDLE) == 100

opts = {"tcp_keepalive_cnt": 100}
req = salt.payload.SREQ(
"tcp://salt:3434", id_=b"id", serial="msgpack", linger=1, opts=opts
)
socket = req.socket
assert req._socket.getsockopt(zmq.TCP_KEEPALIVE_CNT) == 100

opts = {"tcp_keepalive_intvl": 100}
req = salt.payload.SREQ(
"tcp://salt:3434", id_=b"id", serial="msgpack", linger=1, opts=opts
)
socket = req.socket
assert req._socket.getsockopt(zmq.TCP_KEEPALIVE_INTVL) == 100

0 comments on commit 841486c

Please sign in to comment.