[hooks] fix start_period_tests due to a change of API in vcsfile draft
authorDavid Douard <david.douard@logilab.fr>
Thu, 20 Nov 2014 20:04:18 +0100
changeset 1774 873c8740c429
parent 1773 35a2edaa84e5
child 1775 7fcbeba48fd4
[hooks] fix start_period_tests due to a change of API in vcsfile HGRepo.branch_heads may return draft changesets. Also make start_period_tests return the list of the started TE eids. The test StartTestTC.launch_all_tests() method has also been updated to ask head revisions to the hg hgepo itself iinstead of trying to compute it from database.
hooks.py
test/unittest_hooks.py
--- a/hooks.py	Sun Nov 09 20:07:45 2014 +0100
+++ b/hooks.py	Thu Nov 20 20:04:18 2014 +0100
@@ -28,6 +28,7 @@
         'Any TC,TCN,TCS,S WHERE '
         'TC computed_start_mode %(sm)s, TC in_state S, S name "activated", '
         'TC name TCN, TC start_rev_deps TCS', {'sm': period})
+    started = []
     for i in xrange(rset.rowcount):
         tc = rset.get_entity(i, 0)
         for env in tc.iter_environments():
@@ -41,10 +42,10 @@
                 # XXX don't use heads_rset because a branch can have multiple
                 # heads, but we only pass the name of the branch to the test,
                 # so only the tip-most head is ever tested
-                heads = tuple(env.repository.branch_head(branch) for branch in
+                heads = tuple(env.repository.branch_head(branch, public_only=True) for branch in
                               env.repository.branches())
             else:
-                head = env.repository.branch_head(branch)
+                head = env.repository.branch_head(branch, public_only=True)
                 if head is None:
                     # No head found for this branch, skip
                     continue
@@ -60,8 +61,8 @@
                     {'rev': head.eid, 'tc': tc.eid}):
                     # This rev have already been tested
                     continue
-                tc.start(env, head.branch)
-
+                started.append(tc.start(env, head.branch).eid)
+    return started
 
 
 class ComputeStartModeHook(hook.Hook):
--- a/test/unittest_hooks.py	Sun Nov 09 20:07:45 2014 +0100
+++ b/test/unittest_hooks.py	Thu Nov 20 20:04:18 2014 +0100
@@ -153,30 +153,28 @@
         TestExecutions are left in status waiting execution. Also fill-in the
         revision which should normally be filled by StartTestOp.
         """
-
         with self.admin_access.client_cnx() as cnx:
-            start_period_tests(cnx, period)
+            started = start_period_tests(cnx, period)
             cnx.commit() # put new te in base
 
         with self.new_access('narval').client_cnx() as cnx:
             ## make sure they are not waiting execution and REV is filled
-            for (te, rev) in cnx.execute('DISTINCT Any TE, REV WHERE TE in_state ST, '
-                                         'ST name "ready", '
-                                         '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'):
+            for (te, repo, branch) in cnx.execute('DISTINCT Any TE, R, BR WHERE '
+                                                  'TE in_state ST, ST name "ready", '
+                                                  'TE using_config TC, '
+                                                  'TC use_environment PE, '
+                                                  'PE local_repository R, '
+                                                  'TE branch BR'):
                 te_e = cnx.entity_from_eid(te)
                 te_e.cw_adapt_to('IWorkflowable').fire_transition('start')
-                rev_e = cnx.entity_from_eid(rev)
+                repo_e = cnx.entity_from_eid(repo)
+                rev = repo_e.cw_adapt_to('VCSRepo').log_rset(revrange='heads(branch(%s) and public())'%branch).one()
                 # force status and revision since we do not have a narvabot eating
                 # our TestExecutions here
                 te_e.cw_set(status=u'running')
                 te_e.cw_set(using_revision=rev)
             cnx.commit() # change new te status
+        return started
 
     def test_new_vc_trigger(self):
         """ check the on new revision start mode. Run all testconfigs, add new
@@ -184,7 +182,7 @@
         with self.admin_access.client_cnx() as cnx:
             pe = cnx.find('ProjectEnvironment').one()
             tc = cnx.find('TestConfig', name='tc_project1').one()
-            recipe = cnx.find('Recipe', name='apycot.quick')
+            recipe = cnx.find('Recipe', name='apycot.python.quick').one()
             tc.cw_set(start_mode=u'on new revision')
             tc2 = self.add_test_config(cnx, u'tc2',
                                        start_mode=u'manual',
@@ -194,96 +192,99 @@
             tc3 = self.add_test_config(cnx, u'tc3',
                                         start_mode=u'on new revision',
                                         check_config=u'branch=stable',
-                                        env=self.lgce,
-                                        use_recipe=self.recipe)
+                                        env=pe,
+                                        use_recipe=recipe)
             ## same repo, branch default
-            lgc4 = self.add_test_config(cnx, u'lgc4',
-                                        start_mode=u'on new revision',
-                                        check_config=u'branch=default',
-                                        env=self.lgce,
-                                        use_recipe=self.recipe)
+            tc4 = self.add_test_config(cnx, u'tc4',
+                                       start_mode=u'on new revision',
+                                       check_config=u'branch=default',
+                                       env=pe,
+                                       use_recipe=recipe)
             ## on different repo
-            lgce2 = cnx.create_entity(
-                'ProjectEnvironment', name=u'lgce2',
+            pe2 = cnx.create_entity(
+                'ProjectEnvironment', name=u'pe2',
                 check_config=u'install=setup_install',
-                vcs_path=u'dir1', #dummy
                 )
-            lgc5 = self.add_test_config(cnx, u'lgc5',
-                                        start_mode=u'on new revision',
-                                        check_config=u'branch=default',
-                                        env=lgce2,
-                                        start_rev_deps=True,
-                                        use_recipe=self.recipe)
+            tc5 = self.add_test_config(cnx, u'tc5',
+                                       start_mode=u'on new revision',
+                                       check_config=u'branch=default',
+                                       env=pe2,
+                                       start_rev_deps=True,
+                                       use_recipe=recipe)
             ## not sure this is still useful
             cnx.commit()
             ## not reaally necessary now, as they all appear, helps understanding,
             ## we check all the necessary testconfig are there
             self.assertEqual(self.get_tc(cnx, period=u'on new revision'),
-                             set([('lgce', 'lgc', None),
-                                  ('lgce', 'lgc3', None),
-                                  ('lgce', 'lgc4', None),
-                                  ('lgce2', 'lgc5', 1)]))
+                             set([('pe_project1', 'tc_project1', None),
+                                  ('pe_project1', 'tc3', None),
+                                  ('pe_project1', 'tc4', None),
+                                  ('pe2', 'tc5', 1)]))
+
         ## create TestExecution entities
-        self.launch_all_tests('on new revision')
+        started = self.launch_all_tests('on new revision')
 
         with self.admin_access.client_cnx() as cnx:
-            nb_tc_lgc_before = len(self.get_te_for_tc(cnx, self.lgc))
-            nb_tc_lgc3_before = len(self.get_te_for_tc(cnx, lgc3.eid))
-            nb_tc_lgc4_before = len(self.get_te_for_tc(cnx, lgc4.eid))
-            nb_tc_lgc5_before = len(self.get_te_for_tc(cnx, lgc5.eid))
+            nb_te_tc_before = len(self.get_te_for_tc(cnx, tc.eid))
+            nb_te_tc3_before = len(self.get_te_for_tc(cnx, tc3.eid))
+            nb_te_tc4_before = len(self.get_te_for_tc(cnx, tc4.eid))
+            nb_te_tc5_before = len(self.get_te_for_tc(cnx, tc5.eid))
             ## check you do not add anything on rerun (no new revision)
 
-        self.launch_all_tests('on new revision')
+        started = self.launch_all_tests('on new revision')
         with self.admin_access.client_cnx() as cnx:
