Beispiel #1
0
class TestLoadRenderingSettings:
    def setup_method(self):
        self.cli = CLI()
        self.cli.register("render", RenderControl, "TEST")
        self.render = self.cli.controls['render']

    def test_none(self):
        with pytest.raises(NonZeroReturnCode):
            self.render._load_rendering_settings(None)

    def test_non_existing_file(self):
        with pytest.raises(NonZeroReturnCode) as e:
            self.render._load_rendering_settings(str(uuid.uuid4()) + '.yml')
        assert e.value.rv == 103

    def test_no_channels(self, tmpdir):
        d = {'version': 1}
        f = write_yaml(d, tmpdir)
        with pytest.raises(NonZeroReturnCode) as e:
            self.render._load_rendering_settings(f)
        assert e.value.rv == 104

    def test_bad_version(self, tmpdir):
        d = {'channels': {1: {'label': 'foo'}}}
        f = write_yaml(d, tmpdir)
        with pytest.raises(NonZeroReturnCode) as e:
            self.render._load_rendering_settings(f)
        assert e.value.rv == 124
Beispiel #2
0
class TestJvmCfg(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", "jvmcfg"]

    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)
Beispiel #3
0
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)
Beispiel #4
0
    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)
    def import_ln_s(self, host, port):
        """Import file using the ``--transfer=ln_s`` option.

        Parameters
        ----------
        host : str
            Hostname of OMERO server in which images will be imported.
        port : int
            Port used to connect to OMERO.server.

        Returns
        -------
        import_status : boolean
            True if OMERO import returns a 0 exit status, else False.
        """
        cli = CLI()
        cli.register('import', ImportControl, '_')
        cli.register('sessions', SessionsControl, '_')

        cli.invoke([
            'import', '-k',
            self.conn.getSession().getUuid().val, '-s', host, '-p',
            str(port), '--transfer', 'ln_s',
            str(self.file_path)
        ])

        if cli.rv == 0:
            self.imported = True
            print(f'Imported {self.file_path}')
            return True
        else:
            logging.error(f'Import of {self.file_path} has failed!')
            return False
Beispiel #6
0
 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)
Beispiel #7
0
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 testNoArgumentsDies(self):
        cli = CLI()
        cli.register("import", ImportControl, "HELP")

        try:
            cli.invoke([])
            self.assert_(cli.rv != 0)
        except NonZeroReturnCode, nzrc:
            pass
Beispiel #9
0
    def testNoArgumentsDies(self):
        cli = CLI()
        cli.register("import", ImportControl, "HELP")

        try:
            cli.invoke([])
            self.assert_(cli.rv != 0)
        except NonZeroReturnCode, nzrc:
            pass
Beispiel #10
0
 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)
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)
Beispiel #12
0
class AbstractCLITest(ITest):
    def setup_method(self, method):
        super(AbstractCLITest, self).setup_method(method)
        self.cli = CLI()
        self.cli.register("sessions", SessionsControl, "TEST")

    def setup_mock(self):
        self.mox = Mox()

    def teardown_mock(self):
        self.mox.UnsetStubs()
        self.mox.VerifyAll()
Beispiel #13
0
class AbstractCLITest(ITest):

    def setup_method(self, method):
        super(AbstractCLITest, self).setup_method(method)
        self.cli = CLI()
        self.cli.register("sessions", SessionsControl, "TEST")

    def setup_mock(self):
        self.mox = Mox()

    def teardown_mock(self):
        self.mox.UnsetStubs()
        self.mox.VerifyAll()
Beispiel #14
0
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', subcommands)
    def testSubcommandHelp(self, subcommand):
        self.args += [subcommand, "-h"]
        self.cli.invoke(self.args, strict=True)
Beispiel #15
0
class TestGroup(object):
    def setup_method(self, method):
        self.cli = CLI()
        self.cli.register("group", GroupControl, "TEST")
        self.args = ["group"]

    def testHelp(self):
        self.args += ["-h"]
        self.cli.invoke(self.args, strict=True)

    @pytest.mark.parametrize("subcommand", GroupControl().get_subcommands())
    def testSubcommandHelp(self, subcommand):
        self.args += [subcommand, "-h"]
        self.cli.invoke(self.args, strict=True)
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)
    def validate_target(self):
        """Check whether an import target can be imported by OMERO

        This used the OMERO CLI, but since it is just checking whether
        the file can be interpreted by BioFormats, there is no connection
        required.
        """

        cli = CLI()
        cli.register('import', ImportControl, '_')
        cli.invoke(['import', '-f', str(self.path_to_target)])

        if cli.rv == 0:
            self.valid_target = True
        else:
            self.valid_target = False
