2013-06-29 12:54:09 +00:00
|
|
|
# -*- coding: utf-8 -*-\
|
|
|
|
'''
|
2013-11-28 11:02:23 +00:00
|
|
|
unit.cloud_config_test
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~
|
2013-06-29 12:54:09 +00:00
|
|
|
|
|
|
|
Configuration related unit testing
|
|
|
|
|
|
|
|
:codeauthor: :email:`Pedro Algarvio (pedro@algarvio.me)`
|
|
|
|
:copyright: © 2013 by the SaltStack Team, see AUTHORS for more details.
|
|
|
|
:license: Apache 2.0, see LICENSE for more details.
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import python libs
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
|
2013-11-28 11:18:09 +00:00
|
|
|
# Import 3rd-party libs
|
|
|
|
import yaml
|
|
|
|
|
2013-06-29 12:54:09 +00:00
|
|
|
# Import salt testing libs
|
|
|
|
from salttesting import TestCase
|
|
|
|
from salttesting.helpers import ensure_in_syspath
|
|
|
|
ensure_in_syspath('../')
|
|
|
|
|
2013-11-28 11:02:23 +00:00
|
|
|
# Import salt libs
|
|
|
|
import salt.utils
|
2013-11-28 11:18:09 +00:00
|
|
|
import integration
|
2013-11-28 11:02:23 +00:00
|
|
|
from salt import config as cloudconfig
|
2013-06-29 12:54:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CloudConfigTestCase(TestCase):
|
|
|
|
|
|
|
|
def test_load_cloud_config_from_environ_var(self):
|
|
|
|
original_environ = os.environ.copy()
|
|
|
|
|
|
|
|
tempdir = tempfile.mkdtemp()
|
|
|
|
try:
|
|
|
|
env_root_dir = os.path.join(tempdir, 'foo', 'env')
|
|
|
|
os.makedirs(env_root_dir)
|
|
|
|
env_fpath = os.path.join(env_root_dir, 'config-env')
|
|
|
|
|
|
|
|
salt.utils.fopen(env_fpath, 'w').write(
|
|
|
|
'root_dir: {0}\n'
|
|
|
|
'log_file: {1}\n'.format(env_root_dir, env_fpath)
|
|
|
|
)
|
|
|
|
|
|
|
|
os.environ['SALT_CLOUD_CONFIG'] = env_fpath
|
|
|
|
# Should load from env variable, not the default configuration file
|
|
|
|
config = cloudconfig.cloud_config('/etc/salt/cloud')
|
|
|
|
self.assertEqual(config['log_file'], env_fpath)
|
|
|
|
os.environ.clear()
|
|
|
|
os.environ.update(original_environ)
|
|
|
|
|
|
|
|
root_dir = os.path.join(tempdir, 'foo', 'bar')
|
|
|
|
os.makedirs(root_dir)
|
|
|
|
fpath = os.path.join(root_dir, 'config')
|
|
|
|
salt.utils.fopen(fpath, 'w').write(
|
|
|
|
'root_dir: {0}\n'
|
|
|
|
'log_file: {1}\n'.format(root_dir, fpath)
|
|
|
|
)
|
|
|
|
# Let's set the environment variable, yet, since the configuration
|
|
|
|
# file path is not the default one, ie, the user has passed an
|
|
|
|
# alternative configuration file form the CLI parser, the
|
|
|
|
# environment variable will be ignored.
|
|
|
|
os.environ['SALT_CLOUD_CONFIG'] = env_fpath
|
|
|
|
config = cloudconfig.cloud_config(fpath)
|
|
|
|
self.assertEqual(config['log_file'], fpath)
|
2013-11-28 11:02:23 +00:00
|
|
|
finally:
|
|
|
|
# Reset the environ
|
2013-06-29 12:54:09 +00:00
|
|
|
os.environ.clear()
|
|
|
|
os.environ.update(original_environ)
|
|
|
|
|
|
|
|
if os.path.isdir(tempdir):
|
|
|
|
shutil.rmtree(tempdir)
|
|
|
|
|
2013-11-28 11:18:09 +00:00
|
|
|
def test_deploy_search_path_as_string(self):
|
|
|
|
temp_conf_dir = os.path.join(integration.TMP, 'issue-8863')
|
|
|
|
config_file_path = os.path.join(temp_conf_dir, 'cloud')
|
|
|
|
deploy_dir_path = os.path.join(temp_conf_dir, 'test-deploy.d')
|
|
|
|
try:
|
|
|
|
for directory in (temp_conf_dir, deploy_dir_path):
|
|
|
|
if not os.path.isdir(directory):
|
|
|
|
os.makedirs(directory)
|
|
|
|
|
|
|
|
default_config = cloudconfig.cloud_config(config_file_path)
|
|
|
|
default_config['deploy_scripts_search_path'] = deploy_dir_path
|
|
|
|
with salt.utils.fopen(config_file_path, 'w') as cfd:
|
|
|
|
cfd.write(yaml.dump(default_config))
|
|
|
|
|
|
|
|
default_config = cloudconfig.cloud_config(config_file_path)
|
|
|
|
|
|
|
|
# Our custom deploy scripts path was correctly added to the list
|
|
|
|
self.assertIn(
|
|
|
|
deploy_dir_path,
|
|
|
|
default_config['deploy_scripts_search_path']
|
|
|
|
)
|
|
|
|
|
|
|
|
# And it's even the first occurrence as it should
|
|
|
|
self.assertEqual(
|
|
|
|
deploy_dir_path,
|
|
|
|
default_config['deploy_scripts_search_path'][0]
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(temp_conf_dir):
|
|
|
|
shutil.rmtree(temp_conf_dir)
|
|
|
|
|
2013-12-03 19:08:30 +00:00
|
|
|
def test_includes_load(self):
|
|
|
|
'''cloud.{providers,profiles}.d directories are loaded even if not directly passed'''
|
|
|
|
config_path = os.path.join(integration.FILES, 'conf', 'cloud')
|
|
|
|
config = cloudconfig.cloud_config(config_path)
|
|
|
|
self.assertIn('ec2-config', config['providers'])
|
|
|
|
self.assertIn('Ubuntu-13.04-AMD64', config['profiles'])
|
|
|
|
|
2013-06-29 12:54:09 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from salttesting.parser import run_testcase
|
|
|
|
run_testcase(CloudConfigTestCase)
|