def test_config_positional(tmpconfigfile):
    """Test adding a positional argument on the command line.

    (There used to be a bug in adding positional arguments, fixed in
    7d10764.)
    """

    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe", "test.dat"]
    config = icat.config.Config()
    config.add_variable('datafile', ("datafile",), 
                        dict(metavar="input.dat", 
                             help="name of the input datafile"))
    conf = config.getconfig(args)

    attrs = [ a for a in sorted(conf.__dict__.keys()) if a[0] != '_' ]
    assert attrs == [ 'auth', 'checkCert', 'client_kwargs', 'configDir', 
                      'configFile', 'configSection', 'credentials', 
                      'datafile', 'http_proxy', 'https_proxy', 'no_proxy', 
                      'password', 'promptPass', 'url', 'username' ]

    assert conf.configFile == [tmpconfigfile.path]
    assert conf.configDir == tmpconfigfile.dir
    assert conf.configSection == "example_jdoe"
    assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl"
    assert conf.auth == "ldap"
    assert conf.username == "jdoe"
    assert conf.password == "pass"
    assert conf.promptPass == False
    assert conf.credentials == {'username': '******', 'password': '******'}
    assert conf.datafile == "test.dat"
def test_config_authinfo_anon(fakeClient, monkeypatch, tmpconfigfile):
    """Anon login example.

    Same as last test, but selecting the anon authenticator this time.
    """

    userkey = Namespace(name='username')
    passkey = Namespace(name='password', hide=True)
    authInfo = [
        Namespace(mnemonic="simple", admin=True, 
                  keys=[userkey, passkey]),
        Namespace(mnemonic="db", 
                  keys=[userkey, passkey]),
        Namespace(mnemonic="anon"),
    ]
    monkeypatch.setattr(FakeClient, "AuthInfo", authInfo)

    args = ["-c", tmpconfigfile.path, "-s", "example_root", "-a", "anon"]
    config = icat.config.Config(args=args)
    assert list(config.authenticatorInfo) == authInfo
    _, conf = config.getconfig()

    ex = ExpectedConf(configFile=[tmpconfigfile.path],
                      configSection="example_root",
                      url=ex_icat,
                      auth="anon",
                      promptPass=False,
                      credentials={})
    assert ex <= conf
    assert not hasattr(conf, 'username')
def test_config_authinfo_strange(fakeClient, monkeypatch, tmpconfigfile):
    """
    Talk to a server that requests strange credential keys.  Note the
    prefix "cred_" in the name of configuration variable and the
    command line option.
    """

    secretkey = Namespace(name='secret', hide=True)
    authInfo = [
        Namespace(mnemonic="quirks", 
                  keys=[secretkey]),
    ]
    monkeypatch.setattr(FakeClient, "AuthInfo", authInfo)

    args = ["-c", tmpconfigfile.path, "-s", "example_root", 
            "-a", "quirks", "--cred_secret", "geheim"]
    config = icat.config.Config(args=args)
    assert list(config.authenticatorInfo) == authInfo
    _, conf = config.getconfig()

    ex = ExpectedConf(configFile=[tmpconfigfile.path],
                      configSection="example_root",
                      url=ex_icat,
                      auth="quirks",
                      promptPass=False,
                      cred_secret="geheim",
                      credentials={'secret': 'geheim'})
    assert ex <= conf
    assert not hasattr(conf, 'username')
def test_config_subst_cmdline(fakeClient, tmpconfigfile):
    """Use a format string in a configuration variable.

    Same as above, but set the referenced variable from the command
    line.
    """

    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe", 
            "-u", "jonny", "-p", "pass"]
    config = icat.config.Config(args=args)
    config.add_variable('greeting', ("--greeting",), 
                        dict(help="Greeting message"),
                        subst=True)
    _, conf = config.getconfig()

    ex = ExpectedConf(configFile=[tmpconfigfile.path],
                      configSection="example_jdoe",
                      url=ex_icat,
                      auth="ldap",
                      username="******",
                      password="******",
                      promptPass=False,
                      greeting="Hello jonny!",
                      credentials={'username': '******', 'password': '******'})
    assert ex <= conf
Beispiel #5
0
def test_config_subst_cmdline(tmpconfigfile):
    """Use a format string in a configuration variable.

    Same as above, but set the referenced variable from the command
    line.
    """

    args = [
        "-c", tmpconfigfile.path, "-s", "example_jdoe", "-u", "jonny", "-p",
        "pass"
    ]
    config = icat.config.Config()
    config.add_variable('greeting', ("--greeting", ),
                        dict(help="Greeting message"),
                        subst=True)
    conf = config.getconfig(args)

    attrs = [a for a in sorted(conf.__dict__.keys()) if a[0] != '_']
    assert attrs == [
        'auth', 'checkCert', 'client_kwargs', 'configDir', 'configFile',
        'configSection', 'credentials', 'greeting', 'http_proxy',
        'https_proxy', 'no_proxy', 'password', 'promptPass', 'url', 'username'
    ]

    assert conf.configFile == [tmpconfigfile.path]
    assert conf.configDir == tmpconfigfile.dir
    assert conf.configSection == "example_jdoe"
    assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl"
    assert conf.auth == "ldap"
    assert conf.username == "jonny"
    assert conf.password == "pass"
    assert conf.promptPass == False
    assert conf.greeting == "Hello jonny!"
    assert conf.credentials == {'username': '******', 'password': '******'}
