class TestCommandWithConfiguredDefaults(unittest.TestCase):

    def __init__(self, *args, **kwargs):
        self.argv = None
        self.application = None
        super(TestCommandWithConfiguredDefaults, self).__init__(*args, **kwargs)

    @classmethod
    def setUpClass(cls):
        # Ensure initialization has occurred correctly
        import azure.cli.main
        logging.basicConfig(level=logging.DEBUG)

    @classmethod
    def tearDownClass(cls):
        logging.shutdown()

    @staticmethod
    def sample_vm_list(resource_group_name):
        return resource_group_name

    def set_up_command_table(self, required_arg=False):
        command_table.clear()

        module_name = __name__ + '.' + self._testMethodName
        cli_command(module_name, 'test sample-vm-list',
                    '{}#TestCommandWithConfiguredDefaults.sample_vm_list'.format(__name__))

        register_cli_argument('test sample-vm-list', 'resource_group_name',
                              CliArgumentType(options_list=('--resource-group-name', '-g'),
                                              configured_default='group', required=required_arg))

        command_table['test sample-vm-list'].load_arguments()
        _update_command_definitions(command_table)

        self.argv = 'az test sample-vm-list'.split()
        config = Configuration(self.argv)
        config.get_command_table = lambda: command_table
        self.application = Application(config)

    @mock.patch.dict(os.environ, {AzConfig.env_var_name('defaults', 'group'): 'myRG'})
    def test_apply_configured_defaults_on_required_arg(self):
        self.set_up_command_table(required_arg=True)

        # action
        res = self.application.execute(self.argv[1:])

        # assert
        self.assertEqual(res.result, 'myRG')

    @mock.patch.dict(os.environ, {AzConfig.env_var_name('defaults', 'group'): 'myRG'})
    def test_apply_configured_defaults_on_optional_arg(self):
        self.set_up_command_table(required_arg=False)

        # action
        res = self.application.execute(self.argv[1:])

        # assert
        self.assertEqual(res.result, 'myRG')
Example #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")
Example #3
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")
Example #4
0
 def setUp(self):
     self.az_config = AzConfig()
Example #5
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)
Example #6
0
 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)
Example #7
0
 def setUp(self):
     self.az_config = AzConfig()
Example #8
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)
Example #9
0
 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)