cubicweb/server/test/unittest_serverctl.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Tue, 21 Feb 2017 08:56:38 +0100
changeset 12016 88ed82a25f8a
parent 12002 26453d9467f6
child 12028 08c866d2f11d
permissions -rw-r--r--
[server] Add a "scheduler" command to run repository scheduler This commands starts the repository scheduler as a standalone process that should complement a CubicWeb web instance running as a WSGI application. Added a log message in repository's shutdown method to help testing the command (i.e. make sure the method is called after the scheduler stopped). Related to #17057223.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10825
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
     1
import os.path as osp
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
     2
import shutil
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
     3
12016
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
     4
from mock import patch
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
     5
11355
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
     6
from cubicweb import ExecutionError
10960
9e64fddebc89 merge with 3.21.3
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10825
diff changeset
     7
from cubicweb.devtools import testlib, ApptestConfiguration
12016
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
     8
from cubicweb.server.serverctl import (
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
     9
    DBDumpCommand,
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    10
    RepositorySchedulerCommand,
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    11
    SynchronizeSourceCommand,
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    12
)
10825
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    13
from cubicweb.server.serverconfig import ServerConfiguration
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    14
12002
26453d9467f6 [test] Make unittest_serverctl.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11355
diff changeset
    15
10825
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    16
class ServerCTLTC(testlib.CubicWebTC):
12002
26453d9467f6 [test] Make unittest_serverctl.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11355
diff changeset
    17
10825
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    18
    def setUp(self):
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    19
        super(ServerCTLTC, self).setUp()
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    20
        self.orig_config_for = ServerConfiguration.config_for
12002
26453d9467f6 [test] Make unittest_serverctl.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11355
diff changeset
    21
26453d9467f6 [test] Make unittest_serverctl.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11355
diff changeset
    22
        def config_for(appid):
26453d9467f6 [test] Make unittest_serverctl.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11355
diff changeset
    23
            return ApptestConfiguration(appid, __file__)
26453d9467f6 [test] Make unittest_serverctl.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11355
diff changeset
    24
10960
9e64fddebc89 merge with 3.21.3
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10825
diff changeset
    25
        ServerConfiguration.config_for = staticmethod(config_for)
10825
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    26
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    27
    def tearDown(self):
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    28
        ServerConfiguration.config_for = self.orig_config_for
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    29
        super(ServerCTLTC, self).tearDown()
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    30
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    31
    def test_dump(self):
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    32
        DBDumpCommand(None).run([self.appid])
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    33
        shutil.rmtree(osp.join(self.config.apphome, 'backup'))
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    34
12016
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    35
    def test_scheduler(self):
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    36
        cmd = RepositorySchedulerCommand(None)
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    37
        with patch('sched.scheduler.run',
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    38
                   side_effect=RuntimeError('boom')) as patched_run:
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    39
            with self.assertRaises(RuntimeError) as exc_cm:
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    40
                with self.assertLogs('cubicweb.repository', level='INFO') as log_cm:
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    41
                    cmd.run([self.appid])
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    42
        # make sure repository scheduler started
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    43
        scheduler_start_message = (
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    44
            'INFO:cubicweb.repository:starting repository scheduler with '
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    45
            'tasks: update_feeds, clean_sessions, expire_dataimports'
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    46
        )
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    47
        self.assertIn(scheduler_start_message, log_cm.output)
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    48
        # and that scheduler's run method got called
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    49
        self.assertIn('boom', str(exc_cm.exception))
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    50
        patched_run.assert_called_once_with()
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    51
        # make sure repository's shutdown method got called
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    52
        repo_shutdown_message = 'INFO:cubicweb.repository:shutting down repository'
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    53
        self.assertIn(repo_shutdown_message, log_cm.output)
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    54
11355
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    55
    def test_source_sync(self):
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    56
        with self.admin_access.repo_cnx() as cnx:
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    57
            cnx.create_entity('CWSource', name=u'success_feed', type=u'datafeed',
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    58
                              parser=u'test_source_parser_success',
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    59
                              url=u'ignored')
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    60
            cnx.create_entity('CWSource', name=u'fail_feed', type=u'datafeed',
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    61
                              parser=u'test_source_parser_fail',
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    62
                              url=u'ignored')
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    63
            cnx.commit()
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    64
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    65
            cmd = SynchronizeSourceCommand(None)
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    66
            cmd.config.force = 1
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    67
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    68
            # Should sync all sources even if one failed
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    69
            with self.assertRaises(ExecutionError) as exc:
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    70
                cmd.run([self.appid])
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    71
            self.assertEqual(len(cnx.find('Card', title=u'success')), 1)
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    72
            self.assertEqual(len(cnx.find('Card', title=u'fail')), 0)
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    73
            self.assertEqual(str(exc.exception), 'All sources where not synced')
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    74
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    75
            # call with named sources
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    76
            cmd.run([self.appid, u'success_feed'])
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    77
            self.assertEqual(len(cnx.find('Card', title=u'success')), 2)
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    78
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    79
            with self.assertRaises(ExecutionError) as exc:
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    80
                cmd.run([self.appid, u'fail_feed'])
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    81
            self.assertEqual(str(exc.exception), 'All sources where not synced')
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    82
            self.assertEqual(len(cnx.find('Card', title=u'fail')), 0)
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    83
10825
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    84
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    85
if __name__ == '__main__':
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    86
    from unittest import main
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    87
    main()