def test_config_custom_var(fakeClient, tmpconfigfile):
    """Define custom configuration variables.
    """

    # Note that ldap_filter is not defined in the configuration file,
    # but we have a default value defined here, so this is ok.
    args = ["-c", tmpconfigfile.path, "-s", "example_root"]
    config = icat.config.Config(args=args)
    config.add_variable('ldap_uri', ("-l", "--ldap-uri"), 
                        dict(help="URL of the LDAP server"),
                        envvar='LDAP_URI')
    config.add_variable('ldap_base', ("-b", "--ldap-base"), 
                        dict(help="base DN for searching the LDAP server"),
                        envvar='LDAP_BASE')
    config.add_variable('ldap_filter', ("-f", "--ldap-filter"), 
                        dict(help="search filter to select the user entries"),
                        default='(uid=*)')
    _, conf = config.getconfig()

    ex = ExpectedConf(configFile=[tmpconfigfile.path],
                      configSection="example_root",
                      url=ex_icat,
                      idsurl=ex_ids,
                      auth="simple",
                      username="******",
                      password="******",
                      promptPass=False,
                      ldap_uri="ldap://ldap.example.com",
                      ldap_base="ou=People,dc=example,dc=com",
                      ldap_filter="(uid=*)",
                      credentials={'username': '******', 'password': '******'})
    assert ex <= conf
def test_config_no_defaultvars(tmpconfigfile, monkeypatch):
    """Config object with no default variables.

    If `defaultvars=False` is passed to the constructor of Config, no
    default configuration variables will be defined other then
    `configFile` and `configSection`.  The configuration mechanism is
    still intact.  In particular, custom configuration variables may
    be defined and reading the configuration file still works.
    """

    # Manipulate the default search path.
    monkeypatch.setenv("HOME", tmpconfigfile.home)
    cfgdirs = [ os.path.expanduser("~/.config/icat"), 
                os.path.expanduser("~/.icat"), 
                "", ]
    monkeypatch.setattr(icat.config, "cfgdirs", cfgdirs)
    monkeypatch.chdir(tmpconfigfile.home)

    args = ["-s", "example_root"]
    config = icat.config.Config(defaultvars=False, args=args)
    config.add_variable('url', ("-w", "--url"), 
                        dict(help="URL to the web service description"))
    config.add_variable('wobble', ("--wobble",), 
                        dict(help="Strange thing"), 
                        optional=True)
    _, conf = config.getconfig()

    ex = ExpectedConf(configFile=[tmpconfigfile.path], 
                      configSection="example_root", 
                      url=ex_icat, 
                      wobble=None)
    assert ex <= conf
Beispiel #8
0
def test_config_positional(tmpconfigfile):
    """Test adding a positional argument on the command line.

    (There used to be a bug in adding positional arguments, fixed in
    7d10764.)
    """

    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe", "test.dat"]
    config = icat.config.Config()
    config.add_variable(
        'datafile', ("datafile", ),
        dict(metavar="input.dat", help="name of the input datafile"))
    conf = config.getconfig(args)

    attrs = [a for a in sorted(conf.__dict__.keys()) if a[0] != '_']
    assert attrs == [
        'auth', 'checkCert', 'client_kwargs', 'configDir', 'configFile',
        'configSection', 'credentials', 'datafile', 'http_proxy',
        'https_proxy', 'no_proxy', 'password', 'promptPass', 'url', 'username'
    ]

    assert conf.configFile == [tmpconfigfile.path]
    assert conf.configDir == tmpconfigfile.dir
    assert conf.configSection == "example_jdoe"
    assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl"
    assert conf.auth == "ldap"
    assert conf.username == "jdoe"
    assert conf.password == "pass"
    assert conf.promptPass == False
    assert conf.credentials == {'username': '******', 'password': '******'}
    assert conf.datafile == "test.dat"
def test_config_subst_cmdline(tmpconfigfile):
    """Use a format string in a configuration variable.

    Same as above, but set the referenced variable from the command
    line.
    """

    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe", 
            "-u", "jonny", "-p", "pass"]
    config = icat.config.Config()
    config.add_variable('greeting', ("--greeting",), 
                        dict(help="Greeting message"),
                        subst=True)
    conf = config.getconfig(args)

    attrs = [ a for a in sorted(conf.__dict__.keys()) if a[0] != '_' ]
    assert attrs == [ 'auth', 'checkCert', 'client_kwargs', 'configDir', 
                      'configFile', 'configSection', 'credentials', 
                      'greeting', 'http_proxy', 'https_proxy', 'no_proxy', 
                      'password', 'promptPass', 'url', 'username' ]

    assert conf.configFile == [tmpconfigfile.path]
    assert conf.configDir == tmpconfigfile.dir
    assert conf.configSection == "example_jdoe"
    assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl"
    assert conf.auth == "ldap"
    assert conf.username == "jonny"
    assert conf.password == "pass"
    assert conf.promptPass == False
    assert conf.greeting == "Hello jonny!"
    assert conf.credentials == {'username': '******', 'password': '******'}
