Ejemplo n.º 1
0
def test_manager_hierarchy_update(tests_hierarchy_manager, factory_location, shared_location, schemaspace_location):

    # Create a copy of existing factory instance and ensure its in the user area
    byo_2 = tests_hierarchy_manager.get("byo_2")
    assert byo_2.resource.startswith(str(factory_location))

    byo_2.display_name = "user"
    with pytest.raises(MetadataExistsError):
        tests_hierarchy_manager.create("byo_2", byo_2)

    # Repeat with replacement enabled
    instance = tests_hierarchy_manager.update("byo_2", byo_2)
    assert instance is not None
    assert instance.resource.startswith(str(schemaspace_location))

    # now "slip in" a shared instance behind the updated version and ensure
    # the updated version is what's returned.
    byo_instance = byo_metadata_json
    byo_instance["display_name"] = "shared"
    create_json_file(shared_location, "byo_2.json", byo_instance)

    byo_2 = tests_hierarchy_manager.get("byo_2")
    assert byo_2.resource.startswith(str(schemaspace_location))

    # now remove the updated instance and ensure the shared instance appears
    tests_hierarchy_manager.remove("byo_2")

    byo_2 = tests_hierarchy_manager.get("byo_2")
    assert byo_2.resource.startswith(str(shared_location))
Ejemplo n.º 2
0
async def test_update_fields(jp_fetch, schemaspace_location):

    # Create an instance, then update with a new field
    create_json_file(schemaspace_location, "update_fields.json",
                     valid_metadata_json)
    valid = copy.deepcopy(valid_metadata_json)
    valid["metadata"]["number_range_test"] = 7
    body = json.dumps(valid)

    # Update instance adding number_range_test
    r = await jp_fetch("elyra",
                       "metadata",
                       METADATA_TEST_SCHEMASPACE,
                       "update_fields",
                       body=body,
                       method="PUT")
    assert r.code == 200
    instance = json.loads(r.body.decode())
    assert instance["metadata"]["number_range_test"] == 7

    # Add a new field (per schema) and remove another -
    valid["metadata"].pop("number_range_test")
    valid["metadata"]["string_length_test"] = "valid len"
    body = json.dumps(valid)

    r = await jp_fetch("elyra",
                       "metadata",
                       METADATA_TEST_SCHEMASPACE,
                       "update_fields",
                       body=body,
                       method="PUT")
    assert r.code == 200
    instance = json.loads(r.body.decode())
    assert instance["metadata"]["string_length_test"] == "valid len"
    assert "number_range_test" not in instance["metadata"]
Ejemplo n.º 3
0
async def test_delete_instance(jp_fetch, schemaspace_location, setup_data):
    """Create a simple instance - not conflicting with factory instances and delete it."""

    # First, attempt to delete non-existent resource, exception expected.
    with pytest.raises(HTTPClientError) as e:
        await jp_fetch("elyra",
                       "metadata",
                       METADATA_TEST_SCHEMASPACE,
                       "missing",
                       method="DELETE")
    assert expected_http_error(e, 404)

    create_json_file(schemaspace_location, "valid.json", valid_metadata_json)

    r = await jp_fetch("elyra",
                       "metadata",
                       METADATA_TEST_SCHEMASPACE,
                       "valid",
                       method="DELETE")
    assert r.code == 204

    # Confirm deletion
    with pytest.raises(HTTPClientError) as e:
        await jp_fetch("elyra",
                       "metadata",
                       METADATA_TEST_SCHEMASPACE,
                       "valid",
                       method="DELETE")
    assert expected_http_error(e, 404)
Ejemplo n.º 4
0
async def test_invalid_update(jp_fetch, schemaspace_location):
    """Update a simple instance with invalid metadata."""

    # Create an instance, then update with invalid metadata
    create_json_file(schemaspace_location, "update_bad_md.json",
                     valid_metadata_json)

    # Fetch it to get the valid instance
    r = await jp_fetch("elyra", "metadata", METADATA_TEST_SCHEMASPACE,
                       "update_bad_md")
    assert r.code == 200
    instance = json.loads(r.body.decode())

    # Now attempt the update with bad metadata and ensure previous still exists
    valid2 = copy.deepcopy(valid_metadata_json)
    valid2["name"] = "valid"
    valid2["metadata"]["number_range_test"] = 42
    body2 = json.dumps(valid2)

    with pytest.raises(HTTPClientError) as e:
        await jp_fetch("elyra",
                       "metadata",
                       METADATA_TEST_SCHEMASPACE_ID,
                       "update_bad_md",
                       body=body2,
                       method="PUT")
    assert expected_http_error(e, 400)

    # Fetch again and ensure it matches the previous instance
    r = await jp_fetch("elyra", "metadata", METADATA_TEST_SCHEMASPACE_ID,
                       "update_bad_md")
    assert r.code == 200
    instance2 = json.loads(r.body.decode())
    assert instance2 == instance
Ejemplo n.º 5
0
async def test_update_instance(jp_fetch, schemaspace_location):
    """Update a simple instance."""

    # Create an instance, then update
    create_json_file(schemaspace_location, "valid.json", valid_metadata_json)
    valid = copy.deepcopy(valid_metadata_json)
    valid["name"] = "valid"
    valid["metadata"]["number_range_test"] = 7
    body = json.dumps(valid)

    # Update instance
    r = await jp_fetch("elyra",
                       "metadata",
                       METADATA_TEST_SCHEMASPACE_ID,
                       "valid",
                       body=body,
                       method="PUT")
    assert r.code == 200
    instance = json.loads(r.body.decode())
    assert instance["metadata"]["number_range_test"] == 7

    # Confirm update via jp_fetch
    r = await jp_fetch("elyra", "metadata", METADATA_TEST_SCHEMASPACE, "valid")
    assert r.code == 200
    instance = json.loads(r.body.decode())
    assert instance["metadata"]["number_range_test"] == 7
Ejemplo n.º 6
0
def test_manager_hierarchy_remove(tests_hierarchy_manager, factory_location,
                                  shared_location, schemaspace_location):

    # Create additional instances in shared and user areas
    byo_2 = byo_metadata_json
    byo_2["display_name"] = "shared"
    create_json_file(shared_location, "byo_2.json", byo_2)

    metadata = Metadata(**byo_metadata_json)
    metadata.display_name = "user"
    instance = tests_hierarchy_manager.update("byo_2", metadata)
    assert instance is not None
    assert instance.resource.startswith(str(schemaspace_location))

    # Confirm on in user is found...
    metadata_list = tests_hierarchy_manager.get_all()
    assert len(metadata_list) == 3
    # Ensure the proper instances exist
    for metadata in metadata_list:
        if metadata.name == "byo_1":
            assert metadata.display_name == "factory"
        if metadata.name == "byo_2":
            assert metadata.display_name == "user"
        if metadata.name == "byo_3":
            assert metadata.display_name == "factory"

    byo_2 = tests_hierarchy_manager.get("byo_2")
    assert byo_2.resource.startswith(str(schemaspace_location))

    # Now remove instance.  Should be allowed since it resides in user area
    tests_hierarchy_manager.remove("byo_2")
    _ensure_single_instance(tests_hierarchy_manager,
                            schemaspace_location,
                            "byo_2.json",
                            expected_count=0)

    # Attempt to remove instance from shared area and its protected
    with pytest.raises(PermissionError) as pe:
        tests_hierarchy_manager.remove("byo_2")
    assert "Removal of instance 'byo_2'" in str(pe.value)

    # Ensure the one that exists is the one in the shared area
    byo_2 = tests_hierarchy_manager.get("byo_2")
    assert byo_2.resource.startswith(str(shared_location))

    # Attempt to remove instance from factory area and its protected as well
    with pytest.raises(PermissionError) as pe:
        tests_hierarchy_manager.remove("byo_1")
    assert "Removal of instance 'byo_1'" in str(pe.value)

    byo_1 = tests_hierarchy_manager.get("byo_1")
    assert byo_1.resource.startswith(str(factory_location))
Ejemplo n.º 7
0
async def test_delete_hierarchy_instance(jp_fetch, schemaspace_location,
                                         setup_hierarchy):
    """Create a simple instance - that conflicts with factory instances and delete it only if local."""

    with pytest.raises(HTTPClientError) as e:
        await jp_fetch("elyra",
                       "metadata",
                       METADATA_TEST_SCHEMASPACE,
                       "byo_2",
                       method="DELETE")
    assert expected_http_error(e, 403)

    # create local instance, delete should succeed
    create_json_file(schemaspace_location, "byo_2.json", byo_metadata_json)

    r = await jp_fetch("elyra",
                       "metadata",
                       METADATA_TEST_SCHEMASPACE,
                       "byo_2",
                       method="DELETE")
    assert r.code == 204
