-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add tests for cleanse #5499
Merged
Merged
Add tests for cleanse #5499
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
72b78b9
Add 2 basic tests for cleanse (admin only and admin pass with output)
pwalczysko 2b9f5a3
Merge remote-tracking branch 'origin/develop' into cleanse-test
pwalczysko d90d2f4
flake8 fix
pwalczysko e9e4a37
Replace double slashes, use omero.managed.dir to compare
pwalczysko e30ab4c
Compare the whole string, also the start
pwalczysko 4d69e5d
Add test for renaming the original file and subsequent run of cleanse
pwalczysko 22a5fa5
Delete the original file after renaming, then run cleanse and check
pwalczysko e401afe
Add checks for logfile, improve readability, clean code
pwalczysko 7aaa2b0
Add helper method make_path, define data_dir and mrepo_dir in setup
pwalczysko 9f79786
Add Restricted Admins test for cleanse
pwalczysko af4269d
Add comments to privileges parameter of new_user method
pwalczysko b0ca085
Make changes cf. Josh's comments
pwalczysko 08afcb5
Extend CLITest class to run the test as Full Admin, not root
pwalczysko a0dfa53
Update doc string to use () rather than []
joshmoore File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
components/tools/OmeroPy/test/integration/clitest/test_cleanse.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
# | ||
# Copyright (C) 2014-2017 University of Dundee & Open Microscopy Environment. | ||
# All rights reserved. | ||
# | ||
# This program is free software; you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation; either version 2 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License along | ||
# with this program; if not, write to the Free Software Foundation, Inc., | ||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
|
||
|
||
from test.integration.clitest.cli import CLITest | ||
from omero.cli import NonZeroReturnCode | ||
from omero.cmd import Delete2 | ||
|
||
import omero.plugins.admin | ||
import pytest | ||
import os.path | ||
|
||
|
||
class TestCleanse(CLITest): | ||
|
||
def setup_method(self, method): | ||
super(TestCleanse, self).setup_method(method) | ||
self.cli.register("admin", omero.plugins.admin.AdminControl, "TEST") | ||
self.args += ["admin", "cleanse"] | ||
|
||
def testCleanseAdminOnly(self, capsys): | ||
"""Test cleanse is admin-only""" | ||
config_service = self.root.sf.getConfigService() | ||
data_dir = config_service.getConfigValue("omero.data.dir") | ||
self.args += [data_dir] | ||
with pytest.raises(NonZeroReturnCode): | ||
self.cli.invoke(self.args, strict=True) | ||
out, err = capsys.readouterr() | ||
assert err.endswith("SecurityViolation: Admins only!\n") | ||
|
||
|
||
class TestCleanseFullAdmin(CLITest): | ||
|
||
# make the user in this test a member of system group | ||
DEFAULT_SYSTEM = True | ||
# make the new member of system group to a Full Admin | ||
DEFAULT_PRIVILEGES = None | ||
|
||
def setup_method(self, method): | ||
super(TestCleanseFullAdmin, self).setup_method(method) | ||
self.cli.register("admin", omero.plugins.admin.AdminControl, "TEST") | ||
self.args += ["admin", "cleanse"] | ||
self.group_ctx = {'omero.group': str(self.group.id.val)} | ||
config_service = self.root.sf.getConfigService() | ||
self.mrepo_dir = config_service.getConfigValue("omero.managed.dir") | ||
self.data_dir = config_service.getConfigValue("omero.managed.dir") | ||
|
||
def make_path(self, path_in_mrepo, filename=None): | ||
if filename is None: | ||
path = self.mrepo_dir + '/' + path_in_mrepo | ||
else: | ||
path = self.mrepo_dir + '/' + path_in_mrepo + '/' + filename | ||
return path.replace("//", "/") | ||
|
||
def testCleanseBasic(self, capsys): | ||
"""Test cleanse works for Full Admin with expected output""" | ||
self.args += [self.data_dir] | ||
self.cli.invoke(self.args, strict=True) | ||
out, err = capsys.readouterr() | ||
output_start = "Removing empty directories from...\n " | ||
output_string = output_start + self.mrepo_dir.replace("//", "/") | ||
assert output_string in out | ||
|
||
def testCleanseNonsenseName(self, capsys): | ||
""" | ||
Test cleanse removes file on disk after OriginalFile | ||
name was changed to nonsense and its Image was deleted | ||
""" | ||
# import image and retrieve the OriginalFile | ||
# (orig_file), its name and path | ||
image = self.import_fake_file()[0] | ||
fileset = self.get_fileset([image]) | ||
params = omero.sys.ParametersI() | ||
params.addId(fileset.getId()) | ||
q = ("select originalFile.path, originalFile.id " | ||
"from FilesetEntry where fileset.id = :id") | ||
queryService = self.root.sf.getQueryService() | ||
result = queryService.projection(q, params, self.group_ctx) | ||
path_in_mrepo = result[0][0].getValue() | ||
orig_file_path = self.make_path(path_in_mrepo) | ||
assert os.path.exists(orig_file_path) | ||
orig_file_id = result[0][1].getValue() | ||
orig_file = self.query.get("OriginalFile", orig_file_id) | ||
orig_file_name = orig_file.getName().getValue() | ||
orig_file_path_and_name = self.make_path(path_in_mrepo, orig_file_name) | ||
assert os.path.isfile(orig_file_path_and_name) | ||
|
||
# retrieve the logfile, its name and path | ||
q = ("select o from FilesetJobLink l " | ||
"join l.parent as fs join l.child as j " | ||
"join j.originalFileLinks l2 join l2.child as o " | ||
"where fs.id = :id and " | ||
"o.mimetype = 'application/omero-log-file'") | ||
logfile = queryService.findByQuery(q, params, self.group_ctx) | ||
logfile_name = logfile.getName().getValue() | ||
path_in_mrepo = logfile.getPath().getValue() | ||
logfile_path = self.make_path(path_in_mrepo) | ||
assert os.path.exists(logfile_path) | ||
logfile_path_and_name = self.make_path(path_in_mrepo, logfile_name) | ||
assert os.path.isfile(logfile_path_and_name) | ||
|
||
# change the names of original_file and logfile to nonsense | ||
name = "nonsensical" | ||
update_service = self.root.sf.getUpdateService() | ||
orig_file.setName(omero.rtypes.rstring(name)) | ||
update_service.saveAndReturnObject(orig_file, self.group_ctx) | ||
logfile.setName(omero.rtypes.rstring(name)) | ||
update_service.saveAndReturnObject(logfile, self.group_ctx) | ||
|
||
# run the cleanse command, which will not delete | ||
# the files on disk | ||
self.args += [self.data_dir] | ||
self.cli.invoke(self.args, strict=True) | ||
assert os.path.exists(orig_file_path) | ||
assert os.path.isfile(orig_file_path_and_name) | ||
assert os.path.exists(logfile_path) | ||
assert os.path.isfile(logfile_path_and_name) | ||
|
||
# delete the image, which will not delete | ||
# the files on disk because of the nonsensical name | ||
# of orig_file and logfile | ||
command = Delete2(targetObjects={"Image": [image.id.val]}) | ||
handle = self.client.sf.submit(command) | ||
self.wait_on_cmd(self.client, handle) | ||
assert os.path.exists(orig_file_path) | ||
assert os.path.isfile(orig_file_path_and_name) | ||
assert os.path.exists(logfile_path) | ||
assert os.path.isfile(logfile_path_and_name) | ||
|
||
# run cleanse command again, which will now delete the | ||
# files on disk, the original file, the logfile | ||
# and their directories | ||
self.cli.invoke(self.args, strict=True) | ||
out, err = capsys.readouterr() | ||
assert not os.path.exists(orig_file_path) | ||
assert not os.path.isfile(orig_file_path_and_name) | ||
assert not os.path.exists(logfile_path) | ||
assert not os.path.isfile(logfile_path_and_name) | ||
|
||
|
||
class TestCleanseRestrictedAdmin(CLITest): | ||
|
||
# make the user in this test a member of system group | ||
DEFAULT_SYSTEM = True | ||
# make the new member of system group to a Restricted | ||
# Admin with no privileges | ||
DEFAULT_PRIVILEGES = () | ||
|
||
def setup_method(self, method): | ||
super(TestCleanseRestrictedAdmin, self).setup_method(method) | ||
self.cli.register("admin", omero.plugins.admin.AdminControl, "TEST") | ||
self.args += ["admin", "cleanse"] | ||
|
||
def testCleanseRestrictedAdmin(self, capsys): | ||
"""Test cleanse cannot be run by Restricted Admin""" | ||
config_service = self.root.sf.getConfigService() | ||
data_dir = config_service.getConfigValue("omero.data.dir") | ||
self.args += [data_dir] | ||
with pytest.raises(NonZeroReturnCode): | ||
self.cli.invoke(self.args, strict=True) | ||
out, err = capsys.readouterr() | ||
output_end = "SecurityViolation: Admin restrictions: ReadSession\n" | ||
assert err.endswith(output_end) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A next step here (5.4.1+) should be to check if the test is running locally to this directory and
xfail
orskip_if
. This could be done in a base class and made available to all tests:self.is_local_to_server()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cf. https://trello.com/c/vsLBC6Aq/35-define-additional-markers-for-local-tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this point to https://trello.com/c/3gBK3okF/51-integration-test-for-bin-omero-admin-cleanse (last unchecked point on the checklist)