def test_config_authinfo_simple(fakeClient, monkeypatch, tmpconfigfile):
    """Simple login example.

    Talking to a server that supports getAuthenticatorInfo.
    """

    userkey = Namespace(name='username')
    passkey = Namespace(name='password', hide=True)
    authInfo = [
        Namespace(mnemonic="simple", admin=True, 
                  keys=[userkey, passkey]),
        Namespace(mnemonic="db", 
                  keys=[userkey, passkey]),
        Namespace(mnemonic="anon"),
    ]
    monkeypatch.setattr(FakeClient, "AuthInfo", authInfo)

    args = ["-c", tmpconfigfile.path, "-s", "example_root"]
    config = icat.config.Config(args=args)
    assert list(config.authenticatorInfo) == authInfo
    _, conf = config.getconfig()

    ex = ExpectedConf(configFile=[tmpconfigfile.path],
                      configSection="example_root",
                      url=ex_icat,
                      auth="simple",
                      username="******",
                      password="******",
                      promptPass=False,
                      credentials={'username': '******', 'password': '******'})
    assert ex <= conf
Beispiel #11
0
def test_config_type_flag(tmpconfigfile):
    """Test the special configuration variable type flag.
    """

    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"]
    config = icat.config.Config(needlogin=False)
    config.add_variable('flag1', ("--flag1", ),
                        dict(help="Flag 1"),
                        type=icat.config.flag)
    config.add_variable('flag2', ("--flag2", ),
                        dict(help="Flag 2"),
                        type=icat.config.flag)
    conf = config.getconfig(args)

    attrs = [a for a in sorted(conf.__dict__.keys()) if a[0] != '_']
    assert attrs == [
        'checkCert', 'client_kwargs', 'configDir', 'configFile',
        'configSection', 'flag1', 'flag2', 'http_proxy', 'https_proxy',
        'no_proxy', 'url'
    ]

    assert conf.configFile == [tmpconfigfile.path]
    assert conf.configDir == tmpconfigfile.dir
    assert conf.configSection == "example_jdoe"
    assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl"
    assert conf.flag1 == True
    assert conf.flag2 == False

    # Now override the flags from the command line
    args = [
        "-c", tmpconfigfile.path, "-s", "example_jdoe", "--no-flag1", "--flag2"
    ]
    conf = config.getconfig(args)

    attrs = [a for a in sorted(conf.__dict__.keys()) if a[0] != '_']
    assert attrs == [
        'checkCert', 'client_kwargs', 'configDir', 'configFile',
        'configSection', 'flag1', 'flag2', 'http_proxy', 'https_proxy',
        'no_proxy', 'url'
    ]

    assert conf.configFile == [tmpconfigfile.path]
    assert conf.configDir == tmpconfigfile.dir
    assert conf.configSection == "example_jdoe"
    assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl"
    assert conf.flag1 == False
    assert conf.flag2 == True
def test_config_missing_mandatory(fakeClient):
    """Not providing any config at all.

    This throws an error as url is mandatory.
    """
    config = icat.config.Config(needlogin=False, ids=False, args=[])
    with pytest.raises(icat.exception.ConfigError) as err:
        _, conf = config.getconfig()
    assert "Config option 'url' not given" in str(err.value)
def test_config_type_flag(fakeClient, tmpconfigfile, flags, ex):
    """Test the special configuration variable type flag.
    """
    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"] + flags
    config = icat.config.Config(needlogin=False, args=args)
    config.add_variable('flag1', ("--flag1",), 
                        dict(help="Flag 1"), type=icat.config.flag)
    config.add_variable('flag2', ("--flag2",), 
                        dict(help="Flag 2"), type=icat.config.flag)
    _, conf = config.getconfig()
    assert ex <= conf
Beispiel #14
0
def test_config_type_boolean(tmpconfigfile):
    """Test a boolean configuration variable.
    """

    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"]
    config = icat.config.Config(needlogin=False)
    config.add_variable('flag1', ("--flag1",), 
                        dict(help="Flag 1", action='store_const', const=True), 
                        type=icat.config.boolean)
    config.add_variable('flag2', ("--flag2",), 
                        dict(help="Flag 2", action='store_const', const=True), 
                        type=icat.config.boolean)
    conf = config.getconfig(args)

    attrs = [ a for a in sorted(conf.__dict__.keys()) if a[0] != '_' ]
    assert attrs == [ 'checkCert', 'client_kwargs', 'configDir', 
                      'configFile', 'configSection', 'flag1', 'flag2', 
                      'http_proxy', 'https_proxy', 'no_proxy', 'url' ]

    assert conf.configFile == [tmpconfigfile.path]
    assert conf.configDir == tmpconfigfile.dir
    assert conf.configSection == "example_jdoe"
    assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl"
    assert conf.flag1 == True
    assert conf.flag2 == False

    # Now override flag2 from the command line
    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe", "--flag2"]
    conf = config.getconfig(args)

    attrs = [ a for a in sorted(conf.__dict__.keys()) if a[0] != '_' ]
    assert attrs == [ 'checkCert', 'client_kwargs', 'configDir', 
                      'configFile', 'configSection', 'flag1', 'flag2', 
                      'http_proxy', 'https_proxy', 'no_proxy', 'url' ]

    assert conf.configFile == [tmpconfigfile.path]
    assert conf.configDir == tmpconfigfile.dir
    assert conf.configSection == "example_jdoe"
    assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl"
    assert conf.flag1 == True
    assert conf.flag2 == True
