Skip to content

Commit 5503387

Browse files
authored
Merge pull request #47 from hpe-container-platform-community/improve_coverage
add catalog list() test
2 parents e2b1076 + d0b4e66 commit 5503387

File tree

3 files changed

+150
-11
lines changed

3 files changed

+150
-11
lines changed

bin/cli.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ def get_client():
9696
sys.exit(1)
9797

9898

99+
class CatalogProxy(object):
100+
def list(self):
101+
"""Retrieve the list of Catalog Images
102+
"""
103+
print(get_client().catalog.list())
104+
105+
99106
class GatewayProxy(object):
100107
def create_with_ssh_key(
101108
self,
@@ -1115,6 +1122,7 @@ def configure_cli():
11151122

11161123
class CLI(object):
11171124
def __init__(self,):
1125+
self.catalog = CatalogProxy()
11181126
self.k8sworker = K8sWorkerProxy()
11191127
self.k8scluster = K8sClusterProxy()
11201128
self.gateway = GatewayProxy()

hpecp/catalog.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
from tabulate import tabulate
2525

26-
from hpecp.exceptions import APIItemNotFoundException
27-
2826

2927
class CatalogController:
3028
"""This is the main class that users will interact with to talk to catalogs.
@@ -86,12 +84,6 @@ def get(self, catalog_id):
8684
response = self.client._request(
8785
url=catalog_id, http_method="get", description="catalog/get"
8886
)
89-
if response.json()["purpose"] != "proxy":
90-
raise APIItemNotFoundException(
91-
message="catalog not found with id: " + catalog_id,
92-
request_method="get",
93-
request_url=catalog_id,
94-
)
9587

9688
return Catalog(response.json())
9789

@@ -205,10 +197,9 @@ class CatalogList:
205197
"""
206198

207199
def __init__(self, json):
208-
self.json = [g for g in json if g["purpose"] == "proxy"]
200+
self.json = json
209201
self.catalogs = sorted(
210-
[Catalog(g) for g in json if g["purpose"] == "proxy"],
211-
key=attrgetter("id"),
202+
[Catalog(g) for g in json], key=attrgetter("id"),
212203
)
213204
self.display_columns = Catalog.default_display_fields
214205

tests/library/catalog_test.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@
1818
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1919
# OTHER DEALINGS IN THE SOFTWARE.
2020

21+
22+
import os
23+
import sys
2124
import unittest
25+
from textwrap import dedent
2226

2327
import requests
2428
from mock import patch
2529

2630
from hpecp import ContainerPlatformClient
31+
from hpecp.catalog import CatalogList
2732
from hpecp.exceptions import APIItemNotFoundException
33+
import tempfile
2834

2935

3036
class MockResponse:
@@ -139,3 +145,137 @@ def test_get_catalog(self, mock_get, mock_post):
139145
"'catalog not found with id: /api/v1/catalog/100'",
140146
):
141147
get_client().catalog.get("/api/v1/catalog/100")
148+
149+
150+
catalog_list_json = {
151+
"_links": {
152+
"self": {"href": "/api/v1/catalog/"},
153+
"feedlog": {"href": "/api/v1/catalog/feedlog"},
154+
"feed": [
155+
{
156+
"href": "http://127.0.0.1:8080/api/v1/feed/local",
157+
"name": "Feed generated from local bundles.",
158+
},
159+
{
160+
"href": (
161+
"https://s3.amazonaws.com/bluedata-catalog/bundles/"
162+
"catalog/external/docker/EPIC-5.0/feeds/feed.json"
163+
),
164+
"name": "BlueData EPIC-5.0 catalog feed for docker",
165+
},
166+
],
167+
},
168+
"catalog_api_version": 6,
169+
"feeds_refresh_period_seconds": 86400,
170+
"feeds_read_counter": 5,
171+
"catalog_write_counter": 5,
172+
"_embedded": {
173+
"independent_catalog_entries": [
174+
{
175+
"_links": {
176+
"self": {"href": "/api/v1/catalog/29"},
177+
"feed": [
178+
{
179+
"href": (
180+
"https://s3.amazonaws.com/bluedata-catalog/"
181+
"bundles/catalog/external/docker/EPIC-5.0/"
182+
"feeds/feed.json"
183+
),
184+
"name": (
185+
"BlueData EPIC-5.0 catalog feed for docker"
186+
),
187+
}
188+
],
189+
},
190+
"distro_id": "bluedata/spark240juphub7xssl",
191+
"label": {
192+
"name": "Spark240",
193+
"description": (
194+
"Spark240 multirole with Jupyter Notebook, Jupyterhub"
195+
" with SSL and gateway node"
196+
),
197+
},
198+
"version": "2.8",
199+
"timestamp": 0,
200+
"isdebug": False,
201+
"osclass": ["centos"],
202+
"logo": {
203+
"checksum": "1471eb59356066ed4a06130566764ea6",
204+
"url": (
205+
"http://10.1.0.53/catalog/logos/"
206+
"bluedata-spark240juphub7xssl-2.8"
207+
),
208+
},
209+
"documentation": {
210+
"checksum": "52f53f1b2845463b9e370d17fb80bea6",
211+
"mimetype": "text/markdown",
212+
"file": (
213+
"/opt/bluedata/catalog/documentation/"
214+
"bluedata-spark240juphub7xssl-2.8"
215+
),
216+
},
217+
"state": "initialized",
218+
"state_info": "",
219+
}
220+
]
221+
},
222+
}
223+
224+
225+
class TestCatalogList(unittest.TestCase):
226+
def mocked_requests_get(*args, **kwargs):
227+
if args[0] == "https://127.0.0.1:8080/api/v1/catalog/":
228+
return MockResponse(
229+
json_data=catalog_list_json, status_code=200, headers=dict(),
230+
)
231+
raise RuntimeError("Unhandle GET request: " + args[0])
232+
233+
@patch("requests.get", side_effect=mocked_requests_get)
234+
@patch("requests.post", side_effect=mocked_requests_post)
235+
def test_list(self, mock_get, mock_post):
236+
237+
catalogList = get_client().catalog.list()
238+
self.assertIsInstance(catalogList, CatalogList)
239+
240+
241+
class TestCLI(unittest.TestCase):
242+
def setUp(self):
243+
file_data = dedent(
244+
"""[default]
245+
api_host = 127.0.0.1
246+
api_port = 8080
247+
use_ssl = True
248+
verify_ssl = False
249+
warn_ssl = True
250+
username = admin
251+
password = admin123"""
252+
)
253+
254+
self.tmpFile = tempfile.NamedTemporaryFile(delete=True)
255+
self.tmpFile.write(file_data.encode("utf-8"))
256+
self.tmpFile.flush()
257+
258+
sys.path.insert(0, os.path.abspath("../../"))
259+
from bin import cli
260+
261+
self.cli = cli
262+
self.cli.HPECP_CONFIG_FILE = self.tmpFile.name
263+
264+
def tearDown(self):
265+
self.tmpFile.close()
266+
267+
def mocked_requests_get(*args, **kwargs):
268+
if args[0] == "https://127.0.0.1:8080/api/v1/catalog/":
269+
return MockResponse(
270+
json_data=catalog_list_json, status_code=200, headers=dict(),
271+
)
272+
raise RuntimeError("Unhandle GET request: " + args[0])
273+
274+
@patch("requests.post", side_effect=mocked_requests_post)
275+
@patch("requests.get", side_effect=mocked_requests_get)
276+
def test_cli(self, mock_post, mock_get):
277+
278+
hpecp = self.cli.CLI()
279+
hpecp.catalog.list()
280+
281+
self.assertTrue(True)

0 commit comments

Comments
 (0)