cubicweb/test/unittest_cwctl.py
author Philippe Pepiot <ph@itsalwaysdns.eu>
Tue, 31 Mar 2020 19:15:03 +0200
changeset 12957 0c973204033a
parent 12743 a74e77469540
permissions -rw-r--r--
[server] prevent returning closed cursor to the database pool In since c8c6ad8 init_repository use repo.internal_cnx() instead of repo.system_source.get_connection() so it use the pool and we should not close cursors from the pool before returning it back. Otherwise we may have "connection already closed" error. This bug only trigger when connection-pool-size = 1. Since we are moving to use a dynamic pooler we need to get this fixed. This does not occur with sqlite since the connection wrapper instantiate new cursor everytime, but this occur with other databases.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9639
74b3db5bb41d [test] silence warnings for unittest_cwctl by upgrading to new 3.19 API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6632
diff changeset
     1
# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
import sys
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
import os
6631
26c303c3f1aa [test] make test independant from the working directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6197
diff changeset
    20
from os.path import join
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12121
diff changeset
    21
from io import StringIO
11469
e0538fee4caa [test] Use plain unittest in unittest_cwctl.py
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
    22
import unittest
12685
84a8a8915512 [cubicweb-ctl] move to accepting only once instance name per command
Laurent Peuch <cortex@worlddomination.be>
parents: 12568
diff changeset
    23
from unittest.mock import patch, MagicMock
12120
707139a9eb00 [cwctl] Drop 'cubicweb_' prefix when displaying cubes with 'cubicweb-ctl list'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12053
diff changeset
    24
12685
84a8a8915512 [cubicweb-ctl] move to accepting only once instance name per command
Laurent Peuch <cortex@worlddomination.be>
parents: 12568
diff changeset
    25
from logilab.common.clcommands import CommandLine
84a8a8915512 [cubicweb-ctl] move to accepting only once instance name per command
Laurent Peuch <cortex@worlddomination.be>
parents: 12568
diff changeset
    26
12743
a74e77469540 [mod] move get_pdb to utils.py
Laurent Peuch <cortex@worlddomination.be>
parents: 12737
diff changeset
    27
from cubicweb import utils, server
12685
84a8a8915512 [cubicweb-ctl] move to accepting only once instance name per command
Laurent Peuch <cortex@worlddomination.be>
parents: 12568
diff changeset
    28
