testing: store state file with MozReview instance state
authorGregory Szorc <gps@mozilla.com>
Mon, 19 Jan 2015 16:45:58 -0500
changeset 360147 54e830743de97f1cbeb1a249df9bb5378b090622
parent 360146 a01c43122597e5e6e82880902c1f91e4a5a8329b
child 360148 5ec8aa1e605312902dcff70481108476a09c8fe4
push id16998
push userrwood@mozilla.com
push dateMon, 02 May 2016 19:42:03 +0000
testing: store state file with MozReview instance state This will be used in upcoming patches to allow tools to interact with a previously-created MozReview instance. As part of this, writing of some old state files has been refactored slightly.
testing/vcttesting/mozreview.py
--- a/testing/vcttesting/mozreview.py
+++ b/testing/vcttesting/mozreview.py
@@ -1,12 +1,13 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
+import json
 import os
 import signal
 import socket
 import subprocess
 import time
 
 import psutil
 
@@ -46,45 +47,58 @@ class MozReview(object):
         path = os.path.abspath(path)
         self._path = path
 
         self._name = os.path.dirname(path)
 
         if not os.path.exists(path):
             os.mkdir(path)
 
+        self._state_path = os.path.join(path, 'state.json')
+
         docker_state = os.path.join(path, 'docker-state.json')
 
         self._docker_state = docker_state
 
         docker_url, tls = params_from_env(os.environ)
         self._docker = Docker(docker_state, docker_url, tls=tls)
 
         if not self._docker.is_alive():
             raise Exception('Docker is not available.')
 
-    def get_bugzilla(self, url, username='admin@example.com', password='password'):
-        return Bugzilla(url, username=username, password=password)
+        self.bugzilla_username = None
+        self.bugzilla_password = None
+
+        if os.path.exists(self._state_path):
+            with open(self._state_path, 'rb') as fh:
+                state = json.load(fh)
+
+                for k, v in state.items():
+                    setattr(self, k, v)
+
+    def get_bugzilla(self, username=None, password=None):
+        username = username or self.bugzilla_username or 'admin@example.com'
+        password = password or self.bugzilla_password or 'password'
+
+        return Bugzilla(self.bugzilla_url, username=username, password=password)
 
     def start(self, bugzilla_port=None, reviewboard_port=None,
             mercurial_port=None, verbose=False, db_image=None, web_image=None):
         """Start a MozReview instance."""
         if not bugzilla_port:
             bugzilla_port = get_available_port()
         if not reviewboard_port:
             reviewboard_port = get_available_port()
         if not mercurial_port:
             mercurial_port = get_available_port()
 
         bugzilla_url = self._docker.start_mozreview(cluster=self._name,
                 hostname=None, http_port=bugzilla_port,
                 db_image=db_image, web_image=web_image,
                 verbose=verbose)[0]
-        with open(self._bugzilla_url_path, 'wb') as fh:
-            fh.write(bugzilla_url)
 
         bugzilla = self.get_bugzilla(bugzilla_url)
 
         rb = MozReviewBoard(self._path, bugzilla_url=bugzilla_url)
         rb.create()
         reviewboard_pid = rb.start(reviewboard_port)
 
         reviewboard_url = 'http://localhost:%s/' % reviewboard_port
@@ -98,16 +112,29 @@ class MozReview(object):
         mercurial_pid = self._start_mercurial_server(mercurial_port)
 
         self.mercurial_url = 'http://localhost:%s/' % mercurial_port
         with open(self._hg_url_path, 'w') as fh:
             fh.write(self.mercurial_url)
 
         self.mercurial_pid = mercurial_pid
 
+        state = {
+            'bugzilla_url': bugzilla_url,
+            'reviewboard_url': reviewboard_url,
+            'reviewboard_pid': reviewboard_pid,
+            'mercurial_url': self.mercurial_url,
+            'mercurial_pid': mercurial_pid,
+            'admin_username': bugzilla.username,
+            'admin_password': bugzilla.password,
+        }
+
+        with open(self._state_path, 'wb') as fh:
+            json.dump(state, fh, indent=2, sort_keys=True)
+
     def stop(self):
         """Stop all services associated with this MozReview instance."""
         if os.path.exists(self._hg_pid_path):
             with open(self._hg_pid_path, 'rb') as fh:
                 pid = int(fh.read().strip())
                 kill(pid)
 
             os.unlink(self._hg_pid_path)
@@ -135,38 +162,31 @@ class MozReview(object):
                 '[reviewboard]',
                 'repoid = %s' % rbid,
                 '',
             ]))
 
         return url, rbid
 
     def create_user(self, email, password, fullname):
-        with open(self._bugzilla_url_path, 'r') as fh:
-            url = fh.read()
-
-        b = self.get_bugzilla(url)
+        b = self.get_bugzilla()
         return b.create_user(email, password, fullname)
 
     @property
     def _hg_pid_path(self):
         return os.path.join(self._path, 'hg.pid')
 
     @property
     def _hg_url_path(self):
         return os.path.join(self._path, 'hg.url')
 
     @property
     def _hg(self):
         return os.path.join(ROOT, 'venv', 'bin', 'hg')
 
-    @property
-    def _bugzilla_url_path(self):
-        return os.path.join(self._path, 'bugzilla.url')
-
     def _start_mercurial_server(self, port):
         repos_path = os.path.join(self._path, 'repos')
         if not os.path.exists(repos_path):
             os.mkdir(repos_path)
 
         rb_ext_path = os.path.join(ROOT, 'hgext', 'reviewboard', 'server.py')
         dummyssh = os.path.join(ROOT, 'pylib', 'mercurial-support', 'dummyssh')