def test_yaml_binary(): with pytest.raises(TLLError): Config.load('''yamls://{a: !!binary ___}''') c = Config.load('''yamls://{a: !!binary AAECAw== }''') assert c.get('a', decode=False) == b'\x00\x01\x02\x03'
def test_set_sub(): c = Config.load("yamls://{a: 1, c: 2}") sub = Config.load("yamls://{d: 3, e.f: 4}") c["b"] = sub d = dict(c.browse('**')) assert d == {'a': '1', 'c': '2', 'b.d': '3', 'b.e.f': '4'}
def test_yaml_alias(): with pytest.raises(TLLError): Config.load('''yamls://{a: *unknown}''') c = Config.load('''yamls:// a: &list - &scalar 100 - 200 b: &map a: *scalar b: *list c: *map ''') assert c.as_dict() == { 'a': ['100', '200'], 'b': { 'a': '100', 'b': ['100', '200'] }, 'c': { 'a': '100', 'b': ['100', '200'] } }
def _test_yaml(data, v): c = Config.load('yamls://' + data) d = dict(c.browse('**')) assert d == v c = Config.load('yamls+gz://' + base64gz(data)) d = dict(c.browse('**')) assert d == v
def test_open_link(): cfg = Config.load('''yamls:// processor.objects: null: url: null:// test: url: open-test://;shutdown-on=close open: state: !link /sys/null/state depends: null ''') ctx = Context() ctx.register(OpenTest) cfg.copy().copy() p = Processor(cfg, context=ctx) p.open() for w in p.workers: w.open() workers = [p] + p.workers for _ in range(100): if p.state == p.State.Closed: break for w in workers: w.step(timeout=0.001) assert OpenTest.STATE_PARAM == 'Active'
def test_getT(s, k, t, v): c = Config.load('yamls://' + s) if v is None: with pytest.raises(ValueError): c.getT(k, t) else: assert c.getT(k, t) == v
def test_enum(): scheme = '''yamls:// - name: msg id: 10 enums: Enum: {type: int32, options.type: enum, enum: {A: 10, B: 20}} fields: - {name: f0, type: Enum} - {name: f1, type: Enum} ''' url = Config.load(f'''yamls:// tll.proto: yaml name: yaml dump: scheme config.0: name: msg data: f0: A f1: 20 ''') url['scheme'] = scheme c = Accum(url) c.open() c.process() assert [(m.msgid, m.seq) for m in c.result] == [(10, 0)] m = c.unpack(c.result[0]) assert m.f0 == m.f0.A assert m.f1 == m.f1.B
def test_simple(t, v): if isinstance(v, tuple): v, s = v else: s = str(v) scheme = f'''yamls:// - name: msg id: 10 fields: - {{name: f0, type: {t} }} ''' url = Config.load(f'''yamls:// tll.proto: yaml name: yaml dump: scheme config.0: name: msg ''') url['scheme'] = scheme url['config.0.data.f0'] = s c = Accum(url) c.open() c.process() assert [(m.msgid, m.seq) for m in c.result] == [(10, 0)] m = c.unpack(c.result[0]) assert m.as_dict() == {'f0': v}
def test_union(data, r): scheme = '''yamls:// - name: sub fields: - {name: s0, type: int8} - name: msg id: 10 fields: - {name: f0, type: union, union: [{name: i8, type: int8}, {name: string, type: string}, {name: sub, type: sub}, {name: array, type: 'int8[4]'}]} ''' url = Config.load(f'''yamls:// tll.proto: yaml name: yaml dump: scheme config.0: name: msg data.f0: %s ''' % data) url['scheme'] = scheme c = Accum(url) c.open() if r is None: with pytest.raises(TLLError): c.process() assert c.state == c.State.Error return c.process() assert [(m.msgid, m.seq) for m in c.result] == [(10, 0)] m = c.unpack(c.result[0]) assert m.as_dict() == {'f0': r}
def test_many(): scheme = '''yamls:// - name: msg id: 10 fields: - {name: f0, type: int8} ''' url = Config.load('''yamls:// tll.proto: yaml name: yaml dump: scheme autoclose: no config: - {name: msg, seq: 0, data.f0: 0} - {name: msg, seq: 1, data.f0: 1} - {name: msg, seq: 2, data.f0: 2} - {name: msg, seq: 3, data.f0: 3} ''') url['scheme'] = scheme c = Accum(url) c.open() for i in range(4): c.process() assert [(m.msgid, m.seq) for m in c.result] == [(10, j) for j in range(i + 1)] c.process() assert [(m.msgid, m.seq) for m in c.result] == [(10, j) for j in range(4)] for i in range(4): assert c.unpack(c.result[i]).as_dict() == {'f0': i}
def test_message(): scheme = '''yamls:// - name: sub fields: - {name: s0, type: int8} - name: msg id: 10 fields: - {name: f0, type: sub} ''' url = Config.load(f'''yamls:// tll.proto: yaml name: yaml dump: scheme config.0: name: msg data.f0.s0: 123 ''') url['scheme'] = scheme c = Accum(url) c.open() c.process() assert [(m.msgid, m.seq) for m in c.result] == [(10, 0)] m = c.unpack(c.result[0]) assert m.as_dict() == {'f0': {'s0': 123}}
def test_copy(): cfg = Config.load('yamls://{a: 1, b: {x: 2, y: 3}}') assert dict(cfg.browse('**')) == {'a': '1', 'b.x': '2', 'b.y': '3'} cc0 = cfg.copy() cc1 = copy.copy(cfg) cdc = copy.deepcopy(cfg) for c in [cc0, cc1, cdc]: assert dict(c.browse('**')) == {'a': '1', 'b.x': '2', 'b.y': '3'} cfg['a'] = '9' for c in [cc0, cc1, cdc]: assert dict(c.browse('**')) == {'a': '1', 'b.x': '2', 'b.y': '3'}
def test_defaults(): defaults = Config() ctx = C.Context(defaults) defaults['mem.size'] = 'zzz' with pytest.raises(TLLError): ctx.Channel('mem://;name=mem') defaults['mem.size'] = '1kb' c = ctx.Channel('mem://') assert c.config['state'] == 'Closed' del c del defaults['mem.size']
def main(): tll.logger.init() args = parser.parse_args() if '://' not in args.config: args.config = 'yaml://' + args.config cfg = Config.load(args.config) for d in args.defs: kv = d.split('=', 1) if len(kv) != 2: print(f"Invalid KEY=VALUE parameter: '{d}'") sys.exit(1) cfg[kv[0]] = kv[1] cfg.process_imports('processor.include') return run(cfg)
def test_binary(): url = Config.load(br'''yamls:// tll.proto: yaml name: yaml dump: frame config: - msgid: 1 seq: 10 data: "abc\x01def" ''') c = Accum(url) c.open() c.process() assert c.state == c.State.Active assert [(m.msgid, m.seq, m.data.tobytes()) for m in c.result] == [(1, 10, b'abc\x01def')] c.process() assert c.state == c.State.Closed
def test_basic(): cfg = Config.load( 'yamls://{a: [{x: 0, y: 1}, {x: 1, y: 0}], b: {x: 2, y: 2}}') assert cfg['b.x'] == '2' assert cfg['b.y'] == '2' assert 'b.z' not in cfg assert 'b.x' in cfg assert cfg.as_dict() == { 'a': [{ 'x': '0', 'y': '1' }, { 'x': '1', 'y': '0' }], 'b': { 'x': '2', 'y': '2' } }
def test_list(t, v): scheme = f'''yamls:// - name: msg id: 10 fields: - {{name: f0, type: {t} }} ''' url = Config.load(f'''yamls:// tll.proto: yaml name: yaml dump: scheme config.0: name: msg data.f0: {v} ''') url['scheme'] = scheme c = Accum(url) c.open() c.process() assert [(m.msgid, m.seq) for m in c.result] == [(10, 0)] m = c.unpack(c.result[0]) assert m.as_dict() == {'f0': v}
def test_process_imports(): c = Config.load('''yamls:// import: - 'yamls://{a: 1, b.c: 2}' - 'yamls://{a: 2, b.d: 3}' b.c: 10 ''') assert c.as_dict() == { 'b': { 'c': '10' }, 'import': ['yamls://{a: 1, b.c: 2}', 'yamls://{a: 2, b.d: 3}'] } c.process_imports('import') assert c.as_dict() == { 'a': '2', 'b': { 'c': '10', 'd': '3' }, 'import': ['yamls://{a: 1, b.c: 2}', 'yamls://{a: 2, b.d: 3}'] }
def test_yaml_link(): c = Config.load('''yamls:// a: - 100 - 200 b: a: !link /a/0001/../0000 b: !link ../../a c: !link /b ''') print(c.as_dict()) assert c.as_dict() == { 'a': ['100', '200'], 'b': { 'a': '100', 'b': ['100', '200'] }, 'c': { 'a': '100', 'b': ['100', '200'] } }
def _test_merge(ow, c0, c1, r): c = Config.load('yamls://' + c0) c.merge(Config.load('yamls://' + c1), overwrite=ow == 'overwrite') d = dict(c.browse('**')) assert d == r
def _test_load(proto, data, r): c = Config.load_data(proto, data) assert dict(c.browse("**")) == r
def test_yaml_fail(data): with pytest.raises(TLLError): Config.load('yamls://' + data)