test_metadata_app.py 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129
  1. #
  2. # Copyright 2018-2022 Elyra Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. """Tests for elyra-metadata application"""
  17. import json
  18. import os
  19. import shutil
  20. from tempfile import mkdtemp
  21. from tempfile import TemporaryDirectory
  22. from typing import List
  23. from typing import Optional
  24. import pytest
  25. from elyra.metadata.manager import MetadataManager
  26. from elyra.metadata.metadata import Metadata
  27. from elyra.metadata.schema import METADATA_TEST_SCHEMASPACE
  28. from elyra.metadata.schema import METADATA_TEST_SCHEMASPACE_ID
  29. from elyra.tests.metadata.test_utils import all_of_json
  30. from elyra.tests.metadata.test_utils import another_metadata_json
  31. from elyra.tests.metadata.test_utils import create_json_file
  32. from elyra.tests.metadata.test_utils import invalid_metadata_json
  33. from elyra.tests.metadata.test_utils import invalid_no_display_name_json
  34. from elyra.tests.metadata.test_utils import invalid_schema_name_json
  35. from elyra.tests.metadata.test_utils import one_of_json
  36. from elyra.tests.metadata.test_utils import PropertyTester
  37. from elyra.tests.metadata.test_utils import valid_display_name_json
  38. from elyra.tests.metadata.test_utils import valid_metadata2_json
  39. from elyra.tests.metadata.test_utils import valid_metadata_json
  40. os.environ["METADATA_TESTING"] = "1" # Enable metadata-tests schemaspace
  41. @pytest.fixture()
  42. def mock_data_dir():
  43. runtime_dir = mkdtemp(prefix="runtime_")
  44. orig_data_dir = os.environ.get("JUPYTER_DATA_DIR")
  45. os.environ["JUPYTER_DATA_DIR"] = runtime_dir
  46. yield runtime_dir # provide the fixture value
  47. shutil.rmtree(runtime_dir)
  48. if orig_data_dir:
  49. os.environ["JUPYTER_DATA_DIR"] = orig_data_dir
  50. else:
  51. os.environ.pop("JUPYTER_DATA_DIR")
  52. def test_no_opts(script_runner):
  53. ret = script_runner.run("elyra-metadata")
  54. assert ret.success is False
  55. message = (
  56. "No subcommand specified. One of: "
  57. "['list', 'create', 'update', 'install', 'remove', 'migrate', 'export', 'import'] "
  58. "must be specified."
  59. )
  60. assert message in ret.stdout
  61. def test_bad_subcommand(script_runner):
  62. ret = script_runner.run("elyra-metadata", "bogus-subcommand")
  63. assert ret.success is False
  64. assert (
  65. "Subcommand 'bogus-subcommand' is invalid. One of: "
  66. "['list', 'create', 'update', 'install', 'remove', 'migrate', 'export', 'import'] "
  67. "must be specified." in ret.stdout
  68. )
  69. def test_install_bad_argument(script_runner):
  70. ret = script_runner.run("elyra-metadata", "install", "--bogus-argument")
  71. assert ret.success is False
  72. assert "Subcommand '--bogus-argument' is invalid." in ret.stdout
  73. assert f"Install a metadata instance into schemaspace '{METADATA_TEST_SCHEMASPACE}'." in ret.stdout
  74. def test_install_bad_schemaspace(script_runner):
  75. ret = script_runner.run("elyra-metadata", "install", "bogus-schemaspace")
  76. assert ret.success is False
  77. assert "Subcommand 'bogus-schemaspace' is invalid." in ret.stdout
  78. assert f"Install a metadata instance into schemaspace '{METADATA_TEST_SCHEMASPACE}'." in ret.stdout
  79. def test_install_help(script_runner):
  80. ret = script_runner.run("elyra-metadata", "install", METADATA_TEST_SCHEMASPACE, "--help")
  81. assert ret.success is False
  82. assert f"Install a metadata instance into schemaspace '{METADATA_TEST_SCHEMASPACE}'." in ret.stdout
  83. def test_install_no_schema_single(script_runner, mock_data_dir):
  84. # Use the runtime-images schemaspace since that is most likely to always be a single-schema schemaspace.
  85. # Note: this test will break if it ever supports multiple.
  86. ret = script_runner.run("elyra-metadata", "install", "runtime-images")
  87. assert ret.success is False
  88. assert "ERROR: '--display_name' is a required parameter." in ret.stdout
  89. def test_install_no_schema_multiple(script_runner, mock_data_dir):
  90. ret = script_runner.run("elyra-metadata", "install", METADATA_TEST_SCHEMASPACE)
  91. assert ret.success is False
  92. # Since order in dictionaries, where the one-of list is derived, can be random, just check up to the
  93. # first known difference in the schema names.
  94. assert (
  95. "ERROR: '--schema_name' is a required parameter and must be one of the "
  96. "following values: ['metadata-test" in ret.stdout
  97. )
  98. def test_install_bad_schema_multiple(script_runner, mock_data_dir):
  99. ret = script_runner.run("elyra-metadata", "install", METADATA_TEST_SCHEMASPACE, "--schema_name=metadata-foo")
  100. assert ret.success is False
  101. assert "ERROR: Parameter '--schema_name' requires one of the " "following values: ['metadata-test" in ret.stdout
  102. def test_install_no_name(script_runner, mock_data_dir):
  103. ret = script_runner.run("elyra-metadata", "install", METADATA_TEST_SCHEMASPACE, "--schema_name=metadata-test")
  104. assert ret.success is False
  105. assert "ERROR: '--display_name' is a required parameter." in ret.stdout
  106. def test_install_complex_usage(script_runner, mock_data_dir):
  107. ret = script_runner.run("elyra-metadata", "install", METADATA_TEST_SCHEMASPACE, "--schema_name=metadata-test")
  108. assert ret.success is False
  109. assert "Note: The following properties in this schema contain JSON keywords that are not supported" in ret.stdout
  110. assert "*** References unsupported keywords: {'oneOf'}" in ret.stdout
  111. assert "*** References unsupported keywords: {'allOf'}" in ret.stdout
  112. assert "*** References unsupported keywords: {'$ref'}" in ret.stdout
  113. def test_install_only_display_name(script_runner, mock_data_dir):
  114. metadata_display_name = "1 teste 'rápido'"
  115. metadata_name = "a_1_teste_rpido"
  116. ret = script_runner.run(
  117. "elyra-metadata",
  118. "install",
  119. METADATA_TEST_SCHEMASPACE,
  120. "--schema_name=metadata-test",
  121. f"--display_name={metadata_display_name}",
  122. "--required_test=required_value",
  123. )
  124. assert ret.success is True
  125. assert f"Metadata instance '{metadata_name}' for schema 'metadata-test' has been written to:" in ret.stdout
  126. # Ensure it can be fetched by name...
  127. metadata_manager = MetadataManager(schemaspace=METADATA_TEST_SCHEMASPACE_ID)
  128. resource = metadata_manager.get(metadata_name)
  129. assert resource.display_name == metadata_display_name
  130. def test_install_invalid_name(script_runner, mock_data_dir):
  131. ret = script_runner.run(
  132. "elyra-metadata",
  133. "install",
  134. METADATA_TEST_SCHEMASPACE,
  135. "--schema_name=metadata-test",
  136. "--name=UPPER_CASE_NOT_ALLOWED",
  137. "--display_name=display_name",
  138. "--required_test=required_value",
  139. )
  140. assert ret.success is False
  141. assert "The following exception occurred saving metadata instance for schema 'metadata-test'" in ret.stdout
  142. assert "Name of metadata must be lowercase alphanumeric" in ret.stdout
  143. def test_install_simple(script_runner, mock_data_dir):
  144. expected_file = os.path.join(
  145. mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE, "test-metadata_42_valid-name.json"
  146. )
  147. # Cleanup from any potential previous failures
  148. if os.path.exists(expected_file):
  149. os.remove(expected_file)
  150. ret = script_runner.run(
  151. "elyra-metadata",
  152. "install",
  153. METADATA_TEST_SCHEMASPACE,
  154. "--schema_name=metadata-test",
  155. "--name=test-metadata_42_valid-name",
  156. "--display_name=display_name",
  157. "--required_test=required_value",
  158. )
  159. assert ret.success
  160. assert "Metadata instance 'test-metadata_42_valid-name' for schema 'metadata-test' has been written" in ret.stdout
  161. assert os.path.isdir(os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE))
  162. assert os.path.isfile(expected_file)
  163. with open(expected_file, "r") as fd:
  164. instance_json = json.load(fd)
  165. assert instance_json["schema_name"] == "metadata-test"
  166. assert instance_json["display_name"] == "display_name"
  167. assert instance_json["metadata"]["required_test"] == "required_value"
  168. assert instance_json["metadata"]["number_default_test"] == 42 # defaults will always persist
  169. @pytest.mark.parametrize("option_style", ["equals", "sans-equals", "missing"])
  170. def test_create_from_file(script_runner, mock_data_dir, option_style):
  171. content = create_json_file(mock_data_dir, "valid.json", valid_metadata_json)
  172. argv: List[str] = [
  173. "elyra-metadata",
  174. "install",
  175. METADATA_TEST_SCHEMASPACE,
  176. ]
  177. if option_style == "equals":
  178. argv.append(f"--file={content}")
  179. elif option_style == "sans-equals":
  180. argv.append("--file")
  181. argv.append(f"{content}")
  182. else: # missing
  183. argv.append("--file")
  184. ret = script_runner.run(*argv)
  185. if option_style == "missing":
  186. assert ret.success is False
  187. assert (
  188. "ERROR: Parameter '--file' requires a file with format JSON and no value was provided. "
  189. "Try again with an appropriate value." in ret.stdout
  190. )
  191. else: # success expected
  192. assert ret.success
  193. assert "Metadata instance 'valid' for schema 'metadata-test' has been written" in ret.stdout
  194. @pytest.mark.parametrize("option_style", ["equals", "sans-equals", "missing"])
  195. def test_create_from_json(script_runner, mock_data_dir, option_style):
  196. content = json.dumps(valid_metadata_json)
  197. argv: List[str] = [
  198. "elyra-metadata",
  199. "install",
  200. METADATA_TEST_SCHEMASPACE,
  201. ]
  202. if option_style == "equals":
  203. argv.append(f"--json={content}")
  204. elif option_style == "sans-equals":
  205. argv.append("--json")
  206. argv.append(f"{content}")
  207. else: # missing
  208. argv.append("--json")
  209. ret = script_runner.run(*argv)
  210. if option_style == "missing":
  211. assert ret.success is False
  212. assert (
  213. "ERROR: Parameter '--json' requires a value with format JSON and no value was provided. "
  214. "Try again with an appropriate value." in ret.stdout
  215. )
  216. else: # success expected
  217. assert ret.success
  218. assert "Metadata instance 'valid_metadata_instance' for schema 'metadata-test' has been written" in ret.stdout
  219. def test_install_and_replace(script_runner, mock_data_dir):
  220. expected_file = os.path.join(
  221. mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE, "test-metadata_42_valid-name.json"
  222. )
  223. # Cleanup from any potential previous failures
  224. if os.path.exists(expected_file):
  225. os.remove(expected_file)
  226. # Attempt replace before schemaspace exists and ensure appropriate error message
  227. ret = script_runner.run(
  228. "elyra-metadata",
  229. "install",
  230. METADATA_TEST_SCHEMASPACE,
  231. "--schema_name=metadata-test",
  232. "--name=test-metadata_42_valid-name",
  233. "--display_name=display_name",
  234. "--required_test=required_value",
  235. "--replace",
  236. )
  237. assert ret.success is False
  238. assert (
  239. "No such instance named 'test-metadata_42_valid-name' was found in the metadata-tests schemaspace."
  240. in ret.stdout
  241. )
  242. ret = script_runner.run(
  243. "elyra-metadata",
  244. "install",
  245. METADATA_TEST_SCHEMASPACE,
  246. "--schema_name=metadata-test",
  247. "--name=test-metadata_42_valid-name",
  248. "--display_name=display_name",
  249. "--required_test=required_value",
  250. "--number_default_test=24",
  251. )
  252. assert ret.success
  253. assert "Metadata instance 'test-metadata_42_valid-name' for schema 'metadata-test' has been written" in ret.stdout
  254. assert expected_file in ret.stdout
  255. assert os.path.isdir(os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE))
  256. assert os.path.isfile(expected_file)
  257. with open(expected_file, "r") as fd:
  258. instance_json = json.load(fd)
  259. assert instance_json["metadata"]["number_default_test"] == 24 # ensure CLI value is used over default
  260. # Re-attempt w/o replace flag - failure expected
  261. ret = script_runner.run(
  262. "elyra-metadata",
  263. "install",
  264. METADATA_TEST_SCHEMASPACE,
  265. "--schema_name=metadata-test",
  266. "--name=test-metadata_42_valid-name",
  267. "--display_name=display_name",
  268. "--required_test=required_value",
  269. )
  270. assert ret.success is False
  271. assert (
  272. "An instance named 'test-metadata_42_valid-name' already exists in the metadata-tests "
  273. "schemaspace" in ret.stderr
  274. )
  275. # Re-attempt with replace flag but without --name - failure expected
  276. ret = script_runner.run(
  277. "elyra-metadata",
  278. "install",
  279. METADATA_TEST_SCHEMASPACE,
  280. "--schema_name=metadata-test",
  281. "--display_name=display_name",
  282. "--required_test=required_value",
  283. "--replace",
  284. )
  285. assert ret.success is False
  286. assert "The 'name' parameter requires a value" in ret.stdout
  287. # Re-attempt with replace flag - success expected
  288. ret = script_runner.run(
  289. "elyra-metadata",
  290. "install",
  291. METADATA_TEST_SCHEMASPACE,
  292. "--schema_name=metadata-test",
  293. "--name=test-metadata_42_valid-name",
  294. "--display_name=display_name",
  295. "--required_test=required_value",
  296. "--replace",
  297. )
  298. assert ret.success
  299. assert "Metadata instance 'test-metadata_42_valid-name' for schema 'metadata-test' has been written" in ret.stdout
  300. assert os.path.isdir(os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE))
  301. assert os.path.isfile(expected_file)
  302. with open(expected_file, "r") as fd:
  303. instance_json = json.load(fd)
  304. assert instance_json["schema_name"] == "metadata-test"
  305. assert instance_json["display_name"] == "display_name"
  306. assert instance_json["metadata"]["required_test"] == "required_value"
  307. assert instance_json["metadata"]["number_default_test"] == 24 # ensure original value is used over default
  308. @pytest.mark.parametrize("complex_keyword", ["defs", "oneOf", "allOf"])
  309. def test_install_and_replace_complex(script_runner, mock_data_dir, complex_keyword):
  310. # create and use deep copies of the global one_of_json and one_of_json
  311. # to avoid side effects
  312. one_of_json_cp = json.loads(json.dumps(one_of_json))
  313. all_of_json_cp = json.loads(json.dumps(all_of_json))
  314. test_file: Optional[str] = None
  315. name: str = f"test-complex-{complex_keyword}".lower()
  316. if complex_keyword == "defs":
  317. option = "--json"
  318. value = '{ "defs_test": 42 }'
  319. elif complex_keyword == "oneOf":
  320. option = "--file"
  321. # Build the file...
  322. test_file = os.path.join(mock_data_dir, f"{complex_keyword}.json")
  323. with open(test_file, mode="w") as one_of_fd:
  324. json.dump(one_of_json_cp, one_of_fd)
  325. value = test_file
  326. else: # allOf
  327. option = "--allOf_test" # Use "ovp-from-file" approach
  328. # Build the file...
  329. test_file = os.path.join(mock_data_dir, f"{complex_keyword}.json")
  330. with open(test_file, mode="w") as all_of_fd:
  331. json.dump(all_of_json_cp, all_of_fd)
  332. value = test_file
  333. expected_file = os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE, f"{name}.json")
  334. # Cleanup from any potential previous failures (should be rare)
  335. if os.path.exists(expected_file):
  336. os.remove(expected_file)
  337. ret = script_runner.run(
  338. "elyra-metadata",
  339. "install",
  340. METADATA_TEST_SCHEMASPACE,
  341. "--schema_name=metadata-test",
  342. f"--name={name}",
  343. f"--display_name=Test Complex {complex_keyword}",
  344. "--required_test=required_value",
  345. f"{option}={value}",
  346. )
  347. assert ret.success
  348. assert f"Metadata instance '{name}' for schema 'metadata-test' has been written" in ret.stdout
  349. assert expected_file in ret.stdout
  350. assert os.path.exists(expected_file)
  351. with open(expected_file) as fd:
  352. json_results = json.load(fd)
  353. # Verify common stuff
  354. assert json_results["display_name"] == f"Test Complex {complex_keyword}"
  355. assert json_results["metadata"]["required_test"] == "required_value"
  356. # Verify result and prepare for replace...
  357. if complex_keyword == "defs":
  358. assert json_results["metadata"]["defs_test"] == 42
  359. value = '{ "defs_test": 24 }'
  360. elif complex_keyword == "oneOf":
  361. assert json_results["metadata"]["oneOf_test"]["obj_switch"] == "obj2"
  362. assert json_results["metadata"]["oneOf_test"]["obj2_prop1"] == 42
  363. one_of_json_cp["metadata"]["oneOf_test"]["obj2_prop1"] = 24
  364. with open(test_file, mode="w+") as one_of_fd:
  365. json.dump(one_of_json_cp, one_of_fd)
  366. elif complex_keyword == "allOf":
  367. assert len(json_results["metadata"]["allOf_test"]) == 9
  368. assert json_results["metadata"]["allOf_test"]["obj1_switch"] == "obj1"
  369. assert json_results["metadata"]["allOf_test"]["obj1_prop1"] == "allOf-test-val1"
  370. assert json_results["metadata"]["allOf_test"]["obj1_prop2"] == "allOf-test-val2"
  371. all_of_json_cp["obj1_prop1"] = "allOf-test-val1-replace"
  372. assert json_results["metadata"]["allOf_test"]["obj2_switch"] == "obj2"
  373. assert json_results["metadata"]["allOf_test"]["obj2_prop1"] == 42
  374. assert json_results["metadata"]["allOf_test"]["obj2_prop2"] == 24
  375. all_of_json_cp["obj2_prop1"] = 24
  376. assert json_results["metadata"]["allOf_test"]["obj3_switch"] == "obj3"
  377. assert json_results["metadata"]["allOf_test"]["obj3_prop1"] == 42.7
  378. assert json_results["metadata"]["allOf_test"]["obj3_prop2"] is True
  379. all_of_json_cp["obj3_prop1"] = 7.24
  380. with open(test_file, mode="w+") as all_of_fd:
  381. json.dump(all_of_json_cp, all_of_fd)
  382. # Replace the previously-created instance
  383. ret = script_runner.run(
  384. "elyra-metadata",
  385. "install",
  386. METADATA_TEST_SCHEMASPACE,
  387. "--schema_name=metadata-test",
  388. f"--name={name}",
  389. f"--display_name=Test Complex {complex_keyword}2",
  390. "--required_test=required_value",
  391. f"{option}={value}",
  392. "--replace",
  393. )
  394. assert ret.success
  395. assert f"Metadata instance '{name}' for schema 'metadata-test' has been written" in ret.stdout
  396. assert expected_file in ret.stdout
  397. assert os.path.exists(expected_file)
  398. with open(expected_file) as fd:
  399. json_results = json.load(fd)
  400. # Verify common stuff
  401. assert json_results["display_name"] == f"Test Complex {complex_keyword}2"
  402. assert json_results["metadata"]["required_test"] == "required_value"
  403. # Verify result following replace...
  404. if complex_keyword == "defs":
  405. assert json_results["metadata"]["defs_test"] == 24
  406. elif complex_keyword == "oneOf":
  407. assert json_results["metadata"]["oneOf_test"]["obj_switch"] == "obj2"
  408. assert json_results["metadata"]["oneOf_test"]["obj2_prop1"] == 24
  409. assert json_results["metadata"]["oneOf_test"]["obj2_prop2"] == 24
  410. elif complex_keyword == "allOf":
  411. assert len(json_results["metadata"]["allOf_test"]) == 9
  412. assert json_results["metadata"]["allOf_test"]["obj1_prop1"] == "allOf-test-val1-replace"
  413. assert json_results["metadata"]["allOf_test"]["obj1_prop2"] == "allOf-test-val2"
  414. assert json_results["metadata"]["allOf_test"]["obj2_prop1"] == 24
  415. assert json_results["metadata"]["allOf_test"]["obj2_prop2"] == 24
  416. assert json_results["metadata"]["allOf_test"]["obj3_prop1"] == 7.24
  417. assert json_results["metadata"]["allOf_test"]["obj3_prop2"] is True
  418. # ---------- begin of 'create' command tests
  419. def test_create_bad_argument(script_runner):
  420. ret = script_runner.run("elyra-metadata", "create", "--bogus-argument")
  421. assert ret.success is False
  422. assert "Subcommand '--bogus-argument' is invalid." in ret.stdout
  423. assert f"Create a metadata instance in schemaspace '{METADATA_TEST_SCHEMASPACE}'." in ret.stdout
  424. def test_create_bad_schemaspace(script_runner):
  425. ret = script_runner.run("elyra-metadata", "create", "bogus-schemaspace")
  426. assert ret.success is False
  427. assert "Subcommand 'bogus-schemaspace' is invalid." in ret.stdout
  428. assert f"Create a metadata instance in schemaspace '{METADATA_TEST_SCHEMASPACE}'." in ret.stdout
  429. def test_create_help(script_runner):
  430. ret = script_runner.run("elyra-metadata", "create", METADATA_TEST_SCHEMASPACE, "--help")
  431. assert ret.success is False
  432. assert f"Create a metadata instance in schemaspace '{METADATA_TEST_SCHEMASPACE}'." in ret.stdout
  433. def test_create_no_schema_single(script_runner, mock_data_dir):
  434. # Use the runtime-images schemaspace since that is most likely to always be a single-schema schemaspace.
  435. # Note: this test will break if it ever supports multiple.
  436. ret = script_runner.run("elyra-metadata", "create", "runtime-images")
  437. assert ret.success is False
  438. assert "ERROR: '--display_name' is a required parameter." in ret.stdout
  439. def test_create_no_schema_multiple(script_runner, mock_data_dir):
  440. ret = script_runner.run("elyra-metadata", "create", METADATA_TEST_SCHEMASPACE)
  441. assert ret.success is False
  442. # Since order in dictionaries, where the one-of list is derived, can be random, just check up to the
  443. # first known difference in the schema names.
  444. assert (
  445. "ERROR: '--schema_name' is a required parameter and must be one of the "
  446. "following values: ['metadata-test" in ret.stdout
  447. )
  448. def test_create_bad_schema_multiple(script_runner, mock_data_dir):
  449. ret = script_runner.run("elyra-metadata", "create", METADATA_TEST_SCHEMASPACE, "--schema_name=metadata-foo")
  450. assert ret.success is False
  451. assert "ERROR: Parameter '--schema_name' requires one of the " "following values: ['metadata-test" in ret.stdout
  452. def test_create_no_name(script_runner, mock_data_dir):
  453. ret = script_runner.run("elyra-metadata", "create", METADATA_TEST_SCHEMASPACE, "--schema_name=metadata-test")
  454. assert ret.success is False
  455. assert "ERROR: '--display_name' is a required parameter." in ret.stdout
  456. def test_create_complex_usage(script_runner, mock_data_dir):
  457. ret = script_runner.run("elyra-metadata", "create", METADATA_TEST_SCHEMASPACE, "--schema_name=metadata-test")
  458. assert ret.success is False
  459. assert "Note: The following properties in this schema contain JSON keywords that are not supported" in ret.stdout
  460. assert "*** References unsupported keywords: {'oneOf'}" in ret.stdout
  461. assert "*** References unsupported keywords: {'allOf'}" in ret.stdout
  462. assert "*** References unsupported keywords: {'$ref'}" in ret.stdout
  463. def test_create_only_display_name(script_runner, mock_data_dir):
  464. metadata_display_name = "1 teste 'rápido'"
  465. metadata_name = "a_1_teste_rpido"
  466. ret = script_runner.run(
  467. "elyra-metadata",
  468. "create",
  469. METADATA_TEST_SCHEMASPACE,
  470. "--schema_name=metadata-test",
  471. f"--display_name={metadata_display_name}",
  472. "--required_test=required_value",
  473. )
  474. assert ret.success is True
  475. assert f"Metadata instance '{metadata_name}' for schema 'metadata-test' has been written to:" in ret.stdout
  476. # Ensure it can be fetched by name...
  477. metadata_manager = MetadataManager(schemaspace=METADATA_TEST_SCHEMASPACE_ID)
  478. resource = metadata_manager.get(metadata_name)
  479. assert resource.display_name == metadata_display_name
  480. def test_create_invalid_name(script_runner, mock_data_dir):
  481. ret = script_runner.run(
  482. "elyra-metadata",
  483. "create",
  484. METADATA_TEST_SCHEMASPACE,
  485. "--schema_name=metadata-test",
  486. "--name=UPPER_CASE_NOT_ALLOWED",
  487. "--display_name=display_name",
  488. "--required_test=required_value",
  489. )
  490. assert ret.success is False
  491. assert "The following exception occurred saving metadata instance for schema 'metadata-test'" in ret.stdout
  492. assert "Name of metadata must be lowercase alphanumeric" in ret.stdout
  493. def test_create_simple(script_runner, mock_data_dir):
  494. expected_file = os.path.join(
  495. mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE, "test-metadata_42_valid-name.json"
  496. )
  497. # Cleanup from any potential previous failures
  498. if os.path.exists(expected_file):
  499. os.remove(expected_file)
  500. ret = script_runner.run(
  501. "elyra-metadata",
  502. "create",
  503. METADATA_TEST_SCHEMASPACE,
  504. "--schema_name=metadata-test",
  505. "--name=test-metadata_42_valid-name",
  506. "--display_name=display_name",
  507. "--required_test=required_value",
  508. )
  509. assert ret.success
  510. assert "Metadata instance 'test-metadata_42_valid-name' for schema 'metadata-test' has been written" in ret.stdout
  511. assert os.path.isdir(os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE))
  512. assert os.path.isfile(expected_file)
  513. with open(expected_file, "r") as fd:
  514. instance_json = json.load(fd)
  515. assert instance_json["schema_name"] == "metadata-test"
  516. assert instance_json["display_name"] == "display_name"
  517. assert instance_json["metadata"]["required_test"] == "required_value"
  518. assert instance_json["metadata"]["number_default_test"] == 42 # defaults will always persist
  519. def test_create_existing(script_runner, mock_data_dir):
  520. expected_file = os.path.join(
  521. mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE, "test-metadata_42_valid-name.json"
  522. )
  523. # Cleanup from any potential previous failures
  524. if os.path.exists(expected_file):
  525. os.remove(expected_file)
  526. ret = script_runner.run(
  527. "elyra-metadata",
  528. "create",
  529. METADATA_TEST_SCHEMASPACE,
  530. "--schema_name=metadata-test",
  531. "--name=test-metadata_42_valid-name",
  532. "--display_name=display_name",
  533. "--required_test=required_value",
  534. "--number_default_test=24",
  535. )
  536. assert ret.success
  537. assert "Metadata instance 'test-metadata_42_valid-name' for schema 'metadata-test' has been written" in ret.stdout
  538. assert expected_file in ret.stdout
  539. assert os.path.isdir(os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE))
  540. assert os.path.isfile(expected_file)
  541. with open(expected_file, "r") as fd:
  542. instance_json = json.load(fd)
  543. assert instance_json["metadata"]["number_default_test"] == 24 # ensure CLI value is used over default
  544. # Re-attempt create - failure expected
  545. ret = script_runner.run(
  546. "elyra-metadata",
  547. "create",
  548. METADATA_TEST_SCHEMASPACE,
  549. "--schema_name=metadata-test",
  550. "--name=test-metadata_42_valid-name",
  551. "--display_name=display_name",
  552. "--required_test=required_value",
  553. )
  554. assert ret.success is False
  555. assert (
  556. "An instance named 'test-metadata_42_valid-name' already exists in the metadata-tests "
  557. "schemaspace" in ret.stderr
  558. )
  559. def test_create_complex(script_runner, mock_data_dir):
  560. complex_keyword = "defs"
  561. name: str = f"test-complex-{complex_keyword}".lower()
  562. option = "--json"
  563. value = '{ "defs_test": 42 }'
  564. expected_file = os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE, f"{name}.json")
  565. # Cleanup from any potential previous failures (should be rare)
  566. if os.path.exists(expected_file):
  567. os.remove(expected_file)
  568. ret = script_runner.run(
  569. "elyra-metadata",
  570. "create",
  571. METADATA_TEST_SCHEMASPACE,
  572. "--schema_name=metadata-test",
  573. f"--name={name}",
  574. f"--display_name=Test Complex {complex_keyword}",
  575. "--required_test=required_value",
  576. f"{option}", # don't use '=' between option and value
  577. f"{value}",
  578. )
  579. assert ret.success
  580. assert f"Metadata instance '{name}' for schema 'metadata-test' has been written" in ret.stdout
  581. assert expected_file in ret.stdout
  582. assert os.path.exists(expected_file)
  583. with open(expected_file) as fd:
  584. json_results = json.load(fd)
  585. # Verify common stuff
  586. assert json_results["display_name"] == f"Test Complex {complex_keyword}"
  587. assert json_results["metadata"]["required_test"] == "required_value"
  588. assert json_results["metadata"]["defs_test"] == 42
  589. # ---------- end of 'create' command tests
  590. #
  591. # ---------- begin of 'update' command tests
  592. def test_update_bad_argument(script_runner):
  593. ret = script_runner.run("elyra-metadata", "update", "--bogus-argument")
  594. assert ret.success is False
  595. assert "Subcommand '--bogus-argument' is invalid." in ret.stdout
  596. assert f"Update a metadata instance in schemaspace '{METADATA_TEST_SCHEMASPACE}'." in ret.stdout
  597. def test_update_bad_schemaspace(script_runner):
  598. ret = script_runner.run("elyra-metadata", "update", "bogus-schemaspace")
  599. assert ret.success is False
  600. assert "Subcommand 'bogus-schemaspace' is invalid." in ret.stdout
  601. assert f"Update a metadata instance in schemaspace '{METADATA_TEST_SCHEMASPACE}'." in ret.stdout
  602. def test_update_help(script_runner):
  603. ret = script_runner.run("elyra-metadata", "update", METADATA_TEST_SCHEMASPACE, "--help")
  604. assert ret.success is False
  605. assert f"Update a metadata instance in schemaspace '{METADATA_TEST_SCHEMASPACE}'." in ret.stdout
  606. def test_update_no_schema_single(script_runner, mock_data_dir):
  607. # Use the runtime-images schemaspace since that is most likely to always be a single-schema schemaspace.
  608. # Note: this test will break if it ever supports multiple.
  609. ret = script_runner.run("elyra-metadata", "update", "runtime-images")
  610. assert ret.success is False
  611. assert (
  612. "The following exception occurred saving metadata instance for schema 'runtime-image': "
  613. "The 'name' parameter requires a value." in ret.stdout
  614. )
  615. def test_update_no_schema_multiple(script_runner, mock_data_dir):
  616. ret = script_runner.run("elyra-metadata", "update", METADATA_TEST_SCHEMASPACE)
  617. assert ret.success is False
  618. # Since order in dictionaries, where the one-of list is derived, can be random, just check up to the
  619. # first known difference in the schema names.
  620. assert (
  621. "ERROR: '--schema_name' is a required parameter and must be one of the "
  622. "following values: ['metadata-test" in ret.stdout
  623. )
  624. def test_update_bad_schema_multiple(script_runner, mock_data_dir):
  625. ret = script_runner.run("elyra-metadata", "update", METADATA_TEST_SCHEMASPACE, "--schema_name=metadata-foo")
  626. assert ret.success is False
  627. assert "ERROR: Parameter '--schema_name' requires one of the " "following values: ['metadata-test" in ret.stdout
  628. def test_update_no_name(script_runner, mock_data_dir):
  629. ret = script_runner.run("elyra-metadata", "update", METADATA_TEST_SCHEMASPACE, "--schema_name=metadata-test")
  630. assert ret.success is False
  631. assert (
  632. "The following exception occurred saving metadata instance for schema 'metadata-test': "
  633. "The 'name' parameter requires a value." in ret.stdout
  634. )
  635. def test_update_no_instance(script_runner, mock_data_dir):
  636. """Attempt replace before instance exists"""
  637. ret = script_runner.run(
  638. "elyra-metadata",
  639. "update",
  640. METADATA_TEST_SCHEMASPACE,
  641. "--schema_name=metadata-test",
  642. "--name=test-metadata_42_valid-name",
  643. "--display_name=display_name",
  644. "--required_test=required_value",
  645. )
  646. assert ret.success is False
  647. assert (
  648. "No such instance named 'test-metadata_42_valid-name' was found in the metadata-tests schemaspace."
  649. in ret.stdout
  650. )
  651. def test_update_simple(script_runner, mock_data_dir):
  652. expected_file = os.path.join(
  653. mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE, "test-metadata_42_valid-name.json"
  654. )
  655. # Cleanup from any potential previous failures
  656. if os.path.exists(expected_file):
  657. os.remove(expected_file)
  658. # create an instance
  659. ret = script_runner.run(
  660. "elyra-metadata",
  661. "create",
  662. METADATA_TEST_SCHEMASPACE,
  663. "--schema_name=metadata-test",
  664. "--name=test-metadata_42_valid-name",
  665. "--display_name=display_name",
  666. "--required_test=required_value",
  667. )
  668. assert ret.success
  669. assert "Metadata instance 'test-metadata_42_valid-name' for schema 'metadata-test' has been written" in ret.stdout
  670. # update instance
  671. ret = script_runner.run(
  672. "elyra-metadata",
  673. "update",
  674. METADATA_TEST_SCHEMASPACE,
  675. "--schema_name=metadata-test",
  676. "--name=test-metadata_42_valid-name",
  677. "--display_name=display_name",
  678. "--required_test=updated_required_value",
  679. )
  680. assert ret.success
  681. assert "Metadata instance 'test-metadata_42_valid-name' for schema 'metadata-test' has been written" in ret.stdout
  682. assert os.path.isdir(os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE))
  683. assert os.path.isfile(expected_file)
  684. with open(expected_file, "r") as fd:
  685. instance_json = json.load(fd)
  686. assert instance_json["schema_name"] == "metadata-test"
  687. assert instance_json["display_name"] == "display_name"
  688. assert instance_json["metadata"]["required_test"] == "updated_required_value"
  689. assert instance_json["metadata"]["number_default_test"] == 42 # defaults will always persist
  690. @pytest.mark.parametrize("complex_keyword", ["defs", "oneOf", "allOf"])
  691. def test_update_complex(script_runner, mock_data_dir, complex_keyword):
  692. # create and use deep copies of the global one_of_json and one_of_json
  693. # to avoid side effects
  694. one_of_json_cp = json.loads(json.dumps(one_of_json))
  695. all_of_json_cp = json.loads(json.dumps(all_of_json))
  696. test_file: Optional[str] = None
  697. name: str = f"test-complex-{complex_keyword}".lower()
  698. if complex_keyword == "defs":
  699. option = "--json"
  700. value = '{ "defs_test": 42 }'
  701. elif complex_keyword == "oneOf":
  702. option = "--file"
  703. # Build the file...
  704. test_file = os.path.join(mock_data_dir, f"{complex_keyword}.json")
  705. with open(test_file, mode="w") as one_of_fd:
  706. json.dump(one_of_json_cp, one_of_fd)
  707. value = test_file
  708. else: # allOf
  709. option = "--allOf_test" # Use "ovp-from-file" approach
  710. # Build the file...
  711. test_file = os.path.join(mock_data_dir, f"{complex_keyword}.json")
  712. with open(test_file, mode="w") as all_of_fd:
  713. json.dump(all_of_json_cp, all_of_fd)
  714. value = test_file
  715. expected_file = os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE, f"{name}.json")
  716. # Cleanup from any potential previous failures (should be rare)
  717. if os.path.exists(expected_file):
  718. os.remove(expected_file)
  719. # create instance
  720. ret = script_runner.run(
  721. "elyra-metadata",
  722. "create",
  723. METADATA_TEST_SCHEMASPACE,
  724. "--schema_name=metadata-test",
  725. f"--name={name}",
  726. f"--display_name=Test Complex {complex_keyword}",
  727. "--required_test=required_value",
  728. f"{option}={value}",
  729. )
  730. assert ret.success
  731. assert f"Metadata instance '{name}' for schema 'metadata-test' has been written" in ret.stdout
  732. assert expected_file in ret.stdout
  733. assert os.path.exists(expected_file)
  734. with open(expected_file) as fd:
  735. json_results = json.load(fd)
  736. # Verify common stuff
  737. assert json_results["display_name"] == f"Test Complex {complex_keyword}"
  738. assert json_results["metadata"]["required_test"] == "required_value"
  739. # Verify result and prepare for replace...
  740. if complex_keyword == "defs":
  741. assert json_results["metadata"]["defs_test"] == 42
  742. value = '{ "defs_test": 24 }'
  743. elif complex_keyword == "oneOf":
  744. assert json_results["metadata"]["oneOf_test"]["obj_switch"] == "obj2"
  745. assert json_results["metadata"]["oneOf_test"]["obj2_prop1"] == 42, f"--> {json_results}"
  746. one_of_json_cp["metadata"]["oneOf_test"]["obj2_prop1"] = 24
  747. with open(test_file, mode="w+") as one_of_fd:
  748. json.dump(one_of_json_cp, one_of_fd)
  749. elif complex_keyword == "allOf":
  750. assert len(json_results["metadata"]["allOf_test"]) == 9
  751. assert json_results["metadata"]["allOf_test"]["obj1_switch"] == "obj1"
  752. assert json_results["metadata"]["allOf_test"]["obj1_prop1"] == "allOf-test-val1"
  753. assert json_results["metadata"]["allOf_test"]["obj1_prop2"] == "allOf-test-val2"
  754. all_of_json_cp["obj1_prop1"] = "allOf-test-val1-replace"
  755. assert json_results["metadata"]["allOf_test"]["obj2_switch"] == "obj2"
  756. assert json_results["metadata"]["allOf_test"]["obj2_prop1"] == 42
  757. assert json_results["metadata"]["allOf_test"]["obj2_prop2"] == 24
  758. all_of_json_cp["obj2_prop1"] = 24
  759. assert json_results["metadata"]["allOf_test"]["obj3_switch"] == "obj3"
  760. assert json_results["metadata"]["allOf_test"]["obj3_prop1"] == 42.7
  761. assert json_results["metadata"]["allOf_test"]["obj3_prop2"] is True
  762. all_of_json_cp["obj3_prop1"] = 7.24
  763. with open(test_file, mode="w+") as all_of_fd:
  764. json.dump(all_of_json_cp, all_of_fd)
  765. # Replace the previously created instance
  766. ret = script_runner.run(
  767. "elyra-metadata",
  768. "update",
  769. METADATA_TEST_SCHEMASPACE,
  770. "--schema_name=metadata-test",
  771. f"--name={name}",
  772. f"--display_name=Test Complex {complex_keyword}2",
  773. "--required_test=required_value",
  774. f"{option}={value}",
  775. )
  776. assert ret.success
  777. assert f"Metadata instance '{name}' for schema 'metadata-test' has been written" in ret.stdout
  778. assert expected_file in ret.stdout
  779. assert os.path.exists(expected_file)
  780. with open(expected_file) as fd:
  781. json_results = json.load(fd)
  782. # Verify common stuff
  783. assert json_results["display_name"] == f"Test Complex {complex_keyword}2"
  784. assert json_results["metadata"]["required_test"] == "required_value"
  785. # Verify result following replace...
  786. if complex_keyword == "defs":
  787. assert json_results["metadata"]["defs_test"] == 24
  788. elif complex_keyword == "oneOf":
  789. assert json_results["metadata"]["oneOf_test"]["obj_switch"] == "obj2"
  790. assert json_results["metadata"]["oneOf_test"]["obj2_prop1"] == 24
  791. assert json_results["metadata"]["oneOf_test"]["obj2_prop2"] == 24
  792. elif complex_keyword == "allOf":
  793. assert len(json_results["metadata"]["allOf_test"]) == 9
  794. assert json_results["metadata"]["allOf_test"]["obj1_prop1"] == "allOf-test-val1-replace"
  795. assert json_results["metadata"]["allOf_test"]["obj1_prop2"] == "allOf-test-val2"
  796. assert json_results["metadata"]["allOf_test"]["obj2_prop1"] == 24
  797. assert json_results["metadata"]["allOf_test"]["obj2_prop2"] == 24
  798. assert json_results["metadata"]["allOf_test"]["obj3_prop1"] == 7.24
  799. assert json_results["metadata"]["allOf_test"]["obj3_prop2"] is True
  800. # ---------- end of 'update' command tests
  801. def test_list_help(script_runner):
  802. ret = script_runner.run("elyra-metadata", "list", METADATA_TEST_SCHEMASPACE, "--help")
  803. assert ret.success is False
  804. assert f"List installed metadata for {METADATA_TEST_SCHEMASPACE}." in ret.stdout
  805. def test_list_bad_argument(script_runner):
  806. ret = script_runner.run("elyra-metadata", "list", METADATA_TEST_SCHEMASPACE, "--bogus-argument")
  807. assert ret.success is False
  808. assert "ERROR: The following arguments were unexpected: ['--bogus-argument']" in ret.stdout
  809. def test_list_instances(script_runner, mock_data_dir):
  810. metadata_manager = MetadataManager(schemaspace=METADATA_TEST_SCHEMASPACE)
  811. ret = script_runner.run("elyra-metadata", "list", METADATA_TEST_SCHEMASPACE)
  812. assert ret.success
  813. lines = ret.stdout.split("\n")
  814. assert len(lines) == 2 # always 2 more than the actual runtime count
  815. assert f"No metadata instances found for {METADATA_TEST_SCHEMASPACE}" in lines[0]
  816. valid = Metadata(**valid_metadata_json)
  817. resource = metadata_manager.create("valid", valid)
  818. assert resource is not None
  819. resource = metadata_manager.create("valid2", valid)
  820. assert resource is not None
  821. another = Metadata(**another_metadata_json)
  822. resource = metadata_manager.create("another", another)
  823. assert resource is not None
  824. resource = metadata_manager.create("another2", another)
  825. assert resource is not None
  826. ret = script_runner.run("elyra-metadata", "list", METADATA_TEST_SCHEMASPACE)
  827. assert ret.success
  828. lines = ret.stdout.split("\n")
  829. assert len(lines) == 9 # always 5 more than the actual runtime count
  830. assert lines[0] == f"Available metadata instances for {METADATA_TEST_SCHEMASPACE} (includes invalid):"
  831. line_elements = [line.split() for line in lines[4:8]]
  832. assert line_elements[0][0] == "metadata-test"
  833. assert line_elements[0][1] == "another"
  834. assert line_elements[1][0] == "metadata-test"
  835. assert line_elements[1][1] == "another2"
  836. assert line_elements[2][0] == "metadata-test"
  837. assert line_elements[2][1] == "valid"
  838. assert line_elements[3][0] == "metadata-test"
  839. assert line_elements[3][1] == "valid2"
  840. # Remove the '2' runtimes and reconfirm smaller set
  841. metadata_manager.remove("valid2")
  842. metadata_manager.remove("another2")
  843. # Include two additional invalid files as well - one for uri failure, andother missing display_name
  844. metadata_dir = os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE)
  845. create_json_file(metadata_dir, "invalid.json", invalid_metadata_json)
  846. create_json_file(metadata_dir, "no_display_name.json", invalid_no_display_name_json)
  847. create_json_file(metadata_dir, "invalid_schema_name.json", invalid_schema_name_json)
  848. ret = script_runner.run("elyra-metadata", "list", METADATA_TEST_SCHEMASPACE)
  849. assert ret.success
  850. lines = ret.stdout.split("\n")
  851. assert len(lines) == 10 # always 5 more than the actual runtime count
  852. assert lines[0] == f"Available metadata instances for {METADATA_TEST_SCHEMASPACE} (includes invalid):"
  853. line_elements = [line.split() for line in lines[4:9]]
  854. assert line_elements[0][1] == "another"
  855. assert line_elements[1][1] == "invalid"
  856. assert line_elements[1][3] == "**INVALID**"
  857. assert line_elements[1][4] == "(ValidationError)"
  858. assert line_elements[2][3] == "**INVALID**"
  859. assert line_elements[2][4] == "(ValidationError)"
  860. assert line_elements[3][1] == "valid"
  861. assert line_elements[4][3] == "**INVALID**"
  862. assert line_elements[4][4] == "(SchemaNotFoundError)"
  863. ret = script_runner.run("elyra-metadata", "list", METADATA_TEST_SCHEMASPACE, "--valid-only")
  864. assert ret.success
  865. lines = ret.stdout.split("\n")
  866. assert len(lines) == 7 # always 5 more than the actual runtime count
  867. assert lines[0] == f"Available metadata instances for {METADATA_TEST_SCHEMASPACE} (valid only):"
  868. line_elements = [line.split() for line in lines[4:6]]
  869. assert line_elements[0][1] == "another"
  870. assert line_elements[1][1] == "valid"
  871. def test_list_json_instances(script_runner, mock_data_dir):
  872. metadata_manager = MetadataManager(schemaspace=METADATA_TEST_SCHEMASPACE)
  873. ret = script_runner.run("elyra-metadata", "list", METADATA_TEST_SCHEMASPACE, "--json")
  874. assert ret.success
  875. lines = ret.stdout.split("\n")
  876. assert len(lines) == 2
  877. assert lines[0] == "[]"
  878. valid = Metadata(**valid_metadata_json)
  879. resource = metadata_manager.create("valid", valid)
  880. assert resource is not None
  881. resource = metadata_manager.create("valid2", valid)
  882. assert resource is not None
  883. another = Metadata(**another_metadata_json)
  884. resource = metadata_manager.create("another", another)
  885. assert resource is not None
  886. resource = metadata_manager.create("another2", another)
  887. assert resource is not None
  888. ret = script_runner.run("elyra-metadata", "list", METADATA_TEST_SCHEMASPACE, "--json")
  889. assert ret.success
  890. # Consume results
  891. results = json.loads(ret.stdout)
  892. assert len(results) == 4
  893. # Remove the '2' runtimes and reconfirm smaller set
  894. metadata_manager.remove("valid2")
  895. metadata_manager.remove("another2")
  896. # Include two additional invalid files as well - one for uri failure, andother missing display_name
  897. metadata_dir = os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE)
  898. create_json_file(metadata_dir, "invalid.json", invalid_metadata_json)
  899. create_json_file(metadata_dir, "no_display_name.json", invalid_no_display_name_json)
  900. ret = script_runner.run("elyra-metadata", "list", METADATA_TEST_SCHEMASPACE, "--json")
  901. assert ret.success
  902. results = json.loads(ret.stdout)
  903. assert len(results) == 4
  904. ret = script_runner.run("elyra-metadata", "list", METADATA_TEST_SCHEMASPACE, "--json", "--valid-only")
  905. assert ret.success
  906. results = json.loads(ret.stdout)
  907. assert len(results) == 2
  908. def test_remove_help(script_runner):
  909. ret = script_runner.run("elyra-metadata", "remove", METADATA_TEST_SCHEMASPACE, "--help")
  910. assert ret.success is False
  911. assert f"Remove a metadata instance from schemaspace '{METADATA_TEST_SCHEMASPACE}'." in ret.stdout
  912. def test_remove_no_name(script_runner):
  913. ret = script_runner.run("elyra-metadata", "remove", METADATA_TEST_SCHEMASPACE)
  914. assert ret.success is False
  915. assert "ERROR: '--name' is a required parameter." in ret.stdout
  916. def test_remove_with_no_equals(script_runner, mock_data_dir):
  917. # Attempt removal w/o the '=' between parameter and value
  918. metadata_manager = MetadataManager(schemaspace=METADATA_TEST_SCHEMASPACE)
  919. valid = Metadata(**valid_metadata_json)
  920. metadata_manager.create("valid", valid)
  921. ret = script_runner.run("elyra-metadata", "remove", METADATA_TEST_SCHEMASPACE, "--name", "valid")
  922. assert ret.success is True
  923. def test_remove_missing(script_runner, mock_data_dir):
  924. # Create an instance so that the schemaspace exists.
  925. metadata_manager = MetadataManager(schemaspace=METADATA_TEST_SCHEMASPACE)
  926. valid = Metadata(**valid_metadata_json)
  927. metadata_manager.create("valid", valid)
  928. ret = script_runner.run("elyra-metadata", "remove", METADATA_TEST_SCHEMASPACE, "--name=missing")
  929. assert ret.success is False
  930. assert "No such instance named 'missing' was found in the metadata-tests schemaspace." in ret.stdout
  931. # Now cleanup original instance.
  932. ret = script_runner.run("elyra-metadata", "remove", METADATA_TEST_SCHEMASPACE, "--name=valid")
  933. assert ret.success
  934. def test_remove_instance(script_runner, mock_data_dir):
  935. metadata_manager = MetadataManager(schemaspace=METADATA_TEST_SCHEMASPACE)
  936. valid = Metadata(**valid_metadata_json)
  937. resource = metadata_manager.create("valid", valid)
  938. assert resource is not None
  939. resource = metadata_manager.create("valid2", valid)
  940. assert resource is not None
  941. another = Metadata(**another_metadata_json)
  942. resource = metadata_manager.create("another", another)
  943. assert resource is not None
  944. resource = metadata_manager.create("another2", another)
  945. assert resource is not None
  946. ret = script_runner.run("elyra-metadata", "remove", METADATA_TEST_SCHEMASPACE, "--name=valid")
  947. assert ret.success
  948. ret = script_runner.run("elyra-metadata", "remove", METADATA_TEST_SCHEMASPACE, "--name=another")
  949. assert ret.success
  950. instances = metadata_manager.get_all()
  951. assert len(instances) == 2
  952. assert instances[0].name.endswith("2")
  953. assert instances[1].name.endswith("2")
  954. def test_export_help(script_runner):
  955. ret = script_runner.run("elyra-metadata", "export", METADATA_TEST_SCHEMASPACE, "--help")
  956. assert ret.success is False
  957. assert f"Export installed metadata in schemaspace '{METADATA_TEST_SCHEMASPACE}'" in ret.stdout
  958. def test_export_no_directory(script_runner):
  959. ret = script_runner.run("elyra-metadata", "export", METADATA_TEST_SCHEMASPACE)
  960. assert ret.success is False
  961. assert "'--directory' is a required parameter." in ret.stdout
  962. def test_export_bad_argument(script_runner):
  963. ret = script_runner.run(
  964. "elyra-metadata", "export", METADATA_TEST_SCHEMASPACE, "--directory=dummy-directory", "--bogus-argument"
  965. )
  966. assert ret.success is False
  967. assert "The following arguments were unexpected: ['--bogus-argument']" in ret.stdout
  968. def test_export_bad_schemaspace(script_runner):
  969. ret = script_runner.run("elyra-metadata", "export", "bogus-schemaspace")
  970. assert ret.success is False
  971. assert "Subcommand 'bogus-schemaspace' is invalid." in ret.stdout
  972. def test_export_bad_schema(script_runner):
  973. ret = script_runner.run(
  974. "elyra-metadata",
  975. "export",
  976. METADATA_TEST_SCHEMASPACE,
  977. "--directory=dummy-directory",
  978. "--schema_name=bogus-schema",
  979. )
  980. assert (
  981. "Schema name 'bogus-schema' is invalid. For the 'metadata-tests' schemaspace, "
  982. "the schema name must be one of ['metadata-test', 'metadata-test2']" in ret.stdout
  983. )
  984. assert ret.success is False
  985. def test_export_no_schema_no_instances(script_runner, mock_data_dir):
  986. ret = script_runner.run("elyra-metadata", "export", METADATA_TEST_SCHEMASPACE, "--directory=dummy-directory")
  987. assert ret.success
  988. assert f"No metadata instances found for schemaspace '{METADATA_TEST_SCHEMASPACE}'" in ret.stdout
  989. assert "Nothing exported to 'dummy-directory'" in ret.stdout
  990. def test_export_inaccessible_directory(script_runner, mock_data_dir):
  991. metadata_manager = MetadataManager(schemaspace=METADATA_TEST_SCHEMASPACE)
  992. # create metadata
  993. valid = Metadata(**valid_metadata_json)
  994. resource = metadata_manager.create("valid", valid)
  995. assert resource is not None
  996. directory_parameter = "/dummy-directory"
  997. ret = script_runner.run("elyra-metadata", "export", METADATA_TEST_SCHEMASPACE, f"--directory={directory_parameter}")
  998. assert ret.success is False
  999. assert f"Error creating directory structure for '{directory_parameter}/{METADATA_TEST_SCHEMASPACE}': " in ret.stdout
  1000. assert any(ele in ret.stdout for ele in ["Read-only file system: ", "Permission denied: ", "Access Denied: "])
  1001. assert f"'{directory_parameter}'" in ret.stdout
  1002. def test_export_with_schema_no_instances(script_runner, mock_data_dir):
  1003. metadata_manager = MetadataManager(schemaspace=METADATA_TEST_SCHEMASPACE)
  1004. # create metadata in a different schema
  1005. valid = Metadata(**valid_metadata_json)
  1006. resource = metadata_manager.create("valid", valid)
  1007. assert resource is not None
  1008. ret = script_runner.run(
  1009. "elyra-metadata",
  1010. "export",
  1011. METADATA_TEST_SCHEMASPACE,
  1012. "--schema_name=metadata-test2",
  1013. "--directory=dummy-directory",
  1014. )
  1015. assert ret.success
  1016. assert (
  1017. f"No metadata instances found for schemaspace '{METADATA_TEST_SCHEMASPACE}' "
  1018. f"and schema 'metadata-test2'" in ret.stdout
  1019. )
  1020. assert "Nothing exported to 'dummy-directory'" in ret.stdout
  1021. def test_export_no_schema_with_instances(script_runner, mock_data_dir):
  1022. metadata_manager = MetadataManager(schemaspace=METADATA_TEST_SCHEMASPACE)
  1023. # create valid metadata
  1024. valid = Metadata(**valid_metadata_json)
  1025. resource = metadata_manager.create("valid", valid)
  1026. assert resource is not None
  1027. # create invalid metadata
  1028. metadata_dir = os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE)
  1029. create_json_file(metadata_dir, "invalid.json", invalid_metadata_json)
  1030. create_json_file(metadata_dir, "invalid2.json", invalid_metadata_json)
  1031. # test for valid and invalid
  1032. temp_dir = TemporaryDirectory()
  1033. directory_parameter = temp_dir.name
  1034. ret = script_runner.run(
  1035. "elyra-metadata", "export", METADATA_TEST_SCHEMASPACE, "--include-invalid", f"--directory={directory_parameter}"
  1036. )
  1037. assert ret.success
  1038. export_directory = os.path.join(directory_parameter, METADATA_TEST_SCHEMASPACE)
  1039. assert f"Creating directory structure for '{export_directory}'" in ret.stdout
  1040. assert (
  1041. f"Exporting metadata instances for schemaspace '{METADATA_TEST_SCHEMASPACE}' "
  1042. f"(includes invalid) to '{export_directory}'" in ret.stdout
  1043. )
  1044. assert "Exported 3 instances (2 of which are invalid)" in ret.stdout
  1045. exported_metadata = sorted(os.listdir(export_directory), key=str.casefold)
  1046. assert len(exported_metadata) == 3
  1047. assert exported_metadata[0] == "invalid.json"
  1048. assert exported_metadata[1] == "invalid2.json"
  1049. assert exported_metadata[2] == "valid.json"
  1050. temp_dir.cleanup()
  1051. # test for valid and invalid using '--include-invalid' option, which
  1052. # prior to version 4.0 is a no-op
  1053. temp_dir = TemporaryDirectory()
  1054. directory_parameter = temp_dir.name
  1055. ret = script_runner.run(
  1056. "elyra-metadata", "export", METADATA_TEST_SCHEMASPACE, f"--directory={directory_parameter}", "--include-invalid"
  1057. )
  1058. assert ret.success
  1059. export_directory = os.path.join(directory_parameter, METADATA_TEST_SCHEMASPACE)
  1060. assert f"Creating directory structure for '{export_directory}'" in ret.stdout
  1061. assert (
  1062. f"Exporting metadata instances for schemaspace '{METADATA_TEST_SCHEMASPACE}' "
  1063. f"(includes invalid) to '{export_directory}'" in ret.stdout
  1064. )
  1065. assert "Exported 3 instances (2 of which are invalid)" in ret.stdout
  1066. exported_metadata = sorted(os.listdir(export_directory), key=str.casefold)
  1067. assert len(exported_metadata) == 3
  1068. assert exported_metadata[0] == "invalid.json"
  1069. assert exported_metadata[1] == "invalid2.json"
  1070. assert exported_metadata[2] == "valid.json"
  1071. temp_dir.cleanup()
  1072. # test for valid only
  1073. temp_dir = TemporaryDirectory()
  1074. directory_parameter = temp_dir.name
  1075. ret = script_runner.run("elyra-metadata", "export", METADATA_TEST_SCHEMASPACE, f"--directory={directory_parameter}")
  1076. assert ret.success
  1077. export_directory = os.path.join(directory_parameter, METADATA_TEST_SCHEMASPACE)
  1078. assert f"Creating directory structure for '{export_directory}'" in ret.stdout
  1079. assert (
  1080. f"Exporting metadata instances for schemaspace '{METADATA_TEST_SCHEMASPACE}' "
  1081. f"(valid only) to '{export_directory}'" in ret.stdout
  1082. )
  1083. assert "Exported 1 instance (0 of which are invalid)" in ret.stdout
  1084. exported_metadata = os.listdir(export_directory)
  1085. assert len(exported_metadata) == 1
  1086. assert exported_metadata[0] == "valid.json"
  1087. temp_dir.cleanup()
  1088. def test_export_with_schema_with_instances(script_runner, mock_data_dir):
  1089. metadata_manager = MetadataManager(schemaspace=METADATA_TEST_SCHEMASPACE)
  1090. # create valid metadata
  1091. valid = Metadata(**valid_metadata_json)
  1092. resource = metadata_manager.create("valid", valid)
  1093. assert resource is not None
  1094. valid = Metadata(**valid_metadata2_json)
  1095. resource = metadata_manager.create("valid2", valid)
  1096. assert resource is not None
  1097. # create invalid metadata
  1098. metadata_dir = os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE)
  1099. create_json_file(metadata_dir, "invalid.json", invalid_metadata_json)
  1100. # create export directory
  1101. temp_dir = TemporaryDirectory()
  1102. directory_parameter = temp_dir.name
  1103. # test for valid and invalid
  1104. ret = script_runner.run(
  1105. "elyra-metadata",
  1106. "export",
  1107. METADATA_TEST_SCHEMASPACE,
  1108. "--include-invalid",
  1109. "--schema_name=metadata-test",
  1110. f"--directory={directory_parameter}",
  1111. )
  1112. assert ret.success
  1113. export_directory = os.path.join(directory_parameter, METADATA_TEST_SCHEMASPACE)
  1114. assert f"Creating directory structure for '{export_directory}'" in ret.stdout
  1115. assert (
  1116. f"Exporting metadata instances for schemaspace '{METADATA_TEST_SCHEMASPACE}'"
  1117. f" and schema 'metadata-test' (includes invalid) to '{export_directory}'" in ret.stdout
  1118. )
  1119. assert "Exported 2 instances (1 of which is invalid)" in ret.stdout
  1120. exported_metadata = sorted(os.listdir(export_directory), key=str.casefold)
  1121. assert len(exported_metadata) == 2
  1122. assert exported_metadata[0] == "invalid.json"
  1123. assert exported_metadata[1] == "valid.json"
  1124. temp_dir.cleanup()
  1125. # create export directory
  1126. temp_dir = TemporaryDirectory()
  1127. directory_parameter = temp_dir.name
  1128. # test for valid only
  1129. ret = script_runner.run(
  1130. "elyra-metadata",
  1131. "export",
  1132. METADATA_TEST_SCHEMASPACE,
  1133. "--schema_name=metadata-test",
  1134. f"--directory={directory_parameter}",
  1135. )
  1136. assert ret.success
  1137. export_directory = os.path.join(directory_parameter, METADATA_TEST_SCHEMASPACE)
  1138. assert f"Creating directory structure for '{export_directory}'" in ret.stdout
  1139. assert (
  1140. f"Exporting metadata instances for schemaspace '{METADATA_TEST_SCHEMASPACE}'"
  1141. f" and schema 'metadata-test' (valid only) to '{export_directory}'" in ret.stdout
  1142. )
  1143. assert "Exported 1 instance (0 of which are invalid)" in ret.stdout
  1144. exported_metadata = os.listdir(export_directory)
  1145. assert len(exported_metadata) == 1
  1146. assert exported_metadata[0] == "valid.json"
  1147. temp_dir.cleanup()
  1148. def test_export_without_clean(script_runner, mock_data_dir):
  1149. metadata_manager = MetadataManager(schemaspace=METADATA_TEST_SCHEMASPACE)
  1150. # create valid metadata
  1151. valid = Metadata(**valid_metadata_json)
  1152. resource = metadata_manager.create("valid", valid)
  1153. assert resource is not None
  1154. # create export directory
  1155. temp_dir = TemporaryDirectory()
  1156. directory_parameter = temp_dir.name
  1157. export_directory = os.path.join(directory_parameter, METADATA_TEST_SCHEMASPACE)
  1158. os.mkdir(export_directory)
  1159. # create metadata file with dummy data and verify its contents
  1160. dummy_json = {"required_test": "required_value"}
  1161. metadata_filename = "valid.json"
  1162. create_json_file(export_directory, metadata_filename, dummy_json)
  1163. metadata_file_path = os.path.join(export_directory, metadata_filename)
  1164. assert os.path.exists(metadata_file_path)
  1165. assert json.loads(open(metadata_file_path).read()) == dummy_json
  1166. # create additional dummy file with a different name and verify its contents
  1167. dummy_filename = "dummy.json"
  1168. create_json_file(export_directory, dummy_filename, dummy_json)
  1169. dummy_file_path = os.path.join(export_directory, dummy_filename)
  1170. assert os.path.exists(dummy_file_path)
  1171. assert json.loads(open(dummy_file_path).read()) == dummy_json
  1172. # create dummy file under different folder (different schema) and verify its contents
  1173. export_directory_other = os.path.join(directory_parameter, "runtimes")
  1174. os.mkdir(export_directory_other)
  1175. dummy_filename_other = "dummy.json"
  1176. create_json_file(export_directory_other, dummy_filename_other, dummy_json)
  1177. dummy_file_path_other = os.path.join(export_directory_other, dummy_filename_other)
  1178. assert os.path.exists(dummy_file_path_other)
  1179. assert json.loads(open(dummy_file_path_other).read()) == dummy_json
  1180. # export metadata without --clean flag
  1181. ret = script_runner.run(
  1182. "elyra-metadata",
  1183. "export",
  1184. METADATA_TEST_SCHEMASPACE,
  1185. "--schema_name=metadata-test",
  1186. f"--directory={directory_parameter}",
  1187. )
  1188. assert ret.success
  1189. assert f"Creating directory structure for '{export_directory}'" not in ret.stdout
  1190. assert (
  1191. f"Exporting metadata instances for schemaspace '{METADATA_TEST_SCHEMASPACE}'"
  1192. f" and schema 'metadata-test' (valid only) to '{export_directory}'" in ret.stdout
  1193. )
  1194. assert "Exported 1 instance (0 of which are invalid)" in ret.stdout
  1195. # verify that the metadata file was overwritten while both the dummy files were left as is
  1196. export_directory_files = sorted(os.listdir(export_directory), key=str.casefold)
  1197. assert len(export_directory_files) == 2
  1198. assert export_directory_files[0] == dummy_filename
  1199. assert json.loads(open(dummy_file_path).read()) == dummy_json
  1200. assert export_directory_files[1] == metadata_filename
  1201. exported_metadata = json.loads(open(metadata_file_path).read())
  1202. assert "schema_name" in exported_metadata
  1203. assert exported_metadata.get("schema_name") == valid_metadata_json.get("schema_name")
  1204. export_directory_other_files = sorted(os.listdir(export_directory_other), key=str.casefold)
  1205. assert len(export_directory_other_files) == 1
  1206. assert export_directory_other_files[0] == dummy_filename_other
  1207. assert json.loads(open(dummy_file_path_other).read()) == dummy_json
  1208. temp_dir.cleanup()
  1209. def test_export_clean(script_runner, mock_data_dir):
  1210. metadata_manager = MetadataManager(schemaspace=METADATA_TEST_SCHEMASPACE)
  1211. # create valid metadata
  1212. valid = Metadata(**valid_metadata_json)
  1213. resource = metadata_manager.create("valid", valid)
  1214. assert resource is not None
  1215. # create export directory
  1216. temp_dir = TemporaryDirectory()
  1217. directory_parameter = temp_dir.name
  1218. export_directory = os.path.join(directory_parameter, METADATA_TEST_SCHEMASPACE)
  1219. os.mkdir(export_directory)
  1220. # create metadata file with dummy data and verify its contents
  1221. dummy_json = {"required_test": "required_value"}
  1222. metadata_filename = "valid.json"
  1223. create_json_file(export_directory, metadata_filename, dummy_json)
  1224. metadata_file_path = os.path.join(export_directory, metadata_filename)
  1225. assert os.path.exists(metadata_file_path)
  1226. assert json.loads(open(metadata_file_path).read()) == dummy_json
  1227. # create additional dummy file with a different name and verify its contents
  1228. dummy_filename = "dummy.json"
  1229. create_json_file(export_directory, dummy_filename, dummy_json)
  1230. dummy_file_path = os.path.join(export_directory, dummy_filename)
  1231. assert os.path.exists(dummy_file_path)
  1232. assert json.loads(open(dummy_file_path).read()) == dummy_json
  1233. # create dummy file under different folder (different schema) and verify its contents
  1234. export_directory_other = os.path.join(directory_parameter, "runtimes")
  1235. os.mkdir(export_directory_other)
  1236. dummy_filename_other = "dummy.json"
  1237. create_json_file(export_directory_other, dummy_filename_other, dummy_json)
  1238. dummy_file_path_other = os.path.join(export_directory_other, dummy_filename_other)
  1239. assert os.path.exists(dummy_file_path_other)
  1240. assert json.loads(open(dummy_file_path_other).read()) == dummy_json
  1241. # export metadata with --clean flag
  1242. ret = script_runner.run(
  1243. "elyra-metadata",
  1244. "export",
  1245. METADATA_TEST_SCHEMASPACE,
  1246. "--clean",
  1247. "--schema_name=metadata-test",
  1248. f"--directory={directory_parameter}",
  1249. )
  1250. assert ret.success
  1251. assert f"Creating directory structure for '{export_directory}'" not in ret.stdout
  1252. assert f"Cleaning out all files in '{export_directory}'" in ret.stdout
  1253. assert (
  1254. f"Exporting metadata instances for schemaspace '{METADATA_TEST_SCHEMASPACE}'"
  1255. f" and schema 'metadata-test' (valid only) to '{export_directory}'" in ret.stdout
  1256. )
  1257. assert "Exported 1 instance (0 of which are invalid)" in ret.stdout
  1258. # verify that the metadata file was overwritten and dummy file within the same schema folder was deleted
  1259. # whereas the dummy file within the other schema folder was left as is
  1260. export_directory_files = os.listdir(export_directory)
  1261. assert len(export_directory_files) == 1
  1262. assert export_directory_files[0] == metadata_filename
  1263. exported_metadata = json.loads(open(metadata_file_path).read())
  1264. assert "schema_name" in exported_metadata
  1265. assert exported_metadata.get("schema_name") == valid_metadata_json.get("schema_name")
  1266. export_directory_other_files = sorted(os.listdir(export_directory_other), key=str.casefold)
  1267. assert len(export_directory_other_files) == 1
  1268. assert export_directory_other_files[0] == dummy_filename_other
  1269. assert json.loads(open(dummy_file_path_other).read()) == dummy_json
  1270. temp_dir.cleanup()
  1271. def test_import_help(script_runner):
  1272. ret = script_runner.run("elyra-metadata", "import", METADATA_TEST_SCHEMASPACE, "--help")
  1273. assert ret.success is False
  1274. assert f"\nImport metadata instances into schemaspace '{METADATA_TEST_SCHEMASPACE}'" in ret.stdout
  1275. def test_import_no_directory(script_runner):
  1276. ret = script_runner.run("elyra-metadata", "import", METADATA_TEST_SCHEMASPACE)
  1277. assert ret.success is False
  1278. assert "ERROR: '--directory' is a required parameter." in ret.stdout
  1279. def test_import_bad_argument(script_runner):
  1280. ret = script_runner.run(
  1281. "elyra-metadata", "import", METADATA_TEST_SCHEMASPACE, "--directory=dummy-directory", "--bogus-argument"
  1282. )
  1283. assert ret.success is False
  1284. assert "ERROR: The following arguments were unexpected: ['--bogus-argument']" in ret.stdout
  1285. def test_import_bad_schemaspace(script_runner):
  1286. ret = script_runner.run("elyra-metadata", "import", "bogus-schemaspace")
  1287. assert ret.success is False
  1288. assert "Subcommand 'bogus-schemaspace' is invalid." in ret.stdout
  1289. def test_import_inaccessible_directory(script_runner):
  1290. directory_parameter = "/dummy-directory"
  1291. ret = script_runner.run("elyra-metadata", "import", METADATA_TEST_SCHEMASPACE, f"--directory={directory_parameter}")
  1292. assert ret.success is False
  1293. assert (
  1294. f"Unable to reach the '{directory_parameter}'"
  1295. f" directory: No such file or directory: '{directory_parameter}" in ret.stdout
  1296. )
  1297. def test_import_empty_directory(script_runner):
  1298. # create import directory
  1299. temp_dir = TemporaryDirectory()
  1300. directory_parameter = temp_dir.name
  1301. # import metadata
  1302. ret = script_runner.run("elyra-metadata", "import", METADATA_TEST_SCHEMASPACE, f"--directory={directory_parameter}")
  1303. assert ret.success is True
  1304. assert f"No instances for import found in the '{directory_parameter}' directory" in ret.stdout
  1305. temp_dir.cleanup()
  1306. def test_import_non_json_file(script_runner):
  1307. # create import directory
  1308. temp_dir = TemporaryDirectory()
  1309. directory_parameter = temp_dir.name
  1310. # add a dummy file in the directory
  1311. dummy_filename = "dummy.txt"
  1312. dummy_filepath = os.path.join(directory_parameter, dummy_filename)
  1313. dummy_file_content = "This is a dummy txt file."
  1314. with open(dummy_filepath, "w") as f:
  1315. f.write(dummy_file_content)
  1316. assert os.path.exists(dummy_filepath)
  1317. assert open(dummy_filepath).read() == dummy_file_content
  1318. # import metadata
  1319. ret = script_runner.run("elyra-metadata", "import", METADATA_TEST_SCHEMASPACE, f"--directory={directory_parameter}")
  1320. assert ret.success is True
  1321. assert f"No instances for import found in the '{directory_parameter}' directory" in ret.stdout
  1322. temp_dir.cleanup()
  1323. def test_import_valid_metadata_files(script_runner, mock_data_dir):
  1324. # create import directory
  1325. temp_dir = TemporaryDirectory()
  1326. directory_parameter = temp_dir.name
  1327. # add valid metadata json files in the directory
  1328. metadata_filename = "valid.json"
  1329. metadata_file_path = os.path.join(directory_parameter, metadata_filename)
  1330. with open(metadata_file_path, "w") as f:
  1331. json.dump(valid_metadata_json, f)
  1332. assert os.path.exists(metadata_file_path)
  1333. assert json.loads(open(metadata_file_path).read()) == valid_metadata_json
  1334. metadata_filename2 = "valid2.json"
  1335. metadata_file_path2 = os.path.join(directory_parameter, metadata_filename2)
  1336. with open(metadata_file_path2, "w") as f:
  1337. json.dump(valid_metadata2_json, f)
  1338. assert os.path.exists(metadata_file_path2)
  1339. assert json.loads(open(metadata_file_path2).read()) == valid_metadata2_json
  1340. # import metadata
  1341. ret = script_runner.run("elyra-metadata", "import", METADATA_TEST_SCHEMASPACE, f"--directory={directory_parameter}")
  1342. assert ret.success is True
  1343. assert "Imported 2 instances" in ret.stdout
  1344. temp_dir.cleanup()
  1345. # verify contents of imported metadata
  1346. metadata_directory = os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE)
  1347. assert os.path.isdir(metadata_directory)
  1348. installed_metadata_file_path = os.path.join(metadata_directory, metadata_filename)
  1349. assert os.path.isfile(installed_metadata_file_path)
  1350. with open(installed_metadata_file_path, "r") as metadata_file:
  1351. instance_json = json.load(metadata_file)
  1352. assert instance_json["schema_name"] == valid_metadata_json["schema_name"]
  1353. assert instance_json["display_name"] == valid_metadata_json["display_name"]
  1354. assert instance_json["metadata"]["required_test"] == valid_metadata_json["metadata"]["required_test"]
  1355. assert instance_json["metadata"]["uri_test"] == valid_metadata_json["metadata"]["uri_test"]
  1356. assert instance_json["metadata"]["number_range_test"] == valid_metadata_json["metadata"]["number_range_test"]
  1357. installed_metadata_file_path = os.path.join(metadata_directory, metadata_filename2)
  1358. assert os.path.isfile(installed_metadata_file_path)
  1359. with open(installed_metadata_file_path, "r") as metadata_file:
  1360. instance_json = json.load(metadata_file)
  1361. assert instance_json["schema_name"] == valid_metadata2_json["schema_name"]
  1362. assert instance_json["display_name"] == valid_metadata2_json["display_name"]
  1363. assert instance_json["metadata"]["required_test"] == valid_metadata2_json["metadata"]["required_test"]
  1364. assert instance_json["metadata"]["uri_test"] == valid_metadata2_json["metadata"]["uri_test"]
  1365. assert instance_json["metadata"]["number_range_test"] == valid_metadata2_json["metadata"]["number_range_test"]
  1366. def test_import_invalid_metadata_file(script_runner, mock_data_dir):
  1367. # create import directory
  1368. temp_dir = TemporaryDirectory()
  1369. directory_parameter = temp_dir.name
  1370. # add invalid metadata json file in the directory
  1371. metadata_filename = "invalid.json"
  1372. metadata_file_path = os.path.join(directory_parameter, metadata_filename)
  1373. with open(metadata_file_path, "w") as f:
  1374. json.dump(invalid_metadata_json, f)
  1375. assert os.path.exists(metadata_file_path)
  1376. assert json.loads(open(metadata_file_path).read()) == invalid_metadata_json
  1377. # import metadata
  1378. ret = script_runner.run("elyra-metadata", "import", METADATA_TEST_SCHEMASPACE, f"--directory={directory_parameter}")
  1379. assert ret.success is True
  1380. lines = ret.stdout.split("\n")
  1381. assert len(lines) == 8
  1382. assert "Imported 0 instances" in lines[0]
  1383. assert "1 instance could not be imported" in lines[1]
  1384. assert "The following files could not be imported:" in lines[3]
  1385. assert lines[6].startswith("invalid.json")
  1386. assert (
  1387. lines[6]
  1388. .strip()
  1389. .endswith(
  1390. "Validation failed for instance 'invalid' using the metadata-test "
  1391. + "schema with error: '//localhost:8081/' is not a 'uri'."
  1392. )
  1393. )
  1394. temp_dir.cleanup()
  1395. def test_import_with_subfolder(script_runner, mock_data_dir):
  1396. # create import directory
  1397. temp_dir = TemporaryDirectory()
  1398. directory_parameter = temp_dir.name
  1399. # add valid metadata json file in the import directory
  1400. metadata_filename = "valid.json"
  1401. metadata_file_path = os.path.join(directory_parameter, metadata_filename)
  1402. with open(metadata_file_path, "w") as f:
  1403. json.dump(valid_metadata_json, f)
  1404. assert os.path.exists(metadata_file_path)
  1405. assert json.loads(open(metadata_file_path).read()) == valid_metadata_json
  1406. # add invalid metadata json files in the directory
  1407. invalid_metadata_filename = "invalid.json"
  1408. invalid_metadata_file_path = os.path.join(directory_parameter, invalid_metadata_filename)
  1409. with open(invalid_metadata_file_path, "w") as f:
  1410. json.dump(invalid_metadata_json, f)
  1411. assert os.path.exists(invalid_metadata_file_path)
  1412. assert json.loads(open(invalid_metadata_file_path).read()) == invalid_metadata_json
  1413. invalid_metadata_file_path2 = os.path.join(directory_parameter, "invalid2.json")
  1414. shutil.copyfile(invalid_metadata_file_path, invalid_metadata_file_path2)
  1415. assert os.path.exists(invalid_metadata_file_path2)
  1416. assert json.loads(open(invalid_metadata_file_path2).read()) == invalid_metadata_json
  1417. # create a sub-folder within import directory and add a valid metadata file in it
  1418. os.mkdir(os.path.join(directory_parameter, "subfolder"))
  1419. metadata_filename2 = "valid2.json"
  1420. metadata_file_path2 = os.path.join(directory_parameter, "subfolder", metadata_filename2)
  1421. with open(metadata_file_path2, "w") as f:
  1422. json.dump(valid_metadata2_json, f)
  1423. assert os.path.exists(metadata_file_path2)
  1424. assert json.loads(open(metadata_file_path2).read()) == valid_metadata2_json
  1425. # import metadata
  1426. ret = script_runner.run("elyra-metadata", "import", METADATA_TEST_SCHEMASPACE, f"--directory={directory_parameter}")
  1427. assert ret.success is True
  1428. lines = ret.stdout.split("\n")
  1429. assert len(lines) == 9
  1430. assert "Imported 1 instance" in lines[0]
  1431. assert "2 instances could not be imported" in lines[1]
  1432. assert "The following files could not be imported:" in lines[3]
  1433. assert lines[6].startswith("invalid.json")
  1434. assert (
  1435. lines[6]
  1436. .strip()
  1437. .endswith(
  1438. "Validation failed for instance 'invalid' using the metadata-test "
  1439. + "schema with error: '//localhost:8081/' is not a 'uri'."
  1440. )
  1441. )
  1442. assert lines[7].startswith("invalid2.json")
  1443. assert (
  1444. lines[7]
  1445. .strip()
  1446. .endswith(
  1447. "Validation failed for instance 'invalid2' using the metadata-test "
  1448. + "schema with error: '//localhost:8081/' is not a 'uri'."
  1449. )
  1450. )
  1451. temp_dir.cleanup()
  1452. # verify contents of imported metadata
  1453. assert os.path.isdir(os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE))
  1454. installed_metadata_file_path = os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE, metadata_filename)
  1455. assert os.path.isfile(installed_metadata_file_path)
  1456. with open(installed_metadata_file_path, "r") as metadata_file:
  1457. instance_json = json.load(metadata_file)
  1458. assert instance_json["schema_name"] == valid_metadata_json["schema_name"]
  1459. assert instance_json["display_name"] == valid_metadata_json["display_name"]
  1460. assert instance_json["metadata"]["required_test"] == valid_metadata_json["metadata"]["required_test"]
  1461. assert instance_json["metadata"]["uri_test"] == valid_metadata_json["metadata"]["uri_test"]
  1462. assert instance_json["metadata"]["number_range_test"] == valid_metadata_json["metadata"]["number_range_test"]
  1463. def test_import_overwrite_flag(script_runner, mock_data_dir):
  1464. metadata_manager = MetadataManager(schemaspace=METADATA_TEST_SCHEMASPACE)
  1465. # create valid metadata
  1466. valid = Metadata(**valid_metadata_json)
  1467. resource = metadata_manager.create("valid", valid)
  1468. assert resource is not None
  1469. # create import directory
  1470. temp_dir = TemporaryDirectory()
  1471. directory_parameter = temp_dir.name
  1472. # add valid metadata json file for existing metadata in the import directory
  1473. metadata_filename = "valid.json"
  1474. metadata_file_path = os.path.join(directory_parameter, metadata_filename)
  1475. with open(metadata_file_path, "w") as f:
  1476. json.dump(valid_metadata_json, f)
  1477. assert os.path.exists(metadata_file_path)
  1478. assert json.loads(open(metadata_file_path).read()) == valid_metadata_json
  1479. # add valid metadata json file for new metadata
  1480. metadata_filename2 = "valid2.json"
  1481. metadata_file_path2 = os.path.join(directory_parameter, metadata_filename2)
  1482. with open(metadata_file_path2, "w") as f:
  1483. json.dump(valid_metadata_json, f)
  1484. assert os.path.exists(metadata_file_path2)
  1485. assert json.loads(open(metadata_file_path2).read()) == valid_metadata_json
  1486. # import metadata without overwrite flag
  1487. ret = script_runner.run("elyra-metadata", "import", METADATA_TEST_SCHEMASPACE, f"--directory={directory_parameter}")
  1488. assert ret.success is True
  1489. lines = ret.stdout.split("\n")
  1490. assert len(lines) == 8
  1491. assert "Imported 1 instance" in lines[0]
  1492. assert "1 instance could not be imported" in lines[1]
  1493. assert "The following files could not be imported:" in lines[3]
  1494. assert lines[6].startswith("valid.json")
  1495. assert (
  1496. lines[6]
  1497. .strip()
  1498. .endswith(
  1499. "An instance named 'valid' already exists in the metadata-tests "
  1500. + "schemaspace. Use '--overwrite' to update."
  1501. )
  1502. )
  1503. # verify contents of imported metadata
  1504. assert os.path.isdir(os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE))
  1505. installed_metadata_file_path2 = os.path.join(
  1506. mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE, metadata_filename2
  1507. )
  1508. assert os.path.isfile(installed_metadata_file_path2)
  1509. with open(installed_metadata_file_path2, "r") as metadata_file:
  1510. instance_json = json.load(metadata_file)
  1511. assert instance_json["schema_name"] == valid_metadata_json["schema_name"]
  1512. assert instance_json["display_name"] == valid_metadata_json["display_name"]
  1513. assert instance_json["metadata"]["required_test"] == valid_metadata_json["metadata"]["required_test"]
  1514. assert instance_json["metadata"]["uri_test"] == valid_metadata_json["metadata"]["uri_test"]
  1515. assert instance_json["metadata"]["number_range_test"] == valid_metadata_json["metadata"]["number_range_test"]
  1516. # replace one of the metadata files with new content
  1517. os.remove(metadata_file_path2)
  1518. assert os.path.exists(metadata_file_path2) is False
  1519. with open(metadata_file_path2, "w") as f:
  1520. json.dump(valid_display_name_json, f)
  1521. assert os.path.exists(metadata_file_path2)
  1522. assert json.loads(open(metadata_file_path2).read()) == valid_display_name_json
  1523. # add another valid metadata json file for new metadata
  1524. metadata_filename3 = "another.json"
  1525. metadata_file_path3 = os.path.join(directory_parameter, metadata_filename3)
  1526. with open(metadata_file_path3, "w") as f:
  1527. json.dump(another_metadata_json, f)
  1528. assert os.path.exists(metadata_file_path3)
  1529. assert json.loads(open(metadata_file_path3).read()) == another_metadata_json
  1530. # re-try import metadata with overwrite flag
  1531. ret = script_runner.run(
  1532. "elyra-metadata",
  1533. "import",
  1534. METADATA_TEST_SCHEMASPACE,
  1535. f"--directory={directory_parameter}",
  1536. "--overwrite",
  1537. )
  1538. assert ret.success is True
  1539. assert "Imported 3 instances" in ret.stdout
  1540. temp_dir.cleanup()
  1541. # verify contents of existing (unchanged) metadata
  1542. assert os.path.isdir(os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE))
  1543. installed_metadata_file_path = os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE, metadata_filename)
  1544. assert os.path.isfile(installed_metadata_file_path)
  1545. with open(installed_metadata_file_path, "r") as metadata_file:
  1546. instance_json = json.load(metadata_file)
  1547. assert instance_json["schema_name"] == valid_metadata_json["schema_name"]
  1548. assert instance_json["display_name"] == valid_metadata_json["display_name"]
  1549. assert instance_json["metadata"]["required_test"] == valid_metadata_json["metadata"]["required_test"]
  1550. assert instance_json["metadata"]["uri_test"] == valid_metadata_json["metadata"]["uri_test"]
  1551. assert instance_json["metadata"]["number_range_test"] == valid_metadata_json["metadata"]["number_range_test"]
  1552. # verify contents of overwritten metadata
  1553. installed_metadata_filepath2 = os.path.join(
  1554. mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE, metadata_filename2
  1555. )
  1556. assert os.path.isfile(installed_metadata_filepath2)
  1557. with open(installed_metadata_filepath2, "r") as metadata_file:
  1558. instance_json = json.load(metadata_file)
  1559. assert instance_json["schema_name"] == valid_display_name_json["schema_name"]
  1560. assert instance_json["display_name"] == valid_display_name_json["display_name"]
  1561. assert instance_json["metadata"]["required_test"] == valid_display_name_json["metadata"]["required_test"]
  1562. # verify contents of new imported metadata
  1563. installed_metadata_filepath3 = os.path.join(
  1564. mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE, metadata_filename3
  1565. )
  1566. assert os.path.isfile(installed_metadata_filepath3)
  1567. with open(installed_metadata_filepath3, "r") as metadata_file:
  1568. instance_json = json.load(metadata_file)
  1569. assert instance_json["schema_name"] == another_metadata_json["schema_name"]
  1570. assert instance_json["display_name"] == another_metadata_json["display_name"]
  1571. assert instance_json["metadata"]["required_test"] == another_metadata_json["metadata"]["required_test"]
  1572. assert instance_json["metadata"]["uri_test"] == another_metadata_json["metadata"]["uri_test"]
  1573. # Begin property tests...
  1574. def test_required(script_runner, mock_data_dir):
  1575. # Doesn't use PropertyTester due to its unique test since all other tests require this property
  1576. name = "required"
  1577. expected_file = os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE, name + ".json")
  1578. # Cleanup from any potential previous failures
  1579. if os.path.exists(expected_file):
  1580. os.remove(expected_file)
  1581. ret = script_runner.run(
  1582. "elyra-metadata",
  1583. "install",
  1584. METADATA_TEST_SCHEMASPACE,
  1585. "--schema_name=metadata-test",
  1586. "--name=" + name,
  1587. "--display_name=" + name,
  1588. )
  1589. assert ret.success is False
  1590. assert "'--required_test' is a required parameter" in ret.stdout
  1591. ret = script_runner.run(
  1592. "elyra-metadata",
  1593. "install",
  1594. METADATA_TEST_SCHEMASPACE,
  1595. "--schema_name=metadata-test",
  1596. "--name=" + name,
  1597. "--display_name=" + name,
  1598. "--required_test=required_value",
  1599. )
  1600. assert ret.success
  1601. assert "Metadata instance '" + name + "' for schema 'metadata-test' has been written" in ret.stdout
  1602. assert os.path.isdir(os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE))
  1603. assert os.path.isfile(expected_file)
  1604. with open(expected_file, "r") as fd:
  1605. instance_json = json.load(fd)
  1606. assert instance_json["schema_name"] == "metadata-test"
  1607. assert instance_json["display_name"] == name
  1608. assert instance_json["metadata"]["required_test"] == "required_value"
  1609. def test_number_default(script_runner, mock_data_dir):
  1610. # Doesn't use PropertyTester due to its unique test (no failure, needs --replace, etc.)
  1611. name = "number_default"
  1612. expected_file = os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE, name + ".json")
  1613. # Cleanup from any potential previous failures
  1614. if os.path.exists(expected_file):
  1615. os.remove(expected_file)
  1616. # No negative test here. First create w/o a value and ensure 42, then create with a value and ensure that value.
  1617. ret = script_runner.run(
  1618. "elyra-metadata",
  1619. "install",
  1620. METADATA_TEST_SCHEMASPACE,
  1621. "--schema_name=metadata-test",
  1622. "--name=" + name,
  1623. "--display_name=" + name,
  1624. "--required_test=required_value",
  1625. )
  1626. assert ret.success
  1627. assert "Metadata instance '" + name + "' for schema 'metadata-test' has been written" in ret.stdout
  1628. assert os.path.isdir(os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE))
  1629. assert os.path.isfile(expected_file)
  1630. with open(expected_file, "r") as fd:
  1631. instance_json = json.load(fd)
  1632. assert instance_json["schema_name"] == "metadata-test"
  1633. assert instance_json["display_name"] == name
  1634. assert instance_json["metadata"]["number_default_test"] == 42
  1635. # Note that we only include the properties that are changed, along with "identifiers" like name ans schema_name.
  1636. ret = script_runner.run(
  1637. "elyra-metadata",
  1638. "install",
  1639. METADATA_TEST_SCHEMASPACE,
  1640. "--schema_name=metadata-test",
  1641. "--name=" + name,
  1642. "--replace",
  1643. "--number_default_test=7.2",
  1644. )
  1645. assert ret.success
  1646. assert "Metadata instance '" + name + "' for schema 'metadata-test' has been written" in ret.stdout
  1647. assert os.path.isdir(os.path.join(mock_data_dir, "metadata", METADATA_TEST_SCHEMASPACE))
  1648. assert os.path.isfile(expected_file)
  1649. with open(expected_file, "r") as fd:
  1650. instance_json = json.load(fd)
  1651. assert instance_json["schema_name"] == "metadata-test"
  1652. assert instance_json["display_name"] == name
  1653. assert instance_json["metadata"]["number_default_test"] == 7.2
  1654. def test_uri(script_runner, mock_data_dir):
  1655. prop_test = PropertyTester("uri")
  1656. prop_test.negative_value = "//invalid-uri"
  1657. prop_test.negative_stdout = "'//invalid-uri' is not a 'uri'"
  1658. # this can be joined with previous if adding meta-properties
  1659. # "; title: URI Test, format: uri"
  1660. prop_test.negative_stderr = "'//invalid-uri' is not a 'uri'"
  1661. prop_test.positive_value = "http://localhost:31823/v1/models?version=2017-02-13"
  1662. prop_test.run(script_runner, mock_data_dir)
  1663. def test_integer_exclusivity(script_runner, mock_data_dir):
  1664. prop_test = PropertyTester("integer_exclusivity")
  1665. prop_test.negative_value = 3
  1666. prop_test.negative_stdout = "3 is less than or equal to the minimum of 3"
  1667. # this can be joined with previous if adding meta-properties
  1668. # "; title: Integer Exclusivity Test, exclusiveMinimum: 3, exclusiveMaximum: 10"
  1669. prop_test.negative_stderr = "3 is less than or equal to the minimum of 3"
  1670. prop_test.positive_value = 7
  1671. prop_test.run(script_runner, mock_data_dir)
  1672. def test_integer_multiple(script_runner, mock_data_dir):
  1673. prop_test = PropertyTester("integer_multiple")
  1674. prop_test.negative_value = 32
  1675. prop_test.negative_stdout = "32 is not a multiple of 6"
  1676. # this can be joined with previous if adding meta-properties
  1677. # "; title: Integer Multiple Test, multipleOf: 6"
  1678. prop_test.negative_stderr = "32 is not a multiple of 6"
  1679. prop_test.positive_value = 42
  1680. prop_test.run(script_runner, mock_data_dir)
  1681. def test_number_range(script_runner, mock_data_dir):
  1682. prop_test = PropertyTester("number_range")
  1683. prop_test.negative_value = 2.7
  1684. prop_test.negative_stdout = "2.7 is less than the minimum of 3"
  1685. # this can be joined with previous if adding meta-properties
  1686. # "; title: Number Range Test, minimum: 3, maximum: 10"
  1687. prop_test.negative_stderr = "2.7 is less than the minimum of 3"
  1688. prop_test.positive_value = 7.2
  1689. prop_test.run(script_runner, mock_data_dir)
  1690. def test_const(script_runner, mock_data_dir):
  1691. prop_test = PropertyTester("const")
  1692. prop_test.negative_value = 2.718
  1693. prop_test.negative_stdout = "3.14 was expected"
  1694. # this can be joined with previous if adding meta-properties
  1695. # " ; title: Const Test, const: 3.14"
  1696. prop_test.negative_stderr = "3.14 was expected"
  1697. prop_test.positive_value = 3.14
  1698. prop_test.run(script_runner, mock_data_dir)
  1699. def test_string_length(script_runner, mock_data_dir):
  1700. prop_test = PropertyTester("string_length")
  1701. prop_test.negative_value = "12345678901"
  1702. prop_test.negative_stdout = "'12345678901' is too long"
  1703. # this can be joined with previous if adding meta-properties
  1704. # "; title: String Length Test, minLength: 3, maxLength: 10"
  1705. prop_test.negative_stderr = "'12345678901' is too long"
  1706. prop_test.positive_value = "123456"
  1707. prop_test.run(script_runner, mock_data_dir)
  1708. def test_string_pattern(script_runner, mock_data_dir):
  1709. prop_test = PropertyTester("string_pattern") # Must start/end with alphanumeric, can include '-' and '.'
  1710. prop_test.negative_value = "-foo1"
  1711. prop_test.negative_stdout = "'-foo1' does not match '^[a-z0-9][a-z0-9-.]*[a-z0-9]$'"
  1712. # this can be joined with previous if adding meta-properties
  1713. # "; title: String Pattern Test, pattern: ^[a-z0-9][a-z0-9-.]*[a-z0-9]$"
  1714. prop_test.negative_stderr = "'-foo1' does not match '^[a-z0-9][a-z0-9-.]*[a-z0-9]$'"
  1715. prop_test.positive_value = "0foo-bar.com-01"
  1716. prop_test.run(script_runner, mock_data_dir)
  1717. def test_enum(script_runner, mock_data_dir):
  1718. prop_test = PropertyTester("enum")
  1719. prop_test.negative_value = "jupyter"
  1720. prop_test.negative_stdout = "'jupyter' is not one of ['elyra', 'rocks', 'added']"
  1721. # this can be joined with previous if adding meta-properties
  1722. # "; title: Enum Test, enum: ['elyra', 'rocks', 'added']"
  1723. prop_test.negative_stderr = "'jupyter' is not one of ['elyra', 'rocks', 'added']"
  1724. prop_test.positive_value = "added"
  1725. prop_test.run(script_runner, mock_data_dir)
  1726. def test_array(script_runner, mock_data_dir):
  1727. prop_test = PropertyTester("array")
  1728. prop_test.negative_value = [1, 2, 2]
  1729. prop_test.negative_stdout = "[1, 2, 2] has non-unique elements"
  1730. # this can be joined with previous if adding meta-properties
  1731. # "; title: Array Test, minItems: 3, maxItems: 10, uniqueItems: True"
  1732. prop_test.negative_stderr = "[1, 2, 2] has non-unique elements"
  1733. prop_test.positive_value = [1, 2, 3, 4, 5]
  1734. prop_test.run(script_runner, mock_data_dir)
  1735. def test_object(script_runner, mock_data_dir):
  1736. prop_test = PropertyTester("object")
  1737. prop_test.negative_value = {"prop1": 2, "prop2": 3}
  1738. prop_test.negative_stdout = "{'prop1': 2, 'prop2': 3} does not have enough properties"
  1739. # this can be joined with previous if adding meta-properties
  1740. # "; title: Object Test, minProperties: 3, maxProperties: 10"
  1741. prop_test.negative_stderr = "{'prop1': 2, 'prop2': 3} does not have enough properties"
  1742. prop_test.positive_value = {"prop1": 2, "prop2": 3, "prop3": 4, "prop4": 5}
  1743. prop_test.run(script_runner, mock_data_dir)
  1744. def test_boolean(script_runner, mock_data_dir):
  1745. prop_test = PropertyTester("boolean")
  1746. prop_test.negative_value = "bogus_boolean"
  1747. prop_test.negative_stdout = "'bogus_boolean' is not of type 'boolean'"
  1748. # this can be joined with previous if adding meta-properties
  1749. # "; title: Boolean Test"
  1750. prop_test.negative_stderr = "'bogus_boolean' is not of type 'boolean'"
  1751. prop_test.positive_value = True
  1752. prop_test.run(script_runner, mock_data_dir)
  1753. def test_null(script_runner, mock_data_dir):
  1754. prop_test = PropertyTester("null")
  1755. prop_test.negative_value = "bogus_null"
  1756. prop_test.negative_stdout = "'bogus_null' is not of type 'null'"
  1757. # this can be joined with previous if adding meta-properties
  1758. # "; title: Null Test"
  1759. prop_test.negative_stderr = "'bogus_null' is not of type 'null'"
  1760. prop_test.positive_value = None
  1761. prop_test.run(script_runner, mock_data_dir)