Exemplo n.º 1
0
class TestLogResource(LockBypassMixin, OneNodeMixin, ValidateResponseMixin,
                      TestCase):
    def fill_database(self):
        """Create and configure a new app instance for each test."""
        self.dest1 = Server('dest1', port=8000)
        self.dest2 = Server('dest2', port=8000)
        self.log = Log(source_server=self.s1,
                       target='/var/log/log1.log',
                       destination_server=self.dest1)
        db.session.add_all([self.dest1, self.dest2, self.log])

    def test_get(self):
        resp = self.client.get(url_for('api_1_0.logresource',
                                       log_id=str(self.log.id)),
                               headers=self.auth.header)
        self.assertEqual(200, resp.status_code)

        self.assertEqual(self.log.to_json(), resp.get_json())

        resp = self.client.get(url_for('api_1_0.logresource', log_id='aaaa'),
                               headers=self.auth.header)
        self.assertEqual(404, resp.status_code)

    def test_patch(self):
        patch_log_json = {
            "src_server_id": str(self.dest2.id),
        }

        resp = self.client.patch(url_for('api_1_0.logresource',
                                         log_id=str(self.log.id)),
                                 headers=self.auth.header,
                                 json=patch_log_json)
        self.assertEqual(400, resp.status_code)

        patch_log_json = {"dest_folder": '/dest'}

        self.assertIsNone(self.log.dest_folder)
        resp = self.client.patch(url_for('api_1_0.logresource',
                                         log_id=str(self.log.id)),
                                 headers=self.auth.header,
                                 json=patch_log_json)
        self.validate_error_response(
            resp,
            errors.InvalidValue(
                "property dest_folder can not be set with REPO_MIRROR mode"))

        patch_log_json = {"dest_folder": '/dest', 'mode': 'FOLDER'}
        resp = self.client.patch(url_for('api_1_0.logresource',
                                         log_id=str(self.log.id)),
                                 headers=self.auth.header,
                                 json=patch_log_json)
        self.assertEqual(204, resp.status_code)
        db.session.refresh(self.log)
        self.assertEqual('/dest', self.log.dest_folder)

        resp = self.client.patch(url_for('api_1_0.logresource',
                                         log_id=str(self.log.id)),
                                 headers=self.auth.header,
                                 json={"dest_folder": '/dest'})
        self.assertEqual(202, resp.status_code)
Exemplo n.º 2
0
    def test_get(self):
        resp = self.client.get(url_for('api_1_0.loglist'),
                               headers=self.auth.header)
        self.assertListEqual([self.log.to_json()], resp.get_json())

        log = Log(source_server=Server.get_current(),
                  target='/access.log',
                  destination_server=self.dest2)
        db.session.add(log)
        db.session.commit()

        # test with filter
        resp = self.client.get(url_for('api_1_0.loglist') +
                               "?filter[target]=/access.log",
                               headers=self.auth.header)
        self.assertListEqual([log.to_json()], resp.get_json())

        # test with filter on a server
        resp = self.client.get(url_for('api_1_0.loglist') +
                               f"?filter[dst_server_id]={self.dest1.id}",
                               headers=self.auth.header)
        self.assertListEqual([self.log.to_json()], resp.get_json())
