示例#1
0
        protected internal virtual ContainerExecutor CreateContainerExecutor()
        {
            DefaultContainerExecutor exec = new DefaultContainerExecutor();

            exec.SetConf(conf);
            return(exec);
        }
        public virtual void TestContainerLaunchAndStop()
        {
            containerManager.Start();
            FilePath    scriptFile       = Shell.AppendScriptExtension(tmpDir, "scriptFile");
            PrintWriter fileWriter       = new PrintWriter(scriptFile);
            FilePath    processStartFile = new FilePath(tmpDir, "start_file.txt").GetAbsoluteFile
                                               ();
            // ////// Construct the Container-id
            ContainerId cId = CreateContainerId(0);

            if (Shell.Windows)
            {
                fileWriter.WriteLine("@echo Hello World!> " + processStartFile);
                fileWriter.WriteLine("@echo " + cId + ">> " + processStartFile);
                fileWriter.WriteLine("@ping -n 100 127.0.0.1 >nul");
            }
            else
            {
                fileWriter.Write("\numask 0");
                // So that start file is readable by the test
                fileWriter.Write("\necho Hello World! > " + processStartFile);
                fileWriter.Write("\necho $$ >> " + processStartFile);
                fileWriter.Write("\nexec sleep 100");
            }
            fileWriter.Close();
            ContainerLaunchContext containerLaunchContext = recordFactory.NewRecordInstance <ContainerLaunchContext
                                                                                             >();
            URL resource_alpha = ConverterUtils.GetYarnUrlFromPath(localFS.MakeQualified(new
                                                                                         Path(scriptFile.GetAbsolutePath())));
            LocalResource rsrc_alpha = recordFactory.NewRecordInstance <LocalResource>();

            rsrc_alpha.SetResource(resource_alpha);
            rsrc_alpha.SetSize(-1);
            rsrc_alpha.SetVisibility(LocalResourceVisibility.Application);
            rsrc_alpha.SetType(LocalResourceType.File);
            rsrc_alpha.SetTimestamp(scriptFile.LastModified());
            string destinationFile = "dest_file";
            IDictionary <string, LocalResource> localResources = new Dictionary <string, LocalResource
                                                                                 >();

            localResources[destinationFile] = rsrc_alpha;
            containerLaunchContext.SetLocalResources(localResources);
            IList <string> commands = Arrays.AsList(Shell.GetRunScriptCommand(scriptFile));

            containerLaunchContext.SetCommands(commands);
            StartContainerRequest scRequest = StartContainerRequest.NewInstance(containerLaunchContext
                                                                                , CreateContainerToken(cId, DummyRmIdentifier, context.GetNodeId(), user, context
                                                                                                       .GetContainerTokenSecretManager()));
            IList <StartContainerRequest> list = new AList <StartContainerRequest>();

            list.AddItem(scRequest);
            StartContainersRequest allRequests = StartContainersRequest.NewInstance(list);

            containerManager.StartContainers(allRequests);
            int timeoutSecs = 0;

            while (!processStartFile.Exists() && timeoutSecs++ < 20)
            {
                Sharpen.Thread.Sleep(1000);
                Log.Info("Waiting for process start-file to be created");
            }
            NUnit.Framework.Assert.IsTrue("ProcessStartFile doesn't exist!", processStartFile
                                          .Exists());
            // Now verify the contents of the file
            BufferedReader reader = new BufferedReader(new FileReader(processStartFile));

            NUnit.Framework.Assert.AreEqual("Hello World!", reader.ReadLine());
            // Get the pid of the process
            string pid = reader.ReadLine().Trim();

            // No more lines
            NUnit.Framework.Assert.AreEqual(null, reader.ReadLine());
            // Now test the stop functionality.
            // Assert that the process is alive
            NUnit.Framework.Assert.IsTrue("Process is not alive!", DefaultContainerExecutor.ContainerIsAlive
                                              (pid));
            // Once more
            NUnit.Framework.Assert.IsTrue("Process is not alive!", DefaultContainerExecutor.ContainerIsAlive
                                              (pid));
            IList <ContainerId> containerIds = new AList <ContainerId>();

            containerIds.AddItem(cId);
            StopContainersRequest stopRequest = StopContainersRequest.NewInstance(containerIds
                                                                                  );

            containerManager.StopContainers(stopRequest);
            BaseContainerManagerTest.WaitForContainerState(containerManager, cId, ContainerState
                                                           .Complete);
            GetContainerStatusesRequest gcsRequest = GetContainerStatusesRequest.NewInstance(
                containerIds);
            ContainerStatus containerStatus = containerManager.GetContainerStatuses(gcsRequest
                                                                                    ).GetContainerStatuses()[0];
            int expectedExitCode = ContainerExitStatus.KilledByAppmaster;

            NUnit.Framework.Assert.AreEqual(expectedExitCode, containerStatus.GetExitStatus()
                                            );
            // Assert that the process is not alive anymore
            NUnit.Framework.Assert.IsFalse("Process is still alive!", DefaultContainerExecutor
                                           .ContainerIsAlive(pid));
        }