diff --git a/socorro/lib/buildutil.py b/socorro/lib/buildutil.py index b464d466b2..522740ad2c 100644 --- a/socorro/lib/buildutil.py +++ b/socorro/lib/buildutil.py @@ -3,56 +3,32 @@ builds to Socorro. """ import logging - -import util +import psycopg2 logger = logging.getLogger("webapi") -def build_exists(cursor, product_name, version, platform, build_id, build_type, - beta_number, repository): - """ Determine whether or not a particular release build exists already """ - sql = """ - SELECT * - FROM releases_raw - WHERE product_name = %s - AND version = %s - AND platform = %s - AND build_id = %s - AND build_type = %s - """ - - if beta_number is not None: - sql += """ AND beta_number = %s """ - else: - sql += """ AND beta_number IS %s """ - - sql += """ AND repository = %s """ - - params = (product_name, version, platform, build_id, build_type, - beta_number, repository) - cursor.execute(sql, params) - exists = cursor.fetchone() +def insert_build(cursor, product_name, version, platform, build_id, build_type, + beta_number, repository): + """ Insert a particular build into the database """ + # As we use beta numbers, we don't want to keep the 'bX' in versions + if "b" in version: + version = version[:version.index("b")] - return exists is not None + params = (str(product_name), version, build_type, int(build_id), + platform, int(beta_number), repository) + logger.info("Trying to insert new release: %s %s %s %s %s %s %s" % params) -def insert_build(cursor, product_name, version, platform, build_id, build_type, - beta_number, repository): - """ Insert a particular build into the database """ - if not build_exists(cursor, product_name, version, platform, build_id, - build_type, beta_number, repository): - sql = """ INSERT INTO releases_raw - (product_name, version, platform, build_id, build_type, - beta_number, repository) - VALUES (%s, %s, %s, %s, %s, %s, %s)""" + sql = """/* socorro.lib.buildutil.insert_build */ + SELECT add_new_release(%s, %s, %s, %s, %s, %s, %s) + """ - try: - params = (product_name, version, platform, build_id, build_type, - beta_number, repository) - cursor.execute(sql, params) - cursor.connection.commit() - logger.info("Inserted: %s %s %s %s %s %s %s" % params) - except Exception: - cursor.connection.rollback() - util.reportExceptionAndAbort(logger) + try: + cursor.execute(sql, params) + cursor.connection.commit() + except psycopg2.Error, e: + cursor.connection.rollback() + logger.error("Failed inserting new release: %s" % e, + exc_info=True) + raise diff --git a/socorro/unittest/cron/testFtpScraper.py b/socorro/unittest/cron/testFtpScraper.py index e105aea931..d486fc48e5 100644 --- a/socorro/unittest/cron/testFtpScraper.py +++ b/socorro/unittest/cron/testFtpScraper.py @@ -17,28 +17,6 @@ import cronTestconfig as testConfig -def makeBogusBuilds(connection, cursor): - # (product, version, platform, buildid, changeset, filename, date) - fakeBuildsData = [ - ("PRODUCTNAME1", "VERSIONAME1", "PLATFORMNAME1", "1", "BUILDTYPE1", "1", - "REPO1"), - ("PRODUCTNAME2", "VERSIONAME2", "PLATFORMNAME2", "2", "BUILDTYPE2", "2", - "REPO2"), - ("PRODUCTNAME3", "VERSIONAME3", "PLATFORMNAME3", "3", "BUILDTYPE3", "3", - "REPO3"), - ("PRODUCTNAME4", "VERSIONAME4", "PLATFORMNAME4", "4", "BUILDTYPE4", "4", - "REPO4") - ] - - for b in fakeBuildsData: - sql = """INSERT INTO releases_raw - (product_name, version, platform, build_id, build_type, - beta_number, repository) - VALUES (%s, %s, %s, %s, %s, %s, %s)""" - cursor.execute(sql, b) - connection.commit() - - class Me: """ Initialize once per module @@ -98,7 +76,6 @@ def setup_module(): except Exception, x: print "Exception in setup_module() connecting to db: ", type(x), x socorro.lib.util.reportExceptionAndAbort(me.fileLogger) - makeBogusBuilds(me.conn, me.cur) def teardown_module(): @@ -139,44 +116,6 @@ def setUp(self): def tearDown(self): self.logger.clear() - def do_build_exists(self, d, correct): - me.cur = me.conn.cursor(cursor_factory=psy.LoggingCursor) - me.cur.setLogger(me.fileLogger) - - actual = buildutil.build_exists( - me.cur, d[0], d[1], d[2], d[3], d[4], d[5], d[6]) - assert actual == correct, "expected %s, got %s " % (correct, actual) - - def test_build_exists(self): - d = ("failfailfail", "VERSIONAME1", "PLATFORMNAME1", "1", - "BUILDTYPE1", "1", "REPO1") - self.do_build_exists(d, False) - r = ("PRODUCTNAME1", "VERSIONAME1", "PLATFORMNAME1", "1", - "BUILDTYPE1", "1", "REPO1") - self.do_build_exists(r, True) - - def test_insert_build(self): - me.cur = me.conn.cursor(cursor_factory=psy.LoggingCursor) - me.cur.setLogger(me.fileLogger) - - sql = """DELETE FROM releases_raw - WHERE product_name = 'PRODUCTNAME5'""" - me.cur.execute(sql) - me.cur.connection.commit() - - try: - buildutil.insert_build(me.cur, 'PRODUCTNAME5', 'VERSIONAME5', - 'PLATFORMNAME5', '5', 'BUILDTYPE5', '5', 'REPO5') - actual = buildutil.build_exists(me.cur, 'PRODUCTNAME5', - 'VERSIONAME5', 'PLATFORMNAME5', '5', 'BUILDTYPE5', - '5', 'REPO5') - assert actual == 1, "expected 1, got %s" % (actual) - except Exception, x: - print "Exception in do_insert_build() ... Error: ", type(x), x - socorro.lib.util.reportExceptionAndAbort(me.fileLogger) - finally: - me.cur.connection.rollback() - def test_getLinks(self): self.config.products = ('PRODUCT1', 'PRODUCT2') self.config.base_url = 'http://www.example.com/' diff --git a/socorro/unittest/lib/test_buildutil.py b/socorro/unittest/lib/test_buildutil.py new file mode 100644 index 0000000000..b2e423f0a8 --- /dev/null +++ b/socorro/unittest/lib/test_buildutil.py @@ -0,0 +1,116 @@ +import psycopg2 + +from socorro.unittest.external.postgresql.unittestbase import \ + PostgreSQLTestCase +from socorro.lib import buildutil + + +#============================================================================== +class TestBuildUtil(PostgreSQLTestCase): + + def setUp(self): + """Set up this test class by populating the products table with fake + data. """ + super(TestBuildUtil, self).setUp() + + cursor = self.connection.cursor() + + # Insert data + cursor.execute(""" + INSERT INTO products + (product_name, sort, rapid_release_version, release_name) + VALUES + ( + 'Firefox', + '0', + '15.0', + 'firefox' + ), + ( + 'Product', + '0', + '1.0', + 'product' + ); + """) + + cursor.execute(""" + INSERT INTO release_channels + (release_channel, sort) + VALUES + ('Release', 1), + ('Beta', 2), + ('Aurora', 3), + ('Nightly', 4); + """) + + cursor.execute(""" + SELECT add_new_release( + 'Product', '1.0', 'Release', 20111223, 'Linux', NULL, + 'mozilla-central' + ); + """) + + self.connection.commit() + + def tearDown(self): + """Clean up the database, delete tables and functions. """ + cursor = self.connection.cursor() + cursor.execute(""" + TRUNCATE products CASCADE; + TRUNCATE release_channels CASCADE; + TRUNCATE releases_raw CASCADE; + """) + self.connection.commit() + super(TestBuildUtil, self).tearDown() + + def build_exists(self, cursor, product_name, version, platform, build_id, + build_type, beta_number, repository): + """ Determine whether or not a particular release build exists already. + """ + sql = """ + SELECT * + FROM releases_raw + WHERE product_name = %s + AND version = %s + AND platform = %s + AND build_id = %s + AND build_type = %s + """ + + if beta_number is not None: + sql += """ AND beta_number = %s """ + else: + sql += """ AND beta_number IS %s """ + + sql += """ AND repository = %s """ + + params = (product_name, version, platform, build_id, build_type, + beta_number, repository) + cursor.execute(sql, params) + exists = cursor.fetchone() + + return exists is not None + + + def test_insert_build(self): + cursor = self.connection.cursor() + + # Test 1: successfully insert a build + buildutil.insert_build(cursor, 'Firefox', 'VERSIONAME5', + 'PLATFORMNAME5', '20110101', 'Release', '5', 'REPO5') + actual = self.build_exists(cursor, 'Firefox', + 'VERSIONAME5', 'PLATFORMNAME5', '20110101', 'Release', + '5', 'REPO5') + self.assertTrue(actual) + + # Test 2: fail at inserting a build + self.assertRaises( + psycopg2.Error, + buildutil.insert_build, + *(cursor, 'Unknown', 'VERSIONAME5', 'PLATFORMNAME5', + '20110101', 'Release', '5', 'REPO5')) + actual = self.build_exists(cursor, 'Unknown', + 'VERSIONAME5', 'PLATFORMNAME5', '20110101', 'Release', + '5', 'REPO5') + self.assertFalse(actual)