def test_config_minimal(fakeClient):
    """Minimal example.

    No login credentials, only relevant config option is the url which
    is provided as command line argument.
    """

    args = ["-w", ex_icat]
    config = icat.config.Config(needlogin=False, ids=False, args=args)
    _, conf = config.getconfig()

    assert ExpectedConf(configSection=None, url=ex_icat) <= conf
def test_config_type_boolean(fakeClient, tmpconfigfile, flags, ex):
    """Test a boolean configuration variable.
    """
    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"] + flags
    config = icat.config.Config(needlogin=False, args=args)
    config.add_variable('flag1', ("--flag1",), 
                        dict(help="Flag 1", action='store_const', const=True), 
                        type=icat.config.boolean)
    config.add_variable('flag2', ("--flag2",), 
                        dict(help="Flag 2", action='store_const', const=True), 
                        type=icat.config.boolean)
    _, conf = config.getconfig()
    assert ex <= conf
Beispiel #17
0
def test_config_type_int_err(tmpconfigfile):
    """Read an integer variable from the configuration file.

    Same as last one, but have an invalid value this time.
    """

    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"]
    config = icat.config.Config(needlogin=False)
    config.add_variable('invnum', ("--invnum",), 
                        dict(help="Integer variable"), type=int)
    with pytest.raises(icat.exception.ConfigError) as err:
        conf = config.getconfig(args)
    assert 'invalid int value' in str(err.value)
Beispiel #18
0
def getConfig(confSection="root", **confArgs):
    """Get the configuration, skip on ConfigError.
    """
    confFile = os.path.join(testdir, "data", "icat.cfg")
    if not os.path.isfile(confFile):
        pytest.skip("no test ICAT server configured")
    try:
        confArgs['args'] = ["-c", confFile, "-s", confSection]
        config = icat.config.Config(**confArgs)
        client, conf = config.getconfig()
        conf.cmdargs = ["-c", conf.configFile[0], "-s", conf.configSection]
        return (client, conf, config)
    except icat.ConfigError as err:
        pytest.skip(err.message)
Beispiel #19
0
def test_config_type_int_err(tmpconfigfile):
    """Read an integer variable from the configuration file.

    Same as last one, but have an invalid value this time.
    """

    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"]
    config = icat.config.Config(needlogin=False)
    config.add_variable('invnum', ("--invnum", ),
                        dict(help="Integer variable"),
                        type=int)
    with pytest.raises(icat.exception.ConfigError) as err:
        conf = config.getconfig(args)
    assert 'invalid int value' in str(err.value)
def test_config_type_int(fakeClient, tmpconfigfile):
    """Read an integer variable from the configuration file.
    """

    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"]
    config = icat.config.Config(needlogin=False, args=args)
    config.add_variable('num', ("--num",), 
                        dict(help="Integer variable"), type=int)
    _, conf = config.getconfig()

    ex = ExpectedConf(configFile=[tmpconfigfile.path],
                      configSection="example_jdoe",
                      url=ex_icat,
                      num=42)
    assert ex <= conf
def test_config_cfgpath_cmdline(fakeClient, tmpconfigfile, monkeypatch, 
                                tmpfiles, abspath):
    """Test a cfgpath configuration variable.

    Same as test_config_cfgpath_cwd() but override the path on the
    command line.
    """

    # Manipulate the default search path.
    monkeypatch.setenv("HOME", tmpconfigfile.home)
    cfgdirs = [ os.path.expanduser("~/.config/icat"), 
                os.path.expanduser("~/.icat"), 
                "", ]
    monkeypatch.setattr(icat.config, "cfgdirs", cfgdirs)
    monkeypatch.chdir(tmpconfigfile.home)
    cpath = os.path.expanduser("~/.config/icat/control.dat")
    tmpfiles.addfile(cpath, "control config dir\n")
    hpath = os.path.join(tmpconfigfile.home, "control.dat")
    tmpfiles.addfile(hpath, "control home\n")
    if abspath:
        apath = os.path.expanduser("~/custom/cl.dat")
        cfarg = apath
    else:
        apath = os.path.expanduser("~/.config/icat/cl.dat")
        cfarg = "cl.dat"
    tmpfiles.addfile(apath, "control cmdline\n")

    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe", 
            "--control", cfarg]
    config = icat.config.Config(args=args)
    config.add_variable('controlfile', ("--control",), 
                    dict(metavar="control.dat", help="control file"), 
                    default="control.dat", type=icat.config.cfgpath)
    _, conf = config.getconfig()

    ex = ExpectedConf(configFile=[tmpconfigfile.path],
                      configSection="example_jdoe",
                      url=ex_icat,
                      auth="ldap",
                      username="******",
                      password="******",
                      promptPass=False,
                      credentials={'username': '******', 'password': '******'},
                      controlfile=apath)
    assert ex <= conf
    assert os.path.isfile(conf.controlfile)
