Beispiel #1
0
 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()
Beispiel #2
0
 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])
Beispiel #3
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)
Beispiel #4
0
    def test_delete(self):
        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.delete()

        self.assertEqual('/home/dimensigon/dimensigon/dimensigon.log', l._old_target)
        self.assertNotEqual('/home/dimensigon/dimensigon/dimensigon.log', l.target)
        self.assertTrue(l.deleted)

        db.session.add(l)
        db.session.commit()

        log_list = l.query.all()
        self.assertEqual(0, len(log_list))
        l = db.session.query(Log).filter_by(id='11111111-2222-3333-4444-111111110001').one()
Beispiel #5
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())
Beispiel #6
0
 def post(self):
     data = request.get_json()
     source_server = Server.query.get_or_raise(data.pop('src_server_id'))
     destination_server = Server.query.get_or_raise(data.pop('dst_server_id'))
     if source_server == destination_server:
         return {'error': 'source and destination must be different'}, 400
     if 'mode' in data:
         data['mode'] = Mode[data['mode']]
         if data['mode'] == Mode.FOLDER:
             if 'dest_folder' not in data:
                 raise errors.ParameterMustBeSet("property 'dest_folder' must be set when mode=FOLDER")
     log = Log(source_server=source_server, destination_server=destination_server, **data)
     db.session.add(log)
     db.session.commit()
     return {'id': str(log.id)}, 201
    def test_log_sender_file_no_dest_folder(self, mock_pb_rl, mock_pt_uof, mock_isfile, m):
        def callback(url, **kwargs):
            self.assertDictEqual(
                {"file": '/var/log/dimensigon.log', 'data': base64.b64encode('content'.encode()).decode('ascii')},
                kwargs['json'])
            return CallbackResult('POST', status=200)

        m.post(self.dest.url('api_1_0.logresource', log_id='aaaaaaaa-1234-5678-1234-56781234aaa1'), callback=callback)

        mock_isfile.return_value = True

        log = Log(id='aaaaaaaa-1234-5678-1234-56781234aaa1', source_server=self.source,
                  target='/var/log/dimensigon.log',
                  destination_server=self.dest, dest_folder=None)
        db.session.add(log)

        run(self.log_sender.send_new_data())

        mock_isfile.assert_called_once()
        mock_pb_rl.assert_called_once()
        mock_pt_uof.assert_called_once()
    def test_log_sender_folder(self, mock_pb_rl, mock_pt_uof, mock_isfile, mock_walk, mock_post, m):

        def callback(url, **kwargs):
            if kwargs['json']['file'] == '/dimensigon/logs/log1':
                self.assertDictEqual(
                    {"file": '/dimensigon/logs/log1', 'data': base64.b64encode('content1'.encode()).decode('ascii')},
                    kwargs['json'])
                return CallbackResult('POST', payload={'offset': 8}, status=200)
            elif kwargs['json']['file'] == '/dimensigon/logs/dir1/log2':
                self.assertDictEqual(
                    {"file": '/dimensigon/logs/dir1/log2',
                     'data': base64.b64encode('newcontent2'.encode()).decode('ascii')},
                    kwargs['json'])
                return CallbackResult('POST', payload={'offset': 11}, status=200)
            else:
                raise

        mock_post.side_effect = [({'offset': 8}, 200), ({'offset': 11}, 200), ({'offset': 8}, 200)]

        m.post(self.dest.url('api_1_0.logresource', log_id='aaaaaaaa-1234-5678-1234-56781234aaa1'), callback=callback)
        m.post(self.dest.url('api_1_0.logresource', log_id='aaaaaaaa-1234-5678-1234-56781234aaa1'), callback=callback)
        m.post(self.dest.url('api_1_0.logresource', log_id='aaaaaaaa-1234-5678-1234-56781234aaa1'), callback=callback)

        mock_isfile.return_value = False
        mock_walk.side_effect = [
            [('/var/log', ['dir1'], ['log1', 'file']), ('/var/log/dir1', ['dir2'], ['log2'])],
            [('/var/log', ['dir1'], ['log1', 'file']), ('/var/log/dir1', ['dir2'], [])]
        ]

        log = Log(id='aaaaaaaa-1234-5678-1234-56781234aaa1', source_server=self.source, target='/var/log',
                  destination_server=self.dest, dest_folder='/dimensigon/logs/', include='^(log|dir)', exclude='^dir2',
                  recursive=True)
        db.session.add(log)

        run(self.log_sender.send_new_data())

        mock_isfile.assert_called_once()
        self.assertEqual(2, mock_pb_rl.call_count)
        self.assertEqual(2, mock_pt_uof.call_count)
Beispiel #9
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)
Beispiel #10
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)