Beispiel #18
0
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)
Beispiel #19
0
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, tmpdir):
        tmpfile = tmpdir.join('test')
        tmpfile.write("foo")
        self.cli = CLI()
        self.cli.register("load", LoadControl, "help")

        # replace slashes, otherwise C:\\x becomes C:x
        tmpfile = tmpfile.strpath.replace("\\", "/")
        with pytest.raises(NonZeroReturnCode):
            self.cli.invoke("load %s" % tmpfile, strict=True)

        self.cli.invoke("load -k %s" % tmpfile, strict=True)
        self.cli.invoke("load --keep-going %s" % tmpfile, 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}
Beispiel #21
0
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)
Beispiel #24
0
class TestScript(object):

    def setup_method(self, method):
        self.cli = CLI()
        self.cli.register("script", ScriptControl, "TEST")
        self.args = ["script"]

    # Help subcommands
    # ========================================================================
    def testHelp(self):
        self.args += ["-h"]
        self.cli.invoke(self.args, strict=True)

    @pytest.mark.parametrize("subcommand", ScriptControl().get_subcommands())
    def testSubcommandHelp(self, subcommand):
        self.args += [subcommand, "-h"]
        self.cli.invoke(self.args, strict=True)
Beispiel #25
0
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)
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)
Beispiel #27
0
class TestReadChannels:
    def setup_method(self):
        self.cli = CLI()
        self.cli.register("render", RenderControl, "TEST")
        self.render = self.cli.controls['render']

    def test_non_integer_channel(self):
        d = {'channels': {'GFP': {'label': 'foo'}}}
        with pytest.raises(NonZeroReturnCode) as e:
            self.render._read_channels(d)
        assert e.value.rv == 105

    @pytest.mark.parametrize('key', ['min', 'max', 'start', 'end'])
    def test_float_keys(self, key):
        d = {'channels': {1: {key: 'foo'}}}
        with pytest.raises(NonZeroReturnCode) as e:
            self.render._read_channels(d)
        assert e.value.rv == 105
Beispiel #28
0
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)
Beispiel #29
0
def users_groups(conn, omero_params):
    session_uuid = conn.getSession().getUuid().val
    user = omero_params[0]
    host = omero_params[2]
    port = str(omero_params[3])
    cli = CLI()
    cli.register('sessions', SessionsControl, 'test')
    cli.register('user', UserControl, 'test')
    cli.register('group', GroupControl, 'test')

    group_info = []
    for gname, gperms in GROUPS_TO_CREATE:
        cli.invoke([
            'group', 'add', gname, '--type', gperms, '-k', session_uuid, '-u',
            user, '-s', host, '-p', port
        ])
        gid = ezomero.get_group_id(conn, gname)
        group_info.append([gname, gid])

    user_info = []
    for user, groups_add, groups_own in USERS_TO_CREATE:
        # make user while adding to first group
        cli.invoke([
            'user', 'add', user, 'test', 'tester', '--group-name',
            groups_add[0], '-e', '*****@*****.**', '-P', 'abc123', '-k',
            session_uuid, '-u', user, '-s', host, '-p', port
        ])

        # add user to rest of groups
        if len(groups_add) > 1:
            for group in groups_add[1:]:
                cli.invoke([
                    'group', 'adduser', '--user-name', user, '--name', group,
                    '-k', session_uuid, '-u', user, '-s', host, '-p', port
                ])

        # make user owner of listed groups
        if len(groups_own) > 0:
            for group in groups_own:
                cli.invoke([
                    'group', 'adduser', '--user-name', user, '--name', group,
                    '--as-owner', '-k', session_uuid, '-u', user, '-s', host,
                    '-p', port
                ])
        uid = ezomero.get_user_id(conn, user)
        user_info.append([user, uid])

    return (group_info, user_info)