def test_config_subst_confdir(fakeClient, tmpconfigfile):
    """Substitute configDir in the default of a variable.

    Note that configDir is deprecated and will be removed in version 1.0.
    """

    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"]
    config = icat.config.Config(needlogin=False, args=args)
    config.add_variable('extracfg', ("--extracfg",), 
                        dict(help="Extra config file"),
                        default="%(configDir)s/extra.xml", subst=True)
    _, conf = config.getconfig()

    ex = ExpectedConf(configFile=[tmpconfigfile.path],
                      configDir=tmpconfigfile.dir,
                      configSection="example_jdoe",
                      url=ex_icat)
    assert ex <= conf
    assert os.path.dirname(conf.extracfg) == tmpconfigfile.dir
def test_config_client_kwargs(fakeClient, tmpconfigfile, monkeypatch):
    """Test client_kwargs attribute of config.

    Issue #38: There should be a way to access the kwargs used to
    create the client in config.  The resolution was to add this
    attribute to the config object.
    """

    # Manipulate the default search path.
    monkeypatch.setenv("HOME", tmpconfigfile.home)
    cfgdirs = [ os.path.expanduser("~/.config/icat"), 
                os.path.expanduser("~/.icat"), 
                "", ]
    monkeypatch.setattr(icat.config, "cfgdirs", cfgdirs)
    monkeypatch.chdir(tmpconfigfile.home)

    # Add proxy settings just to have non-trivial content in client_kwargs.
    monkeypatch.setenv("http_proxy", "http://www-cache.example.org:3128/")
    monkeypatch.setenv("https_proxy", "http://www-cache.example.org:3128/")
    monkeypatch.setenv("no_proxy", "localhost, .example.org")

    args = ["-c", tmpconfigfile.path, "-s", "example_root"]
    config = icat.config.Config(args=args)
    client, conf = config.getconfig()

    ex = ExpectedConf(configFile=[tmpconfigfile.path],
                      configSection="example_root",
                      url=ex_icat,
                      idsurl=ex_ids,
                      http_proxy="http://www-cache.example.org:3128/",
                      https_proxy="http://www-cache.example.org:3128/",
                      no_proxy="localhost, .example.org",
                      auth="simple",
                      username="******",
                      password="******",
                      promptPass=False,
                      credentials={'username': '******', 'password': '******'})
    assert ex <= conf
    # create a second, independent client object and check that it
    # has been created using the same arguments.
    client2 = FakeClient(conf.url, **config.client_kwargs)
    assert client2 == client
Beispiel #24
0
def test_config_type_int(tmpconfigfile):
    """Read an integer variable from the configuration file.
    """

    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"]
    config = icat.config.Config(needlogin=False)
    config.add_variable('num', ("--num",), 
                        dict(help="Integer variable"), type=int)
    conf = config.getconfig(args)

    attrs = [ a for a in sorted(conf.__dict__.keys()) if a[0] != '_' ]
    assert attrs == [ 'checkCert', 'client_kwargs', 'configDir', 
                      'configFile', 'configSection', 'http_proxy', 
                      'https_proxy', 'no_proxy', 'num', 'url' ]

    assert conf.configFile == [tmpconfigfile.path]
    assert conf.configDir == tmpconfigfile.dir
    assert conf.configSection == "example_jdoe"
    assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl"
    assert conf.num == 42
def test_config_authinfo_no_authinfo(fakeClient, monkeypatch, tmpconfigfile):
    """
    Talk to an old server that does not support getAuthenticatorInfo.
    """

    args = ["-c", tmpconfigfile.path, "-s", "example_root"]
    config = icat.config.Config(args=args)
    client, conf = config.getconfig()

    with pytest.raises(icat.exception.VersionMethodError) as err:
        authInfo = client.getAuthenticatorInfo()

    ex = ExpectedConf(configFile=[tmpconfigfile.path],
                      configSection="example_root",
                      url=ex_icat,
                      auth="simple",
                      username="******",
                      password="******",
                      promptPass=False,
                      credentials={'username': '******', 'password': '******'})
    assert ex <= conf
def test_config_disable(fakeClient, tmpconfigfile):
    """Configuration variables may be disabled.

    Note that this feature is used internally in config and not
    intended to be used in client code.
    """

    args = ["-c", tmpconfigfile.path, "-s", "example_root"]
    config = icat.config.Config(args=args)
    config.confvariable['promptPass'].disabled = True
    _, conf = config.getconfig()

    ex = ExpectedConf(configFile=[tmpconfigfile.path],
                      configSection="example_root",
                      url=ex_icat,
                      auth="simple",
                      username="******",
                      password="******",
                      credentials={'username': '******', 'password': '******'})
    assert ex <= conf
    assert not hasattr(conf, 'promptPass')
