예제 #1
0
        /// <exception cref="System.IO.IOException"/>
        private void SetupTokens(ContainerLaunchContext container, ContainerId containerID
                                 )
        {
            IDictionary <string, string> environment = container.GetEnvironment();

            environment[ApplicationConstants.ApplicationWebProxyBaseEnv] = application.GetWebProxyBase
                                                                               ();
            // Set AppSubmitTime and MaxAppAttempts to be consumable by the AM.
            ApplicationId applicationId = application.GetAppAttemptId().GetApplicationId();

            environment[ApplicationConstants.AppSubmitTimeEnv] = rmContext.GetRMApps()[applicationId
                                                                 ].GetSubmitTime().ToString();
            environment[ApplicationConstants.MaxAppAttemptsEnv] = rmContext.GetRMApps()[applicationId
                                                                  ].GetMaxAppAttempts().ToString();
            Credentials         credentials = new Credentials();
            DataInputByteBuffer dibb        = new DataInputByteBuffer();

            if (container.GetTokens() != null)
            {
                // TODO: Don't do this kind of checks everywhere.
                dibb.Reset(container.GetTokens());
                credentials.ReadTokenStorageStream(dibb);
            }
            // Add AMRMToken
            Org.Apache.Hadoop.Security.Token.Token <AMRMTokenIdentifier> amrmToken = CreateAndSetAMRMToken
                                                                                         ();
            if (amrmToken != null)
            {
                credentials.AddToken(amrmToken.GetService(), amrmToken);
            }
            DataOutputBuffer dob = new DataOutputBuffer();

            credentials.WriteTokenStorageToStream(dob);
            container.SetTokens(ByteBuffer.Wrap(dob.GetData(), 0, dob.GetLength()));
        }
