def test_create_records_collection_with_last_send(self): last_send = datetime.datetime(year=2018, month=6, day=5, hour=16, minute=36, second=7) collection = RecordsCollection(last_send, self.work_dir) collection.snapshot_and_read() self.assert_cache_files_count(1) self.assertEqual(453, len([r for r in collection]))
def main(): from azure.cli.telemetry.util import should_upload from azure.cli.telemetry.components.telemetry_note import TelemetryNote from azure.cli.telemetry.components.records_collection import RecordsCollection from azure.cli.telemetry.components.telemetry_client import CliTelemetryClient from azure.cli.telemetry.components.telemetry_logging import config_logging_for_upload, get_logger try: config_dir = sys.argv[1] config_logging_for_upload(config_dir) logger = get_logger('main') logger.info('Attempt start. Configuration directory [%s].', sys.argv[1]) if not should_upload(config_dir): logger.info( 'Exit early. The note file indicates it is not a suitable time to upload telemetry.' ) sys.exit(0) try: with TelemetryNote(config_dir) as telemetry_note: telemetry_note.touch() collection = RecordsCollection(telemetry_note.get_last_sent(), config_dir) collection.snapshot_and_read() client = CliTelemetryClient() for each in collection: client.add(each, flush=True) client.flush(force=True) telemetry_note.update_telemetry_note(collection.next_send) except portalocker.AlreadyLocked: # another upload process is running. logger.info( 'Lock out from note file under %s which means another process is running. Exit 0.', config_dir) sys.exit(0) except IOError as err: logger.warning('Unexpected IO Error %s. Exit 1.', err) sys.exit(1) except Exception as err: # pylint: disable=broad-except logger.error('Unexpected Error %s. Exit 2.', err) logger.exception(err) sys.exit(2) except IndexError: sys.exit(1)
def test_create_records_collection(self): collection = RecordsCollection(datetime.datetime.min, self.work_dir) # cache files are not moved while the collection is created self.assert_cache_files_count(self.TEST_CACHE_FILE_COUNT) # take snapshot and move the files collection.snapshot_and_read() # all but one file, the 'cache' file, is moved. self.assert_cache_files_count(1) # total records self.assertEqual(1750, len([r for r in collection]))
def test_create_records_collection_against_empty_folder(self): shutil.rmtree(os.path.join(self.work_dir, TELEMETRY_CACHE_DIR)) os.makedirs(os.path.join(self.work_dir, TELEMETRY_CACHE_DIR)) collection = RecordsCollection(datetime.datetime.min, self.TEST_RESOURCE_FOLDER) self.assertEqual(0, len([r for r in collection]))
def main(): from azure.cli.telemetry.util import should_upload from azure.cli.telemetry.components.telemetry_note import TelemetryNote from azure.cli.telemetry.components.records_collection import RecordsCollection from azure.cli.telemetry.components.telemetry_client import CliTelemetryClient from azure.cli.telemetry.components.telemetry_logging import config_logging_for_upload, get_logger try: config_dir = sys.argv[1] config_logging_for_upload(config_dir) logger = get_logger('main') logger.info('Attempt start. Configuration directory [%s].', sys.argv[1]) if not should_upload(config_dir): logger.info('Exit early. The note file indicates it is not a suitable time to upload telemetry.') sys.exit(0) try: with TelemetryNote(config_dir) as telemetry_note: telemetry_note.touch() collection = RecordsCollection(telemetry_note.get_last_sent(), config_dir) collection.snapshot_and_read() client = CliTelemetryClient() for each in collection: client.add(each, flush=True) client.flush(force=True) telemetry_note.update_telemetry_note(collection.next_send) except portalocker.AlreadyLocked: # another upload process is running. logger.info('Lock out from note file under %s which means another process is running. Exit 0.', config_dir) sys.exit(0) except IOError as err: logger.warning('Unexpected IO Error %s. Exit 1.', err) sys.exit(1) except Exception as err: # pylint: disable=broad-except logger.error('Unexpected Error %s. Exit 2.', err) logger.exception(err) sys.exit(2) except IndexError: sys.exit(1)
def test_create_records_collection_against_missing_folder(self): shutil.rmtree(os.path.join(self.work_dir, TELEMETRY_CACHE_DIR)) collection = RecordsCollection(datetime.datetime.min, self.work_dir) self.assertEqual(0, len([r for r in collection]))
def test_create_records_collection_against_missing_config_folder(self): collection = RecordsCollection(datetime.datetime.min, tempfile.mktemp()) self.assertEqual(0, len([r for r in collection]))