def testParametersParsedCorrectly(self): class TestControl(BaseControl): def __call__(self2, args): self.assertEquals("b",args["a"]) cli = CLI() cli.register("test", TestControl, "HELP") cli.invoke(["test","a=b"]) self.assertEquals(0, cli.rv)
def testLineParsedCorrectly(self): class TestControl(BaseControl): def __call__(self, args): if len(*args) != 2: raise Exc("Args was over parsed! %s" % args) cli = CLI() cli.register("test", TestControl, "HELP") cli.invoke(["test","a","a b c d e"]) self.assertEquals(0, cli.rv)
def testNoArgumentsDies(self): cli = CLI() cli.register("import", ImportControl, "HELP") try: cli.invoke([]) self.assert_(cli.rv != 0) except NonZeroReturnCode, nzrc: pass
class TestChgrp(object): def setup_method(self, method): self.cli = CLI() self.cli.register("chgrp", ChgrpControl, "TEST") self.args = ["chgrp"] def testHelp(self): self.args += ["-h"] self.cli.invoke(self.args, strict=True)
def as_stdout(path, readers=""): path = _to_list(path) readers = str(readers) cli = CLI() cli.loadplugins() if readers: cli.invoke(["import", "-l", readers, "-f"]+path) else: cli.invoke(["import", "-f"]+path) if cli.rv != 0: raise omero.InternalException(None, None, "'import -f' exited with a rc=%s. See console for more information" % cli.rv)
class TestTag(object): def setup_method(self, method): self.cli = CLI() self.cli.register("fs", FsControl, "TEST") self.args = ["fs"] def testHelp(self): self.args += ["-h"] self.cli.invoke(self.args, strict=True) @pytest.mark.parametrize('subcommand', FsControl().get_subcommands()) def testSubcommandHelp(self, subcommand): self.args += [subcommand, "-h"] self.cli.invoke(self.args, strict=True)
class TestUser(object): def setup_method(self, method): self.cli = CLI() self.cli.register("user", UserControl, "TEST") self.args = ["user"] # Help subcommands # ======================================================================== def testHelp(self): self.args += ["-h"] self.cli.invoke(self.args, strict=True) @pytest.mark.parametrize("subcommand", UserControl().get_subcommands()) def testSubcommandHelp(self, subcommand): self.args += [subcommand, "-h"] self.cli.invoke(self.args, strict=True)
class TestHql(object): def setup_method(self, method): self.cli = CLI() self.cli.register("hql", HqlControl, "TEST") self.args = ["hql"] def testHelp(self): self.args += ["-h"] self.cli.invoke(self.args, strict=True) @pytest.mark.parametrize("key", BLACKLISTED_KEYS) def testFilterBlacklist(self, key): output = self.cli.controls["hql"].filter({key: 1}) assert output == {} @pytest.mark.parametrize("key", ["rois", "groupExperimenterMap"]) def testFilterLoaded(self, key): output = self.cli.controls["hql"].filter({"_" + key + "Loaded": 1}) assert output == {} @pytest.mark.parametrize( ("value", "outcome"), [("owner=None;group=None", {}), ("owner=1", {"details": "owner=1"})]) def testFilterDetails(self, value, outcome): output = self.cli.controls["hql"].filter({"_details": value}) assert output == outcome @pytest.mark.parametrize("multi_value", [[0, 1]]) def testFilterMultiValue(self, multi_value): output = self.cli.controls["hql"].filter({'key': multi_value}) assert output == {} @pytest.mark.parametrize("empty_value", [None, [], {}]) def testFilterEmptyValue(self, empty_value): output = self.cli.controls["hql"].filter({'key': empty_value}) assert output == {} @pytest.mark.parametrize("value", WHITELISTED_VALUES) def testFilterWhitelist(self, value): output = self.cli.controls["hql"].filter({'key': value}) assert output == {'key': value} def testFilterStrip(self): output = self.cli.controls["hql"].filter({'_key': 1}) assert output == {'key': 1}
def testDropBoxArgs(self): class MockImportControl(ImportControl): def importer(this, args): self.assertEquals(args.server, "localhost") self.assertEquals(args.port, "4064") self.assertEquals(args.key, "b0742975-03a1-4f6d-b0ac-639943f1a147") self.assertEquals(args.errs, "/Users/cblackburn/omero/tmp/omero_cblackburn/6915/dropboxuUGl5rerr") self.assertEquals(args.file, "/Users/cblackburn/omero/tmp/omero_cblackburn/6915/dropboxaDCjQlout") cmd = ['-s', 'localhost', '-p', '4064', '-k', 'b0742975-03a1-4f6d-b0ac-639943f1a147'] cmd += ['import', '---errs=/Users/cblackburn/omero/tmp/omero_cblackburn/6915/dropboxuUGl5rerr'] cmd += ['---file=/Users/cblackburn/omero/tmp/omero_cblackburn/6915/dropboxaDCjQlout'] cmd += ['--', '/OMERO/DropBox/root/tinyTest.d3d.dv'] cli = CLI() cli.register("import", MockImportControl, "HELP") cli.invoke(cmd)
class TestDownload(object): def setup_method(self, method): self.cli = CLI() self.cli.register("download", DownloadControl, "TEST") self.args = ["download"] def testHelp(self): self.args += ["-h"] self.cli.invoke(self.args, strict=True) @pytest.mark.parametrize( 'bad_input', ['-1', 'OriginalFile:-1', 'FileAnnotation:-1', 'Image:-1']) def testInvalidInput(self, bad_input): self.args += [bad_input, '-'] with pytest.raises(NonZeroReturnCode): self.cli.invoke(self.args, strict=True)
class TestJvmCfg(object): """Test template files regeneration""" @pytest.fixture(autouse=True) def setup_method(self, tmpadmindir): self.cli = CLI() self.cli.register("admin", AdminControl, "TEST") self.cli.register("config", PrefsControl, "TEST") self.args = ["admin", "jvmcfg"] self.cli.dir = path(tmpadmindir) def testNoTemplatesGeneration(self): """Test no template files are generated by the jvmcfg subcommand""" # Test non-existence of configuration files for f in GRID_FILES: assert not os.path.exists(path(self.cli.dir) / "etc" / "grid" / f) for f in ETC_FILES: assert not os.path.exists(path(self.cli.dir) / "etc" / f) # Call the jvmcf command and test file genearation self.cli.invoke(self.args, strict=True) for f in GRID_FILES: assert not os.path.exists(path(self.cli.dir) / "etc" / "grid" / f) for f in ETC_FILES: assert not os.path.exists(path(self.cli.dir) / "etc" / f) @pytest.mark.parametrize("suffix", ["", ".blitz", ".indexer", ".pixeldata", ".repository"]) def testInvalidJvmCfgStrategy(self, suffix, tmpdir): """Test invalid JVM strategy configuration leads to CLI error""" key = "omero.jvmcfg.strategy%s" % suffix self.cli.invoke(["config", "set", key, "bad"], strict=True) with pytest.raises(NonZeroReturnCode): self.cli.invoke(self.args, strict=True)
class TestCli(object): def testMultipleLoad(self): """ In DropBox, the loading of multiple CLIs seems to lead to the wrong context being assigned to some controls. See #4749 """ import random from threading import Thread, Event event = Event() class T(Thread): def run(self, *args): pause = random.random() event.wait(pause) self.cli = CLI() self.cli.loadplugins() self.con = self.cli.controls["admin"] self.cmp = self.con.ctx threads = [T() for x in range(20)] for t in threads: t.start() event.set() for t in threads: t.join() assert len(threads) == len(set([t.cli for t in threads])) assert len(threads) == len(set([t.con for t in threads])) assert len(threads) == len(set([t.cmp for t in threads])) def testLoad(self): tmp = create_path() tmp.write_text("foo") self.cli = CLI() self.cli.register("load", LoadControl, "help") with pytest.raises(NonZeroReturnCode): self.cli.invoke("load %s" % tmp, strict=True) self.cli.invoke("load -k %s" % tmp, strict=True) self.cli.invoke("load --keep-going %s" % tmp, strict=True)
def hrm_to_omero(conn, id_str, image_file): """Upload an image into a specific dataset in OMERO. In case we know from the suffix that a given file format is not supported by OMERO, the upload will not be initiated at all (e.g. for SVI-HDF5, having the suffix '.h5'). The import itself is done by instantiating the CLI class, assembling the required arguments, and finally running cli.invoke(). This eventually triggers the importer() method defined in OMERO's Python bindings in <OMERO.server/lib/python/omero/plugins/import.py>, respectively (source) <openmicroscopy.git/components/tools/OmeroPy/src/omero/plugins/import.py>. Parameters ========== id_str: str - the ID of the target dataset in OMERO (e.g. "G:7:Dataset:23") image_file: str - the local image file including the full path Returns ======= True in case of success, False otherwise. """ if image_file.lower().endswith(('.h5', '.hdf5')): print 'ERROR: HDF5 files are not supported by OMERO!' return False # TODO I: group switching required!! _, gid, obj_type, dset_id = id_str.split(':') # we have to create the annotations *before* we actually upload the image # data itself and link them to the image during the upload - the other way # round is not possible right now as the CLI wrapper (see below) doesn't # expose the ID of the newly created object in OMERO (confirmed by J-M and # Sebastien on the 2015 OME Meeting): namespace = 'deconvolved.hrm' #### mime = 'text/plain' # extract the image basename without suffix: # TODO: is it [0-9a-f] or really [0-9a-z] as in the original PHP code? basename = re.sub(r'(_[0-9a-f]{13}_hrm)\..*', r'\1', image_file) comment = gen_parameter_summary(basename + '.parameters.txt') #### annotations = [] #### # TODO: the list of suffixes should not be hardcoded here! #### for suffix in ['.hgsb', '.log.txt', '.parameters.txt']: #### if not os.path.exists(basename + suffix): #### continue #### ann = conn.createFileAnnfromLocalFile( #### basename + suffix, mimetype=mime, ns=namespace, desc=None) #### annotations.append(ann.getId()) # currently there is no direct "Python way" to import data into OMERO, so # we have to use the CLI wrapper for this: from omero.cli import CLI cli = CLI() cli.loadplugins() # NOTE: cli._client should be replaced with cli.set_client() when switching # to support for OMERO 5.1 and later only: cli._client = conn.c import_args = ["import"] import_args.extend(['-d', dset_id]) if comment is not None: import_args.extend(['--annotation_ns', namespace]) import_args.extend(['--annotation_text', comment]) #### for ann_id in annotations: #### import_args.extend(['--annotation_link', str(ann_id)]) import_args.append(image_file) # print("import_args: " + str(import_args)) try: cli.invoke(import_args, strict=True) except: print('ERROR: uploading "%s" to %s failed!' % (image_file, id_str)) # print(import_args) return False return True
class TestPrefs(object): def setup_method(self, method): self.cli = CLI() self.cli.register("config", PrefsControl, HELP) self.p = create_path() self.args = ["config", "--source", "%s" % self.p] def config(self): return ConfigXml(filename=str(self.p)) def assertStdoutStderr(self, capsys, out='', err=''): o, e = capsys.readouterr() assert o.strip() == out assert e.strip() == err def invoke(self, s): self.cli.invoke(self.args + s.split(), strict=True) def testHelp(self): self.invoke("-h") assert 0 == self.cli.rv def testAll(self, capsys): config = self.config() config.default("test") config.close() self.invoke("all") self.assertStdoutStderr(capsys, out="test\ndefault") def testDefaultInitial(self, capsys): self.invoke("def") self.assertStdoutStderr(capsys, out="default") def testDefaultEnvironment(self, capsys): T = "testDefaultEnvironment" old = os.environ.get("OMERO_CONFIG", None) os.environ["OMERO_CONFIG"] = T try: self.invoke("def") self.assertStdoutStderr(capsys, out=T) finally: if old: os.environ["OMERO_CONFIG"] = old else: del os.environ["OMERO_CONFIG"] def testDefaultSet(self, capsys): self.invoke("def x") self.assertStdoutStderr(capsys, out="x") self.invoke("def") self.assertStdoutStderr(capsys, out="x") def testGetSet(self, capsys): self.invoke("get X") self.assertStdoutStderr(capsys) self.invoke("set A B") self.assertStdoutStderr(capsys) self.invoke("get A") self.assertStdoutStderr(capsys, out='B') self.invoke("get") self.assertStdoutStderr(capsys, out='A=B') self.invoke("set A") self.assertStdoutStderr(capsys) self.invoke("keys") self.assertStdoutStderr(capsys) def testKeys(self, capsys): self.invoke("keys") self.assertStdoutStderr(capsys) self.invoke("set A B") self.assertStdoutStderr(capsys) self.invoke("keys") self.assertStdoutStderr(capsys, out="A") def testVersion(self, capsys): self.invoke("version") self.assertStdoutStderr(capsys, out=ConfigXml.VERSION) def testPath(self, capsys): self.invoke("path") self.assertStdoutStderr(capsys, out=self.p) def testLoad(self, capsys): to_load = create_path() to_load.write_text("A=B") self.invoke("load %s" % to_load) self.assertStdoutStderr(capsys) self.invoke("get") self.assertStdoutStderr(capsys, out="A=B") # Same property/value pairs should pass self.invoke("load %s" % to_load) to_load.write_text("A=C") with pytest.raises(NonZeroReturnCode): # Different property/value pair should fail self.invoke("load %s" % to_load) self.assertStdoutStderr( capsys, err="Duplicate property: A ('B' => 'C')") # Quiet load self.invoke("load -q %s" % to_load) self.assertStdoutStderr(capsys) self.invoke("get") self.assertStdoutStderr(capsys, out="A=C") def testLoadDoesNotExist(self): # ticket:7273 pytest.raises(NonZeroReturnCode, self.invoke, "load THIS_FILE_SHOULD_NOT_EXIST") def testDrop(self, capsys): self.invoke("def x") self.assertStdoutStderr(capsys, out="x") self.invoke("def") self.assertStdoutStderr(capsys, out="x") self.invoke("all") self.assertStdoutStderr(capsys, out="x\ndefault") self.invoke("def y") self.assertStdoutStderr(capsys, out="y") self.invoke("all") self.assertStdoutStderr(capsys, out="y\nx\ndefault") self.invoke("drop x") self.assertStdoutStderr(capsys) self.invoke("all") self.assertStdoutStderr(capsys, 'y\ndefault') def testEdit(self): """ Testing edit is a bit more complex since it wants to start another process. Rather than using invoke, we manage things ourselves here. """ def fake_edit_path(tmp_file, tmp_text): pass args = self.cli.parser.parse_args("config edit".split()) control = self.cli.controls["config"] config = self.config() try: control.edit(args, config, fake_edit_path) finally: config.close() def testNewEnvironment(self, capsys): config = self.config() config.default("default") config.close() os.environ["OMERO_CONFIG"] = "testNewEnvironment" self.invoke("set A B") self.assertStdoutStderr(capsys) self.invoke("get") self.assertStdoutStderr(capsys, out="A=B")
class TestPrefs(object): def setup_method(self, method): self.cli = CLI() self.cli.register("config", PrefsControl, HELP) self.p = create_path() self.args = ["config", "--source", "%s" % self.p] def config(self): return ConfigXml(filename=str(self.p)) def assertStdoutStderr(self, capsys, out='', err=''): o, e = capsys.readouterr() assert o.strip() == out assert e.strip() == err def invoke(self, s): self.cli.invoke(self.args + s.split(), strict=True) def testHelp(self): self.invoke("-h") assert 0 == self.cli.rv def testAll(self, capsys): config = self.config() config.default("test") config.close() self.invoke("all") self.assertStdoutStderr(capsys, out="test\ndefault") def testDefaultInitial(self, capsys): self.invoke("def") self.assertStdoutStderr(capsys, out="default") def testDefaultEnvironment(self, capsys): T = "testDefaultEnvironment" old = os.environ.get("OMERO_CONFIG", None) os.environ["OMERO_CONFIG"] = T try: self.invoke("def") self.assertStdoutStderr(capsys, out=T) finally: if old: os.environ["OMERO_CONFIG"] = old else: del os.environ["OMERO_CONFIG"] def testDefaultSet(self, capsys): self.invoke("def x") self.assertStdoutStderr(capsys, out="x") self.invoke("def") self.assertStdoutStderr(capsys, out="x") def testGetSet(self, capsys): self.invoke("get X") self.assertStdoutStderr(capsys) self.invoke("set A B") self.assertStdoutStderr(capsys) self.invoke("get A") self.assertStdoutStderr(capsys, out='B') self.invoke("get") self.assertStdoutStderr(capsys, out='A=B') self.invoke("set A") self.assertStdoutStderr(capsys) self.invoke("keys") self.assertStdoutStderr(capsys) def testKeys(self, capsys): self.invoke("keys") self.assertStdoutStderr(capsys) self.invoke("set A B") self.assertStdoutStderr(capsys) self.invoke("keys") self.assertStdoutStderr(capsys, out="A") def testVersion(self, capsys): self.invoke("version") self.assertStdoutStderr(capsys, out=ConfigXml.VERSION) def testPath(self, capsys): self.invoke("path") self.assertStdoutStderr(capsys, out=self.p) def testLoad(self, capsys): to_load = create_path() to_load.write_text("A=B") self.invoke("load %s" % to_load) self.assertStdoutStderr(capsys) self.invoke("get") self.assertStdoutStderr(capsys, out="A=B") # Same property/value pairs should pass self.invoke("load %s" % to_load) to_load.write_text("A=C") with pytest.raises(NonZeroReturnCode): # Different property/value pair should fail self.invoke("load %s" % to_load) self.assertStdoutStderr(capsys, err="Duplicate property: A ('B' => 'C')") # Quiet load self.invoke("load -q %s" % to_load) self.assertStdoutStderr(capsys) self.invoke("get") self.assertStdoutStderr(capsys, out="A=C") def testLoadDoesNotExist(self): # ticket:7273 pytest.raises(NonZeroReturnCode, self.invoke, "load THIS_FILE_SHOULD_NOT_EXIST") def testDrop(self, capsys): self.invoke("def x") self.assertStdoutStderr(capsys, out="x") self.invoke("def") self.assertStdoutStderr(capsys, out="x") self.invoke("all") self.assertStdoutStderr(capsys, out="x\ndefault") self.invoke("def y") self.assertStdoutStderr(capsys, out="y") self.invoke("all") self.assertStdoutStderr(capsys, out="y\nx\ndefault") self.invoke("drop x") self.assertStdoutStderr(capsys) self.invoke("all") self.assertStdoutStderr(capsys, 'y\ndefault') def testEdit(self): """ Testing edit is a bit more complex since it wants to start another process. Rather than using invoke, we manage things ourselves here. """ def fake_edit_path(tmp_file, tmp_text): pass args = self.cli.parser.parse_args("config edit".split()) control = self.cli.controls["config"] config = self.config() try: control.edit(args, config, fake_edit_path) finally: config.close() def testNewEnvironment(self, capsys): config = self.config() config.default("default") config.close() os.environ["OMERO_CONFIG"] = "testNewEnvironment" self.invoke("set A B") self.assertStdoutStderr(capsys) self.invoke("get") self.assertStdoutStderr(capsys, out="A=B")
class TestPrefs(object): def setup_method(self, method): self.cli = CLI() self.cli.register("config", PrefsControl, HELP) self.p = create_path() self.args = ["-d1", "config", "--source", "%s" % self.p] def config(self): return ConfigXml(filename=str(self.p)) def assertStdoutStderr(self, capsys, out='', err='', strip_warning=False): o, e = capsys.readouterr() if strip_warning: assert (e.startswith('WARNING: ')) e = '\n'.join(e.split('\n')[1:]) assert (o.strip() == out and e.strip() == err) def invoke(self, s): if isinstance(s, basestring): s = s.split() self.cli.invoke(self.args + s, strict=True) def testHelp(self): self.invoke("-h") assert 0 == self.cli.rv @pytest.mark.parametrize('subcommand', PrefsControl().get_subcommands()) def testSubcommandHelp(self, subcommand): self.invoke("%s -h" % subcommand) assert 0 == self.cli.rv def testAll(self, capsys): config = self.config() config.default("test") config.close() self.invoke("all") self.assertStdoutStderr(capsys, out="test\ndefault") def testDefaultInitial(self, capsys): self.invoke("def") self.assertStdoutStderr(capsys, out="default") def testDefaultEnvironment(self, capsys, monkeypatch): monkeypatch.setenv("OMERO_CONFIG", "testDefaultEnvironment") self.invoke("def") self.assertStdoutStderr(capsys, out="testDefaultEnvironment") def testDefaultSet(self, capsys): self.invoke("def x") self.assertStdoutStderr(capsys, out="x") self.invoke("def") self.assertStdoutStderr(capsys, out="x") def testGetSet(self, capsys): self.invoke("get X") self.assertStdoutStderr(capsys) self.invoke("set A B") self.assertStdoutStderr(capsys) self.invoke("get A") self.assertStdoutStderr(capsys, out='B') self.invoke("get") self.assertStdoutStderr(capsys, out='A=B') self.invoke("set A") self.assertStdoutStderr(capsys) self.invoke("keys") self.assertStdoutStderr(capsys) def testGetHidePassword(self, capsys): config = { "omero.X.mypassword": "******", "omero.X.pass": "******", "omero.X.password": "******", "omero.X.regular": "value", "omero.Y.MyPassword": "******", "omero.Y.Pass": "******", "omero.Y.Password": "******", "omero.Z.mypassword": "", "omero.Z.pass": "", "omero.Z.password": "" } output_hidden_password = ('omero.X.mypassword=********\n' 'omero.X.pass=********\n' 'omero.X.password=********\n' 'omero.X.regular=value\n' 'omero.Y.MyPassword=********\n' 'omero.Y.Pass=********\n' 'omero.Y.Password=********\n' 'omero.Z.mypassword=\n' 'omero.Z.pass=\n' 'omero.Z.password='******'omero.X.mypassword=long_password\n' 'omero.X.pass=shortpass\n' 'omero.X.password=medium_password\n' 'omero.X.regular=value\n' 'omero.Y.MyPassword=long_password\n' 'omero.Y.Pass=shortpass\n' 'omero.Y.Password=medium_password\n' 'omero.Z.mypassword=\n' 'omero.Z.pass=\n' 'omero.Z.password='******'argument', ['A=B', 'A= B']) def testSetFails(self, capsys, argument): self.invoke("set %s" % argument) self.assertStdoutStderr( capsys, err="\"=\" in key name. Did you mean \"...set A B\"?") def testKeys(self, capsys): self.invoke("keys") self.assertStdoutStderr(capsys) self.invoke("set A B") self.assertStdoutStderr(capsys) self.invoke("keys") self.assertStdoutStderr(capsys, out="A") self.invoke("set C D=E") self.assertStdoutStderr(capsys) self.invoke("keys") self.assertStdoutStderr(capsys, out="A\nC") def testVersion(self, capsys): self.invoke("version") self.assertStdoutStderr(capsys, out=ConfigXml.VERSION) def testPath(self, capsys): self.invoke("path") self.assertStdoutStderr(capsys, out=self.p) def testLoad(self, capsys): to_load = create_path() to_load.write_text("A=B") self.invoke("load %s" % to_load) self.assertStdoutStderr(capsys) self.invoke("get") self.assertStdoutStderr(capsys, out="A=B") # Same property/value pairs should pass self.invoke("load %s" % to_load) to_load.write_text("A=C") with pytest.raises(NonZeroReturnCode): # Different property/value pair should fail self.invoke("load %s" % to_load) self.assertStdoutStderr(capsys, err="Duplicate property: A ('B' => 'C')") # Quiet load self.invoke("load -q %s" % to_load) self.assertStdoutStderr(capsys) self.invoke("get") self.assertStdoutStderr(capsys, out="A=C") def testLoadDoesNotExist(self): # ticket:7273 pytest.raises(NonZeroReturnCode, self.invoke, "load THIS_FILE_SHOULD_NOT_EXIST") def testLoadMultiLine(self, capsys): to_load = create_path() to_load.write_text("A=B\\\nC") self.invoke("load %s" % to_load) self.invoke("get") self.assertStdoutStderr(capsys, out="A=BC") @pytest.mark.parametrize('validkeyvalue', [('A', 'B'), ('A', 'B=C'), ('A.B', 'C.D'), ('A.B', "'C.D'")]) def testLoadWhitelist(self, capsys, validkeyvalue): valid_key, valid_value = validkeyvalue to_load = create_path() to_load.write_text("%s=%s\n" % (valid_key, valid_value)) self.invoke("load %s" % to_load) self.invoke("get %s" % valid_key) self.assertStdoutStderr(capsys, out=valid_value) @pytest.mark.parametrize(('invalidline', 'invalidkey'), [('E F G', 'E F G'), ('E!F=G', 'E!F'), ('E = F', 'E')]) def testLoadInvalidKey(self, capsys, invalidline, invalidkey): self.invoke("set A B") self.assertStdoutStderr(capsys) to_load = create_path() to_load.write_text("C=D\n%s\nH=I\n" % invalidline) with pytest.raises(NonZeroReturnCode): self.invoke("load %s" % to_load) self.assertStdoutStderr(capsys, err="Illegal property name: %s" % invalidkey) self.invoke("get") self.assertStdoutStderr(capsys, out="A=B") @pytest.mark.parametrize('valid_key_value', [('A', 'B'), ('A', 'B=C'), ('A.B', 'C.D'), ('A.B', "'C.D'")]) def testSetWhitelist(self, capsys, valid_key_value): valid_key, valid_value = valid_key_value self.invoke(["set", valid_key, valid_value]) self.invoke(["get", valid_key]) self.assertStdoutStderr(capsys, out=valid_value) @pytest.mark.parametrize('invalid_key', ['E F', 'E!F', 'E ']) def testSetInvalidKey(self, capsys, invalid_key): with pytest.raises(NonZeroReturnCode): self.invoke(["set", invalid_key, "test"]) self.assertStdoutStderr(capsys, err="Illegal property name: %s" % invalid_key.strip()) def testSetFromFile(self, capsys): to_load = create_path() to_load.write_text("Test") self.invoke("set -f %s A" % to_load) self.invoke("get") self.assertStdoutStderr(capsys, out="A=Test") def testDrop(self, capsys): self.invoke("def x") self.assertStdoutStderr(capsys, out="x") self.invoke("def") self.assertStdoutStderr(capsys, out="x") self.invoke("all") self.assertStdoutStderr(capsys, out="x\ndefault") self.invoke("def y") self.assertStdoutStderr(capsys, out="y") self.invoke("all") self.assertStdoutStderr(capsys, out="y\nx\ndefault") self.invoke("drop x") self.assertStdoutStderr(capsys) self.invoke("all") self.assertStdoutStderr(capsys, 'y\ndefault') def testDropFails(self, capsys): self.invoke("drop x") self.assertStdoutStderr(capsys, err="Unknown configuration: x") def testEdit(self): """ Testing edit is a bit more complex since it wants to start another process. Rather than using invoke, we manage things ourselves here. """ def fake_edit_path(tmp_file, tmp_text): pass args = self.cli.parser.parse_args("config edit".split()) control = self.cli.controls["config"] config = self.config() try: control.edit(args, config, fake_edit_path) finally: config.close() def testNewEnvironment(self, capsys, monkeypatch): config = self.config() config.default("default") config.close() monkeypatch.setenv("OMERO_CONFIG", "testNewEnvironment") self.invoke("set A B") self.assertStdoutStderr(capsys) self.invoke("get") self.assertStdoutStderr(capsys, out="A=B") @pytest.mark.parametrize(('initval', 'newval'), [('1', '2'), ('\"1\"', '\"2\"'), ('test', 'test')]) def testAppendFails(self, initval, newval): self.invoke("set A %s" % initval) with pytest.raises(NonZeroReturnCode): self.invoke("append A %s" % newval) def testRemoveUnsetPropertyFails(self): with pytest.raises(NonZeroReturnCode): self.invoke("remove A x") @pytest.mark.parametrize(('initval', 'newval'), [('1', '1'), ('[\"1\"]', '1'), ('[1]', '\"1\"')]) def testRemoveFails(self, initval, newval): self.invoke("set A %s" % initval) with pytest.raises(NonZeroReturnCode): self.invoke("remove A %s" % newval) @pytest.mark.parametrize('report', ['--report', '']) def testAppendRemove(self, report, capsys): self.invoke("append %s A 1" % report) self.assertReportStdout(report, capsys, 'Appended A:1') self.invoke("get A") self.assertStdoutStderr(capsys, out='[1]') self.invoke("append %s A \"y\"" % report) self.assertReportStdout(report, capsys, 'Appended A:"y"') self.invoke("get A") self.assertStdoutStderr(capsys, out='[1, "y"]') self.invoke("remove %s A \"y\"" % report) self.assertReportStdout(report, capsys, 'Removed A:"y"') self.invoke("get A") self.assertStdoutStderr(capsys, out='[1]') self.invoke("remove %s A 1" % report) self.assertReportStdout(report, capsys, 'Removed A:1') self.invoke("get A") self.assertStdoutStderr(capsys, out='[]') def assertReportStdout(self, report, capsys, out): if report and out: self.assertStdoutStderr(capsys, out='Changed: %s' % out) else: self.assertStdoutStderr(capsys, out='') @pytest.mark.parametrize('report', ['--report', '']) def testAppendSet(self, report, capsys): self.invoke("append %s --set A 1" % report) self.assertReportStdout(report, capsys, 'Appended A:1') self.invoke("get A") self.assertStdoutStderr(capsys, out='[1]') self.invoke("append %s --set A 2" % report) self.assertReportStdout(report, capsys, 'Appended A:2') self.invoke("get A") self.assertStdoutStderr(capsys, out='[1, 2]') self.invoke("append %s --set A 1" % report) self.assertReportStdout(report, capsys, '') self.invoke("get A") self.assertStdoutStderr(capsys, out='[1, 2]') self.invoke("append %s A 1" % report) self.assertReportStdout(report, capsys, 'Appended A:1') self.invoke("get A") self.assertStdoutStderr(capsys, out='[1, 2, 1]') def testRemoveIdenticalValues(self, capsys): self.invoke("set A [1,1]") self.invoke("remove A 1") self.invoke("get A") self.assertStdoutStderr(capsys, out='[1]') self.invoke("remove A 1") self.invoke("get A") self.assertStdoutStderr(capsys, out='[]') @pytest.mark.broken(reason="migrate to omero-web") @pytest.mark.usefixtures('configxml') def testAppendWithDefault(self, monkeypatch, capsys): import json monkeypatch.setattr( "omeroweb.settings.CUSTOM_SETTINGS_MAPPINGS", { "omero.web.test": ["TEST", "[1,2,3]", json.loads], "omero.web.notalist": ["NOTALIST", "abc", str], }) self.invoke("append omero.web.test 4") self.invoke("get omero.web.test") self.assertStdoutStderr(capsys, out='[1, 2, 3, 4]') self.invoke("append --set omero.web.test 2") self.assertStdoutStderr(capsys, out='') self.invoke("get omero.web.test") self.assertStdoutStderr(capsys, out='[1, 2, 3, 4]') self.invoke("append omero.web.unknown 1") self.invoke("get omero.web.unknown") self.assertStdoutStderr(capsys, out='[1]') with pytest.raises(NonZeroReturnCode): self.invoke("append omero.web.notalist 1") @pytest.mark.broken(reason="migrate to omero-web") @pytest.mark.usefixtures('configxml') def testRemoveWithDefault(self, monkeypatch, capsys): import json monkeypatch.setattr("omeroweb.settings.CUSTOM_SETTINGS_MAPPINGS", { "omero.web.test": ["TEST", "[1,2,3]", json.loads], }) self.invoke("remove omero.web.test 2") self.invoke("get omero.web.test") self.assertStdoutStderr(capsys, out='[1, 3]') self.invoke("remove omero.web.test 1") self.invoke("remove omero.web.test 3") self.invoke("get omero.web.test") self.assertStdoutStderr(capsys, out='[]') @pytest.mark.parametrize("data", ( ({}, ""), ({ "a": "b" }, "a=b"), ({ "a": "b", "a": "d" }, "a=d"), ({ "a": "b", "c": "d" }, "a=b\nc=d"), ({ "c": "d", "a": "b" }, "a=b\nc=d"), )) @pytest.mark.usefixtures('configxml') def testList(self, data, monkeypatch, capsys): for k, v in data[0].items(): self.invoke("set %s %s" % (k, v)) self.invoke("list") self.assertStdoutStderr(capsys, out=data[1], strip_warning=True) @pytest.mark.parametrize("data", ( ("omero.a=b\nomero.c=d\n##ignore=me\n", "omero.a=b\nomero.c=d", "a (1)\n\t\nc (1)"), ("omero.whitelist=\\\nome.foo,\\\nome.bar\n### END", "omero.whitelist=ome.foo,ome.bar", "whitelist (1)"), ("omero.whitelist=\\\nome.foo,\\\nome.bar\n", "omero.whitelist=ome.foo,ome.bar", "whitelist (1)"), ("omero.whitelist=\\\nome.foo,\\\nome.bar", "omero.whitelist=ome.foo,ome.bar", "whitelist (1)"), ("omero.user_mapping=\\\na=b,c=d", "omero.user_mapping=a=b,c=d", "user_mapping (1)"), ("omero.whitelist=ome.foo\nIce.c=d\n", "Ice.c=d\nomero.whitelist=ome.foo", "whitelist (1)"), ("omero.a=b\nomero.c=d\nomero.e=f\n##ignore=me\n", "omero.a=b\nomero.c=d\nomero.e=f", "a (1)\n\t\nc (1)\n\t\ne (1)"), ("omero.a=b\nomero.c=d\nomero.e=f\n##ignore=me\n", "omero.a=b\nomero.c=d\nomero.e=f", "a (1)\n\t\nc (1)\n\t\ne (1)"), )) def testFileParsing(self, tmpdir, capsys, data): input, defaults, keys = data cfg = tmpdir.join("test.cfg") cfg.write(input) self.invoke("parse --file=%s --no-web" % cfg) self.assertStdoutStderr(capsys, out=defaults) self.invoke("parse --file=%s --defaults --no-web" % cfg) self.assertStdoutStderr(capsys, out=defaults) self.invoke("parse --file=%s --keys --no-web" % cfg) self.assertStdoutStderr(capsys, out=keys) @pytest.mark.parametrize("data", ( (u"omero.ldap.base=ou=ascii\n", "ascii2"), (u"omero.ldap.base=ou=ascii\n", "unicodé"), (u"omero.ldap.base=ou=unicodé\n", "ascii"), (u"omero.ldap.base=ou=unicodé\n", "unicodé2"), )) def testUnicode(self, tmpdir, capsys, data): input, update = data cfg = tmpdir.join("test.cfg") cfg.write(input.encode("utf-8"), "wb") self.invoke("load %s" % cfg) self.invoke("get omero.ldap.base") self.invoke("set omero.ldap.base %s" % update) self.invoke("get omero.ldap.base") @pytest.mark.xfail def testConfigPropertyParser(self, tmpdir): cfg = tmpdir.join("test.properties") s = "a=1\nb.c=a b <!> c\nd.e=line1\\\nline2\nf.g=\\n\n" cfg.write(s) pp = PropertyParser() props = pp.parse_file(str(cfg)) # Fails, the last two properties are parsed as one: # 'd.e' = 'line1line2f.g=\\n' assert len(props) == 4 assert props[0].key == 'a' assert props[0].val == '1' assert props[1].key == 'b.c' assert props[1].val == 'a b <!> c' assert props[2].key == 'd.e' assert props[2].val == 'line1line2' assert props[3].key == 'f.g' assert props[3].val == '\\n'
class TestRewrite(object): """Test template files regeneration""" @pytest.fixture(autouse=True) def setup_method(self, tmpadmindir): self.cli = CLI() self.cli.dir = path(tmpadmindir) self.cli.register("admin", AdminControl, "TEST") self.cli.register("config", PrefsControl, "TEST") self.args = ["admin", "rewrite"] def testTemplatesGeneration(self): """Test template files are generated by the rewrite subcommand""" # Test non-existence of configuration files for f in GRID_FILES: assert not os.path.exists(path(self.cli.dir) / "etc" / "grid" / f) for f in ETC_FILES: assert not os.path.exists(path(self.cli.dir) / "etc" / f) # Call the jvmcf command and test file genearation self.cli.invoke(self.args, strict=True) for f in GRID_FILES: assert os.path.exists(path(self.cli.dir) / "etc" / "grid" / f) for f in ETC_FILES: assert os.path.exists(path(self.cli.dir) / "etc" / f) def testForceRewrite(self, monkeypatch): """Test template regeneration while the server is running""" # Call the jvmcfg command and test file generation self.cli.invoke(self.args, strict=True) monkeypatch.setattr(AdminControl, "status", lambda *args, **kwargs: 0) with pytest.raises(NonZeroReturnCode): self.cli.invoke(self.args, strict=True) def testOldTemplates(self): old_templates = path(__file__).dirname() / ".." / "old_templates.xml" old_templates.copy( path(self.cli.dir) / "etc" / "templates" / "grid" / "templates.xml") with pytest.raises(NonZeroReturnCode): self.cli.invoke(self.args, strict=True) @pytest.mark.parametrize('prefix', [None, 1]) @pytest.mark.parametrize('registry', [None, 111]) @pytest.mark.parametrize('tcp', [None, 222]) @pytest.mark.parametrize('ssl', [None, 333]) def testExplicitPorts(self, registry, ssl, tcp, prefix, monkeypatch): """ Test the omero.ports.xxx configuration properties during the generation of the configuration files """ # Skip the JVM settings calculation for this test monkeypatch.setattr(omero.install.jvmcfg, "adjust_settings", lambda x, y: {}) kwargs = {} if prefix: kwargs["prefix"] = prefix if registry: kwargs["registry"] = registry if tcp: kwargs["tcp"] = tcp if ssl: kwargs["ssl"] = ssl for (k, v) in kwargs.iteritems(): self.cli.invoke( ["config", "set", "omero.ports.%s" % k, "%s" % v], strict=True) self.cli.invoke(self.args, strict=True) check_ice_config(self.cli.dir, **kwargs) check_registry(self.cli.dir, **kwargs) check_default_xml(self.cli.dir, **kwargs)
class TestTag(object): def setup_method(self, method): self.cli = CLI() self.cli.register("tag", TagControl, "TEST") self.args = ["tag"] def testHelp(self): self.args += ["-h"] self.cli.invoke(self.args, strict=True) @pytest.mark.parametrize('subcommand', TagControl().get_subcommands()) def testSubcommandHelp(self, subcommand): self.args += [subcommand, "-h"] self.cli.invoke(self.args, strict=True) def testCreateTagsetFails(self): self.args += ["createset", "--tag", "A"] with pytest.raises(NonZeroReturnCode): self.cli.invoke(self.args, strict=True) def testListFails(self): self.args += ["list", "--tagset", "tagset"] with pytest.raises(NonZeroReturnCode): self.cli.invoke(self.args, strict=True) def testListsetsFails(self): self.args += ["listsets", "--tag", "tag"] with pytest.raises(NonZeroReturnCode): self.cli.invoke(self.args, strict=True) @pytest.mark.parametrize( ('object_arg', 'tag_arg'), [('Image:1', 'test'), ('Image', '1'), ('Image:image', '1'), ('1', '1')]) def testLinkFails(self, object_arg, tag_arg): self.args += ["link", object_arg, tag_arg] with pytest.raises(NonZeroReturnCode): self.cli.invoke(self.args, strict=True)
class TestDatabase(object): def setup_method(self, method): self.cli = CLI() self.cli.register("db", DatabaseControl, "TEST") self.args = ["db"] dir = path(__file__) / ".." / ".." / ".." / ".." / ".." / ".." /\ ".." / "dist" # FIXME: should not be hard-coded dir = dir.abspath() cfg = dir / "etc" / "omero.properties" cfg = cfg.abspath() self.cli.dir = dir self.data = {} for line in cfg.text().split("\n"): line = line.strip() for x in ("version", "patch"): key = "omero.db." + x if line.startswith(key): self.data[x] = line[len(key)+1:] self.file = create_path() self.script_file = "%(version)s__%(patch)s.sql" % self.data if os.path.isfile(self.script_file): os.rename(self.script_file, self.script_file + '.bak') assert not os.path.isfile(self.script_file) self.mox = Mox() self.mox.StubOutWithMock(getpass, 'getpass') self.mox.StubOutWithMock(__builtin__, "raw_input") def teardown_method(self, method): self.file.remove() if os.path.isfile(self.script_file): os.remove(self.script_file) if os.path.isfile(self.script_file + '.bak'): os.rename(self.script_file + '.bak', self.script_file) self.mox.UnsetStubs() self.mox.VerifyAll() def password(self, string, strict=True): self.cli.invoke("db password " + string % self.data, strict=strict) def testHelp(self): self.args += ["-h"] self.cli.invoke(self.args, strict=True) @pytest.mark.parametrize( 'subcommand', DatabaseControl().get_subcommands()) def testSubcommandHelp(self, subcommand): self.args += [subcommand, "-h"] self.cli.invoke(self.args, strict=True) def testBadVersionDies(self): with pytest.raises(NonZeroReturnCode): self.cli.invoke("db script NONE NONE pw", strict=True) def testPasswordIsAskedForAgainIfDiffer(self): self.expectPassword("ome") self.expectConfirmation("bad") self.expectPassword("ome") self.expectConfirmation("ome") self.mox.ReplayAll() self.password("") def testPasswordIsAskedForAgainIfEmpty(self): self.expectPassword("") self.expectPassword("ome") self.expectConfirmation("ome") self.mox.ReplayAll() self.password("") @pytest.mark.parametrize('no_salt', ['', '--no-salt']) @pytest.mark.parametrize('user_id', ['', '0', '1']) @pytest.mark.parametrize('password', ['', 'ome']) def testPassword(self, user_id, password, no_salt, capsys): args = "" if user_id: args += "--user-id=%s " % user_id if no_salt: args += "%s " % no_salt if password: args += "%s" % password else: self.expectPassword("ome", id=user_id) self.expectConfirmation("ome", id=user_id) self.mox.ReplayAll() self.password(args) out, err = capsys.readouterr() assert out.strip() == self.password_output(user_id, no_salt) @pytest.mark.parametrize('file_arg', ['', '-f', '--file']) @pytest.mark.parametrize('no_salt', ['', '--no-salt']) @pytest.mark.parametrize('password', ['', '--password ome']) def testScript(self, no_salt, file_arg, password, capsys): """ Recommended usage of db script """ args = "db script " + password if no_salt: args += " %s" % no_salt if file_arg: args += " %s %s" % (file_arg, str(self.file)) output = self.file else: output = self.script_file if not password: self.expectPassword("ome") self.expectConfirmation("ome") self.mox.ReplayAll() self.cli.invoke(args, strict=True) out, err = capsys.readouterr() assert 'Using %s for version' % self.data['version'] in err assert 'Using %s for patch' % self.data['patch'] in err if password: assert 'Using password from commandline' in err with open(output) as f: lines = f.readlines() for line in lines: if line.startswith('insert into password values (0'): assert line.strip() == self.script_output(no_salt) @pytest.mark.parametrize('file_arg', ['', '-f', '--file']) @pytest.mark.parametrize('no_salt', ['', '--no-salt']) @pytest.mark.parametrize('pos_args', [ '%s %s %s', '--version %s --patch %s --password %s']) def testScriptDeveloperArgs(self, pos_args, no_salt, file_arg, capsys): """ Deprecated and developer usage of db script """ arg_values = ('VERSION', 'PATCH', 'PASSWORD') args = "db script " + pos_args % arg_values if no_salt: args += " %s" % no_salt if file_arg: args += " %s %s" % (file_arg, str(self.file)) self.file else: self.script_file self.mox.ReplayAll() with pytest.raises(NonZeroReturnCode): self.cli.invoke(args, strict=True) out, err = capsys.readouterr() assert 'Using %s for version' % (arg_values[0]) in err assert 'Using %s for patch' % (arg_values[1]) in err assert 'Using password from commandline' in err assert 'Invalid Database version/patch' in err def password_ending(self, user, id): if id and id != '0': rv = "user %s: " % id else: rv = "%s user: "******"password for OMERO " + rv def expectPassword(self, pw, user="******", id=None): getpass.getpass("Please enter %s" % self.password_ending(user, id)).AndReturn(pw) def expectConfirmation(self, pw, user="******", id=None): getpass.getpass("Please re-enter %s" % self.password_ending(user, id)).AndReturn(pw) def password_output(self, user_id, no_salt): update_msg = "UPDATE password SET hash = \'%s\'" \ " WHERE experimenter_id = %s;" if not user_id: user_id = "0" return update_msg % (hash_map[(user_id, no_salt)], user_id) def script_output(self, no_salt): root_password_msg = "insert into password values (0,\'%s\');" return root_password_msg % (hash_map[("0", no_salt)])
class TestTag(object): def setup_method(self, method): self.cli = CLI() self.cli.register("tag", TagControl, "TEST") self.args = ["tag"] def testHelp(self): self.args += ["-h"] self.cli.invoke(self.args, strict=True) @pytest.mark.parametrize('subcommand', TagControl().get_subcommands()) def testSubcommandHelp(self, subcommand): self.args += [subcommand, "-h"] self.cli.invoke(self.args, strict=True) def testCreateTagsetFails(self): self.args += ["createset", "--tag", "A"] with pytest.raises(NonZeroReturnCode): self.cli.invoke(self.args, strict=True) def testListFails(self): self.args += ["list", "--tagset", "tagset"] with pytest.raises(NonZeroReturnCode): self.cli.invoke(self.args, strict=True) def testListsetsFails(self): self.args += ["listsets", "--tag", "tag"] with pytest.raises(NonZeroReturnCode): self.cli.invoke(self.args, strict=True) @pytest.mark.parametrize(('object_arg', 'tag_arg'), [('Image:1', 'test'), ('Image', '1'), ('Image:image', '1'), ('1', '1')]) def testLinkFails(self, object_arg, tag_arg): self.args += ["link", object_arg, tag_arg] with pytest.raises(NonZeroReturnCode): self.cli.invoke(self.args, strict=True)
def invoke(self, *args): CLI.invoke(self, *args, strict = True)
def testOne(self): cli = CLI() cli.register("t", TestRCode.T, "TEST") cli.invoke(["t"]) self.assert_(cli.rv == 1, cli.rv)
class TestRewrite(object): """Test template files regeneration""" @pytest.fixture(autouse=True) def setup_method(self, tmpadmindir): self.cli = CLI() self.cli.dir = path(tmpadmindir) self.cli.register("admin", AdminControl, "TEST") self.cli.register("config", PrefsControl, "TEST") self.args = ["admin", "rewrite"] def testTemplatesGeneration(self): """Test template files are generated by the rewrite subcommand""" # Test non-existence of configuration files for f in GRID_FILES: assert not os.path.exists(path(self.cli.dir) / "etc" / "grid" / f) for f in ETC_FILES: assert not os.path.exists(path(self.cli.dir) / "etc" / f) # Call the jvmcf command and test file genearation self.cli.invoke(self.args, strict=True) for f in GRID_FILES: assert os.path.exists(path(self.cli.dir) / "etc" / "grid" / f) for f in ETC_FILES: assert os.path.exists(path(self.cli.dir) / "etc" / f) def testForceRewrite(self, monkeypatch): """Test template regeneration while the server is running""" # Call the jvmcfg command and test file generation self.cli.invoke(self.args, strict=True) monkeypatch.setattr(AdminControl, "status", lambda *args, **kwargs: 0) with pytest.raises(NonZeroReturnCode): self.cli.invoke(self.args, strict=True) def testOldTemplates(self): old_templates = path(__file__).dirname() / ".." / "old_templates.xml" old_templates.copy( path(self.cli.dir) / "etc" / "templates" / "grid" / "templates.xml") with pytest.raises(NonZeroReturnCode): self.cli.invoke(self.args, strict=True) @pytest.mark.parametrize('prefix', [None, 1]) @pytest.mark.parametrize('registry', [None, 111]) @pytest.mark.parametrize('tcp', [None, 222]) @pytest.mark.parametrize('ssl', [None, 333]) def testExplicitPorts(self, registry, ssl, tcp, prefix, monkeypatch): """ Test the omero.ports.xxx configuration properties during the generation of the configuration files """ # Skip the JVM settings calculation for this test monkeypatch.setattr(omero.install.jvmcfg, "adjust_settings", lambda x, y: {}) kwargs = {} if prefix: kwargs["prefix"] = prefix if registry: kwargs["registry"] = registry if tcp: kwargs["tcp"] = tcp if ssl: kwargs["ssl"] = ssl for (k, v) in kwargs.iteritems(): self.cli.invoke(["config", "set", "omero.ports.%s" % k, "%s" % v], strict=True) self.cli.invoke(self.args, strict=True) check_ice_config(self.cli.dir, **kwargs) check_registry(self.cli.dir, **kwargs) check_default_xml(self.cli.dir, **kwargs)
class TestImport(object): def setup_method(self, method): self.cli = CLI() self.cli.register("import", ImportControl, "TEST") self.args = ["import"] def add_client_dir(self): dist_dir = path(__file__) / ".." / ".." / ".." / ".." / ".." / ".." /\ ".." / "dist" # FIXME: should not be hard-coded dist_dir = dist_dir.abspath() client_dir = dist_dir / "lib" / "client" logback = dist_dir / "etc" / "logback-cli.xml" self.args += ["--clientdir", client_dir] self.args += ["--logback", logback] def mkdir(self, parent, name, with_ds_store=False): child = parent / name child.mkdir() if with_ds_store: ds_store = child / ".DS_STORE" ds_store.write("") return child def mkfakescreen(self, screen_dir, nplates=2, nruns=2, nwells=2, nfields=4, with_ds_store=False): fieldfiles = [] for iplate in range(nplates): plate_dir = self.mkdir(screen_dir, "Plate00%s" % str(iplate), with_ds_store=with_ds_store) for irun in range(nruns): run_dir = self.mkdir(plate_dir, "Run00%s" % str(irun), with_ds_store=with_ds_store) for iwell in range(nwells): well_dir = self.mkdir(run_dir, "WellA00%s" % str(iwell), with_ds_store=with_ds_store) for ifield in range(nfields): fieldfile = (well_dir / ("Field00%s.fake" % str(ifield))) fieldfile.write('') fieldfiles.append(fieldfile) return fieldfiles def mkfakepattern(self, tmpdir, nangles=7, ntimepoints=10): spim_dir = tmpdir.join("SPIM") spim_dir.mkdir() tiffiles = [] for angle in range(1, nangles + 1): for timepoint in range(1, ntimepoints + 1): tiffile = (spim_dir / ("spim_TL%s_Angle%s.fake" % (str(timepoint), str(angle)))) tiffile.write('') print str(tiffile) tiffiles.append(tiffile) patternfile = spim_dir / "spim.pattern" patternfile.write("spim_TL<1-%s>_Angle<1-%s>.fake" % (str(ntimepoints), str(nangles))) assert len(tiffiles) == nangles * ntimepoints return patternfile, tiffiles def testDropBoxArgs(self): class MockImportControl(ImportControl): def importer(this, args): assert args.server == "localhost" assert args.port == "4064" assert args.key == "b0742975-03a1-4f6d-b0ac-639943f1a147" assert args.errs == "/tmp/dropbox.err" assert args.file == "/tmp/dropbox.out" self.cli.register("mock-import", MockImportControl, "HELP") self.args = [ '-s', 'localhost', '-p', '4064', '-k', 'b0742975-03a1-4f6d-b0ac-639943f1a147' ] self.args += ['mock-import', '---errs=/tmp/dropbox.err'] self.args += ['---file=/tmp/dropbox.out'] self.args += ['--', '/OMERO/DropBox/root/test.fake'] self.cli.invoke(self.args) @pytest.mark.parametrize('help_argument', help_arguments) def testHelp(self, help_argument): """Test help arguments""" self.args += [help_argument] self.cli.invoke(self.args) @pytest.mark.parametrize('clientdir_exists', [True, False]) def testImportNoClientDirFails(self, tmpdir, clientdir_exists): """Test fake screen import""" fakefile = tmpdir.join("test.fake") fakefile.write('') if clientdir_exists: self.args += ["--clientdir", str(tmpdir)] self.args += [str(fakefile)] with pytest.raises(NonZeroReturnCode): self.cli.invoke(self.args, strict=True) @pytest.mark.parametrize("data", (("1", False), ("3", True))) def testImportDepth(self, tmpdir, capfd, data): """Test import using depth argument""" dir1 = tmpdir.join("a") dir1.mkdir() dir2 = dir1 / "b" dir2.mkdir() fakefile = dir2 / "test.fake" fakefile.write('') self.add_client_dir() self.args += ["-f", "--debug=ERROR"] self.args += [str(dir1)] def f(): self.cli.invoke(self.args + ["--depth=%s" % depth], strict=True) depth, result = data if result: f() o, e = capfd.readouterr() assert str(fakefile) in str(o) else: # Now a failure condition with pytest.raises(NonZeroReturnCode): f() def testImportFakeImage(self, tmpdir, capfd): """Test fake image import""" fakefile = tmpdir.join("test.fake") fakefile.write('') self.add_client_dir() self.args += ["-f", "--debug=ERROR"] self.args += [str(fakefile)] self.cli.invoke(self.args, strict=True) o, e = capfd.readouterr() outputlines = str(o).split('\n') reader = 'loci.formats.in.FakeReader' assert outputlines[-2] == str(fakefile) assert outputlines[-3] == \ "# Group: %s SPW: false Reader: %s" % (str(fakefile), reader) @pytest.mark.parametrize('params', ( ("-l", "only_fakes.txt", True), ("-l", "no_fakes.txt", False), ("--readers", "only_fakes.txt", True), ("--readers", "no_fakes.txt", False), )) def testImportReaders(self, tmpdir, capfd, params): """Test fake image import""" fakefile = tmpdir.join("test.fake") fakefile.write('') flag, filename, status = params filename = path(__file__).parent / "readers" / filename self.add_client_dir() self.args += ["-f", flag, filename] self.args += [str(fakefile)] if status: self.cli.invoke(self.args, strict=True) o, e = capfd.readouterr() outputlines = str(o).split('\n') reader = 'loci.formats.in.FakeReader' assert outputlines[-2] == str(fakefile) assert outputlines[-3] == \ "# Group: %s SPW: false Reader: %s" % (str(fakefile), reader) else: with pytest.raises(NonZeroReturnCode): self.cli.invoke(self.args, strict=True) o, e = capfd.readouterr() assert "parsed into 0 group" in e @pytest.mark.parametrize('with_ds_store', (True, False)) def testImportFakeScreen(self, tmpdir, capfd, with_ds_store): """Test fake screen import""" screen_dir = tmpdir.join("screen.fake") screen_dir.mkdir() fieldfiles = self.mkfakescreen(screen_dir, with_ds_store=with_ds_store) self.add_client_dir() self.args += ["-f", "--debug=ERROR"] self.args += [str(fieldfiles[0])] self.cli.invoke(self.args, strict=True) o, e = capfd.readouterr() outputlines = str(o).split('\n') reader = 'loci.formats.in.FakeReader' assert outputlines[-len(fieldfiles)-2] == \ "# Group: %s SPW: true Reader: %s" % (str(fieldfiles[0]), reader) for i in range(len(fieldfiles)): assert outputlines[-1 - len(fieldfiles) + i] == str(fieldfiles[i]) def testImportPattern(self, tmpdir, capfd): """Test pattern import""" patternfile, tiffiles = self.mkfakepattern(tmpdir) self.add_client_dir() self.args += ["-f", "--debug=ERROR"] self.args += [str(patternfile)] self.cli.invoke(self.args, strict=True) o, e = capfd.readouterr() outputlines = str(o).split('\n') reader = 'loci.formats.in.FilePatternReader' print o assert outputlines[-len(tiffiles)-3] == \ "# Group: %s SPW: false Reader: %s" % (str(patternfile), reader) assert outputlines[-len(tiffiles) - 2] == str(patternfile) for i in range(len(tiffiles)): assert outputlines[-1 - len(tiffiles) + i] == str(tiffiles[i]) @pytest.mark.parametrize('hostname', ['localhost', 'servername']) @pytest.mark.parametrize('port', [None, 4064, 14064]) def testLoginArguments(self, monkeypatch, hostname, port, tmpdir): self.args += ['test.fake'] sessionid = str(uuid.uuid4()) def new_client(x): if port: c = MockClient(hostname, port) else: c = MockClient(hostname) c.setSessionId(sessionid) return c monkeypatch.setattr(self.cli, 'conn', new_client) ice_config = tmpdir / 'ice.config' ice_config.write('omero.host=%s\nomero.port=%g' % (hostname, (port or 4064))) monkeypatch.setenv("ICE_CONFIG", ice_config) args = self.cli.parser.parse_args(self.args) command_args = CommandArguments(self.cli, args) expected_args = ['-s', '%s' % hostname] expected_args += ['-p', '%s' % (port or 4064)] expected_args += ['-k', '%s' % sessionid] expected_args += ['test.fake'] assert command_args.java_args() == expected_args def testLogPrefix(self, tmpdir, capfd): fakefile = tmpdir.join("test.fake") fakefile.write('') prefix = tmpdir.join("log") self.add_client_dir() self.args += [ "-f", "---logprefix=%s" % prefix, "---file=out", "---errs=errs" ] self.args += [str(fakefile)] self.cli.invoke(self.args, strict=True) o, e = capfd.readouterr() assert o == "" assert e == "" outlines = prefix.join("out").read().split("\n") reader = 'loci.formats.in.FakeReader' assert outlines[-2] == str(fakefile) assert outlines[-3] == \ "# Group: %s SPW: false Reader: %s" % (str(fakefile), reader) def testYamlOutput(self, tmpdir, capfd): import yaml from StringIO import StringIO fakefile = tmpdir.join("test.fake") fakefile.write('') self.add_client_dir() self.args += ["-f", "--output=yaml", str(fakefile)] self.cli.invoke(self.args, strict=True) o, e = capfd.readouterr() result = yaml.load(StringIO(o)) result = result[0] assert "fake" in result["group"] assert 1 == len(result["files"]) assert "reader" in result assert "spw" in result def testBulkNoPaths(self): t = path(__file__) / "bulk_import" / "test_simple" b = t / "bulk.yml" self.add_client_dir() self.args += ["-f", "---bulk=%s" % b, "dne.fake"] with pytest.raises(NonZeroReturnCode): self.cli.invoke(self.args, strict=True) def testBulkSimple(self): t = path(__file__).parent / "bulk_import" / "test_simple" b = t / "bulk.yml" self.add_client_dir() self.args += ["-f", "---bulk=%s" % b] self.cli.invoke(self.args, strict=True) def testBulkInclude(self): t = path(__file__).parent / "bulk_import" / "test_include" / "inner" b = t / "bulk.yml" self.add_client_dir() self.args += ["-f", "---bulk=%s" % b] self.cli.invoke(self.args, strict=True) def testBulkName(self): # Metadata provided in the yml file will be applied # to the args t = path(__file__).parent / "bulk_import" / "test_name" b = t / "bulk.yml" class MockImportControl(ImportControl): def do_import(self, command_args, xargs): assert "--name=testname" in command_args.java_args() self.cli.register("mock-import", MockImportControl, "HELP") self.args = ["mock-import", "-f", "---bulk=%s" % b] self.add_client_dir() self.cli.invoke(self.args, strict=True) def testBulkCols(self): # Metadata provided about the individual columns in # the tsv will be used. t = path(__file__).parent / "bulk_import" / "test_cols" b = t / "bulk.yml" class MockImportControl(ImportControl): def do_import(self, command_args, xargs): cmd = command_args.java_args() assert "--name=meta_one" in cmd or \ "--name=meta_two" in cmd self.cli.register("mock-import", MockImportControl, "HELP") self.args = ["mock-import", "-f", "---bulk=%s" % b] self.add_client_dir() self.cli.invoke(self.args, strict=True) def testBulkBad(self): t = path(__file__).parent / "bulk_import" / "test_bad" b = t / "bulk.yml" self.add_client_dir() self.args += ["-f", "---bulk=%s" % b] with pytest.raises(NonZeroReturnCode): self.cli.invoke(self.args, strict=True) def testBulkDry(self, capfd): t = path(__file__).parent / "bulk_import" / "test_dryrun" b = t / "bulk.yml" self.add_client_dir() self.args += ["-f", "---bulk=%s" % b] self.cli.invoke(self.args, strict=True) o, e = capfd.readouterr() assert o == '"--name=no-op" "1.fake"\n' def testBulkJavaArgs(self): """Test Java arguments""" t = path(__file__).parent / "bulk_import" / "test_javaargs" b = t / "bulk.yml" class MockImportControl(ImportControl): def do_import(self, command_args, xargs): assert ("--checksum-algorithm=File-Size-64" in command_args.java_args()) assert "--parallel-upload=10" in command_args.java_args() assert "--parallel-fileset=5" in command_args.java_args() assert "--transfer=ln_s" in command_args.java_args() assert "--exclude=clientpath" in command_args.java_args() self.cli.register("mock-import", MockImportControl, "HELP") self.args = ["mock-import", "-f", "---bulk=%s" % b] self.add_client_dir() self.cli.invoke(self.args, strict=True) @pytest.mark.parametrize('skip', plugin.SKIP_CHOICES) def testBulkSkip(self, skip): """Test skip arguments""" t = path(__file__).parent / "bulk_import" / "test_skip" b = t / "%s.yml" % skip class MockImportControl(ImportControl): def do_import(self, command_args, xargs): if skip in ["all", "checksum"]: assert ("--checksum-algorithm=File-Size-64" in command_args.java_args()) if skip in ["all", "minmax"]: assert ("--no-stats-info" in command_args.java_args()) if skip in ["all", "thumbnails"]: assert ("--no-thumbnails" in command_args.java_args()) if skip in ["all", "upgrade"]: assert ("--no-upgrade-check" in command_args.java_args()) self.cli.register("mock-import", MockImportControl, "HELP") self.args = ["mock-import", "-f", "---bulk=%s" % b] self.add_client_dir() self.cli.invoke(self.args, strict=True)
class TestDatabase(object): def setup_method(self, method): self.cli = CLI() self.cli.register("db", DatabaseControl, "TEST") dir = path(__file__) / ".." / ".." / ".." / ".." / ".." / ".." /\ ".." / "dist" # FIXME: should not be hard-coded dir = dir.abspath() cfg = dir / "etc" / "omero.properties" cfg = cfg.abspath() self.cli.dir = dir self.data = {} for line in cfg.text().split("\n"): line = line.strip() for x in ("version", "patch"): key = "omero.db." + x if line.startswith(key): self.data[x] = line[len(key)+1:] self.file = create_path() self.script_file = "%(version)s__%(patch)s.sql" % self.data if os.path.isfile(self.script_file): os.rename(self.script_file, self.script_file + '.bak') assert not os.path.isfile(self.script_file) self.mox = Mox() self.mox.StubOutWithMock(getpass, 'getpass') self.mox.StubOutWithMock(__builtin__, "raw_input") def teardown_method(self, method): self.file.remove() if os.path.isfile(self.script_file): os.remove(self.script_file) if os.path.isfile(self.script_file + '.bak'): os.rename(self.script_file + '.bak', self.script_file) self.mox.UnsetStubs() self.mox.VerifyAll() def password(self, string, strict=True): self.cli.invoke("db password " + string % self.data, strict=strict) def testBadVersionDies(self): with pytest.raises(NonZeroReturnCode): self.cli.invoke("db script NONE NONE pw", strict=True) def testPasswordIsAskedForAgainIfDiffer(self): self.expectPassword("ome") self.expectConfirmation("bad") self.expectPassword("ome") self.expectConfirmation("ome") self.mox.ReplayAll() self.password("") def testPasswordIsAskedForAgainIfEmpty(self): self.expectPassword("") self.expectPassword("ome") self.expectConfirmation("ome") self.mox.ReplayAll() self.password("") @pytest.mark.parametrize('no_salt', ['', '--no-salt']) @pytest.mark.parametrize('user_id', ['', '0', '1']) @pytest.mark.parametrize('password', ['', 'ome']) def testPassword(self, user_id, password, no_salt, capsys): args = "" if user_id: args += "--user-id=%s " % user_id if no_salt: args += "%s " % no_salt if password: args += "%s" % password else: self.expectPassword("ome", id=user_id) self.expectConfirmation("ome", id=user_id) self.mox.ReplayAll() self.password(args) out, err = capsys.readouterr() assert out.strip() == self.password_output(user_id, no_salt) @pytest.mark.parametrize('file_arg', ['', '-f', '--file']) @pytest.mark.parametrize('no_salt', ['', '--no-salt']) @pytest.mark.parametrize( 'pos_input', ["", "%(version)s", "%(version)s %(patch)s", "%(version)s %(patch)s ome"]) def testScript(self, pos_input, no_salt, file_arg): args = "db script " args += pos_input if no_salt: args += " %s" % no_salt if file_arg: args += " %s %s" % (file_arg, str(self.file)) output = self.file else: output = self.script_file if "version" not in pos_input or "patch" not in pos_input: self.expectVersion(self.data["version"]) self.expectPatch(self.data["patch"]) if "ome" not in pos_input: self.expectPassword("ome") self.expectConfirmation("ome") self.mox.ReplayAll() self.cli.invoke(args % self.data, strict=True) with open(output) as f: lines = f.readlines() for line in lines: if line.startswith('insert into password values (0'): assert line.strip() == self.script_output(no_salt) def password_ending(self, user, id): if id and id != '0': rv = "user %s: " % id else: rv = "%s user: "******"password for OMERO " + rv def expectPassword(self, pw, user="******", id=None): getpass.getpass("Please enter %s" % self.password_ending(user, id)).AndReturn(pw) def expectConfirmation(self, pw, user="******", id=None): getpass.getpass("Please re-enter %s" % self.password_ending(user, id)).AndReturn(pw) def expectVersion(self, version): raw_input("Please enter omero.db.version [%s]: " % self.data["version"]).AndReturn(version) def expectPatch(self, patch): raw_input("Please enter omero.db.patch [%s]: " % self.data["patch"]).AndReturn(patch) def password_output(self, user_id, no_salt): update_msg = "UPDATE password SET hash = \'%s\'" \ " WHERE experimenter_id = %s;" if not user_id: user_id = "0" return update_msg % (hash_map[(user_id, no_salt)], user_id) def script_output(self, no_salt): root_password_msg = "insert into password values (0,\'%s\');" return root_password_msg % (hash_map[("0", no_salt)])
""" m = RE.match(line) if m: return self._complete_file(RE.sub('', line)) else: return BaseControl._complete(self, text, line, begidx, endidx) def _configure(self, parser): parser.add_argument("--pytable", action="store_true", help="If set, the following files are interpreted as pytable files" ) parser.add_argument("file", nargs="+") parser.set_defaults(func=self.upload) def upload(self, args): client = self.ctx.conn(args) for file in args.file: is_importer, omero_format = omero.util.originalfileutils.getFormat(file) if (is_importer == omero.util.originalfileutils.IMPORTER): self.ctx.dir(493, "This file should be imported using omero import") else: obj = client.upload(file, type=omero_format) self.ctx.out("Uploaded %s as " % file + str(obj.id.val)) self.ctx.set("last.upload.id", obj.id.val) try: register("upload", UploadControl, HELP) except NameError: if __name__ == "__main__": cli = CLI() cli.register("upload", UploadControl, HELP) cli.invoke(sys.argv[1:])
class TestDatabase(object): def setup_method(self, method): self.cli = CLI() self.cli.register("db", DatabaseControl, "TEST") dir = path(__file__) / ".." / ".." / ".." / ".." / ".." / ".." /\ ".." / "dist" # FIXME: should not be hard-coded dir = dir.abspath() cfg = dir / "etc" / "omero.properties" cfg = cfg.abspath() self.cli.dir = dir self.data = {} for line in cfg.text().split("\n"): line = line.strip() for x in ("version", "patch"): key = "omero.db." + x if line.startswith(key): self.data[x] = line[len(key) + 1:] self.file = create_path() self.script_file = "%(version)s__%(patch)s.sql" % self.data if os.path.isfile(self.script_file): os.rename(self.script_file, self.script_file + '.bak') assert not os.path.isfile(self.script_file) self.mox = Mox() self.mox.StubOutWithMock(getpass, 'getpass') self.mox.StubOutWithMock(__builtin__, "raw_input") def teardown_method(self, method): self.file.remove() if os.path.isfile(self.script_file): os.remove(self.script_file) if os.path.isfile(self.script_file + '.bak'): os.rename(self.script_file + '.bak', self.script_file) self.mox.UnsetStubs() self.mox.VerifyAll() def password(self, string, strict=True): self.cli.invoke("db password " + string % self.data, strict=strict) def testBadVersionDies(self): with pytest.raises(NonZeroReturnCode): self.cli.invoke("db script NONE NONE pw", strict=True) def testPasswordIsAskedForAgainIfDiffer(self): self.expectPassword("ome") self.expectConfirmation("bad") self.expectPassword("ome") self.expectConfirmation("ome") self.mox.ReplayAll() self.password("") def testPasswordIsAskedForAgainIfEmpty(self): self.expectPassword("") self.expectPassword("ome") self.expectConfirmation("ome") self.mox.ReplayAll() self.password("") @pytest.mark.parametrize('no_salt', ['', '--no-salt']) @pytest.mark.parametrize('user_id', ['', '0', '1']) @pytest.mark.parametrize('password', ['', 'ome']) def testPassword(self, user_id, password, no_salt, capsys): args = "" if user_id: args += "--user-id=%s " % user_id if no_salt: args += "%s " % no_salt if password: args += "%s" % password else: self.expectPassword("ome", id=user_id) self.expectConfirmation("ome", id=user_id) self.mox.ReplayAll() self.password(args) out, err = capsys.readouterr() assert out.strip() == self.password_output(user_id, no_salt) @pytest.mark.parametrize('file_arg', ['', '-f', '--file']) @pytest.mark.parametrize('no_salt', ['', '--no-salt']) @pytest.mark.parametrize('pos_input', [ "", "%(version)s", "%(version)s %(patch)s", "%(version)s %(patch)s ome" ]) def testScript(self, pos_input, no_salt, file_arg): args = "db script " args += pos_input if no_salt: args += " %s" % no_salt if file_arg: args += " %s %s" % (file_arg, str(self.file)) output = self.file else: output = self.script_file if "version" not in pos_input or "patch" not in pos_input: self.expectVersion(self.data["version"]) self.expectPatch(self.data["patch"]) if "ome" not in pos_input: self.expectPassword("ome") self.expectConfirmation("ome") self.mox.ReplayAll() self.cli.invoke(args % self.data, strict=True) with open(output) as f: lines = f.readlines() for line in lines: if line.startswith('insert into password values (0'): assert line.strip() == self.script_output(no_salt) def password_ending(self, user, id): if id and id != '0': rv = "user %s: " % id else: rv = "%s user: "******"password for OMERO " + rv def expectPassword(self, pw, user="******", id=None): getpass.getpass("Please enter %s" % self.password_ending(user, id)).AndReturn(pw) def expectConfirmation(self, pw, user="******", id=None): getpass.getpass("Please re-enter %s" % self.password_ending(user, id)).AndReturn(pw) def expectVersion(self, version): raw_input("Please enter omero.db.version [%s]: " % self.data["version"]).AndReturn(version) def expectPatch(self, patch): raw_input("Please enter omero.db.patch [%s]: " % self.data["patch"]).AndReturn(patch) def password_output(self, user_id, no_salt): update_msg = "UPDATE password SET hash = \'%s\'" \ " WHERE experimenter_id = %s;" if not user_id: user_id = "0" return update_msg % (hash_map[(user_id, no_salt)], user_id) def script_output(self, no_salt): root_password_msg = "insert into password values (0,\'%s\');" return root_password_msg % (hash_map[("0", no_salt)])
class TestSessions(object): def setup_method(self, method): self.cli = CLI() self.cli.register("sessions", SessionsControl, "TEST") self.args = ["sessions"] def testHelp(self): self.args += ["-h"] self.cli.invoke(self.args, strict=True) @pytest.mark.parametrize( "subcommand", SessionsControl().get_subcommands()) def testSubcommandHelp(self, subcommand): self.args += [subcommand, "-h"] self.cli.invoke(self.args, strict=True) def testDefaultSessionsDir(self): from omero.util import get_user_dir from path import path # Default store sessions dir is under user dir store = self.cli.controls['sessions'].store(None) assert store.dir == path(get_user_dir()) / 'omero' / 'sessions' @pytest.mark.parametrize('environment', ( {'OMERO_USERDIR': None, 'OMERO_SESSION_DIR': None, 'OMERO_SESSIONDIR': None}, {'OMERO_USERDIR': None, 'OMERO_SESSION_DIR': 'session_dir', 'OMERO_SESSIONDIR': None}, {'OMERO_USERDIR': None, 'OMERO_SESSION_DIR': None, 'OMERO_SESSIONDIR': 'sessiondir'}, {'OMERO_USERDIR': 'userdir', 'OMERO_SESSION_DIR': None, 'OMERO_SESSIONDIR': None}, {'OMERO_USERDIR': None, 'OMERO_SESSION_DIR': 'session_dir', 'OMERO_SESSIONDIR': 'sessiondir'}, {'OMERO_USERDIR': 'userdir', 'OMERO_SESSION_DIR': 'session_dir', 'OMERO_SESSIONDIR': None}, {'OMERO_USERDIR': 'userdir', 'OMERO_SESSION_DIR': None, 'OMERO_SESSIONDIR': 'sessiondir'}, {'OMERO_USERDIR': 'userdir', 'OMERO_SESSION_DIR': 'session_dir', 'OMERO_SESSIONDIR': 'sessiondir'})) @pytest.mark.parametrize('session_args', [None, 'session_dir']) def testCustomSessionsDir( self, tmpdir, monkeypatch, environment, session_args): from argparse import Namespace from omero.util import get_user_dir from path import path for var in environment.keys(): if environment[var]: monkeypatch.setenv(var, tmpdir / environment.get(var)) else: monkeypatch.delenv(var, raising=False) # args.session_dir sets the sessions dir args = Namespace() if session_args: setattr(args, session_args, tmpdir / session_args) if environment.get('OMERO_SESSION_DIR') or session_args: pytest.deprecated_call(self.cli.controls['sessions'].store, args) store = self.cli.controls['sessions'].store(args) # By order of precedence if environment.get('OMERO_SESSIONDIR'): sdir = path(tmpdir) / environment.get('OMERO_SESSIONDIR') elif environment.get('OMERO_SESSION_DIR'): sdir = (path(tmpdir) / environment.get('OMERO_SESSION_DIR') / 'omero' / 'sessions') elif session_args: sdir = path(getattr(args, session_args)) / 'omero' / 'sessions' elif environment.get('OMERO_USERDIR'): sdir = path(tmpdir) / environment.get('OMERO_USERDIR') / 'sessions' else: sdir = path(get_user_dir()) / 'omero' / 'sessions' assert store.dir == sdir
class TestPrefs(object): def setup_method(self, method): self.cli = CLI() self.cli.register("config", PrefsControl, HELP) self.p = create_path() self.args = ["config", "--source", "%s" % self.p] def config(self): return ConfigXml(filename=str(self.p)) def assertStdoutStderr(self, capsys, out='', err=''): o, e = capsys.readouterr() assert o.strip() == out assert e.strip() == err def invoke(self, s): self.cli.invoke(self.args + s.split(), strict=True) def testHelp(self): self.invoke("-h") assert 0 == self.cli.rv @pytest.mark.parametrize('subcommand', subcommands) def testSubcommandHelp(self, subcommand): self.invoke("%s -h" % subcommand) assert 0 == self.cli.rv def testAll(self, capsys): config = self.config() config.default("test") config.close() self.invoke("all") self.assertStdoutStderr(capsys, out="test\ndefault") def testDefaultInitial(self, capsys): self.invoke("def") self.assertStdoutStderr(capsys, out="default") def testDefaultEnvironment(self, capsys, monkeypatch): monkeypatch.setenv("OMERO_CONFIG", "testDefaultEnvironment") self.invoke("def") self.assertStdoutStderr(capsys, out="testDefaultEnvironment") def testDefaultSet(self, capsys): self.invoke("def x") self.assertStdoutStderr(capsys, out="x") self.invoke("def") self.assertStdoutStderr(capsys, out="x") def testGetSet(self, capsys): self.invoke("get X") self.assertStdoutStderr(capsys) self.invoke("set A B") self.assertStdoutStderr(capsys) self.invoke("get A") self.assertStdoutStderr(capsys, out='B') self.invoke("get") self.assertStdoutStderr(capsys, out='A=B') self.invoke("set A") self.assertStdoutStderr(capsys) self.invoke("keys") self.assertStdoutStderr(capsys) def testSetFails(self, capsys): self.invoke("set A=B") self.assertStdoutStderr( capsys, err="\"=\" in key name. Did you mean \"...set A B\"?") def testKeys(self, capsys): self.invoke("keys") self.assertStdoutStderr(capsys) self.invoke("set A B") self.assertStdoutStderr(capsys) self.invoke("keys") self.assertStdoutStderr(capsys, out="A") def testVersion(self, capsys): self.invoke("version") self.assertStdoutStderr(capsys, out=ConfigXml.VERSION) def testPath(self, capsys): self.invoke("path") self.assertStdoutStderr(capsys, out=self.p) def testLoad(self, capsys): to_load = create_path() to_load.write_text("A=B") self.invoke("load %s" % to_load) self.assertStdoutStderr(capsys) self.invoke("get") self.assertStdoutStderr(capsys, out="A=B") # Same property/value pairs should pass self.invoke("load %s" % to_load) to_load.write_text("A=C") with pytest.raises(NonZeroReturnCode): # Different property/value pair should fail self.invoke("load %s" % to_load) self.assertStdoutStderr( capsys, err="Duplicate property: A ('B' => 'C')") # Quiet load self.invoke("load -q %s" % to_load) self.assertStdoutStderr(capsys) self.invoke("get") self.assertStdoutStderr(capsys, out="A=C") def testLoadDoesNotExist(self): # ticket:7273 pytest.raises(NonZeroReturnCode, self.invoke, "load THIS_FILE_SHOULD_NOT_EXIST") def testLoadMultiLine(self, capsys): to_load = create_path() to_load.write_text("A=B\\\nC") self.invoke("load %s" % to_load) self.invoke("get") self.assertStdoutStderr(capsys, out="A=BC") def testSetFromFile(self, capsys): to_load = create_path() to_load.write_text("Test") self.invoke("set -f %s A" % to_load) self.invoke("get") self.assertStdoutStderr(capsys, out="A=Test") def testDrop(self, capsys): self.invoke("def x") self.assertStdoutStderr(capsys, out="x") self.invoke("def") self.assertStdoutStderr(capsys, out="x") self.invoke("all") self.assertStdoutStderr(capsys, out="x\ndefault") self.invoke("def y") self.assertStdoutStderr(capsys, out="y") self.invoke("all") self.assertStdoutStderr(capsys, out="y\nx\ndefault") self.invoke("drop x") self.assertStdoutStderr(capsys) self.invoke("all") self.assertStdoutStderr(capsys, 'y\ndefault') def testDropFails(self, capsys): self.invoke("drop x") self.assertStdoutStderr(capsys, err="Unknown configuration: x") def testEdit(self): """ Testing edit is a bit more complex since it wants to start another process. Rather than using invoke, we manage things ourselves here. """ def fake_edit_path(tmp_file, tmp_text): pass args = self.cli.parser.parse_args("config edit".split()) control = self.cli.controls["config"] config = self.config() try: control.edit(args, config, fake_edit_path) finally: config.close() def testNewEnvironment(self, capsys, monkeypatch): config = self.config() config.default("default") config.close() monkeypatch.setenv("OMERO_CONFIG", "testNewEnvironment") self.invoke("set A B") self.assertStdoutStderr(capsys) self.invoke("get") self.assertStdoutStderr(capsys, out="A=B") @pytest.mark.parametrize( ('initval', 'newval'), [('1', '2'), ('\"1\"', '\"2\"'), ('test', 'test')]) def testAppendFails(self, initval, newval): self.invoke("set A %s" % initval) with pytest.raises(NonZeroReturnCode): self.invoke("append A %s" % newval) def testRemoveUnsetPropertyFails(self): with pytest.raises(NonZeroReturnCode): self.invoke("remove A x") @pytest.mark.parametrize( ('initval', 'newval'), [('1', '1'), ('[\"1\"]', '1'), ('[1]', '\"1\"')]) def testRemoveFails(self, initval, newval): self.invoke("set A %s" % initval) with pytest.raises(NonZeroReturnCode): self.invoke("remove A %s" % newval) def testAppendRemove(self, capsys): self.invoke("append A 1") self.invoke("get A") self.assertStdoutStderr(capsys, out='[1]') self.invoke("append A \"y\"") self.invoke("get A") self.assertStdoutStderr(capsys, out='[1, "y"]') self.invoke("remove A \"y\"") self.invoke("get A") self.assertStdoutStderr(capsys, out='[1]') self.invoke("remove A 1") self.invoke("get A") self.assertStdoutStderr(capsys, out='[]') def testRemoveIdenticalValues(self, capsys): self.invoke("set A [1,1]") self.invoke("remove A 1") self.invoke("get A") self.assertStdoutStderr(capsys, out='[1]') self.invoke("remove A 1") self.invoke("get A") self.assertStdoutStderr(capsys, out='[]') @pytest.mark.usefixtures('configxml') def testAppendWithDefault(self, monkeypatch, capsys): import json monkeypatch.setattr("omeroweb.settings.CUSTOM_SETTINGS_MAPPINGS", { "omero.web.test": ["TEST", "[1,2,3]", json.loads], "omero.web.notalist": ["NOTALIST", "abc", str], }) self.invoke("append omero.web.test 4") self.invoke("get omero.web.test") self.assertStdoutStderr(capsys, out='[1, 2, 3, 4]') self.invoke("append omero.web.unknown 1") self.invoke("get omero.web.unknown") self.assertStdoutStderr(capsys, out='[1]') with pytest.raises(NonZeroReturnCode): self.invoke("append omero.web.notalist 1") @pytest.mark.usefixtures('configxml') def testRemoveWithDefault(self, monkeypatch, capsys): import json monkeypatch.setattr("omeroweb.settings.CUSTOM_SETTINGS_MAPPINGS", { "omero.web.test": ["TEST", "[1,2,3]", json.loads], }) self.invoke("remove omero.web.test 2") self.invoke("get omero.web.test") self.assertStdoutStderr(capsys, out='[1, 3]') self.invoke("remove omero.web.test 1") self.invoke("remove omero.web.test 3") self.invoke("get omero.web.test") self.assertStdoutStderr(capsys, out='[]')
class TestPrefs(object): def setup_method(self, method): self.cli = CLI() self.cli.register("config", PrefsControl, HELP) self.p = create_path() self.args = ["config", "--source", "%s" % self.p] def config(self): return ConfigXml(filename=str(self.p)) def assertStdoutStderr(self, capsys, out='', err=''): o, e = capsys.readouterr() assert (o.strip() == out and e.strip() == err) def invoke(self, s): self.cli.invoke(self.args + s.split(), strict=True) def testHelp(self): self.invoke("-h") assert 0 == self.cli.rv @pytest.mark.parametrize('subcommand', PrefsControl().get_subcommands()) def testSubcommandHelp(self, subcommand): self.invoke("%s -h" % subcommand) assert 0 == self.cli.rv def testAll(self, capsys): config = self.config() config.default("test") config.close() self.invoke("all") self.assertStdoutStderr(capsys, out="test\ndefault") def testDefaultInitial(self, capsys): self.invoke("def") self.assertStdoutStderr(capsys, out="default") def testDefaultEnvironment(self, capsys, monkeypatch): monkeypatch.setenv("OMERO_CONFIG", "testDefaultEnvironment") self.invoke("def") self.assertStdoutStderr(capsys, out="testDefaultEnvironment") def testDefaultSet(self, capsys): self.invoke("def x") self.assertStdoutStderr(capsys, out="x") self.invoke("def") self.assertStdoutStderr(capsys, out="x") def testGetSet(self, capsys): self.invoke("get X") self.assertStdoutStderr(capsys) self.invoke("set A B") self.assertStdoutStderr(capsys) self.invoke("get A") self.assertStdoutStderr(capsys, out='B') self.invoke("get") self.assertStdoutStderr(capsys, out='A=B') self.invoke("set A") self.assertStdoutStderr(capsys) self.invoke("keys") self.assertStdoutStderr(capsys) def testGetHidePassword(self, capsys): self.invoke("set omero.X.pass shortpass") self.cli.invoke(self.args + ["set", "omero.Y.pass", ""], strict=True) self.invoke("set omero.Y.password long_password") self.invoke("set omero.Z val") self.invoke("get") self.assertStdoutStderr(capsys, out=( 'omero.X.pass=shortpass\n' 'omero.Y.pass=\n' 'omero.Y.password=long_password\n' 'omero.Z=val')) self.invoke("get --hide-password") self.assertStdoutStderr(capsys, out=( 'omero.X.pass=********\n' 'omero.Y.pass=\n' 'omero.Y.password=********\n' 'omero.Z=val')) @pytest.mark.parametrize('argument', ['A=B', 'A= B']) def testSetFails(self, capsys, argument): self.invoke("set %s" % argument) self.assertStdoutStderr( capsys, err="\"=\" in key name. Did you mean \"...set A B\"?") def testKeys(self, capsys): self.invoke("keys") self.assertStdoutStderr(capsys) self.invoke("set A B") self.assertStdoutStderr(capsys) self.invoke("keys") self.assertStdoutStderr(capsys, out="A") def testVersion(self, capsys): self.invoke("version") self.assertStdoutStderr(capsys, out=ConfigXml.VERSION) def testPath(self, capsys): self.invoke("path") self.assertStdoutStderr(capsys, out=self.p) def testLoad(self, capsys): to_load = create_path() to_load.write_text("A=B") self.invoke("load %s" % to_load) self.assertStdoutStderr(capsys) self.invoke("get") self.assertStdoutStderr(capsys, out="A=B") # Same property/value pairs should pass self.invoke("load %s" % to_load) to_load.write_text("A=C") with pytest.raises(NonZeroReturnCode): # Different property/value pair should fail self.invoke("load %s" % to_load) self.assertStdoutStderr( capsys, err="Duplicate property: A ('B' => 'C')") # Quiet load self.invoke("load -q %s" % to_load) self.assertStdoutStderr(capsys) self.invoke("get") self.assertStdoutStderr(capsys, out="A=C") def testLoadDoesNotExist(self): # ticket:7273 pytest.raises(NonZeroReturnCode, self.invoke, "load THIS_FILE_SHOULD_NOT_EXIST") def testLoadMultiLine(self, capsys): to_load = create_path() to_load.write_text("A=B\\\nC") self.invoke("load %s" % to_load) self.invoke("get") self.assertStdoutStderr(capsys, out="A=BC") def testSetFromFile(self, capsys): to_load = create_path() to_load.write_text("Test") self.invoke("set -f %s A" % to_load) self.invoke("get") self.assertStdoutStderr(capsys, out="A=Test") def testDrop(self, capsys): self.invoke("def x") self.assertStdoutStderr(capsys, out="x") self.invoke("def") self.assertStdoutStderr(capsys, out="x") self.invoke("all") self.assertStdoutStderr(capsys, out="x\ndefault") self.invoke("def y") self.assertStdoutStderr(capsys, out="y") self.invoke("all") self.assertStdoutStderr(capsys, out="y\nx\ndefault") self.invoke("drop x") self.assertStdoutStderr(capsys) self.invoke("all") self.assertStdoutStderr(capsys, 'y\ndefault') def testDropFails(self, capsys): self.invoke("drop x") self.assertStdoutStderr(capsys, err="Unknown configuration: x") def testEdit(self): """ Testing edit is a bit more complex since it wants to start another process. Rather than using invoke, we manage things ourselves here. """ def fake_edit_path(tmp_file, tmp_text): pass args = self.cli.parser.parse_args("config edit".split()) control = self.cli.controls["config"] config = self.config() try: control.edit(args, config, fake_edit_path) finally: config.close() def testNewEnvironment(self, capsys, monkeypatch): config = self.config() config.default("default") config.close() monkeypatch.setenv("OMERO_CONFIG", "testNewEnvironment") self.invoke("set A B") self.assertStdoutStderr(capsys) self.invoke("get") self.assertStdoutStderr(capsys, out="A=B") @pytest.mark.parametrize( ('initval', 'newval'), [('1', '2'), ('\"1\"', '\"2\"'), ('test', 'test')]) def testAppendFails(self, initval, newval): self.invoke("set A %s" % initval) with pytest.raises(NonZeroReturnCode): self.invoke("append A %s" % newval) def testRemoveUnsetPropertyFails(self): with pytest.raises(NonZeroReturnCode): self.invoke("remove A x") @pytest.mark.parametrize( ('initval', 'newval'), [('1', '1'), ('[\"1\"]', '1'), ('[1]', '\"1\"')]) def testRemoveFails(self, initval, newval): self.invoke("set A %s" % initval) with pytest.raises(NonZeroReturnCode): self.invoke("remove A %s" % newval) def testAppendRemove(self, capsys): self.invoke("append A 1") self.invoke("get A") self.assertStdoutStderr(capsys, out='[1]') self.invoke("append A \"y\"") self.invoke("get A") self.assertStdoutStderr(capsys, out='[1, "y"]') self.invoke("remove A \"y\"") self.invoke("get A") self.assertStdoutStderr(capsys, out='[1]') self.invoke("remove A 1") self.invoke("get A") self.assertStdoutStderr(capsys, out='[]') def testRemoveIdenticalValues(self, capsys): self.invoke("set A [1,1]") self.invoke("remove A 1") self.invoke("get A") self.assertStdoutStderr(capsys, out='[1]') self.invoke("remove A 1") self.invoke("get A") self.assertStdoutStderr(capsys, out='[]') @pytest.mark.usefixtures('configxml') def testAppendWithDefault(self, monkeypatch, capsys): import json monkeypatch.setattr("omeroweb.settings.CUSTOM_SETTINGS_MAPPINGS", { "omero.web.test": ["TEST", "[1,2,3]", json.loads], "omero.web.notalist": ["NOTALIST", "abc", str], }) self.invoke("append omero.web.test 4") self.invoke("get omero.web.test") self.assertStdoutStderr(capsys, out='[1, 2, 3, 4]') self.invoke("append omero.web.unknown 1") self.invoke("get omero.web.unknown") self.assertStdoutStderr(capsys, out='[1]') with pytest.raises(NonZeroReturnCode): self.invoke("append omero.web.notalist 1") @pytest.mark.usefixtures('configxml') def testRemoveWithDefault(self, monkeypatch, capsys): import json monkeypatch.setattr("omeroweb.settings.CUSTOM_SETTINGS_MAPPINGS", { "omero.web.test": ["TEST", "[1,2,3]", json.loads], }) self.invoke("remove omero.web.test 2") self.invoke("get omero.web.test") self.assertStdoutStderr(capsys, out='[1, 3]') self.invoke("remove omero.web.test 1") self.invoke("remove omero.web.test 3") self.invoke("get omero.web.test") self.assertStdoutStderr(capsys, out='[]') @pytest.mark.parametrize("data", ( ({}, ""), ({"a": "b"}, "a=b"), ({"a": "b", "a": "d"}, "a=d"), ({"a": "b", "c": "d"}, "a=b\nc=d"), ({"c": "d", "a": "b"}, "a=b\nc=d"), )) @pytest.mark.usefixtures('configxml') def testList(self, data, monkeypatch, capsys): for k, v in data[0].items(): self.invoke("set %s %s" % (k, v)) self.invoke("list") self.assertStdoutStderr(capsys, out=data[1]) @pytest.mark.parametrize("data", ( ("omero.a=b\nomero.c=d\n##ignore=me\n", "omero.a=b\nomero.c=d", "a (1)\n\t\nc (1)"), ("omero.whitelist=\\\nome.foo,\\\nome.bar\n### END", "omero.whitelist=ome.foo,ome.bar", "whitelist (1)"), ("omero.whitelist=\\\nome.foo,\\\nome.bar\n", "omero.whitelist=ome.foo,ome.bar", "whitelist (1)"), ("omero.whitelist=\\\nome.foo,\\\nome.bar", "omero.whitelist=ome.foo,ome.bar", "whitelist (1)"), ("omero.user_mapping=\\\na=b,c=d", "omero.user_mapping=a=b,c=d", "user_mapping (1)"), ("omero.whitelist=ome.foo\nIce.c=d\n", "Ice.c=d\nomero.whitelist=ome.foo", "whitelist (1)"), ("omero.a=b\nomero.c=d\nomero.e=f\n##ignore=me\n", "omero.a=b\nomero.c=d\nomero.e=f", "a (1)\n\t\nc (1)\n\t\ne (1)"), ("omero.a=b\nomero.c=d\nomero.e=f\n##ignore=me\n", "omero.a=b\nomero.c=d\nomero.e=f", "a (1)\n\t\nc (1)\n\t\ne (1)"), )) def testFileParsing(self, tmpdir, capsys, data): input, defaults, keys = data cfg = tmpdir.join("test.cfg") cfg.write(input) self.invoke("parse --file=%s --no-web" % cfg) self.assertStdoutStderr(capsys, out=defaults) self.invoke("parse --file=%s --defaults --no-web" % cfg) self.assertStdoutStderr(capsys, out=defaults) self.invoke("parse --file=%s --keys --no-web" % cfg) self.assertStdoutStderr(capsys, out=keys)
class TestAdminPorts(object): def setup_method(self, method): # # Non-temp directories ctxdir = path() / ".." / ".." / ".." / "dist" etc_dir = ctxdir / "etc" # List configuration files to backup self.cfg_files = {} for f in ['internal.cfg', 'master.cfg', 'ice.config']: self.cfg_files[f] = etc_dir / f for f in ['windefault.xml', 'default.xml', 'config.xml']: self.cfg_files[f] = etc_dir / 'grid' / f # Create temp files for backup tmp_dir = create_path(folder=True) self.tmp_cfg_files = {} for key in self.cfg_files.keys(): if self.cfg_files[key].exists(): self.tmp_cfg_files[key] = tmp_dir / key self.cfg_files[key].copy(self.tmp_cfg_files[key]) else: self.tmp_cfg_files[key] = None # Other setup self.cli = CLI() self.cli.dir = ctxdir self.cli.register("admin", AdminControl, "TEST") self.args = ["admin", "ports"] def teardown_method(self, method): # Restore backups for key in self.cfg_files.keys(): if self.tmp_cfg_files[key] is not None: self.tmp_cfg_files[key].copy(self.cfg_files[key]) else: self.cfg_files[key].remove() def create_new_ice_config(self): with open(self.cfg_files['ice.config'], 'w') as f: f.write('omero.host=localhost') def check_cfg(self, prefix='', registry=4061, **kwargs): for key in ['master.cfg', 'internal.cfg']: s = self.cfg_files[key].text() assert 'tcp -h 127.0.0.1 -p %s%s' % (prefix, registry) in s def check_config_xml(self, prefix='', webserver=4080, ssl=4064, **kwargs): config_text = self.cfg_files["config.xml"].text() serverport_property = ( '<property name="omero.web.application_server.port"' ' value="%s%s"') % (prefix, webserver) serverlist_property = ( '<property name="omero.web.server_list"' ' value="[["localhost", %s%s, "omero"]]"' ) % (prefix, ssl) assert serverport_property in config_text assert serverlist_property in config_text def check_ice_config(self, prefix='', webserver=4080, ssl=4064, **kwargs): config_text = self.cfg_files["ice.config"].text() pattern = re.compile('^omero.port=\d+$', re.MULTILINE) matches = pattern.findall(config_text) assert matches == ["omero.port=%s%s" % (prefix, ssl)] def check_default_xml(self, prefix='', tcp=4063, ssl=4064, **kwargs): routerport = ( '<variable name="ROUTERPORT" value="%s%s"/>' % (prefix, ssl)) insecure_routerport = ( '<variable name="INSECUREROUTER" value="OMERO.Glacier2' '/router:tcp -p %s%s -h @omero.host@"/>' % (prefix, tcp)) client_endpoints = ( 'client-endpoints="ssl -p ${ROUTERPORT}:tcp -p %s%s"' % (prefix, tcp)) for key in ['default.xml', 'windefault.xml']: s = self.cfg_files[key].text() assert routerport in s assert insecure_routerport in s assert client_endpoints in s @pytest.mark.parametrize('prefix', [None, 1, 2]) @pytest.mark.parametrize('default', [True, False]) def testRevert(self, prefix, default): if not default: self.create_new_ice_config() kwargs = {} if prefix: self.args += ['--prefix', '%s' % prefix] kwargs['prefix'] = prefix self.args += ['--skipcheck'] self.cli.invoke(self.args, strict=True) # Check configuration file ports have been prefixed self.check_ice_config(**kwargs) self.check_cfg(**kwargs) self.check_config_xml(**kwargs) self.check_default_xml(**kwargs) # Check revert argument self.args += ['--revert'] self.cli.invoke(self.args, strict=True) # Check configuration file ports have been deprefixed self.check_ice_config() self.check_cfg() self.check_config_xml() self.check_default_xml() @pytest.mark.parametrize('default', [True, False]) def testFailingRevert(self, default): if not default: self.create_new_ice_config() kwargs = {'prefix': 1} self.args += ['--skipcheck'] self.args += ['--prefix', '%s' % kwargs['prefix']] self.cli.invoke(self.args, strict=True) # Check configuration file ports self.check_ice_config(**kwargs) self.check_cfg(**kwargs) self.check_config_xml(**kwargs) self.check_default_xml(**kwargs) # Test revert with a mismatching prefix self.args[-1] = "2" self.args += ['--revert'] self.cli.invoke(self.args, strict=True) # Check configuration file ports have not been modified self.check_ice_config(**kwargs) self.check_cfg(**kwargs) self.check_config_xml(**kwargs) self.check_default_xml(**kwargs) @pytest.mark.parametrize('prefix', [None, 1]) @pytest.mark.parametrize('registry', [None, 111]) @pytest.mark.parametrize('tcp', [None, 222]) @pytest.mark.parametrize('ssl', [None, 333]) @pytest.mark.parametrize('webserver', [None, 444]) def testExplicitPorts(self, registry, ssl, tcp, webserver, prefix): kwargs = {} if prefix: self.args += ['--prefix', '%s' % prefix] kwargs['prefix'] = prefix if registry: self.args += ['--registry', '%s' % registry] kwargs["registry"] = registry if tcp: self.args += ['--tcp', '%s' % tcp] kwargs["tcp"] = tcp if ssl: self.args += ['--ssl', '%s' % ssl] kwargs["ssl"] = ssl if webserver: self.args += ['--webserver', '%s' % webserver] kwargs["webserver"] = webserver self.args += ['--skipcheck'] self.cli.invoke(self.args, strict=True) self.check_ice_config(**kwargs)
def invoke(self, *args): CLI.invoke(self, *args, strict=True)
class TestDatabase(object): def setup_method(self, method): self.cli = CLI() self.cli.register("db", DatabaseControl, "TEST") self.args = ["db"] dir = path(__file__) / ".." / ".." / ".." / ".." / ".." / ".." /\ ".." / "dist" # FIXME: should not be hard-coded dir = dir.abspath() cfg = dir / "etc" / "omero.properties" cfg = cfg.abspath() self.cli.dir = dir self.data = {} for line in cfg.text().split("\n"): line = line.strip() for x in ("version", "patch"): key = "omero.db." + x if line.startswith(key): self.data[x] = line[len(key) + 1:] self.file = create_path() self.script_file = "%(version)s__%(patch)s.sql" % self.data if os.path.isfile(self.script_file): os.rename(self.script_file, self.script_file + '.bak') assert not os.path.isfile(self.script_file) self.mox = Mox() self.mox.StubOutWithMock(getpass, 'getpass') self.mox.StubOutWithMock(__builtin__, "raw_input") def teardown_method(self, method): self.file.remove() if os.path.isfile(self.script_file): os.remove(self.script_file) if os.path.isfile(self.script_file + '.bak'): os.rename(self.script_file + '.bak', self.script_file) self.mox.UnsetStubs() self.mox.VerifyAll() def password(self, string, strict=True): self.cli.invoke("db password " + string % self.data, strict=strict) def testHelp(self): self.args += ["-h"] self.cli.invoke(self.args, strict=True) @pytest.mark.parametrize('subcommand', DatabaseControl().get_subcommands()) def testSubcommandHelp(self, subcommand): self.args += [subcommand, "-h"] self.cli.invoke(self.args, strict=True) def testBadVersionDies(self): with pytest.raises(NonZeroReturnCode): self.cli.invoke("db script NONE NONE pw", strict=True) def testPasswordIsAskedForAgainIfDiffer(self): self.expectPassword("ome") self.expectConfirmation("bad") self.expectPassword("ome") self.expectConfirmation("ome") self.mox.ReplayAll() self.password("") def testPasswordIsAskedForAgainIfEmpty(self): self.expectPassword("") self.expectPassword("ome") self.expectConfirmation("ome") self.mox.ReplayAll() self.password("") @pytest.mark.parametrize('no_salt', ['', '--no-salt']) @pytest.mark.parametrize('user_id', ['', '0', '1']) @pytest.mark.parametrize('password', ['', 'ome']) def testPassword(self, user_id, password, no_salt, capsys): args = "" if user_id: args += "--user-id=%s " % user_id if no_salt: args += "%s " % no_salt if password: args += "%s" % password else: self.expectPassword("ome", id=user_id) self.expectConfirmation("ome", id=user_id) self.mox.ReplayAll() self.password(args) out, err = capsys.readouterr() assert out.strip() == self.password_output(user_id, no_salt) @pytest.mark.parametrize('file_arg', ['', '-f', '--file']) @pytest.mark.parametrize('no_salt', ['', '--no-salt']) @pytest.mark.parametrize('password', ['', '--password ome']) def testScript(self, no_salt, file_arg, password, capsys): """ Recommended usage of db script """ args = "db script " + password if no_salt: args += " %s" % no_salt if file_arg: args += " %s %s" % (file_arg, str(self.file)) output = self.file else: output = self.script_file if not password: self.expectPassword("ome") self.expectConfirmation("ome") self.mox.ReplayAll() self.cli.invoke(args, strict=True) out, err = capsys.readouterr() assert 'Using %s for version' % self.data['version'] in err assert 'Using %s for patch' % self.data['patch'] in err if password: assert 'Using password from commandline' in err with open(output) as f: lines = f.readlines() for line in lines: if line.startswith('insert into password values (0'): assert line.strip() == self.script_output(no_salt) @pytest.mark.parametrize('file_arg', ['', '-f', '--file']) @pytest.mark.parametrize('no_salt', ['', '--no-salt']) @pytest.mark.parametrize( 'pos_args', ['%s %s %s', '--version %s --patch %s --password %s']) def testScriptDeveloperArgs(self, pos_args, no_salt, file_arg, capsys): """ Deprecated and developer usage of db script """ arg_values = ('VERSION', 'PATCH', 'PASSWORD') args = "db script " + pos_args % arg_values if no_salt: args += " %s" % no_salt if file_arg: args += " %s %s" % (file_arg, str(self.file)) self.file else: self.script_file self.mox.ReplayAll() with pytest.raises(NonZeroReturnCode): self.cli.invoke(args, strict=True) out, err = capsys.readouterr() assert 'Using %s for version' % (arg_values[0]) in err assert 'Using %s for patch' % (arg_values[1]) in err assert 'Using password from commandline' in err assert 'Invalid Database version/patch' in err def password_ending(self, user, id): if id and id != '0': rv = "user %s: " % id else: rv = "%s user: "******"password for OMERO " + rv def expectPassword(self, pw, user="******", id=None): getpass.getpass("Please enter %s" % self.password_ending(user, id)).AndReturn(pw) def expectConfirmation(self, pw, user="******", id=None): getpass.getpass("Please re-enter %s" % self.password_ending(user, id)).AndReturn(pw) def password_output(self, user_id, no_salt): update_msg = "UPDATE password SET hash = \'%s\'" \ " WHERE experimenter_id = %s;" if not user_id: user_id = "0" return update_msg % (hash_map[(user_id, no_salt)], user_id) def script_output(self, no_salt): root_password_msg = "insert into password values (0,\'%s\');" return root_password_msg % (hash_map[("0", no_salt)])
class TestImport(object): def setup_method(self, method): self.cli = CLI() self.cli.register("import", ImportControl, "TEST") self.args = ["import"] dist_dir = path(__file__) / ".." / ".." / ".." / ".." / ".." / ".." /\ ".." / "dist" # FIXME: should not be hard-coded dist_dir = dist_dir.abspath() client_dir = dist_dir / "lib" / "client" self.args += ["--clientdir", client_dir] def mkdir(self, parent, name, with_ds_store=False): child = parent / name child.mkdir() if with_ds_store: ds_store = child / ".DS_STORE" ds_store.write("") return child def mkfakescreen(self, screen_dir, nplates=2, nruns=2, nwells=2, nfields=4, with_ds_store=False): fieldfiles = [] for iplate in range(nplates): plate_dir = self.mkdir( screen_dir, "Plate00%s" % str(iplate), with_ds_store=with_ds_store) for irun in range(nruns): run_dir = self.mkdir( plate_dir, "Run00%s" % str(irun), with_ds_store=with_ds_store) for iwell in range(nwells): well_dir = self.mkdir( run_dir, "WellA00%s" % str(iwell), with_ds_store=with_ds_store) for ifield in range(nfields): fieldfile = (well_dir / ("Field00%s.fake" % str(ifield))) fieldfile.write('') fieldfiles.append(fieldfile) return fieldfiles def testDropBoxArgs(self): class MockImportControl(ImportControl): def importer(this, args): assert args.server == "localhost" assert args.port == "4064" assert args.key == "b0742975-03a1-4f6d-b0ac-639943f1a147" assert args.errs == "/Users/cblackburn/omero/tmp/\ omero_cblackburn/6915/dropboxuUGl5rerr" assert args.file == "/Users/cblackburn/omero/tmp/\ omero_cblackburn/6915/dropboxaDCjQlout" self.cli.register("mock-import", MockImportControl, "HELP") self.args = ['-s', 'localhost', '-p', '4064', '-k', 'b0742975-03a1-4f6d-b0ac-639943f1a147'] self.args += ['mock-import', '---errs=/Users/cblackburn/omero/tmp/\ omero_cblackburn/6915/dropboxuUGl5rerr'] self.args += ['---file=/Users/cblackburn/omero/tmp/\ omero_cblackburn/6915/dropboxaDCjQlout'] self.args += ['--', '/OMERO/DropBox/root/tinyTest.d3d.dv'] self.cli.invoke(self.args) @pytest.mark.parametrize('help_argument', help_arguments) def testHelp(self, help_argument): """Test help arguments""" self.args += [help_argument] self.cli.invoke(self.args) @pytest.mark.parametrize("data", (("1", False), ("3", True))) def testImportDepth(self, tmpdir, capfd, data): """Test import using depth argument""" dir1 = tmpdir.join("a") dir1.mkdir() dir2 = dir1 / "b" dir2.mkdir() fakefile = dir2 / "test.fake" fakefile.write('') self.args += ["-f", "--debug=ERROR"] self.args += [str(dir1)] depth, result = data self.cli.invoke(self.args + ["--depth=%s" % depth], strict=True) o, e = capfd.readouterr() if result: assert str(fakefile) in str(o) else: assert str(fakefile) not in str(o) def testImportFakeImage(self, tmpdir, capfd): """Test fake image import""" fakefile = tmpdir.join("test.fake") fakefile.write('') self.args += ["-f", "--debug=ERROR"] self.args += [str(fakefile)] self.cli.invoke(self.args, strict=True) o, e = capfd.readouterr() outputlines = str(o).split('\n') reader = 'loci.formats.in.FakeReader' assert outputlines[-2] == str(fakefile) assert outputlines[-3] == \ "# Group: %s SPW: false Reader: %s" % (str(fakefile), reader) @pytest.mark.parametrize('with_ds_store', (True, False)) def testImportFakeScreen(self, tmpdir, capfd, with_ds_store): """Test fake screen import""" screen_dir = tmpdir.join("screen.fake") screen_dir.mkdir() fieldfiles = self.mkfakescreen( screen_dir, with_ds_store=with_ds_store) self.args += ["-f", "--debug=ERROR"] self.args += [str(fieldfiles[0])] self.cli.invoke(self.args, strict=True) o, e = capfd.readouterr() outputlines = str(o).split('\n') reader = 'loci.formats.in.FakeReader' assert outputlines[-len(fieldfiles)-2] == \ "# Group: %s SPW: true Reader: %s" % (str(fieldfiles[0]), reader) for i in range(len(fieldfiles)): assert outputlines[-1-len(fieldfiles)+i] == str(fieldfiles[i])
class TestRewrite(object): """Test template files regeneration""" @pytest.fixture(autouse=True) def setup_method(self, tmpadmindir): self.cli = CLI() self.cli.dir = path(tmpadmindir) self.cli.register("admin", AdminControl, "TEST") self.cli.register("config", PrefsControl, "TEST") self.args = ["admin", "rewrite"] def testTemplatesGeneration(self): """Test template files are generated by the rewrite subcommand""" # Test non-existence of configuration files for f in GRID_FILES: assert not os.path.exists(path(self.cli.dir) / "etc" / "grid" / f) for f in ETC_FILES: assert not os.path.exists(path(self.cli.dir) / "etc" / f) # Call the jvmcf command and test file genearation self.cli.invoke(self.args, strict=True) for f in GRID_FILES: assert os.path.exists(path(self.cli.dir) / "etc" / "grid" / f) for f in ETC_FILES: assert os.path.exists(path(self.cli.dir) / "etc" / f) def testForceRewrite(self, monkeypatch): """Test template regeneration while the server is running""" # Call the jvmcfg command and test file generation self.cli.invoke(self.args, strict=True) monkeypatch.setattr(AdminControl, "status", lambda *args, **kwargs: 0) with pytest.raises(NonZeroReturnCode): self.cli.invoke(self.args, strict=True) def testOldTemplates(self): old_templates = path(__file__).dirname() / ".." / "old_templates.xml" old_templates.copy( path(self.cli.dir) / "etc" / "templates" / "grid" / "templates.xml") with pytest.raises(NonZeroReturnCode): self.cli.invoke(self.args, strict=True) @pytest.mark.parametrize('prefix', [None, 1]) @pytest.mark.parametrize('registry', [None, 111]) @pytest.mark.parametrize('tcp', [None, 222]) @pytest.mark.parametrize('ssl', [None, 333]) @pytest.mark.parametrize('ws_wss_transports', [ (None, None, None), (444, None, ('ssl', 'tcp', 'wss', 'ws')), (None, 555, ('ssl', 'tcp', 'wss', 'ws')), ]) def testExplicitPorts(self, registry, ssl, tcp, prefix, ws_wss_transports, monkeypatch): """ Test the omero.ports.xxx and omero.client.icetransports configuration properties during the generation of the configuration files """ # Skip the JVM settings calculation for this test ws, wss, transports = ws_wss_transports kwargs = {} if prefix: kwargs["prefix"] = prefix if registry: kwargs["registry"] = registry if tcp: kwargs["tcp"] = tcp if ssl: kwargs["ssl"] = ssl if ws: kwargs["ws"] = ws if wss: kwargs["wss"] = wss for (k, v) in kwargs.iteritems(): self.cli.invoke(["config", "set", "omero.ports.%s" % k, "%s" % v], strict=True) if transports: self.cli.invoke([ "config", "set", "omero.client.icetransports", "%s" % ','.join(transports) ], strict=True) kwargs["transports"] = transports self.cli.invoke(self.args, strict=True) check_ice_config(self.cli.dir, **kwargs) check_registry(self.cli.dir, **kwargs) def testGlacier2Icessl(self, monkeypatch): """ Test the omero.glacier2.IceSSL.* properties during the generation of the configuration files """ # Skip the JVM settings calculation for this test # monkeypatch.setattr(omero.install.jvmcfg, "adjust_settings", # lambda x, y: {}) if sys.platform == "darwin": expected_ciphers = '(AES)' else: expected_ciphers = 'ADH:!LOW:!MD5:!EXP:!3DES:@STRENGTH' glacier2 = [ ("IceSSL.Ciphers", expected_ciphers), ("IceSSL.TestKey", "TestValue"), ] self.cli.invoke([ "config", "set", "omero.glacier2." + glacier2[1][0], glacier2[1][1] ], strict=True) self.cli.invoke(self.args, strict=True) check_templates_xml(self.cli.dir, glacier2)
def testOne(self): cli = CLI() cli.register("t", TestRCode.T, "TEST") cli.invoke(["t"]) assert cli.rv == 1, cli.rv
idx = server.rindex("@") return server[idx+1:], server[0:idx] # server, user which may also contain an @ except ValueError: return server, None def _get_server(self, store): defserver = store.last_host() rv = self.ctx.input("Server: [%s]" % defserver) if not rv: return defserver, None else: return self._parse_conn(rv) def _get_username(self, defuser): if defuser is None: defuser = get_user("root") rv = self.ctx.input("Username: [%s]" % defuser) if not rv: return defuser else: return rv try: register("sessions", SessionsControl, HELP) except NameError: if __name__ == "__main__": cli = CLI() cli.register("sessions", SessionsControl, HELP) cli.invoke(sys.argv[1:])
class TestImport(object): def setup_method(self, method): self.cli = CLI() self.cli.register("import", ImportControl, "TEST") self.args = ["import"] def add_client_dir(self): dist_dir = path(__file__) / ".." / ".." / ".." / ".." / ".." / ".." /\ ".." / "dist" # FIXME: should not be hard-coded dist_dir = dist_dir.abspath() client_dir = dist_dir / "lib" / "client" self.args += ["--clientdir", client_dir] def mkdir(self, parent, name, with_ds_store=False): child = parent / name child.mkdir() if with_ds_store: ds_store = child / ".DS_STORE" ds_store.write("") return child def mkfakescreen(self, screen_dir, nplates=2, nruns=2, nwells=2, nfields=4, with_ds_store=False): fieldfiles = [] for iplate in range(nplates): plate_dir = self.mkdir( screen_dir, "Plate00%s" % str(iplate), with_ds_store=with_ds_store) for irun in range(nruns): run_dir = self.mkdir( plate_dir, "Run00%s" % str(irun), with_ds_store=with_ds_store) for iwell in range(nwells): well_dir = self.mkdir( run_dir, "WellA00%s" % str(iwell), with_ds_store=with_ds_store) for ifield in range(nfields): fieldfile = (well_dir / ("Field00%s.fake" % str(ifield))) fieldfile.write('') fieldfiles.append(fieldfile) return fieldfiles def mkfakepattern(self, tmpdir, nangles=7, ntimepoints=10): spim_dir = tmpdir.join("SPIM") spim_dir.mkdir() tiffiles = [] for angle in range(1, nangles + 1): for timepoint in range(1, ntimepoints + 1): tiffile = (spim_dir / ("spim_TL%s_Angle%s.fake" % (str(timepoint), str(angle)))) tiffile.write('') print str(tiffile) tiffiles.append(tiffile) patternfile = spim_dir / "spim.pattern" patternfile.write("spim_TL<1-%s>_Angle<1-%s>.fake" % (str(ntimepoints), str(nangles))) assert len(tiffiles) == nangles * ntimepoints return patternfile, tiffiles def testDropBoxArgs(self): class MockImportControl(ImportControl): def importer(this, args): assert args.server == "localhost" assert args.port == "4064" assert args.key == "b0742975-03a1-4f6d-b0ac-639943f1a147" assert args.errs == "/tmp/dropbox.err" assert args.file == "/tmp/dropbox.out" self.cli.register("mock-import", MockImportControl, "HELP") self.args = ['-s', 'localhost', '-p', '4064', '-k', 'b0742975-03a1-4f6d-b0ac-639943f1a147'] self.args += ['mock-import', '---errs=/tmp/dropbox.err'] self.args += ['---file=/tmp/dropbox.out'] self.args += ['--', '/OMERO/DropBox/root/tinyTest.d3d.dv'] self.cli.invoke(self.args) @pytest.mark.parametrize('help_argument', help_arguments) def testHelp(self, help_argument): """Test help arguments""" self.args += [help_argument] self.cli.invoke(self.args) @pytest.mark.parametrize('clientdir_exists', [True, False]) def testImportNoClientDirFails(self, tmpdir, clientdir_exists): """Test fake screen import""" fakefile = tmpdir.join("test.fake") fakefile.write('') if clientdir_exists: self.args += ["--clientdir", str(tmpdir)] self.args += [str(fakefile)] with pytest.raises(NonZeroReturnCode): self.cli.invoke(self.args, strict=True) @pytest.mark.parametrize("data", (("1", False), ("3", True))) def testImportDepth(self, tmpdir, capfd, data): """Test import using depth argument""" dir1 = tmpdir.join("a") dir1.mkdir() dir2 = dir1 / "b" dir2.mkdir() fakefile = dir2 / "test.fake" fakefile.write('') self.add_client_dir() self.args += ["-f", "--debug=ERROR"] self.args += [str(dir1)] depth, result = data self.cli.invoke(self.args + ["--depth=%s" % depth], strict=True) o, e = capfd.readouterr() if result: assert str(fakefile) in str(o) else: assert str(fakefile) not in str(o) def testImportFakeImage(self, tmpdir, capfd): """Test fake image import""" fakefile = tmpdir.join("test.fake") fakefile.write('') self.add_client_dir() self.args += ["-f", "--debug=ERROR"] self.args += [str(fakefile)] self.cli.invoke(self.args, strict=True) o, e = capfd.readouterr() outputlines = str(o).split('\n') reader = 'loci.formats.in.FakeReader' assert outputlines[-2] == str(fakefile) assert outputlines[-3] == \ "# Group: %s SPW: false Reader: %s" % (str(fakefile), reader) @pytest.mark.parametrize('with_ds_store', (True, False)) def testImportFakeScreen(self, tmpdir, capfd, with_ds_store): """Test fake screen import""" screen_dir = tmpdir.join("screen.fake") screen_dir.mkdir() fieldfiles = self.mkfakescreen( screen_dir, with_ds_store=with_ds_store) self.add_client_dir() self.args += ["-f", "--debug=ERROR"] self.args += [str(fieldfiles[0])] self.cli.invoke(self.args, strict=True) o, e = capfd.readouterr() outputlines = str(o).split('\n') reader = 'loci.formats.in.FakeReader' assert outputlines[-len(fieldfiles)-2] == \ "# Group: %s SPW: true Reader: %s" % (str(fieldfiles[0]), reader) for i in range(len(fieldfiles)): assert outputlines[-1-len(fieldfiles)+i] == str(fieldfiles[i]) def testImportPattern(self, tmpdir, capfd): """Test pattern import""" patternfile, tiffiles = self.mkfakepattern(tmpdir) self.add_client_dir() self.args += ["-f", "--debug=ERROR"] self.args += [str(patternfile)] self.cli.invoke(self.args, strict=True) o, e = capfd.readouterr() outputlines = str(o).split('\n') reader = 'loci.formats.in.FilePatternReader' print o assert outputlines[-len(tiffiles)-3] == \ "# Group: %s SPW: false Reader: %s" % (str(patternfile), reader) assert outputlines[-len(tiffiles)-2] == str(patternfile) for i in range(len(tiffiles)): assert outputlines[-1-len(tiffiles)+i] == str(tiffiles[i]) @pytest.mark.parametrize('hostname', ['localhost', 'servername']) @pytest.mark.parametrize('port', [None, 4064, 14064]) def testLoginArguments(self, monkeypatch, hostname, port): self.args += ['test.fake'] control = self.cli.controls['import'] control.command_args = [] sessionid = str(uuid.uuid4()) def new_client(x): if port: c = MockClient(hostname, port) else: c = MockClient(hostname) c.setSessionId(sessionid) return c monkeypatch.setattr(self.cli, 'conn', new_client) control.set_login_arguments(self.cli.parser.parse_args(self.args)) expected_args = ['-s', '%s' % hostname] expected_args += ['-p', '%s' % (port or 4064)] expected_args += ['-k', '%s' % sessionid] assert control.command_args == expected_args