diff --git a/ocs/site_config.py b/ocs/site_config.py index f25fa375..3752c602 100644 --- a/ocs/site_config.py +++ b/ocs/site_config.py @@ -47,6 +47,7 @@ def from_dict(cls, data): """ self = cls() + self.data = data for k, v in data.get('hosts', {}).items(): assert (k not in self.hosts) # duplicate host name in config file! self.hosts[k] = HostConfig.from_dict(v, parent=self, name=k) @@ -62,6 +63,10 @@ def from_yaml(cls, filename): self.source_file = filename return self + def __repr__(self): + repr_ = f'SiteConfig.from_dict({self.data})' + return repr_ + class HostConfig: def __init__(self, name=None): @@ -111,6 +116,11 @@ def from_dict(cls, data, parent=None, name=None): self.log_dir = data.get('log-dir', None) return self + def __repr__(self): + repr_ = f'HostConfig.from_dict({self.data}, ' \ + f'name={self.name})' + return repr_ + class CrossbarConfig: @classmethod @@ -134,6 +144,7 @@ def from_dict(cls, data, parent=None): if data is None: return None self = cls() + self.data = data self.parent = parent self.binary = data.get('bin', shutil.which('crossbar')) self.cbdir = data.get('config-dir') @@ -158,6 +169,13 @@ def summary(self): 'config-dir': self.cbdir, }) + def __eq__(self, other): + return self.binary == other.binary and self.cbdir == other.cbdir + + def __repr__(self): + repr_ = f'CrossbarConfig.from_dict({self.data})' + return repr_ + class HubConfig: @classmethod @@ -199,6 +217,9 @@ def from_dict(cls, data, parent=None): def summary(self): return summarize_dict(self.data) + def __repr__(self): + return f"HubConfig.from_dict({self.data})" + class InstanceConfig: def __init__(self): @@ -248,6 +269,9 @@ def from_dict(cls, data, parent=None): self.manage = "yes" return self + def __repr__(self): + return f"InstanceConfig.from_dict({self.data})" + def summarize_dict(d): output = '\n'.join([' %s: %s,' % (repr(k), repr(v)) @@ -306,6 +330,10 @@ def to_list(self): return arg_list + def __repr__(self): + args = str(self.to_list()) + return f'ArgContainer({args})' + def add_arguments(parser=None): """ diff --git a/tests/test_site_config.py b/tests/test_site_config.py index a2ed359d..b67a0f88 100644 --- a/tests/test_site_config.py +++ b/tests/test_site_config.py @@ -1,7 +1,70 @@ from unittest.mock import MagicMock, patch import pytest -from ocs.site_config import get_control_client, CrossbarConfig +import yaml + +from ocs.site_config import (ArgContainer, CrossbarConfig, HostConfig, + HubConfig, InstanceConfig, SiteConfig, + get_control_client) + +# Used for testing all Config objects +HUB_CFG = {'wamp_server': 'ws://127.0.0.1:18001/ws', + 'wamp_http': 'http://127.0.0.1:18001/call', + 'wamp_realm': 'test_realm', + 'address_root': 'observatory'} +CROSSBAR_CFG = {'config-dir': '/simonsobs/ocs/dot_crossbar/', + 'bin': '/path/to/crossbar'} +INSTANCE_CFG = {'agent-class': 'FakeDataAgent', + 'instance-id': 'fake-data1', + 'arguments': ['--mode', 'acq', + '--num-channels', '16', + '--sample-rate', '5', + '--frame-length', '10'], + 'manage': None} +HOST_1_NAME = 'ocs-docker' +HOST_1_CFG = {'log-dir': '/simonsobs/log/ocs/', + 'crossbar': CROSSBAR_CFG, + 'agent-paths': ['/path/to/ocs/agents/'], + 'agent-instances': [INSTANCE_CFG]} +SITE_CFG = {'hub': HUB_CFG, + 'hosts': {HOST_1_NAME: HOST_1_CFG}} + + +def test_site_config_from_yaml(tmp_path): + cfg_file = tmp_path / "default.yaml" + with open(cfg_file, 'w') as f: + yaml.dump(SITE_CFG, f) + cfg = SiteConfig.from_yaml(cfg_file) + assert cfg.data == SITE_CFG + + +def test_crossbar_config_from_dict(): + cfg = CrossbarConfig.from_dict(CROSSBAR_CFG) + assert cfg.data == CROSSBAR_CFG + + +def test_hub_config_from_dict(): + cfg = HubConfig.from_dict(HUB_CFG) + assert cfg.data == HUB_CFG + + +def test_host_config_from_dict(): + cfg = HostConfig.from_dict(HOST_1_CFG, name=HOST_1_NAME) + assert cfg.data == HOST_1_CFG + assert cfg.name == HOST_1_NAME + assert cfg.crossbar == CrossbarConfig.from_dict(CROSSBAR_CFG) + + +def test_instance_config_from_dict(): + cfg = InstanceConfig.from_dict(INSTANCE_CFG) + assert cfg.data == INSTANCE_CFG + + +def test_arg_container_update(): + arg1 = ArgContainer(['--arg1', 'foo', '--arg2', 'bar']) + arg2 = ArgContainer(['--arg2', 'baz']) + arg1.update(arg2) + assert arg1.to_list() == ['--arg1', 'foo', '--arg2', 'baz'] class TestGetControlClient: