Set remote url on every getRepo in merger

In case we run on github with apps we have an access token in the git
url. This access token is only valid for a specific period of
time. Currently the merger caches its repos and never changes the git
url on subsequent merge or cat requests. After timeout the merger then
faces an exception [1].

The fix is to update the remote url on every getRepo call in the
merger. This makes sure that we have a recent access token on every
merge or cat call.

Further the executor has the same problems and gets fixed by this too
as it also uses getRepo from the merger.

[1] Exception:
 2017-12-20 07:00:00,874 ERROR zuul.Merger: Unable to update github/sandbox/sandbox
 Traceback (most recent call last):
   File "/usr/lib/python3.6/site-packages/zuul/merger/merger.py", line 382, in updateRepo
     repo.reset()
   File "/usr/lib/python3.6/site-packages/zuul/merger/merger.py", line 143, in reset
     self.update()
   File "/usr/lib/python3.6/site-packages/zuul/merger/merger.py", line 293, in update
     self._git_fetch(repo, 'origin', tags=True)
   File "/usr/lib/python3.6/site-packages/zuul/merger/merger.py", line 133, in _git_fetch
     **kwargs)
   File "/usr/lib/python3.6/site-packages/git/cmd.py", line 551, in <lambda>
     return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
   File "/usr/lib/python3.6/site-packages/git/cmd.py", line 1010, in _call_process
     return self.execute(call, **exec_kwargs)
   File "/usr/lib/python3.6/site-packages/git/cmd.py", line 821, in execute
     raise GitCommandError(command, status, stderr_value, stdout_value)
 git.exc.GitCommandError: Cmd('git') failed due to: exit code(128)
   cmdline: git fetch --tags origin
   stderr: 'remote: Invalid username or password.
 fatal: Authentication failed for 'https://x-access-token:v1.c8ec09e233b16871b64843adeec5fb048a383fba@github.example.com/sandbox/sandbox/''

Change-Id: If990dc48e6c10c24b6b32db5f5711fc3608bdfe4
diff --git a/tests/base.py b/tests/base.py
index f68f59a..c13519e 100755
--- a/tests/base.py
+++ b/tests/base.py
@@ -941,7 +941,7 @@
     log = logging.getLogger("zuul.test.FakeGithubConnection")
 
     def __init__(self, driver, connection_name, connection_config, rpcclient,
-                 changes_db=None, upstream_root=None):
+                 changes_db=None, upstream_root=None, git_url_with_auth=False):
         super(FakeGithubConnection, self).__init__(driver, connection_name,
                                                    connection_config)
         self.connection_name = connection_name
@@ -953,6 +953,7 @@
         self.merge_not_allowed_count = 0
         self.reports = []
         self.github_client = tests.fakegithub.FakeGithub(changes_db)
+        self.git_url_with_auth = git_url_with_auth
         self.rpcclient = rpcclient
 
     def getGithubClient(self,
@@ -1045,7 +1046,13 @@
                     return 'read'
 
     def getGitUrl(self, project):
-        return os.path.join(self.upstream_root, str(project))
+        if self.git_url_with_auth:
+            auth_token = ''.join(
+                random.choice(string.ascii_lowercase) for x in range(8))
+            prefix = 'file://x-access-token:%s@' % auth_token
+        else:
+            prefix = ''
+        return prefix + os.path.join(self.upstream_root, str(project))
 
     def real_getGitUrl(self, project):
         return super(FakeGithubConnection, self).getGitUrl(project)
@@ -1901,6 +1908,7 @@
     run_ansible = False
     create_project_keys = False
     use_ssl = False
+    git_url_with_auth = False
 
     def _startMerger(self):
         self.merge_server = zuul.merger.server.MergeServer(self.config,
@@ -2070,10 +2078,12 @@
         def getGithubConnection(driver, name, config):
             server = config.get('server', 'github.com')
             db = self.github_changes_dbs.setdefault(server, {})
-            con = FakeGithubConnection(driver, name, config,
-                                       self.rpcclient,
-                                       changes_db=db,
-                                       upstream_root=self.upstream_root)
+            con = FakeGithubConnection(
+                driver, name, config,
+                self.rpcclient,
+                changes_db=db,
+                upstream_root=self.upstream_root,
+                git_url_with_auth=self.git_url_with_auth)
             self.event_queues.append(con.event_queue)
             setattr(self, 'fake_' + name, con)
             return con