예제 #2
0
        public virtual void TestAMStandardEnv()
        {
            string  AdminLibPath = "foo";
            string  UserLibPath  = "bar";
            string  UserShell    = "shell";
            JobConf jobConf      = new JobConf();

            jobConf.Set(MRJobConfig.MrAmAdminUserEnv, "LD_LIBRARY_PATH=" + AdminLibPath);
            jobConf.Set(MRJobConfig.MrAmEnv, "LD_LIBRARY_PATH=" + UserLibPath);
            jobConf.Set(MRJobConfig.MapredAdminUserShell, UserShell);
            YARNRunner yarnRunner = new YARNRunner(jobConf);
            ApplicationSubmissionContext appSubCtx = BuildSubmitContext(yarnRunner, jobConf);
            // make sure PWD is first in the lib path
            ContainerLaunchContext       clc = appSubCtx.GetAMContainerSpec();
            IDictionary <string, string> env = clc.GetEnvironment();
            string libPath = env[ApplicationConstants.Environment.LdLibraryPath.ToString()];

            NUnit.Framework.Assert.IsNotNull("LD_LIBRARY_PATH not set", libPath);
            string cps = jobConf.GetBoolean(MRConfig.MapreduceAppSubmissionCrossPlatform, MRConfig
                                            .DefaultMapreduceAppSubmissionCrossPlatform) ? ApplicationConstants.ClassPathSeparator
                                 : FilePath.pathSeparator;

            NUnit.Framework.Assert.AreEqual("Bad AM LD_LIBRARY_PATH setting", MRApps.CrossPlatformifyMREnv
                                                (conf, ApplicationConstants.Environment.Pwd) + cps + AdminLibPath + cps + UserLibPath
                                            , libPath);
            // make sure SHELL is set
            string shell = env[ApplicationConstants.Environment.Shell.ToString()];

            NUnit.Framework.Assert.IsNotNull("SHELL not set", shell);
            NUnit.Framework.Assert.AreEqual("Bad SHELL setting", UserShell, shell);
        }
        /// <exception cref="System.IO.IOException"/>
        private int RunAndBlock(ContainerId cId, IDictionary <string, string> launchCtxEnv
                                , params string[] cmd)
        {
            string appId = "APP_" + Runtime.CurrentTimeMillis();

            Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container container
                = Org.Mockito.Mockito.Mock <Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                                            >();
            ContainerLaunchContext context = Org.Mockito.Mockito.Mock <ContainerLaunchContext>
                                                 ();

            Org.Mockito.Mockito.When(container.GetContainerId()).ThenReturn(cId);
            Org.Mockito.Mockito.When(container.GetLaunchContext()).ThenReturn(context);
            Org.Mockito.Mockito.When(cId.GetApplicationAttemptId().GetApplicationId().ToString
                                         ()).ThenReturn(appId);
            Org.Mockito.Mockito.When(context.GetEnvironment()).ThenReturn(launchCtxEnv);
            string script     = WriteScriptFile(launchCtxEnv, cmd);
            Path   scriptPath = new Path(script);
            Path   tokensPath = new Path("/dev/null");
            Path   workDir    = new Path(workSpace.GetAbsolutePath());
            Path   pidFile    = new Path(workDir, "pid.txt");

            exec.ActivateContainer(cId, pidFile);
            return(exec.LaunchContainer(container, scriptPath, tokensPath, appSubmitter, appId
                                        , workDir, dirsHandler.GetLocalDirs(), dirsHandler.GetLogDirs()));
        }
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestContainerLaunchNullImage()
        {
            string appSubmitter = "nobody";
            string appId        = "APP_ID";
            string containerId  = "CONTAINER_ID";
            string testImage    = string.Empty;

            Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container container
                = Org.Mockito.Mockito.Mock <Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                                            >(Org.Mockito.Mockito.ReturnsDeepStubs);
            ContainerId cId = Org.Mockito.Mockito.Mock <ContainerId>(Org.Mockito.Mockito.ReturnsDeepStubs
                                                                     );
            ContainerLaunchContext context = Org.Mockito.Mockito.Mock <ContainerLaunchContext>
                                                 ();
            Dictionary <string, string> env = new Dictionary <string, string>();

            Org.Mockito.Mockito.When(container.GetContainerId()).ThenReturn(cId);
            Org.Mockito.Mockito.When(container.GetLaunchContext()).ThenReturn(context);
            Org.Mockito.Mockito.When(cId.GetApplicationAttemptId().GetApplicationId().ToString
                                         ()).ThenReturn(appId);
            Org.Mockito.Mockito.When(cId.ToString()).ThenReturn(containerId);
            Org.Mockito.Mockito.When(context.GetEnvironment()).ThenReturn(env);
            env[YarnConfiguration.NmDockerContainerExecutorImageName] = testImage;
            dockerContainerExecutor.GetConf().Set(YarnConfiguration.NmDockerContainerExecutorImageName
                                                  , testImage);
            Path scriptPath = new Path("file:///bin/echo");
            Path tokensPath = new Path("file:///dev/null");
            Path pidFile    = new Path(workDir, "pid.txt");

            dockerContainerExecutor.ActivateContainer(cId, pidFile);
            dockerContainerExecutor.LaunchContainer(container, scriptPath, tokensPath, appSubmitter
                                                    , appId, workDir, dirsHandler.GetLocalDirs(), dirsHandler.GetLogDirs());
        }
        public virtual void TestContainerLaunch()
        {
            string appSubmitter = "nobody";
            string cmd          = LinuxContainerExecutor.Commands.LaunchContainer.GetValue().ToString(
                );
            string appId       = "APP_ID";
            string containerId = "CONTAINER_ID";

            Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container container
                = Org.Mockito.Mockito.Mock <Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                                            >();
            ContainerId            cId     = Org.Mockito.Mockito.Mock <ContainerId>();
            ContainerLaunchContext context = Org.Mockito.Mockito.Mock <ContainerLaunchContext>
                                                 ();
            Dictionary <string, string> env = new Dictionary <string, string>();

            Org.Mockito.Mockito.When(container.GetContainerId()).ThenReturn(cId);
            Org.Mockito.Mockito.When(container.GetLaunchContext()).ThenReturn(context);
            Org.Mockito.Mockito.When(cId.ToString()).ThenReturn(containerId);
            Org.Mockito.Mockito.When(context.GetEnvironment()).ThenReturn(env);
            Path scriptPath = new Path("file:///bin/echo");
            Path tokensPath = new Path("file:///dev/null");
            Path workDir    = new Path("/tmp");
            Path pidFile    = new Path(workDir, "pid.txt");

            mockExec.ActivateContainer(cId, pidFile);
            int ret = mockExec.LaunchContainer(container, scriptPath, tokensPath, appSubmitter
                                               , appId, workDir, dirsHandler.GetLocalDirs(), dirsHandler.GetLogDirs());

            NUnit.Framework.Assert.AreEqual(0, ret);
            NUnit.Framework.Assert.AreEqual(Arrays.AsList(YarnConfiguration.DefaultNmNonsecureModeLocalUser
                                                          , appSubmitter, cmd, appId, containerId, workDir.ToString(), "/bin/echo", "/dev/null"
                                                          , pidFile.ToString(), StringUtils.Join(",", dirsHandler.GetLocalDirs()), StringUtils
                                                          .Join(",", dirsHandler.GetLogDirs()), "cgroups=none"), ReadMockParams());
        }
        public virtual void TestContainerLaunchError()
        {
            // reinitialize executer
            FilePath f = new FilePath("./src/test/resources/mock-container-executer-with-error"
                                      );

            if (!FileUtil.CanExecute(f))
            {
                FileUtil.SetExecutable(f, true);
            }
            string        executorPath = f.GetAbsolutePath();
            Configuration conf         = new Configuration();

            conf.Set(YarnConfiguration.NmLinuxContainerExecutorPath, executorPath);
            conf.Set(YarnConfiguration.NmLocalDirs, "file:///bin/echo");
            conf.Set(YarnConfiguration.NmLogDirs, "file:///dev/null");
            mockExec = Org.Mockito.Mockito.Spy(new LinuxContainerExecutor());
            Org.Mockito.Mockito.DoAnswer(new _Answer_226()).When(mockExec).LogOutput(Matchers.Any
                                                                                     <string>());
            dirsHandler = new LocalDirsHandlerService();
            dirsHandler.Init(conf);
            mockExec.SetConf(conf);
            string appSubmitter = "nobody";
            string cmd          = LinuxContainerExecutor.Commands.LaunchContainer.GetValue().ToString(
                );
            string appId       = "APP_ID";
            string containerId = "CONTAINER_ID";

            Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container container
                = Org.Mockito.Mockito.Mock <Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                                            >();
            ContainerId            cId     = Org.Mockito.Mockito.Mock <ContainerId>();
            ContainerLaunchContext context = Org.Mockito.Mockito.Mock <ContainerLaunchContext>
                                                 ();
            Dictionary <string, string> env = new Dictionary <string, string>();

            Org.Mockito.Mockito.When(container.GetContainerId()).ThenReturn(cId);
            Org.Mockito.Mockito.When(container.GetLaunchContext()).ThenReturn(context);
            Org.Mockito.Mockito.DoAnswer(new _Answer_254()).When(container).Handle(Matchers.Any
                                                                                   <ContainerDiagnosticsUpdateEvent>());
            Org.Mockito.Mockito.When(cId.ToString()).ThenReturn(containerId);
            Org.Mockito.Mockito.When(context.GetEnvironment()).ThenReturn(env);
            Path scriptPath = new Path("file:///bin/echo");
            Path tokensPath = new Path("file:///dev/null");
            Path workDir    = new Path("/tmp");
            Path pidFile    = new Path(workDir, "pid.txt");

            mockExec.ActivateContainer(cId, pidFile);
            int ret = mockExec.LaunchContainer(container, scriptPath, tokensPath, appSubmitter
                                               , appId, workDir, dirsHandler.GetLocalDirs(), dirsHandler.GetLogDirs());

            NUnit.Framework.Assert.AreNotSame(0, ret);
            NUnit.Framework.Assert.AreEqual(Arrays.AsList(YarnConfiguration.DefaultNmNonsecureModeLocalUser
                                                          , appSubmitter, cmd, appId, containerId, workDir.ToString(), "/bin/echo", "/dev/null"
                                                          , pidFile.ToString(), StringUtils.Join(",", dirsHandler.GetLocalDirs()), StringUtils
                                                          .Join(",", dirsHandler.GetLogDirs()), "cgroups=none"), ReadMockParams());
        }
