|
18 | 18 | # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
19 | 19 | # OTHER DEALINGS IN THE SOFTWARE.
|
20 | 20 |
|
| 21 | + |
| 22 | +import os |
| 23 | +import sys |
21 | 24 | import unittest
|
| 25 | +from textwrap import dedent |
22 | 26 |
|
23 | 27 | import requests
|
24 | 28 | from mock import patch
|
25 | 29 |
|
26 | 30 | from hpecp import ContainerPlatformClient
|
| 31 | +from hpecp.catalog import CatalogList |
27 | 32 | from hpecp.exceptions import APIItemNotFoundException
|
| 33 | +import tempfile |
28 | 34 |
|
29 | 35 |
|
30 | 36 | class MockResponse:
|
@@ -139,3 +145,137 @@ def test_get_catalog(self, mock_get, mock_post):
|
139 | 145 | "'catalog not found with id: /api/v1/catalog/100'",
|
140 | 146 | ):
|
141 | 147 | 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