2011-10-12 15:20:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 2010-2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
2012-11-06 16:01:20 +00:00
|
|
|
# 2012 Nico Schottelius (nico-cdist at schottelius.org)
|
2011-10-12 15:20:47 +00:00
|
|
|
#
|
|
|
|
# This file is part of cdist.
|
|
|
|
#
|
|
|
|
# cdist is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# cdist is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
|
|
|
import getpass
|
|
|
|
import shutil
|
|
|
|
import string
|
|
|
|
import random
|
2011-10-14 21:18:58 +00:00
|
|
|
import logging
|
2011-10-18 12:23:06 +00:00
|
|
|
import io
|
|
|
|
import sys
|
2011-10-12 15:20:47 +00:00
|
|
|
|
|
|
|
import cdist
|
2011-10-19 15:59:21 +00:00
|
|
|
from cdist import test
|
2011-10-12 15:20:47 +00:00
|
|
|
from cdist.exec import local
|
|
|
|
from cdist import core
|
|
|
|
from cdist.core import manifest
|
|
|
|
|
|
|
|
import os.path as op
|
|
|
|
my_dir = op.abspath(op.dirname(__file__))
|
|
|
|
fixtures = op.join(my_dir, 'fixtures')
|
2012-11-06 16:01:20 +00:00
|
|
|
conf_dir = op.join(fixtures, 'conf')
|
2011-10-12 15:20:47 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2011-10-19 15:59:21 +00:00
|
|
|
class ManifestTestCase(test.CdistTestCase):
|
2011-10-12 15:20:47 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2011-10-18 12:23:06 +00:00
|
|
|
self.orig_environ = os.environ
|
|
|
|
os.environ = os.environ.copy()
|
2011-10-12 15:20:47 +00:00
|
|
|
self.temp_dir = self.mkdtemp()
|
2013-08-19 10:03:25 +00:00
|
|
|
|
2011-10-12 15:20:47 +00:00
|
|
|
out_path = self.temp_dir
|
2016-08-10 21:56:56 +00:00
|
|
|
hostdir = cdist.str_hash(self.target_host[0])
|
2016-07-23 14:13:59 +00:00
|
|
|
base_root_path = os.path.join(out_path, hostdir)
|
2012-11-06 16:01:20 +00:00
|
|
|
self.local = local.Local(
|
|
|
|
target_host=self.target_host,
|
2017-07-20 20:04:44 +00:00
|
|
|
target_host_tags=self.target_host_tags,
|
2016-07-23 14:13:59 +00:00
|
|
|
base_root_path=base_root_path,
|
|
|
|
host_dir_name=hostdir,
|
2016-07-05 18:44:24 +00:00
|
|
|
exec_path=cdist.test.cdist_exec_path,
|
2012-11-06 16:01:20 +00:00
|
|
|
add_conf_dirs=[conf_dir])
|
2012-11-01 13:42:55 +00:00
|
|
|
self.local.create_files_dirs()
|
2012-11-06 16:01:20 +00:00
|
|
|
|
2011-10-14 21:18:58 +00:00
|
|
|
self.manifest = manifest.Manifest(self.target_host, self.local)
|
2016-08-10 21:56:56 +00:00
|
|
|
self.log = logging.getLogger(self.target_host[0])
|
2011-10-12 15:20:47 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2011-10-18 12:23:06 +00:00
|
|
|
os.environ = self.orig_environ
|
2011-10-12 15:58:09 +00:00
|
|
|
shutil.rmtree(self.temp_dir)
|
2011-10-12 15:20:47 +00:00
|
|
|
|
|
|
|
def test_initial_manifest_environment(self):
|
2016-07-05 18:44:24 +00:00
|
|
|
initial_manifest = os.path.join(self.local.manifest_path,
|
|
|
|
"dump_environment")
|
2011-10-18 12:23:06 +00:00
|
|
|
handle, output_file = self.mkstemp(dir=self.temp_dir)
|
2011-10-21 13:26:27 +00:00
|
|
|
os.close(handle)
|
2011-10-18 12:23:06 +00:00
|
|
|
os.environ['__cdist_test_out'] = output_file
|
2011-10-14 14:14:04 +00:00
|
|
|
self.manifest.run_initial_manifest(initial_manifest)
|
2011-10-12 15:20:47 +00:00
|
|
|
|
2011-10-18 12:23:06 +00:00
|
|
|
with open(output_file, 'r') as fd:
|
|
|
|
output_string = fd.read()
|
|
|
|
output_dict = {}
|
|
|
|
for line in output_string.split('\n'):
|
|
|
|
if line:
|
2016-07-05 18:44:24 +00:00
|
|
|
key, value = line.split(': ')
|
2011-10-18 12:23:06 +00:00
|
|
|
output_dict[key] = value
|
|
|
|
self.assertTrue(output_dict['PATH'].startswith(self.local.bin_path))
|
2016-08-10 21:56:56 +00:00
|
|
|
self.assertEqual(output_dict['__target_host'],
|
|
|
|
self.local.target_host[0])
|
|
|
|
self.assertEqual(output_dict['__target_hostname'],
|
|
|
|
self.local.target_host[1])
|
|
|
|
self.assertEqual(output_dict['__target_fqdn'],
|
|
|
|
self.local.target_host[2])
|
2013-08-19 10:03:25 +00:00
|
|
|
self.assertEqual(output_dict['__global'], self.local.base_path)
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertEqual(output_dict['__cdist_type_base_path'],
|
|
|
|
self.local.type_path)
|
2011-10-18 12:23:06 +00:00
|
|
|
self.assertEqual(output_dict['__manifest'], self.local.manifest_path)
|
2016-06-11 10:02:13 +00:00
|
|
|
self.assertEqual(output_dict['__files'], self.local.files_path)
|
2017-07-20 20:04:44 +00:00
|
|
|
self.assertEqual(output_dict['__target_host_tags'],
|
|
|
|
self.local.target_host_tags)
|
2011-10-18 12:23:06 +00:00
|
|
|
|
2011-10-12 15:56:45 +00:00
|
|
|
def test_type_manifest_environment(self):
|
2012-02-12 00:18:14 +00:00
|
|
|
cdist_type = core.CdistType(self.local.type_path, '__dump_environment')
|
2016-07-05 18:44:24 +00:00
|
|
|
cdist_object = core.CdistObject(cdist_type, self.local.object_path,
|
|
|
|
self.local.object_marker_name,
|
|
|
|
'whatever')
|
2011-10-18 12:23:06 +00:00
|
|
|
handle, output_file = self.mkstemp(dir=self.temp_dir)
|
2011-10-21 13:26:27 +00:00
|
|
|
os.close(handle)
|
2011-10-18 12:23:06 +00:00
|
|
|
os.environ['__cdist_test_out'] = output_file
|
2011-10-14 14:14:04 +00:00
|
|
|
self.manifest.run_type_manifest(cdist_object)
|
2011-10-14 21:18:58 +00:00
|
|
|
|
2011-10-18 12:23:06 +00:00
|
|
|
with open(output_file, 'r') as fd:
|
|
|
|
output_string = fd.read()
|
|
|
|
output_dict = {}
|
|
|
|
for line in output_string.split('\n'):
|
|
|
|
if line:
|
2016-07-05 18:44:24 +00:00
|
|
|
key, value = line.split(': ')
|
2011-10-18 12:23:06 +00:00
|
|
|
output_dict[key] = value
|
|
|
|
self.assertTrue(output_dict['PATH'].startswith(self.local.bin_path))
|
2016-08-10 21:56:56 +00:00
|
|
|
self.assertEqual(output_dict['__target_host'],
|
|
|
|
self.local.target_host[0])
|
|
|
|
self.assertEqual(output_dict['__target_hostname'],
|
|
|
|
self.local.target_host[1])
|
|
|
|
self.assertEqual(output_dict['__target_fqdn'],
|
|
|
|
self.local.target_host[2])
|
2013-08-19 10:03:25 +00:00
|
|
|
self.assertEqual(output_dict['__global'], self.local.base_path)
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertEqual(output_dict['__cdist_type_base_path'],
|
|
|
|
self.local.type_path)
|
2011-10-18 12:23:06 +00:00
|
|
|
self.assertEqual(output_dict['__type'], cdist_type.absolute_path)
|
|
|
|
self.assertEqual(output_dict['__object'], cdist_object.absolute_path)
|
|
|
|
self.assertEqual(output_dict['__object_id'], cdist_object.object_id)
|
2011-10-19 13:15:05 +00:00
|
|
|
self.assertEqual(output_dict['__object_name'], cdist_object.name)
|
2016-06-11 10:02:13 +00:00
|
|
|
self.assertEqual(output_dict['__files'], self.local.files_path)
|
2017-07-20 20:04:44 +00:00
|
|
|
self.assertEqual(output_dict['__target_host_tags'],
|
|
|
|
self.local.target_host_tags)
|
2011-10-18 12:23:06 +00:00
|
|
|
|
2011-10-14 21:18:58 +00:00
|
|
|
def test_debug_env_setup(self):
|
2012-11-07 10:47:26 +00:00
|
|
|
current_level = self.log.getEffectiveLevel()
|
2011-10-14 21:18:58 +00:00
|
|
|
self.log.setLevel(logging.DEBUG)
|
|
|
|
manifest = cdist.core.manifest.Manifest(self.target_host, self.local)
|
2017-08-12 19:40:23 +00:00
|
|
|
self.assertTrue("__cdist_loglevel" in manifest.env)
|
2012-11-07 10:47:26 +00:00
|
|
|
self.log.setLevel(current_level)
|
2016-06-11 10:02:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import unittest
|
|
|
|
unittest.main()
|