def test_config_minimal_file(fakeClient, tmpconfigfile, monkeypatch):
    """Minimal example.

    Almost the same as test_config_minimal(), but read the url from
    a config file this time.
    """

    # Let the config file be found in the default location, but
    # manipulate the search path such that only the cwd exists.
    cfgdirs = [ os.path.join(tmpconfigfile.dir, "wobble"), "" ]
    monkeypatch.setattr(icat.config, "cfgdirs", cfgdirs)
    monkeypatch.chdir(tmpconfigfile.dir)

    args = ["-s", "example_root"]
    config = icat.config.Config(needlogin=False, ids=False, args=args)
    _, conf = config.getconfig()

    ex = ExpectedConf(configFile=["icat.cfg"],
                      configSection="example_root", 
                      url=ex_icat)
    assert ex <= conf
Beispiel #28
0
def test_config_subst_confdir(tmpconfigfile):
    """Substitute configDir in the default of a variable.
    """

    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"]
    config = icat.config.Config(needlogin=False)
    config.add_variable('extracfg', ("--extracfg",), 
                        dict(help="Extra config file"),
                        default="%(configDir)s/extra.xml", subst=True)
    conf = config.getconfig(args)

    attrs = [ a for a in sorted(conf.__dict__.keys()) if a[0] != '_' ]
    assert attrs == [ 'checkCert', 'client_kwargs', 'configDir', 
                      'configFile', 'configSection', 'extracfg', 
                      'http_proxy', 'https_proxy', 'no_proxy', 'url' ]

    assert conf.configFile == [tmpconfigfile.path]
    assert conf.configDir == tmpconfigfile.dir
    assert conf.configSection == "example_jdoe"
    assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl"
    assert os.path.dirname(conf.extracfg) == tmpconfigfile.dir
Beispiel #29
0
def test_config_custom_var(tmpconfigfile):
    """Define custom configuration variables.
    """

    # Note that ldap_filter is not defined in the configuration file,
    # but we have a default value defined here, so this is ok.
    args = ["-c", tmpconfigfile.path, "-s", "example_root"]
    config = icat.config.Config()
    config.add_variable('ldap_uri', ("-l", "--ldap-uri"),
                        dict(help="URL of the LDAP server"),
                        envvar='LDAP_URI')
    config.add_variable('ldap_base', ("-b", "--ldap-base"),
                        dict(help="base DN for searching the LDAP server"),
                        envvar='LDAP_BASE')
    config.add_variable('ldap_filter', ("-f", "--ldap-filter"),
                        dict(help="search filter to select the user entries"),
                        default='(uid=*)')
    conf = config.getconfig(args)

    attrs = [a for a in sorted(conf.__dict__.keys()) if a[0] != '_']
    assert attrs == [
        'auth', 'checkCert', 'client_kwargs', 'configDir', 'configFile',
        'configSection', 'credentials', 'http_proxy', 'https_proxy',
        'ldap_base', 'ldap_filter', 'ldap_uri', 'no_proxy', 'password',
        'promptPass', 'url', 'username'
    ]

    assert conf.configFile == [tmpconfigfile.path]
    assert conf.configDir == tmpconfigfile.dir
    assert conf.configSection == "example_root"
    assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl"
    assert conf.auth == "simple"
    assert conf.username == "root"
    assert conf.password == "secret"
    assert conf.promptPass == False
    assert conf.ldap_uri == "ldap://ldap.example.com"
    assert conf.ldap_base == "ou=People,dc=example,dc=com"
    assert conf.ldap_filter == "(uid=*)"
    assert conf.credentials == {'username': '******', 'password': '******'}
Beispiel #30
0
def test_config_type_int(tmpconfigfile):
    """Read an integer variable from the configuration file.
    """

    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"]
    config = icat.config.Config(needlogin=False)
    config.add_variable('num', ("--num", ),
                        dict(help="Integer variable"),
                        type=int)
    conf = config.getconfig(args)

    attrs = [a for a in sorted(conf.__dict__.keys()) if a[0] != '_']
    assert attrs == [
        'checkCert', 'client_kwargs', 'configDir', 'configFile',
        'configSection', 'http_proxy', 'https_proxy', 'no_proxy', 'num', 'url'
    ]

    assert conf.configFile == [tmpconfigfile.path]
    assert conf.configDir == tmpconfigfile.dir
    assert conf.configSection == "example_jdoe"
    assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl"
    assert conf.num == 42
