def test_apply_with_no_new_value():
    foo = Foo()
    foo.bar = 1
    a = RawDataApplier({}, foo)
    assert not a.errors
    a.apply()
    assert foo.bar == 1
Exemplo n.º 2
0
def test_activate_compute():
    shutil.copy(os.path.join(opennode.knot.tests.__path__[0], 'u1.xml'),
                '/tmp/salt_vm_test_state.xml')

    compute = make_compute(hostname=u'vm1', state=u'inactive')
    compute.__name__ = '4dea22b31d52d8f32516782e98ab3fa0'

    dom0 = make_compute(hostname=u'localhost', state=u'active')
    dom0.__name__ = 'f907e3553a8c4cc5a6db1790b65f93f8'
    alsoProvides(dom0, ISaltInstalled)

    container = VirtualizationContainer('test')
    container.__parent__ = dom0
    compute.__parent__ = container

    assert compute.state == 'inactive'
    compute.state = u'inactive'

    form = RawDataApplier({'state': u'active'}, compute)
    print form.errors
    form.apply()

    root = ElementTree.parse('/tmp/salt_vm_test_state.xml')
    for node in root.findall('domain'):
        if node.find('name').text == 'vm1':
            assert node.attrib['state'] == 'inactive'
            return
    assert False
Exemplo n.º 3
0
def test_activate_compute():
    shutil.copy(os.path.join(opennode.knot.tests.__path__[0], 'u1.xml'), '/tmp/salt_vm_test_state.xml')

    compute = make_compute(hostname=u'vm1', state=u'inactive')
    compute.__name__ = '4dea22b31d52d8f32516782e98ab3fa0'

    dom0 = make_compute(hostname=u'localhost', state=u'active')
    dom0.__name__ = 'f907e3553a8c4cc5a6db1790b65f93f8'
    alsoProvides(dom0, ISaltInstalled)

    container = VirtualizationContainer('test')
    container.__parent__ = dom0
    compute.__parent__ = container

    assert compute.state == 'inactive'
    compute.state = u'inactive'

    form = RawDataApplier({'state': u'active'}, compute)
    print form.errors
    form.apply()

    root = ElementTree.parse('/tmp/salt_vm_test_state.xml')
    for node in root.findall('domain'):
        if node.find('name').text == 'vm1':
            assert node.attrib['state'] == 'inactive'
            return
    assert False
Exemplo n.º 4
0
        def apply():
            obj = self.traverse(args.path)
            raw_data = args.keywords

            if args.verbose:
                for key, value in raw_data.items():
                    self.write("Setting %s=%s\n" % (key, value))

            principals = map(lambda p: p.id, effective_principals(self.user))

            if 'admins' in principals:
                setattr(obj, '_admin_access', True)
            elif getattr(obj, '_admin_access', False):
                log.msg('WARNING: admin access in SetAttrCmd when accessed by non-admin (%s: %s)!'
                        % (self.user, principals), system='set-cmd')

            try:
                form = RawDataApplier(raw_data, obj)

                if not form.errors:
                    form.apply()
                else:
                    form.write_errors(to=self)
            finally:
                if hasattr(obj, '_admin_access'):
                    delattr(obj, '_admin_access')
Exemplo n.º 5
0
    def render_PUT(self, request):
        data = json.load(request.content)
        if 'id' in data:
            del data['id']

        data = self.put_filter_attributes(request, data)

        form = RawDataApplier(data, self.context)
        if not form.errors:
            form.apply()
            return [IHttpRestView(self.context).render_recursive(request, depth=0)]
        else:
            request.setResponseCode(BadRequest.status_code)
            return form.error_dict()
def test_handler():
    modified = []

    @adapter(IFoo, IModelModifiedEvent)
    def fooModified(foo, event):
        modified.append(event.modified)

    provideHandler(fooModified)

    foo = Foo()
    a = RawDataApplier({'bar': '2'}, foo)
    a.apply()

    assert modified[0] == {'bar': 2}
Exemplo n.º 7
0
        def apply():
            obj = self.traverse(args.path)
            raw_data = args.keywords

            if args.verbose:
                for key, value in raw_data.items():
                    self.write("Setting %s=%s\n" % (key, value))

            form = RawDataApplier(raw_data, obj)

            if not form.errors:
                form.apply()
            else:
                form.write_errors(to=self)
Exemplo n.º 8
0
    def render_PUT(self, request):
        data = json.load(request.content)
        if 'id' in data:
            del data['id']

        data = self.put_filter_attributes(request, data)

        form = RawDataApplier(data, self.context)
        if not form.errors:
            form.apply()
            return [
                IHttpRestView(self.context).render_recursive(request, depth=0)
            ]
        else:
            request.setResponseCode(BadRequest.status_code)
            return form.error_dict()
Exemplo n.º 9
0
    def _save(self, args, old, updated):
        obj = self.traverse(args.path)

        if old == updated:
            self.write("No changes\n")
        else:
            raw_data = {}
            for line in updated.split('\n'):
                line = line.strip()
                if line and not line.startswith('#'):
                    key, value = line.split('=', 1)
                    raw_data[key.strip()] = value.strip()
            form = RawDataApplier(raw_data, obj)
            if not form.errors:
                form.apply()
            else:
                form.write_errors(to=self)
Exemplo n.º 10
0
        def apply():
            obj = self.traverse(args.path)
            raw_data = args.keywords

            if args.verbose:
                for key, value in raw_data.items():
                    self.write("Setting %s=%s\n" % (key, value))

            principals = map(lambda p: p.id, effective_principals(self.user))

            if 'admins' in principals:
                setattr(obj, '_admin_access', True)
            elif getattr(obj, '_admin_access', False):
                log.msg(
                    'WARNING: admin access in SetAttrCmd when accessed by non-admin (%s: %s)!'
                    % (self.user, principals),
                    system='set-cmd')

            try:
                form = RawDataApplier(raw_data, obj)

                if not form.errors:
                    form.apply()
                else:
                    form.write_errors(to=self)
            finally:
                if hasattr(obj, '_admin_access'):
                    delattr(obj, '_admin_access')
Exemplo n.º 11
0
    def _save(self, args, old, updated):
        obj = self.traverse(args.path)

        if old == updated:
            self.write("No changes\n")
        else:
            raw_data = {}
            for line in updated.split('\n'):
                line = line.strip()
                if line and not line.startswith('#'):
                    key, value = line.split('=', 1)
                    raw_data[key.strip()] = value.strip()
            form = RawDataApplier(raw_data, obj)
            if not form.errors:
                form.apply()
            else:
                form.write_errors(to=self)
def test_apply_with_valid_new_value():
    foo = Foo()
    a = RawDataApplier({'bar': '2'}, obj=foo)
    a.apply()
    assert foo.bar == 2