[test] update/fix previously skipped tests
authorPaul Tonelli <paul.tonelli@logilab.fr>
Mon, 17 Mar 2014 14:35:03 +0100
changeset 1469 8e03b378a932
parent 1468 3644b206edb5
child 1470 774bd1d5f4bc
[test] update/fix previously skipped tests - replace SetUpModule and TearDownModule by in class equivalent (repo reset needed between tests) - update old code in test_new_vc_trigger
test/unittest_hooks.py
testutils.py
--- a/test/unittest_hooks.py	Tue Apr 08 15:28:12 2014 +0200
+++ b/test/unittest_hooks.py	Mon Mar 17 14:35:03 2014 +0100
@@ -6,7 +6,8 @@
 from cubicweb.devtools.testlib import MAILBOX
 from cubicweb import Binary
 
-
+from cubes.vcsfile import bridge
+from cubes.apycot.hooks import start_period_tests
 from cubes.apycot.testutils import INPUTS_DIR, ApycotBaseTC
 
 
@@ -14,7 +15,9 @@
     url_filtered = re.sub('lgc/[0-9]*', 'lgc/<EID>', string.strip())
     return re.sub('[0-9]', 'X', url_filtered)
 
+
 class NotificationTC(ApycotBaseTC):
+
     def setup_database(self):
         super(NotificationTC, self).setup_database()
         self.request().create_entity('EmailAddress',
@@ -117,83 +120,180 @@
         self.assertFalse('anon' in [u.login for u in plan.nosy_list])
 
 
-HGREPO = os.path.join(INPUTS_DIR, u'hgrepo')
-def setupModule(*args, **kwargs):
-    if not os.path.exists(HGREPO):
-        os.mkdir(HGREPO)
-        os.system('cd %s && hg init' % HGREPO)
-
-def teardownModule(*args, **kwargs):
-    if os.path.exists(HGREPO):
-        os.system('rm -rf %s' % HGREPO)
-
-
 class StartTestTC(ApycotBaseTC):
 
-    def grant_write_perm(self, repo, group):
-        req = self.request()
-        managers = req.execute('CWGroup G WHERE G name %(group)s',
-                               {'group': group}).get_entity(0, 0)
-        req.create_entity(
-            'CWPermission', name=u"write", label=u'repo x write perm',
-            reverse_require_permission=repo,
-            require_group=managers)
+    def setUp(self):
+        super(StartTestTC, self).setUp()
+        r = self.repo
+        r.path = self.vcsrepo.path
+        os.system('hg init %s' % r.path)
+        os.system('echo data > %s/tutu.png' % r.path)
+        os.system('hg -R %s add %s/tutu.png' % (r.path, r.path))
+        os.system('hg -R %s commit -m rien' % r.path)
+        # reset vcsrepo
+        with r.internal_session(safe=True) as session:
+            bridge.import_content(session, commitevery=1, raise_on_error=True)
+
+    def tearDown(self):
+        r = self.repo
+        os.system('rm -rf %s/.hg' % r.path)
+        os.system('rm -rf %s/*.png' % r.path)
+        super(StartTestTC, self).tearDown()
+
+    def get_tc(self, period=None):
+        rql = ('Any PEN, TCN, TCS  WHERE TC in_state S, '
+               'TC use_environment PE, '
+               'PE name PEN, '
+               'S name "activated", '
+               'TC name TCN, '
+               'TC start_rev_deps TCS')
+        if period:
+            rql +=', TC computed_start_mode "%s"' % period
+        return set(tuple(tc) for tc in self.execute(rql))
+
+    def get_te_for_tc_name(self, eid):
+        rql = 'Any TE where TE using_config TC, TC eid %(eid)s'
+        return self.execute(rql, {'eid': eid})
+
+    def launch_all_tests(self, period):
+        """ launch all tests in the correct state and and make sure no
+        TestExecutions are left in status waiting execution. Also fill-in the
+        revision which should normally be filled by StartTestOp.
+        """
+
+        session = self.session
+        session.set_cnxset()
+        start_period_tests(session, period)
+        self.commit() #put new te in base
+        ## make sure they are not waiting execution and REV is filled
+        for te, rev in self.execute('Any TE, REV WHERE TE status %(st)s, '
+                                    'TE using_config TC, '
+                                    'TE branch BR, '
+                                    'TC use_environment PE, '
+                                    'PE local_repository R, '
+                                    'REV from_repository R, '
+                                    'REV branch BR, '
+                                    'NOT REV parent_revision REV2',
+                                    {'st': 'waiting execution'}):
+            te_e = self.request().entity_from_eid(te)
+            rev_e = self.request().entity_from_eid(rev)
+            te_e.cw_adapt_to('IWorkflowable').fire_transition('start')
+            te_e.cw_set(status=u'running')
+            te_e.cw_set(using_revision=rev)
+        self.commit() #change new te status
 
     def test_new_vc_trigger(self):
-        self.skipTest('XXX need update')
+        """ check the on new revision start mode. Run all testconfigs, add new
+        revision, re-run and check there are new test configs"""
         self.lgc.set_attributes(start_mode=u'on new revision')
-        lgc2 = self.add_test_config(u'lgc2', start_mode=u'manual', env=self.lgce)
-        lgc3 = self.add_test_config(u'lgc3', start_mode=u'on new revision',
-                                    check_config=u'branch=stable', env=self.lgce)
-        lgce2 = self.request().create_entity(
+        lgc2 = self.add_test_config(u'lgc2',
+                                    start_mode=u'manual',
+                                    env=self.lgce,
+                                    use_recipe=self.recipe)
+        ## same repo, branch stable
+        lgc3 = self.add_test_config(u'lgc3',
+                                    start_mode=u'on new revision',
+                                    check_config=u'branch=stable',
+                                    env=self.lgce,
+                                    use_recipe=self.recipe)
+        ## same repo, branch default
+        lgc4 = self.add_test_config(u'lgc4',
+                                    start_mode=u'on new revision',
+                                    check_config=u'branch=default',
+                                    env=self.lgce,
+                                    use_recipe=self.recipe)
+        ## on different repo
+        req = self.request()
+        lgce2 = req.create_entity(
             'ProjectEnvironment', name=u'lgce2',
             check_config=u'install=setup_install',
-            vcs_path=u'dir1',
+            vcs_path=u'dir1', #dummy
             )
-        req = self.request()
-        req.create_entity('Repository', type=u'mercurial',
-                          source_url=u'http://www.logilab.org/src/logilab/common',
-                          reverse_local_repository=lgce2)
-        lgc4 = self.add_test_config(u'lgc4', start_mode=u'on new revision',
+        lgc5 = self.add_test_config(u'lgc5',
+                                    start_mode=u'on new revision',
                                     check_config=u'branch=default',
-                                    env=lgce2, start_rev_deps=True)
-        lgc5 = self.add_test_config(u'lgc5', start_mode=u'manual',
-                                    env=lgce2)
-        self.commit()
-        r = self.request().create_entity('Repository', path=HGREPO, type=u'mercurial', encoding=u'utf8')
-        self.execute('SET PE local_repository R WHERE PE is ProjectEnvironment, R is Repository')
-        self.grant_write_perm(r, 'managers')
-        self.commit()
-        # now test
-        r.vcs_add(u'dir1', u'tutu.png', Binary('data'))
+                                    env=lgce2,
+                                    start_rev_deps=True,
+                                    use_recipe=self.recipe)
+        ## not sure this is still useful
         self.commit()
-        self.assertEqual(sorted(proxy.queued),
-                          # data is the pyro instance id
-                          [(('lgce', 'lgc'), {'cwinstid': ':cubicweb.data',
-                                              'branch': 'default',
-                                              'start_rev_deps': False}),
-                           (('lgce2', 'lgc4'), {'cwinstid': ':cubicweb.data',
-                                                'branch': 'default',
-                                                'start_rev_deps': True})])
-        proxy.queued = []
-        r.vcs_add(u'dir1', u'tutu.png', Binary('data'), branch=u'stable')
+        ## not reaally necessary now, as they all appear, helps understanding,
+        ## we check all the necessary testconfig are there
+        self.assertEqual(self.get_tc(period=u'on new revision'),
+                         set([('lgce', 'lgc', None),
+                              ('lgce', 'lgc3', None),
+                              ('lgce', 'lgc4', None),
+                              ('lgce2', 'lgc5', 1)]))
+        ## run everything 
+
+        self.launch_all_tests('on new revision')
+
+        nb_tc_lgc_before = len(self.get_te_for_tc_name(self.lgc.eid))
+        nb_tc_lgc3_before = len(self.get_te_for_tc_name(lgc3.eid))
+        nb_tc_lgc4_before = len(self.get_te_for_tc_name(lgc4.eid))
+        nb_tc_lgc5_before = len(self.get_te_for_tc_name(lgc5.eid))
+        ##check you do not add anything on rerun (no new revision)
+        self.launch_all_tests('on new revision')
+        self.assertEqual(nb_tc_lgc_before,
+                         len(self.get_te_for_tc_name(self.lgc.eid)))
+        self.assertEqual(nb_tc_lgc3_before,
+                         len(self.get_te_for_tc_name(lgc3.eid)))
+        self.assertEqual(nb_tc_lgc4_before,
+                         len(self.get_te_for_tc_name(lgc4.eid)))
+        self.assertEqual(nb_tc_lgc5_before,
+                         len(self.get_te_for_tc_name(lgc5.eid)))
+        r = self.repo
+
+        ## add a new revision in stable
+        #r.vcs_add(u'dir1', u'tutu.png', Binary('data'), branch=u'stable')
         self.commit()
-        self.assertEqual(proxy.queued, [(('lgce', 'lgc'), {'cwinstid': ':cubicweb.data',
-                                                            'branch': 'stable',
-                                                            'start_rev_deps': False}),
-                                         (('lgce', 'lgc3'), {'cwinstid': ':cubicweb.data',
-                                                             'branch': 'stable',
-                                                             'start_rev_deps': False})])
-        proxy.queued = []
-        r.vcs_add(u'dir2', u'tutu.png', Binary('data'))
+        os.system('echo data > %s/tutu1.png' % r.path)
+        os.system('hg -R %s branch stable' % r.path)
+        os.system('hg -R %s add %s/tutu1.png' % (r.path, r.path))
+        os.system('hg -R %s commit -m rien' % r.path)
+        with r.internal_session(safe=True) as session:
+            bridge.import_content(session, commitevery=1, raise_on_error=True)
+
+        ## now it should add te for the tc linked to lgce and running on new 
+        ## revision and not the others
+        self.launch_all_tests('on new revision')
+        self.assertEqual(nb_tc_lgc_before + 1,
+                         len(self.get_te_for_tc_name(self.lgc.eid)))
+        self.assertEqual(nb_tc_lgc3_before + 1,
+                         len(self.get_te_for_tc_name(lgc3.eid)))
+        self.assertEqual(nb_tc_lgc4_before,
+                          len(self.get_te_for_tc_name(lgc4.eid)))
+        self.assertEqual(nb_tc_lgc5_before,
+                          len(self.get_te_for_tc_name(lgc5.eid)))
+
+        ## add a new revision in default
+        #r.vcs_add(u'dir1', u'tutu1.png', Binary('data'))
         self.commit()
-        self.assertEqual(proxy.queued, [(('lgce', 'lgc'), {'cwinstid': ':cubicweb.data',
-                                                            'branch': 'default',
-                                                            'start_rev_deps': False})])
+        self.assertEqual(0, os.system('echo data > %s/tutu2.png' % r.path))
+        self.assertEqual(0,os.system('hg -R %s up default' % r.path))
+        self.assertEqual(0,os.system('hg -R %s add %s/tutu2.png' % (r.path, r.path)))
+        self.assertEqual(0,os.system('hg -R %s commit -m rien' % r.path))
+        with r.internal_session(safe=True) as session:
+            bridge.import_content(session, commitevery=1, raise_on_error=True)
+        ## it should add te to the tc on default (lgc and lgc4)
+        self.launch_all_tests('on new revision')
+        self.assertEqual(nb_tc_lgc_before + 2,
+                         len(self.get_te_for_tc_name(self.lgc.eid)))
+        self.assertEqual(nb_tc_lgc3_before + 1,
+                         len(self.get_te_for_tc_name(lgc3.eid)))
+        self.assertEqual(nb_tc_lgc4_before + 1,
+                         len(self.get_te_for_tc_name(lgc4.eid)))
+        self.assertEqual(nb_tc_lgc5_before,
+                         len(self.get_te_for_tc_name(lgc5.eid)))
 
     def test_datetime_trigger(self):
-        self.skipTest('XXX need update')
-        self.lgc.set_attributes(start_mode=u'hourly')
+        """test the registering of TC depending on their start_mode
+
+        - check they are added correctly when created
+        - check they are removed when deactivated
+        - check they are removed when finished
+        """
+        self.lgc.cw_set(start_mode=u'hourly')
         lgc2 = self.add_test_config(u'lgc2', start_mode=u'hourly', env=self.lgce)
         req = self.request()
         lgce2 = req.create_entity(
@@ -201,42 +301,48 @@
             check_config=u'install=setup_install',
             vcs_path=u'dir1',
             )
-        req.create_entity('Repository', type=u'mercurial',
-                          source_url=u'http://www.logilab.org/src/logilab/common',
-                          reverse_local_repository=lgce2)
         lgc3 = self.add_test_config(u'lgc3', start_mode=u'hourly',
                                     check_config=u'branch=default',
-                                    env=lgce2, start_rev_deps=True)
-        r = req.create_entity('Repository', path=HGREPO, type=u'mercurial', encoding=u'utf8')
-        self.grant_write_perm(r, 'managers')
-        r.vcs_add(u'dir1', u'tutu.png', Binary('data'))
-        self.execute('SET PE local_repository R WHERE PE name "lgce2"')
+                                    env=lgce2, start_rev_deps=True,
+                                    use_recipe=self.recipe)
+        self.execute('SET PE local_repository R WHERE PE name "lgce2", R eid %(r)s',
+                     {'r': self.vcsrepo.eid})
         self.commit()
-        session = self.session
-        session.set_pool()
-        self.assertEqual(start_test(session, 'daily'),
-                          set())
-        self.assertEqual(start_test(session, 'hourly'),
-                          set((('lgce', 'lgc', False, None),
-                               ('lgce', 'lgc2', False, None),
-                               ('lgce2', 'lgc3', True, 'default')))
+        self.assertEqual(self.get_tc('daily'),
+                          set([]))
+        self.assertEqual(self.get_tc('hourly'),
+                          set((('lgce', 'lgc', None),
+                               ('lgce', 'lgc2', None),
+                               ('lgce2', 'lgc3', 1)))
                           )
         lgc2.cw_adapt_to('IWorkflowable').fire_transition('deactivate')
         self.commit()
-        session.set_pool()
-        self.assertEqual(start_test(session, 'hourly'),
-                          set((('lgce', 'lgc', False, None),
-                               ('lgce2', 'lgc3', True, 'default')))
+        self.assertEqual(self.get_tc('hourly'),
+                          set((('lgce', 'lgc', None),
+                               ('lgce2', 'lgc3', 1)))
                           )
-        self.login('narval', password='narval0')
-        ex = self.dumb_execution('lgc3', (), setend=True)
-        ex.set_relations(using_revision=r.branch_head())
         self.commit()
-        self.restore_connection()
-        session.set_pool()
-        self.assertEqual(start_test(session, 'hourly'),
-                          set((('lgce', 'lgc', False, None),))
+        rset_before_lgc = self.get_te_for_tc_name(self.lgc.eid)
+        te = lgc3.start(lgce2, branch=u'default')
+        lgc3._cw.cnx.commit()
+        ###not really useful as the method directly check the existence of a
+        ###relevant tc before running the tests,
+        ### we should instead test the launched test configs
+        self.assertEqual(self.get_tc('hourly'),
+                          set((('lgce', 'lgc', None),
+                               ('lgce2', 'lgc3', 1)))
                           )
+        rset_before_lgc = self.get_te_for_tc_name(self.lgc.eid)
+        rset_before_lgc3 = self.get_te_for_tc_name(lgc3.eid)
+        self.launch_all_tests('hourly')
+        self.commit()
+        rset_after_lgc = self.get_te_for_tc_name(self.lgc.eid)
+        rset_after_lgc3 = self.get_te_for_tc_name(lgc3.eid)
+        ### check there is one new TestConfig for lgc and the same number for lgc3
+        #(alredy launched)
+        self.assertEqual(len(rset_after_lgc3), len(rset_before_lgc3))
+        self.assertEqual(len(rset_after_lgc), 1 + len(rset_before_lgc))
+
 
 class ComputedStartModeTC(ApycotBaseTC):
 
--- a/testutils.py	Tue Apr 08 15:28:12 2014 +0200
+++ b/testutils.py	Mon Mar 17 14:35:03 2014 +0100
@@ -13,6 +13,7 @@
 
 from cubicweb.devtools.testlib import CubicWebTC
 
+from cubes.vcsfile.testutils import init_vcsrepo
 from cubes.narval.testutils import NarvalBaseTC
 from cubes.apycot.recipes import quick_script
 
@@ -135,6 +136,10 @@
     recipescript = quick_script
 
     def setup_database(self):
+        """ self.repo: used to get the session to connect to cw
+            self.vcsrepo: new entity
+        """
+
         req = self.request()
         self.lgce = req.create_entity(
             'ProjectEnvironment', name=u'lgce',
@@ -151,6 +156,8 @@
                                      'python_test_coverage_treshold=70\n',
                                      check_environment=u'NO_SETUPTOOLS=1\nDISPLAY=:1.0')
         self.recipe = req.execute('Recipe X WHERE X name "apycot.recipe.quick"').get_entity(0, 0)
+        # reset vcsrepo (using the session )
+        init_vcsrepo(self.repo)
         # reset recipe content
         self.recipe.set_attributes(script=self.recipescript)
         self.lgc = self.add_test_config(u'lgc', env=self.lgce, group=self.pyp, use_recipe=self.recipe)