Beispiel #30
0
            self.ctx.die(
                197, "Current user is not member of group: %s" % group.id.val)

        # Set requests group
        for request in req.requests:
            request.grp = gid

        super(ChgrpControl, self)._process_request(req, args, client)

    def create_error_report(self, rsp):
        import omero.cmd
        if isinstance(rsp, omero.cmd.GraphConstraintERR):
            if "Fileset" in rsp.constraints:
                fileset = rsp.constraints.get("Fileset")
                return "You cannot move part of fileset %s; only complete" \
                    " filesets can be moved to another group.\n" % \
                    ", ".join(str(x) for x in fileset)
            else:
                return super(ChgrpControl, self).create_error_report(rsp)
        else:
            return super(ChgrpControl, self).create_error_report(rsp)


try:
    register("chgrp", ChgrpControl, HELP)
except NameError:
    if __name__ == "__main__":
        cli = CLI()
        cli.register("chgrp", ChgrpControl, HELP)
        cli.invoke(sys.argv[1:])
Beispiel #31
0
                    else:
                        self.ctx.out("Experimenter=%s\tomeName=%s"
                                     % (e.getId().getValue(),
                                        e.getOmeName().getValue()))

    @admin_only
    def create(self, args):
        c = self.ctx.conn(args)
        ildap = c.sf.getLdapService()
        iadmin = c.sf.getAdminService()

        import omero
        import Ice
        try:
            exp = ildap.createUser(args.username)
            dn = iadmin.lookupLdapAuthExperimenter(exp.id.val)
            self.ctx.out("Added user %s (id=%s) with DN=%s" %
                         (exp.omeName.val, exp.id.val, dn))
        except omero.ValidationException as ve:
            self.ctx.die(132, ve.message)
        except Ice.RequestFailedException as rfe:
            self.ctx.die(133, self.exc.handle_failed_request(rfe))

try:
    register("ldap", LdapControl, HELP)
except NameError:
    if __name__ == "__main__":
        cli = CLI()
        cli.register("ldap", LdapControl, HELP)
        cli.invoke(sys.argv[1:])
Beispiel #32
0
                            help="Read from files or standard in")
        parser.set_defaults(func=self.__call__)
        parser.add_login_arguments()

    def __call__(self, args):
        if args.list:
            ops = [x[4:] for x in dir(perf_test.Item) if x.startswith("_op_")]
            ops.sort()
            for op in ops:
                print(op)
        else:
            if not args.file:
                self.ctx.die(167, "No files given. Use '-' for stdin.")
            client = self.ctx.conn(args)
            ctx = perf_test.Context(None, client=client)
            self.ctx.out("Saving performance results to %s" % ctx.dir)
            ctx.add_reporter(perf_test.CsvReporter(ctx.dir))
            # ctx.add_reporter(perf_test.HdfReporter(ctx.dir))
            # ctx.add_reporter(perf_test.PlotReporter())
            handler = perf_test.PerfHandler(ctx)
            perf_test.handle(handler, args.file)


try:
    register("perf", PerfControl, HELP)
except NameError:
    if __name__ == "__main__":
        cli = CLI()
        cli.register("perf", PerfControl, HELP)
        cli.invoke(sys.argv[1:])
Beispiel #33
0
 def cli(self):
     cli = CLI()
     cli.register("upload", UploadControl, "TEST")
     cli.register("sessions", SessionsControl, "TEST")
     cli.register("s", ScriptControl, "TEST")
     return cli
Beispiel #34
0
                        continue
                try:
                    exp = iadmin.lookupExperimenter(omeName)
                    olddn = iadmin.lookupLdapAuthExperimenter(exp.id.val)
                except omero.ApiUsageException:
                    continue  # Unknown user

                if olddn:
                    if olddn != dn:
                        self.ctx.err("Found different DN for %s: %s" %
                                     (omeName, olddn))
                    else:
                        self.ctx.dbg("DN already set for %s: %s" %
                                     (omeName, olddn))
                else:
                    if args.commands:
                        self.ctx.out("%s ldap setdn %s %s" %
                                     (sys.argv[0], omeName, dn))
                    else:
                        self.ctx.out("Experimenter:%s\tomeName=%s\t%s" %
                                     (exp.id.val, omeName, dn))


try:
    register("ldap", LdapControl, HELP)
except NameError:
    if __name__ == "__main__":
        cli = CLI()
        cli.register("ldap", LdapControl, HELP)
        cli.invoke(sys.argv[1:])
