Re-enable test_abandoned_not_timer

- Re-working old layouts into new ones.
- The timer driver was never registered, so do that.
- Registering it revealed bugs in it. Fixing those.

Change-Id: I0fe6095bd7f5391f4e55d7f0356bfd7de9ad5866
Story: 2000773
Task: 3460
diff --git a/tests/base.py b/tests/base.py
index 566ceae..d0727d7 100755
--- a/tests/base.py
+++ b/tests/base.py
@@ -1758,7 +1758,8 @@
 
     def updateConfigLayout(self, path):
         root = os.path.join(self.test_root, "config")
-        os.makedirs(root)
+        if not os.path.exists(root):
+            os.makedirs(root)
         f = tempfile.NamedTemporaryFile(dir=root, delete=False)
         f.write("""
 - tenant:
diff --git a/tests/fixtures/config/single-tenant/git/layout-idle/playbooks/project-bitrot-stable-old.yaml b/tests/fixtures/config/single-tenant/git/layout-idle/playbooks/project-bitrot-stable-old.yaml
new file mode 100644
index 0000000..f679dce
--- /dev/null
+++ b/tests/fixtures/config/single-tenant/git/layout-idle/playbooks/project-bitrot-stable-old.yaml
@@ -0,0 +1,2 @@
+- hosts: all
+  tasks: []
diff --git a/tests/fixtures/config/single-tenant/git/layout-idle/playbooks/project-bitrot-stable-older.yaml b/tests/fixtures/config/single-tenant/git/layout-idle/playbooks/project-bitrot-stable-older.yaml
new file mode 100644
index 0000000..f679dce
--- /dev/null
+++ b/tests/fixtures/config/single-tenant/git/layout-idle/playbooks/project-bitrot-stable-older.yaml
@@ -0,0 +1,2 @@
+- hosts: all
+  tasks: []
diff --git a/tests/fixtures/config/single-tenant/git/layout-idle/playbooks/project-test1.yaml b/tests/fixtures/config/single-tenant/git/layout-idle/playbooks/project-test1.yaml
new file mode 100644
index 0000000..f679dce
--- /dev/null
+++ b/tests/fixtures/config/single-tenant/git/layout-idle/playbooks/project-test1.yaml
@@ -0,0 +1,2 @@
+- hosts: all
+  tasks: []
diff --git a/tests/fixtures/config/single-tenant/git/layout-idle/zuul.yaml b/tests/fixtures/config/single-tenant/git/layout-idle/zuul.yaml
new file mode 100644
index 0000000..f71f3e4
--- /dev/null
+++ b/tests/fixtures/config/single-tenant/git/layout-idle/zuul.yaml
@@ -0,0 +1,27 @@
+- pipeline:
+    name: periodic
+    manager: independent
+    source:
+      gerrit
+    trigger:
+      timer:
+        - time: '* * * * * */1'
+
+- job:
+    name: project-bitrot-stable-old
+    nodes:
+      - name: static
+        image: ubuntu-xenial
+
+- job:
+    name: project-bitrot-stable-older
+    nodes:
+      - name: static
+        image: ubuntu-trusty
+
+- project:
+    name: org/project
+    periodic:
+      jobs:
+        - project-bitrot-stable-old
+        - project-bitrot-stable-older
diff --git a/tests/fixtures/config/single-tenant/git/layout-no-timer/playbooks/project-bitrot-stable-old.yaml b/tests/fixtures/config/single-tenant/git/layout-no-timer/playbooks/project-bitrot-stable-old.yaml
new file mode 100644
index 0000000..f679dce
--- /dev/null
+++ b/tests/fixtures/config/single-tenant/git/layout-no-timer/playbooks/project-bitrot-stable-old.yaml
@@ -0,0 +1,2 @@
+- hosts: all
+  tasks: []
diff --git a/tests/fixtures/config/single-tenant/git/layout-no-timer/playbooks/project-bitrot-stable-older.yaml b/tests/fixtures/config/single-tenant/git/layout-no-timer/playbooks/project-bitrot-stable-older.yaml
new file mode 100644
index 0000000..f679dce
--- /dev/null
+++ b/tests/fixtures/config/single-tenant/git/layout-no-timer/playbooks/project-bitrot-stable-older.yaml
@@ -0,0 +1,2 @@
+- hosts: all
+  tasks: []
diff --git a/tests/fixtures/config/single-tenant/git/layout-no-timer/playbooks/project-test1.yaml b/tests/fixtures/config/single-tenant/git/layout-no-timer/playbooks/project-test1.yaml
new file mode 100644
index 0000000..f679dce
--- /dev/null
+++ b/tests/fixtures/config/single-tenant/git/layout-no-timer/playbooks/project-test1.yaml
@@ -0,0 +1,2 @@
+- hosts: all
+  tasks: []
diff --git a/tests/fixtures/config/single-tenant/git/layout-no-timer/zuul.yaml b/tests/fixtures/config/single-tenant/git/layout-no-timer/zuul.yaml
new file mode 100644
index 0000000..f754e37
--- /dev/null
+++ b/tests/fixtures/config/single-tenant/git/layout-no-timer/zuul.yaml
@@ -0,0 +1,50 @@
+- pipeline:
+    name: check
+    manager: independent
+    source:
+      gerrit
+    trigger:
+      gerrit:
+        - event: patchset-created
+    success:
+      gerrit:
+        verified: 1
+    failure:
+      gerrit:
+        verified: -1
+
+- pipeline:
+    name: periodic
+    manager: independent
+    # Trigger is required, set it to one that is a noop
+    # during tests that check the timer trigger.
+    source:
+      gerrit
+    trigger:
+      gerrit:
+        - event: ref-updated
+
+- job:
+    name: project-test1
+
+- job:
+    name: project-bitrot-stable-old
+    nodes:
+      - name: static
+        image: ubuntu-xenial
+
+- job:
+    name: project-bitrot-stable-older
+    nodes:
+      - name: static
+        image: ubuntu-trusty
+
+- project:
+    name: org/project
+    check:
+      jobs:
+        - project-test1
+    periodic:
+      jobs:
+        - project-bitrot-stable-old
+        - project-bitrot-stable-older
diff --git a/tests/fixtures/layout-idle.yaml b/tests/fixtures/layout-idle.yaml
deleted file mode 100644
index 0870788..0000000
--- a/tests/fixtures/layout-idle.yaml
+++ /dev/null
@@ -1,12 +0,0 @@
-pipelines:
-  - name: periodic
-    manager: IndependentPipelineManager
-    trigger:
-      timer:
-        - time: '* * * * * */1'
-
-projects:
-  - name: org/project
-    periodic:
-      - project-bitrot-stable-old
-      - project-bitrot-stable-older
diff --git a/tests/fixtures/layout-no-timer.yaml b/tests/fixtures/layout-no-timer.yaml
deleted file mode 100644
index ca40d13..0000000
--- a/tests/fixtures/layout-no-timer.yaml
+++ /dev/null
@@ -1,28 +0,0 @@
-pipelines:
-  - name: check
-    manager: IndependentPipelineManager
-    trigger:
-      gerrit:
-        - event: patchset-created
-    success:
-      gerrit:
-        verified: 1
-    failure:
-      gerrit:
-        verified: -1
-
-  - name: periodic
-    manager: IndependentPipelineManager
-    # Trigger is required, set it to one that is a noop
-    # during tests that check the timer trigger.
-    trigger:
-      gerrit:
-        - event: ref-updated
-
-projects:
-  - name: org/project
-    check:
-      - project-test1
-    periodic:
-      - project-bitrot-stable-old
-      - project-bitrot-stable-older
diff --git a/tests/unit/test_scheduler.py b/tests/unit/test_scheduler.py
index 6426692..06ec70d 100755
--- a/tests/unit/test_scheduler.py
+++ b/tests/unit/test_scheduler.py
@@ -1638,27 +1638,28 @@
         self.assertEqual(A.reported, 0, "Abandoned change should not report")
         self.assertEqual(B.reported, 1, "Change should report")
 
-    @skip("Disabled for early v3 development")
     def test_abandoned_not_timer(self):
         "Test that an abandoned change does not cancel timer jobs"
 
         self.launch_server.hold_jobs_in_build = True
 
         # Start timer trigger - also org/project
-        self.updateConfigLayout(
-            'tests/fixtures/layout-idle.yaml')
+        self.updateConfigLayout('layout-idle')
         self.sched.reconfigure(self.config)
-        self.registerJobs()
         # The pipeline triggers every second, so we should have seen
         # several by now.
         time.sleep(5)
         self.waitUntilSettled()
         # Stop queuing timer triggered jobs so that the assertions
         # below don't race against more jobs being queued.
-        self.updateConfigLayout(
-            'tests/fixtures/layout-no-timer.yaml')
+        # Must be in same repo, so overwrite config with another one
+        no_timer_path = os.path.join(self.test_root, 'upstream',
+                                     'layout-no-timer', 'zuul.yaml')
+        with open(no_timer_path, 'r') as nt:
+            self.addCommitToRepo('layout-idle', 'Removing timer jobs',
+                                 {'zuul.yaml': nt.read()})
+
         self.sched.reconfigure(self.config)
-        self.registerJobs()
         self.assertEqual(len(self.builds), 2, "Two timer jobs")
 
         A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
diff --git a/zuul/driver/gerrit/gerritconnection.py b/zuul/driver/gerrit/gerritconnection.py
index 627c716..683c8ff 100644
--- a/zuul/driver/gerrit/gerritconnection.py
+++ b/zuul/driver/gerrit/gerritconnection.py
@@ -282,6 +282,7 @@
             change.url = self._getGitwebUrl(project, sha=event.newrev)
         else:
             # TODOv3(jeblair): we need to get the project from the event
+            project = self.getProject(event.project_name)
             change = NullChange(project)
         return change
 
diff --git a/zuul/driver/timer/__init__.py b/zuul/driver/timer/__init__.py
index b8979c4..a188a26 100644
--- a/zuul/driver/timer/__init__.py
+++ b/zuul/driver/timer/__init__.py
@@ -49,7 +49,7 @@
     def _addJobs(self, tenant):
         jobs = []
         self.tenant_jobs[tenant.name] = jobs
-        for pipeline in tenant.layout.pipelines:
+        for pipeline in tenant.layout.pipelines.values():
             for ef in pipeline.manager.event_filters:
                 if not isinstance(ef.trigger, timertrigger.TimerTrigger):
                     continue
@@ -88,8 +88,8 @@
     def stop(self):
         self.apsched.shutdown()
 
-    def getTrigger(self, connection_name):
-        return timertrigger.TimerTrigger(self)
+    def getTrigger(self, connection_name, config=None):
+        return timertrigger.TimerTrigger(self, config)
 
     def getTriggerSchema(self):
         return timertrigger.getSchema()
diff --git a/zuul/launcher/client.py b/zuul/launcher/client.py
index 458aeaf..5b60506 100644
--- a/zuul/launcher/client.py
+++ b/zuul/launcher/client.py
@@ -42,6 +42,10 @@
         oldrev = item.change.oldrev
         newrev = item.change.newrev
         branch = item.change.ref
+    else:
+        oldrev = None
+        newrev = None
+        branch = None
     connection_name = item.pipeline.source.connection.connection_name
     project = item.change.project.name
 
diff --git a/zuul/lib/connections.py b/zuul/lib/connections.py
index 35b4417..0a8be03 100644
--- a/zuul/lib/connections.py
+++ b/zuul/lib/connections.py
@@ -17,6 +17,7 @@
 import zuul.driver.zuul
 import zuul.driver.gerrit
 import zuul.driver.smtp
+import zuul.driver.timer
 from zuul.connection import BaseConnection
 
 
@@ -34,6 +35,7 @@
         self.registerDriver(zuul.driver.zuul.ZuulDriver())
         self.registerDriver(zuul.driver.gerrit.GerritDriver())
         self.registerDriver(zuul.driver.smtp.SMTPDriver())
+        self.registerDriver(zuul.driver.timer.TimerDriver())
 
     def registerDriver(self, driver):
         if driver.name in self.drivers: