class TestImport(unittest.TestCase): def setUp(self): self.conf = PyConfigParser() self.dir = tempfile.mkdtemp() self.file = os.path.join(self.dir, 'extend.conf') def tearDown(self): shutil.rmtree(self.dir) def test_import_empty_file_with_inline_comment(self): with open(os.path.join(self.dir, 'base.conf'), 'w') as f: print('a = 1', file=f) with open(self.file, 'w') as f: print('from base import * # really', file=f) print('b = 1', file=f) self.conf.load_from_file(self.file) self.assertEqual(self.conf, dict(a=1, b=1)) def test_opened_files(self): with open(os.path.join(self.dir, 'base.conf'), 'w') as f: print('a = 1', file=f) with open(self.file, 'w') as f: print('from base import * # really', file=f) print('b = 1', file=f) self.conf.load_from_file(self.file) self.assertTrue(self.file in self.conf.opened_files) self.assertTrue( os.path.join(self.dir, 'base.conf') in self.conf.opened_files)
class TestImport(unittest.TestCase): def setUp(self): self.conf = PyConfigParser() self.dir = tempfile.mkdtemp() self.file = os.path.join(self.dir, 'extend.conf') def tearDown(self): shutil.rmtree(self.dir) def test_import_empty_file_with_inline_comment(self): with open(os.path.join(self.dir, 'base.conf'), 'w') as f: print('a = 1', file=f) with open(self.file, 'w') as f: print('from base import * # really', file=f) print('b = 1', file=f) self.conf.load_from_file(self.file) self.assertEqual(self.conf, dict(a=1, b=1)) def test_opened_files(self): with open(os.path.join(self.dir, 'base.conf'), 'w') as f: print('a = 1', file=f) with open(self.file, 'w') as f: print('from base import * # really', file=f) print('b = 1', file=f) self.conf.load_from_file(self.file) self.assertTrue(self.file in self.conf.opened_files) self.assertTrue(os.path.join(self.dir, 'base.conf') in self.conf.opened_files)
class TestClientCommandContainer(unittest.TestCase): def setUp(self): self.dir = tempfile.mkdtemp() self.conf = PyConfigParser() self.file = os.path.join(self.dir, 'test.conf') with open(self.file, 'w') as f: f.write(TEST_CONFIG) self.conf.load_from_file(self.file) def tearDown(self): shutil.rmtree(self.dir) def test_config_from_file(self): container = ClientCommandContainer(self.conf) values = { 'HUB_URL': 'https://localhost/hub/xmlrpc', 'AUTH_METHOD': 'krbv' } self.assertEqual(container.conf, values) def test_config_from_kwargs(self): container = ClientCommandContainer(self.conf, USERNAME='******') values = { 'HUB_URL': 'https://localhost/hub/xmlrpc', 'AUTH_METHOD': 'krbv', 'USERNAME': '******' } self.assertEqual(container.conf, values)
def test_login_gssapi_principal_needs_keytab(requests_session): """Login with gssapi method raises if principal is provided without keytab.""" hub_url = "https://hub.example.com/myapp/endpoint" conf = PyConfigParser() conf.load_from_dict( { "HUB_URL": hub_url, "AUTH_METHOD": "gssapi", "KRB_PRINCIPAL": "*****@*****.**", } ) transport = FakeTransport() logger = mock.Mock() proxy = HubProxy(conf, transport=transport, logger=logger) proxy._login(force=True) # This is pretty dumb: login() swallows all exceptions (probably for no good reason). # The only hint there was a problem is a DEBUG log message, so we detect the error # that way. logger.debug.assert_called_with( "Failed to create new session: Cannot specify a principal without a keytab" )
def test_no_auto_logout(requests_session): """auto_logout argument warns of deprecation""" conf = PyConfigParser() conf.load_from_dict({"HUB_URL": 'https://example.com/hub'}) transport = FakeTransport() with pytest.deprecated_call(): HubProxy(conf, transport=transport, auto_logout=True)
def setUp(self): self.dir = tempfile.mkdtemp() self.conf = PyConfigParser() self.file = os.path.join(self.dir, 'test.conf') with open(self.file, 'w') as f: f.write(TEST_CONFIG) self.conf.load_from_file(self.file)
def main(): conf = PyConfigParser() conf.load_from_file(os.path.expanduser(CONF_PATH)) container = BorgaCommandContainer(conf=conf) formatter = IndentedHelpFormatter(max_help_position=60, width=120) parser = CommandOptionParser(command_container=container, formatter=formatter, default_command='help') parser.run() sys.exit(0)
class TestConf(unittest.TestCase): def setUp(self): self.conf = PyConfigParser() self.conf.load_from_string(CONFIG) def test_integer(self): self.assertEqual(self.conf["integer1"], 1) self.assertEqual(self.conf["integer2"], -1) def test_float(self): self.assertEqual(self.conf["float1"], 1.0) self.assertEqual(self.conf["float2"], -1.0) def test_boolean(self): self.assertEqual(self.conf["boolean1"], True) self.assertEqual(self.conf["boolean2"], False) def test_string(self): self.assertEqual(self.conf["string1"], "a string") self.assertEqual(self.conf["string2"], "This is a string.") self.assertEqual(self.conf["string3"], "int: 1") self.assertEqual(self.conf["string4"], "float: 1.000000") self.assertEqual(self.conf["string5"], "1 True") def test_dict(self): self.assertEqual(self.conf["dict"], {"a": 1, "b": True, "c": None, True: False, "tuple": (1, "a"), "list": [1, "a"]}) def test_dict_with_default(self): self.assertEqual(self.conf.get_dict_value(self.conf["dict_with_default"], "a"), 1) self.assertEqual(get_dict_value(self.conf["dict_with_default"], "b"), 2) self.assertEqual(self.conf.get_dict_value(self.conf["dict_with_default"], "c"), 0) self.assertRaises(TypeError, self.conf.get_dict_value, self.conf["string1"], "a") self.assertRaises(KeyError, self.conf.get_dict_value, self.conf["dict"], "not_found") def test_dict_with_mask(self): dct = self.conf['dict_with_mask'] self.assertEqual(self.conf.get_dict_value(self.conf['dict_with_mask'], 'a'), 4) self.assertEqual(get_dict_value(self.conf['dict_with_mask'], 'a'), 4) self.assertEqual(self.conf.get_dict_value(self.conf['dict_with_mask'], 'c'), 3) self.assertEqual(self.conf.get_dict_value(self.conf['dict_with_mask'], 'a1'), 0) self.assertEqual(self.conf.get_dict_value(self.conf['dict_with_mask'], 'a22'), 0) self.assertEqual(self.conf.get_dict_value(self.conf['dict_with_mask'], 'b'), 5) self.assertRaises(KeyError, self.conf.get_dict_value, self.conf['dict_with_mask'], 'b1') # two masks corresponds self.assertEqual(self.conf.get_dict_value(self.conf['dict_with_mask'], 'b12'), 1) def test_tuple(self): self.assertEqual(self.conf["tuple1"], ()) self.assertEqual(self.conf["tuple2"], (1, )) self.assertEqual(self.conf["tuple3"], (1, "a")) def test_list(self): self.assertEqual(self.conf["list1"], []) self.assertEqual(self.conf["list2"], [1]) self.assertEqual(self.conf["list3"], [1, "a"])
def test_login_gssapi_krb_opts(requests_session): """Login with gssapi method prepares auth using correct gssapi parameters according to config.""" hub_url = "https://hub.example.com/myapp/endpoint" login_url = "https://hub.example.com/myapp/auth/krb5login/" conf = PyConfigParser() conf.load_from_dict({ "HUB_URL": hub_url, "AUTH_METHOD": "gssapi", "CA_CERT": "/some/ca-bundle.pem", "KRB_PRINCIPAL": "*****@*****.**", "KRB_SERVICE": "SVC", "KRB_REALM": "REALM.EXAMPLE.COM", "KRB_KEYTAB": "some-keytab", "KRB_CCACHE": "some-cache", }) transport = FakeTransport() proxy = HubProxy(conf, transport=transport) mock_get = requests_session.return_value.get calls_before = len(mock_get.mock_calls) with mock.patch("requests_gssapi.HTTPSPNEGOAuth") as mock_auth: with mock.patch("gssapi.Credentials") as mock_creds: # Force a login proxy._login(force=True) get_call = mock_get.mock_calls[calls_before] # It should have prepared credentials with the details from config mock_creds.assert_called_once_with( name=gssapi.Name("*****@*****.**", gssapi.NameType.kerberos_principal), store={ "client_keytab": "some-keytab", "ccache": "FILE:some-cache" }, usage="initiate", ) # It should have prepared auth with those credentials and our configured # server principal mock_auth.assert_called_once_with( creds=mock_creds.return_value, target_name=gssapi.Name("SVC/[email protected]", gssapi.NameType.kerberos_principal), ) # It should have used the configured CA bundle when issuing the request assert get_call[2]["verify"] == "/some/ca-bundle.pem"
def test_duplicate_keys(self): cfg = """foo = { "bar": 1, "bar": 2, } """ conf = PyConfigParser() with self.assertRaises(SyntaxError) as ctx: conf.load_from_string(cfg) self.assertEqual(str(ctx.exception), "Duplicate dict key 'bar' in file None on line 3")
def test_proxies_to_xmlrpc(requests_session): """HubProxy proxies to underlying XML-RPC ServerProxy""" conf = PyConfigParser() conf.load_from_dict({"HUB_URL": 'https://example.com/hub'}) transport = FakeTransport() proxy = HubProxy(conf, transport=transport) proxy.some_obj.some_method() # Last call should have invoked the method I requested (_, request_xml) = transport.fake_transport_calls[-1] assert b'some_obj.some_method' in request_xml
def test_login_gssapi(requests_session): """Login with gssapi method obtains session cookie via SPNEGO & krb5login.""" hub_url = "https://example.com/myapp/endpoint" login_url = "https://example.com/myapp/auth/krb5login/" conf = PyConfigParser() conf.load_from_dict({ "HUB_URL": hub_url, "AUTH_METHOD": "gssapi", }) transport = FakeTransport() proxy = HubProxy(conf, transport=transport) # Proxy might have already done some calls during initialization. # We're trying to test login in isolation, so keep track of how many # mock calls there have been already. mock_get = requests_session.return_value.get calls_before = len(mock_get.mock_calls) # Force a login proxy._login(force=True) # Cookies should have been shared between session and transport assert requests_session.return_value.cookies is transport.cookiejar # Check the requests done calls = mock_get.mock_calls[calls_before:] assert calls[0][0] == "" call_args = calls[0][1] call_kwargs = calls[0][2] # It should have made a request to log in assert call_args == (login_url, ) # It should have enabled SPNEGO auth. # More details about this object are verified in a separate test. assert "HTTPSPNEGOAuth" in str(type(call_kwargs["auth"])) # It should have verified the result assert calls[1][0] == "().raise_for_status" # And that's all assert len(calls) == 2
class TestUndefinedVariable(unittest.TestCase): def setUp(self): self.conf = PyConfigParser() def test_import_empty_file_with_inline_comment(self): cfg = ''' a = [ 'b', missing start of comment 'c', ] ''' with self.assertRaises(SyntaxError) as ctx: self.conf.load_from_string(cfg) self.assertRegexpMatches(str(ctx.exception), "Undefined variable 'missing': .+")
class TestImport(unittest.TestCase): def setUp(self): self.conf = PyConfigParser() self.dir = tempfile.mkdtemp() self.file = os.path.join(self.dir, 'extend.conf') def tearDown(self): shutil.rmtree(self.dir) def test_import_empty_file_with_inline_comment(self): with open(os.path.join(self.dir, 'base.conf'), 'w') as f: print('a = 1', file=f) with open(self.file, 'w') as f: print('from base import * # really', file=f) print('b = 1', file=f) self.conf.load_from_file(self.file) self.assertEqual(self.conf, dict(a=1, b=1)) def test_opened_files(self): with open(os.path.join(self.dir, 'base.conf'), 'w') as f: print('a = 1', file=f) with open(self.file, 'w') as f: print('from base import * # really', file=f) print('b = 1', file=f) self.conf.load_from_file(self.file) self.assertTrue(self.file in self.conf.opened_files) self.assertTrue( os.path.join(self.dir, 'base.conf') in self.conf.opened_files) def test_global_variables(self): with open(os.path.join(self.dir, 'base.conf'), 'w') as f: print('a = c', file=f) print('d = 5', file=f) print('global x', file=f) print('x = 6', file=f) with open(self.file, 'w') as f: print('global c', file=f) print('global d', file=f) print('global f', file=f) print('c = 42', file=f) print('d = 42', file=f) print('from base import * # really', file=f) self.conf.load_from_file(self.file) self.assertEqual(self.conf["a"], 42) self.assertEqual(self.conf["d"], 5) self.assertEqual(self.conf["x"], 6) self.assertEqual(self.conf["f"], None)
def setUp(self): self.conf = PyConfigParser()
def setUp(self): self.conf = PyConfigParser() self.dir = tempfile.mkdtemp() self.file = os.path.join(self.dir, 'extend.conf')
def conf(fixtures_dir): conf_file = os.path.join(fixtures_dir, 'basic.conf') conf = PyConfigParser() conf.load_from_file(conf_file) return conf
def setUp(self): self.conf = PyConfigParser() self.conf.load_from_string(CONFIG)