Exemplo n.º 1
0
def test_revert_removed_child():
    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)

    res1 = resource.load('test1')
    res2 = resource.load('test2')
    signals.connect(res1, res2)

    staged_log = change.stage_changes()
    assert len(staged_log) == 2
    for item in staged_log:
        operations.move_to_commited(item.log_action)
    res2.remove()

    staged_log = change.stage_changes()
    assert len(staged_log) == 1
    logitem = next(staged_log.collection())
    operations.move_to_commited(logitem.log_action)

    with mock.patch.object(resource, 'read_meta') as mread:
        mread.return_value = {'input': {'a': {'schema': 'str!'}}}
        change.revert(logitem.uid)

    res2 = resource.load('test2')
    assert res2.args == {'a': '9'}
Exemplo n.º 2
0
def test_revert_removal():
    res = orm.DBResource(id='test1', name='test1', base_path='x')
    res.save()
    res.add_input('a', 'str', '9')
    res.add_input('location_id', 'str', '1')
    res.add_input('transports_id', 'str', '1')

    commited = orm.DBCommitedState.get_or_create('test1')
    commited.inputs = {'a': '9', 'location_id': '1', 'transports_id': '1'}
    commited.save()

    logitem =change.create_logitem(
        res.name, 'remove', change.create_diff({}, {'a': '9'}), [],
        base_path=res.base_path)
    log = data.SL()
    log.append(logitem)
    resource_obj = resource.load(res.name)
    resource_obj.remove()
    operations.move_to_commited(logitem.log_action)

    resources = orm.DBResource.load_all()

    assert resources == []
    assert logitem.diff == [('remove', '', [('a', '9')])]

    with mock.patch.object(resource, 'read_meta') as mread:
        mread.return_value = {'input': {'a': {'schema': 'str!'}}, 'id': 'mocked'}
        change.revert(logitem.uid)
    resource_obj = resource.load('test1')
    assert resource_obj.args == {'a': '9', 'location_id': '1', 'transports_id': '1'}
Exemplo n.º 3
0
def test_revert_update():
    commit = {'a': '10'}
    previous = {'a': '9'}
    res = orm.DBResource(id='test1', name='test1', base_path='x')
    res.save()
    res.add_input('a', 'str', '9')
    action = 'update'

    resource_obj = resource.load(res.name)

    assert resource_obj.args == previous

    log = data.SL()
    logitem =change.create_logitem(
        res.name, action, change.create_diff(commit, previous), [],
        base_path=res.base_path)
    log.append(logitem)
    resource_obj.update(commit)
    operations.move_to_commited(logitem.log_action)

    assert logitem.diff == [('change', 'a', ('9', '10'))]
    assert resource_obj.args == commit

    change.revert(logitem.uid)
    assert resource_obj.args == previous
Exemplo n.º 4
0
def test_discard_all_pending_changes_resources_created():
    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)

    staged_log = change.stage_changes()
    assert len(staged_log) == 2

    change.discard_all()
    staged_log = change.stage_changes()
    assert len(staged_log) == 0
    assert orm.DBResource.load_all() == []
Exemplo n.º 5
0
def test_remove_events(events_example):
    r = orm.DBResource(id='e1', name='e1', base_path='x')
    r.save()
    to_be_removed = events_example[2]
    evapi.add_events('e1', events_example)
    evapi.remove_event(to_be_removed)
    assert to_be_removed not in evapi.all_events('e1')
Exemplo n.º 6
0
def test_set_events(events_example):
    r = orm.DBResource(id='e1', name='e1', base_path='x')
    r.save()
    partial = events_example[:2]
    evapi.add_events('e1', events_example[:2])
    evapi.set_events('e1', events_example[2:])
    assert evapi.all_events('e1') == events_example[2:]
Exemplo n.º 7
0
    def test_save(self):
        r = orm.DBResource(id='test1', name='test1', base_path='x')
        r.save()

        rr = resource.load(r.id)

        self.assertEqual(r, rr.db_obj)
