def __init__(self, methodName='runTest'): super(TestCollector, self).__init__(methodName) # source db settings.ICS_DB_CONN_STR = {'host': str('localhost'), 'database': str('/tmp/ics_main.gdb'), 'user': str('SYSDBA'), 'password': str('masterkey')} source_location = os.path.abspath('tests/ics_main.gdb') self._source_db_manager = FirebirdDbManager(source_location, settings.ICS_DB_CONN_STR['database'], settings.ICS_DB_CONN_STR['user'], settings.ICS_DB_CONN_STR['password'], settings.ICS_DB_CONN_STR['host']) # dest db self._dest_db_manager = sqlite_db_manager.SqliteDbManager('../stat_sender_db/create.py') settings.DEST_DB_CONN_STR = self._dest_db_manager.connection_string settings.LOG_CONF['handlers'] = {'console': {'level': 'INFO', 'class': 'logging.StreamHandler', 'formatter': 'default'}} settings.LOG_CONF['loggers'] = {'stat_ics_conf_collector.entry_point': {'handlers': ['console'], 'level': 'INFO', 'propagate': True}}
def __init__(self): self._stat_sender_db_manager = SqliteDbManager(os.path.abspath('../stat_sender_db/create.py'), os.path.join(DATA_DEST_DIR, 'usage_stat_db/')) self._server_db_manager = PgDbManager('postgres', '31415926') self._ics_db_manager = FirebirdDbManager(os.path.abspath('../stat_ics_db_collector/tests/ics_main.gdb'), '/tmp/usagestat_test/data/ics_main.gdb', 'SYSDBA', 'masterkey')
class TestManager(object): def __init__(self): self._stat_sender_db_manager = SqliteDbManager(os.path.abspath('../stat_sender_db/create.py'), os.path.join(DATA_DEST_DIR, 'usage_stat_db/')) self._server_db_manager = PgDbManager('postgres', '31415926') self._ics_db_manager = FirebirdDbManager(os.path.abspath('../stat_ics_db_collector/tests/ics_main.gdb'), '/tmp/usagestat_test/data/ics_main.gdb', 'SYSDBA', 'masterkey') def __enter__(self): self._create_prerequisites() self._stat_sender_db_manager.__enter__() self._server_db_manager.__enter__() self._ics_db_manager.__enter__() return self def __exit__(self, exc_type, exc_val, exc_tb): self._ics_db_manager.__exit__(exc_type, exc_val, exc_tb) self._server_db_manager.__exit__(exc_type, exc_val, exc_tb) self._stat_sender_db_manager.__exit__(exc_type, exc_val, exc_tb) self._rm_prerequisites() return True @property def stat_sender_db(self): return self._stat_sender_db_manager @property def server_db(self): return self._server_db_manager def _create_prerequisites(self): self._rm_prerequisites() create_dir_if_not_exist(TEST_BASE_DIR) os.mkdir(CODE_DEST_DIR) os.mkdir(DATA_DEST_DIR) create_dir_if_not_exist(PERSIST_DATA_DEST_DIR) self._copy_data() self._copy_code() self._prepare_code() def _rm_prerequisites(self): self._rm_data() self._rm_code() def _copy_data(self): # copy key and cert files shutil.copy2(os.path.abspath('../ssl/test.server.ideco.usagestat.crt'), os.path.join(DATA_DEST_DIR, 'test.server.ideco.usagestat.crt')) shutil.copy2(os.path.abspath('../ssl/test.server.ideco.usagestat.key'), os.path.join(DATA_DEST_DIR, 'test.server.ideco.usagestat.key')) # copy test_data.conf shutil.copy2(os.path.abspath('test_data.conf'), os.path.join(DATA_DEST_DIR, 'test_data.conf')) # copy ics.conf shutil.copy2(os.path.abspath('../stat_ics_conf_collector/tests/ics.conf'), os.path.join(DATA_DEST_DIR, 'ics.conf')) def _rm_data(self): rm_dir_if_exist(DATA_DEST_DIR) def _copy_code(self): # copy stat_source_common shutil.copytree(os.path.abspath('../stat_source_common'), os.path.join(CODE_DEST_DIR, 'stat_source_common')) # copy stat_db_source shutil.copytree(os.path.abspath('../stat_db_source'), os.path.join(CODE_DEST_DIR, 'stat_db_source')) # copy stat_file_source shutil.copytree(os.path.abspath('../stat_file_source'), os.path.join(CODE_DEST_DIR, 'stat_file_source')) # copy stat_ics_conf_collector & conf file shutil.copytree(os.path.abspath('../stat_ics_conf_collector'), os.path.join(CODE_DEST_DIR, 'stat_ics_conf_collector')) shutil.copy2(os.path.abspath('test_settings/stat_ics_conf_collector.settings.py'), os.path.join(CODE_DEST_DIR, 'stat_ics_conf_collector/stat_ics_conf_collector/settings.py')) # copy stat_ics_db_collector & conf file shutil.copytree(os.path.abspath('../stat_ics_db_collector'), os.path.join(CODE_DEST_DIR, 'stat_ics_db_collector')) shutil.copy2(os.path.abspath('test_settings/stat_ics_db_collector.settings.py'), os.path.join(CODE_DEST_DIR, 'stat_ics_db_collector/stat_ics_db_collector/settings.py')) # copy stat_sender & conf file shutil.copytree(os.path.abspath('../stat_sender'), os.path.join(CODE_DEST_DIR, 'stat_sender')) shutil.copy2(os.path.abspath('test_settings/stat_sender.settings.py'), os.path.join(CODE_DEST_DIR, 'stat_sender/stat_sender/settings.py')) # copy stat_server & conf file shutil.copytree(os.path.abspath('../stat_server'), os.path.join(CODE_DEST_DIR, 'stat_server')) shutil.copy2(os.path.abspath('test_settings/stat_server.settings.py'), os.path.join(CODE_DEST_DIR, 'stat_server/stat_server/settings.py')) def _rm_code(self): rm_dir_if_exist(CODE_DEST_DIR) def _prepare_code(self): python_path_list = [os.path.join(CODE_DEST_DIR, 'stat_source_common'), os.path.join(CODE_DEST_DIR, 'stat_db_source'), os.path.join(CODE_DEST_DIR, 'stat_file_source'), os.path.join(CODE_DEST_DIR, 'stat_ics_conf_collector'), os.path.join(CODE_DEST_DIR, 'stat_ics_db_collector'), os.path.join(CODE_DEST_DIR, 'stat_sender'), os.path.join(CODE_DEST_DIR, 'stat_server')] old_pythonpath = os.getenv(PYTHONPATH_ENV, '') delta_pythonpath = ':'.join(python_path_list) new_pythonpath = python_path_list if old_pythonpath == '' else old_pythonpath + ':' + delta_pythonpath os.putenv(PYTHONPATH_ENV, new_pythonpath)
class TestCollector(TestCase): def __init__(self, methodName='runTest'): super(TestCollector, self).__init__(methodName) # source db settings.ICS_DB_CONN_STR = {'host': str('localhost'), 'database': str('/tmp/ics_main.gdb'), 'user': str('SYSDBA'), 'password': str('masterkey')} source_location = os.path.abspath('tests/ics_main.gdb') self._source_db_manager = FirebirdDbManager(source_location, settings.ICS_DB_CONN_STR['database'], settings.ICS_DB_CONN_STR['user'], settings.ICS_DB_CONN_STR['password'], settings.ICS_DB_CONN_STR['host']) # dest db self._dest_db_manager = sqlite_db_manager.SqliteDbManager('../stat_sender_db/create.py') settings.DEST_DB_CONN_STR = self._dest_db_manager.connection_string settings.LOG_CONF['handlers'] = {'console': {'level': 'INFO', 'class': 'logging.StreamHandler', 'formatter': 'default'}} settings.LOG_CONF['loggers'] = {'stat_ics_conf_collector.entry_point': {'handlers': ['console'], 'level': 'INFO', 'propagate': True}} def setUp(self): self._source_db_manager.__enter__() self._dest_db_manager.__enter__() def tearDown(self): self._dest_db_manager.__exit__(None, None, None) self._source_db_manager.__exit__(None, None, None) def test_on_ics_db_with_test_data(self): base_expected_data = [('users.EndUserCount', 9), ('users.EndUserWithAgentAuthCount', 4), ('ad.ADSync', 'enabled'), ('ad.EndUserCount', 2)] lic_data = list(base_expected_data) lic_data.append(('license.Type', 'Trial')) self._test_common_body(lambda: self._prepare_data(None), lic_data) lic_data = list(base_expected_data) lic_data.append(('license.Type', 'Standard Named')) self._test_common_body(lambda: self._prepare_data(4), lic_data) lic_data = list(base_expected_data) lic_data.append(('license.Type', 'Enterprise Named')) self._test_common_body(lambda: self._prepare_data(5), lic_data) lic_data = list(base_expected_data) lic_data.append(('license.Type', 'Standard Concurrent')) self._test_common_body(lambda: self._prepare_data(6), lic_data) lic_data = list(base_expected_data) lic_data.append(('license.Type', 'Enterprise Concurrent')) self._test_common_body(lambda: self._prepare_data(7), lic_data) def test_on_ics_db_with_test_data_and_wrong_license(self): self._prepare_data(666) result = collector_entry_point.execute() self.assertFalse(result) def _test_common_body(self, prepare_fun, expected_data): prepare_fun() result = collector_entry_point.execute() self.assertTrue(result) actual = self._dest_db_manager.execute_query('SELECT * FROM STAT_DATA') self._check_data('ics.db', expected_data, actual) self._dest_db_manager.execute_nonquery('DELETE FROM STAT_DATA') def _prepare_data(self, reg_ver=None): self._source_db_manager.execute_nonquery('DELETE FROM REG') # ID, REG_VER reg_data = [(1, reg_ver)] insert_reg_query = 'INSERT INTO REG(ID, REG_VER) VALUES(?, ?)' self._source_db_manager.execute_nonquery(insert_reg_query, reg_data) self._source_db_manager.execute_nonquery('DELETE FROM USERS') # ID, PARID, EMAIL, LOGIN, ENABLED, DELETED, END_USER, SERVER, AUTH_TYPE, AD_IS user_data = [(1, None, '*****@*****.**', 'root', 1, 0, 0, 0, None, 0), (2, 1, '*****@*****.**', 'subroot', 1, 0, 0, 0, None, 0), (3, 1, '*****@*****.**', 'subroot_ad', 1, 0, 0, 0, None, 1), (400, 2, '*****@*****.**', 'ivanoff66', 1, 0, 1, 0, 1, 0), (401, 2, '*****@*****.**', 'petroff66', 0, 0, 1, 0, 1, 0), (402, 2, '*****@*****.**', 'sidoroff88', 1, 1, 1, 0, 1, 0), (403, 2, '*****@*****.**', 'kozlov74', 1, 0, 1, 0, 2, 0), (404, 2, '*****@*****.**', 'petuhov76', 0, 0, 1, 0, 2, 0), (405, 2, '*****@*****.**', 'bobrov81', 1, 1, 1, 0, 2, 0), (406, 2, '*****@*****.**', 'berezko83', 1, 0, 1, 0, 3, 0), (407, 2, '*****@*****.**', 'osinko84', 0, 0, 1, 0, 3, 0), (408, 2, '*****@*****.**', 'dub85', 1, 1, 1, 0, 3, 0), (410, 2, '', 'web-server', 1, 0, 0, 1, 1, 0), (411, 2, '', 'ftp-server', 0, 0, 0, 1, 1, 0), (412, 2, '', 'jabber-server', 1, 1, 0, 1, 1, 0), (500, 3, '*****@*****.**', 'ctulhu666', 1, 0, 1, 0, 1, 1), (501, 3, '*****@*****.**', 'shambler99', 0, 0, 1, 0, 1, 1), (502, 3, 'kiberdemon [email protected]', 'kiberdemon 777', 1, 1, 1, 0, 1, 1), (504, 3, '*****@*****.**', 'kakodemon13', 1, 0, 1, 0, 1, 0)] insert_user_query = 'INSERT INTO USERS(ID, PARID, EMAIL, LOGIN, ENABLED, DELETED, END_USER, SERVER, AUTH_TYPE, AD_IS) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' self._source_db_manager.execute_nonquery(insert_user_query, user_data) # TODO (aushakov) : move 2 stat_db_funtest_utils # spec: str, [(str, str)], [(int, str, str, str, str)] -> None def _check_data(self, source_id, expected, actual): self.assertEqual(len(expected), len(actual)) for expected_row in expected: actual_row = self._get_actual_row(expected_row, actual) self.assertIsNotNone(actual_row) self.assertEqual(source_id, actual_row[1]) # TODO (aushakov) : move 2 stat_db_funtest_utils # spec: str, (str, str), [(int, str, str, str, str)] -> (int, str, str, str, str) def _get_actual_row(self, expected_row, actual_rows): expected_category = expected_row[0] expected_data = expected_row[1] find_result = filter(lambda (id, source_id, category, timemarker, data): category == expected_category and data == expected_data, actual_rows) if len(find_result) == 1: return find_result[0] raise KeyError("row with category = '{0:s}', data = '{1!s}' not found".format(expected_row[0], expected_row[1]))