cubicweb/server/test/unittest_serverctl.py
author Philippe Pepiot <philippe.pepiot@logilab.fr>
Fri, 24 May 2019 16:29:14 +0200
changeset 12626 32ee89340e59
parent 12568 fc45f22c8100
permissions -rw-r--r--
Merge 3.26
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
12568
fc45f22c8100 [test] use unittest.mock instead of mock library
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12028
diff changeset
     3
from unittest.mock import patch
12016
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
     4
11355
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
     5
from cubicweb import ExecutionError
10960
9e64fddebc89 merge with 3.21.3
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10825
diff changeset
     6
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
     7
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
     8
    DBDumpCommand,
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
     9
    RepositorySchedulerCommand,
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    10
    SynchronizeSourceCommand,
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    11
)
10825
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    12
from cubicweb.server.serverconfig import ServerConfiguration
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    13
12002
26453d9467f6 [test] Make unittest_serverctl.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11355
diff changeset
    14
10825
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    15
class ServerCTLTC(testlib.CubicWebTC):
12002
26453d9467f6 [test] Make unittest_serverctl.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11355
diff changeset
    16
10825
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    17
    def setUp(self):
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    18
        super(ServerCTLTC, self).setUp()
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    19
        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
    20
26453d9467f6 [test] Make unittest_serverctl.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11355
diff changeset
    21
        def config_for(appid):
26453d9467f6 [test] Make unittest_serverctl.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11355
diff changeset
    22
            return ApptestConfiguration(appid, __file__)
26453d9467f6 [test] Make unittest_serverctl.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11355
diff changeset
    23
10960
9e64fddebc89 merge with 3.21.3
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10825
diff changeset
    24
        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
    25
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    26
    def tearDown(self):
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    27
        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
    28
        super(ServerCTLTC, self).tearDown()
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    29
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    30
    def test_dump(self):
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    31
        DBDumpCommand(None).run([self.appid])
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    32
        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
    33
12016
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    34
    def test_scheduler(self):
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    35
        cmd = RepositorySchedulerCommand(None)
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    36
        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
    37
                   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
    38
            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
    39
                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
    40
                    cmd.run([self.appid])
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    41
        # 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
    42
        scheduler_start_message = (
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    43
            'INFO:cubicweb.repository:starting repository scheduler with '
12028
08c866d2f11d [repo] Kill repo._sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12016
diff changeset
    44
            'tasks: update_feeds, expire_dataimports'
12016
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    45
        )
88ed82a25f8a [server] Add a "scheduler" command to run repository scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12002
diff changeset
    46
        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
    47
        # 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
    48
        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
    49
        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
    50
        # 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
    51
        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
    52
        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
    53
11355
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    54
    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
    55
        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
    56
            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
    57
                              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
    58
                              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
    59
            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
    60
                              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
    61
                              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
    62
            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
    63
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    64
            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
    65
            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
    66
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    67
            # 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
    68
            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
    69
                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
    70
            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
    71
            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
    72
            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
    73
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    74
            # 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
    75
            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
    76
            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
    77
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    78
            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
    79
                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
    80
            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
    81
            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
    82
10825
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    83
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    84
if __name__ == '__main__':
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    85
    from unittest import main
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    86
    main()