Exemplo n.º 1
0
        public async Task MemoryOut()
        {
            using TempFile tmp = new TempFile();
            File.WriteAllText(tmp.File.FullName, C_3MB, Encoding.UTF8);
            using CLIExecutor cli = new CLIExecutor(new CLIExecutorSettings(Utils.GetShell(), new string[] { "-c", $"python {tmp.File.FullName}" })
            {
                MemoryLimit = 1024
            });
            ExecutorResult res = await cli.Run();

            Assert.AreEqual(ExecutorState.OutOfMemory, res.State);
        }
Exemplo n.º 2
0
        public async Task TimeOut()
        {
            using TempFile tmp = new TempFile();
            File.WriteAllText(tmp.File.FullName, C_DeadCycle, Encoding.UTF8);
            using CLIExecutor cli = new CLIExecutor(new CLIExecutorSettings(Utils.GetShell(), new string[] { "-c", $"python {tmp.File.FullName}" })
            {
                TimeLimit = TimeSpan.FromSeconds(0.2)
            });
            ExecutorResult res = await cli.Run();

            Assert.AreEqual(ExecutorState.OutOfTime, res.State);
            Assert.IsTrue(res.RunningTime.TotalSeconds >= 0.1);
        }
        public override async Task <PipelineBuilder <OperationWatcher, Wrapper <bool> > > Resolve(ResolveContext context)
        {
            Assert.IsNotNull(context);

            CommandLineOperationSettings settings = await GetSettings(context).ConfigureAwait(false);

            string shell            = string.IsNullOrEmpty(settings.Shell) ? context.GetShell() : settings.Shell;
            string workingDirectory = string.IsNullOrEmpty(settings.WorkingDirectory) ? context.GetWorkingDirectory() : settings.WorkingDirectory;
            PipelineBuilder <OperationWatcher, Wrapper <bool> > builder = new PipelineBuilder <OperationWatcher, Wrapper <bool> >();

            _ = builder.Configure("service", scope => scope.Add <CommandLineOperationSettings>(settings))
                .Use("init", context => Task.FromResult <Wrapper <bool> >(true));
            foreach (CommandLineTemplate item in settings.Scripts)
            {
                CLIExecutorSettings res = new CLIExecutorSettings(shell, new string[]
                {
                    "-c",
                    string.Join(' ', await item.Resolve(context).ConfigureAwait(false))
                })
                {
                    TimeLimit        = TimeSpan.FromSeconds(10),
                    WorkingDirectory = workingDirectory,
                    CollectError     = true,
                    CollectOutput    = true,
                };

                _ = builder.Use("script", async context =>
                {
                    context.Logs.Debug($"Execute {res.Arguments[1]}");
                    using CLIExecutor exe = new CLIExecutor(res);
                    ExecutorResult result = await exe.Run().ConfigureAwait(false);
                    if (result.ExitCode != 0)
                    {
                        context.IsStopped = true;
                    }

                    if (!string.IsNullOrEmpty(result.Output))
                    {
                        context.Logs.Information(result.Output);
                    }
                    if (!string.IsNullOrEmpty(result.Error))
                    {
                        context.Logs.Error(result.Error);
                    }

                    context.Logs.Debug($"Executed {res.Arguments[1]}");
                    return(result.ExitCode == 0);
                });
            }
            return(builder);
        }
Exemplo n.º 4
0
        public async Task Input()
        {
            using TempFile tmp = new TempFile();
            File.WriteAllText(tmp.File.FullName, C_Input, Encoding.UTF8);
            using CLIExecutor cli = new CLIExecutor(new CLIExecutorSettings(Utils.GetShell(), new string[] { "-c", $"python {tmp.File.FullName}" })
            {
                Input         = "hello",
                CollectOutput = true
            });
            ExecutorResult res = await cli.Run();

            Assert.AreEqual(ExecutorState.Ended, res.State);
            StringAssert.Contains(res.Output, "hello");
        }
Exemplo n.º 5
0
        public async Task Basic()
        {
            using (TempFile tmp = new TempFile())
            {
                File.WriteAllText(tmp.File.FullName, C_HelloWorld, Encoding.UTF8);
                using CLIExecutor cli = new CLIExecutor(new CLIExecutorSettings(Utils.GetShell(), new string[] { "-c", $"python {tmp.File.FullName}" })
                {
                    CollectOutput = true
                });
                ExecutorResult res = await cli.Run();

                Assert.AreEqual(0, res.ExitCode);
                StringAssert.Contains(res.Output, "Hello World!");
                _ = await Assert.ThrowsExceptionAsync <Exception>(async() => await cli.Run());
            }
            using (TempFile tmp = new TempFile())
            {
                File.WriteAllText(tmp.File.FullName, C_Exit1, Encoding.UTF8);
                using CLIExecutor cli = new CLIExecutor(new CLIExecutorSettings(Utils.GetShell(), new string[] { "-c", $"python {tmp.File.FullName}" }));
                ExecutorResult res = await cli.Run();

                Assert.AreEqual(1, res.ExitCode);
            }
        }
Exemplo n.º 6
0
        public async Task Error()
        {
            using TempFile tmp = new TempFile();
            File.WriteAllText(tmp.File.FullName, C_DeadCycle, Encoding.UTF8);
            using CLIExecutor cli = new CLIExecutor(new CLIExecutorSettings(Utils.GetShell(), new string[] { "-c", $"python {tmp.File.FullName}" })
            {
                TimeLimit = TimeSpan.FromSeconds(0.5)
            });
            Task <ExecutorResult> task = cli.Run();
            await cli.Kill();

            ExecutorResult res = await task;

            Assert.AreEqual(ExecutorState.Ended, res.State);
            Assert.AreNotEqual(0, res.ExitCode);
        }