From 6a750127fedf86baaa5d768361393370bdb2ccc4 Mon Sep 17 00:00:00 2001 From: Ryosuke Yabuki Date: Tue, 24 Aug 2021 10:35:01 +0900 Subject: [PATCH 1/4] align test session id format with cli command --- nose_launchable/client.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/nose_launchable/client.py b/nose_launchable/client.py index 2dfac95..59165cd 100644 --- a/nose_launchable/client.py +++ b/nose_launchable/client.py @@ -78,12 +78,11 @@ def start(self, build_number): response_body = res.json() logger.debug("Response body: {}".format(response_body)) - self.test_session_id = response_body['id'] + self.test_session_id = "builds/{}/test_sessions/{}".format( + self.build_number, response_body['id']) def subset(self, test_names, options, target): - url = "/test_sessions/{}".format(self.test_session_id) - - cmd = ['launchable', 'subset', '--session', url] + cmd = ['launchable', 'subset', '--session', self.test_session_id] if options is not None: cmd.extend([option.strip() for option in options.split(' ')]) cmd.append('file') From 83beb539d1365178a4d5ee6462368b182eb3f77e Mon Sep 17 00:00:00 2001 From: Ryosuke Yabuki Date: Tue, 24 Aug 2021 10:45:47 +0900 Subject: [PATCH 2/4] fix test_session_id format --- tests/test_client.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/tests/test_client.py b/tests/test_client.py index 40faca2..70615ec 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -60,7 +60,8 @@ def test_start(self): mock_response.json.assert_called_once_with() self.assertEqual("test_build_number", client.build_number) - self.assertEqual(1, client.test_session_id) + self.assertEqual( + "builds/test_build_number/test_sessions/1", client.test_session_id) def test_subset_success_with_target(self): mock_output = MagicMock(name="output") @@ -74,15 +75,18 @@ def test_subset_success_with_target(self): mock_requests = MagicMock(name="requests") - client = LaunchableClient("base_url", "org_name", "wp_name", "token", mock_requests, mock_subprocess) - client.test_session_id = 1 + client = LaunchableClient( + "base_url", "org_name", "wp_name", "token", mock_requests, mock_subprocess) + client.test_session_id = "builds/test_subset_success_with_target/1" got = client.subset(["tests/test1.py", "tests/test2.py"], None, "10") - expected_command = ['launchable', 'subset', '--session', '/test_sessions/1', '--target', '10%', 'file'] + expected_command = ['launchable', 'subset', '--session', + 'builds/test_subset_success_with_target/1', '--target', '10%', 'file'] expected_input = 'tests/test1.py\ntests/test2.py' - mock_subprocess.run.assert_called_once_with(expected_command, input=expected_input, encoding='utf-8', stdout='PIPE', stderr='PIPE') + mock_subprocess.run.assert_called_once_with( + expected_command, input=expected_input, encoding='utf-8', stdout='PIPE', stderr='PIPE') self.assertEqual(['tests/test2.py', 'tests/test1.py'], got) def test_subset_success_with_options(self): @@ -97,15 +101,19 @@ def test_subset_success_with_options(self): mock_requests = MagicMock(name="requests") - client = LaunchableClient("base_url", "org_name", "wp_name", "token", mock_requests, mock_subprocess) - client.test_session_id = 1 + client = LaunchableClient( + "base_url", "org_name", "wp_name", "token", mock_requests, mock_subprocess) + client.test_session_id = "builds/test_subset_success_with_options/test_sessions/1" - got = client.subset(["tests/test1.py", "tests/test2.py"], '--target 10%', None) + got = client.subset( + ["tests/test1.py", "tests/test2.py"], '--target 10%', None) - expected_command = ['launchable', 'subset', '--session', '/test_sessions/1', '--target', '10%', 'file'] + expected_command = ['launchable', 'subset', '--session', + 'builds/test_subset_success_with_options/test_sessions/1', '--target', '10%', 'file'] expected_input = 'tests/test1.py\ntests/test2.py' - mock_subprocess.run.assert_called_once_with(expected_command, input=expected_input, encoding='utf-8', stdout='PIPE', stderr='PIPE') + mock_subprocess.run.assert_called_once_with( + expected_command, input=expected_input, encoding='utf-8', stdout='PIPE', stderr='PIPE') self.assertEqual(['tests/test2.py', 'tests/test1.py'], got) def test_subset_failure(self): From 2009ff16ecc43dff4bf4bc98458d62080cda44cf Mon Sep 17 00:00:00 2001 From: Ryosuke Yabuki Date: Tue, 24 Aug 2021 14:33:48 +0900 Subject: [PATCH 3/4] introduce TestSessionContext to handle test_session and test_session_id --- nose_launchable/client.py | 29 ++++++++++++++++++----------- tests/test_client.py | 11 +++++++++++ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/nose_launchable/client.py b/nose_launchable/client.py index 59165cd..dd1afee 100644 --- a/nose_launchable/client.py +++ b/nose_launchable/client.py @@ -59,11 +59,10 @@ def __init__(self, base_url, org_name, workspace_name, token, http, process): self.http = http self.process = process self.build_number = None - self.test_session_id = None + self.test_session_context = None def start(self, build_number): self.build_number = build_number - url = "{}/intake/organizations/{}/workspaces/{}/builds/{}/test_sessions".format( self.base_url, self.org_name, @@ -78,11 +77,12 @@ def start(self, build_number): response_body = res.json() logger.debug("Response body: {}".format(response_body)) - self.test_session_id = "builds/{}/test_sessions/{}".format( - self.build_number, response_body['id']) + self.test_session_context = TestSessionContext( + build_number=build_number, test_session_id=response_body["id"]) def subset(self, test_names, options, target): - cmd = ['launchable', 'subset', '--session', self.test_session_id] + cmd = ['launchable', 'subset', '--session', + self.test_session_context.get_build_path()] if options is not None: cmd.extend([option.strip() for option in options.split(' ')]) cmd.append('file') @@ -110,12 +110,11 @@ def subset(self, test_names, options, target): return order def upload_events(self, events): - url = "{}/intake/organizations/{}/workspaces/{}/builds/{}/test_sessions/{}/events".format( + url = "{}/intake/organizations/{}/workspaces/{}/{}/events".format( self.base_url, self.org_name, self.workspace_name, - self.build_number, - self.test_session_id + self.test_session_context.get_build_path(), ) request_body = self._upload_request_body(events) @@ -125,12 +124,11 @@ def upload_events(self, events): res.raise_for_status() def finish(self): - url = "{}/intake/organizations/{}/workspaces/{}/builds/{}/test_sessions/{}/close".format( + url = "{}/intake/organizations/{}/workspaces/{}/{}/close".format( self.base_url, self.org_name, self.workspace_name, - self.build_number, - self.test_session_id + self.test_session_context.get_build_path(), ) res = self.http.patch(url, headers=self._headers()) @@ -146,3 +144,12 @@ def _headers(self): def _upload_request_body(self, events): return {"events": [event.to_body() for event in events]} + + +class TestSessionContext: + def __init__(self, build_number=None, test_session_id=None): + self.build_number = build_number + self.test_session_id = test_session_id + + def get_build_path(self): + return "builds/{}/test_sessions/{}".format(self.build_number, self.test_session_id) diff --git a/tests/test_client.py b/tests/test_client.py index 70615ec..42353df 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -216,3 +216,14 @@ def test_finish(self): mock_requests.patch.assert_called_once_with(expected_url, headers=expected_headers) mock_response.raise_for_status.assert_called_once_with() + + +class TestTSessionContext(unittest.TestCase): + def test_get_build_path(self): + context = TestSessionContext(build_number="1", test_session_id="2") + self.assertEqual(context.get_build_path(), + "builds/1/test_sessions/2") + + context = TestSessionContext(build_number="aaa", test_session_id="bbb") + self.assertEqual(context.get_build_path(), + "builds/aaa/test_sessions/bbb") From 3ed2c551920d3aa37bfc8ef086bb813dd49f7e61 Mon Sep 17 00:00:00 2001 From: Ryosuke Yabuki Date: Tue, 24 Aug 2021 14:39:09 +0900 Subject: [PATCH 4/4] fix test --- tests/test_client.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/tests/test_client.py b/tests/test_client.py index 42353df..9bb43fa 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -4,7 +4,7 @@ from unittest.mock import MagicMock from nose_launchable.case_event import CaseEvent -from nose_launchable.client import LaunchableClientFactory, LaunchableClient +from nose_launchable.client import LaunchableClientFactory, LaunchableClient, TestSessionContext from nose_launchable.version import __version__ @@ -55,13 +55,15 @@ def test_start(self): 'Authorization': 'Bearer token' } - mock_requests.post.assert_called_once_with(expected_url, headers=expected_headers) + mock_requests.post.assert_called_once_with( + expected_url, headers=expected_headers) mock_response.raise_for_status.assert_called_once_with() mock_response.json.assert_called_once_with() - self.assertEqual("test_build_number", client.build_number) + self.assertEqual("test_build_number", + client.build_number) self.assertEqual( - "builds/test_build_number/test_sessions/1", client.test_session_id) + "builds/test_build_number/test_sessions/1", client.test_session_context.get_build_path()) def test_subset_success_with_target(self): mock_output = MagicMock(name="output") @@ -77,12 +79,13 @@ def test_subset_success_with_target(self): client = LaunchableClient( "base_url", "org_name", "wp_name", "token", mock_requests, mock_subprocess) - client.test_session_id = "builds/test_subset_success_with_target/1" + client.test_session_context = TestSessionContext( + build_number="test_subset_success_with_target", test_session_id=1) got = client.subset(["tests/test1.py", "tests/test2.py"], None, "10") expected_command = ['launchable', 'subset', '--session', - 'builds/test_subset_success_with_target/1', '--target', '10%', 'file'] + 'builds/test_subset_success_with_target/test_sessions/1', '--target', '10%', 'file'] expected_input = 'tests/test1.py\ntests/test2.py' mock_subprocess.run.assert_called_once_with( @@ -103,7 +106,8 @@ def test_subset_success_with_options(self): client = LaunchableClient( "base_url", "org_name", "wp_name", "token", mock_requests, mock_subprocess) - client.test_session_id = "builds/test_subset_success_with_options/test_sessions/1" + client.test_session_context = TestSessionContext( + build_number="test_subset_success_with_options", test_session_id=1) got = client.subset( ["tests/test1.py", "tests/test2.py"], '--target 10%', None) @@ -128,8 +132,10 @@ def test_subset_failure(self): mock_requests = MagicMock(name="requests") - client = LaunchableClient("base_url", "org_name", "wp_name", "token", mock_requests, mock_subprocess) - client.test_session_id = 1 + client = LaunchableClient( + "base_url", "org_name", "wp_name", "token", mock_requests, mock_subprocess) + client.test_session_context = TestSessionContext( + build_number="test", test_session_id=1) with self.assertRaises(RuntimeError): client.subset(["tests/test1.py", "tests/test2.py"], None, "10") @@ -155,7 +161,8 @@ def test_upload_events(self): ] client.build_number = 1 - client.test_session_id = 2 + client.test_session_context = TestSessionContext( + build_number=1, test_session_id=2) client.upload_events(events) @@ -200,9 +207,10 @@ def test_finish(self): mock_requests = MagicMock(name="requests") mock_requests.patch.return_value = mock_response - client = LaunchableClient("base_url", "org_name", "wp_name", "token", mock_requests, MagicMock(name="subprecess")) - client.build_number = "test_build_number" - client.test_session_id = "1" + client = LaunchableClient("base_url", "org_name", "wp_name", + "token", mock_requests, MagicMock(name="subprecess")) + client.test_session_context = TestSessionContext( + build_number="test_build_number", test_session_id=1) client.finish()