Bug 1420372 - Implemented raise if instance PID doesn't match PID from capabilities and unit test;r=whimboo draft
authorGaluh <galuh.tunggadewi@gmail.com>
Thu, 11 Jan 2018 20:06:14 +0700
changeset 756646 32479696ca46bb51a734e52426388a0e14fc6aa4
parent 716807 ece8a96dfaa436c9bcf53105877b3923a264ae31
push id99516
push userbmo:galuh.tunggadewi@gmail.com
push dateSat, 17 Feb 2018 07:47:03 +0000
reviewerswhimboo
bugs1420372
milestone59.0a1
Bug 1420372 - Implemented raise if instance PID doesn't match PID from capabilities and unit test;r=whimboo MozReview-Commit-ID: 2xF8U422qN8 *** Bug 1420372 - Removed extra white-space characters;r=whimboo MozReview-Commit-ID: 3ejXyRnKsrx *** Bug 1420372 - Changed variable name self.handled_instance_process_id to self.instance_pid;r=whimboo MozReview-Commit-ID: LTrJqVZzMgG
testing/marionette/client/marionette_driver/marionette.py
testing/marionette/harness/marionette_harness/tests/unit/test_marionette.py
--- a/testing/marionette/client/marionette_driver/marionette.py
+++ b/testing/marionette/client/marionette_driver/marionette.py
@@ -1238,16 +1238,23 @@ class Marionette(object):
         if "capabilities" not in body and capabilities is not None:
             body["capabilities"] = dict(capabilities)
 
         resp = self._send_message("newSession", body)
         self.session_id = resp["sessionId"]
         self.session = resp["capabilities"]
         # fallback to processId can be removed in Firefox 55
         self.process_id = self.session.get("moz:processID", self.session.get("processId"))
+
+        if self.instance is not None:
+            self.instance_pid = self.instance.runner.process_handler.pid
+
+            if self.instance_pid != self.process_id:
+                raise errors.MarionetteException("Instance PID '{0}' doesn't match the PID from capabilities {1}".format(self.instance_pid, self.process_id))
+
         self.profile = self.session.get("moz:profile")
 
         return self.session
 
     @property
     def test_name(self):
         return self._test_name
 
--- a/testing/marionette/harness/marionette_harness/tests/unit/test_marionette.py
+++ b/testing/marionette/harness/marionette_harness/tests/unit/test_marionette.py
@@ -4,16 +4,18 @@
 
 import socket
 import time
 
 from marionette_driver import errors
 from marionette_driver.marionette import Marionette
 from marionette_harness import MarionetteTestCase, run_if_manage_instance, skip_if_mobile
 
+from mock import Mock, patch
+
 
 class TestMarionette(MarionetteTestCase):
 
     def test_correct_test_name(self):
         """Test that the correct test name gets set."""
         expected_test_name = '{module}.py {cls}.{func}'.format(
             module=__name__,
             cls=self.__class__.__name__,
@@ -53,17 +55,30 @@ class TestMarionette(MarionetteTestCase)
 
     def test_client_socket_uses_expected_socket_timeout(self):
         current_socket_timeout = self.marionette.socket_timeout
 
         self.assertEqual(current_socket_timeout,
                          self.marionette.client.socket_timeout)
         self.assertEqual(current_socket_timeout,
                          self.marionette.client._sock.gettimeout())
+    
+    def test_start_for_instanceless_client(self):
+        marionette = Marionette(host=self.marionette.host, port=self.marionette.port)
+        marionette.start_session()
+    
+    def test_raise_for_pid_mismatch(self):
+        test_resp = {"sessionId": -1, "capabilities":{"moz:processID": -1}}
 
+        with patch.object(Marionette, "_send_message") as mock_send_message:
+            mock_send_message.return_value = test_resp
+
+            marionette = Marionette(bin=self.marionette.bin, port=2829)
+            self.assertRaises(errors.MarionetteException, marionette.start_session)
+            marionette.cleanup()
 
 class TestContext(MarionetteTestCase):
 
     def setUp(self):
         MarionetteTestCase.setUp(self)
         self.marionette.set_context(self.marionette.CONTEXT_CONTENT)
 
     def get_context(self):