Beispiel #31
0
def test_config_custom_var(tmpconfigfile):
    """Define custom configuration variables.
    """

    # Note that ldap_filter is not defined in the configuration file,
    # but we have a default value defined here, so this is ok.
    args = ["-c", tmpconfigfile.path, "-s", "example_root"]
    config = icat.config.Config()
    config.add_variable('ldap_uri', ("-l", "--ldap-uri"), 
                        dict(help="URL of the LDAP server"),
                        envvar='LDAP_URI')
    config.add_variable('ldap_base', ("-b", "--ldap-base"), 
                        dict(help="base DN for searching the LDAP server"),
                        envvar='LDAP_BASE')
    config.add_variable('ldap_filter', ("-f", "--ldap-filter"), 
                        dict(help="search filter to select the user entries"),
                        default='(uid=*)')
    conf = config.getconfig(args)

    attrs = [ a for a in sorted(conf.__dict__.keys()) if a[0] != '_' ]
    assert attrs == [ 'auth', 'checkCert', 'client_kwargs', 'configDir', 
                      'configFile', 'configSection', 'credentials', 
                      'http_proxy', 'https_proxy', 'ldap_base', 
                      'ldap_filter', 'ldap_uri', 'no_proxy', 'password', 
                      'promptPass', 'url', 'username' ]

    assert conf.configFile == [tmpconfigfile.path]
    assert conf.configDir == tmpconfigfile.dir
    assert conf.configSection == "example_root"
    assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl"
    assert conf.auth == "simple"
    assert conf.username == "root"
    assert conf.password == "secret"
    assert conf.promptPass == False
    assert conf.ldap_uri == "ldap://ldap.example.com"
    assert conf.ldap_base == "ou=People,dc=example,dc=com"
    assert conf.ldap_filter == "(uid=*)"
    assert conf.credentials == {'username': '******', 'password': '******'}
def test_config_authinfo_invalid_auth(fakeClient, monkeypatch, tmpconfigfile):
    """
    Try to use an invalid authenticator.

    Issue #41: AttributeError is raised during internal error handling.
    """

    userkey = Namespace(name='username')
    passkey = Namespace(name='password', hide=True)
    authInfo = [
        Namespace(mnemonic="simple", admin=True, 
                  keys=[userkey, passkey]),
        Namespace(mnemonic="db", 
                  keys=[userkey, passkey]),
        Namespace(mnemonic="anon"),
    ]
    monkeypatch.setattr(FakeClient, "AuthInfo", authInfo)

    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"]
    config = icat.config.Config(args=args)
    with pytest.raises(icat.exception.ConfigError) as err:
        _, conf = config.getconfig()
    assert "No such authenticator 'ldap'" in str(err.value)
def test_config_subst_nosubst(fakeClient, tmpconfigfile):
    """Use a format string in a configuration variable.

    But disable the substitution.
    """

    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"]
    config = icat.config.Config(args=args)
    config.add_variable('greeting', ("--greeting",), 
                        dict(help="Greeting message"),
                        subst=False)
    _, conf = config.getconfig()

    ex = ExpectedConf(configFile=[tmpconfigfile.path],
                      configSection="example_jdoe",
                      url=ex_icat,
                      auth="ldap",
                      username="******",
                      password="******",
                      promptPass=False,
                      greeting="Hello %(username)s!",
                      credentials={'username': '******', 'password': '******'})
    assert ex <= conf
def test_config_authinfo_anon_only(fakeClient, monkeypatch, tmpconfigfile):
    """
    Talk to a server that supports getAuthenticatorInfo and has only
    the anon authenticator.
    """

    authInfo = [
        Namespace(mnemonic="anon"),
    ]
    monkeypatch.setattr(FakeClient, "AuthInfo", authInfo)

    args = ["-c", tmpconfigfile.path, "-s", "example_anon"]
    config = icat.config.Config(args=args)
    assert list(config.authenticatorInfo) == authInfo
    _, conf = config.getconfig()

    ex = ExpectedConf(configFile=[tmpconfigfile.path],
                      configSection="example_anon",
                      url=ex_icat,
                      auth="anon",
                      credentials={})
    assert ex <= conf
    assert not hasattr(conf, 'promptPass')
    assert not hasattr(conf, 'username')
def test_config_minimal_defaultfile(fakeClient, tmpconfigfile, monkeypatch):
    """Minimal example.

    Almost the same as test_config_minimal_file(), but let the
    configuration file be found in the default search path rather then
    pointing to the full path.
    """

    # Manipulate the default search path.
    monkeypatch.setenv("HOME", tmpconfigfile.home)
    cfgdirs = [ os.path.expanduser("~/.config/icat"), 
                os.path.expanduser("~/.icat"), 
                "", ]
    monkeypatch.setattr(icat.config, "cfgdirs", cfgdirs)
    monkeypatch.chdir(tmpconfigfile.home)

    args = ["-s", "example_root"]
    config = icat.config.Config(needlogin=False, ids=False, args=args)
    _, conf = config.getconfig()

    ex = ExpectedConf(configFile=[tmpconfigfile.path], 
                      configSection="example_root", 
                      url=ex_icat)
    assert ex <= conf
