Skip to content

Commit

Permalink
test(sdk): Components tests - Made test_load_component_from_url herme…
Browse files Browse the repository at this point in the history
…tic (#4121)

Mocking the `requests.get` call so that the test can run without internet.
  • Loading branch information
Ark-kun authored Jul 2, 2020
1 parent d4a8329 commit 62c7433
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions sdk/python/kfp/components_tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import mock
import os
import requests
import sys
import textwrap
import unittest
Expand Down Expand Up @@ -52,13 +54,23 @@ def test_load_component_from_zipped_yaml_file(self):
self._test_load_component_from_file(str(component_path))

def test_load_component_from_url(self):
url = 'https://raw.githubusercontent.com/kubeflow/pipelines/e54fe675432cfef1d115a7a2909f08ed95ea8933/sdk/python/tests/components/test_data/python_add.component.yaml'
component_path = Path(__file__).parent / 'test_data' / 'python_add.component.yaml'
component_url = 'https://raw.githubusercontent.com/some/repo/components/component_group/python_add/component.yaml'
component_bytes = component_path.read_bytes()
component_dict = load_yaml(component_bytes)

def mock_response_factory(url, params=None, **kwargs):
if url == component_url:
response = requests.Response()
response.url = component_url
response.status_code = 200
response._content = component_bytes
return response
raise RuntimeError('Unexpected URL "{}"'.format(url))

with mock.patch('requests.get', mock_response_factory):
task_factory1 = comp.load_component_from_url(component_url)

import requests
resp = requests.get(url)
component_text = resp.content
component_dict = load_yaml(component_text)
task_factory1 = comp.load_component_from_url(url)
self.assertEqual(task_factory1.__doc__, component_dict['name'] + '\n' + component_dict['description'])

arg1 = 3
Expand Down

0 comments on commit 62c7433

Please sign in to comment.