|
27 | 27 | import static org.mockito.Mockito.doAnswer; |
28 | 28 | import static org.junit.Assert.assertTrue; |
29 | 29 |
|
| 30 | +import java.io.DataOutputStream; |
30 | 31 | import java.io.File; |
31 | 32 | import java.io.FileNotFoundException; |
32 | 33 | import java.io.FileReader; |
|
41 | 42 | import java.util.List; |
42 | 43 | import java.util.Random; |
43 | 44 |
|
44 | | -import org.apache.hadoop.fs.FileUtil; |
45 | | -import org.apache.hadoop.yarn.api.records.ApplicationAttemptId; |
46 | | -import org.apache.hadoop.yarn.api.records.ApplicationId; |
47 | | -import org.apache.hadoop.yarn.api.records.ContainerId; |
48 | | -import org.apache.hadoop.yarn.api.records.ContainerLaunchContext; |
49 | | -import org.apache.hadoop.yarn.conf.YarnConfiguration; |
50 | | -import org.apache.hadoop.yarn.server.nodemanager.containermanager.container.Container; |
51 | | -import org.apache.hadoop.yarn.server.nodemanager.containermanager.container.ContainerDiagnosticsUpdateEvent; |
52 | 45 |
|
53 | 46 | import org.apache.hadoop.conf.Configuration; |
54 | 47 | import org.apache.hadoop.fs.AbstractFileSystem; |
|
57 | 50 | import org.apache.hadoop.fs.FSDataOutputStream; |
58 | 51 | import org.apache.hadoop.fs.FileContext; |
59 | 52 | import org.apache.hadoop.fs.FileStatus; |
| 53 | +import org.apache.hadoop.fs.FileUtil; |
| 54 | +import org.apache.hadoop.fs.FsStatus; |
60 | 55 | import org.apache.hadoop.fs.Options.CreateOpts; |
61 | 56 | import org.apache.hadoop.fs.Path; |
62 | 57 | import org.apache.hadoop.fs.permission.FsPermission; |
63 | 58 | import org.apache.hadoop.io.DataInputBuffer; |
64 | 59 | import org.apache.hadoop.io.DataOutputBuffer; |
| 60 | +import org.apache.hadoop.security.Credentials; |
65 | 61 | import org.apache.hadoop.util.Progressable; |
| 62 | +import org.apache.hadoop.yarn.api.records.ApplicationAttemptId; |
| 63 | +import org.apache.hadoop.yarn.api.records.ApplicationId; |
| 64 | +import org.apache.hadoop.yarn.api.records.ContainerId; |
| 65 | +import org.apache.hadoop.yarn.api.records.ContainerLaunchContext; |
| 66 | +import org.apache.hadoop.yarn.conf.YarnConfiguration; |
| 67 | +import org.apache.hadoop.yarn.server.nodemanager.containermanager.container.Container; |
| 68 | +import org.apache.hadoop.yarn.server.nodemanager.containermanager.container.ContainerDiagnosticsUpdateEvent; |
66 | 69 | import org.apache.hadoop.yarn.server.nodemanager.containermanager.localizer.ContainerLocalizer; |
67 | 70 | import org.apache.hadoop.yarn.server.nodemanager.containermanager.localizer.FakeFSDataInputStream; |
68 | 71 |
|
| 72 | +import org.junit.After; |
69 | 73 | import org.junit.AfterClass; |
| 74 | +import org.junit.Assert; |
70 | 75 | import org.junit.Before; |
71 | 76 | import org.junit.Test; |
72 | | -import org.junit.After; |
73 | | -import org.junit.Assert; |
74 | 77 | import org.mockito.invocation.InvocationOnMock; |
75 | 78 | import org.mockito.stubbing.Answer; |
76 | 79 |
|
@@ -296,6 +299,102 @@ public Object answer(InvocationOnMock invocationOnMock) |
296 | 299 | } |
297 | 300 | } |
298 | 301 |
|
| 302 | + @Test(timeout = 30000) |
| 303 | + public void testStartLocalizer() |
| 304 | + throws IOException, InterruptedException { |
| 305 | + InetSocketAddress localizationServerAddress; |
| 306 | + final Path firstDir = new Path(BASE_TMP_PATH, "localDir1"); |
| 307 | + List<String> localDirs = new ArrayList<String>(); |
| 308 | + final Path secondDir = new Path(BASE_TMP_PATH, "localDir2"); |
| 309 | + List<String> logDirs = new ArrayList<String>(); |
| 310 | + final Path logDir = new Path(BASE_TMP_PATH, "logDir"); |
| 311 | + final Path tokenDir = new Path(BASE_TMP_PATH, "tokenDir"); |
| 312 | + FsPermission perms = new FsPermission((short)0770); |
| 313 | + |
| 314 | + Configuration conf = new Configuration(); |
| 315 | + localizationServerAddress = conf.getSocketAddr( |
| 316 | + YarnConfiguration.NM_BIND_HOST, |
| 317 | + YarnConfiguration.NM_LOCALIZER_ADDRESS, |
| 318 | + YarnConfiguration.DEFAULT_NM_LOCALIZER_ADDRESS, |
| 319 | + YarnConfiguration.DEFAULT_NM_LOCALIZER_PORT); |
| 320 | + |
| 321 | + final FileContext mockLfs = spy(FileContext.getLocalFSFileContext(conf)); |
| 322 | + final FileContext.Util mockUtil = spy(mockLfs.util()); |
| 323 | + doAnswer(new Answer() { |
| 324 | + @Override |
| 325 | + public Object answer(InvocationOnMock invocationOnMock) |
| 326 | + throws Throwable { |
| 327 | + return mockUtil; |
| 328 | + } |
| 329 | + }).when(mockLfs).util(); |
| 330 | + doAnswer(new Answer() { |
| 331 | + @Override |
| 332 | + public Object answer(InvocationOnMock invocationOnMock) |
| 333 | + throws Throwable { |
| 334 | + Path dest = (Path) invocationOnMock.getArguments()[1]; |
| 335 | + if (dest.toString().contains(firstDir.toString())) { |
| 336 | + // throw an Exception when copy token to the first local dir |
| 337 | + // to simulate no space on the first drive |
| 338 | + throw new IOException("No space on this drive " + |
| 339 | + dest.toString()); |
| 340 | + } else { |
| 341 | + // copy token to the second local dir |
| 342 | + DataOutputStream tokenOut = null; |
| 343 | + try { |
| 344 | + Credentials credentials = new Credentials(); |
| 345 | + tokenOut = mockLfs.create(dest, |
| 346 | + EnumSet.of(CREATE, OVERWRITE)); |
| 347 | + credentials.writeTokenStorageToStream(tokenOut); |
| 348 | + } finally { |
| 349 | + if (tokenOut != null) { |
| 350 | + tokenOut.close(); |
| 351 | + } |
| 352 | + } |
| 353 | + } |
| 354 | + return null; |
| 355 | + } |
| 356 | + }).when(mockUtil).copy(any(Path.class), any(Path.class)); |
| 357 | + doAnswer(new Answer() { |
| 358 | + @Override |
| 359 | + public Object answer(InvocationOnMock invocationOnMock) |
| 360 | + throws Throwable { |
| 361 | + Path p = (Path) invocationOnMock.getArguments()[0]; |
| 362 | + // let second local directory return more free space than |
| 363 | + // first local directory |
| 364 | + if (p.toString().contains(firstDir.toString())) { |
| 365 | + return new FsStatus(2000, 2000, 0); |
| 366 | + } else { |
| 367 | + return new FsStatus(1000, 0, 1000); |
| 368 | + } |
| 369 | + } |
| 370 | + }).when(mockLfs).getFsStatus(any(Path.class)); |
| 371 | + |
| 372 | + DefaultContainerExecutor mockExec = spy(new DefaultContainerExecutor( |
| 373 | + mockLfs)); |
| 374 | + mockExec.setConf(conf); |
| 375 | + localDirs.add(mockLfs.makeQualified(firstDir).toString()); |
| 376 | + localDirs.add(mockLfs.makeQualified(secondDir).toString()); |
| 377 | + logDirs.add(mockLfs.makeQualified(logDir).toString()); |
| 378 | + conf.setStrings(YarnConfiguration.NM_LOCAL_DIRS, |
| 379 | + localDirs.toArray(new String[localDirs.size()])); |
| 380 | + conf.set(YarnConfiguration.NM_LOG_DIRS, logDir.toString()); |
| 381 | + mockLfs.mkdir(tokenDir, perms, true); |
| 382 | + Path nmPrivateCTokensPath = new Path(tokenDir, "test.tokens"); |
| 383 | + String appSubmitter = "nobody"; |
| 384 | + String appId = "APP_ID"; |
| 385 | + String locId = "LOC_ID"; |
| 386 | + try { |
| 387 | + mockExec.startLocalizer(nmPrivateCTokensPath, localizationServerAddress, |
| 388 | + appSubmitter, appId, locId, localDirs, logDirs); |
| 389 | + } catch (IOException e) { |
| 390 | + Assert.fail("StartLocalizer failed to copy token file " + e); |
| 391 | + } finally { |
| 392 | + mockExec.deleteAsUser(appSubmitter, firstDir); |
| 393 | + mockExec.deleteAsUser(appSubmitter, secondDir); |
| 394 | + mockExec.deleteAsUser(appSubmitter, logDir); |
| 395 | + deleteTmpFiles(); |
| 396 | + } |
| 397 | + } |
299 | 398 | // @Test |
300 | 399 | // public void testInit() throws IOException, InterruptedException { |
301 | 400 | // Configuration conf = new Configuration(); |
|
0 commit comments