예제 #7
0
 public override void Handle(ContainerLauncherEvent @event)
 {
     if (@event.GetType() == ContainerLauncher.EventType.ContainerRemoteLaunch)
     {
         ContainerRemoteLaunchEvent launchEvent   = (ContainerRemoteLaunchEvent)@event;
         ContainerLaunchContext     launchContext = launchEvent.GetContainerLaunchContext();
         string cmdString = launchContext.GetCommands().ToString();
         TestMapReduceChildJVM.Log.Info("launchContext " + cmdString);
         this._enclosing.myCommandLine  = cmdString;
         this._enclosing.cmdEnvironment = launchContext.GetEnvironment();
     }
     base.Handle(@event);
 }
예제 #8
0
        public virtual int Call()
        {
            // dispatcher not typed
            ContainerLaunchContext launchContext = container.GetLaunchContext();
            IDictionary <Path, IList <string> > localResources = null;
            ContainerId    containerID    = container.GetContainerId();
            string         containerIdStr = ConverterUtils.ToString(containerID);
            IList <string> command        = launchContext.GetCommands();
            int            ret            = -1;

            // CONTAINER_KILLED_ON_REQUEST should not be missed if the container
            // is already at KILLING
            if (container.GetContainerState() == ContainerState.Killing)
            {
                dispatcher.GetEventHandler().Handle(new ContainerExitEvent(containerID, ContainerEventType
                                                                           .ContainerKilledOnRequest, Shell.Windows ? ContainerExecutor.ExitCode.ForceKilled
                                                                           .GetExitCode() : ContainerExecutor.ExitCode.Terminated.GetExitCode(), "Container terminated before launch."
                                                                           ));
                return(0);
            }
            try
            {
                localResources = container.GetLocalizedResources();
                if (localResources == null)
                {
                    throw RPCUtil.GetRemoteException("Unable to get local resources when Container "
                                                     + containerID + " is at " + container.GetContainerState());
                }
                string user = container.GetUser();
                // /////////////////////////// Variable expansion
                // Before the container script gets written out.
                IList <string> newCmds  = new AList <string>(command.Count);
                string         appIdStr = app.GetAppId().ToString();
                string         relativeContainerLogDir = Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Launcher.ContainerLaunch
                                                         .GetRelativeContainerLogDir(appIdStr, containerIdStr);
                Path containerLogDir = dirsHandler.GetLogPathForWrite(relativeContainerLogDir, false
                                                                      );
                foreach (string str in command)
                {
                    // TODO: Should we instead work via symlinks without this grammar?
                    newCmds.AddItem(ExpandEnvironment(str, containerLogDir));
                }
                launchContext.SetCommands(newCmds);
                IDictionary <string, string> environment = launchContext.GetEnvironment();
                // Make a copy of env to iterate & do variable expansion
                foreach (KeyValuePair <string, string> entry in environment)
                {
                    string value = entry.Value;
                    value = ExpandEnvironment(value, containerLogDir);
                    entry.SetValue(value);
                }
                // /////////////////////////// End of variable expansion
                FileContext lfs = FileContext.GetLocalFSFileContext();
                Path        nmPrivateContainerScriptPath = dirsHandler.GetLocalPathForWrite(GetContainerPrivateDir
                                                                                                (appIdStr, containerIdStr) + Path.Separator + ContainerScript);
                Path nmPrivateTokensPath = dirsHandler.GetLocalPathForWrite(GetContainerPrivateDir
                                                                                (appIdStr, containerIdStr) + Path.Separator + string.Format(ContainerLocalizer.TokenFileNameFmt
                                                                                                                                            , containerIdStr));
                Path nmPrivateClasspathJarDir = dirsHandler.GetLocalPathForWrite(GetContainerPrivateDir
                                                                                     (appIdStr, containerIdStr));
                DataOutputStream containerScriptOutStream = null;
                DataOutputStream tokensOutStream          = null;
                // Select the working directory for the container
                Path containerWorkDir = dirsHandler.GetLocalPathForWrite(ContainerLocalizer.Usercache
                                                                         + Path.Separator + user + Path.Separator + ContainerLocalizer.Appcache + Path.Separator
                                                                         + appIdStr + Path.Separator + containerIdStr, LocalDirAllocator.SizeUnknown, false
                                                                         );
                string pidFileSubpath = GetPidFileSubpath(appIdStr, containerIdStr);
                // pid file should be in nm private dir so that it is not
                // accessible by users
                pidFilePath = dirsHandler.GetLocalPathForWrite(pidFileSubpath);
                IList <string> localDirs        = dirsHandler.GetLocalDirs();
                IList <string> logDirs          = dirsHandler.GetLogDirs();
                IList <string> containerLogDirs = new AList <string>();
                foreach (string logDir in logDirs)
                {
                    containerLogDirs.AddItem(logDir + Path.Separator + relativeContainerLogDir);
                }
                if (!dirsHandler.AreDisksHealthy())
                {
                    ret = ContainerExitStatus.DisksFailed;
                    throw new IOException("Most of the disks failed. " + dirsHandler.GetDisksHealthReport
                                              (false));
                }
                try
                {
                    // /////////// Write out the container-script in the nmPrivate space.
                    IList <Path> appDirs = new AList <Path>(localDirs.Count);
                    foreach (string localDir in localDirs)
                    {
                        Path usersdir = new Path(localDir, ContainerLocalizer.Usercache);
                        Path userdir  = new Path(usersdir, user);
                        Path appsdir  = new Path(userdir, ContainerLocalizer.Appcache);
                        appDirs.AddItem(new Path(appsdir, appIdStr));
                    }
                    containerScriptOutStream = lfs.Create(nmPrivateContainerScriptPath, EnumSet.Of(CreateFlag
                                                                                                   .Create, CreateFlag.Overwrite));
                    // Set the token location too.
                    environment[ApplicationConstants.ContainerTokenFileEnvName] = new Path(containerWorkDir
                                                                                           , FinalContainerTokensFile).ToUri().GetPath();
                    // Sanitize the container's environment
                    SanitizeEnv(environment, containerWorkDir, appDirs, containerLogDirs, localResources
                                , nmPrivateClasspathJarDir);
                    // Write out the environment
                    exec.WriteLaunchEnv(containerScriptOutStream, environment, localResources, launchContext
                                        .GetCommands());
                    // /////////// End of writing out container-script
                    // /////////// Write out the container-tokens in the nmPrivate space.
                    tokensOutStream = lfs.Create(nmPrivateTokensPath, EnumSet.Of(CreateFlag.Create, CreateFlag
                                                                                 .Overwrite));
                    Credentials creds = container.GetCredentials();
                    creds.WriteTokenStorageToStream(tokensOutStream);
                }
                finally
                {
                    // /////////// End of writing out container-tokens
                    IOUtils.Cleanup(Log, containerScriptOutStream, tokensOutStream);
                }
                // LaunchContainer is a blocking call. We are here almost means the
                // container is launched, so send out the event.
                dispatcher.GetEventHandler().Handle(new ContainerEvent(containerID, ContainerEventType
                                                                       .ContainerLaunched));
                context.GetNMStateStore().StoreContainerLaunched(containerID);
                // Check if the container is signalled to be killed.
                if (!shouldLaunchContainer.CompareAndSet(false, true))
                {
                    Log.Info("Container " + containerIdStr + " not launched as " + "cleanup already called"
                             );
                    ret = ContainerExecutor.ExitCode.Terminated.GetExitCode();
                }
                else
                {
                    exec.ActivateContainer(containerID, pidFilePath);
                    ret = exec.LaunchContainer(container, nmPrivateContainerScriptPath, nmPrivateTokensPath
                                               , user, appIdStr, containerWorkDir, localDirs, logDirs);
                }
            }
            catch (Exception e)
            {
                Log.Warn("Failed to launch container.", e);
                dispatcher.GetEventHandler().Handle(new ContainerExitEvent(containerID, ContainerEventType
                                                                           .ContainerExitedWithFailure, ret, e.Message));
                return(ret);
            }
            finally
            {
                completed.Set(true);
                exec.DeactivateContainer(containerID);
                try
                {
                    context.GetNMStateStore().StoreContainerCompleted(containerID, ret);
                }
                catch (IOException)
                {
                    Log.Error("Unable to set exit code for container " + containerID);
                }
            }
            if (Log.IsDebugEnabled())
            {
                Log.Debug("Container " + containerIdStr + " completed with exit code " + ret);
            }
            if (ret == ContainerExecutor.ExitCode.ForceKilled.GetExitCode() || ret == ContainerExecutor.ExitCode
                .Terminated.GetExitCode())
            {
                // If the process was killed, Send container_cleanedup_after_kill and
                // just break out of this method.
                dispatcher.GetEventHandler().Handle(new ContainerExitEvent(containerID, ContainerEventType
                                                                           .ContainerKilledOnRequest, ret, "Container exited with a non-zero exit code " +
                                                                           ret));
                return(ret);
            }
            if (ret != 0)
            {
                Log.Warn("Container exited with a non-zero exit code " + ret);
                this.dispatcher.GetEventHandler().Handle(new ContainerExitEvent(containerID, ContainerEventType
                                                                                .ContainerExitedWithFailure, ret, "Container exited with a non-zero exit code "
                                                                                + ret));
                return(ret);
            }
            Log.Info("Container " + containerIdStr + " succeeded ");
            dispatcher.GetEventHandler().Handle(new ContainerEvent(containerID, ContainerEventType
                                                                   .ContainerExitedWithSuccess));
            return(0);
        }
        public virtual void TestContainerLaunch()
        {
            string appSubmitter = "nobody";
            string appId        = "APP_ID";
            string containerId  = "CONTAINER_ID";
            string testImage    = "\"sequenceiq/hadoop-docker:2.4.1\"";

            Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container container
                = Org.Mockito.Mockito.Mock <Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                                            >(Org.Mockito.Mockito.ReturnsDeepStubs);
            ContainerId cId = Org.Mockito.Mockito.Mock <ContainerId>(Org.Mockito.Mockito.ReturnsDeepStubs
                                                                     );
            ContainerLaunchContext context = Org.Mockito.Mockito.Mock <ContainerLaunchContext>
                                                 ();
            Dictionary <string, string> env = new Dictionary <string, string>();

            Org.Mockito.Mockito.When(container.GetContainerId()).ThenReturn(cId);
            Org.Mockito.Mockito.When(container.GetLaunchContext()).ThenReturn(context);
            Org.Mockito.Mockito.When(cId.GetApplicationAttemptId().GetApplicationId().ToString
                                         ()).ThenReturn(appId);
            Org.Mockito.Mockito.When(cId.ToString()).ThenReturn(containerId);
            Org.Mockito.Mockito.When(context.GetEnvironment()).ThenReturn(env);
            env[YarnConfiguration.NmDockerContainerExecutorImageName] = testImage;
            Path scriptPath = new Path("file:///bin/echo");
            Path tokensPath = new Path("file:///dev/null");
            Path pidFile    = new Path(workDir, "pid");

            dockerContainerExecutor.ActivateContainer(cId, pidFile);
            int ret = dockerContainerExecutor.LaunchContainer(container, scriptPath, tokensPath
                                                              , appSubmitter, appId, workDir, dirsHandler.GetLocalDirs(), dirsHandler.GetLogDirs
                                                                  ());

            NUnit.Framework.Assert.AreEqual(0, ret);
            //get the script
            Path sessionScriptPath = new Path(workDir, Shell.AppendScriptExtension(DockerContainerExecutor
                                                                                   .DockerContainerExecutorSessionScript));
            LineNumberReader lnr = new LineNumberReader(new FileReader(sessionScriptPath.ToString
                                                                           ()));
            bool           cmdFound     = false;
            IList <string> localDirs    = DirsToMount(dirsHandler.GetLocalDirs());
            IList <string> logDirs      = DirsToMount(dirsHandler.GetLogDirs());
            IList <string> workDirMount = DirsToMount(Sharpen.Collections.SingletonList(workDir
                                                                                        .ToUri().GetPath()));
            IList <string> expectedCommands = new AList <string>(Arrays.AsList(DockerLaunchCommand
                                                                               , "run", "--rm", "--net=host", "--name", containerId));

            Sharpen.Collections.AddAll(expectedCommands, localDirs);
            Sharpen.Collections.AddAll(expectedCommands, logDirs);
            Sharpen.Collections.AddAll(expectedCommands, workDirMount);
            string shellScript = workDir + "/launch_container.sh";

            Sharpen.Collections.AddAll(expectedCommands, Arrays.AsList(testImage.ReplaceAll("['\"]"
                                                                                            , string.Empty), "bash", "\"" + shellScript + "\""));
            string expectedPidString = "echo `/bin/true inspect --format {{.State.Pid}} " + containerId
                                       + "` > " + pidFile.ToString() + ".tmp";
            bool pidSetterFound = false;

            while (lnr.Ready())
            {
                string line = lnr.ReadLine();
                Log.Debug("line: " + line);
                if (line.StartsWith(DockerLaunchCommand))
                {
                    IList <string> command = new AList <string>();
                    foreach (string s in line.Split("\\s+"))
                    {
                        command.AddItem(s.Trim());
                    }
                    NUnit.Framework.Assert.AreEqual(expectedCommands, command);
                    cmdFound = true;
                }
                else
                {
                    if (line.StartsWith("echo"))
                    {
                        NUnit.Framework.Assert.AreEqual(expectedPidString, line);
                        pidSetterFound = true;
                    }
                }
            }
            NUnit.Framework.Assert.IsTrue(cmdFound);
            NUnit.Framework.Assert.IsTrue(pidSetterFound);
        }
