Beispiel #1
0
class SfConfigParser(object):
    def __init__(self, config_path=None):
        if not config_path:
            config_path = os.path.join(get_config_dir(), "config")
        self.az_config = AzConfig()
        self.az_config.config_parser.read(config_path)

    def no_verify_setting(self):
        return self.az_config.get("servicefabric", "no_verify", fallback="False") == "True"

    def ca_cert_info(self):
        using_ca = self.az_config.get("servicefabric", "use_ca", fallback="False")
        if using_ca == "True":
            return self.az_config.get("servicefabric", "ca_path", fallback=None)
        return None

    def connection_endpoint(self):
        return self.az_config.get("servicefabric", "endpoint", fallback=None)

    def cert_info(self):
        security_type = str(self.az_config.get("servicefabric", "security", fallback=""))
        if security_type == "pem":
            return self.az_config.get("servicefabric", "pem_path", fallback=None)
        elif security_type == "cert":
            cert_path = self.az_config.get("servicefabric", "cert_path", fallback=None)
            key_path = self.az_config.get("servicefabric", "key_path", fallback=None)
            return cert_path, key_path
        elif security_type == "none":
            return None
        else:
            raise CLIError("Cluster security type not set")
Beispiel #2
0
class SfConfigParser(object):
    def __init__(self, config_path=None):
        if not config_path:
            config_path = os.path.join(get_config_dir(), "config")
        self.az_config = AzConfig()
        self.az_config.config_parser.read(config_path)

    def no_verify_setting(self):
        return self.az_config.get("servicefabric",
                                  "no_verify",
                                  fallback="False") == "True"

    def ca_cert_info(self):
        using_ca = self.az_config.get("servicefabric",
                                      "use_ca",
                                      fallback="False")
        if using_ca == "True":
            return self.az_config.get("servicefabric",
                                      "ca_path",
                                      fallback=None)
        return None

    def connection_endpoint(self):
        return self.az_config.get("servicefabric", "endpoint", fallback=None)

    def cert_info(self):
        security_type = str(
            self.az_config.get("servicefabric", "security", fallback=""))
        if security_type == "pem":
            return self.az_config.get("servicefabric",
                                      "pem_path",
                                      fallback=None)
        elif security_type == "cert":
            cert_path = self.az_config.get("servicefabric",
                                           "cert_path",
                                           fallback=None)
            key_path = self.az_config.get("servicefabric",
                                          "key_path",
                                          fallback=None)
            return cert_path, key_path
        elif security_type == "none":
            return None
        else:
            raise CLIError("Cluster security type not set")
Beispiel #3
0
class TestAzConfig(unittest.TestCase):

    def setUp(self):
        self.az_config = AzConfig()

    def test_has_option(self):
        section = 'MySection'
        option = 'myoption'
        value = 'myvalue'
        self.az_config.config_parser.add_section(section)
        self.az_config.config_parser.set(section, option, value)
        self.assertTrue(self.az_config.has_option(section, option))

    @mock.patch.dict(os.environ, {AzConfig.env_var_name('MySection', 'myoption'): 'myvalue'})
    def test_has_option_env(self):
        section = 'MySection'
        option = 'myoption'
        self.assertTrue(self.az_config.has_option(section, option))

    def test_has_option_env_no(self):
        section = 'MySection'
        option = 'myoption'
        self.assertFalse(self.az_config.has_option(section, option))

    def test_get(self):
        section = 'MySection'
        option = 'myoption'
        value = 'myvalue'
        self.az_config.config_parser.add_section(section)
        self.az_config.config_parser.set(section, option, value)
        self.assertEqual(self.az_config.get(section, option), value)

    @mock.patch.dict(os.environ, {AzConfig.env_var_name('MySection', 'myoption'): 'myvalue'})
    def test_get_env(self):
        section = 'MySection'
        option = 'myoption'
        value = 'myvalue'
        self.assertEqual(self.az_config.get(section, option), value)

    def test_get_not_found_section(self):
        section = 'MySection'
        option = 'myoption'
        with self.assertRaises(configparser.NoSectionError):
            self.az_config.get(section, option)

    def test_get_not_found_option(self):
        section = 'MySection'
        option = 'myoption'
        self.az_config.config_parser.add_section(section)
        with self.assertRaises(configparser.NoOptionError):
            self.az_config.get(section, option)

    def test_get_fallback(self):
        section = 'MySection'
        option = 'myoption'
        self.assertEqual(self.az_config.get(section, option, fallback='fallback'), 'fallback')

    def test_getint(self):
        section = 'MySection'
        option = 'myoption'
        value = '123'
        self.az_config.config_parser.add_section(section)
        self.az_config.config_parser.set(section, option, value)
        self.assertEqual(self.az_config.getint(section, option), int(value))

    def test_getint_error(self):
        section = 'MySection'
        option = 'myoption'
        value = 'not_an_int'
        self.az_config.config_parser.add_section(section)
        self.az_config.config_parser.set(section, option, value)
        with self.assertRaises(ValueError):
            self.az_config.getint(section, option)

    def test_getfloat(self):
        section = 'MySection'
        option = 'myoption'
        value = '123.456'
        self.az_config.config_parser.add_section(section)
        self.az_config.config_parser.set(section, option, value)
        self.assertEqual(self.az_config.getfloat(section, option), float(value))

    def test_getfloat_error(self):
        section = 'MySection'
        option = 'myoption'
        value = 'not_a_float'
        self.az_config.config_parser.add_section(section)
        self.az_config.config_parser.set(section, option, value)
        with self.assertRaises(ValueError):
            self.az_config.getfloat(section, option)

    def test_getboolean(self):
        section = 'MySection'
        option = 'myoption'
        value = 'true'
        self.az_config.config_parser.add_section(section)
        self.az_config.config_parser.set(section, option, value)
        self.assertEqual(self.az_config.getboolean(section, option), True)

    def test_getboolean_error(self):
        section = 'MySection'
        option = 'myoption'
        value = 'not_a_boolean'
        self.az_config.config_parser.add_section(section)
        self.az_config.config_parser.set(section, option, value)
        with self.assertRaises(ValueError):
            self.az_config.getboolean(section, option)
