Exemple #1
0
        protected override bool InitializeServer(RunFrame frame)
        {
            var sandbox = CreateFullTrustAppDomain();

            frame.SetState(KeySandbox, sandbox);

            // load test assemly into sandbox
            if (sandbox != null && TypeDescriptor.TestAssembly != null)
            {
                sandbox.Load(TypeDescriptor.TestAssembly.GetName());
            }

            SecurityHelper.AddIpListen();

            // retry three times using port scan with three port types.
            int repeat = 3;

            bool result = false;

            for (int i = 1; i <= repeat; ++i)
            {
                if (TrySetupServer(i, sandbox, frame))
                {
                    result = true;
                    break;
                }
            }

            if (result == false)
            {
                throw new Exception(string.Format("Cannot setup host server. See Event log for details"));
            }

            return(true);
        }
        /// <summary>
        /// Initialize the fixture.
        /// </summary>
        private void Initialize()
        {
            SecurityHelper.AddIpListen();

            // Be noted:
            // We use the convention as follows
            // 1) if you want to configure the service, add "protected static void UpdateConfigureServices(IServiceCollection)" method into your test class.
            // 2) if you want to configure the routing, add "protected static void updateConfigure(EndpointRouteConfiguration)" method into your test class.
            Type       testType = typeof(T);
            MethodInfo configureServicesMethod = testType.GetMethod("UpdateConfigureServices", BindingFlags.NonPublic | BindingFlags.Static);
            MethodInfo configureMethod         = testType.GetMethod("UpdateConfigure", BindingFlags.NonPublic | BindingFlags.Static);
            // Owing that this is used in Test only, I assume every developer can following the convention.
            // So I skip the method parameter checking.

            string serverName = "localhost";

            // setup base address
            int port = PortArranger.Reserve();

            this.BaseAddress = string.Format(NormalBaseAddressTemplate, serverName, port.ToString());

            _selfHostServer = Host.CreateDefaultBuilder()
                              .ConfigureWebHostDefaults(webBuilder => webBuilder
                                                        .UseKestrel(options => options.Listen(IPAddress.Loopback, port))
                                                        .ConfigureServices(services =>
            {
                services.AddHttpClient();         // Add IHttpClientFactory
                services.AddOData();
                services.AddRouting();

                // Apply custom services for each test class
                configureServicesMethod?.Invoke(null, new object[] { services });
            })
                                                        .Configure(app =>
            {
                this.ClientFactory = app.ApplicationServices.GetRequiredService <IHttpClientFactory>();

                // should add ODataBatch middleware before the routing middelware
                app.UseODataBatching();
                app.UseRouting();
                app.UseEndpoints(endpoints =>
                {
                    // Apply test configuration.
                    EndpointRouteConfiguration config = new EndpointRouteConfiguration(endpoints);
                    configureMethod?.Invoke(null, new object[] { config });
                });
            })
                                                        .ConfigureLogging((hostingContext, logging) =>
            {
                logging.AddDebug();
                logging.SetMinimumLevel(LogLevel.Warning);
            }
                                                                          )).Build();

            _selfHostServer.Start();
        }
