Beispiel #1
0
        public void BasicExecutionFlow()
        {
            TimeSpan            testTimeout  = TimeSpan.FromMinutes(1);
            MockRunspaceManager mockRunspace = new MockRunspaceManager();
            MockGeneratedModule module       = new MockGeneratedModule(mockRunspace);

            mockRunspace.ModuleMocks["test"] = module;
            TestBlockPipe blockPipe = new TestBlockPipe();

            blockPipe.Requests.Enqueue(new LiveTestRequest()
            {
                Id      = "0",
                JsonRpc = "2.0",
                Method  = "A.B_C",
                Params  = new System.Collections.Generic.Dictionary <string, object>()
                {
                    { "parm", 5.0 }
                }
            });

            LiveTestServerStartParams parms = new LiveTestServerStartParams()
            {
                Input           = blockPipe,
                Output          = blockPipe,
                RunspaceManager = mockRunspace,
                ModulePath      = "test"
            };
            LiveTestServer server = new LiveTestServer(parms);

            server.RunAsync().Wait();
            DateTime start = DateTime.Now;

            while ((DateTime.Now - start) < testTimeout && !module.ProcessRequestCalled)
            {
                // Wait for the request to be processed, if ever
                Thread.Sleep(1);
            }

            server.Stop();

            // Check the expected flow
            //      1. Get module info
            //      2. Load module
            //      3. Process request
            Assert.True(mockRunspace.GetModuleInfoCalled, "GetModuleInfo was never called.");
            Assert.True(module.LoadCalled, "Load was never called.");
            Assert.True(module.ProcessRequestCalled, "ProcessRequest was never called.");
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            ServerArgs serverArgs = new ServerArgs().Parse(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "config.json")).Parse(args).Validate();

            EventLogOutputPipe eventLogOutputPipe = new EventLogOutputPipe();
            CompositeLogger    logger             = new CompositeLogger();

            if (serverArgs.EnableEventLog)
            {
                logger.AddLogger(new Logger(eventLogOutputPipe, eventLogOutputPipe));
            }

            if (serverArgs.EnablePipeLog)
            {
                try
                {
                    NamedPipeServer namedPipe       = new NamedPipeServer(serverArgs.LogPipeName);
                    Logger          namedPipeLogger = new Logger(namedPipe, namedPipe);
                    logger.AddLogger(namedPipeLogger);
                }
                catch (Exception e)
                {
                    logger.LogError("Failed to initialize named pipe logger: " + e.Message);
                }
            }

            if (serverArgs.Errors.Count > 0)
            {
                logger.LogError("Server arguments had errors.");
                foreach (string error in serverArgs.Errors)
                {
                    logger.LogError(error);
                }
                return;
            }

            // Load external modules
            PowerShellRunspace runspace = new PowerShellRunspace(logger);

            foreach (string externalModule in serverArgs.ExternalModules)
            {
                GeneratedModule module = new GeneratedModule(runspace);
                module.ModulePath = externalModule;
                CommandExecutionResult result = module.Load();
                if (result != null && result.HadErrors)
                {
                    logger.LogError(String.Format(CultureInfo.CurrentCulture, "Failed to load extra module: {0}", externalModule));
                    result.LogErrors(logger);
                    return;
                }
            }

            // Start test server
            JsonRpcPipe    jsonRpcPipe = new JsonRpcPipe(new StandardInputPipe(), new StandardOutputPipe());
            LiveTestServer server      = new LiveTestServer(new LiveTestServerStartParams()
            {
                Logger             = logger,
                Input              = jsonRpcPipe,
                Output             = jsonRpcPipe,
                CredentialFactory  = new LiveTestCredentialFactory(),
                RunspaceManager    = runspace,
                ModulePath         = serverArgs.ModulePath,
                TracingManager     = new ServiceTracingManager(),
                SpecificationPaths = serverArgs.SpecificationPaths,
                ObjectTransforms   = serverArgs.GetTransforms()
            });

            try
            {
                server.RunAsync().Wait();
            } catch (Exception ex)
            {
                logger.LogError("Failed to start server: " + ex.ToString());
            }

            // Wait until server exits (usually means the server ran into an internal error)
            while (server.IsRunning)
            {
                Thread.Sleep(2);
            }
        }