Exemplo n.º 8
0
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']]]
Exemplo n.º 9
0
def test_discard_connection():
    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')

    staged_log = change.stage_changes()
    for item in staged_log:
        operations.move_to_commited(item.log_action)

    res1 = resource.load('test1')
    res2 = resource.load('test2')
    signals.connect(res1, res2)
    staged_log = change.stage_changes()
    assert len(staged_log) == 1
    assert res2.args == {'a': '9'}
    change.discard_all()
    assert res2.args == {'a': '0'}
    assert len(change.stage_changes()) == 0
Exemplo n.º 10
0
def test_discard_update():
    res1 = orm.DBResource(id='test1', name='test1', base_path='x')
    res1.save()
    res1.add_input('a', 'str', '9')
    staged_log = change.stage_changes()
    for item in staged_log:
        operations.move_to_commited(item.log_action)
    res1 = resource.load('test1')
    res1.update({'a': '11'})
    assert len(change.stage_changes()) == 1
    assert res1.args == {'a': '11'}

    change.discard_all()
    assert res1.args == {'a': '9'}
Exemplo n.º 11
0
def test_discard_removed():
    res1 = orm.DBResource(id='test1', name='test1', base_path='x')
    res1.save()
    res1.add_input('a', 'str', '9')
    staged_log = change.stage_changes()
    for item in staged_log:
        operations.move_to_commited(item.log_action)
    res1 = resource.load('test1')
    res1.remove()
    assert len(change.stage_changes()) == 1
    assert res1.to_be_removed()

    change.discard_all()

    assert len(change.stage_changes()) == 0
    assert not resource.load('test1').to_be_removed()
Exemplo n.º 12
0
    def __init__(self,
                 name,
                 base_path,
                 args=None,
                 tags=None,
                 virtual_resource=None):
        args = args or {}
        self.name = name
        if base_path:
            metadata = read_meta(base_path)
        else:
            metadata = deepcopy(self._metadata)

        self.base_path = base_path

        if tags is None:
            tags = []
        m_tags = metadata.get('tags', [])
        tags.extend(m_tags)
        tags.append('resource={}'.format(metadata['id']))

        self.virtual_resource = virtual_resource

        inputs = metadata.get('input', {})

        self.auto_extend_inputs(inputs)

        self.db_obj = orm.DBResource(
            **{
                'id': name,
                'name': name,
                'actions_path': metadata.get('actions_path', ''),
                'base_name': metadata.get('base_name', ''),
                'base_path': metadata.get('base_path', ''),
                'handler': metadata.get('handler', ''),
                'puppet_module': metadata.get('puppet_module', ''),
                'version': metadata.get('version', ''),
                'meta_inputs': inputs,
                'tags': tags
            })
        self.db_obj.state = RESOURCE_STATE.created.name
        self.db_obj.tags = tags or []
        self.db_obj.save()

        self.create_inputs(args)
Exemplo n.º 13
0
def test_revert_create():
    res = orm.DBResource(id='test1', name='test1', base_path='x')
    res.save()
    res.add_input('a', 'str', '9')

    staged_log = change.stage_changes()
    assert len(staged_log) == 1
    logitem = next(staged_log.collection())

    operations.move_to_commited(logitem.log_action)
    assert logitem.diff == [['add', '', [['a', '9']]]]

    commited = orm.DBCommitedState.load('test1')
    assert commited.inputs == {'a': '9'}

    change.revert(logitem.uid)

    staged_log = change.stage_changes()
    assert len(staged_log) == 1
    for item in staged_log:
        operations.move_to_commited(item.log_action)
    assert orm.DBResource.load_all() == []
Exemplo n.º 14
0
def test_single_event(events_example):
    r = orm.DBResource(id='e1', name='e1', base_path='x')
    r.save()
    evapi.add_events('e1', events_example[:2])
    evapi.add_event(events_example[2])
    assert set(evapi.all_events('e1')) == set(events_example)
Exemplo n.º 15
0
    def test_delete_resource(self):
        r = orm.DBResource(id='test1', name='test1', base_path='x')
        r.save()

        r.add_input('ip', 'str!', '10.0.0.2')
Exemplo n.º 16
0
    def test_add_input(self):
        r = orm.DBResource(id='test1', name='test1', base_path='x')
        r.save()

        r.add_input('ip', 'str!', '10.0.0.2')
        self.assertEqual(len(r.inputs.as_set()), 1)