Beispiel #35
0
        config["omero.config.upgraded"] = "4.2.0"

    def handle_line(self, line, config, keys):
        line = line.strip()
        if not line or line.startswith("#"):
            return None
        if line.endswith("\\"):
            return line[:-1]

        parts = line.split("=", 1)
        if len(parts[0]) == 0:
            return
        if len(parts) == 1:
            parts.append("")
        if keys and parts[0] in keys:
            self.ctx.die(502, "Duplicate property: %s (%s => %s)"\
                % (parts[0], config[parts[0]], parts[1]))
            keys.append(parts[0])
        config[parts[0]] = parts[1]

    def old(self, args):
        self.ctx.out(getprefs(args.target, str(self.ctx.dir / "lib")))

try:
    register("config", PrefsControl, HELP)
except NameError:
    if __name__ == "__main__":
        cli = CLI()
        cli.register("config", PrefsControl, HELP)
        cli.invoke(sys.argv[1:])
Beispiel #36
0
        for attr_name, arg_name in java_args.items():
            arg_value = getattr(args, attr_name)
            if arg_value:
                if isinstance(arg_name, tuple):
                    arg_name = arg_name[0]
                    login_args.append("%s=%s" % (arg_name, arg_value))
                else:
                    login_args.append(arg_name)
                    if isinstance(arg_value, (str, unicode)):
                        login_args.append(arg_value)

        a = self.COMMAND + login_args + args.path
        p = omero.java.popen(a, debug=False, xargs=xargs, stdout=out, stderr=err)
        self.ctx.rv = p.wait()


class TestEngine(ImportControl):
    COMMAND = [TEST_CLASS]


try:
    register("import", ImportControl, HELP, epilog=EXAMPLES)
    register("testengine", TestEngine, TESTHELP)
except NameError:
    if __name__ == "__main__":
        cli = CLI()
        cli.register("import", ImportControl, HELP, epilog=EXAMPLES)
        cli.register("testengine", TestEngine, TESTHELP)
        cli.invoke(sys.argv[1:])
Beispiel #37
0
            self.print_usage()

        if args.all:
            self.print_all_commands_and_topics(args)
        elif args.list:
            self.print_commands_list(args)
        elif args.topic:
            self.print_single_command_or_topic(args)


controls = {
    "help": (HelpControl, "Syntax help for all commands"),
    "quit": (QuitControl, "Quit application"),
    "shell": (ShellControl, """Starts an IPython interpreter session

All arguments not understood vi %(prog)s will be passed to the shell.
Use "--" to end parsing, e.g. '%(prog)s -- --help' for IPython help"""),
    "version": (VersionControl, "Version number"),
    "load": (LoadControl, LOAD_HELP)
}

try:
    for k, v in controls.items():
        register(k, v[0], v[1])
except NameError:
    if __name__ == "__main__":
        cli = CLI()
        for k, v in controls.items():
            cli.register(k, v[0], v[1])
        cli.invoke(sys.argv[1:])