Beispiel #4
0
class TestAzConfig(unittest.TestCase):

    def setUp(self):
        self.az_config = AzConfig()

    def test_has_option(self):
        section = 'MySection'
        option = 'myoption'
        value = 'myvalue'
        self.az_config.config_parser.add_section(section)
        self.az_config.config_parser.set(section, option, value)
        self.assertTrue(self.az_config.has_option(section, option))

    @mock.patch.dict(os.environ, {AzConfig.env_var_name('MySection', 'myoption'): 'myvalue'})
    def test_has_option_env(self):
        section = 'MySection'
        option = 'myoption'
        self.assertTrue(self.az_config.has_option(section, option))

    def test_has_option_env_no(self):
        section = 'MySection'
        option = 'myoption'
        self.assertFalse(self.az_config.has_option(section, option))

    def test_get(self):
        section = 'MySection'
        option = 'myoption'
        value = 'myvalue'
        self.az_config.config_parser.add_section(section)
        self.az_config.config_parser.set(section, option, value)
        self.assertEqual(self.az_config.get(section, option), value)

    @mock.patch.dict(os.environ, {AzConfig.env_var_name('MySection', 'myoption'): 'myvalue'})
    def test_get_env(self):
        section = 'MySection'
        option = 'myoption'
        value = 'myvalue'
        self.assertEqual(self.az_config.get(section, option), value)

    def test_get_not_found_section(self):
        section = 'MySection'
        option = 'myoption'
        with self.assertRaises(configparser.NoSectionError):
            self.az_config.get(section, option)

    def test_get_not_found_option(self):
        section = 'MySection'
        option = 'myoption'
        self.az_config.config_parser.add_section(section)
        with self.assertRaises(configparser.NoOptionError):
            self.az_config.get(section, option)

    def test_get_fallback(self):
        section = 'MySection'
        option = 'myoption'
        self.assertEqual(self.az_config.get(section, option, fallback='fallback'), 'fallback')

    def test_getint(self):
        section = 'MySection'
        option = 'myoption'
        value = '123'
        self.az_config.config_parser.add_section(section)
        self.az_config.config_parser.set(section, option, value)
        self.assertEqual(self.az_config.getint(section, option), int(value))

    def test_getint_error(self):
        section = 'MySection'
        option = 'myoption'
        value = 'not_an_int'
        self.az_config.config_parser.add_section(section)
        self.az_config.config_parser.set(section, option, value)
        with self.assertRaises(ValueError):
            self.az_config.getint(section, option)

    def test_getfloat(self):
        section = 'MySection'
        option = 'myoption'
        value = '123.456'
        self.az_config.config_parser.add_section(section)
        self.az_config.config_parser.set(section, option, value)
        self.assertEqual(self.az_config.getfloat(section, option), float(value))

    def test_getfloat_error(self):
        section = 'MySection'
        option = 'myoption'
        value = 'not_a_float'
        self.az_config.config_parser.add_section(section)
        self.az_config.config_parser.set(section, option, value)
        with self.assertRaises(ValueError):
            self.az_config.getfloat(section, option)

    def test_getboolean(self):
        section = 'MySection'
        option = 'myoption'
        value = 'true'
        self.az_config.config_parser.add_section(section)
        self.az_config.config_parser.set(section, option, value)
        self.assertEqual(self.az_config.getboolean(section, option), True)

    def test_getboolean_error(self):
        section = 'MySection'
        option = 'myoption'
        value = 'not_a_boolean'
        self.az_config.config_parser.add_section(section)
        self.az_config.config_parser.set(section, option, value)
        with self.assertRaises(ValueError):
            self.az_config.getboolean(section, option)