def test_local_backed_remote_cache_corrupt_artifact(self): """Ensure that a combined cache clears outputs after a failure to extract an artifact.""" with temporary_dir() as remote_cache_dir: with self.setup_server(cache_root=remote_cache_dir) as server: with self.setup_local_cache() as local: tmp = TempLocalArtifactCache(local.artifact_root, compression=1) remote = RESTfulArtifactCache(local.artifact_root, BestUrlSelector([server.url]), tmp) combined = RESTfulArtifactCache(local.artifact_root, BestUrlSelector([server.url]), local) key = CacheKey('muppet_key', 'fake_hash') results_dir = os.path.join(local.artifact_root, 'a/sub/dir') safe_mkdir(results_dir) self.assertTrue(os.path.exists(results_dir)) with self.setup_test_file(results_dir) as path: # Add to only the remote cache. remote.insert(key, [path]) # Corrupt the artifact in the remote storage. self.assertTrue(server.corrupt_artifacts(r'.*muppet_key.*') == 1) # An attempt to read the corrupt artifact should fail. self.assertFalse(combined.use_cached_files(key, results_dir=results_dir)) # The local artifact should not have been stored, and the results_dir should exist, # but be empty. self.assertFalse(local.has(key)) self.assertTrue(os.path.exists(results_dir)) self.assertTrue(len(os.listdir(results_dir)) == 0)
def test_combined_cache(self): """Make sure that the combined cache finds what it should and that it backfills.""" httpd = None httpd_thread = None try: with temporary_dir() as http_root: with temporary_dir() as cache_root: with pushd(http_root): # SimpleRESTHandler serves from the cwd. httpd = SocketServer.TCPServer(('localhost', 0), SimpleRESTHandler) port = httpd.server_address[1] httpd_thread = Thread(target=httpd.serve_forever) httpd_thread.start() with temporary_dir() as artifact_root: local = LocalArtifactCache(None, artifact_root, cache_root) remote = RESTfulArtifactCache(MockLogger(), artifact_root, 'http://localhost:%d' % port) combined = CombinedArtifactCache([local, remote]) key = CacheKey('muppet_key', 'fake_hash', 42) with temporary_file(artifact_root) as f: # Write the file. f.write(TEST_CONTENT1) path = f.name f.close() # No cache has key. self.assertFalse(local.has(key)) self.assertFalse(remote.has(key)) self.assertFalse(combined.has(key)) # No cache returns key. self.assertFalse(bool(local.use_cached_files(key))) self.assertFalse(bool(remote.use_cached_files(key))) self.assertFalse(bool(combined.use_cached_files(key))) # Attempting to use key that no cache had should not change anything. self.assertFalse(local.has(key)) self.assertFalse(remote.has(key)) self.assertFalse(combined.has(key)) # Add to only remote cache. remote.insert(key, [path]) self.assertFalse(local.has(key)) self.assertTrue(remote.has(key)) self.assertTrue(combined.has(key)) # Successfully using via remote should NOT change local. self.assertTrue(bool(remote.use_cached_files(key))) self.assertFalse(local.has(key)) # Successfully using via combined SHOULD backfill local. self.assertTrue(bool(combined.use_cached_files(key))) self.assertTrue(local.has(key)) self.assertTrue(bool(local.use_cached_files(key))) finally: if httpd: httpd.shutdown() if httpd_thread: httpd_thread.join()
def test_local_backed_remote_cache(self): """make sure that the combined cache finds what it should and that it backfills.""" with self.setup_server() as server: with self.setup_local_cache() as local: tmp = TempLocalArtifactCache(local.artifact_root, local.artifact_extraction_root, 0) remote = RESTfulArtifactCache(local.artifact_root, BestUrlSelector([server.url]), tmp) combined = RESTfulArtifactCache(local.artifact_root, BestUrlSelector([server.url]), local) key = CacheKey("muppet_key", "fake_hash") with self.setup_test_file(local.artifact_root) as path: # No cache has key. self.assertFalse(local.has(key)) self.assertFalse(remote.has(key)) self.assertFalse(combined.has(key)) # No cache returns key. self.assertFalse(bool(local.use_cached_files(key))) self.assertFalse(bool(remote.use_cached_files(key))) self.assertFalse(bool(combined.use_cached_files(key))) # Attempting to use key that no cache had should not change anything. self.assertFalse(local.has(key)) self.assertFalse(remote.has(key)) self.assertFalse(combined.has(key)) # Add to only remote cache. remote.insert(key, [path]) # After insertion to remote, remote and only remote should have key self.assertFalse(local.has(key)) self.assertTrue(remote.has(key)) self.assertTrue(combined.has(key)) # Successfully using via remote should NOT change local. self.assertTrue(bool(remote.use_cached_files(key))) self.assertFalse(local.has(key)) # Successfully using via combined SHOULD backfill local. self.assertTrue(bool(combined.use_cached_files(key))) self.assertTrue(local.has(key)) self.assertTrue(bool(local.use_cached_files(key)))
def test_local_backed_remote_cache(self): """make sure that the combined cache finds what it should and that it backfills""" with self.setup_server() as url: with self.setup_local_cache() as local: tmp = TempLocalArtifactCache(local.artifact_root, 0) remote = RESTfulArtifactCache(local.artifact_root, BestUrlSelector([url]), tmp) combined = RESTfulArtifactCache(local.artifact_root, BestUrlSelector([url]), local) key = CacheKey('muppet_key', 'fake_hash') with self.setup_test_file(local.artifact_root) as path: # No cache has key. self.assertFalse(local.has(key)) self.assertFalse(remote.has(key)) self.assertFalse(combined.has(key)) # No cache returns key. self.assertFalse(bool(local.use_cached_files(key))) self.assertFalse(bool(remote.use_cached_files(key))) self.assertFalse(bool(combined.use_cached_files(key))) # Attempting to use key that no cache had should not change anything. self.assertFalse(local.has(key)) self.assertFalse(remote.has(key)) self.assertFalse(combined.has(key)) # Add to only remote cache. remote.insert(key, [path]) # After insertion to remote, remote and only remote should have key self.assertFalse(local.has(key)) self.assertTrue(remote.has(key)) self.assertTrue(combined.has(key)) # Successfully using via remote should NOT change local. self.assertTrue(bool(remote.use_cached_files(key))) self.assertFalse(local.has(key)) # Successfully using via combined SHOULD backfill local. self.assertTrue(bool(combined.use_cached_files(key))) self.assertTrue(local.has(key)) self.assertTrue(bool(local.use_cached_files(key)))
def test_local_backed_remote_cache_corrupt_artifact(self): """Ensure that a combined cache clears outputs after a failure to extract an artifact.""" with temporary_dir() as remote_cache_dir: with self.setup_server(cache_root=remote_cache_dir) as server: with self.setup_local_cache() as local: tmp = TempLocalArtifactCache( local.artifact_root, local.artifact_extraction_root, compression=1) remote = RESTfulArtifactCache( local.artifact_root, BestUrlSelector([server.url]), tmp) combined = RESTfulArtifactCache( local.artifact_root, BestUrlSelector([server.url]), local) key = CacheKey("muppet_key", "fake_hash") results_dir = os.path.join(local.artifact_root, "a/sub/dir") safe_mkdir(results_dir) self.assertTrue(os.path.exists(results_dir)) with self.setup_test_file(results_dir) as path: # Add to only the remote cache. remote.insert(key, [path]) # Corrupt the artifact in the remote storage. self.assertTrue( server.corrupt_artifacts(r".*muppet_key.*") == 1) # An attempt to read the corrupt artifact should fail. self.assertFalse( combined.use_cached_files(key, results_dir=results_dir)) # The local artifact should not have been stored, and the results_dir should exist, # but be empty. self.assertFalse(local.has(key)) self.assertTrue(os.path.exists(results_dir)) self.assertTrue(len(os.listdir(results_dir)) == 0)