-            self.assertEqual(nb_tc_lgc_before,
-                             len(self.get_te_for_tc(cnx, self.lgc)))
-            self.assertEqual(nb_tc_lgc3_before,
-                             len(self.get_te_for_tc(cnx, lgc3.eid)))
-            self.assertEqual(nb_tc_lgc4_before,
-                             len(self.get_te_for_tc(cnx, lgc4.eid)))
-            self.assertEqual(nb_tc_lgc5_before,
-                             len(self.get_te_for_tc(cnx, lgc5.eid)))
-        r = self.repo
-        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 -u narval' % r.path)
-        with r.internal_cnx() as cnx:
+            self.assertEqual(nb_te_tc_before,
+                             len(self.get_te_for_tc(cnx, tc.eid)))
+            self.assertEqual(nb_te_tc3_before,
+                             len(self.get_te_for_tc(cnx, tc3.eid)))
+            self.assertEqual(nb_te_tc4_before,
+                             len(self.get_te_for_tc(cnx, tc4.eid)))
+            self.assertEqual(nb_te_tc5_before,
+                             len(self.get_te_for_tc(cnx, tc5.eid)))
+            rpath = cnx.find('Repository', title='project1').one().localcachepath
+
+        self.assertEqual(0, os.system('echo data > %s/tutu1.png' % rpath))
+        self.assertEqual(0, os.system('hg -R %s branch stable' % rpath))
+        self.assertEqual(0, os.system('hg -R %s add %s/tutu1.png' % (rpath, rpath)))
+        self.assertEqual(0, os.system('hg -R %s commit -m rien -u narval' % rpath))
+        self.assertEqual(0, os.system('hg -R %s phase -p .' % rpath))
+        with self.repo.internal_cnx() as cnx:
             bridge.import_content(cnx, commitevery=1, raise_on_error=True)
             cnx.commit()
 
-        self.launch_all_tests('on new revision')
+        started = self.launch_all_tests('on new revision')
         with self.admin_access.client_cnx() as cnx:
-            ## now it should add te for the tc linked to lgce and running on new
+            ## now it should add te for the tc linked to pe and running on new
             ## revision and not the others
-            self.assertEqual(nb_tc_lgc_before + 1,
-                             len(self.get_te_for_tc(cnx, self.lgc)))
-            self.assertEqual(nb_tc_lgc3_before + 1,
-                             len(self.get_te_for_tc(cnx, lgc3.eid)))
-            self.assertEqual(nb_tc_lgc4_before,
-                              len(self.get_te_for_tc(cnx, lgc4.eid)))
-            self.assertEqual(nb_tc_lgc5_before,
-                              len(self.get_te_for_tc(cnx, lgc5.eid)))
+            self.assertEqual(nb_te_tc_before + 1,
+                             len(self.get_te_for_tc(cnx, tc.eid)))
+            self.assertEqual(nb_te_tc3_before + 1,
+                             len(self.get_te_for_tc(cnx, tc3.eid)))
+            self.assertEqual(nb_te_tc4_before,
+                              len(self.get_te_for_tc(cnx, tc4.eid)))
+            self.assertEqual(nb_te_tc5_before,
+                              len(self.get_te_for_tc(cnx, tc5.eid)))
 
-        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 -u narval' % r.path))
-        with r.internal_cnx() as cnx:
+        self.assertEqual(0, os.system('echo data > %s/tutu2.png' % rpath))
+        self.assertEqual(0, os.system('hg -R %s up default' % rpath))
+        self.assertEqual(0, os.system('hg -R %s add %s/tutu2.png' % (rpath, rpath)))
+        self.assertEqual(0, os.system('hg -R %s commit -m rien -u narval' % rpath))
+        self.assertEqual(0, os.system('hg -R %s phase -p .' % rpath))
+        with self.repo.internal_cnx() as cnx:
             bridge.import_content(cnx, commitevery=1, raise_on_error=True)
             cnx.commit()
 
         ## it should add te to the tc on default (lgc and lgc4)
         self.launch_all_tests('on new revision')
         with self.admin_access.client_cnx() as cnx:
-            self.assertEqual(nb_tc_lgc_before + 2,
-                             len(self.get_te_for_tc(cnx, self.lgc)))
-            self.assertEqual(nb_tc_lgc3_before + 1,
-                             len(self.get_te_for_tc(cnx, lgc3.eid)))
-            self.assertEqual(nb_tc_lgc4_before + 1,
-                             len(self.get_te_for_tc(cnx, lgc4.eid)))
-            self.assertEqual(nb_tc_lgc5_before,
-                             len(self.get_te_for_tc(cnx, lgc5.eid)))
+            self.assertEqual(nb_te_tc_before + 2,
+                             len(self.get_te_for_tc(cnx, tc.eid)))
+            self.assertEqual(nb_te_tc3_before + 1,
+                             len(self.get_te_for_tc(cnx, tc3.eid)))
+            self.assertEqual(nb_te_tc4_before + 1,
+                             len(self.get_te_for_tc(cnx, tc4.eid)))
+            self.assertEqual(nb_te_tc5_before,
+                             len(self.get_te_for_tc(cnx, tc5.eid)))
 
     def test_datetime_trigger(self):
         """test the registering of TC depending on their start_mode
@@ -293,55 +294,35 @@
         - check they are removed when finished
         """
         with self.admin_access.client_cnx() as cnx:
-            lgc = cnx.entity_from_eid(self.lgc)
+            lgc = cnx.find('TestConfig', name='tc_quick').one()
             lgc.cw_set(start_mode=u'hourly')
-            lgc2 = self.add_test_config(cnx, u'lgc2', start_mode=u'hourly', env=self.lgce)
-            lgce2 = cnx.create_entity(
-                'ProjectEnvironment', name=u'lgce2',
+
+            pe1 = cnx.find('ProjectEnvironment', name='pe_project1').one()
+            tc2 = self.add_test_config(cnx, u'tc2', start_mode=u'hourly', env=pe1)
+            repo1 = cnx.find('Repository', title='project1').one()
+            pe2 = cnx.create_entity(
+                'ProjectEnvironment', name=u'pe2',
                 check_config=u'install=setup_install',
-                vcs_path=u'dir1',
+                local_repository=repo1,
                 )
-            lgc3 = self.add_test_config(cnx, u'lgc3', start_mode=u'hourly',
-                                        check_config=u'branch=default',
-                                        env=lgce2, start_rev_deps=True,
-                                        use_recipe=self.recipe)
-            cnx.execute('SET PE local_repository R WHERE PE name "lgce2", R eid %(r)s',
-                         {'r': self.vcsrepo})
-            cnx.commit()
-            self.assertEqual(self.get_tc(cnx, 'daily'),
-                              set([]))
-            self.assertEqual(self.get_tc(cnx, 'hourly'),
-                              set((('lgce', 'lgc', None),
-                                   ('lgce', 'lgc2', None),
-                                   ('lgce2', 'lgc3', 1)))
-                              )
-            lgc2.cw_adapt_to('IWorkflowable').fire_transition('deactivate')
+            quickrecipe = cnx.find('Recipe', name='apycot.python.quick').one()
+            tc3 = self.add_test_config(cnx, u'tc3', start_mode=u'hourly',
+                                       check_config=u'branch=default',
+                                       env=pe2, start_rev_deps=True,
+                                       use_recipe=quickrecipe)
             cnx.commit()
-            self.assertEqual(self.get_tc(cnx, 'hourly'),
-                              set((('lgce', 'lgc', None),
-                                   ('lgce2', 'lgc3', 1)))
-                              )
-            cnx.commit()
-            rset_before_lgc = self.get_te_for_tc(cnx, self.lgc)
-            te = lgc3.start(lgce2, branch=u'default')
+
+            self.assertEqual(set([]), self.get_tc(cnx, 'daily'))
+            self.assertEqual(set((('pe_project1', 'tc2', None),
+                                  ('pe2', 'tc3', 1))),
+                             self.get_tc(cnx, 'hourly'))
+
+            tc2.cw_adapt_to('IWorkflowable').fire_transition('deactivate')
             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(cnx, 'hourly'),
-                              set((('lgce', 'lgc', None),
-                                   ('lgce2', 'lgc3', 1)))
-                              )
-            rset_before_lgc = self.get_te_for_tc(cnx, self.lgc)
-            rset_before_lgc3 = self.get_te_for_tc(cnx, lgc3.eid)
-        self.launch_all_tests('hourly')
-        with self.admin_access.client_cnx() as cnx:
-            rset_after_lgc = self.get_te_for_tc(cnx, self.lgc)
-            rset_after_lgc3 = self.get_te_for_tc(cnx, 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))
+            self.assertEqual(set((('pe2', 'tc3', 1),)),
+                             self.get_tc(cnx, 'hourly'))
+        started = self.launch_all_tests('hourly')
+        self.assertEqual(1, len(started))
 
     def test_error_in_recipe(self):
         """this testcase should check what happens when a recipe does not have
@@ -363,8 +344,8 @@
 
     def test_start_mode(self):
         with self.admin_access.client_cnx() as cnx:
-            pyp = cnx.entity_from_eid(self.pyp)
-            lgc = cnx.entity_from_eid(self.lgc)
+            pyp = cnx.find('TestConfig', name='PYTHONPACKAGE').one()
+            lgc = cnx.find('TestConfig', name='tc_quick').one()
             pyp.cw_set(start_mode=u'monthly')
             cnx.commit()
             self.assertEqual(pyp.computed_start_mode, pyp.start_mode)