Beispiel #38
0
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(
                old_div(path(self.cli.dir), "etc" / "grid" / f))
        for f in ETC_FILES:
            assert not os.path.exists(old_div(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(
                old_div(path(self.cli.dir), "etc" / "grid" / f))
        for f in ETC_FILES:
            assert os.path.exists(old_div(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 = old_div(
            path(__file__).dirname(), ".." / "old_templates.xml")
        old_templates.copy(
            old_div(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 list(kwargs.items()):
            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)
Beispiel #39
0
        """
        Parses who items of the form: "user", "group", "user=1", "group=6"
        """

        import omero
        WHO_FACTORY = {"user": omero.model.ExperimenterI,
                       "group": omero.model.ExperimenterGroupI}
        WHO_CURRENT = {"user": lambda ec: ec.userId,
                       "group": lambda ec: ec.groupId}

        for key, factory in WHO_FACTORY.items():
            if who.startswith(key):
                if who == key:
                    id = WHO_CURRENT[key](self.ctx._event_context)
                    return factory(id, False)
                else:
                    parts = who.split("=")
                    if len(parts) != 2:
                        continue
                    else:
                        id = long(parts[1])
                        return factory(id, False)

try:
    register("script", ScriptControl, HELP)
except NameError:
    if __name__ == "__main__":
        cli = CLI()
        cli.register("script", ScriptControl, HELP)
        cli.invoke(sys.argv[1:])
Beispiel #40
0
        WHO_FACTORY = {
            "user": omero.model.ExperimenterI,
            "group": omero.model.ExperimenterGroupI
        }
        WHO_CURRENT = {
            "user": lambda ec: ec.userId,
            "group": lambda ec: ec.groupId
        }

        for key, factory in list(WHO_FACTORY.items()):
            if who.startswith(key):
                if who == key:
                    id = WHO_CURRENT[key](self.ctx.get_event_context())
                    return factory(id, False)
                else:
                    parts = who.split("=")
                    if len(parts) != 2:
                        continue
                    else:
                        id = int(parts[1])
                        return factory(id, False)


try:
    register("script", ScriptControl, HELP)
except NameError:
    if __name__ == "__main__":
        cli = CLI()
        cli.register("script", ScriptControl, HELP)
        cli.invoke(sys.argv[1:])
Beispiel #41
0
    def remove(self, args):
        import omero
        c = self.ctx.conn(args)
        a = c.sf.getAdminService()
        gid, grp = self.find_group(a, args.GROUP)
        uids = [self.find_user(a, x)[0] for x in args.USER]
        self.removeusersbyid(c, grp, uids)

    def addusersbyid(self, c, group, users):
        import omero
        a = c.sf.getAdminService()
        for add in list(users):
            a.addGroups(omero.model.ExperimenterI(add, False), [group])
            self.ctx.out("Added %s to group %s" % (add, group.id.val))

    def removeusersbyid(self, c, group, users):
        import omero
        a = c.sf.getAdminService()
        for add in list(users):
            a.removeGroups(omero.model.ExperimenterI(add, False), [group])
            self.ctx.out("Removed %s from group %s" % (add, group.id.val))

try:
    register("group", GroupControl, HELP)
except NameError:
    if __name__ == "__main__":
        cli = CLI()
        cli.register("group", GroupControl, HELP)
        cli.invoke(sys.argv[1:])
Beispiel #42
0
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)])
Beispiel #43
0
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="[[&quot;localhost&quot;, %s%s, &quot;omero&quot;]]"'
            ) % (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)
Beispiel #44
0
            if out:
                out = open(out, "w")
            if err:
                err = open(err, "w")

            p = omero.java.popen(
                import_command, debug=False, xargs=xargs, stdout=out,
                stderr=err)
            self.ctx.rv = p.wait()

        finally:
            # Make sure file handles are closed
            if out:
                out.close()
            if err:
                err.close()


class TestEngine(ImportControl):
    COMMAND = [TEST_CLASS]

try:
    register("import", ImportControl, HELP, epilog=EXAMPLES)
    register("testengine", TestEngine, TESTHELP)
except NameError:
    if __name__ == "__main__":
        cli = CLI()
        cli.register("import", ImportControl, HELP, epilog=EXAMPLES)
        cli.register("testengine", TestEngine, TESTHELP)
        cli.invoke(sys.argv[1:])
Beispiel #45
0
                self.ctx.dbg(e)
                self.ctx.die(432, "Bad object: %s" % args.type)

            c.sf.getUpdateService().indexObject(obj)

        else:
            search = c.sf.createSearchService()
            try:
                try:
                    search.onlyType(args.type)
                    search.byFullText(args.search_string)
                    if not search.hasNext():
                        self.ctx.die(433, "No results found.")
                    while search.hasNext():
                        results = search.results()
                        results = [[x] for x in results]
                        self.display(results)
                except omero.ApiUsageException, aue:
                    self.ctx.die(434, aue.message)

            finally:
                search.close()

try:
    register("search", SearchControl, HELP)
except NameError:
    if __name__ == "__main__":
        cli = CLI()
        cli.register("search", SearchControl, HELP)
        cli.invoke(sys.argv[1:])
Beispiel #46
0
            return
        if len(parts) == 1:
            parts.append("")

        _key = parts[0]
        _new = parts[1]
        if _key in config.keys():
            _old = config[_key]
        else:
            self.assert_valid_property_name(_key)
            _old = None

        if keys and _key in keys and _new != _old:

            self.ctx.die(502, "Duplicate property: %s ('%s' => '%s')"
                         % (_key, _old, _new))
            keys.append(_key)

        config[_key] = _new

    def old(self, args):
        self.ctx.out(getprefs(args.target, str(self.ctx.dir / "lib")))

try:
    register("config", PrefsControl, HELP)
except NameError:
    if __name__ == "__main__":
        cli = CLI()
        cli.register("config", PrefsControl, HELP)
        cli.invoke(sys.argv[1:])
Beispiel #47
0
                    pos = showCols.index(col)
                    keys.append(pos)
                except:
                    pass
        else:
            keys = [0]
        tb.sort(cols=keys, reverse=args.reverse)

        # Format the size column after sorting.
        if args.units:
            col = tb.get_col("size")
            col = [self._to_units(val, args.units) for val in col]
            tb.replace_col("size", col)
            tb.replace_header("size", "size (%siB)" % args.units)
        elif args.human_readable:
            col = tb.get_col("size")
            col = [filesizeformat(val) for val in col]
            tb.replace_col("size", col)
        else:
            tb.replace_header("size", "size (bytes)")

        self.ctx.out(str(tb.build()))

try:
    register("fs", FsControl, HELP)
except NameError:
    if __name__ == "__main__":
        cli = CLI()
        cli.register("fs", FsControl, HELP)
        cli.invoke(sys.argv[1:])
Beispiel #48
0
            name = default_name
        return server, name, port

    def _get_server(self, store, name, port):
        defserver = store.last_host()
        if not port:
            port = str(omero.constants.GLACIER2PORT)
        rv = self.ctx.input("Server: [%s:%s]" % (defserver, port))
        if not rv:
            return defserver, name, port
        else:
            return self._parse_conn(rv, name)

    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:])