Beispiel #36
0
def test_config_subst_confdir(tmpconfigfile):
    """Substitute configDir in the default of a variable.
    """

    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"]
    config = icat.config.Config(needlogin=False)
    config.add_variable('extracfg', ("--extracfg", ),
                        dict(help="Extra config file"),
                        default="%(configDir)s/extra.xml",
                        subst=True)
    conf = config.getconfig(args)

    attrs = [a for a in sorted(conf.__dict__.keys()) if a[0] != '_']
    assert attrs == [
        'checkCert', 'client_kwargs', 'configDir', 'configFile',
        'configSection', 'extracfg', 'http_proxy', 'https_proxy', 'no_proxy',
        'url'
    ]

    assert conf.configFile == [tmpconfigfile.path]
    assert conf.configDir == tmpconfigfile.dir
    assert conf.configSection == "example_jdoe"
    assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl"
    assert os.path.dirname(conf.extracfg) == tmpconfigfile.dir
def test_config_positional(fakeClient, tmpconfigfile):
    """Test adding a positional argument on the command line.

    (There used to be a bug in adding positional arguments, fixed in
    7d10764.)
    """

    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe", "test.dat"]
    config = icat.config.Config(args=args)
    config.add_variable('datafile', ("datafile",), 
                        dict(metavar="input.dat", 
                             help="name of the input datafile"))
    _, conf = config.getconfig()

    ex = ExpectedConf(configFile=[tmpconfigfile.path],
                      configSection="example_jdoe",
                      url=ex_icat,
                      auth="ldap",
                      username="******",
                      password="******",
                      promptPass=False,
                      credentials={'username': '******', 'password': '******'},
                      datafile="test.dat")
    assert ex <= conf
def test_config_cfgpath_default(fakeClient, tmpconfigfile, monkeypatch, 
                                tmpfiles):
    """Test a cfgpath configuration variable.

    This searches a file in the default configuration directories.
    Feature added in Issue #30.
    """

    # Manipulate the default search path.
    monkeypatch.setenv("HOME", tmpconfigfile.home)
    cfgdirs = [ os.path.expanduser("~/.config/icat"), 
                os.path.expanduser("~/.icat"), 
                "", ]
    monkeypatch.setattr(icat.config, "cfgdirs", cfgdirs)
    monkeypatch.chdir(tmpconfigfile.home)
    cpath = os.path.expanduser("~/.config/icat/control.dat")
    tmpfiles.addfile(cpath, "control\n")

    args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"]
    config = icat.config.Config(args=args)
    config.add_variable('controlfile', ("--control",), 
                    dict(metavar="control.dat", help="control file"), 
                    default="control.dat", type=icat.config.cfgpath)
    _, conf = config.getconfig()

    ex = ExpectedConf(configFile=[tmpconfigfile.path],
                      configSection="example_jdoe",
                      url=ex_icat,
                      auth="ldap",
                      username="******",
                      password="******",
                      promptPass=False,
                      credentials={'username': '******', 'password': '******'},
                      controlfile=cpath)
    assert ex <= conf
    assert os.path.isfile(conf.controlfile)
logging.basicConfig(level=logging.INFO)
#logging.getLogger('suds.client').setLevel(logging.DEBUG)

formats = icat.dumpfile.Backends.keys()
config = icat.config.Config()
config.add_variable('file', ("-o", "--outputfile"),
                    dict(help="output file name or '-' for stdout"),
                    default='-')
config.add_variable('format', ("-f", "--format"),
                    dict(help="output file format", choices=formats),
                    default='YAML')
config.add_variable(
    'investigation', ("investigation", ),
    dict(help="name and optionally visit id "
         "(separated by a colon) of the investigation"))
conf = config.getconfig()

client = icat.Client(conf.url, **conf.client_kwargs)
if client.apiversion < '4.3.99':
    raise RuntimeError(
        "Sorry, ICAT version %s is too old, need 4.4.0 or newer." %
        client.apiversion)
client.login(conf.auth, conf.credentials)

# ------------------------------------------------------------
# helper
# ------------------------------------------------------------


def getinvestigation(invid):
    """Search the investigation id from name and optionally visitid."""
Beispiel #40
0
config = icat.config.Config()
config.add_variable('mainStorageBase', ("--mainStorageBase", ),
                    dict(help="base directory of the main storage area"))
config.add_variable('archiveStorageBase', ("--archiveStorageBase", ),
                    dict(help="base directory of the archive storage area"))
config.add_variable('datafileFormats', ("--datafileformats", ),
                    dict(help="configuration file for DatafileFormats"),
                    default="%(configDir)s/fileformats.xml",
                    subst=True)
config.add_variable('investigation', ("investigation", ),
                    dict(help="the proposal number"))
config.add_variable('dataset', ("dataset", ), dict(help="name of the dataset"))
config.add_variable('files', ("files", ),
                    dict(help="name of the files to add", nargs="+"))
client, conf = config.getconfig()

client.login(conf.auth, conf.credentials)

mainStorage = MainFileStorage(conf.mainStorageBase)
archiveStorage = ArchiveFileStorage(conf.archiveStorageBase)

dff = FormatChecker(client, conf.datafileFormats)

os.umask(0o007)

# ------------------------------------------------------------
# class zipcreate.
# ------------------------------------------------------------