Skip to content

Commit 5efc0f6

Browse files
mukulmurthyMukul Murthy
authored andcommitted
Fix pipelines request paths for delete, get, and reset commands (#274)
Previous change passed unit tests which made sure the correct call was made to the generated client, but didn't verify the generated client actually makes the right requests. Fixed service.py to send the right requests and updated unit tests to make sure the called paths are correct. Also made the `databricks pipelines get` command print the server's response.
1 parent 01932ec commit 5efc0f6

4 files changed

Lines changed: 44 additions & 34 deletions

File tree

databricks_cli/pipelines/api.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,14 @@ def deploy(self, spec, headers=None):
4848

4949
spec['libraries'] = LibraryObject.to_json(external_lib_objects +
5050
self._upload_local_libraries(local_lib_objects))
51-
self.client.client.perform_query('PUT',
52-
'/pipelines/{}'.format(spec['id']),
53-
data=spec,
54-
headers=headers)
51+
pipeline_id = spec['id']
52+
self.client.deploy_spec(pipeline_id, spec, headers)
5553

5654
def delete(self, pipeline_id, headers=None):
5755
self.client.delete(pipeline_id, headers)
5856

5957
def get(self, pipeline_id, headers=None):
60-
self.client.get(pipeline_id, headers)
58+
return self.client.get(pipeline_id, headers)
6159

6260
def reset(self, pipeline_id, headers=None):
6361
self.client.reset(pipeline_id, headers)

databricks_cli/pipelines/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import click
2828

2929
from databricks_cli.click_types import PipelineSpecClickType, PipelineIdClickType
30-
from databricks_cli.utils import eat_exceptions, CONTEXT_SETTINGS
30+
from databricks_cli.utils import eat_exceptions, CONTEXT_SETTINGS, pretty_format
3131
from databricks_cli.version import print_version_callback, version
3232
from databricks_cli.pipelines.api import PipelinesApi
3333
from databricks_cli.configure.config import provide_api_client, profile_option, debug_option
@@ -120,7 +120,7 @@ def get_cli(api_client, spec_arg, spec, pipeline_id):
120120
databricks pipelines get --pipeline-id 1234
121121
"""
122122
pipeline_id = _get_pipeline_id(spec_arg=spec_arg, spec=spec, pipeline_id=pipeline_id)
123-
PipelinesApi(api_client).get(pipeline_id)
123+
click.echo(pretty_format(PipelinesApi(api_client).get(pipeline_id)))
124124

125125

126126
@click.command(context_settings=CONTEXT_SETTINGS,

databricks_cli/sdk/service.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -825,21 +825,24 @@ def deploy(self, pipeline_id=None, id=None, name=None, storage=None, configurati
825825
raise TypeError('Expected databricks.Filters() or dict for field filters')
826826
return self.client.perform_query('PUT', '/pipelines/{pipeline_id}', data=_data, headers=headers)
827827

828+
def deploy_spec(self, pipeline_id=None, spec={}, headers=None):
829+
return self.client.perform_query('PUT', '/pipelines/{}'.format(pipeline_id), data=spec, headers=headers)
830+
828831
def delete(self, pipeline_id=None, headers=None):
829832
_data = {}
830833
if pipeline_id is not None:
831834
_data['pipeline_id'] = pipeline_id
832-
return self.client.perform_query('DELETE', '/pipelines/{pipeline_id}', data=_data, headers=headers)
835+
return self.client.perform_query('DELETE', '/pipelines/{}'.format(pipeline_id), data=_data, headers=headers)
833836

834837
def get(self, pipeline_id=None, headers=None):
835838
_data = {}
836839
if pipeline_id is not None:
837840
_data['pipeline_id'] = pipeline_id
838-
return self.client.perform_query('GET', '/pipelines/{pipeline_id}', data=_data, headers=headers)
841+
return self.client.perform_query('GET', '/pipelines/{}'.format(pipeline_id), data={}, headers=headers)
839842

840843
def reset(self, pipeline_id=None, headers=None):
841844
_data = {}
842845
if pipeline_id is not None:
843846
_data['pipeline_id'] = pipeline_id
844-
return self.client.perform_query('POST', '/pipelines/{pipeline_id}/reset', data=_data, headers=headers)
847+
return self.client.perform_query('POST', '/pipelines/{}/reset'.format(pipeline_id), data=_data, headers=headers)
845848

tests/pipelines/test_api.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,25 @@
3737
PIPELINE_ID = '123456'
3838
SPEC = {
3939
'id': PIPELINE_ID,
40-
'name': 'test_pipeline'
40+
'name': 'test_pipeline',
41+
'storage': 'dbfs:/path'
4142
}
42-
HEADERS = 'dummy_headers'
43+
ID_ONLY_SPEC = {
44+
'pipeline_id': PIPELINE_ID,
45+
}
46+
HEADERS = {'dummy_header': 'dummy_value'}
4347

4448

4549
@pytest.fixture()
4650
def pipelines_api():
47-
with mock.patch('databricks_cli.pipelines.api.DeltaPipelinesService') \
48-
as DeltaPipelinesServiceMock:
49-
DeltaPipelinesServiceMock.return_value = mock.MagicMock()
50-
_pipelines_api = api.PipelinesApi(None)
51-
yield _pipelines_api
51+
client_mock = mock.MagicMock()
52+
53+
def server_response(*args, **kwargs):
54+
if args[0] == 'GET':
55+
return {'pipeline_id': PIPELINE_ID, 'state': 'RUNNING'}
56+
client_mock.perform_query = mock.MagicMock(side_effect=server_response)
57+
_pipelines_api = api.PipelinesApi(client_mock)
58+
yield _pipelines_api
5259

5360

5461
def file_exists_stub(_, dbfs_path):
@@ -75,7 +82,6 @@ def test_deploy(put_file_mock, dbfs_path_validate, pipelines_api, tmpdir):
7582
A test local file which has '456' written to it is not present in Dbfs and therefore must be.
7683
uploaded to dbfs.
7784
"""
78-
deploy_mock = pipelines_api.client.client.perform_query
7985
# set-up the test
8086
jar1 = tmpdir.join('jar1.jar').strpath
8187
jar2 = tmpdir.join('jar2.jar').strpath
@@ -122,39 +128,42 @@ def test_deploy(put_file_mock, dbfs_path_validate, pipelines_api, tmpdir):
122128
assert put_file_mock.call_args_list[1][0][0] == jar3_relpath
123129
assert put_file_mock.call_args_list[2][0][0] == jar4
124130
assert put_file_mock.call_args_list[3][0][0] == wheel1
125-
deploy_mock.assert_called_with('PUT', '/pipelines/{}'.format(PIPELINE_ID),
131+
client_mock = pipelines_api.client.client.perform_query
132+
client_mock.assert_called_with('PUT', '/pipelines/' + PIPELINE_ID,
126133
data=expected_spec, headers=None)
127134

128135
pipelines_api.deploy(spec, HEADERS)
129-
deploy_mock.assert_called_with('PUT', '/pipelines/{}'.format(PIPELINE_ID),
136+
client_mock.assert_called_with('PUT', '/pipelines/' + PIPELINE_ID,
130137
data=expected_spec, headers=HEADERS)
138+
assert client_mock.call_count == 2
131139

132140

133141
def test_delete(pipelines_api):
134142
pipelines_api.delete(PIPELINE_ID)
135-
delete_mock = pipelines_api.client.delete
136-
assert delete_mock.call_count == 1
137-
assert delete_mock.call_args[0][0] == PIPELINE_ID
138-
assert delete_mock.call_args[0][1] is None
143+
client_mock = pipelines_api.client.client.perform_query
144+
assert client_mock.call_count == 1
145+
client_mock.assert_called_with('DELETE', '/pipelines/' + PIPELINE_ID,
146+
data=ID_ONLY_SPEC, headers=None)
139147

140148
pipelines_api.delete(PIPELINE_ID, HEADERS)
141-
assert delete_mock.call_args[0][0] == PIPELINE_ID
142-
assert delete_mock.call_args[0][1] == HEADERS
149+
client_mock.assert_called_with('DELETE', '/pipelines/' + PIPELINE_ID,
150+
data=ID_ONLY_SPEC, headers=HEADERS)
143151

144152

145153
def test_get(pipelines_api):
146-
pipelines_api.get(PIPELINE_ID)
147-
get_mock = pipelines_api.client.get
148-
assert get_mock.call_count == 1
149-
assert get_mock.call_args[0][0] == PIPELINE_ID
154+
response = pipelines_api.get(PIPELINE_ID)
155+
client_mock = pipelines_api.client.client.perform_query
156+
assert client_mock.call_count == 1
157+
client_mock.assert_called_with('GET', '/pipelines/' + PIPELINE_ID, data={}, headers=None)
158+
assert(response['pipeline_id'] == PIPELINE_ID and response['state'] == 'RUNNING')
150159

151160

152161
def test_reset(pipelines_api):
153162
pipelines_api.reset(PIPELINE_ID)
154-
reset_mock = pipelines_api.client.reset
155-
assert reset_mock.call_count == 1
156-
assert reset_mock.call_args[0][0] == PIPELINE_ID
157-
assert reset_mock.call_args[0][1] is None
163+
client_mock = pipelines_api.client.client.perform_query
164+
assert client_mock.call_count == 1
165+
client_mock.assert_called_with('POST', '/pipelines/{}/reset'.format(PIPELINE_ID),
166+
data=ID_ONLY_SPEC, headers=None)
158167

159168

160169
def test_partition_local_remote(pipelines_api):

0 commit comments

Comments
 (0)