|
| 1 | +# |
| 2 | +# sambacc: a samba container configuration tool |
| 3 | +# Copyright (C) 2025 John Mulligan |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with this program. If not, see <http://www.gnu.org/licenses/> |
| 17 | +# |
| 18 | + |
| 19 | +import contextlib |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture() |
| 25 | +def mock_varlink_server(tmp_path): |
| 26 | + try: |
| 27 | + import sambacc.varlink.server |
| 28 | + import sambacc.varlink.keybridge |
| 29 | + except ImportError: |
| 30 | + pytest.skip("can not import varlink server") |
| 31 | + |
| 32 | + sockdir = tmp_path / "socket" |
| 33 | + sockpath = sockdir / "v.sock" |
| 34 | + sockdir.mkdir(parents=True, exist_ok=True) |
| 35 | + |
| 36 | + addr = f"unix:{sockpath.absolute()}" |
| 37 | + sambacc.varlink.server.patch_varlink_encoder() |
| 38 | + opts = sambacc.varlink.server.VarlinkServerOptions(addr) |
| 39 | + srv = sambacc.varlink.server.VarlinkServer(opts) |
| 40 | + mem_scope = sambacc.varlink.keybridge.MemScope() |
| 41 | + mem_scope.set( |
| 42 | + "example", sambacc.varlink.keybridge.EntryKind.VALUE, "nice one" |
| 43 | + ) |
| 44 | + srv.add_endpoint( |
| 45 | + sambacc.varlink.keybridge.endpoint( |
| 46 | + [ |
| 47 | + sambacc.varlink.keybridge.StaticValueScope(), |
| 48 | + mem_scope, |
| 49 | + ] |
| 50 | + ) |
| 51 | + ) |
| 52 | + with srv.serve(): |
| 53 | + yield srv |
| 54 | + |
| 55 | + |
| 56 | +@contextlib.contextmanager |
| 57 | +def _keybridge_conn(address): |
| 58 | + import varlink # type: ignore[import] |
| 59 | + |
| 60 | + with contextlib.ExitStack() as estack: |
| 61 | + client = estack.enter_context(varlink.Client(address)) |
| 62 | + connection = estack.enter_context( |
| 63 | + client.open("org.samba.containers.keybridge") |
| 64 | + ) |
| 65 | + yield connection |
| 66 | + |
| 67 | + |
| 68 | +def test_scope_list(mock_varlink_server): |
| 69 | + with _keybridge_conn(mock_varlink_server.options.address) as connection: |
| 70 | + result = connection.Scopes() |
| 71 | + assert len(result["scopes"]) == 2 |
| 72 | + |
| 73 | + |
| 74 | +def test_has_scope(mock_varlink_server): |
| 75 | + with _keybridge_conn(mock_varlink_server.options.address) as connection: |
| 76 | + # first scope |
| 77 | + result = connection.HasScope("static_value_scope") |
| 78 | + assert "scope" in result |
| 79 | + assert result["scope"]["name"] == "static_value_scope" |
| 80 | + # second scope |
| 81 | + result = connection.HasScope("mem") |
| 82 | + assert "scope" in result |
| 83 | + assert result["scope"]["name"] == "mem" |
| 84 | + # missing scope |
| 85 | + result = connection.HasScope("foo") |
| 86 | + assert "scope" not in result |
| 87 | + |
| 88 | + |
| 89 | +def test_get_invalid_scope(mock_varlink_server): |
| 90 | + import varlink |
| 91 | + from sambacc.varlink.keybridge import EntryKind |
| 92 | + |
| 93 | + with _keybridge_conn(mock_varlink_server.options.address) as connection: |
| 94 | + with pytest.raises(varlink.VarlinkError): |
| 95 | + connection.Get("bob", "foo", EntryKind.VALUE) |
| 96 | + |
| 97 | + |
| 98 | +def test_get_static_value(mock_varlink_server): |
| 99 | + from sambacc.varlink.keybridge import EntryKind |
| 100 | + |
| 101 | + with _keybridge_conn(mock_varlink_server.options.address) as connection: |
| 102 | + result = connection.Get("foo", "static_value_scope", EntryKind.VALUE) |
| 103 | + assert "entry" in result |
| 104 | + assert "name" in result["entry"] |
| 105 | + assert result["entry"]["name"] == "foo" |
| 106 | + assert result["entry"]["data"] == "foo-opolis" |
| 107 | + |
| 108 | + |
| 109 | +def test_set_static_value_badop(mock_varlink_server): |
| 110 | + import varlink |
| 111 | + |
| 112 | + with _keybridge_conn(mock_varlink_server.options.address) as connection: |
| 113 | + with pytest.raises(varlink.VarlinkError): |
| 114 | + connection.Set( |
| 115 | + { |
| 116 | + "name": "quux", |
| 117 | + "scope": "static_value_scope", |
| 118 | + "kind": "VALUE", |
| 119 | + "data": "flippy", |
| 120 | + } |
| 121 | + ) |
| 122 | + |
| 123 | + |
| 124 | +def test_delete_static_value_badop(mock_varlink_server): |
| 125 | + import varlink |
| 126 | + |
| 127 | + with _keybridge_conn(mock_varlink_server.options.address) as connection: |
| 128 | + with pytest.raises(varlink.VarlinkError): |
| 129 | + connection.Delete("quux", "static_value_scope") |
| 130 | + |
| 131 | + |
| 132 | +def test_get_mem_missing(mock_varlink_server): |
| 133 | + import varlink |
| 134 | + from sambacc.varlink.keybridge import EntryKind |
| 135 | + |
| 136 | + with _keybridge_conn(mock_varlink_server.options.address) as connection: |
| 137 | + with pytest.raises(varlink.VarlinkError): |
| 138 | + connection.Get("foo", "mem", EntryKind.VALUE) |
| 139 | + |
| 140 | + |
| 141 | +def test_get_mem(mock_varlink_server): |
| 142 | + from sambacc.varlink.keybridge import EntryKind |
| 143 | + |
| 144 | + with _keybridge_conn(mock_varlink_server.options.address) as connection: |
| 145 | + result = connection.Get("example", "mem", EntryKind.VALUE) |
| 146 | + assert "entry" in result |
| 147 | + assert "name" in result["entry"] |
| 148 | + assert result["entry"]["name"] == "example" |
| 149 | + assert result["entry"]["data"] == "nice one" |
| 150 | + |
| 151 | + |
| 152 | +def test_set_mem(mock_varlink_server): |
| 153 | + with _keybridge_conn(mock_varlink_server.options.address) as connection: |
| 154 | + connection.Set( |
| 155 | + { |
| 156 | + "name": "quux", |
| 157 | + "scope": "mem", |
| 158 | + "kind": "VALUE", |
| 159 | + "data": "flippy", |
| 160 | + } |
| 161 | + ) |
| 162 | + |
| 163 | + |
| 164 | +def test_delete_mem(mock_varlink_server): |
| 165 | + with _keybridge_conn(mock_varlink_server.options.address) as connection: |
| 166 | + connection.Delete("quux", "mem") |
0 commit comments