Add -l/--log-level option. Honor __cdist_loglevel env var. (#572)

Add -l/--log-level option, __cdist_loglevel -> __cdist_log_level; honor __cdist_log_level env var
This commit is contained in:
Darko Poljak 2017-09-09 21:17:29 +02:00 committed by GitHub
commit 3454da076f
23 changed files with 152 additions and 69 deletions

View file

@ -23,6 +23,7 @@
import getpass
import os
import shutil
import logging
import cdist
from cdist import core
@ -100,6 +101,7 @@ class CodeTestCase(test.CdistTestCase):
self.assertEqual(output_dict['__files'], self.local.files_path)
self.assertEqual(output_dict['__target_host_tags'],
self.local.target_host_tags)
self.assertEqual(output_dict['__cdist_log_level'], 'WARNING')
def test_run_gencode_remote_environment(self):
output_string = self.code.run_gencode_remote(self.cdist_object)
@ -125,6 +127,7 @@ class CodeTestCase(test.CdistTestCase):
self.assertEqual(output_dict['__files'], self.local.files_path)
self.assertEqual(output_dict['__target_host_tags'],
self.local.target_host_tags)
self.assertEqual(output_dict['__cdist_log_level'], 'WARNING')
def test_transfer_code_remote(self):
self.cdist_object.code_remote = self.code.run_gencode_remote(

View file

@ -10,3 +10,4 @@ echo "echo __object_id: $__object_id"
echo "echo __object_name: $__object_name"
echo "echo __files: $__files"
echo "echo __target_host_tags: $__target_host_tags"
echo "echo __cdist_log_level: $__cdist_log_level"

View file

@ -64,8 +64,8 @@ class EmulatorTestCase(test.CdistTestCase):
self.manifest = core.Manifest(self.target_host, self.local)
self.env = self.manifest.env_initial_manifest(self.script)
self.env['__cdist_object_marker'] = self.local.object_marker_name
if '__cdist_loglevel' in self.env:
del self.env['__cdist_loglevel']
if '__cdist_log_level' in self.env:
del self.env['__cdist_log_level']
def tearDown(self):
shutil.rmtree(self.temp_dir)
@ -124,11 +124,11 @@ class EmulatorTestCase(test.CdistTestCase):
emu = emulator.Emulator(argv, env=self.env)
emu_loglevel = emu.log.getEffectiveLevel()
self.assertEqual(emu_loglevel, logging.WARNING)
self.env['__cdist_loglevel'] = logging.getLevelName(logging.DEBUG)
self.env['__cdist_log_level'] = logging.getLevelName(logging.DEBUG)
emu = emulator.Emulator(argv, env=self.env)
emu_loglevel = emu.log.getEffectiveLevel()
self.assertEqual(emu_loglevel, logging.DEBUG)
del self.env['__cdist_loglevel']
del self.env['__cdist_log_level']
def test_invalid_loglevel_value(self):
argv = ['__file', '/tmp/foobar']
@ -137,11 +137,11 @@ class EmulatorTestCase(test.CdistTestCase):
emu_loglevel = emu.log.getEffectiveLevel()
self.assertEqual(emu_loglevel, logging.WARNING)
# lowercase is invalid
self.env['__cdist_loglevel'] = 'debug'
self.env['__cdist_log_level'] = 'debug'
emu = emulator.Emulator(argv, env=self.env)
emu_loglevel = emu.log.getEffectiveLevel()
self.assertEqual(emu_loglevel, logging.WARNING)
del self.env['__cdist_loglevel']
del self.env['__cdist_log_level']
def test_requirement_via_order_dependency(self):
self.env['CDIST_ORDER_DEPENDENCY'] = 'on'

View file

@ -210,6 +210,31 @@ class ExplorerClassTestCase(test.CdistTestCase):
self.assertEqual(names, output)
shutil.rmtree(out_path)
def test_explorer_environment(self):
cdist_type = core.CdistType(self.local.type_path, '__dump_env')
cdist_object = core.CdistObject(cdist_type, self.local.object_path,
self.local.object_marker_name,
'whatever')
self.explorer.transfer_type_explorers(cdist_type)
output = self.explorer.run_type_explorer('dump', cdist_object)
output_dict = {}
for line in output.split('\n'):
if line:
key, value = line.split(': ')
output_dict[key] = value
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])
self.assertEqual(output_dict['__explorer'],
self.remote.global_explorer_path)
self.assertEqual(output_dict['__target_host_tags'],
self.local.target_host_tags)
self.assertEqual(output_dict['__cdist_log_level'], 'WARNING')
if __name__ == '__main__':
import unittest

