|
1 | 1 | import argparse |
2 | 2 | import pexpect |
3 | 3 | import pytest |
| 4 | +from labgrid import Environment, Target |
4 | 5 | from labgrid.remote.coordinator import get_server_credentials |
5 | 6 | from labgrid.remote.common import get_client_credentials |
| 7 | +from labgrid.resource import RemotePlace |
| 8 | +from labgrid.resource.common import ResourceManager |
| 9 | +from labgrid.util.yaml import dump |
6 | 10 |
|
7 | 11 |
|
8 | 12 | def test_client_help(): |
@@ -287,3 +291,81 @@ def test_get_client_credentials_valid(tmpdir): |
287 | 291 | creds = get_client_credentials(ns) |
288 | 292 |
|
289 | 293 | assert creds is not None |
| 294 | + |
| 295 | + |
| 296 | +def _make_remote_place_config(tmpdir, options=None): |
| 297 | + config = tmpdir.join("config.yaml") |
| 298 | + data = {"targets": {}} |
| 299 | + if options: |
| 300 | + data["options"] = options |
| 301 | + |
| 302 | + config.write(dump(data)) |
| 303 | + |
| 304 | + return config |
| 305 | + |
| 306 | + |
| 307 | +def _capture_remote_place_credentials(monkeypatch, tmpdir, *, options=None, tls_env=False): |
| 308 | + monkeypatch.setattr(ResourceManager, "instances", {}) |
| 309 | + monkeypatch.delenv("LG_COORDINATOR_TLS", raising=False) |
| 310 | + |
| 311 | + if tls_env: |
| 312 | + monkeypatch.setenv("LG_COORDINATOR_TLS", "true") |
| 313 | + |
| 314 | + class DummySession: |
| 315 | + loop = None |
| 316 | + |
| 317 | + def get_place(self, name): |
| 318 | + return argparse.Namespace(tags={}, acquired_resources=[]) |
| 319 | + |
| 320 | + def get_target_resources(self, place): |
| 321 | + return {} |
| 322 | + |
| 323 | + captured = {} |
| 324 | + |
| 325 | + def get_client_credentials(args): |
| 326 | + captured["args"] = args |
| 327 | + return "credentials" if args.tls else None |
| 328 | + |
| 329 | + def start_session(address, *, extra=None, credentials=None, **kwargs): |
| 330 | + captured["credentials"] = credentials |
| 331 | + return DummySession() |
| 332 | + |
| 333 | + monkeypatch.setattr("labgrid.remote.common.get_client_credentials", get_client_credentials) |
| 334 | + monkeypatch.setattr("labgrid.remote.client.start_session", start_session) |
| 335 | + |
| 336 | + config = _make_remote_place_config(tmpdir, options=options) |
| 337 | + env = Environment(str(config)) |
| 338 | + target = Target("test", env=env) |
| 339 | + RemotePlace(target, name="test") |
| 340 | + |
| 341 | + return captured |
| 342 | + |
| 343 | + |
| 344 | +@pytest.mark.parametrize( |
| 345 | + ("options", "tls_env", "expected_tls", "expected_cert"), |
| 346 | + [ |
| 347 | + pytest.param({}, False, False, None, id="default-no-tls"), |
| 348 | + pytest.param({}, True, True, None, id="tls-env"), |
| 349 | + pytest.param( |
| 350 | + {"coordinator_tls": True, "coordinator_cert": "cert.pem"}, |
| 351 | + False, |
| 352 | + True, |
| 353 | + "cert.pem", |
| 354 | + id="tls-config", |
| 355 | + ), |
| 356 | + pytest.param({"coordinator_tls": "TRUE"}, False, True, None, id="tls-config-string"), |
| 357 | + pytest.param({"coordinator_tls": False}, True, False, None, id="config-overrides-env"), |
| 358 | + pytest.param({"coordinator_tls": "false"}, True, False, None, id="config-string-overrides-env"), |
| 359 | + ], |
| 360 | +) |
| 361 | +def test_remote_place_client_credentials(monkeypatch, tmpdir, options, tls_env, expected_tls, expected_cert): |
| 362 | + cert_path, _ = setup_tmp_cert_key(tmpdir) |
| 363 | + assert cert_path.basename == "cert.pem" |
| 364 | + if expected_cert: |
| 365 | + expected_cert = str(tmpdir.join(expected_cert)) |
| 366 | + |
| 367 | + captured = _capture_remote_place_credentials(monkeypatch, tmpdir, options=options, tls_env=tls_env) |
| 368 | + |
| 369 | + assert captured["args"].tls is expected_tls |
| 370 | + assert captured["args"].cert == expected_cert |
| 371 | + assert captured["credentials"] == ("credentials" if expected_tls else None) |
0 commit comments