Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

align test session id format with cli command #52

Merged
merged 4 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions nose_launchable/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -78,12 +77,12 @@ 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_context = TestSessionContext(
build_number=build_number, test_session_id=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_context.get_build_path()]
if options is not None:
cmd.extend([option.strip() for option in options.split(' ')])
cmd.append('file')
Expand Down Expand Up @@ -111,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)
Expand All @@ -126,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(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, I haven't noticed we could remove self.build_number but this makes sense!

)

res = self.http.patch(url, headers=self._headers())
Expand All @@ -147,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)
65 changes: 46 additions & 19 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__


Expand Down Expand Up @@ -55,12 +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(1, client.test_session_id)
self.assertEqual("test_build_number",
client.build_number)
self.assertEqual(
"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")
Expand All @@ -74,15 +77,19 @@ 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_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', '/test_sessions/1', '--target', '10%', 'file']
expected_command = ['launchable', 'subset', '--session',
'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(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):
Expand All @@ -97,15 +104,20 @@ 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_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)
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):
Expand All @@ -120,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")
Expand All @@ -147,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)

Expand Down Expand Up @@ -192,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()

Expand All @@ -208,3 +224,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")