def test_should_prepare_string_as_expected(self, blanks, underscores, special, dashes, uri, next_logfile): uri.return_value = 'replaced uri specific characters' dashes.return_value = 'stripped dashes' special.return_value = 'stripped special characters' underscores.return_value = 'trimmed underscores' blanks.return_value = 'replaced blanks with underscores' next_logfile.return_value = 'log-file-name' actual_log_file_name = create_next_log_file_name_with_command_arguments_as_tag( log_dir='log-directory', target_name='target-name', command_start_timestamp='2013-01-31--11-27-56', user_name='user-name', source_host='host-name', command_arguments=['yadtshell', 'arg1', 'arg2'] ) self.assertEqual('log-file-name', actual_log_file_name) uri.assert_called_with('arg1_arg2') dashes.assert_called_with('replaced uri specific characters') special.assert_called_with('stripped dashes') underscores.assert_called_with('stripped special characters') blanks.assert_called_with('trimmed underscores') next_logfile.assert_called_with( 'log-directory', 'target-name', '2013-01-31--11-27-56', 'user-name', 'host-name', tag='replaced blanks with underscores')
def test_should_prepare_string_as_expected(self): when(yadtshell.loggingtools)._replace_uri_specific_characters_with_underscores(any_value()).thenReturn('replaced uri specific characters') when(yadtshell.loggingtools)._strip_dashes(any_value()).thenReturn('stripped dashes') when(yadtshell.loggingtools)._strip_special_characters(any_value()).thenReturn('stripped special characters') when(yadtshell.loggingtools)._trim_underscores(any_value()).thenReturn('trimmed underscores') when(yadtshell.loggingtools)._replace_blanks_with_underscores(any_value()).thenReturn('replaced blanks with underscores') when(yadtshell.loggingtools).create_next_log_file_name(any_value(), any_value(), any_value(), any_value(), any_value(), tag=any_value()).thenReturn('log-file-name') actual_log_file_name = self.actual_file_name = create_next_log_file_name_with_command_arguments_as_tag( log_dir='log-directory', target_name='target-name', command_start_timestamp='2013-01-31--11-27-56', user_name='user-name', source_host='host-name', command_arguments=['yadtshell', 'arg1', 'arg2'] ) self.assertEqual('log-file-name', actual_log_file_name) verify(yadtshell.loggingtools)._replace_uri_specific_characters_with_underscores('arg1_arg2') verify(yadtshell.loggingtools)._strip_dashes('replaced uri specific characters') verify(yadtshell.loggingtools)._strip_special_characters('stripped dashes') verify(yadtshell.loggingtools)._trim_underscores('stripped special characters') verify(yadtshell.loggingtools)._replace_blanks_with_underscores('trimmed underscores') verify(yadtshell.loggingtools).create_next_log_file_name('log-directory', 'target-name', '2013-01-31--11-27-56', 'user-name', 'host-name', tag='replaced blanks with underscores')
def test_should_prepare_string_as_expected(self, blanks, underscores, special, dashes, uri, next_logfile): uri.return_value = 'replaced uri specific characters' dashes.return_value = 'stripped dashes' special.return_value = 'stripped special characters' underscores.return_value = 'trimmed underscores' blanks.return_value = 'replaced blanks with underscores' next_logfile.return_value = 'log-file-name' actual_log_file_name = create_next_log_file_name_with_command_arguments_as_tag( log_dir='log-directory', target_name='target-name', command_start_timestamp='2013-01-31--11-27-56', user_name='user-name', source_host='host-name', command_arguments=['yadtshell', 'arg1', 'arg2']) self.assertEqual('log-file-name', actual_log_file_name) uri.assert_called_with('arg1_arg2') dashes.assert_called_with('replaced uri specific characters') special.assert_called_with('stripped dashes') underscores.assert_called_with('stripped special characters') blanks.assert_called_with('trimmed underscores') next_logfile.assert_called_with('log-directory', 'target-name', '2013-01-31--11-27-56', 'user-name', 'host-name', tag='replaced blanks with underscores')
def test_should_use_command_argument_as_seventh_element(self): actual_file_name = create_next_log_file_name_with_command_arguments_as_tag( log_dir='log-directory', target_name='target-name', command_start_timestamp='2013-01-31--11-27-56', user_name='user-name', source_host='host-name', command_arguments=['yadtshell', 'status']) self._assert(actual_file_name)._element_at(6)._is_equal_to('status')
def test_should_use_command_argument_as_seventh_element(self): actual_file_name = create_next_log_file_name_with_command_arguments_as_tag( log_dir='log-directory', target_name='target-name', command_start_timestamp='2013-01-31--11-27-56', user_name='user-name', source_host='host-name', command_arguments=['yadtshell', 'status'] ) self._assert(actual_file_name)._element_at(6)._is_equal_to('status')
def test_should_call_create_next_log_file_name_using_given_arguments(self): when(yadtshell.loggingtools).create_next_log_file_name(any_value(), any_value(), any_value(), any_value(), any_value(), tag=any_value()).thenReturn('log-file-name') actual_log_file_name = self.actual_file_name = create_next_log_file_name_with_command_arguments_as_tag( log_dir='log-directory', target_name='target-name', command_start_timestamp='2013-01-31--11-27-56', user_name='user-name', source_host='host-name', command_arguments=['yadtshell', 'status'] ) self.assertEqual('log-file-name', actual_log_file_name) verify(yadtshell.loggingtools).create_next_log_file_name('log-directory', 'target-name', '2013-01-31--11-27-56', 'user-name', 'host-name', tag='status')
def test_should_join_command_and_arguments_using_underscore_if_command_is_not_yadtshell(self): when(yadtshell.loggingtools).create_next_log_file_name(any_value(), any_value(), any_value(), any_value(), any_value(), tag=any_value()).thenReturn('log-file-name') actual_log_file_name = self.actual_file_name = create_next_log_file_name_with_command_arguments_as_tag( log_dir='log-directory', target_name='target-name', command_start_timestamp='2013-01-31--11-27-56', user_name='user-name', source_host='host-name', command_arguments=['foobar', 'abc', 'def', 'ghi', 'jkl'] ) self.assertEqual('log-file-name', actual_log_file_name) verify(yadtshell.loggingtools).create_next_log_file_name('log-directory', 'target-name', '2013-01-31--11-27-56', 'user-name', 'host-name', tag='foobar_abc_def_ghi_jkl')
def test_should_join_command_and_arguments_using_underscore_if_command_is_not_yadtshell(self, next_logfile): next_logfile.return_value = 'log-file-name' actual_log_file_name = create_next_log_file_name_with_command_arguments_as_tag( log_dir='log-directory', target_name='target-name', command_start_timestamp='2013-01-31--11-27-56', user_name='user-name', source_host='host-name', command_arguments=['foobar', 'abc', 'def', 'ghi', 'jkl'] ) self.assertEqual('log-file-name', actual_log_file_name) next_logfile.assert_called_with( 'log-directory', 'target-name', '2013-01-31--11-27-56', 'user-name', 'host-name', tag='foobar_abc_def_ghi_jkl')
def test_should_call_create_next_log_file_name_using_given_arguments(self, next_logfile): next_logfile.return_value = 'log-file-name' actual_log_file_name = create_next_log_file_name_with_command_arguments_as_tag( log_dir='log-directory', target_name='target-name', command_start_timestamp='2013-01-31--11-27-56', user_name='user-name', source_host='host-name', command_arguments=['yadtshell', 'status'] ) self.assertEqual('log-file-name', actual_log_file_name) next_logfile.assert_called_with( 'log-directory', 'target-name', '2013-01-31--11-27-56', 'user-name', 'host-name', tag='status')
def test_should_call_create_next_log_file_name_using_given_arguments( self, next_logfile): next_logfile.return_value = 'log-file-name' actual_log_file_name = create_next_log_file_name_with_command_arguments_as_tag( log_dir='log-directory', target_name='target-name', command_start_timestamp='2013-01-31--11-27-56', user_name='user-name', source_host='host-name', command_arguments=['yadtshell', 'status']) self.assertEqual('log-file-name', actual_log_file_name) next_logfile.assert_called_with('log-directory', 'target-name', '2013-01-31--11-27-56', 'user-name', 'host-name', tag='status')
def test_should_join_command_and_arguments_using_underscore_if_command_is_not_yadtshell( self, next_logfile): next_logfile.return_value = 'log-file-name' actual_log_file_name = create_next_log_file_name_with_command_arguments_as_tag( log_dir='log-directory', target_name='target-name', command_start_timestamp='2013-01-31--11-27-56', user_name='user-name', source_host='host-name', command_arguments=['foobar', 'abc', 'def', 'ghi', 'jkl']) self.assertEqual('log-file-name', actual_log_file_name) next_logfile.assert_called_with('log-directory', 'target-name', '2013-01-31--11-27-56', 'user-name', 'host-name', tag='foobar_abc_def_ghi_jkl')
def load_settings_and_create_dirs(log_to_file=True): TARGET_BASENAME = 'target' global TARGET_SETTINGS TARGET_SETTINGS = load_target_file(TARGET_BASENAME) initialize_broadcast_client() os.umask(2) try: os.makedirs(OUT_DIR) except OSError as e: if e.errno != 17: # 17: file exists root_logger.critical('Cannot create cache directory %s on %s' % (OUT_DIR, USER_INFO['yadt_host'])) root_logger.exception(e) sys.exit(1) pass global TODAY TODAY = time.strftime('%Y-%m-%d') TIME_FORMAT = '%Y-%m-%d--%H-%M-%S' global STARTED_ON STARTED_ON = time.strftime(TIME_FORMAT) global term term = yadtshell.TerminalController.TerminalController() global ybc if broadcasterconf_imported: ybc = broadcasterconf.create(TARGET_SETTINGS['name']) else: ybc = DummyBroadcaster() LOG_DIR = os.path.join(LOG_DIR_PREFIX, TODAY, TARGET_SETTINGS['name']) try: os.makedirs(LOG_DIR) except OSError as e: if e.errno != 17: # 17: file exists root_logger.critical('cannot write to log dir %s' % LOG_DIR) root_logger.exception(e) sys.exit(1) global log_file log_file = create_next_log_file_name_with_command_arguments_as_tag( log_dir=LOG_DIR, target_name=TARGET_SETTINGS['name'], command_start_timestamp=STARTED_ON, user_name=USER_INFO['user'], source_host=USER_INFO['yadt_host'].split('.')[0], command_arguments=sys.argv ) formatter = logging.Formatter( '%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s', '%Y%m%d-%H%M%S') if log_to_file: file_handler = logging.FileHandler(log_file) file_handler.setLevel(logging.DEBUG) file_handler.setFormatter(formatter) root_logger.addHandler(file_handler) logger.debug(yaml.dump(USER_INFO, default_flow_style=False)) logger.debug('Called "{0}"'.format(' '.join(sys.argv))) logger.debug('output dir is %s' % OUTPUT_DIR) OUT_TARGET_FILE = os.path.join(OUT_DIR, TARGET_BASENAME) try: changed = not filecmp.cmp(TARGET_BASENAME, OUT_TARGET_FILE) if changed: logger.info( 'target settings have changed since last call, thus cleaning cached data') except OSError: changed = True if changed: shutil.rmtree(OUT_DIR) os.makedirs(OUT_DIR) shutil.copy2(TARGET_BASENAME, OUT_TARGET_FILE) global VIEW_SETTINGS VIEW_SETTINGS_FILE = 'view' try: view_file = open(VIEW_SETTINGS_FILE) VIEW_SETTINGS = yaml.load(view_file) view_file.close() except Exception: logger.debug( '"view" file not found, falling back to default values: %s' % VIEW_SETTINGS) hosts_condensed_filename = os.path.join(OUT_DIR, 'hosts_condensed') with open(hosts_condensed_filename, 'w') as hosts_condensed_file: condensed = condense_hosts2(condense_hosts(TARGET_SETTINGS['hosts'])) hosts_condensed_file.write(' '.join(condensed)) def list_selected_hosts(): return 'You are working now on %s\n\nas full list: %s\n' % ( term.render('${BOLD}') + ', '.join( condense_hosts2( condense_hosts( TARGET_SETTINGS['hosts']))) + term.render('${NORMAL}'), ', '.join(TARGET_SETTINGS['hosts']), ) identity = TARGET_SETTINGS.get('identity') login = TARGET_SETTINGS.get('login') credentials = '' if identity: credentials += ' -i %(identity)s' % locals() if login: credentials += ' -l %(login)s' % locals() CONNECTIONS_DIR = os.path.join(OUTPUT_DIR, 'connections') global SSH_CONTROL_PATH SSH_CONTROL_PATH = os.path.join(CONNECTIONS_DIR, '%h') try: os.makedirs(CONNECTIONS_DIR) except OSError: pass global SSH SSH = 'ssh -o ControlPath=%s -A %s -T -o ConnectTimeout=4 -o BatchMode=yes -o CheckHostIP=no -o StrictHostKeyChecking=no -q' % ( SSH_CONTROL_PATH, credentials) global tracking_id tracking_id = None
ybc = DummyBroadcaster() LOG_DIR = os.path.join(LOG_DIR_PREFIX, TODAY) try: os.makedirs(LOG_DIR) except OSError, e: if e.errno != 17: # 17: file exists root_logger.critical('cannot write to log dir %s' % LOG_DIR) root_logger.exception(e) sys.exit(1) global log_file log_file = create_next_log_file_name_with_command_arguments_as_tag( log_dir=LOG_DIR, target_name=TARGET_SETTINGS['name'], command_start_timestamp=STARTED_ON, user_name=USER_INFO['user'], source_host=USER_INFO['yadt_host'].split('.')[0], command_arguments=sys.argv ) formatter = logging.Formatter('%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s', '%Y%m%d-%H%M%S') if log_to_file: file_handler = logging.FileHandler(log_file) file_handler.setLevel(logging.DEBUG) file_handler.setFormatter(formatter) root_logger.addHandler(file_handler) logger.debug(yaml.dump(USER_INFO, default_flow_style=False)) logger.debug('Called "{0}"'.format(' '.join(sys.argv))) logger.debug('output dir is %s' % OUTPUT_DIR)