unittests: refactoring, deriving from common controllable class.

Bit of a large patch, but each feature touches on the other, making this
frankly easier to just do in one shot (both for review, and to write).

* cros_test_lib.TestCase is setup to be the TestCase class we derive from
  going forward; no unittest.TestCase unless theres a good reason.  This
  class does some basic protection- environ, getcwd(), and adds a
  assertRaises2 method for finer grained assertRaises type behaviour.
  Future useful new assert methods should be added there if they can be
  reused.

* Cleans up all mox usage so it's directly using a class of ours that is
  controllable as to whether or not VerifyAll() is ran; this mainly just
  cleans up and simplifies the existing hodgepodge usage.

* Created some convenience TestCase classes for deriving from;
  MoxTempDirTestCase in particular sees a lot of use.

* A minor bit of voodoo via cros_test_lib.StackedSetup.  In a *lot* of
  our code, we were screwing up invoking either a setUp step for one
  of our parents, or a tearDown; this is basically a known fault of
  unittest.TestCase, something everyone screws up sooner or later.
  This automatically stacks setUp's in class inheritance order, and
  ensures that tearDown is invoked in the reverse order; effectively
  a stack of "set this stuff up", and unwound.  In the process, suppressing
  another flaw of TestCase where if an exception occurs in setUp, tearDown
  will not be ran (which via our mox usage, when it occurs means all
  test methods invoked past that point *will* fail).  The vast majority
  of deletions map back to this; it just makes doing tests easier.

* Since everything now imports cros_test_lib, add a 'main' function that
  is our unittest.main analogue; this currently just ensures logging is
  setup correctly, but serves as a single point for any future additions
  (for example, a TempDirContextManager wrapping check that looks for
  crap files left behind by the tests).

* Since all were being touched, cleaned out the existing bad tempfile
  usage in tests.

* Since this involved grepping for unittest, wound up finding a lot of
  older modules that tried to invoke their unittests; none of them actually
  would work however due to the fact the library wasn't chmod +x'd (while
  one could do a python invocation on it, find it unlikely it was occurring),
  thus pulled that dead code.  Same angle, fixed any shebangs encountered
  in those files that were hardcoded to 2.6 (gPrecise is 2.7).

* Finally, TempDirMixin, and related were collapsed into TestCases; we're
  no longer having to mix MoxTestBase metaclass in, so we don't have to
  use mixins; this has the added benefit that we now can init whatever
  values the setUp/tearDown rely on (or test methods use) allowing
  pylint to be aware that yes, they exist, and yes, we allow access.

BUG=chromium-os:34677
TEST=ran/each/every/freaking test, making sure nothing new fails, nor
     that any existing failure maps back to something changed here.
     pylinting also.

Change-Id: Ie63b4b8ce455ca94fac2e7f832d8aa4db44d73e3
Reviewed-on: https://gerrit.chromium.org/gerrit/33834
Commit-Ready: Brian Harring <ferringb@chromium.org>
Reviewed-by: Brian Harring <ferringb@chromium.org>
Tested-by: Brian Harring <ferringb@chromium.org>
diff --git a/scripts/upload_prebuilts_unittest.py b/scripts/upload_prebuilts_unittest.py
index 540c92b..eade9ae 100755
--- a/scripts/upload_prebuilts_unittest.py
+++ b/scripts/upload_prebuilts_unittest.py
@@ -9,12 +9,12 @@
 import multiprocessing
 import sys
 import tempfile
-import unittest
 
 sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                 '..', '..'))
 from chromite.scripts import upload_prebuilts as prebuilt
 from chromite.lib import cros_build_lib
+from chromite.lib import cros_test_lib
 from chromite.lib import binpkg
 
 # pylint: disable=E1120,W0212,R0904
@@ -33,7 +33,7 @@
   return pkgindex
 
 
-class TestUpdateFile(unittest.TestCase):
+class TestUpdateFile(cros_test_lib.TempDirTestCase):
 
   def setUp(self):
     self.contents_str = ['# comment that should be skipped',
@@ -46,9 +46,6 @@
     os.write(temp_fd, '\n'.join(self.contents_str))
     os.close(temp_fd)
 
-  def tearDown(self):
-    os.remove(self.version_file)
-
   def _read_version_file(self, version_file=None):
     """Read the contents of self.version_file and return as a list."""
     if not version_file:
@@ -106,14 +103,7 @@
         os.remove(non_existent_file)
 
 
-class TestPrebuilt(unittest.TestCase):
-
-  def setUp(self):
-    self.mox = mox.Mox()
-
-  def tearDown(self):
-    self.mox.UnsetStubs()
-    self.mox.VerifyAll()
+class TestPrebuilt(cros_test_lib.MoxTestCase):
 
   def testGenerateUploadDict(self):
     base_local_path = '/b/cbuild/build/chroot/build/x86-dogfood/'
@@ -191,7 +181,7 @@
         expected_results['result'])
 
 
