# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
-from heat.openstack.common import log as logging
+
+import os
+import os.path
+
+from heat.common import environment_format
+from heat.openstack.common import log
from heat.openstack.common.gettextutils import _
from heat.engine import environment
-logger = logging.getLogger(__name__)
+LOG = log.getLogger(__name__)
def _register_resources(type_pairs):
try:
return module.resource_mapping().iteritems()
except Exception as ex:
- logger.error(_('Failed to load resources from %s') % str(module))
+ LOG.error(_('Failed to load resources from %s') % str(module))
else:
return []
return _environment
+def _list_environment_files(env_dir):
+ try:
+ return os.listdir(env_dir)
+ except OSError as osex:
+ LOG.error('Failed to read %s' % (env_dir))
+ LOG.exception(osex)
+ return []
+
+
+def _load_global_environment(env_dir):
+ for env_name in _list_environment_files(env_dir):
+ try:
+ file_path = os.path.join(env_dir, env_name)
+ with open(file_path) as env_fd:
+ LOG.info('Loading %s' % file_path)
+ env_body = environment_format.parse(env_fd.read())
+ environment_format.default_for_missing(env_body)
+ _environment.load(env_body)
+ except ValueError as vex:
+ LOG.error('Failed to parse %s/%s' % (env_dir, env_name))
+ LOG.exception(vex)
+ except IOError as ioex:
+ LOG.error('Failed to read %s/%s' % (env_dir, env_name))
+ LOG.exception(ioex)
+
+
def initialise():
global _environment
if _environment is not None:
return
import sys
+ from oslo.config import cfg
from heat.common import plugin_loader
_environment = environment.Environment({}, user_env=False)
+ cfg.CONF.import_opt('environment_dir', 'heat.common.config')
+ _load_global_environment(cfg.CONF.environment_dir)
_register_modules(plugin_loader.load_modules(sys.modules[__name__]))
- from oslo.config import cfg
-
cfg.CONF.import_opt('plugin_dirs', 'heat.common.config')
plugin_pkg = plugin_loader.create_subpackage(cfg.CONF.plugin_dirs,
# License for the specific language governing permissions and limitations
# under the License.
+import mock
+
from heat.engine import environment
from heat.engine import resources
self.assertEqual('ip.yaml',
env.get_resource_info('OS::Networking::FloatingIP',
'my_fip').value)
+
+
+class GlobalEnvLoadingTest(common.HeatTestCase):
+
+ def test_happy_path(self):
+ list_dir = 'heat.engine.resources._list_environment_files'
+ with mock.patch(list_dir) as m_ldir:
+ m_ldir.return_value = ['a.yaml']
+ env_dir = '/etc_etc/heat/enviroment.d'
+ env_content = '{"resource_registry": {}}'
+
+ with mock.patch('heat.engine.resources.open',
+ mock.mock_open(read_data=env_content),
+ create=True) as m_open:
+ resources._load_global_environment(env_dir)
+
+ m_ldir.assert_called_once_with(env_dir)
+ m_open.assert_called_once_with('%s/a.yaml' % env_dir)
+
+ def test_empty_env_dir(self):
+ list_dir = 'heat.engine.resources._list_environment_files'
+ with mock.patch(list_dir) as m_ldir:
+ m_ldir.return_value = []
+ env_dir = '/etc_etc/heat/enviroment.d'
+ resources._load_global_environment(env_dir)
+
+ m_ldir.assert_called_once_with(env_dir)
+
+ def test_continue_on_ioerror(self):
+ """assert we get all files processed even if there are
+ processing exceptions.
+ """
+ list_dir = 'heat.engine.resources._list_environment_files'
+ with mock.patch(list_dir) as m_ldir:
+ m_ldir.return_value = ['a.yaml', 'b.yaml']
+ env_dir = '/etc_etc/heat/enviroment.d'
+ env_content = '{}'
+
+ with mock.patch('heat.engine.resources.open',
+ mock.mock_open(read_data=env_content),
+ create=True) as m_open:
+ m_open.side_effect = IOError
+ resources._load_global_environment(env_dir)
+
+ m_ldir.assert_called_once_with(env_dir)
+ expected = [mock.call('%s/a.yaml' % env_dir),
+ mock.call('%s/b.yaml' % env_dir)]
+ self.assertEqual(expected, m_open.call_args_list)
+
+ def test_continue_on_parse_error(self):
+ """assert we get all files processed even if there are
+ processing exceptions.
+ """
+ list_dir = 'heat.engine.resources._list_environment_files'
+ with mock.patch(list_dir) as m_ldir:
+ m_ldir.return_value = ['a.yaml', 'b.yaml']
+ env_dir = '/etc_etc/heat/enviroment.d'
+ env_content = '{@$%#$%'
+
+ with mock.patch('heat.engine.resources.open',
+ mock.mock_open(read_data=env_content),
+ create=True) as m_open:
+ resources._load_global_environment(env_dir)
+
+ m_ldir.assert_called_once_with(env_dir)
+ expected = [mock.call('%s/a.yaml' % env_dir),
+ mock.call('%s/b.yaml' % env_dir)]
+ self.assertEqual(expected, m_open.call_args_list)