View file

@ -0,0 +1,8 @@
#!/bin/sh
echo "__target_host: $__target_host"
echo "__target_hostname: $__target_hostname"
echo "__target_fqdn: $__target_fqdn"
echo "__explorer: $__explorer"
echo "__target_host_tags: $__target_host_tags"
echo "__cdist_log_level: $__cdist_log_level"

View file

@ -73,7 +73,10 @@ class ManifestTestCase(test.CdistTestCase):
handle, output_file = self.mkstemp(dir=self.temp_dir)
os.close(handle)
os.environ['__cdist_test_out'] = output_file
self.manifest.run_initial_manifest(initial_manifest)
old_loglevel = logging.root.getEffectiveLevel()
self.log.setLevel(logging.VERBOSE)
manifest = cdist.core.manifest.Manifest(self.target_host, self.local)
manifest.run_initial_manifest(initial_manifest)
with open(output_file, 'r') as fd:
output_string = fd.read()
@ -96,6 +99,8 @@ class ManifestTestCase(test.CdistTestCase):
self.assertEqual(output_dict['__files'], self.local.files_path)
self.assertEqual(output_dict['__target_host_tags'],
self.local.target_host_tags)
self.assertEqual(output_dict['__cdist_log_level'], 'VERBOSE')
self.log.setLevel(old_loglevel)
def test_type_manifest_environment(self):
cdist_type = core.CdistType(self.local.type_path, '__dump_environment')
@ -105,7 +110,10 @@ class ManifestTestCase(test.CdistTestCase):
handle, output_file = self.mkstemp(dir=self.temp_dir)
os.close(handle)
os.environ['__cdist_test_out'] = output_file
self.manifest.run_type_manifest(cdist_object)
old_loglevel = self.log.getEffectiveLevel()
self.log.setLevel(logging.VERBOSE)
manifest = cdist.core.manifest.Manifest(self.target_host, self.local)
manifest.run_type_manifest(cdist_object)
with open(output_file, 'r') as fd:
output_string = fd.read()
@ -131,13 +139,15 @@ class ManifestTestCase(test.CdistTestCase):
self.assertEqual(output_dict['__files'], self.local.files_path)
self.assertEqual(output_dict['__target_host_tags'],
self.local.target_host_tags)
self.assertEqual(output_dict['__cdist_log_level'], 'VERBOSE')
self.log.setLevel(old_loglevel)
def test_debug_env_setup(self):
def test_loglevel_env_setup(self):
current_level = self.log.getEffectiveLevel()
self.log.setLevel(logging.DEBUG)
manifest = cdist.core.manifest.Manifest(self.target_host, self.local)
self.assertTrue("__cdist_loglevel" in manifest.env)
self.assertEqual(manifest.env["__cdist_loglevel"], 'DEBUG')
self.assertTrue("__cdist_log_level" in manifest.env)
self.assertEqual(manifest.env["__cdist_log_level"], 'DEBUG')
self.log.setLevel(current_level)

View file

@ -10,4 +10,5 @@ __cdist_type_base_path: $__cdist_type_base_path
__manifest: $__manifest
__files: $__files
__target_host_tags: $__target_host_tags
__cdist_log_level: $__cdist_log_level
DONE

View file

@ -14,4 +14,5 @@ __object_id: $__object_id
__object_name: $__object_name
__files: $__files
__target_host_tags: $__target_host_tags
__cdist_log_level: $__cdist_log_level
DONE