test_metadata.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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. from collections import OrderedDict
  17. import copy
  18. import json
  19. import os
  20. import shutil
  21. import time
  22. from jsonschema import ValidationError
  23. import pytest
  24. from elyra.metadata.error import MetadataExistsError
  25. from elyra.metadata.error import MetadataNotFoundError
  26. from elyra.metadata.error import SchemaNotFoundError
  27. from elyra.metadata.manager import MetadataManager
  28. from elyra.metadata.metadata import Metadata
  29. from elyra.metadata.schema import METADATA_TEST_SCHEMASPACE
  30. from elyra.metadata.schema import METADATA_TEST_SCHEMASPACE_ID
  31. from elyra.metadata.storage import FileMetadataCache
  32. from elyra.metadata.storage import FileMetadataStore
  33. from elyra.metadata.storage import MetadataStore
  34. from elyra.tests.metadata.test_utils import byo_metadata_json
  35. from elyra.tests.metadata.test_utils import create_instance
  36. from elyra.tests.metadata.test_utils import create_json_file
  37. from elyra.tests.metadata.test_utils import invalid_metadata_json
  38. from elyra.tests.metadata.test_utils import invalid_no_display_name_json
  39. from elyra.tests.metadata.test_utils import MockMetadataStore
  40. from elyra.tests.metadata.test_utils import valid_display_name_json
  41. from elyra.tests.metadata.test_utils import valid_metadata2_json
  42. from elyra.tests.metadata.test_utils import valid_metadata_json
  43. os.environ["METADATA_TESTING"] = "1" # Enable metadata-tests schemaspace
  44. # ########################## MetadataManager Tests ###########################
  45. def test_manager_add_invalid(tests_manager):
  46. with pytest.raises(ValueError):
  47. MetadataManager(schemaspace="invalid")
  48. # Attempt with non Metadata instance
  49. with pytest.raises(TypeError):
  50. tests_manager.create(valid_metadata_json)
  51. # and invalid parameters
  52. with pytest.raises(TypeError):
  53. tests_manager.create(None, invalid_no_display_name_json)
  54. with pytest.raises(ValueError):
  55. tests_manager.create("foo", None)
  56. def test_manager_add_no_name(tests_manager, schemaspace_location):
  57. metadata_name = "valid_metadata_instance"
  58. metadata = Metadata.from_dict(METADATA_TEST_SCHEMASPACE_ID, {**valid_metadata_json})
  59. instance = tests_manager.create(None, metadata)
  60. assert instance is not None
  61. assert instance.name == metadata_name
  62. assert instance.pre_property == instance.metadata.get("required_test")
  63. assert instance.post_property == instance.display_name
  64. # Ensure file was created using store_manager
  65. instance_list = tests_manager.metadata_store.fetch_instances(metadata_name)
  66. assert len(instance_list) == 1
  67. instance = Metadata.from_dict(METADATA_TEST_SCHEMASPACE, instance_list[0])
  68. metadata_location = _compose_instance_location(tests_manager.metadata_store, schemaspace_location, metadata_name)
  69. assert instance.resource == metadata_location
  70. assert instance.pre_property == instance.metadata.get("required_test")
  71. # This will be None because the hooks don't get called when fetched directly from the store
  72. assert instance.post_property is None
  73. # And finally, remove it.
  74. tests_manager.remove(metadata_name)
  75. # Verify removal using metadata_store
  76. with pytest.raises(MetadataNotFoundError):
  77. tests_manager.metadata_store.fetch_instances(metadata_name)
  78. def test_manager_add_short_name(tests_manager, schemaspace_location):
  79. # Found that single character names were failing validation
  80. metadata_name = "a"
  81. metadata = Metadata(**valid_metadata_json)
  82. instance = tests_manager.create(metadata_name, metadata)
  83. assert instance is not None
  84. assert instance.name == metadata_name
  85. # Ensure file was created using store_manager
  86. instance_list = tests_manager.metadata_store.fetch_instances(metadata_name)
  87. assert len(instance_list) == 1
  88. instance = Metadata.from_dict(METADATA_TEST_SCHEMASPACE_ID, instance_list[0])
  89. metadata_location = _compose_instance_location(tests_manager.metadata_store, schemaspace_location, metadata_name)
  90. assert instance.resource == metadata_location
  91. # And finally, remove it.
  92. tests_manager.remove(metadata_name)
  93. # Verify removal using metadata_store
  94. with pytest.raises(MetadataNotFoundError):
  95. tests_manager.metadata_store.fetch_instances(metadata_name)
  96. def test_manager_add_empty_display_name(tests_manager):
  97. # Found that empty display_name values were passing validation, so minLength=1 was added
  98. metadata_name = "empty_display_name"
  99. metadata = Metadata(**valid_metadata_json)
  100. metadata.display_name = ""
  101. with pytest.raises(ValidationError):
  102. tests_manager.create(metadata_name, metadata)
  103. # Ensure file was not created using storage manager
  104. with pytest.raises(MetadataNotFoundError):
  105. tests_manager.metadata_store.fetch_instances(metadata_name)
  106. def test_manager_add_display_name(tests_manager, schemaspace_location):
  107. metadata_display_name = '1 teste "rápido"'
  108. metadata_name = "a_1_teste_rpido"
  109. metadata = Metadata(**valid_display_name_json)
  110. instance = tests_manager.create(None, metadata)
  111. assert instance is not None
  112. assert instance.name == metadata_name
  113. assert instance.display_name == metadata_display_name
  114. # Ensure file was created using store_manager
  115. instance_list = tests_manager.metadata_store.fetch_instances(metadata_name)
  116. assert len(instance_list) == 1
  117. instance = Metadata.from_dict(METADATA_TEST_SCHEMASPACE, instance_list[0])
  118. metadata_location = _compose_instance_location(tests_manager.metadata_store, schemaspace_location, metadata_name)
  119. assert instance.resource == metadata_location
  120. assert instance.display_name == metadata_display_name
  121. # And finally, remove it.
  122. tests_manager.remove(metadata_name)
  123. # Verify removal using metadata_store
  124. with pytest.raises(MetadataNotFoundError):
  125. tests_manager.metadata_store.fetch_instances(metadata_name)
  126. @pytest.mark.parametrize(
  127. "complex_string, valid",
  128. [
  129. ("", False),
  130. (" ", False),
  131. (" starting-whitespace", False),
  132. ("ending-whitespace ", False),
  133. (" whitespace-both-ends ", False),
  134. ("whitespace in between", True),
  135. ("no-whitespace", True),
  136. ],
  137. )
  138. def test_manager_complex_string_schema(tests_manager, schemaspace_location, complex_string, valid):
  139. metadata_name = "valid_metadata_instance"
  140. metadata_dict = {**valid_metadata_json}
  141. metadata_dict["metadata"]["string_complex_test"] = complex_string
  142. metadata = Metadata.from_dict(METADATA_TEST_SCHEMASPACE_ID, metadata_dict)
  143. if not valid:
  144. with pytest.raises(ValidationError):
  145. tests_manager.create(metadata_name, metadata)
  146. else:
  147. instance = tests_manager.create(metadata_name, metadata)
  148. assert instance.metadata.get("string_complex_test") == complex_string
  149. # And finally, remove it.
  150. tests_manager.remove(metadata_name)
  151. # Verify removal using metadata_store
  152. with pytest.raises(MetadataNotFoundError):
  153. tests_manager.metadata_store.fetch_instances(metadata_name)
  154. def test_manager_get_include_invalid(tests_manager):
  155. metadata_list = tests_manager.get_all(include_invalid=False)
  156. assert len(metadata_list) == 2
  157. metadata_list = tests_manager.get_all(include_invalid=True)
  158. assert len(metadata_list) == 5
  159. def test_manager_get_of_schema(tests_manager):
  160. metadata_list = tests_manager.get_all(include_invalid=True)
  161. assert len(metadata_list) == 5
  162. metadata_list = tests_manager.get_all(include_invalid=True, of_schema="metadata-test")
  163. assert len(metadata_list) == 3 # does not include metadata with schema {unknown} and metadata-testxxx
  164. def test_manager_get_bad_json(tests_manager):
  165. with pytest.raises(ValueError) as ve:
  166. tests_manager.get("bad")
  167. assert "JSON failed to load for instance 'bad'" in str(ve.value)
  168. def test_manager_get_all(tests_manager):
  169. metadata_list = tests_manager.get_all()
  170. assert len(metadata_list) == 2
  171. # Ensure name is getting derived from resource and not from contents
  172. for metadata in metadata_list:
  173. if metadata.display_name == "Another Metadata Instance (2)":
  174. assert metadata.name == "another"
  175. else:
  176. assert metadata.name == "valid"
  177. def test_manager_get_none(tests_manager, schemaspace_location):
  178. # Attempt to get a metadata instance using `None` (error expected)
  179. with pytest.raises(ValueError, match="The 'name' parameter requires a value."):
  180. tests_manager.get(name=None)
  181. def test_manager_get_all_none(tests_manager, schemaspace_location):
  182. # Delete the schemaspace contents and attempt listing metadata
  183. _remove_schemaspace(tests_manager.metadata_store, schemaspace_location)
  184. assert tests_manager.schemaspace_exists() is False
  185. _create_schemaspace(tests_manager.metadata_store, schemaspace_location)
  186. assert tests_manager.schemaspace_exists()
  187. metadata_list = tests_manager.get_all()
  188. assert len(metadata_list) == 0
  189. def test_manager_add_remove_valid(tests_manager, schemaspace_location):
  190. metadata_name = "valid_add_remove"
  191. # Remove schemaspace_location and ensure it gets created
  192. _remove_schemaspace(tests_manager.metadata_store, schemaspace_location)
  193. metadata = Metadata(**valid_metadata_json)
  194. instance = tests_manager.create(metadata_name, metadata)
  195. assert instance is not None
  196. # Attempt to create again w/o replace, then replace it.
  197. with pytest.raises(MetadataExistsError):
  198. tests_manager.create(metadata_name, metadata)
  199. instance = tests_manager.update(metadata_name, metadata)
  200. assert instance is not None
  201. # And finally, remove it.
  202. tests_manager.remove(metadata_name)
  203. # Verify removal using metadata_store
  204. with pytest.raises(MetadataNotFoundError):
  205. tests_manager.metadata_store.fetch_instances(metadata_name)
  206. def test_manager_remove_invalid(tests_manager, schemaspace_location):
  207. # Ensure invalid metadata file isn't validated and is removed.
  208. create_instance(tests_manager.metadata_store, schemaspace_location, "remove_invalid", invalid_metadata_json)
  209. metadata_name = "remove_invalid"
  210. tests_manager.remove(metadata_name)
  211. # Verify removal using metadata_store
  212. with pytest.raises(MetadataNotFoundError):
  213. tests_manager.metadata_store.fetch_instances(metadata_name)
  214. def test_manager_remove_missing(tests_manager):
  215. # Ensure removal of missing metadata file is handled.
  216. metadata_name = "missing"
  217. with pytest.raises(MetadataNotFoundError):
  218. tests_manager.remove(metadata_name)
  219. def test_manager_read_valid_by_name(tests_manager, schemaspace_location):
  220. metadata_name = "valid"
  221. some_metadata = tests_manager.get(metadata_name)
  222. assert some_metadata.name == metadata_name
  223. assert some_metadata.schema_name == "metadata-test"
  224. metadata_location = _compose_instance_location(tests_manager.metadata_store, schemaspace_location, metadata_name)
  225. assert metadata_location == some_metadata.resource
  226. def test_manager_read_invalid_by_name(tests_manager):
  227. metadata_name = "invalid"
  228. with pytest.raises(ValidationError):
  229. tests_manager.get(metadata_name)
  230. def test_manager_read_missing_by_name(tests_manager):
  231. metadata_name = "missing"
  232. with pytest.raises(MetadataNotFoundError):
  233. tests_manager.get(metadata_name)
  234. def test_manager_rollback_create(tests_manager):
  235. metadata_name = "rollback_create"
  236. metadata = Metadata(**valid_metadata2_json)
  237. os.environ["METADATA_TEST_HOOK_OP"] = "create" # Tell test class which op to raise
  238. # Create post-save hook will throw NotImplementedError
  239. with pytest.raises(NotImplementedError):
  240. tests_manager.create(metadata_name, metadata)
  241. # Ensure nothing got created
  242. with pytest.raises(MetadataNotFoundError):
  243. tests_manager.get(metadata_name)
  244. os.environ.pop("METADATA_TEST_HOOK_OP") # Restore normal operation
  245. instance = tests_manager.create(metadata_name, metadata)
  246. instance2 = tests_manager.get(metadata_name)
  247. assert instance.name == instance2.name
  248. assert instance.schema_name == instance2.schema_name
  249. assert instance.post_property == instance2.post_property
  250. def test_manager_rollback_update(tests_manager):
  251. metadata_name = "rollback_update"
  252. metadata = Metadata(**valid_metadata2_json)
  253. # Create the instance
  254. instance = tests_manager.create(metadata_name, metadata)
  255. original_display_name = instance.display_name
  256. instance.display_name = "Updated_" + original_display_name
  257. os.environ["METADATA_TEST_HOOK_OP"] = "update" # Tell test class which op to raise
  258. # Update post-save hook will throw ModuleNotFoundError
  259. with pytest.raises(ModuleNotFoundError):
  260. tests_manager.update(metadata_name, instance)
  261. # Ensure the display_name is still the original value.
  262. instance2 = tests_manager.get(metadata_name)
  263. assert instance2.display_name == original_display_name
  264. os.environ.pop("METADATA_TEST_HOOK_OP") # Restore normal operation
  265. # Ensure we can still update
  266. instance = tests_manager.update(metadata_name, instance)
  267. assert instance.display_name == "Updated_" + original_display_name
  268. def test_manager_rollback_delete(tests_manager):
  269. metadata_name = "rollback_delete"
  270. metadata = Metadata(**valid_metadata2_json)
  271. # Create the instance
  272. instance = tests_manager.create(metadata_name, metadata)
  273. os.environ["METADATA_TEST_HOOK_OP"] = "delete" # Tell test class which op to raise
  274. # Delete post-save hook will throw FileNotFoundError
  275. with pytest.raises(FileNotFoundError):
  276. tests_manager.remove(metadata_name)
  277. # Ensure the instance still exists
  278. instance2 = tests_manager.get(metadata_name)
  279. assert instance2.display_name == instance.display_name
  280. os.environ.pop("METADATA_TEST_HOOK_OP") # Restore normal operation
  281. # Ensure we can still delete
  282. tests_manager.remove(metadata_name)
  283. # Ensure the instance was deleted
  284. with pytest.raises(MetadataNotFoundError):
  285. tests_manager.get(metadata_name)
  286. def test_manager_hierarchy_fetch(tests_hierarchy_manager, factory_location, shared_location, schemaspace_location):
  287. # fetch initial instances, only factory data should be present
  288. metadata_list = tests_hierarchy_manager.get_all()
  289. assert len(metadata_list) == 3
  290. # Ensure these are all factory instances
  291. for metadata in metadata_list:
  292. assert metadata.display_name == "factory"
  293. byo_3 = tests_hierarchy_manager.get("byo_3")
  294. assert byo_3.resource.startswith(str(factory_location))
  295. # add a shared instance and confirm list count is still the same, but
  296. # only that instance is present in shared directory...
  297. byo_instance = byo_metadata_json
  298. byo_instance["display_name"] = "shared"
  299. create_json_file(shared_location, "byo_3.json", byo_instance)
  300. metadata_list = tests_hierarchy_manager.get_all()
  301. assert len(metadata_list) == 3
  302. # Ensure the proper instances exist
  303. for metadata in metadata_list:
  304. if metadata.name == "byo_3":
  305. assert metadata.display_name == "shared"
  306. else:
  307. assert metadata.display_name == "factory"
  308. byo_3 = tests_hierarchy_manager.get("byo_3")
  309. assert byo_3.resource.startswith(str(shared_location))
  310. # add a shared and a user instance confirm list count is still the same, but
  311. # both the user and shared instances are correct.
  312. byo_instance = byo_metadata_json
  313. byo_instance["display_name"] = "shared"
  314. create_json_file(shared_location, "byo_2.json", byo_instance)
  315. byo_instance["display_name"] = "user"
  316. create_json_file(schemaspace_location, "byo_2.json", byo_instance)
  317. metadata_list = tests_hierarchy_manager.get_all()
  318. assert len(metadata_list) == 3
  319. # Ensure the proper instances exist
  320. for metadata in metadata_list:
  321. if metadata.name == "byo_1":
  322. assert metadata.display_name == "factory"
  323. if metadata.name == "byo_2":
  324. assert metadata.display_name == "user"
  325. if metadata.name == "byo_3":
  326. assert metadata.display_name == "shared"
  327. byo_2 = tests_hierarchy_manager.get("byo_2")
  328. assert byo_2.resource.startswith(str(schemaspace_location))
  329. # delete the user instance and ensure its shared copy is now exposed
  330. tests_hierarchy_manager.metadata_store.delete_instance(byo_2.to_dict())
  331. metadata_list = tests_hierarchy_manager.get_all()
  332. assert len(metadata_list) == 3
  333. # Ensure the proper instances exist
  334. for metadata in metadata_list:
  335. if metadata.name == "byo_1":
  336. assert metadata.display_name == "factory"
  337. if metadata.name == "byo_2":
  338. assert metadata.display_name == "shared"
  339. if metadata.name == "byo_3":
  340. assert metadata.display_name == "shared"
  341. byo_2 = tests_hierarchy_manager.get("byo_2")
  342. assert byo_2.resource.startswith(str(shared_location))
  343. # delete both shared copies and ensure only factory is left
  344. # Note: because we can only delete user instances via the APIs, this
  345. # code is metadata_store-sensitive. If other stores implement this
  346. # hierachy scheme, similar storage-specific code will be necessary.
  347. if isinstance(tests_hierarchy_manager.metadata_store, FileMetadataStore):
  348. os.remove(os.path.join(shared_location, "byo_2.json"))
  349. os.remove(os.path.join(shared_location, "byo_3.json"))
  350. # fetch initial instances, only factory data should be present
  351. metadata_list = tests_hierarchy_manager.get_all()
  352. assert len(metadata_list) == 3
  353. # Ensure these are all factory instances
  354. for metadata in metadata_list:
  355. assert metadata.display_name == "factory"
  356. byo_2 = tests_hierarchy_manager.get("byo_2")
  357. assert byo_2.resource.startswith(str(factory_location))
  358. def test_manager_hierarchy_create(tests_hierarchy_manager, schemaspace_location):
  359. # Note, this is really more of an update test (replace = True), since you cannot "create" an
  360. # instance if it already exists - which, in this case, it exists in the factory area
  361. metadata = Metadata(**byo_metadata_json)
  362. metadata.display_name = "user"
  363. with pytest.raises(MetadataExistsError):
  364. tests_hierarchy_manager.create("byo_2", metadata)
  365. instance = tests_hierarchy_manager.update("byo_2", metadata)
  366. assert instance is not None
  367. assert instance.resource.startswith(str(schemaspace_location))
  368. metadata_list = tests_hierarchy_manager.get_all()
  369. assert len(metadata_list) == 3
  370. # Ensure the proper instances exist
  371. for metadata in metadata_list:
  372. if metadata.name == "byo_1":
  373. assert metadata.display_name == "factory"
  374. if metadata.name == "byo_2":
  375. assert metadata.display_name == "user"
  376. if metadata.name == "byo_3":
  377. assert metadata.display_name == "factory"
  378. byo_2 = tests_hierarchy_manager.get("byo_2")
  379. assert byo_2.resource.startswith(str(schemaspace_location))
  380. metadata = Metadata(**byo_metadata_json)
  381. metadata.display_name = "user"
  382. instance = tests_hierarchy_manager.update("byo_3", metadata)
  383. assert instance is not None
  384. assert instance.resource.startswith(str(schemaspace_location))
  385. metadata_list = tests_hierarchy_manager.get_all()
  386. assert len(metadata_list) == 3
  387. # Ensure the proper instances exist
  388. for metadata in metadata_list:
  389. if metadata.name == "byo_1":
  390. assert metadata.display_name == "factory"
  391. if metadata.name == "byo_2":
  392. assert metadata.display_name == "user"
  393. if metadata.name == "byo_3":
  394. assert metadata.display_name == "user"
  395. byo_2 = tests_hierarchy_manager.get("byo_2")
  396. assert byo_2.resource.startswith(str(schemaspace_location))
  397. def test_manager_hierarchy_update(tests_hierarchy_manager, factory_location, shared_location, schemaspace_location):
  398. # Create a copy of existing factory instance and ensure its in the user area
  399. byo_2 = tests_hierarchy_manager.get("byo_2")
  400. assert byo_2.resource.startswith(str(factory_location))
  401. byo_2.display_name = "user"
  402. with pytest.raises(MetadataExistsError):
  403. tests_hierarchy_manager.create("byo_2", byo_2)
  404. # Repeat with replacement enabled
  405. instance = tests_hierarchy_manager.update("byo_2", byo_2)
  406. assert instance is not None
  407. assert instance.resource.startswith(str(schemaspace_location))
  408. # now "slip in" a shared instance behind the updated version and ensure
  409. # the updated version is what's returned.
  410. byo_instance = byo_metadata_json
  411. byo_instance["display_name"] = "shared"
  412. create_json_file(shared_location, "byo_2.json", byo_instance)
  413. byo_2 = tests_hierarchy_manager.get("byo_2")
  414. assert byo_2.resource.startswith(str(schemaspace_location))
  415. # now remove the updated instance and ensure the shared instance appears
  416. tests_hierarchy_manager.remove("byo_2")
  417. byo_2 = tests_hierarchy_manager.get("byo_2")
  418. assert byo_2.resource.startswith(str(shared_location))
  419. def test_manager_update(tests_hierarchy_manager, schemaspace_location):
  420. # Create some metadata, then attempt to update it with a known schema violation
  421. # and ensure the previous copy still exists...
  422. # Create a user instance...
  423. metadata = Metadata.from_dict(METADATA_TEST_SCHEMASPACE_ID, {**byo_metadata_json})
  424. metadata.display_name = "user1"
  425. instance = tests_hierarchy_manager.create("update", metadata)
  426. assert instance is not None
  427. assert instance.resource.startswith(str(schemaspace_location))
  428. assert instance.pre_property == instance.metadata["required_test"]
  429. assert instance.post_property == instance.display_name
  430. # Now update the user instance - add a field - and ensure that the original renamed file is not present.
  431. instance2 = tests_hierarchy_manager.get("update")
  432. instance2.display_name = "user2"
  433. instance2.metadata["number_range_test"] = 7
  434. instance = tests_hierarchy_manager.update("update", instance2)
  435. assert instance.pre_property == instance.metadata["required_test"]
  436. assert instance.post_property == instance2.display_name
  437. _ensure_single_instance(tests_hierarchy_manager, schemaspace_location, "update.json")
  438. instance2 = tests_hierarchy_manager.get("update")
  439. assert instance2.display_name == "user2"
  440. assert instance2.metadata["number_range_test"] == 7
  441. def test_manager_default_value(tests_hierarchy_manager, schemaspace_location):
  442. # Create some metadata, then attempt to update it with a known schema violation
  443. # and ensure the previous copy still exists...
  444. # Create a user instance...
  445. metadata = Metadata.from_dict(METADATA_TEST_SCHEMASPACE, {**byo_metadata_json})
  446. metadata.display_name = "user1"
  447. instance = tests_hierarchy_manager.create("default_value", metadata)
  448. assert instance.metadata["number_default_test"] == 42 # Ensure default value was applied when not present
  449. instance2 = tests_hierarchy_manager.get("default_value")
  450. instance2.metadata["number_default_test"] = 37
  451. tests_hierarchy_manager.update("default_value", instance2)
  452. instance3 = tests_hierarchy_manager.get("default_value")
  453. assert instance3.metadata["number_default_test"] == 37
  454. # Now remove the updated value and ensure it comes back with the default
  455. instance3.metadata.pop("number_default_test")
  456. assert "number_default_test" not in instance3.metadata
  457. tests_hierarchy_manager.update("default_value", instance3)
  458. instance4 = tests_hierarchy_manager.get("default_value")
  459. assert instance4.metadata["number_default_test"] == 42
  460. def test_manager_bad_update(tests_hierarchy_manager, schemaspace_location):
  461. # Create some metadata, then attempt to update it with a known schema violation
  462. # and ensure the previous copy still exists...
  463. # Create a user instance...
  464. metadata = Metadata(**byo_metadata_json)
  465. metadata.display_name = "user1"
  466. instance = tests_hierarchy_manager.create("bad_update", metadata)
  467. assert instance is not None
  468. assert instance.resource.startswith(str(schemaspace_location))
  469. # Now, attempt to update the user instance, but include a schema violation.
  470. # Verify the update failed, but also ensure the previous instance is still there.
  471. instance2 = tests_hierarchy_manager.get("bad_update")
  472. instance2.display_name = "user2"
  473. instance2.metadata["number_range_test"] = 42 # number is out of range
  474. with pytest.raises(ValidationError):
  475. tests_hierarchy_manager.update("bad_update", instance2)
  476. _ensure_single_instance(tests_hierarchy_manager, schemaspace_location, "bad_update.json")
  477. instance2 = tests_hierarchy_manager.get("bad_update")
  478. assert instance2.display_name == instance.display_name
  479. assert "number_range_test" not in instance2.metadata
  480. # Now try update without providing a name, ValueError expected
  481. instance2 = tests_hierarchy_manager.get("bad_update")
  482. instance2.display_name = "user update with no name"
  483. with pytest.raises(ValueError):
  484. tests_hierarchy_manager.update(None, instance2)
  485. _ensure_single_instance(tests_hierarchy_manager, schemaspace_location, "bad_update.json")
  486. def test_manager_hierarchy_remove(tests_hierarchy_manager, factory_location, shared_location, schemaspace_location):
  487. # Create additional instances in shared and user areas
  488. byo_2 = byo_metadata_json
  489. byo_2["display_name"] = "shared"
  490. create_json_file(shared_location, "byo_2.json", byo_2)
  491. metadata = Metadata(**byo_metadata_json)
  492. metadata.display_name = "user"
  493. instance = tests_hierarchy_manager.update("byo_2", metadata)
  494. assert instance is not None
  495. assert instance.resource.startswith(str(schemaspace_location))
  496. # Confirm on in user is found...
  497. metadata_list = tests_hierarchy_manager.get_all()
  498. assert len(metadata_list) == 3
  499. # Ensure the proper instances exist
  500. for metadata in metadata_list:
  501. if metadata.name == "byo_1":
  502. assert metadata.display_name == "factory"
  503. if metadata.name == "byo_2":
  504. assert metadata.display_name == "user"
  505. if metadata.name == "byo_3":
  506. assert metadata.display_name == "factory"
  507. byo_2 = tests_hierarchy_manager.get("byo_2")
  508. assert byo_2.resource.startswith(str(schemaspace_location))
  509. # Now remove instance. Should be allowed since it resides in user area
  510. tests_hierarchy_manager.remove("byo_2")
  511. _ensure_single_instance(tests_hierarchy_manager, schemaspace_location, "byo_2.json", expected_count=0)
  512. # Attempt to remove instance from shared area and its protected
  513. with pytest.raises(PermissionError) as pe:
  514. tests_hierarchy_manager.remove("byo_2")
  515. assert "Removal of instance 'byo_2'" in str(pe.value)
  516. # Ensure the one that exists is the one in the shared area
  517. byo_2 = tests_hierarchy_manager.get("byo_2")
  518. assert byo_2.resource.startswith(str(shared_location))
  519. # Attempt to remove instance from factory area and its protected as well
  520. with pytest.raises(PermissionError) as pe:
  521. tests_hierarchy_manager.remove("byo_1")
  522. assert "Removal of instance 'byo_1'" in str(pe.value)
  523. byo_1 = tests_hierarchy_manager.get("byo_1")
  524. assert byo_1.resource.startswith(str(factory_location))
  525. # ########################## MetadataStore Tests ###########################
  526. def test_store_schemaspace(store_manager, schemaspace_location):
  527. # Delete the metadata dir contents and attempt listing metadata
  528. _remove_schemaspace(store_manager, schemaspace_location)
  529. assert store_manager.schemaspace_exists() is False
  530. # create some metadata
  531. store_manager.store_instance("ensure_schemaspace_exists", Metadata(**valid_metadata_json).prepare_write())
  532. assert store_manager.schemaspace_exists()
  533. def test_store_fetch_instances(store_manager):
  534. instances_list = store_manager.fetch_instances()
  535. assert len(instances_list) == 4
  536. def test_store_fetch_no_schemaspace(store_manager, schemaspace_location):
  537. # Delete the schemaspace contents and attempt listing metadata
  538. _remove_schemaspace(store_manager, schemaspace_location)
  539. instance_list = store_manager.fetch_instances()
  540. assert len(instance_list) == 0
  541. def test_store_fetch_by_name(store_manager):
  542. metadata_name = "valid"
  543. instance_list = store_manager.fetch_instances(name=metadata_name)
  544. assert instance_list[0].get("name") == metadata_name
  545. def test_store_fetch_missing(store_manager):
  546. metadata_name = "missing"
  547. with pytest.raises(MetadataNotFoundError):
  548. store_manager.fetch_instances(name=metadata_name)
  549. def test_store_store_instance(store_manager, schemaspace_location):
  550. # Remove schemaspace to test raw creation and confirm perms
  551. _remove_schemaspace(store_manager, schemaspace_location)
  552. metadata_name = "persist"
  553. metadata = Metadata(**valid_metadata_json)
  554. metadata_dict = metadata.prepare_write()
  555. instance = store_manager.store_instance(metadata_name, metadata_dict)
  556. assert instance is not None
  557. if isinstance(store_manager, FileMetadataStore):
  558. dir_mode = oct(os.stat(schemaspace_location).st_mode & 0o777777) # Be sure to include other attributes
  559. assert dir_mode == "0o40700" # and ensure this is a directory with only rwx by owner enabled
  560. # Ensure file was created
  561. metadata_file = os.path.join(schemaspace_location, "persist.json")
  562. assert os.path.exists(metadata_file)
  563. file_mode = oct(os.stat(metadata_file).st_mode & 0o777777) # Be sure to include other attributes
  564. assert file_mode == "0o100600" # and ensure this is a regular file with only rw by owner enabled
  565. with open(metadata_file, "r", encoding="utf-8") as f:
  566. valid_add = json.loads(f.read())
  567. assert "resource" not in valid_add
  568. assert "name" not in valid_add
  569. assert "display_name" in valid_add
  570. assert valid_add["display_name"] == "valid metadata instance"
  571. assert "schema_name" in valid_add
  572. assert valid_add["schema_name"] == "metadata-test"
  573. # Attempt to create again w/o replace, then replace it.
  574. with pytest.raises(MetadataExistsError):
  575. store_manager.store_instance(metadata_name, metadata.prepare_write())
  576. metadata.metadata["number_range_test"] = 10
  577. instance = store_manager.store_instance(metadata_name, metadata.prepare_write(), for_update=True)
  578. assert instance is not None
  579. assert instance.get("metadata")["number_range_test"] == 10
  580. def test_store_delete_instance(store_manager, schemaspace_location):
  581. metadata_name = "valid"
  582. instance_list = store_manager.fetch_instances(name=metadata_name)
  583. metadata = instance_list[0]
  584. store_manager.delete_instance(metadata)
  585. with pytest.raises(MetadataNotFoundError):
  586. store_manager.fetch_instances(name=metadata_name)
  587. if isinstance(store_manager, FileMetadataStore):
  588. # Ensure file was physically deleted
  589. metadata_file = os.path.join(schemaspace_location, "valid.json")
  590. assert not os.path.exists(metadata_file)
  591. # ########################## Error Tests ###########################
  592. def test_error_metadata_not_found():
  593. schemaspace = METADATA_TEST_SCHEMASPACE
  594. resource = "missing_metadata"
  595. try:
  596. raise MetadataNotFoundError(schemaspace, resource)
  597. except MetadataNotFoundError as mnfe:
  598. assert str(mnfe) == f"No such instance named '{resource}' was found in the {schemaspace} schemaspace."
  599. def test_error_metadata_exists():
  600. schemaspace = METADATA_TEST_SCHEMASPACE
  601. resource = "existing_metadata"
  602. try:
  603. raise MetadataExistsError(schemaspace, resource)
  604. except MetadataExistsError as mee:
  605. assert str(mee) == f"An instance named '{resource}' already exists in the {schemaspace} schemaspace."
  606. def test_error_schema_not_found():
  607. schemaspace = METADATA_TEST_SCHEMASPACE
  608. resource = "missing_schema"
  609. try:
  610. raise SchemaNotFoundError(schemaspace, resource)
  611. except SchemaNotFoundError as snfe:
  612. assert str(snfe) == f"No such schema named '{resource}' was found in the {schemaspace} schemaspace."
  613. def test_cache_init():
  614. FileMetadataCache.clear_instance()
  615. cache = FileMetadataCache.instance()
  616. assert cache.max_size == 128
  617. FileMetadataCache.clear_instance()
  618. cache = FileMetadataCache.instance(max_size=3)
  619. assert cache.max_size == 3
  620. FileMetadataCache.clear_instance()
  621. def test_cache_ops(tests_manager, schemaspace_location):
  622. FileMetadataCache.clear_instance()
  623. test_items = OrderedDict({"a": 3, "b": 4, "c": 5, "d": 6, "e": 7})
  624. test_resources = {}
  625. test_content = {}
  626. # Setup test data
  627. for name, number in test_items.items():
  628. content = copy.deepcopy(valid_metadata_json)
  629. content["display_name"] = name
  630. content["metadata"]["number_range_test"] = number
  631. resource = create_instance(tests_manager.metadata_store, schemaspace_location, name, content)
  632. test_resources[name] = resource
  633. test_content[name] = content
  634. # Add initial entries
  635. cache = FileMetadataCache.instance(max_size=3)
  636. for name in test_items: # Add the items to the cache
  637. cache.add_item(test_resources[name], test_content[name])
  638. assert len(cache) == 3
  639. assert cache.trims == 2
  640. assert cache.get_item(test_resources.get("a")) is None
  641. assert cache.get_item(test_resources.get("b")) is None
  642. assert cache.get_item(test_resources.get("c")) is not None
  643. assert cache.get_item(test_resources.get("d")) is not None
  644. assert cache.get_item(test_resources.get("e")) is not None
  645. assert cache.misses == 2
  646. assert cache.hits == 3
  647. cache.add_item(test_resources.get("a"), test_content.get("a"))
  648. assert len(cache) == 3
  649. assert cache.trims == 3
  650. assert cache.get_item(test_resources.get("c")) is None # since 'c' was aged out
  651. assert cache.get_item(test_resources.get("a")) is not None
  652. assert cache.misses == 3
  653. assert cache.hits == 4
  654. e_val = cache.remove_item(test_resources.get("e"))
  655. assert len(cache) == 2
  656. assert e_val["metadata"]["number_range_test"] == test_items.get("e")
  657. assert cache.get_item(test_resources.get("e")) is None
  658. assert cache.misses == 4
  659. assert cache.hits == 4
  660. assert cache.trims == 3
  661. a_val = cache.remove_item(test_resources.get("a"))
  662. assert len(cache) == 1
  663. assert a_val["metadata"]["number_range_test"] == test_items.get("a")
  664. assert cache.get_item(test_resources.get("a")) is None
  665. assert cache.misses == 5
  666. assert cache.hits == 4
  667. assert cache.trims == 3
  668. d_val = cache.get_item(test_resources.get("d"))
  669. assert len(cache) == 1
  670. assert d_val["metadata"]["number_range_test"] == test_items.get("d")
  671. assert cache.misses == 5
  672. assert cache.hits == 5
  673. assert cache.trims == 3
  674. if isinstance(tests_manager.metadata_store, FileMetadataStore):
  675. # Exercise delete from filesystem and ensure cached item is removed
  676. assert os.path.exists(test_resources.get("d"))
  677. os.remove(test_resources.get("d"))
  678. recorded = 0.0
  679. for i in range(1, 6): # allow up to a second for delete to record in cache
  680. time.sleep(0.2) # initial tests are showing only one sub-second delay is necessary
  681. recorded += 0.2
  682. if len(cache) == 0:
  683. break
  684. assert len(cache) == 0
  685. print(f"\ntest_cache_ops: Delete recorded after {recorded} seconds")
  686. assert cache.get_item(test_resources.get("d")) is None
  687. assert cache.misses == 6
  688. assert cache.hits == 5
  689. assert cache.trims == 3
  690. def test_cache_disabled(tests_manager, schemaspace_location):
  691. FileMetadataCache.clear_instance()
  692. test_items = OrderedDict({"a": 3, "b": 4, "c": 5, "d": 6, "e": 7})
  693. test_resources = {}
  694. test_content = {}
  695. # Setup test data
  696. for name, number in test_items.items():
  697. content = copy.deepcopy(valid_metadata_json)
  698. content["display_name"] = name
  699. content["metadata"]["number_range_test"] = number
  700. resource = create_instance(tests_manager.metadata_store, schemaspace_location, name, content)
  701. test_resources[name] = resource
  702. test_content[name] = content
  703. # Add initial entries
  704. cache = FileMetadataCache.instance(max_size=3, enabled=False)
  705. assert hasattr(cache, "observer") is False
  706. assert hasattr(cache, "observed_dirs") is False
  707. for name in test_items: # Add the items to the cache
  708. cache.add_item(test_resources[name], test_content[name])
  709. assert len(cache) == 0
  710. assert cache.trims == 0
  711. assert cache.get_item(test_resources.get("a")) is None
  712. assert cache.get_item(test_resources.get("b")) is None
  713. assert cache.get_item(test_resources.get("c")) is None
  714. assert cache.get_item(test_resources.get("d")) is None
  715. assert cache.get_item(test_resources.get("e")) is None
  716. assert cache.misses == 0
  717. assert cache.hits == 0
  718. def _ensure_single_instance(tests_hierarchy_manager, schemaspace_location, name, expected_count=1):
  719. """Because updates can trigger the copy of the original, this methods ensures that
  720. only the named instance (`name`) exists after the operation. The expected_count
  721. can be altered so that it can also be used to ensure clean removals.
  722. """
  723. if isinstance(tests_hierarchy_manager.metadata_store, FileMetadataStore):
  724. # Ensure only the actual metadata file exists. The renamed instance will start with 'name' but have
  725. # a timestamp appended to it.
  726. count = 0
  727. actual = 0
  728. for f in os.listdir(str(schemaspace_location)):
  729. if name in f:
  730. count = count + 1
  731. if name == f:
  732. actual = actual + 1
  733. assert count == expected_count, "Temporarily renamed file was not removed"
  734. assert actual == expected_count
  735. def _create_schemaspace(store_manager: MetadataStore, schemaspace_location: str):
  736. """Creates schemaspace in a storage-independent manner"""
  737. if isinstance(store_manager, FileMetadataStore):
  738. os.makedirs(schemaspace_location)
  739. elif isinstance(store_manager, MockMetadataStore):
  740. instances = store_manager.instances
  741. if instances is None:
  742. setattr(store_manager, "instances", dict())
  743. def _remove_schemaspace(store_manager: MetadataStore, schemaspace_location: str):
  744. """Removes schemaspace in a storage-independent manner"""
  745. if isinstance(store_manager, FileMetadataStore):
  746. shutil.rmtree(schemaspace_location)
  747. elif isinstance(store_manager, MockMetadataStore):
  748. setattr(store_manager, "instances", None)
  749. def _compose_instance_location(store_manager: MetadataStore, location: str, name: str) -> str:
  750. """Compose location of the named instance in a storage-independent manner"""
  751. if isinstance(store_manager, FileMetadataStore):
  752. location = os.path.join(location, f"{name}.json")
  753. elif isinstance(store_manager, MockMetadataStore):
  754. location = None
  755. return location