-class TestPackagesFileFiltering(unittest.TestCase):
+class TestPackagesFileFiltering(cros_test_lib.TestCase):
 
   def testFilterPkgIndex(self):
     pkgindex = SimplePackageIndex()
@@ -200,7 +190,7 @@
     self.assertEqual(pkgindex.modified, True)
 
 
-class TestPopulateDuplicateDB(unittest.TestCase):
+class TestPopulateDuplicateDB(cros_test_lib.TestCase):
 
   def testEmptyIndex(self):
     pkgindex = SimplePackageIndex(packages=False)
@@ -235,18 +225,14 @@
     self.assertRaises(KeyError, pkgindex._PopulateDuplicateDB, db, 0)
 
 
-class TestResolveDuplicateUploads(unittest.TestCase):
+class TestResolveDuplicateUploads(cros_test_lib.MoxTestCase):
 
   def setUp(self):
-    self.mox = mox.Mox()
     self.mox.StubOutWithMock(binpkg.time, 'time')
     binpkg.time.time().AndReturn(binpkg.TWO_WEEKS)
+    # wtf...?
     self.mox.ReplayAll()
 
-  def tearDown(self):
-    self.mox.UnsetStubs()
-    self.mox.VerifyAll()
-
   def testEmptyList(self):
     pkgindex = SimplePackageIndex()
     pristine = SimplePackageIndex()
@@ -298,14 +284,7 @@
     self.assertEqual(pkgindex.packages, expected_pkgindex.packages)
 
 
-class TestWritePackageIndex(unittest.TestCase):
-
-  def setUp(self):
-    self.mox = mox.Mox()
-
-  def tearDown(self):
-    self.mox.UnsetStubs()
-    self.mox.VerifyAll()
+class TestWritePackageIndex(cros_test_lib.MoxTestCase):
 
   def testSimple(self):
     pkgindex = SimplePackageIndex()
@@ -316,7 +295,7 @@
     self.assertEqual(f.read(), '')
 
 
-class TestUploadPrebuilt(unittest.TestCase):
+class TestUploadPrebuilt(cros_test_lib.TestCase):
 
   def setUp(self):
     class MockTemporaryFile(object):
@@ -333,10 +312,6 @@
     fake_pkgs_file = MockTemporaryFile('fake')
     self.pkgindex.WriteToNamedTemporaryFile().AndReturn(fake_pkgs_file)
 
-  def tearDown(self):
-    self.mox.UnsetStubs()
-    self.mox.VerifyAll()
-
   def testSuccessfulGsUpload(self):
     uploads = {'/packages/private.tbz2': 'gs://foo/private.tbz2'}
     self.mox.StubOutWithMock(prebuilt, 'GenerateUploadDict')
@@ -353,10 +328,9 @@
     uploader._UploadPrebuilt('/packages', 'suffix')
 
 
-class TestSyncPrebuilts(unittest.TestCase):
+class TestSyncPrebuilts(cros_test_lib.MoxTestCase):
 
   def setUp(self):
-    self.mox = mox.Mox()
     self.mox.StubOutWithMock(prebuilt, 'DeterminePrebuiltConfFile')
     self.mox.StubOutWithMock(prebuilt, 'RevGitFile')
     self.mox.StubOutWithMock(prebuilt, 'UpdateBinhostConfFile')
@@ -367,10 +341,6 @@
     self.key = 'PORTAGE_BINHOST'
     self.mox.StubOutWithMock(prebuilt.PrebuiltUploader, '_UploadPrebuilt')
 
-  def tearDown(self):
-    self.mox.UnsetStubs()
-    self.mox.VerifyAll()
-
   def testSyncHostPrebuilts(self):
     board = 'x86-foo'
     target = prebuilt.BuildTarget(board, 'aura')
@@ -432,14 +402,7 @@
     uploader.SyncBoardPrebuilts(self.version, self.key, True, True, True, None)
 
 
-class TestMain(unittest.TestCase):
-
-  def setUp(self):
-    self.mox = mox.Mox()
-
-  def tearDown(self):
-    self.mox.UnsetStubs()
-    self.mox.VerifyAll()
+class TestMain(cros_test_lib.MoxTestCase):
 
   def testMain(self):
     """Test that the main function works."""
@@ -494,5 +457,4 @@
     prebuilt.main([])
 
 if __name__ == '__main__':
-  cros_build_lib.SetupBasicLogging()
-  unittest.main()
+  cros_test_lib.main()