### Question:
TransferFsImage { static MD5Hash getFileClient(String nnHostPort, String queryString, List<File> localPaths, Storage dstStorage, boolean getChecksum) throws IOException { String str = HttpConfig.getSchemePrefix() + nnHostPort + "/getimage?" + queryString; LOG.info("Opening connection to " + str); URL url = new URL(str); return doGetUrl(url, localPaths, dstStorage, getChecksum); }  static void downloadMostRecentImageToDirectory(String fsName,
      File dir); static MD5Hash downloadImageToStorage(
      String fsName, long imageTxId, Storage dstStorage, boolean needDigest); static void uploadImageFromStorage(String fsName,
      InetSocketAddress imageListenAddress,
      Storage storage, long txid); static void getFileServer(ServletResponse response, File localfile,
      FileInputStream infile,
      DataTransferThrottler throttler); static MD5Hash doGetUrl(URL url, List<File> localPaths,
      Storage dstStorage, boolean getChecksum); final static String CONTENT_LENGTH; final static String MD5_HEADER; }### Answer:
@Test public void testClientSideException() throws IOException { Configuration conf = new HdfsConfiguration(); MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf) .numDataNodes(0).build(); NNStorage mockStorage = Mockito.mock(NNStorage.class); List<File> localPath = Collections.<File>singletonList( new File("/xxxxx-does-not-exist/blah")); try { String fsName = NetUtils.getHostPortString( cluster.getNameNode().getHttpAddress()); String id = "getimage=1&txid=0"; TransferFsImage.getFileClient(fsName, id, localPath, mockStorage, false); fail("Didn't get an exception!"); } catch (IOException ioe) { Mockito.verify(mockStorage).reportErrorOnFile(localPath.get(0)); assertTrue( "Unexpected exception: " + StringUtils.stringifyException(ioe), ioe.getMessage().contains("Unable to download to any storage")); } finally { cluster.shutdown(); } }
@Test public void testClientSideExceptionOnJustOneDir() throws IOException { Configuration conf = new HdfsConfiguration(); MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf) .numDataNodes(0).build(); NNStorage mockStorage = Mockito.mock(NNStorage.class); List<File> localPaths = ImmutableList.of( new File("/xxxxx-does-not-exist/blah"), new File(TEST_DIR, "testfile") ); try { String fsName = NetUtils.getHostPortString( cluster.getNameNode().getHttpAddress()); String id = "getimage=1&txid=0"; TransferFsImage.getFileClient(fsName, id, localPaths, mockStorage, false); Mockito.verify(mockStorage).reportErrorOnFile(localPaths.get(0)); assertTrue("The valid local file should get saved properly", localPaths.get(1).length() > 0); } finally { cluster.shutdown(); } }
@Test(timeout = 5000) public void testImageTransferTimeout() throws Exception { HttpServer testServer = HttpServerFunctionalTest.createServer("hdfs"); try { testServer.addServlet("GetImage", "/getimage", TestGetImageServlet.class); testServer.start(); URL serverURL = HttpServerFunctionalTest.getServerURL(testServer); TransferFsImage.timeout = 2000; try { TransferFsImage.getFileClient(serverURL.getAuthority(), "txid=1", null, null, false); fail("TransferImage Should fail with timeout"); } catch (SocketTimeoutException e) { assertEquals("Read should timeout", "Read timed out", e.getMessage()); } } finally { if (testServer != null) { testServer.stop(); } } }