def clone_app_mock(app_home): static_files = [ ['static', 'static/static_file', 'static'], ['media', 'media/media_file', 'media'], ['', 'uwsgi_params', 'uwsgi_params'], ['static/base/site_down', 'static/base/site_down/index.html', 'index.html'] ] for static_dir, static_file_path, static_name in static_files: os.makedirs(os.path.join(app_home, 'app_name', static_dir), exist_ok=True) static_file_abs_path = os.path.join(app_home, 'app_name', static_file_path) with open(static_file_abs_path, 'w') as static_file: static_file.write('%s stuff\n' % static_name) cfu = CommandFileUtils(DIST_VERSION, LOG_FILE, LOG_LEVEL) cfu.write_to_log('successfully cloned %s to %s' % ('app_name', app_home), 'INFO')
def test_write_to_log(self): """ tests that write_to_log writes messages to log """ runlog = CommandFileUtils(self.dist_version, self.log_file, 'DEBUG') msgs = [ ['debug message', 'DEBUG'], ['info message', 'INFO'], ['warning message', 'WARNING'], ['error message', 'ERROR'], ['critical message', 'CRITICAL'] ] for m, ll in msgs: runlog.write_to_log(m, ll) self.log('%s: %s' % (ll, m))
def test_basic_functionality(self): """ tests basic logging functionality """ runlog = CommandFileUtils(self.dist_version, self.log_file, 'DEBUG') msgs = [ ['debug message', 'DEBUG'], ['info message', 'INFO'], ['warning message', 'WARNING'], ['error message', 'ERROR'], ['critical message', 'CRITICAL'] ] for m, ll in msgs: runlog.write_to_log(m, ll) for m, ll in msgs: self.log('%s: %s' % (ll, m))
def test_level_functionality(self): """ tests logging functionality when log level is higher than DEBUG """ runlog = CommandFileUtils(self.dist_version, self.log_file, 'INFO') msgs = [ ['debug message', 'DEBUG'], ['info message', 'INFO'], ['warning message', 'WARNING'], ['error message', 'ERROR'], ['critical message', 'CRITICAL'] ] for m, ll in msgs: runlog.write_to_log(m, ll) for m, ll in msgs[1:]: self.log('%s: %s' % (ll, m)) self.log('%s: %s' % (msgs[0][1], msgs[0][0]), test=False)
def test_write_to_log_only_logs_messages_of_appropriate_log_level(self): """ tests that write_to_log only writes messages with appropriate log level to log """ runlog = CommandFileUtils(self.dist_version, self.log_file, 'ERROR') msgs_log = [ ['error message', 'ERROR'], ['CRITICAL message', 'CRITICAL'] ] msgs_no_log = [ ['debug message', 'DEBUG'], ['info message', 'INFO'], ] for m, ll in msgs_log: runlog.write_to_log(m, ll) self.log('%s: %s' % (ll, m)) for m, ll in msgs_no_log: runlog.write_to_log(m, ll) self.log('%s: %s' % (ll, m), test=False)
def test_write_to_log_adds_timestamp_to_message(self): """ tests that write_to_log adds the current time to messages """ runlog = CommandFileUtils(self.dist_version, self.log_file, 'DEBUG') msgs = [ ['debug message', 'DEBUG'], ['info message', 'INFO'], ['warning message', 'WARNING'], ['error message', 'ERROR'], ['critical message', 'CRITICAL'] ] now = datetime.datetime.utcnow() for m, ll in msgs: runlog.write_to_log(m, ll) with open(self.log_file) as log: log_list = [l[:19] for l in log][-5:] for l in log_list: self.assertEqual(now.strftime('%Y-%m-%d %H:%M:%S'), l, l)
def test_write_to_log_exits_when_log_level_is_not_specified(self): """ tests that write_to_log uses default log level (DEBUG) when level is not specified and exits when log level is invalid """ runlog = CommandFileUtils(self.dist_version, self.log_file, 'DEBUG') msgs = [ ['warning message', 'WARNING'], ['other warning message', 'WARNING'], ['debug message', 'IMPORTANT'], ['info message', 'INFO'], ['default info message', False], ] for m, ll in msgs: try: runlog.write_to_log(m, ll) self.log('%s: %s' % (ll, m)) except SystemExit as error: self.assertEqual(1, error.code, '%s exited with: %s' % ('write_to_log', str(error))) self.log('ERROR: log level "%s" is not specified or not valid' % ll)
def stop_celery_mock(app_home): cfu = CommandFileUtils(DIST_VERSION, LOG_FILE, LOG_LEVEL) cfu.write_to_log('stopped celery and beat', 'INFO') if os.path.exists(os.path.join(CELERY_PID_PATH, 'w1.pid')): os.remove(os.path.join(CELERY_PID_PATH, 'w1.pid')) return 0
def own_app_mock(path, owner, group): cfu = CommandFileUtils(DIST_VERSION, LOG_FILE, LOG_LEVEL) cfu.write_to_log('changed ownership of %s to %s:%s' % (path, owner, group), 'INFO')