from cubicweb.cwctl import ListCommand, InstanceCommand
5430
ed8f71e244f8 [shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5426
diff changeset
    29
from cubicweb.devtools.testlib import CubicWebTC
ed8f71e244f8 [shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5426
diff changeset
    30
from cubicweb.server.migractions import ServerMigrationHelper
12685
84a8a8915512 [cubicweb-ctl] move to accepting only once instance name per command
Laurent Peuch <cortex@worlddomination.be>
parents: 12568
diff changeset
    31
from cubicweb.cwconfig import CubicWebConfiguration as cwcfg
84a8a8915512 [cubicweb-ctl] move to accepting only once instance name per command
Laurent Peuch <cortex@worlddomination.be>
parents: 12568
diff changeset
    32
from cubicweb.__pkginfo__ import version as cw_version
5430
ed8f71e244f8 [shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5426
diff changeset
    33
12120
707139a9eb00 [cwctl] Drop 'cubicweb_' prefix when displaying cubes with 'cubicweb-ctl list'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12053
diff changeset
    34
import unittest_cwconfig
707139a9eb00 [cwctl] Drop 'cubicweb_' prefix when displaying cubes with 'cubicweb-ctl list'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12053
diff changeset
    35
5430
ed8f71e244f8 [shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5426
diff changeset
    36
11469
e0538fee4caa [test] Use plain unittest in unittest_cwctl.py
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
    37
class CubicWebCtlTC(unittest.TestCase):
12120
707139a9eb00 [cwctl] Drop 'cubicweb_' prefix when displaying cubes with 'cubicweb-ctl list'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12053
diff changeset
    38
    setUpClass = unittest_cwconfig.CubicWebConfigurationTC.setUpClass
707139a9eb00 [cwctl] Drop 'cubicweb_' prefix when displaying cubes with 'cubicweb-ctl list'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12053
diff changeset
    39
    tearDownClass = unittest_cwconfig.CubicWebConfigurationTC.tearDownClass
707139a9eb00 [cwctl] Drop 'cubicweb_' prefix when displaying cubes with 'cubicweb-ctl list'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12053
diff changeset
    40
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
    def setUp(self):
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12121
diff changeset
    42
        self.stream = StringIO()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
        sys.stdout = self.stream
11469
e0538fee4caa [test] Use plain unittest in unittest_cwctl.py
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
    44
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
    def tearDown(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
        sys.stdout = sys.__stdout__
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    47
12120
707139a9eb00 [cwctl] Drop 'cubicweb_' prefix when displaying cubes with 'cubicweb-ctl list'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12053
diff changeset
    48
    @patch('pkg_resources.iter_entry_points', side_effect=unittest_cwconfig.iter_entry_points)
707139a9eb00 [cwctl] Drop 'cubicweb_' prefix when displaying cubes with 'cubicweb-ctl list'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12053
diff changeset
    49
    def test_list(self, mock_iter_entry_points):
6197
d8f0dce0b86d [test] fixes to follow recent changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5436
diff changeset
    50
        ListCommand(None).run([])
12120
707139a9eb00 [cwctl] Drop 'cubicweb_' prefix when displaying cubes with 'cubicweb-ctl list'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12053
diff changeset
    51
        self.assertNotIn('cubicweb_', self.stream.getvalue())
707139a9eb00 [cwctl] Drop 'cubicweb_' prefix when displaying cubes with 'cubicweb-ctl list'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12053
diff changeset
    52
        mock_iter_entry_points.assert_called_once_with(
707139a9eb00 [cwctl] Drop 'cubicweb_' prefix when displaying cubes with 'cubicweb-ctl list'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12053
diff changeset
    53
            group='cubicweb.cubes', name=None)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    54
12053
c3c9f2e1424c [pyramid] Add a "pyramid" instance configuration type
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12020
diff changeset
    55
    def test_list_configurations(self):
c3c9f2e1424c [pyramid] Add a "pyramid" instance configuration type
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12020
diff changeset
    56
        ListCommand(None).run(['configurations'])
c3c9f2e1424c [pyramid] Add a "pyramid" instance configuration type
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12020
diff changeset
    57
        configs = [l[2:].strip() for l in self.stream.getvalue().splitlines()
c3c9f2e1424c [pyramid] Add a "pyramid" instance configuration type
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12020
diff changeset
    58
                   if l.startswith('* ')]
c3c9f2e1424c [pyramid] Add a "pyramid" instance configuration type
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12020
diff changeset
    59
        self.assertIn('all-in-one', configs)
c3c9f2e1424c [pyramid] Add a "pyramid" instance configuration type
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12020
diff changeset
    60
        self.assertIn('pyramid', configs)
c3c9f2e1424c [pyramid] Add a "pyramid" instance configuration type
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12020
diff changeset
    61
5430
ed8f71e244f8 [shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5426
diff changeset
    62
ed8f71e244f8 [shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5426
diff changeset
    63
class CubicWebShellTC(CubicWebTC):
ed8f71e244f8 [shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5426
diff changeset
    64
ed8f71e244f8 [shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5426
diff changeset
    65
    def test_process_script_args_context(self):
9639
74b3db5bb41d [test] silence warnings for unittest_cwctl by upgrading to new 3.19 API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6632
diff changeset
    66
        repo = self.repo
10353
d9a1e7939ee6 [migractions] remove any session related leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9639
diff changeset
    67
        with self.admin_access.repo_cnx() as cnx:
9639
74b3db5bb41d [test] silence warnings for unittest_cwctl by upgrading to new 3.19 API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6632
diff changeset
    68
            mih = ServerMigrationHelper(None, repo=repo, cnx=cnx,
74b3db5bb41d [test] silence warnings for unittest_cwctl by upgrading to new 3.19 API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6632
diff changeset
    69
                                        interactive=False,
74b3db5bb41d [test] silence warnings for unittest_cwctl by upgrading to new 3.19 API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6632
diff changeset
    70
                                        # hack so it don't try to load fs schema
74b3db5bb41d [test] silence warnings for unittest_cwctl by upgrading to new 3.19 API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6632
diff changeset
    71
                                        schema=1)
12121
86ec847835cd Fix flake8 errors in unittest_cwctl
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12120
diff changeset
    72
            scripts = {
86ec847835cd Fix flake8 errors in unittest_cwctl
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12120
diff changeset
    73
                'script1.py': list(),
86ec847835cd Fix flake8 errors in unittest_cwctl
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12120
diff changeset
    74
                'script2.py': ['-v'],
86ec847835cd Fix flake8 errors in unittest_cwctl
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12120
diff changeset
    75
                'script3.py': ['-vd', '-f', 'FILE.TXT'],
86ec847835cd Fix flake8 errors in unittest_cwctl
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12120
diff changeset
    76
            }
9639
74b3db5bb41d [test] silence warnings for unittest_cwctl by upgrading to new 3.19 API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6632
diff changeset
    77
            mih.cmd_process_script(join(self.datadir, 'scripts', 'script1.py'),
74b3db5bb41d [test] silence warnings for unittest_cwctl by upgrading to new 3.19 API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6632
diff changeset
    78
                                   funcname=None)
74b3db5bb41d [test] silence warnings for unittest_cwctl by upgrading to new 3.19 API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6632
diff changeset
    79
            for script, args in scripts.items():
74b3db5bb41d [test] silence warnings for unittest_cwctl by upgrading to new 3.19 API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6632
diff changeset
    80
                scriptname = os.path.join(self.datadir, 'scripts', script)
10600
180aa08cad48 [tests] Replace use of deprecated TestCase.assert_
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10353
diff changeset
    81
                self.assertTrue(os.path.exists(scriptname))
9639
74b3db5bb41d [test] silence warnings for unittest_cwctl by upgrading to new 3.19 API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6632
diff changeset
    82
                mih.cmd_process_script(scriptname, None, scriptargs=args)
5430
ed8f71e244f8 [shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5426
diff changeset
    83
ed8f71e244f8 [shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5426
diff changeset
    84
12691
3f0e64630d94 [test] Rename TestCommand to avoid PytestCollectionWarning
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12685
diff changeset
    85
class _TestCommand(InstanceCommand):
12685
84a8a8915512 [cubicweb-ctl] move to accepting only once instance name per command
Laurent Peuch <cortex@worlddomination.be>
parents: 12568
diff changeset
    86
    "I need some doc"
84a8a8915512 [cubicweb-ctl] move to accepting only once instance name per command
Laurent Peuch <cortex@worlddomination.be>
parents: 12568
diff changeset
    87
    name = "test"
12695
38dfd90c335a [test] Add an "actionverb" attribute to test commands
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12694
diff changeset
    88
    actionverb = 'failtested'
12685
84a8a8915512 [cubicweb-ctl] move to accepting only once instance name per command
Laurent Peuch <cortex@worlddomination.be>
parents: 12568
diff changeset
    89
84a8a8915512 [cubicweb-ctl] move to accepting only once instance name per command
Laurent Peuch <cortex@worlddomination.be>
parents: 12568
diff changeset
    90
    def test_instance(self, appid):
84a8a8915512 [cubicweb-ctl] move to accepting only once instance name per command
Laurent Peuch <cortex@worlddomination.be>
parents: 12568
diff changeset
    91
        pass
84a8a8915512 [cubicweb-ctl] move to accepting only once instance name per command
Laurent Peuch <cortex@worlddomination.be>
parents: 12568
diff changeset
    92
84a8a8915512 [cubicweb-ctl] move to accepting only once instance name per command
Laurent Peuch <cortex@worlddomination.be>
parents: 12568
diff changeset
    93
12692
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
    94
class _TestFailCommand(InstanceCommand):
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
    95
    "I need some doc"
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
    96
    name = "test_fail"
12695
38dfd90c335a [test] Add an "actionverb" attribute to test commands
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12694
diff changeset
    97
    actionverb = 'tested'
12692
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
    98
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
    99
    def test_fail_instance(self, appid):
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   100
        raise Exception()
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   101
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   102
12685
84a8a8915512 [cubicweb-ctl] move to accepting only once instance name per command
Laurent Peuch <cortex@worlddomination.be>
parents: 12568
diff changeset
   103
class InstanceCommandTest(unittest.TestCase):
12692
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   104
    def setUp(self):
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   105
        self.CWCTL = CommandLine('cubicweb-ctl', 'The CubicWeb swiss-knife.',
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   106
                                 version=cw_version, check_duplicated_command=False)
12685
84a8a8915512 [cubicweb-ctl] move to accepting only once instance name per command
Laurent Peuch <cortex@worlddomination.be>
parents: 12568
diff changeset
   107
        cwcfg.load_cwctl_plugins()
12692
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   108
        self.CWCTL.register(_TestCommand)
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   109
        self.CWCTL.register(_TestFailCommand)
12685
84a8a8915512 [cubicweb-ctl] move to accepting only once instance name per command
Laurent Peuch <cortex@worlddomination.be>
parents: 12568
diff changeset
   110
12736
5add82b08a6d [cubicweb-ctl] backport --loglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12735
diff changeset
   111
        self.fake_config = MagicMock()
5add82b08a6d [cubicweb-ctl] backport --loglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12735
diff changeset
   112
        self.fake_config.global_set_option = MagicMock()
5add82b08a6d [cubicweb-ctl] backport --loglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12735
diff changeset
   113
12685
84a8a8915512 [cubicweb-ctl] move to accepting only once instance name per command
Laurent Peuch <cortex@worlddomination.be>
parents: 12568
diff changeset
   114
        # pretend that this instance exists
12737
56f7386bc6b9 [cubicweb-ctl] backport --dbglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12736
diff changeset
   115
        config_patcher = patch.object(cwcfg, 'config_for', return_value=self.fake_config)
56f7386bc6b9 [cubicweb-ctl] backport --dbglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12736
diff changeset
   116
        config_patcher.start()
56f7386bc6b9 [cubicweb-ctl] backport --dbglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12736
diff changeset
   117
        self.addCleanup(config_patcher.stop)
12685
84a8a8915512 [cubicweb-ctl] move to accepting only once instance name per command
Laurent Peuch <cortex@worlddomination.be>
parents: 12568
diff changeset
   118
12693
5e7212209ea6 [test] ensure mocking is stopped in cwctl tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12692
diff changeset
   119
    @patch.object(_TestCommand, 'test_instance', return_value=0)
5e7212209ea6 [test] ensure mocking is stopped in cwctl tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12692
diff changeset
   120
    def test_getting_called(self, test_instance):
12694
58f132392cba [test] use self.assertRaises() in cwctl tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12693
diff changeset
   121
        with self.assertRaises(SystemExit) as cm:
12692
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   122
            self.CWCTL.run(["test", "some_instance"])
12694
58f132392cba [test] use self.assertRaises() in cwctl tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12693
diff changeset
   123
        self.assertEqual(cm.exception.code, 0)
12693
5e7212209ea6 [test] ensure mocking is stopped in cwctl tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12692
diff changeset
   124
        test_instance.assert_called_with("some_instance")
12685
84a8a8915512 [cubicweb-ctl] move to accepting only once instance name per command
Laurent Peuch <cortex@worlddomination.be>
parents: 12568
diff changeset
   125
12743
a74e77469540 [mod] move get_pdb to utils.py
Laurent Peuch <cortex@worlddomination.be>
parents: 12737
diff changeset
   126
    @patch.object(utils, 'get_pdb')
12692
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   127
    def test_pdb_not_called(self, get_pdb):
12694
58f132392cba [test] use self.assertRaises() in cwctl tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12693
diff changeset
   128
        # CWCTL will finish the program after that
58f132392cba [test] use self.assertRaises() in cwctl tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12693
diff changeset
   129
        with self.assertRaises(SystemExit) as cm:
12692
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   130
            self.CWCTL.run(["test", "some_instance"])
12694
58f132392cba [test] use self.assertRaises() in cwctl tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12693
diff changeset
   131
        self.assertEqual(cm.exception.code, 0)
12692
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   132
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   133
        get_pdb.assert_not_called()
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   134
12743
a74e77469540 [mod] move get_pdb to utils.py
Laurent Peuch <cortex@worlddomination.be>
parents: 12737
diff changeset
   135
    @patch.object(utils, 'get_pdb')
12692
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   136
    def test_pdb_called(self, get_pdb):
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   137
        post_mortem = get_pdb.return_value.post_mortem
12694
58f132392cba [test] use self.assertRaises() in cwctl tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12693
diff changeset
   138
        with self.assertRaises(SystemExit) as cm:
12692
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   139
            self.CWCTL.run(["test_fail", "some_instance", "--pdb"])
12694
58f132392cba [test] use self.assertRaises() in cwctl tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12693
diff changeset
   140
        self.assertEqual(cm.exception.code, 8)
12692
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   141
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   142
        get_pdb.assert_called_once()
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   143
        post_mortem.assert_called_once()
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   144
12735
17d1b1f4eddd [cubicweb-ctl/fix] correctly get exception traceback_ for pdb.post_mortem
Laurent Peuch <cortex@worlddomination.be>
parents: 12696
diff changeset
   145
        # we want post_mortem to actually receive the traceback
17d1b1f4eddd [cubicweb-ctl/fix] correctly get exception traceback_ for pdb.post_mortem
Laurent Peuch <cortex@worlddomination.be>
parents: 12696
diff changeset
   146
        self.assertNotEqual(post_mortem.call_args, ((None,),))
17d1b1f4eddd [cubicweb-ctl/fix] correctly get exception traceback_ for pdb.post_mortem
Laurent Peuch <cortex@worlddomination.be>
parents: 12696
diff changeset
   147
12692
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   148
    @patch.dict(sys.modules, ipdb=MagicMock())
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   149
    def test_ipdb_selected_and_called(self):
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   150
        ipdb = sys.modules['ipdb']
12694
58f132392cba [test] use self.assertRaises() in cwctl tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12693
diff changeset
   151
        with self.assertRaises(SystemExit) as cm:
12692
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   152
            self.CWCTL.run(["test_fail", "some_instance", "--pdb"])
12694
58f132392cba [test] use self.assertRaises() in cwctl tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12693
diff changeset
   153
        self.assertEqual(cm.exception.code, 8)
12692
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   154
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   155
        ipdb.post_mortem.assert_called_once()
8673da7c2f85 [cubicweb-ctl] add '--pdb' global option to launch (i)pdb on exception
Laurent Peuch <cortex@worlddomination.be>
parents: 12691
diff changeset
   156
12696
eb83daa69495 [cubicweb-ctl] respect sys.exit status code when aborting a command
Laurent Peuch <cortex@worlddomination.be>
parents: 12695
diff changeset
   157
    @patch.object(_TestFailCommand, 'test_fail_instance', side_effect=SystemExit(42))
eb83daa69495 [cubicweb-ctl] respect sys.exit status code when aborting a command
Laurent Peuch <cortex@worlddomination.be>
parents: 12695
diff changeset
   158
    def test_respect_return_error_code(self, test_fail_instance):
eb83daa69495 [cubicweb-ctl] respect sys.exit status code when aborting a command
Laurent Peuch <cortex@worlddomination.be>
parents: 12695
diff changeset
   159
        with self.assertRaises(SystemExit) as cm:
eb83daa69495 [cubicweb-ctl] respect sys.exit status code when aborting a command
Laurent Peuch <cortex@worlddomination.be>
parents: 12695
diff changeset
   160
            self.CWCTL.run(["test_fail", "some_instance"])
eb83daa69495 [cubicweb-ctl] respect sys.exit status code when aborting a command
Laurent Peuch <cortex@worlddomination.be>
parents: 12695
diff changeset
   161
        self.assertEqual(cm.exception.code, 42)
eb83daa69495 [cubicweb-ctl] respect sys.exit status code when aborting a command
Laurent Peuch <cortex@worlddomination.be>
parents: 12695
diff changeset
   162
eb83daa69495 [cubicweb-ctl] respect sys.exit status code when aborting a command
Laurent Peuch <cortex@worlddomination.be>
parents: 12695
diff changeset
   163
        test_fail_instance.assert_called_once()
eb83daa69495 [cubicweb-ctl] respect sys.exit status code when aborting a command
Laurent Peuch <cortex@worlddomination.be>
parents: 12695
diff changeset
   164
eb83daa69495 [cubicweb-ctl] respect sys.exit status code when aborting a command
Laurent Peuch <cortex@worlddomination.be>
parents: 12695
diff changeset
   165
    @patch.object(_TestFailCommand, 'test_fail_instance', side_effect=KeyboardInterrupt)
eb83daa69495 [cubicweb-ctl] respect sys.exit status code when aborting a command
Laurent Peuch <cortex@worlddomination.be>
parents: 12695
diff changeset
   166
    def test_error_code_keyboardinterupt_2(self, test_fail_instance):
eb83daa69495 [cubicweb-ctl] respect sys.exit status code when aborting a command
Laurent Peuch <cortex@worlddomination.be>
parents: 12695
diff changeset
   167
        with self.assertRaises(SystemExit) as cm:
eb83daa69495 [cubicweb-ctl] respect sys.exit status code when aborting a command
Laurent Peuch <cortex@worlddomination.be>
parents: 12695
diff changeset
   168
            self.CWCTL.run(["test_fail", "some_instance"])
eb83daa69495 [cubicweb-ctl] respect sys.exit status code when aborting a command
Laurent Peuch <cortex@worlddomination.be>
parents: 12695
diff changeset
   169
        self.assertEqual(cm.exception.code, 2)
eb83daa69495 [cubicweb-ctl] respect sys.exit status code when aborting a command
Laurent Peuch <cortex@worlddomination.be>
parents: 12695
diff changeset
   170
eb83daa69495 [cubicweb-ctl] respect sys.exit status code when aborting a command
Laurent Peuch <cortex@worlddomination.be>
parents: 12695
diff changeset
   171
        test_fail_instance.assert_called_once()
eb83daa69495 [cubicweb-ctl] respect sys.exit status code when aborting a command
Laurent Peuch <cortex@worlddomination.be>
parents: 12695
diff changeset
   172
12736
5add82b08a6d [cubicweb-ctl] backport --loglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12735
diff changeset
   173
    def test_set_loglevel(self):
5add82b08a6d [cubicweb-ctl] backport --loglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12735
diff changeset
   174
        LOG_LEVELS = ('debug', 'info', 'warning', 'error')
5add82b08a6d [cubicweb-ctl] backport --loglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12735
diff changeset
   175
5add82b08a6d [cubicweb-ctl] backport --loglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12735
diff changeset
   176
        for log_level in LOG_LEVELS:
5add82b08a6d [cubicweb-ctl] backport --loglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12735
diff changeset
   177
            with self.assertRaises(SystemExit) as cm:
5add82b08a6d [cubicweb-ctl] backport --loglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12735
diff changeset
   178
                self.CWCTL.run(["test", "some_instance", "--loglevel", log_level])
5add82b08a6d [cubicweb-ctl] backport --loglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12735
diff changeset
   179
            self.assertEqual(cm.exception.code, 0)
5add82b08a6d [cubicweb-ctl] backport --loglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12735
diff changeset
   180
5add82b08a6d [cubicweb-ctl] backport --loglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12735
diff changeset
   181
            self.fake_config.global_set_option.assert_called_with('log-threshold',
5add82b08a6d [cubicweb-ctl] backport --loglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12735
diff changeset
   182
                                                                  log_level.upper())
5add82b08a6d [cubicweb-ctl] backport --loglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12735
diff changeset
   183
12737
56f7386bc6b9 [cubicweb-ctl] backport --dbglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12736
diff changeset
   184
    @patch.object(server, "DEBUG", 0)
56f7386bc6b9 [cubicweb-ctl] backport --dbglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12736
diff changeset
   185
    def test_set_dblevel(self):
56f7386bc6b9 [cubicweb-ctl] backport --dbglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12736
diff changeset
   186
        DBG_FLAGS = ('RQL', 'SQL', 'REPO', 'HOOKS', 'OPS', 'SEC', 'MORE')
56f7386bc6b9 [cubicweb-ctl] backport --dbglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12736
diff changeset
   187
56f7386bc6b9 [cubicweb-ctl] backport --dbglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12736
diff changeset
   188
        total_value = 0
56f7386bc6b9 [cubicweb-ctl] backport --dbglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12736
diff changeset
   189
56f7386bc6b9 [cubicweb-ctl] backport --dbglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12736
diff changeset
   190
        for dbg_flag in DBG_FLAGS:
56f7386bc6b9 [cubicweb-ctl] backport --dbglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12736
diff changeset
   191
            with self.assertRaises(SystemExit) as cm:
56f7386bc6b9 [cubicweb-ctl] backport --dbglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12736
diff changeset
   192
                self.CWCTL.run(["test", "some_instance", "--dbglevel", dbg_flag])
56f7386bc6b9 [cubicweb-ctl] backport --dbglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12736
diff changeset
   193
            self.assertEqual(cm.exception.code, 0)
56f7386bc6b9 [cubicweb-ctl] backport --dbglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12736
diff changeset
   194
56f7386bc6b9 [cubicweb-ctl] backport --dbglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12736
diff changeset
   195
            total_value += getattr(server, "DBG_%s" % dbg_flag)
56f7386bc6b9 [cubicweb-ctl] backport --dbglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12736
diff changeset
   196
            self.assertEqual(total_value, server.DEBUG)
56f7386bc6b9 [cubicweb-ctl] backport --dbglevel option of pyramid to all instance commands
Laurent Peuch <cortex@worlddomination.be>
parents: 12736
diff changeset
   197
12685
84a8a8915512 [cubicweb-ctl] move to accepting only once instance name per command
Laurent Peuch <cortex@worlddomination.be>
parents: 12568
diff changeset
   198
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
if __name__ == '__main__':
11469
e0538fee4caa [test] Use plain unittest in unittest_cwctl.py
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   200
    unittest.main()