Skip to content
Snippets Groups Projects
Verified Commit 84db5260 authored by Karel Koci's avatar Karel Koci :metal:
Browse files

selftests/cli: use lorem-text instead of static lorem ipsum

This replaces static strings with generated ones from lorem-text
library.

To make tests reproducible we have to se seed for random. This is pretty
common so this adds context function for it to toolbox.

Toolbox was labeled as common tasks run on router. Now this changes it
to just generic common toolbox and splits it to three submodules. There
is module for Alpine Linux as well as for OpenWrt. Then there is one
additional module for utilities needed in tests implementation.
parent 4890485d
No related merge requests found
Pipeline #92901 passed with warnings with stages
in 1 minute and 7 seconds
"""This module contains various utilities that are used in tests regularly so we want to share them.
Understand this as catch all place for utilities but think first before you add function here. It is always way more
preferable to add functions to other places than this one.
"""
from . import alpine, openwrt, tests
__all__ = [
"alpine",
"openwrt",
"tests",
]
"""This contains implementations of common actions that are commonly performed in tests on Alpine.
"""
"""This contains implementations of common actions that are commonly performed on router in tests.
"""This contains implementations of common actions that are commonly performed in tests on OpenWrt.
"""
import json
from .cli import Shell
from ..cli import Shell
def service_is_running(service: str, shell: Shell):
......
"""These are common utilities used in tests implementation that control how tests are executed.
"""
import contextlib
import random
@contextlib.contextmanager
def deterministic_random(seed=42):
"""Makes random module somewhat deterministic by setting specific seed every time.
The state of random generator is preserved during this so you can even nest these.
"""
state = random.getstate()
random.seed(seed)
yield
random.setstate(state)
......@@ -4,3 +4,4 @@ pyserial
pylxd
dateutils
selenium
lorem-text
......@@ -3,8 +3,10 @@
import sys
import pytest
from lorem_text import lorem
from nsfarm.cli import LineBytesAggregate
from nsfarm.toolbox.tests import deterministic_random
@pytest.fixture(name="aggregate")
......@@ -14,65 +16,56 @@ def fixture_aggregate():
return aggregate, collected
lorem_ipsum = (
b"Lorem ipsum dolor sit amet,",
b"consectetur adipiscing elit,",
b"sed do eiusmod tempor incididunt ut labore et",
b"dolore magna aliqua. Ut enim ad minim veniam,",
b"quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea",
b"commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla "
+ b"pariatur. Excepteur sint occaecat cupidatat non proident,",
b"sunt in culpa qui officia deserunt mollit anim id est laborum.",
)
@pytest.mark.parametrize("delimiter", [b"\n", b"\r", b"\n\r", b"\r\n", b"\r\r\n", b"\r\n\r\r"])
class TestSplits:
"""Test our ability to split stream to lines based on different delimiters."""
with deterministic_random() as _:
lorem = [lorem.sentence().encode() for i in range(7)]
def test_one_call(self, aggregate, delimiter):
"""Test that we correctly parse it if we call it with all data at once."""
agg, collected = aggregate
agg.add(delimiter.join(lorem_ipsum))
assert tuple(collected) == lorem_ipsum[:-1]
agg.add(delimiter.join(self.lorem))
assert collected == self.lorem[:-1]
agg.flush()
assert tuple(collected) == lorem_ipsum
assert collected == self.lorem
def test_by_block(self, aggregate, delimiter):
"""Test that we correctly parse it if we call it multiple times with same sized blockes."""
agg, collected = aggregate
data = delimiter.join(lorem_ipsum)
data = delimiter.join(self.lorem)
block = 24
for i in range(len(data) // block + 1):
agg.add(data[block * i : min(block * (i + 1), len(data))])
assert tuple(collected) == lorem_ipsum[:-1]
assert collected == self.lorem[:-1]
agg.flush()
assert tuple(collected) == lorem_ipsum
assert collected == self.lorem
def test_by_line(self, aggregate, delimiter):
"""Test that we correctly parse it if we call it multiple times with same sized blockes."""
agg, collected = aggregate
for line in lorem_ipsum:
for line in self.lorem:
agg.add(line + delimiter)
assert tuple(collected) == lorem_ipsum
assert collected == self.lorem
def test_by_line_delay(self, aggregate, delimiter):
"""Test that we correctly parse it if we call it multiple times with same sized blockes."""
agg, collected = aggregate
for line in lorem_ipsum:
agg.add((b"" if line is lorem_ipsum[0] else delimiter) + line)
assert tuple(collected) == lorem_ipsum[:-1]
for line in self.lorem:
agg.add((b"" if line is self.lorem[0] else delimiter) + line)
assert collected == self.lorem[:-1]
agg.flush()
assert tuple(collected) == lorem_ipsum
assert collected == self.lorem
def test_by_char(self, aggregate, delimiter):
"""Test that we correctly parse it if we call it multiple times with same sized blockes."""
agg, collected = aggregate
for byte in delimiter.join(lorem_ipsum):
for byte in delimiter.join(self.lorem):
agg.add(byte.to_bytes(1, byteorder=sys.byteorder))
assert tuple(collected) == lorem_ipsum[:-1]
assert collected == self.lorem[:-1]
agg.flush()
assert tuple(collected) == lorem_ipsum
assert collected == self.lorem
def test_real(aggregate):
......
"""Shell class tests.
"""
import pytest
from lorem_text import lorem
from nsfarm.cli import Shell
from nsfarm.lxd import Container
from nsfarm.toolbox.tests import deterministic_random
class Common:
......@@ -37,24 +39,15 @@ class Common:
def test_txt(self, request, shell, test_file):
"""Check for simple use of txt_write and txt_read."""
txt = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et \
dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex \
ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu \
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt \
mollit anim id est laborum."
with deterministic_random() as _:
txt = lorem.paragraph()
shell.txt_write(test_file, txt)
assert txt == shell.txt_read(test_file)
def test_txt_multiline(self, request, shell, test_file):
"""Check for multiline use of txt_write and txt_read."""
txt = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla \
pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum."""
with deterministic_random() as _:
txt = lorem.paragraphs(5) # 5 paragraphs means five lines
shell.txt_write(test_file, txt)
assert txt == shell.txt_read(test_file)
......
......@@ -2,7 +2,7 @@
"""
import pytest
from nsfarm.toolbox import service_is_running
from nsfarm.toolbox.openwrt import service_is_running
sentinel_services = [
"sentinel-dynfw-client",
......
......@@ -7,7 +7,7 @@ import pytest
from nsfarm.cli import Shell
from nsfarm.lxd import Container
from nsfarm.toolbox import service_is_running
from nsfarm.toolbox.openwrt import service_is_running
from . import mark
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment