"""What a connector keeps across restarts: per package, small, and durable.""" import pytest from fluksio.flow import connector_store from fluksio.flow.connector import CONTRACT_VERSION, ConnectorNode from fluksio.flow.connector_store import MAX_VALUE_BYTES, ConnectorStore class Bridge(ConnectorNode): contract = CONTRACT_VERSION @pytest.fixture def store(): store = ConnectorStore("store_test") yield store store.clear() def test_the_nodes_of_one_package_share_its_keys(): first, second = Bridge(name="a"), Bridge(name="b") try: first.store.set("cert:10.0.0.2", "PEM") assert first.store.connector == "tests" assert second.store.get("cert:10.0.0.2") == "PEM" assert ConnectorStore("other").get("cert:10.0.0.2") is None finally: first.store.clear() def test_a_value_survives_a_fresh_store(store): store.set("token", "one") store.set("token", "two") again = ConnectorStore("store_test") assert again.get("token") == "two" assert again.delete("token") is True assert again.delete("token") is False assert store.get("token") is None def test_a_value_or_a_key_count_past_the_limit_is_refused(store, monkeypatch): with pytest.raises(ValueError, match="too large"): store.set("big", "x" * (MAX_VALUE_BYTES + 1)) monkeypatch.setattr(connector_store, "MAX_KEYS", 2) store.set("one", "1") store.set("two", "2") with pytest.raises(ValueError, match="2 keys"): store.set("three", "3") # Replacing a key that is already there is not a new one. store.set("two", "22") assert [row.key for row in store.entries()] == ["one", "two"] @pytest.mark.parametrize("key", ["", "a/b", "a b", "x" * 201]) def test_a_key_the_reset_route_could_not_name_is_refused(store, key): with pytest.raises(ValueError, match="not a valid key"): store.set(key, "v") def test_a_package_name_is_a_name(): with pytest.raises(ValueError): ConnectorStore("../flows")