10111: Merge branch 'master' into 10111-cr-provenance-graph
[arvados.git] / sdk / python / tests / test_cache.py
index 6a4d4cf02e47ae9b4113ccd604286ff7ae265e7d..4d68b401ee2c219cce872ce75d6ec81a6a678108 100644 (file)
@@ -1,12 +1,17 @@
+from __future__ import print_function
+
 import md5
 import mock
-import shutil
+import os
 import random
+import shutil
+import sys
 import tempfile
 import threading
 import unittest
 
 import arvados.cache
+import arvados
 import run_test_server
 
 
@@ -22,13 +27,19 @@ class CacheTestThread(threading.Thread):
     def run(self):
         c = arvados.cache.SafeHTTPCache(self._dir)
         url = 'http://example.com/foo'
+        self.ok = True
         for x in range(16):
-            data_in = _random(128)
-            data_in = md5.new(data_in).hexdigest() + "\n" + str(data_in)
-            c.set(url, data_in)
-            data_out = c.get(url)
-            digest, content = data_out.split("\n", 1)
-            self.ok = (digest == md5.new(content).hexdigest())
+            try:
+                data_in = _random(128)
+                data_in = md5.new(data_in).hexdigest() + "\n" + str(data_in)
+                c.set(url, data_in)
+                data_out = c.get(url)
+                digest, content = data_out.split("\n", 1)
+                if digest != md5.new(content).hexdigest():
+                    self.ok = False
+            except Exception as err:
+                self.ok = False
+                print("cache failed: {}".format(err), file=sys.stderr)
 
 
 class CacheTest(unittest.TestCase):
@@ -38,6 +49,17 @@ class CacheTest(unittest.TestCase):
     def tearDown(self):
         shutil.rmtree(self._dir)
 
+    def test_cache_create_error(self):
+        _, filename = tempfile.mkstemp()
+        home_was = os.environ['HOME']
+        os.environ['HOME'] = filename
+        try:
+            c = arvados.http_cache('test')
+            self.assertEqual(None, c)
+        finally:
+            os.environ['HOME'] = home_was
+            os.unlink(filename)
+
     def test_cache_crud(self):
         c = arvados.cache.SafeHTTPCache(self._dir, max_age=0)
         url = 'https://example.com/foo?bar=baz'
@@ -63,6 +85,7 @@ class CacheTest(unittest.TestCase):
             t.join()
             self.assertTrue(t.ok)
 
+
 class CacheIntegrationTest(run_test_server.TestCaseWithServers):
     MAIN_SERVER = {}