def test_input_dict_type(self): sample_meta_dir = self.make_resource_meta(""" id: sample handler: ansible version: 1.0.0 input: values: schema: {a: int, b: int} value: {} """) sample1 = self.create_resource('sample1', sample_meta_dir, {'values': { 'a': 1, 'b': 2 }}) sample2 = self.create_resource('sample2', sample_meta_dir) xs.connect(sample1, sample2) self.assertEqual(sample1.args['values'], sample2.args['values']) # Check update sample1.update({'values': {'a': 2}}) self.assertEqual(sample1.args['values'], {'a': 2}) self.assertEqual( sample1.args['values'], sample2.args['values'], ) # Check disconnect # TODO: should sample2.value be reverted to original value? xs.disconnect(sample1, sample2) sample1.update({'values': {'a': 3}}) self.assertEqual(sample1.args['values'], {'a': 3})
def disconnect(receiver, emitter): click.echo('Disconnect {} from {}'.format(emitter, receiver)) emitter = sresource.load(emitter) receiver = sresource.load(receiver) click.echo(emitter) click.echo(receiver) signals.disconnect(emitter, receiver) show_emitter_connections(emitter)
def test_backtrack_dict_list(self): sample_meta_dir = self.make_resource_meta(""" id: sample handler: ansible version: 1.0.0 input: value: schema: str! value: """) sample_dict_list_meta_dir = self.make_resource_meta(""" id: sample_dict_list handler: ansible version: 1.0.0 input: value: schema: [{a: str!}] value: """) sample_dict_list = self.create_resource( 'sample_dict', sample_dict_list_meta_dir ) vi = sample_dict_list.resource_inputs()['value'] sample1 = self.create_resource( 'sample1', sample_meta_dir, {'value': 'x'} ) sample2 = self.create_resource( 'sample2', sample_meta_dir, {'value': 'y'} ) sample3 = self.create_resource( 'sample3', sample_meta_dir, {'value': 'z'} ) self.assertEqual(vi.backtrack_value_emitter(), vi) # [{a: sample1}] -> sample_dict_list signals.connect(sample1, sample_dict_list, {'value': 'value:a'}) self.assertListEqual(vi.backtrack_value_emitter(), [{'a': sample1.resource_inputs()['value']}]) # [{a: sample1}, {a: sample3}] -> sample_dict_list signals.connect(sample3, sample_dict_list, {'value': 'value:a'}) self.assertItemsEqual(vi.backtrack_value_emitter(), [{'a': sample1.resource_inputs()['value']}, {'a': sample3.resource_inputs()['value']}]) # [{a: sample1}, {a: sample2}] -> sample_dict_list signals.connect(sample2, sample_dict_list, {'value': 'value:a|sample3'}) self.assertItemsEqual(vi.backtrack_value_emitter(), [{'a': sample1.resource_inputs()['value']}, {'a': sample2.resource_inputs()['value']}]) # sample2 disconnected signals.disconnect(sample2, sample_dict_list) self.assertEqual(vi.backtrack_value_emitter(), [{'a': sample1.resource_inputs()['value']}])
def disconnect(receiver, emitter): click.echo('Disconnect {} from {}'.format(emitter, receiver)) emitter = sresource.load(emitter) receiver = sresource.load(receiver) click.echo(emitter) click.echo(receiver) signals.disconnect(emitter, receiver) clients = signals.Connections.read_clients() show_emitter_connections(emitter.name, clients[emitter.name])
def test_input_dict_type(self): sample_meta_dir = self.make_resource_meta(""" id: sample handler: ansible version: 1.0.0 input: values: schema: {a: int, b: int} value: {} """) sample1 = self.create_resource( 'sample1', sample_meta_dir, {'values': {'a': 1, 'b': 2}} ) sample2 = self.create_resource( 'sample2', sample_meta_dir, {'values': None} ) xs.connect(sample1, sample2) self.assertEqual( sample1.args['values'], sample2.args['values'] ) self.assertEqual( sample2.args['values'].emitter, sample1.args['values'] ) # Check update sample1.update({'values': {'a': 2}}) self.assertEqual( sample1.args['values'], {'a': 2} ) self.assertEqual( sample1.args['values'], sample2.args['values'], ) # Check disconnect # TODO: should sample2.value be reverted to original value? xs.disconnect(sample1, sample2) sample1.update({'values': {'a': 3}}) self.assertEqual( sample1.args['values'], {'a': 3} ) self.assertEqual( sample2.args['values'], {'a': 2} ) self.assertEqual(sample2.args['values'].emitter, None)
def test_backtrack_list(self): sample_meta_dir = self.make_resource_meta(""" id: sample handler: ansible version: 1.0.0 input: value: schema: str! value: """) sample_list_meta_dir = self.make_resource_meta(""" id: sample_list handler: ansible version: 1.0.0 input: values: schema: [str!] value: """) sample_list = self.create_resource( 'sample_list', sample_list_meta_dir ) vi = sample_list.resource_inputs()['values'] sample1 = self.create_resource( 'sample1', sample_meta_dir, {'value': 'x'} ) sample2 = self.create_resource( 'sample2', sample_meta_dir, {'value': 'y'} ) sample3 = self.create_resource( 'sample3', sample_meta_dir, {'value': 'z'} ) self.assertEqual(vi.backtrack_value_emitter(), vi) # [sample1] -> sample_list signals.connect(sample1, sample_list, {'value': 'values'}) self.assertEqual(vi.backtrack_value_emitter(), [sample1.resource_inputs()['value']]) # [sample3, sample1] -> sample_list signals.connect(sample3, sample_list, {'value': 'values'}) self.assertSetEqual(set(vi.backtrack_value_emitter()), set([sample1.resource_inputs()['value'], sample3.resource_inputs()['value']])) # sample2 disconnected signals.disconnect(sample1, sample_list) self.assertEqual(vi.backtrack_value_emitter(), [sample3.resource_inputs()['value']])
def test_hash_input_with_list(self): sample_meta_dir = self.make_resource_meta(""" id: sample handler: ansible version: 1.0.0 input: ip: schema: str! value: port: schema: int! value: """) receiver_meta_dir = self.make_resource_meta(""" id: receiver handler: ansible version: 1.0.0 input: server: schema: [{ip: str!, port: int!}] """) sample1 = self.create_resource( 'sample1', sample_meta_dir, args={'ip': '10.0.0.1', 'port': 5000} ) receiver = self.create_resource( 'receiver', receiver_meta_dir ) xs.connect(sample1, receiver, mapping={'ip': 'server:ip', 'port': 'server:port'}) self.assertItemsEqual( [{'ip': sample1.args['ip'], 'port': sample1.args['port']}], receiver.args['server'], ) sample2 = self.create_resource( 'sample2', sample_meta_dir, args={'ip': '10.0.0.2', 'port': 5001} ) xs.connect(sample2, receiver, mapping={'ip': 'server:ip', 'port': 'server:port'}) self.assertItemsEqual( [{'ip': sample1.args['ip'], 'port': sample1.args['port']}, {'ip': sample2.args['ip'], 'port': sample2.args['port']}], receiver.args['server'], ) xs.disconnect(sample1, receiver) self.assertItemsEqual( [{'ip': sample2.args['ip'], 'port': sample2.args['port']}], receiver.args['server'], )
def test_backtrack_dict(self): sample_meta_dir = self.make_resource_meta(""" id: sample handler: ansible version: 1.0.0 input: value: schema: str! value: """) sample_dict_meta_dir = self.make_resource_meta(""" id: sample_dict handler: ansible version: 1.0.0 input: value: schema: {a: str!} value: """) sample_dict = self.create_resource( 'sample_dict', sample_dict_meta_dir ) vi = sample_dict.resource_inputs()['value'] sample1 = self.create_resource( 'sample1', sample_meta_dir, {'value': 'x'} ) sample2 = self.create_resource( 'sample2', sample_meta_dir, {'value': 'z'} ) self.assertEqual(vi.backtrack_value_emitter(), vi) # {a: sample1} -> sample_dict signals.connect(sample1, sample_dict, {'value': 'value:a'}) self.assertDictEqual(vi.backtrack_value_emitter(), {'a': sample1.resource_inputs()['value']}) # {a: sample2} -> sample_dict signals.connect(sample2, sample_dict, {'value': 'value:a'}) self.assertDictEqual(vi.backtrack_value_emitter(), {'a': sample2.resource_inputs()['value']}) # sample2 disconnected signals.disconnect(sample2, sample_dict) self.assertEqual(vi.backtrack_value_emitter(), vi)
def test_revert_update_connected(): res1 = orm.DBResource(id='test1', name='test1', base_path='x') res1.save() res1.add_input('a', 'str', '9') res2 = orm.DBResource(id='test2', name='test2', base_path='x') res2.save() res2.add_input('a', 'str', 0) res3 = orm.DBResource(id='test3', name='test3', base_path='x') res3.save() res3.add_input('a', 'str', 0) res1 = resource.load('test1') res2 = resource.load('test2') res3 = resource.load('test3') signals.connect(res1, res2) signals.connect(res2, res3) staged_log = change.stage_changes() assert len(staged_log) == 3 for item in staged_log: operations.move_to_commited(item.log_action) assert len(staged_log) == 0 signals.disconnect(res1, res2) staged_log = change.stage_changes() assert len(staged_log) == 2 to_revert = [] for item in staged_log: operations.move_to_commited(item.log_action) to_revert.append(item.uid) change.revert_uids(sorted(to_revert, reverse=True)) staged_log = change.stage_changes() assert len(staged_log) == 2 for item in staged_log: assert item.diff == [['change', 'a', [0, '9']]]
def test_backtrack_simple(self): sample_meta_dir = self.make_resource_meta(""" id: sample handler: ansible version: 1.0.0 input: value: schema: str! value: """) sample1 = self.create_resource( 'sample1', sample_meta_dir, {'value': 'x'} ) sample2 = self.create_resource( 'sample2', sample_meta_dir, {'value': 'y'} ) sample3 = self.create_resource( 'sample3', sample_meta_dir, {'value': 'z'} ) vi = sample2.resource_inputs()['value'] self.assertEqual(vi.backtrack_value_emitter(), vi) # sample1 -> sample2 signals.connect(sample1, sample2) self.assertEqual(vi.backtrack_value_emitter(), sample1.resource_inputs()['value']) # sample3 -> sample1 -> sample2 signals.connect(sample3, sample1) self.assertEqual(vi.backtrack_value_emitter(), sample3.resource_inputs()['value']) # sample2 disconnected signals.disconnect(sample1, sample2) self.assertEqual(vi.backtrack_value_emitter(), vi)
def _update_inputs_connections(res_obj, args, old_connections, new_connections): res_obj.update(args) removed = [] for item in old_connections: if item not in new_connections: removed.append(item) added = [] for item in new_connections: if item not in old_connections: added.append(item) for emitter, _, receiver, _ in removed: emmiter_obj = resource.load(emitter) receiver_obj = resource.load(receiver) signals.disconnect(emmiter_obj, receiver_obj) for emitter, emitter_input, receiver, receiver_input in added: emmiter_obj = resource.load(emitter) receiver_obj = resource.load(receiver) signals.connect(emmiter_obj, receiver_obj, {emitter_input: receiver_input})
def test_list_input_single(self): sample_meta_dir = self.make_resource_meta(""" id: sample handler: ansible version: 1.0.0 input: ip: schema: str value: """) list_input_single_meta_dir = self.make_resource_meta(""" id: list-input-single handler: ansible version: 1.0.0 input: ips: schema: [str] value: [] """) sample1 = self.create_resource('sample1', sample_meta_dir, {'ip': '10.0.0.1'}) sample2 = self.create_resource('sample2', sample_meta_dir, {'ip': '10.0.0.2'}) list_input_single = self.create_resource('list-input-single', list_input_single_meta_dir, {'ips': []}) xs.connect(sample1, list_input_single, mapping={'ip': 'ips'}) self.assertItemsEqual( #[ip['value'] for ip in list_input_single.args['ips']], list_input_single.args['ips'], [ sample1.args['ip'], ]) #self.assertListEqual( # [(e['emitter_attached_to'], e['emitter']) for e in list_input_single.args['ips']], # [(sample1.args['ip'].attached_to.name, 'ip')] #) xs.connect(sample2, list_input_single, mapping={'ip': 'ips'}) self.assertItemsEqual( #[ip['value'] for ip in list_input_single.args['ips']], list_input_single.args['ips'], [ sample1.args['ip'], sample2.args['ip'], ]) #self.assertListEqual( # [(e['emitter_attached_to'], e['emitter']) for e in list_input_single.args['ips']], # [(sample1.args['ip'].attached_to.name, 'ip'), # (sample2.args['ip'].attached_to.name, 'ip')] #) # Test update sample2.update({'ip': '10.0.0.3'}) self.assertItemsEqual( #[ip['value'] for ip in list_input_single.args['ips']], list_input_single.args['ips'], [ sample1.args['ip'], sample2.args['ip'], ]) # Test disconnect xs.disconnect(sample2, list_input_single) self.assertItemsEqual( #[ip['value'] for ip in list_input_single.args['ips']], list_input_single.args['ips'], [ sample1.args['ip'], ])
def test_list_input_multi(self): sample_meta_dir = self.make_resource_meta(""" id: sample handler: ansible version: 1.0.0 input: ip: schema: str value: port: schema: int value: """) list_input_multi_meta_dir = self.make_resource_meta(""" id: list-input-multi handler: ansible version: 1.0.0 input: ips: schema: [str] value: ports: schema: [int] value: """) sample1 = self.create_resource( 'sample1', sample_meta_dir, {'ip': '10.0.0.1', 'port': '1000'} ) sample2 = self.create_resource( 'sample2', sample_meta_dir, {'ip': '10.0.0.2', 'port': '1001'} ) list_input_multi = self.create_resource( 'list-input-multi', list_input_multi_meta_dir, {'ips': [], 'ports': []} ) xs.connect(sample1, list_input_multi, mapping={'ip': 'ips', 'port': 'ports'}) self.assertEqual( [ip['value'] for ip in list_input_multi.args['ips'].value], [sample1.args['ip']] ) self.assertEqual( [p['value'] for p in list_input_multi.args['ports'].value], [sample1.args['port']] ) xs.connect(sample2, list_input_multi, mapping={'ip': 'ips', 'port': 'ports'}) self.assertEqual( [ip['value'] for ip in list_input_multi.args['ips'].value], [ sample1.args['ip'], sample2.args['ip'], ] ) self.assertListEqual( [(e['emitter_attached_to'], e['emitter']) for e in list_input_multi.args['ips'].value], [(sample1.args['ip'].attached_to.name, 'ip'), (sample2.args['ip'].attached_to.name, 'ip')] ) self.assertEqual( [p['value'] for p in list_input_multi.args['ports'].value], [ sample1.args['port'], sample2.args['port'], ] ) self.assertListEqual( [(e['emitter_attached_to'], e['emitter']) for e in list_input_multi.args['ports'].value], [(sample1.args['port'].attached_to.name, 'port'), (sample2.args['port'].attached_to.name, 'port')] ) # Test disconnect xs.disconnect(sample2, list_input_multi) self.assertEqual( [ip['value'] for ip in list_input_multi.args['ips'].value], [sample1.args['ip']] ) self.assertEqual( [p['value'] for p in list_input_multi.args['ports'].value], [sample1.args['port']] )
def test_nested_list_input(self): """ Make sure that single input change is propagated along the chain of lists. """ sample_meta_dir = self.make_resource_meta(""" id: sample handler: ansible version: 1.0.0 input: ip: schema: str value: port: schema: int value: """) list_input_meta_dir = self.make_resource_meta(""" id: list-input handler: ansible version: 1.0.0 input: ips: schema: [str] value: [] ports: schema: [int] value: [] """) list_input_nested_meta_dir = self.make_resource_meta(""" id: list-input-nested handler: ansible version: 1.0.0 input: ipss: schema: [[str]] value: [] portss: schema: [[int]] value: [] """) sample1 = self.create_resource( 'sample1', sample_meta_dir, {'ip': '10.0.0.1', 'port': '1000'} ) sample2 = self.create_resource( 'sample2', sample_meta_dir, {'ip': '10.0.0.2', 'port': '1001'} ) list_input = self.create_resource( 'list-input', list_input_meta_dir, {} ) list_input_nested = self.create_resource( 'list-input-nested', list_input_nested_meta_dir, {} ) xs.connect(sample1, list_input, mapping={'ip': 'ips', 'port': 'ports'}) xs.connect(sample2, list_input, mapping={'ip': 'ips', 'port': 'ports'}) xs.connect(list_input, list_input_nested, mapping={'ips': 'ipss', 'ports': 'portss'}) self.assertListEqual( [ips['value'] for ips in list_input_nested.args['ipss'].value], [list_input.args['ips'].value] ) self.assertListEqual( [ps['value'] for ps in list_input_nested.args['portss'].value], [list_input.args['ports'].value] ) # Test disconnect xs.disconnect(sample1, list_input) self.assertListEqual( [[ip['value'] for ip in ips['value']] for ips in list_input_nested.args['ipss'].value], [[sample2.args['ip'].value]] ) self.assertListEqual( [[p['value'] for p in ps['value']] for ps in list_input_nested.args['portss'].value], [[sample2.args['port'].value]] )
def test_hash_input_with_list(self): sample_meta_dir = self.make_resource_meta(""" id: sample handler: ansible version: 1.0.0 input: ip: schema: str! value: port: schema: int! value: """) receiver_meta_dir = self.make_resource_meta(""" id: receiver handler: ansible version: 1.0.0 input: server: schema: [{ip: str!, port: int!}] """) sample1 = self.create_resource('sample1', sample_meta_dir, args={ 'ip': '10.0.0.1', 'port': 5000 }) receiver = self.create_resource('receiver', receiver_meta_dir) xs.connect(sample1, receiver, mapping={ 'ip': 'server:ip', 'port': 'server:port' }) self.assertItemsEqual( [{ 'ip': sample1.args['ip'], 'port': sample1.args['port'] }], receiver.args['server'], ) sample2 = self.create_resource('sample2', sample_meta_dir, args={ 'ip': '10.0.0.2', 'port': 5001 }) xs.connect(sample2, receiver, mapping={ 'ip': 'server:ip', 'port': 'server:port' }) self.assertItemsEqual( [{ 'ip': sample1.args['ip'], 'port': sample1.args['port'] }, { 'ip': sample2.args['ip'], 'port': sample2.args['port'] }], receiver.args['server'], ) xs.disconnect(sample1, receiver) self.assertItemsEqual( [{ 'ip': sample2.args['ip'], 'port': sample2.args['port'] }], receiver.args['server'], )
def test_nested_list_input(self): """ Make sure that single input change is propagated along the chain of lists. """ sample_meta_dir = self.make_resource_meta(""" id: sample handler: ansible version: 1.0.0 input: ip: schema: str value: port: schema: int value: """) list_input_meta_dir = self.make_resource_meta(""" id: list-input handler: ansible version: 1.0.0 input: ips: schema: [str] value: [] ports: schema: [int] value: [] """) list_input_nested_meta_dir = self.make_resource_meta(""" id: list-input-nested handler: ansible version: 1.0.0 input: ipss: schema: [[str]] value: [] portss: schema: [[int]] value: [] """) sample1 = self.create_resource('sample1', sample_meta_dir, { 'ip': '10.0.0.1', 'port': 1000 }) sample2 = self.create_resource('sample2', sample_meta_dir, { 'ip': '10.0.0.2', 'port': 1001 }) list_input = self.create_resource( 'list-input', list_input_meta_dir, ) list_input_nested = self.create_resource( 'list-input-nested', list_input_nested_meta_dir, ) xs.connect(sample1, list_input, mapping={'ip': 'ips', 'port': 'ports'}) xs.connect(sample2, list_input, mapping={'ip': 'ips', 'port': 'ports'}) xs.connect(list_input, list_input_nested, mapping={ 'ips': 'ipss', 'ports': 'portss' }) self.assertListEqual( #[ips['value'] for ips in list_input_nested.args['ipss']], list_input_nested.args['ipss'], [list_input.args['ips']]) self.assertListEqual( #[ps['value'] for ps in list_input_nested.args['portss']], list_input_nested.args['portss'], [list_input.args['ports']]) # Test disconnect xs.disconnect(sample1, list_input) self.assertListEqual( #[[ip['value'] for ip in ips['value']] for ips in list_input_nested.args['ipss']], list_input_nested.args['ipss'], [[sample2.args['ip']]]) self.assertListEqual(list_input_nested.args['portss'], [[sample2.args['port']]])
def test_list_input_multi(self): sample_meta_dir = self.make_resource_meta(""" id: sample handler: ansible version: 1.0.0 input: ip: schema: str value: port: schema: int value: """) list_input_multi_meta_dir = self.make_resource_meta(""" id: list-input-multi handler: ansible version: 1.0.0 input: ips: schema: [str] value: ports: schema: [int] value: """) sample1 = self.create_resource('sample1', sample_meta_dir, { 'ip': '10.0.0.1', 'port': 1000 }) sample2 = self.create_resource('sample2', sample_meta_dir, { 'ip': '10.0.0.2', 'port': 1001 }) list_input_multi = self.create_resource('list-input-multi', list_input_multi_meta_dir, args={ 'ips': [], 'ports': [] }) xs.connect(sample1, list_input_multi, mapping={ 'ip': 'ips', 'port': 'ports' }) self.assertItemsEqual( #[ip['value'] for ip in list_input_multi.args['ips']], list_input_multi.args['ips'], [sample1.args['ip']]) self.assertItemsEqual( #[p['value'] for p in list_input_multi.args['ports']], list_input_multi.args['ports'], [sample1.args['port']]) xs.connect(sample2, list_input_multi, mapping={ 'ip': 'ips', 'port': 'ports' }) self.assertItemsEqual( #[ip['value'] for ip in list_input_multi.args['ips']], list_input_multi.args['ips'], [ sample1.args['ip'], sample2.args['ip'], ]) #self.assertListEqual( # [(e['emitter_attached_to'], e['emitter']) for e in list_input_multi.args['ips']], # [(sample1.args['ip'].attached_to.name, 'ip'), # (sample2.args['ip'].attached_to.name, 'ip')] #) self.assertItemsEqual( #[p['value'] for p in list_input_multi.args['ports']], list_input_multi.args['ports'], [ sample1.args['port'], sample2.args['port'], ]) #self.assertListEqual( # [(e['emitter_attached_to'], e['emitter']) for e in list_input_multi.args['ports']], # [(sample1.args['port'].attached_to.name, 'port'), # (sample2.args['port'].attached_to.name, 'port')] #) # Test disconnect xs.disconnect(sample2, list_input_multi) self.assertItemsEqual( #[ip['value'] for ip in list_input_multi.args['ips']], list_input_multi.args['ips'], [sample1.args['ip']]) self.assertItemsEqual( #[p['value'] for p in list_input_multi.args['ports']], list_input_multi.args['ports'], [sample1.args['port']])
def test_list_input_single(self): sample_meta_dir = self.make_resource_meta(""" id: sample handler: ansible version: 1.0.0 input: ip: schema: str value: """) list_input_single_meta_dir = self.make_resource_meta(""" id: list-input-single handler: ansible version: 1.0.0 input: ips: schema: [str] value: [] """) sample1 = self.create_resource( 'sample1', sample_meta_dir, {'ip': '10.0.0.1'} ) sample2 = self.create_resource( 'sample2', sample_meta_dir, {'ip': '10.0.0.2'} ) list_input_single = self.create_resource( 'list-input-single', list_input_single_meta_dir, {'ips': []} ) xs.connect(sample1, list_input_single, mapping={'ip': 'ips'}) self.assertEqual( [ip['value'] for ip in list_input_single.args['ips'].value], [ sample1.args['ip'], ] ) self.assertListEqual( [(e['emitter_attached_to'], e['emitter']) for e in list_input_single.args['ips'].value], [(sample1.args['ip'].attached_to.name, 'ip')] ) xs.connect(sample2, list_input_single, mapping={'ip': 'ips'}) self.assertEqual( [ip['value'] for ip in list_input_single.args['ips'].value], [ sample1.args['ip'], sample2.args['ip'], ] ) self.assertListEqual( [(e['emitter_attached_to'], e['emitter']) for e in list_input_single.args['ips'].value], [(sample1.args['ip'].attached_to.name, 'ip'), (sample2.args['ip'].attached_to.name, 'ip')] ) # Test update sample2.update({'ip': '10.0.0.3'}) self.assertEqual( [ip['value'] for ip in list_input_single.args['ips'].value], [ sample1.args['ip'], sample2.args['ip'], ] ) # Test disconnect xs.disconnect(sample2, list_input_single) self.assertEqual( [ip['value'] for ip in list_input_single.args['ips'].value], [ sample1.args['ip'], ] ) self.assertListEqual( [(e['emitter_attached_to'], e['emitter']) for e in list_input_single.args['ips'].value], [(sample1.args['ip'].attached_to.name, 'ip')] )