Beispiel #49
0
        else:
            return False

    def set_environ(self, ice_config=None):
        os.environ['ICE_CONFIG'] = ice_config is None and \
            str(self.ctx.dir / "etc" / "ice.config") or str(ice_config)
        os.environ['PATH'] = str(os.environ.get('PATH', '.') + ':' +
                                 self.ctx.dir / 'bin')

    def iis(self, args):
        if not (self._isWindows() or self.ctx.isdebug):
            self.ctx.die(2, "'iis' command is for Windows only")

        self.collectstatic()
        if not args.keep_sessions:
            self.clearsessions(args)

        web_iis = self._get_python_dir() / "omero_web_iis.py"
        cmd = [sys.executable, str(web_iis)]
        if args.remove:
            cmd.append("remove")
        self.ctx.call(cmd)

try:
    register("web", WebControl, HELP)
except NameError:
    if __name__ == "__main__":
        cli = CLI()
        cli.register("web", WebControl, HELP)
        cli.invoke(sys.argv[1:])
Beispiel #50
0
                " left outer join i.fileset as fs" \
                " join fs.usedFiles as uf" \
                " join uf.originalFile as f" \
                " where i.id = :iid"
            query_out = query.projection(sql, params, {'omero.group': '-1'})
            if not query_out:
                self.ctx.die(602, 'Input image has no associated Fileset')
            if len(query_out) > 1:
                self.ctx.die(603, 'Input image has more than 1 associated '
                             'file: %s' % len(query_out))
            return unwrap(query_out[0])[0].id.val

        self.ctx.die(601, 'Invalid object input')

    def parse_object_id(self, object_type, value):

        pattern = r'%s:(?P<id>\d+)' % object_type
        pattern = re.compile('^' + pattern + '$')
        m = pattern.match(value)
        if not m:
            return
        return long(m.group('id'))

try:
    register("download", DownloadControl, HELP)
except NameError:
    if __name__ == "__main__":
        cli = CLI()
        cli.register("download", DownloadControl, HELP)
        cli.invoke(sys.argv[1:])
Beispiel #51
0
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='[]')
Beispiel #52
0
            rv = list()
            mapped.append(rv)
            if not s.isLoaded():
                rv.append("")
                rv.append("id=%s" % s.id.val)
                rv.append("")
                rv.append("")
                rv.append("")
                rv.append("insufficient privileges")
            else:
                rv.append(s.node.id)
                rv.append(s.uuid)
                rv.append(s.started)
                rv.append(s.owner.omeName)
                if s.userAgent is None:
                    rv.append("")
                else:
                    rv.append(s.userAgent)
                if client.getSessionId() == s.uuid.val:
                    rv.append("current session")
                else:
                    rv.append("")
        self.ctx.controls["hql"].display(mapped, ("node", "session", "started", "owner", "agent", "notes"))