Ejemplo n.º 8
0
def setup_hierarchy(jp_environ, factory_location):
    # Only populate factory info
    byo_instance = byo_metadata_json
    byo_instance["display_name"] = "factory"
    create_json_file(factory_location, "byo_1.json", byo_instance)
    create_json_file(factory_location, "byo_2.json", byo_instance)
    create_json_file(factory_location, "byo_3.json", byo_instance)
Ejemplo n.º 9
0
def test_manager_hierarchy_fetch(tests_hierarchy_manager, factory_location, shared_location, schemaspace_location):

    # fetch initial instances, only factory data should be present
    metadata_list = tests_hierarchy_manager.get_all()
    assert len(metadata_list) == 3
    # Ensure these are all factory instances
    for metadata in metadata_list:
        assert metadata.display_name == "factory"

    byo_3 = tests_hierarchy_manager.get("byo_3")
    assert byo_3.resource.startswith(str(factory_location))

    # add a shared instance and confirm list count is still the same, but
    # only that instance is present in shared directory...
    byo_instance = byo_metadata_json
    byo_instance["display_name"] = "shared"
    create_json_file(shared_location, "byo_3.json", byo_instance)

    metadata_list = tests_hierarchy_manager.get_all()
    assert len(metadata_list) == 3
    # Ensure the proper instances exist
    for metadata in metadata_list:
        if metadata.name == "byo_3":
            assert metadata.display_name == "shared"
        else:
            assert metadata.display_name == "factory"

    byo_3 = tests_hierarchy_manager.get("byo_3")
    assert byo_3.resource.startswith(str(shared_location))

    # add a shared and a user instance confirm list count is still the same, but
    # both the user and shared instances are correct.
    byo_instance = byo_metadata_json
    byo_instance["display_name"] = "shared"
    create_json_file(shared_location, "byo_2.json", byo_instance)
    byo_instance["display_name"] = "user"
    create_json_file(schemaspace_location, "byo_2.json", byo_instance)

    metadata_list = tests_hierarchy_manager.get_all()
    assert len(metadata_list) == 3
    # Ensure the proper instances exist
    for metadata in metadata_list:
        if metadata.name == "byo_1":
            assert metadata.display_name == "factory"
        if metadata.name == "byo_2":
            assert metadata.display_name == "user"
        if metadata.name == "byo_3":
            assert metadata.display_name == "shared"

    byo_2 = tests_hierarchy_manager.get("byo_2")
    assert byo_2.resource.startswith(str(schemaspace_location))

    # delete the user instance and ensure its shared copy is now exposed
    tests_hierarchy_manager.metadata_store.delete_instance(byo_2.to_dict())

    metadata_list = tests_hierarchy_manager.get_all()
    assert len(metadata_list) == 3
    # Ensure the proper instances exist
    for metadata in metadata_list:
        if metadata.name == "byo_1":
            assert metadata.display_name == "factory"
        if metadata.name == "byo_2":
            assert metadata.display_name == "shared"
        if metadata.name == "byo_3":
            assert metadata.display_name == "shared"

    byo_2 = tests_hierarchy_manager.get("byo_2")
    assert byo_2.resource.startswith(str(shared_location))

    # delete both shared copies and ensure only factory is left
    # Note: because we can only delete user instances via the APIs, this
    # code is metadata_store-sensitive.  If other stores implement this
    # hierachy scheme, similar storage-specific code will be necessary.
    if isinstance(tests_hierarchy_manager.metadata_store, FileMetadataStore):
        os.remove(os.path.join(shared_location, "byo_2.json"))
        os.remove(os.path.join(shared_location, "byo_3.json"))

    # fetch initial instances, only factory data should be present
    metadata_list = tests_hierarchy_manager.get_all()
    assert len(metadata_list) == 3
    # Ensure these are all factory instances
    for metadata in metadata_list:
        assert metadata.display_name == "factory"

    byo_2 = tests_hierarchy_manager.get("byo_2")
    assert byo_2.resource.startswith(str(factory_location))
Ejemplo n.º 10
0
def setup_data(schemaspace_location):
    create_json_file(schemaspace_location, "valid.json", valid_metadata_json)
    create_json_file(schemaspace_location, "another.json",
                     another_metadata_json)
    create_json_file(schemaspace_location, "invalid.json",
                     invalid_metadata_json)