示例#1
0
    def set_default_config_file(self, account_name, platform=None):
        paths = ConfigFilePath(account_name, platform)
        account_config_file = paths.default_new_account_config()
        if not os.path.exists(account_config_file):
            raise AzureConfigAccountFileNotFound(
                'Account config file %s not found' % account_config_file
            )

        default_config_file = paths.default_config()
        if not default_config_file:
            default_config_file = paths.default_new_config()

        default_exists = os.path.exists(default_config_file)
        default_islink = os.path.islink(default_config_file)

        if default_exists and not default_islink:
            message = dedent('''
                Can not link %s as default account.

                A default account configuration file from a former
                azurectl version was found. Consider one of the following
                options to handle the config file: %s

                1. Delete the configuration file if no longer needed
                2. Move the configuration file with context information to
                   ~/.config/azurectl/config.<context>
            ''').strip()
            raise AzureConfigDefaultLinkError(
                message % (account_config_file, default_config_file)
            )

        if default_exists:
            os.remove(default_config_file)

        os.symlink(account_config_file, default_config_file)
示例#2
0
 def __lookup_config_file(self, platform, account_name, filename):
     paths = ConfigFilePath(account_name, platform)
     if filename:
         # lookup a custom config file
         if not os.path.isfile(filename):
             raise AzureAccountLoadFailed(
                 'Could not find config file: %s' % filename
             )
     elif account_name:
         # lookup an account config file
         filename = paths.default_new_account_config()
         if not os.path.isfile(filename):
             raise AzureAccountLoadFailed(
                 'Could not find account config file: %s %s: %s' %
                 (
                     paths.account_config_file, 'in home directory',
                     paths.home_path
                 )
             )
     else:
         # lookup default config file
         filename = paths.default_config()
         if not filename:
             raise AzureAccountLoadFailed(
                 'could not find default configuration file %s %s: %s' %
                 (
                     ' or '.join(paths.config_files),
                     'in home directory',
                     paths.home_path
                 )
             )
     return filename
示例#3
0
 def get_config_file(self, account_name=None, filename=None, platform=None):
     paths = ConfigFilePath(account_name, platform)
     if filename:
         return filename
     elif account_name:
         return paths.default_new_account_config()
     else:
         return paths.default_config()
示例#4
0
class TestConfigFilePath:
    def setup(self):
        self.paths = ConfigFilePath(account_name='bob', platform='lin')

    def test_home_path_linux(self):
        with patch.dict('os.environ', {'HOME': 'foo'}):
            paths = ConfigFilePath(platform='lin')
            assert paths.default_new_config() == \
                os.environ['HOME'] + '/.config/azurectl/config'

    def test_home_path_win(self):
        with patch.dict('os.environ', {'HOMEPATH': 'foo'}):
            paths = ConfigFilePath(platform='win')
            assert paths.default_new_config() == \
                os.environ['HOMEPATH'] + '/.config/azurectl/config'
        with patch.dict('os.environ', {'UserProfile': 'foo'}):
            paths = ConfigFilePath(platform='win')
            assert paths.default_new_config() == \
                os.environ['UserProfile'] + '/.config/azurectl/config'

    @patch('os.path.isfile')
    def test_default_config(self, mock_isfile):
        mock_isfile.return_value = True
        assert self.paths.default_config() == \
            os.environ['HOME'] + '/' + self.paths.config_files[0]

    def test_default_new_account_config(self):
        assert self.paths.default_new_account_config() == \
            os.environ['HOME'] + '/.config/azurectl/bob.config'

    @patch('glob.iglob')
    def test_account_config(self, mock_glob):
        mock_glob.return_value = ['a', 'b', 'c']
        assert self.paths.account_config() == mock_glob.return_value
        mock_glob.assert_called_once_with(os.environ['HOME'] +
                                          '/.config/azurectl/*.config')