2929import static org .apache .hadoop .test .MetricsAsserts .assertGauge ;
3030import static org .apache .hadoop .test .MetricsAsserts .getMetrics ;
3131import static org .junit .Assert .assertNotEquals ;
32+ import static org .junit .Assert .assertNotNull ;
3233import static org .junit .Assert .assertTrue ;
3334import static org .junit .Assert .assertEquals ;
3435import static org .junit .Assert .fail ;
4243import java .io .File ;
4344import java .io .FileInputStream ;
4445import java .io .IOException ;
46+ import java .io .InputStream ;
4547import java .io .InputStreamReader ;
4648import java .net .HttpURLConnection ;
4749import java .net .MalformedURLException ;
@@ -160,7 +162,7 @@ public void testMaxConnections() throws Exception {
160162 shuffleHandler .init (conf );
161163 shuffleHandler .start ();
162164 final String port = shuffleHandler .getConfig ().get (SHUFFLE_PORT_CONFIG_KEY );
163- final SecretKey secretKey = shuffleHandler .addTestApp ();
165+ final SecretKey secretKey = shuffleHandler .addTestApp (TEST_USER );
164166
165167 // setup connections
166168 HttpURLConnection [] conns = new HttpURLConnection [connAttempts ];
@@ -238,7 +240,7 @@ public void testKeepAlive() throws Exception {
238240 shuffleHandler .init (conf );
239241 shuffleHandler .start ();
240242 final String port = shuffleHandler .getConfig ().get (ShuffleHandler .SHUFFLE_PORT_CONFIG_KEY );
241- final SecretKey secretKey = shuffleHandler .addTestApp ();
243+ final SecretKey secretKey = shuffleHandler .addTestApp (TEST_USER );
242244
243245 HttpURLConnection conn1 = createRequest (
244246 geURL (port , TEST_JOB_ID , 0 , Collections .singletonList (TEST_ATTEMPT_1 ), true ),
@@ -279,18 +281,34 @@ public void testMapFileAccess() throws IOException {
279281 conf .set (CommonConfigurationKeysPublic .HADOOP_SECURITY_AUTHENTICATION , "kerberos" );
280282 UserGroupInformation .setConfiguration (conf );
281283
284+ final String randomUser = "randomUser" ;
285+ final String attempt = "attempt_1111111111111_0004_m_000004_0" ;
286+ generateMapOutput (randomUser , tempDir .toAbsolutePath ().toString (), attempt ,
287+ Arrays .asList (TEST_DATA_C , TEST_DATA_B , TEST_DATA_A ));
288+
282289 ShuffleHandlerMock shuffleHandler = new ShuffleHandlerMock ();
283290 shuffleHandler .init (conf );
284291 try {
285292 shuffleHandler .start ();
286293 final String port = shuffleHandler .getConfig ().get (ShuffleHandler .SHUFFLE_PORT_CONFIG_KEY );
287- final SecretKey secretKey = shuffleHandler .addTestApp ();
294+ final SecretKey secretKey = shuffleHandler .addTestApp (randomUser );
288295
289296 HttpURLConnection conn = createRequest (
290- geURL (port , TEST_JOB_ID , 0 , Collections .singletonList (TEST_ATTEMPT_1 ), false ),
297+ geURL (port , TEST_JOB_ID , 0 , Collections .singletonList (attempt ), false ),
291298 secretKey );
292299 conn .connect ();
293- BufferedReader in = new BufferedReader (new InputStreamReader (conn .getInputStream ()));
300+
301+ InputStream is = null ;
302+ try {
303+ is = conn .getInputStream ();
304+ } catch (IOException ioe ) {
305+ if (conn .getResponseCode () != HttpURLConnection .HTTP_OK ) {
306+ is = conn .getErrorStream ();
307+ }
308+ }
309+
310+ assertNotNull (is );
311+ BufferedReader in = new BufferedReader (new InputStreamReader (is ));
294312 StringBuilder builder = new StringBuilder ();
295313 String inputLine ;
296314 while ((inputLine = in .readLine ()) != null ) {
@@ -300,19 +318,19 @@ public void testMapFileAccess() throws IOException {
300318 String receivedString = builder .toString ();
301319
302320 //Retrieve file owner name
303- String indexFilePath = getIndexFile (tempDir .toAbsolutePath ().toString (), TEST_ATTEMPT_1 );
321+ String indexFilePath = getIndexFile (randomUser , tempDir .toAbsolutePath ().toString (), attempt );
304322 String owner ;
305323 try (FileInputStream fis = new FileInputStream (indexFilePath )) {
306324 owner = NativeIO .POSIX .getFstat (fis .getFD ()).getOwner ();
307325 }
308326
309327 String message =
310328 "Owner '" + owner + "' for path " + indexFilePath
311- + " did not match expected owner '" + TEST_USER + "'" ;
329+ + " did not match expected owner '" + randomUser + "'" ;
312330 assertTrue (String .format ("Received string '%s' should contain " +
313331 "message '%s'" , receivedString , message ),
314332 receivedString .contains (message ));
315- assertEquals (HttpURLConnection .HTTP_OK , conn .getResponseCode ());
333+ assertEquals (HttpURLConnection .HTTP_INTERNAL_ERROR , conn .getResponseCode ());
316334 LOG .info ("received: " + receivedString );
317335 assertNotEquals ("" , receivedString );
318336 } finally {
@@ -335,7 +353,7 @@ public void testRecovery() throws IOException {
335353 shuffle .init (conf );
336354 shuffle .start ();
337355 final String port = shuffle .getConfig ().get (ShuffleHandler .SHUFFLE_PORT_CONFIG_KEY );
338- final SecretKey secretKey = shuffle .addTestApp ();
356+ final SecretKey secretKey = shuffle .addTestApp (TEST_USER );
339357
340358 // verify we are authorized to shuffle
341359 int rc = getShuffleResponseCode (port , secretKey );
@@ -388,7 +406,7 @@ public void testRecoveryFromOtherVersions() throws IOException {
388406 shuffle .init (conf );
389407 shuffle .start ();
390408 final String port = shuffle .getConfig ().get (ShuffleHandler .SHUFFLE_PORT_CONFIG_KEY );
391- final SecretKey secretKey = shuffle .addTestApp ();
409+ final SecretKey secretKey = shuffle .addTestApp (TEST_USER );
392410
393411 // verify we are authorized to shuffle
394412 int rc = getShuffleResponseCode (port , secretKey );
@@ -490,14 +508,14 @@ private static HttpURLConnection createRequest(URL url, SecretKey secretKey) thr
490508
491509 class ShuffleHandlerMock extends ShuffleHandler {
492510
493- public SecretKey addTestApp () throws IOException {
511+ public SecretKey addTestApp (String user ) throws IOException {
494512 DataOutputBuffer outputBuffer = new DataOutputBuffer ();
495513 outputBuffer .reset ();
496514 Token <JobTokenIdentifier > jt = new Token <>(
497- "identifier" .getBytes (), "password" .getBytes (), new Text (TEST_USER ),
515+ "identifier" .getBytes (), "password" .getBytes (), new Text (user ),
498516 new Text ("shuffleService" ));
499517 jt .write (outputBuffer );
500- initializeApplication (new ApplicationInitializationContext (TEST_USER , TEST_APP_ID ,
518+ initializeApplication (new ApplicationInitializationContext (user , TEST_APP_ID ,
501519 ByteBuffer .wrap (outputBuffer .getData (), 0 ,
502520 outputBuffer .getLength ())));
503521
0 commit comments