def test_insert(self): log = BuildLog(self.env, build=1, step='test', generator='distutils', filename='1.log.gz') full_file = log.get_log_file('1.log.gz') if os.path.exists(full_file): os.remove(full_file) log.messages = [(BuildLog.INFO, 'running tests'), (BuildLog.ERROR, 'tests failed')] log.insert() self.assertNotEqual(None, log.id) db = self.env.get_db_cnx() cursor = db.cursor() cursor.execute( "SELECT build,step,generator,filename FROM bitten_log " "WHERE id=%s", (log.id, )) self.assertEqual((1, 'test', 'distutils', '1.log.gz'), cursor.fetchone()) lines = gzip.open(full_file, "rb").readlines() self.assertEqual('running tests\n', lines[0]) self.assertEqual('tests failed\n', lines[1]) if os.path.exists(full_file): os.remove(full_file)
def add_log_table(env, db): """Add a table for storing the builds logs.""" from bitten.model import BuildLog, BuildStep cursor = db.cursor() connector, _ = DatabaseManager(env)._get_connector() for table in BuildLog._schema: for stmt in connector.to_sql(table): cursor.execute(stmt) cursor.execute("SELECT build,name,log FROM bitten_step " "WHERE log IS NOT NULL") for build, step, log in cursor: build_log = BuildLog(env, build, step) build_log.messages = [(BuildLog.INFO, msg) for msg in log.splitlines()] build_log.insert(db) cursor.execute("CREATE TEMPORARY TABLE old_step AS SELECT * FROM bitten_step") cursor.execute("DROP TABLE bitten_step") for table in BuildStep._schema: for stmt in connector.to_sql(table): cursor.execute(stmt) cursor.execute("INSERT INTO bitten_step (build,name,description,status," "started,stopped) SELECT build,name,description,status," "started,stopped FROM old_step")
def add_log_table(env, db): """Add a table for storing the builds logs.""" from bitten.model import BuildLog, BuildStep cursor = db.cursor() connector, _ = DatabaseManager(env)._get_connector() for table in BuildLog._schema: for stmt in connector.to_sql(table): cursor.execute(stmt) cursor.execute("SELECT build,name,log FROM bitten_step " "WHERE log IS NOT NULL") for build, step, log in cursor: build_log = BuildLog(env, build, step) build_log.messages = [(BuildLog.INFO, msg) for msg in log.splitlines()] build_log.insert(db) cursor.execute( "CREATE TEMPORARY TABLE old_step AS SELECT * FROM bitten_step") cursor.execute("DROP TABLE bitten_step") for table in BuildStep._schema: for stmt in connector.to_sql(table): cursor.execute(stmt) cursor.execute("INSERT INTO bitten_step (build,name,description,status," "started,stopped) SELECT build,name,description,status," "started,stopped FROM old_step")
def test_insert_empty(self): log = BuildLog(self.env, build=1, step='test', generator='distutils', filename="1.log") full_file = log.get_log_file('1.log') if os.path.exists(full_file): os.remove(full_file) log.messages = [] log.insert() self.assertNotEqual(None, log.id) db = self.env.get_db_cnx() cursor = db.cursor() cursor.execute("SELECT build,step,generator,filename FROM bitten_log " "WHERE id=%s", (log.id,)) self.assertEqual((1, 'test', 'distutils', '1.log'), cursor.fetchone()) file_exists = os.path.exists(full_file) if file_exists: os.remove(full_file) assert not file_exists
def test_insert_and_delete_files(self): # create - files should be created automatically build_log = BuildLog(self.env, build=1, step='test', generator='make') build_log.messages = [(BuildLog.INFO, 'running')] build_log.insert() # fetch it fresh - check object and files build_log = BuildLog.fetch(self.env, id=build_log.id) self.assertEquals(build_log.filename, "%s.log" % build_log.id) log_file = build_log.get_log_file(build_log.filename) levels_file = log_file+'.levels' self.failUnless(os.path.exists(log_file), 'log_file does not exist') self.failUnless(os.path.exists(levels_file), 'levels_file does not exist') self.assertEquals(build_log.messages, [(BuildLog.INFO, 'running')]) # delete - object and file should be gone build_log.delete() self.assertEquals(None, BuildLog.fetch(self.env, id=build_log.id)) self.failIf(os.path.exists(log_file), 'log_file exists after delete()') self.failIf(os.path.exists(levels_file), 'levels_file exists after delete()')
def test_insert(self): log = BuildLog(self.env, build=1, step='test', generator='distutils', filename='1.log') full_file = log.get_log_file('1.log') if os.path.exists(full_file): os.remove(full_file) log.messages = [ (BuildLog.INFO, 'running tests'), (BuildLog.ERROR, 'tests failed') ] log.insert() self.assertNotEqual(None, log.id) db = self.env.get_db_cnx() cursor = db.cursor() cursor.execute("SELECT build,step,generator,filename FROM bitten_log " "WHERE id=%s", (log.id,)) self.assertEqual((1, 'test', 'distutils', '1.log'), cursor.fetchone()) lines = open(full_file, "rb").readlines() self.assertEqual('running tests\n', lines[0]) self.assertEqual('tests failed\n', lines[1]) if os.path.exists(full_file): os.remove(full_file)