Exemple #3
0
        private bool TrySetupServer(int tryIndex, AppDomain sandbox, RunFrame frame)
        {
            string baseAddress;
            string port = _portArranger.Reserve();

            SecurityHelper.AddIpListen();
            SecurityOptionElement securityElem = frame.GetFirstElement <SecurityOptionElement>();

            if (securityElem != null)
            {
                SetupSecureEnvironment(securityElem.Certificate, port);
                baseAddress = string.Format(SecureBaseAddressTemplate, Environment.MachineName, port);
                frame.SetState(KeyIsSecuredServer, true);
            }
            else
            {
                baseAddress = string.Format(NormalBaseAddressTemplate, Environment.MachineName, port);
                frame.SetState(KeyIsSecuredServer, false);
            }

            // looking into the RunFrames and search for TraceElement. if it exists
            // set the tracer's type to the configuration otherwise skip this step
            TraceElement traceElem = frame.GetFirstElement <TraceElement>();
            Type         traceType = null;

            if (traceElem != null)
            {
                traceType = traceElem.TracerType;
            }

            KatanaSelfHostServerInitiator serverInitiator;

            // create initiator in the sandbox
            if (sandbox != null)
            {
                serverInitiator = sandbox.CreateInstanceAndUnwrap(
                    typeof(KatanaSelfHostServerInitiator).Assembly.FullName,
                    typeof(KatanaSelfHostServerInitiator).FullName)
                                  as KatanaSelfHostServerInitiator;
            }
            else
            {
                serverInitiator = new KatanaSelfHostServerInitiator();
            }
            try
            {
                // set up the server
                serverInitiator.Setup(
                    baseAddress,
                    TypeDescriptor.GetDesignatedMethod <NuwaKatanaConfigurationAttribute>(),
                    TypeDescriptor.ConfigureMethod,
                    traceType, GetDefaultRouteTemplate());
            }
            catch (Exception ex)
            {
                EventLog appLog = new System.Diagnostics.EventLog();
                appLog.Source = "Nuwa Katana Self Host Test";
                appLog.WriteEntry(string.Format("try index: {0}\nbase address: {1}\n message: {2}\n stack trace: {3}\n", tryIndex, baseAddress, ex.Message, ex.StackTrace),
                                  EventLogEntryType.Error);

                return(false);
            }

            frame.SetState(KeyReservedPort, port);
            frame.SetState(KeyBaseAddresss, baseAddress);
            frame.SetState(KeyServerInitiator, serverInitiator);
            return(true);
        }
        protected override bool InitializeServer(RunFrame frame)
        {
            string baseAddress;
            string port;

            var sandbox = CreateFullTrustAppDomain();

            frame.SetState(KeySandbox, sandbox);

            // load test assemly into sandbox
            if (sandbox != null && TypeDescriptor.TestAssembly != null)
            {
                sandbox.Load(TypeDescriptor.TestAssembly.GetName());
            }

            // setup security strategy and base address
            port = _portArranger.Reserve();
            SecurityHelper.AddIpListen();
            SecurityOptionElement securityElem = frame.GetFirstElement <SecurityOptionElement>();

            if (securityElem != null)
            {
                SetupSecureEnvironment(securityElem.Certificate, port);
                baseAddress = string.Format(SecureBaseAddressTemplate, Environment.MachineName, port);
                frame.SetState(KeyIsSecuredServer, true);
            }
            else
            {
                baseAddress = string.Format(NormalBaseAddressTemplate, Environment.MachineName, port);
                frame.SetState(KeyIsSecuredServer, false);
            }

            // looking into the RunFrames and search for TraceElement. if it exists
            // set the tracer's type to the configuration otherwise skip this step
            TraceElement traceElem = frame.GetFirstElement <TraceElement>();
            Type         traceType = null;

            if (traceElem != null)
            {
                traceType = traceElem.TracerType;
            }

            // create initiator in the sandbox
            KatanaSelfHostServerInitiator serverInitiator;

            if (sandbox != null)
            {
                serverInitiator = sandbox.CreateInstanceAndUnwrap(
                    typeof(KatanaSelfHostServerInitiator).Assembly.FullName,
                    typeof(KatanaSelfHostServerInitiator).FullName)
                                  as KatanaSelfHostServerInitiator;
            }
            else
            {
                serverInitiator = new KatanaSelfHostServerInitiator();
            }

            // set up the server
            serverInitiator.Setup(
                baseAddress,
                TypeDescriptor.GetDesignatedMethod <NuwaKatanaConfigurationAttribute>(),
                TypeDescriptor.ConfigureMethod,
                traceType, GetDefaultRouteTemplate());

            frame.SetState(KeyReservedPort, port);
            frame.SetState(KeyBaseAddresss, baseAddress);
            frame.SetState(KeyServerInitiator, serverInitiator);

            return(true);
        }