예제 #10
0
        public virtual void TestContainerLaunchError()
        {
            if (Shell.Windows)
            {
                BaseTmpPath = new Path(new FilePath("target").GetAbsolutePath(), typeof(TestDefaultContainerExecutor
                                                                                        ).Name);
            }
            Path           localDir  = new Path(BaseTmpPath, "localDir");
            IList <string> localDirs = new AList <string>();

            localDirs.AddItem(localDir.ToString());
            IList <string> logDirs = new AList <string>();
            Path           logDir  = new Path(BaseTmpPath, "logDir");

            logDirs.AddItem(logDir.ToString());
            Configuration conf = new Configuration();

            conf.Set(CommonConfigurationKeys.FsPermissionsUmaskKey, "077");
            conf.Set(YarnConfiguration.NmLocalDirs, localDir.ToString());
            conf.Set(YarnConfiguration.NmLogDirs, logDir.ToString());
            FileContext lfs = FileContext.GetLocalFSFileContext(conf);
            DefaultContainerExecutor mockExec = Org.Mockito.Mockito.Spy(new DefaultContainerExecutor
                                                                            (lfs));

            mockExec.SetConf(conf);
            Org.Mockito.Mockito.DoAnswer(new _Answer_245()).When(mockExec).LogOutput(Matchers.Any
                                                                                     <string>());
            string appSubmitter = "nobody";
            string appId        = "APP_ID";
            string containerId  = "CONTAINER_ID";

            Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container container
                = Org.Mockito.Mockito.Mock <Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                                            >();
            ContainerId            cId     = Org.Mockito.Mockito.Mock <ContainerId>();
            ContainerLaunchContext context = Org.Mockito.Mockito.Mock <ContainerLaunchContext>
                                                 ();
            Dictionary <string, string> env = new Dictionary <string, string>();

            Org.Mockito.Mockito.When(container.GetContainerId()).ThenReturn(cId);
            Org.Mockito.Mockito.When(container.GetLaunchContext()).ThenReturn(context);
            try
            {
                Org.Mockito.Mockito.DoAnswer(new _Answer_268()).When(container).Handle(Matchers.Any
                                                                                       <ContainerDiagnosticsUpdateEvent>());
                Org.Mockito.Mockito.When(cId.ToString()).ThenReturn(containerId);
                Org.Mockito.Mockito.When(cId.GetApplicationAttemptId()).ThenReturn(ApplicationAttemptId
                                                                                   .NewInstance(ApplicationId.NewInstance(0, 1), 0));
                Org.Mockito.Mockito.When(context.GetEnvironment()).ThenReturn(env);
                mockExec.CreateUserLocalDirs(localDirs, appSubmitter);
                mockExec.CreateUserCacheDirs(localDirs, appSubmitter);
                mockExec.CreateAppDirs(localDirs, appSubmitter, appId);
                mockExec.CreateAppLogDirs(appId, logDirs, appSubmitter);
                Path scriptPath = new Path("file:///bin/echo");
                Path tokensPath = new Path("file:///dev/null");
                if (Shell.Windows)
                {
                    FilePath       tmp    = new FilePath(BaseTmpPath.ToString(), "test_echo.cmd");
                    BufferedWriter output = new BufferedWriter(new FileWriter(tmp));
                    output.Write("Exit 1");
                    output.Write("Echo No such file or directory 1>&2");
                    output.Close();
                    scriptPath = new Path(tmp.GetAbsolutePath());
                    tmp        = new FilePath(BaseTmpPath.ToString(), "tokens");
                    tmp.CreateNewFile();
                    tokensPath = new Path(tmp.GetAbsolutePath());
                }
                Path workDir = localDir;
                Path pidFile = new Path(workDir, "pid.txt");
                mockExec.Init();
                mockExec.ActivateContainer(cId, pidFile);
                int ret = mockExec.LaunchContainer(container, scriptPath, tokensPath, appSubmitter
                                                   , appId, workDir, localDirs, localDirs);
                NUnit.Framework.Assert.AreNotSame(0, ret);
            }
            finally
            {
                mockExec.DeleteAsUser(appSubmitter, localDir);
                mockExec.DeleteAsUser(appSubmitter, logDir);
            }
        }