try:
    register("admin", AdminControl, HELP)
except NameError:
    if __name__ == "__main__":
        cli = CLI()
        cli.register("admin", AdminControl, HELP)
        cli.invoke(sys.argv[1:])
Beispiel #53
0
        uid, username = self.parse_userid(a, args)
        groups = self.list_groups(a, args)
        groups = self.filter_groups(groups, uid, args.as_owner, True)

        for group in groups:
            if args.as_owner:
                self.addownersbyid(a, group, [uid])
            else:
                self.addusersbyid(a, group, [uid])

    def leavegroup(self, args):
        c = self.ctx.conn(args)
        a = c.sf.getAdminService()

        uid, username = self.parse_userid(a, args)
        groups = self.list_groups(a, args)
        groups = self.filter_groups(groups, uid, args.as_owner, False)

        for group in list(groups):
            if args.as_owner:
                self.removeownersbyid(a, group, [uid])
            else:
                self.removeusersbyid(a, group, [uid])
try:
    register("user", UserControl, HELP)
except NameError:
    if __name__ == "__main__":
        cli = CLI()
        cli.register("user", UserControl, HELP)
        cli.invoke(sys.argv[1:])
Beispiel #54
0
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.xfail
    @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.xfail
    @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 list(data[0].items()):
            self.invoke("set %s %s" % (k, v))
        self.invoke("list")
        self.assertStdoutStderr(capsys, out=data[1], strip_warning=True)

    @pytest.mark.xfail
    @pytest.mark.broken(reason="needs whitespace fixing")
    @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)  # 3 fail here
        self.invoke("get omero.ldap.base")
        self.invoke("set omero.ldap.base %s" % update)  # 1 here
        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'
Beispiel #55
0
        rv = dict()
        for k, v in values.items():
            if k.startswith("_"):
                rv[k[1:]] = v
            else:
                rv[k] = v
        return rv

    def project(self, querySvc, queryStr, params, ice_map):
        import omero
        try:
            rv = querySvc.projection(queryStr, params, ice_map)
            self.ctx.set("last.hql.rv", rv)
            return rv
        except omero.SecurityViolation, sv:
            if "omero.group" in ice_map:
                self.ctx.die(53, "SecurityViolation: Current user is not an admin and cannot use '--admin'")
            else:
                self.ctx.die(54, "SecurityViolation: %s" % sv)
        except omero.QueryException, qe:
            self.ctx.set("last.hql.rv", [])
            self.ctx.die(52, "Bad query: %s" % qe.message)

try:
    register("hql", HqlControl, HELP)
except NameError:
    if __name__ == "__main__":
        cli = CLI()
        cli.register("hql", HqlControl, HELP)
        cli.invoke(sys.argv[1:])
            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 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)
Beispiel #58
0
                rv.append("")
                rv.append("id=%s" % s.id.val)
                rv.append("")
                rv.append("")
                rv.append("")
                rv.append("insufficient privileges")
            else:
                rv.append(s.node.id)
                rv.append(s.uuid)
                rv.append(s.started)
                rv.append(s.owner.omeName)
                if s.userAgent is None:
                    rv.append("")
                else:
                    rv.append(s.userAgent)
                if client.getSessionId() == s.uuid.val:
                    rv.append("current session")
                else:
                    rv.append("")
        self.ctx.controls["hql"].display(
            mapped, ("node", "session", "started", "owner", "agent", "notes"))


try:
    register("admin", AdminControl, HELP)
except NameError:
    if __name__ == "__main__":
        cli = CLI()
        cli.register("admin", AdminControl, HELP)
        cli.invoke(sys.argv[1:])
Beispiel #59
0
                return

            remove = False
            offset = 0
            while True:
                rv = e.read(offset, DEFAULT_READ_LENGTH)
                if not rv:
                    break
                rv = rv[:min(DEFAULT_READ_LENGTH, l - offset)]
                offset += len(rv)
                handle.write(rv)

        finally:
            try:
                if not issysout:
                    handle.close()
                    if remove:
                        os.remove(handle.name)
            except Exception, e:
                self.ctx.err("Failed to close handle: %s" % e)

            e.close()

try:
    register("export", ExportControl, HELP)
except NameError:
    if __name__ == "__main__":
        cli = CLI()
        cli.register("export", ExportControl, HELP)
        cli.invoke(sys.argv[1:])