Unit Testing
============
-You can unit test outside of docker, or in docker. Unit testing outside of docker currently requires rmr to be installed.
+You can unit test in docker or outside of docker.
+The preferred method (by far) is to use Docker, because it encapsulates rmr, as well as ensuring that ports that rmr opens do not conflict with the host machine
::
- tox
- open htmlcov/index.html
+ docker build -t rmrunittestt:latest -f Dockerfile-Unit-Test .
+
+A coverage report will be shown in stdout.
-Or, use the provided Dockerfile (building is enough):
+It is possible to run tox locally provided that rmr is intalled, and you are prepared to have the test open ports (4562) as ``rmr_init`` must succeed for the tests to proceed.
::
- docker build -t rmrunittestt:latest -f Dockerfile-Unit-Test .
+ tox
+ open htmlcov/index.html
+
+The added benefit of the local option is that the coverage report can be viewed in html, which I find easier to read than the term coverage reort the docker build will print.
Installation
============
import pytest
+# These are here just to reduce the size of the code in test_rmr so those (important) tests are more readable; in theory these dicts could be large
@pytest.fixture
-def fake_consts():
- return {"RMR_MAX_XID": 32,
- "RMR_MAX_SID": 32,
- "RMR_MAX_MEID": 32,
- "RMR_MAX_SRC": 64,
- "RMR_MAX_RCV_BYTES": 4096,
- "RMRFL_NONE": 0,
- "RMRFL_AUTO_ALLOC": 1,
- "RMR_DEF_SIZE": 0,
- "RMR_VOID_MSGTYPE": -1,
- "RMR_VOID_SUBID": -1,
- "RMR_OK": 0,
- "RMR_ERR_BADARG": 1,
- "RMR_ERR_NOENDPT": 2,
- "RMR_ERR_EMPTY": 3,
- "RMR_ERR_NOHDR": 4,
- "RMR_ERR_SENDFAILED": 5,
- "RMR_ERR_CALLFAILED": 6,
- "RMR_ERR_NOWHOPEN": 7,
- "RMR_ERR_WHID": 8,
- "RMR_ERR_OVERFLOW": 9,
- "RMR_ERR_RETRY": 10,
- "RMR_ERR_RCVFAILED": 11,
- "RMR_ERR_TIMEOUT": 12,
- "RMR_ERR_UNSET": 13,
- "RMR_ERR_TRUNC": 14,
- "RMR_ERR_INITFAILED": 15}
+def expected_constants():
+ return {
+ "RMR_MAX_XID": 32,
+ "RMR_MAX_SID": 32,
+ "RMR_MAX_MEID": 32,
+ "RMR_MAX_SRC": 64,
+ "RMR_MAX_RCV_BYTES": 4096,
+ "RMRFL_NONE": 0,
+ "RMRFL_AUTO_ALLOC": 3,
+ "RMR_DEF_SIZE": 0,
+ "RMR_VOID_MSGTYPE": -1,
+ "RMR_VOID_SUBID": -1,
+ "RMR_OK": 0,
+ "RMR_ERR_BADARG": 1,
+ "RMR_ERR_NOENDPT": 2,
+ "RMR_ERR_EMPTY": 3,
+ "RMR_ERR_NOHDR": 4,
+ "RMR_ERR_SENDFAILED": 5,
+ "RMR_ERR_CALLFAILED": 6,
+ "RMR_ERR_NOWHOPEN": 7,
+ "RMR_ERR_WHID": 8,
+ "RMR_ERR_OVERFLOW": 9,
+ "RMR_ERR_RETRY": 10,
+ "RMR_ERR_RCVFAILED": 11,
+ "RMR_ERR_TIMEOUT": 12,
+ "RMR_ERR_UNSET": 13,
+ "RMR_ERR_TRUNC": 14,
+ "RMR_ERR_INITFAILED": 15,
+ }
@pytest.fixture
def expected_states():
- return {0: "RMR_OK",
- 1: "RMR_ERR_BADARG",
- 2: "RMR_ERR_NOENDPT",
- 3: "RMR_ERR_EMPTY",
- 4: "RMR_ERR_NOHDR",
- 5: "RMR_ERR_SENDFAILED",
- 6: "RMR_ERR_CALLFAILED",
- 7: "RMR_ERR_NOWHOPEN",
- 8: "RMR_ERR_WHID",
- 9: "RMR_ERR_OVERFLOW",
- 10: "RMR_ERR_RETRY",
- 11: "RMR_ERR_RCVFAILED",
- 12: "RMR_ERR_TIMEOUT",
- 13: "RMR_ERR_UNSET",
- 14: "RMR_ERR_TRUNC",
- 15: "RMR_ERR_INITFAILED"}
+ return {
+ 0: "RMR_OK",
+ 1: "RMR_ERR_BADARG",
+ 2: "RMR_ERR_NOENDPT",
+ 3: "RMR_ERR_EMPTY",
+ 4: "RMR_ERR_NOHDR",
+ 5: "RMR_ERR_SENDFAILED",
+ 6: "RMR_ERR_CALLFAILED",
+ 7: "RMR_ERR_NOWHOPEN",
+ 8: "RMR_ERR_WHID",
+ 9: "RMR_ERR_OVERFLOW",
+ 10: "RMR_ERR_RETRY",
+ 11: "RMR_ERR_RCVFAILED",
+ 12: "RMR_ERR_TIMEOUT",
+ 13: "RMR_ERR_UNSET",
+ 14: "RMR_ERR_TRUNC",
+ 15: "RMR_ERR_INITFAILED",
+ }
# See the License for the specific language governing permissions and
# limitations under the License.
# ==================================================================================
+import time
import pytest
-import json
from rmr import rmr
-from rmr.rmr_mocks import rmr_mocks
-MRC = None
SIZE = 256
+MRC = None
+
+
+def setup_module():
+ global MRC
+ MRC = rmr.rmr_init(b"4562", rmr.RMR_MAX_RCV_BYTES, 0x00)
+ while rmr.rmr_ready(MRC) == 0:
+ time.sleep(1)
-def test_get_mapping_dict(monkeypatch, fake_consts, expected_states):
+def teardown_module():
+ rmr.rmr_close(MRC)
+
+
+def _assert_new_sbuf(sbuf):
"""
- test getting mapping string
+ verify the initial state of an alloced message is what we expect
"""
+ summary = rmr.message_summary(sbuf)
+ assert summary["payload"] == b""
+ assert summary["payload length"] == 0
+ assert summary["transaction id"] == b""
+ assert summary["message state"] == 0
+ assert summary["message status"] == "RMR_OK"
+ assert summary["meid"] is None
+ assert summary["errno"] == 0
- def fake_rmr_get_consts():
- return json.dumps(fake_consts).encode("utf-8")
- monkeypatch.setattr("rmr.rmr._rmr_const", fake_rmr_get_consts)
- assert rmr._get_mapping_dict() == expected_states
- # do again, trigger cache line coverage
- assert rmr._get_mapping_dict() == expected_states
+def test_get_constants(expected_constants):
+ """
+ test getting constants
+ """
+ assert rmr._get_constants() == expected_constants
+
+def test_get_mapping_dict(expected_states):
+ """
+ test getting mapping string
+ """
+ assert rmr._get_mapping_dict() == expected_states
assert rmr._state_to_status(0) == "RMR_OK"
assert rmr._state_to_status(12) == "RMR_ERR_TIMEOUT"
assert rmr._state_to_status(666) == "UNKNOWN STATE"
-def test_meid_prettify(monkeypatch):
- rmr_mocks.patch_rmr(monkeypatch)
-
- # here we re-monkey get_meid
- monkeypatch.setattr("rmr.rmr.get_meid", lambda _: "yoooo")
+def test_meid_prettify():
+ """
+ test the printing of meid based on it's value
+ """
+ # TODO?? weirdness: setting it takes bytes, but getting it returns a string. This does NOT happen for payload; bytes in, bytes come out.
sbuf = rmr.rmr_alloc_msg(MRC, SIZE)
+ rmr.rmr_set_meid(sbuf, b"\x00" * 32, 32)
summary = rmr.message_summary(sbuf)
- assert summary["meid"] == "yoooo"
+ assert summary["meid"] is None # summary does a pretty print" of 32 null bytes
+ assert rmr.get_meid(sbuf) == "\x00" * 32 # real underlying value
+
- # test bytes
- monkeypatch.setattr("rmr.rmr.get_meid", lambda _: b"\x01\x00f\x80")
+def test_rmr_set_get():
+ """
+ test set functions
+ """
sbuf = rmr.rmr_alloc_msg(MRC, SIZE)
+ _assert_new_sbuf(sbuf)
+
+ # test payload
+ pay = b"\x01\x00\x80"
+ rmr.set_payload_and_length(pay, sbuf)
summary = rmr.message_summary(sbuf)
- assert summary["meid"] == b"\x01\x00f\x80"
+ assert summary["payload"] == pay
+ assert summary["payload length"] == 3
- # test the cleanup of null bytes
- monkeypatch.setattr(
- "rmr.rmr.get_meid",
- lambda _: "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
- )
- sbuf = rmr.rmr_alloc_msg(MRC, SIZE)
+ # test transid (note we cant test payload because it's randomly gen)
+ assert summary["transaction id"] == b""
+ assert len(summary["transaction id"]) == 0
+ rmr.generate_and_set_transaction_id(sbuf)
+ summary = rmr.message_summary(sbuf)
+ assert summary["transaction id"] != b""
+ assert len(summary["transaction id"]) == 32
+
+ # test meid
+ assert rmr.get_meid(sbuf) == "\x00" * 32
+ assert summary["meid"] is None # the summary printing function shows the above horridness as None
+ rmr.rmr_set_meid(sbuf, b"666", 3)
summary = rmr.message_summary(sbuf)
- assert summary["meid"] == None
+ # TODO?? weirdness: setting it takes bytes, but getting it returns a string. This does NOT happen for payload; bytes in, bytes come out.
+ assert rmr.get_meid(sbuf) == summary["meid"] == "666" + "\x00" * 29