Exemplo n.º 3
0
class TestLogList(LockBypassMixin, OneNodeMixin, TestCase):
    initials = dict(server=False, action_template=False)

    def fill_database(self):
        self.dest1 = Server('dest1', port=8000)
        self.dest2 = Server('dest2', port=8000)
        self.log = Log(source_server=self.s1,
                       target='/var/log/log1.log',
                       destination_server=self.dest1)
        db.session.add_all([self.dest1, self.dest2, self.log])
        db.session.commit()

    def test_get(self):
        resp = self.client.get(url_for('api_1_0.loglist'),
                               headers=self.auth.header)
        self.assertListEqual([self.log.to_json()], resp.get_json())

        log = Log(source_server=Server.get_current(),
                  target='/access.log',
                  destination_server=self.dest2)
        db.session.add(log)
        db.session.commit()

        # test with filter
        resp = self.client.get(url_for('api_1_0.loglist') +
                               "?filter[target]=/access.log",
                               headers=self.auth.header)
        self.assertListEqual([log.to_json()], resp.get_json())

        # test with filter on a server
        resp = self.client.get(url_for('api_1_0.loglist') +
                               f"?filter[dst_server_id]={self.dest1.id}",
                               headers=self.auth.header)
        self.assertListEqual([self.log.to_json()], resp.get_json())

    def test_post(self):
        new_log_json = {
            "src_server_id": str(self.dest1.id),
            "target": '/var/log',
            "exclude": 'system.log'
        }

        resp = self.client.post(url_for('api_1_0.loglist'),
                                headers=self.auth.header,
                                json=new_log_json)
        self.assertEqual(400, resp.status_code)

        new_log_json = {
            "src_server_id": str(self.dest1.id),
            "target": '/var/log',
            "exclude": 'system.log',
            "dst_server_id": str(self.dest1.id)
        }

        resp = self.client.post(url_for('api_1_0.loglist'),
                                headers=self.auth.header,
                                json=new_log_json)
        self.assertEqual(400, resp.status_code)
        self.assertDictEqual(
            {'error': 'source and destination must be different'},
            resp.get_json())

        new_log_json = {
            "src_server_id": str(self.dest1.id),
            "target": '/var/log',
            "exclude": 'system.log',
            "dst_server_id": str(self.dest2.id)
        }

        resp = self.client.post(url_for('api_1_0.loglist'),
                                headers=self.auth.header,
                                json=new_log_json)
        self.assertEqual(201, resp.status_code)
        log = Log.query.get(resp.get_json().get('id'))
        self.assertEqual(self.dest1, log.source_server)
        self.assertEqual('/var/log', log.target)
        self.assertIsNone(log.include)
        self.assertEqual('system.log', log.exclude)
        self.assertEqual(self.dest2, log.destination_server)
        self.assertFalse(log.recursive)
        self.assertIsNone(log.dest_folder)
Exemplo n.º 4
0
    def test_to_from_json(self):
        db.session.add(self.src)
        db.session.add(self.dst)
        self.src.id = '11111111-2222-3333-4444-555555550001'
        self.dst.id = '11111111-2222-3333-4444-555555550002'
        db.session.commit()

        l = Log(source_server=self.src, target='/home/dimensigon/dimensigon/dimensigon.log',
                destination_server=self.dst,
                dest_folder='/home/dimensigon/dimensigon-node3', id='11111111-2222-3333-4444-111111110001')

        l_json = l.to_json()
        self.assertDictEqual(
            dict(id='11111111-2222-3333-4444-111111110001',
                 src_server_id='11111111-2222-3333-4444-555555550001',
                 target='/home/dimensigon/dimensigon/dimensigon.log',
                 include=None,
                 exclude=None, dst_server_id='11111111-2222-3333-4444-555555550002',
                 dest_folder='/home/dimensigon/dimensigon-node3',
                 recursive=False,
                 mode='FOLDER',
                 deleted=False,
                 _old_target=None), l_json)


        smashed = Log.from_json(l_json)

        self.assertEqual(smashed.source_server, self.src)
        self.assertEqual(smashed.destination_server, self.dst)
        self.assertIsNone(smashed.include)
        self.assertIsNone(smashed.exclude)
        self.assertEqual(smashed.dest_folder, '/home/dimensigon/dimensigon-node3')
        self.assertEqual(smashed.target, '/home/dimensigon/dimensigon/dimensigon.log')
        self.assertFalse(smashed.recursive)
        self.assertFalse(smashed.deleted)
        self.assertEqual(Mode.FOLDER, smashed.mode)
        self.assertIsNone(smashed._old_target)

        l_json = l.to_json(human=True)
        l_json.pop('last_modified_at')
        self.assertDictEqual(
            dict(id='11111111-2222-3333-4444-111111110001',
                 src_server=self.src.name, target='/home/dimensigon/dimensigon/dimensigon.log',
                 include=None,
                 exclude=None, dst_server=self.dst.name,
                 dest_folder='/home/dimensigon/dimensigon-node3',
                 recursive=False,
                 mode='FOLDER',
                 deleted=False,
                 _old_target=None), l_json)

        l_json = l.to_json(human=True, no_delete=True)
        l_json.pop('last_modified_at')
        self.assertDictEqual(
            dict(id='11111111-2222-3333-4444-111111110001',
                 src_server=self.src.name, target='/home/dimensigon/dimensigon/dimensigon.log',
                 include=None,
                 exclude=None, dst_server=self.dst.name,
                 dest_folder='/home/dimensigon/dimensigon-node3',
                 recursive=False,
                 mode='FOLDER'
                 ), l_json)