// Any Errors thrown from this method indicate that the client
        // runner is putting invalid values into the package.
        private void ValidatePackageSettings()
        {
#if !NETSTANDARD1_6 && !NETSTANDARD2_0  // TODO: How do we validate runtime framework for .NET Standard 2.0?
            var processModel = TestPackage.GetSetting(EnginePackageSettings.ProcessModel, "Default").ToLower();
            var runningInProcess = processModel == "single" || processModel == "inprocess";
            var frameworkSetting = TestPackage.GetSetting(EnginePackageSettings.RuntimeFramework, "");
            var runAsX86 = TestPackage.GetSetting(EnginePackageSettings.RunAsX86, false);

            if (frameworkSetting.Length > 0)
            {
                // Check requested framework is actually available
                var runtimeService = _services.GetService<IRuntimeFrameworkService>();
                if (!runtimeService.IsAvailable(frameworkSetting))
                    throw new NUnitEngineException(string.Format("The requested framework {0} is unknown or not available.", frameworkSetting));

                // If running in process, check requested framework is compatible
                if (runningInProcess)
                {
                    var currentFramework = RuntimeFramework.CurrentFramework;

                    RuntimeFramework requestedFramework;
                    if (!RuntimeFramework.TryParse(frameworkSetting, out requestedFramework))
                        throw new NUnitEngineException("Invalid or unknown framework requested: " + frameworkSetting);

                    if (!currentFramework.Supports(requestedFramework))
                        throw new NUnitEngineException(string.Format(
                            "Cannot run {0} framework in process already running {1}.", frameworkSetting, currentFramework));
                }
            }

            if (runningInProcess && runAsX86 && IntPtr.Size == 8)
                throw new NUnitEngineException("Cannot run tests in process - a 32 bit process is required.");
#endif
        }
Example #2
0
        // Any Errors thrown from this method indicate that the client
        // runner is putting invalid values into the package.
        private void ValidatePackageSettings()
        {
            var frameworkSetting = TestPackage.GetSetting(EnginePackageSettings.RuntimeFramework, "");

            if (frameworkSetting.Length > 0)
            {
                // Check requested framework is actually available
                var runtimeService = _services.GetService <IRuntimeFrameworkService>();
                if (!runtimeService.IsAvailable(frameworkSetting))
                {
                    throw new NUnitEngineException(string.Format("The requested framework {0} is unknown or not available.", frameworkSetting));
                }

                // If running in process, check requested framework is compatible
                var processModel = TestPackage.GetSetting(EnginePackageSettings.ProcessModel, "Default").ToLower();
                if (processModel == "single" || processModel == "inprocess")
                {
                    var currentFramework = RuntimeFramework.CurrentFramework;

                    RuntimeFramework requestedFramework;
                    if (!RuntimeFramework.TryParse(frameworkSetting, out requestedFramework))
                    {
                        throw new NUnitEngineException("Invalid or unknown framework requested: " + frameworkSetting);
                    }

                    if (!currentFramework.Supports(requestedFramework))
                    {
                        throw new NUnitEngineException(string.Format(
                                                           "Cannot run {0} framework in process already running {1}.", frameworkSetting, currentFramework));
                    }
                }
            }
        }
Example #3
0
        private static RuntimeFramework SelectRuntimeFrameworkInner(TestPackage package)
        {
            foreach (var subPackage in package.SubPackages)
            {
                SelectRuntimeFrameworkInner(subPackage);
            }

            // Examine the provided settings
            RuntimeFramework currentFramework = RuntimeFramework.CurrentFramework;

            log.Debug("Current framework is " + currentFramework);

            string frameworkSetting = package.GetSetting(EnginePackageSettings.RequestedRuntimeFramework, "");

            RuntimeFramework requestedFramework;

            if (frameworkSetting.Length > 0)
            {
                if (!RuntimeFramework.TryParse(frameworkSetting, out requestedFramework))
                {
                    throw new NUnitEngineException("Invalid or unknown framework requested: " + frameworkSetting);
                }

                log.Debug($"Requested framework for {package.Name} is {requestedFramework}");
            }
            else
            {
                requestedFramework = new RuntimeFramework(RuntimeType.Any, RuntimeFramework.DefaultVersion);
                log.Debug($"No specific framework requested for {package.Name}");
            }

            RuntimeType targetRuntime = requestedFramework.Runtime;
            Version     targetVersion = requestedFramework.FrameworkVersion;

            if (targetRuntime == RuntimeType.Any)
            {
                targetRuntime = currentFramework.Runtime;
            }

            if (targetVersion == RuntimeFramework.DefaultVersion)
            {
                targetVersion = package.GetSetting(InternalEnginePackageSettings.ImageRuntimeVersion, currentFramework.FrameworkVersion);
            }

            if (!new RuntimeFramework(targetRuntime, targetVersion).IsAvailable)
            {
                log.Debug("Preferred version {0} is not installed or this NUnit installation does not support it", targetVersion);
                if (targetVersion < currentFramework.FrameworkVersion)
                {
                    targetVersion = currentFramework.FrameworkVersion;
                }
            }

            RuntimeFramework targetFramework = new RuntimeFramework(targetRuntime, targetVersion);

            package.Settings[EnginePackageSettings.TargetRuntimeFramework] = targetFramework.ToString();

            log.Debug($"Test will use {targetFramework} for {package.Name}");
            return(targetFramework);
        }
Example #4
0
        // Any Errors thrown from this method indicate that the client
        // runner is putting invalid values into the package.
        private void ValidatePackageSettings()
        {
#if NETFRAMEWORK  // TODO: How do we validate runtime framework for .NET Standard 2.0?
            var processModel     = TestPackage.GetSetting(EnginePackageSettings.ProcessModel, "Default").ToLower();
            var runningInProcess = processModel == "single" || processModel == "inprocess";
            var frameworkSetting = TestPackage.GetSetting(EnginePackageSettings.RequestedRuntimeFramework, "");
            var runAsX86         = TestPackage.GetSetting(EnginePackageSettings.RunAsX86, false);

            if (frameworkSetting.Length > 0)
            {
                // Check requested framework is actually available
                var runtimeService = _services.GetService <IRuntimeFrameworkService>();
                if (!runtimeService.IsAvailable(frameworkSetting))
                {
                    throw new NUnitEngineException(string.Format("The requested framework {0} is unknown or not available.", frameworkSetting));
                }

                // If running in process, check requested framework is compatible
                if (runningInProcess)
                {
                    var currentFramework = RuntimeFramework.CurrentFramework;

                    RuntimeFramework requestedFramework;
                    if (!RuntimeFramework.TryParse(frameworkSetting, out requestedFramework))
                    {
                        throw new NUnitEngineException("Invalid or unknown framework requested: " + frameworkSetting);
                    }

                    if (!currentFramework.Supports(requestedFramework))
                    {
                        throw new NUnitEngineException(string.Format(
                                                           "Cannot run {0} framework in process already running {1}.", frameworkSetting, currentFramework));
                    }
                }
            }

            if (runningInProcess && runAsX86 && IntPtr.Size == 8)
            {
                throw new NUnitEngineException("Cannot run tests in process - a 32 bit process is required.");
            }

            foreach (var entry in ObsoleteSettings)
            {
                var oldKey = entry.OldKey;
                var newKey = entry.NewKey;
                if (TestPackage.Settings.ContainsKey(oldKey) && !TestPackage.Settings.ContainsKey(newKey))
                {
                    TestPackage.Settings[newKey] = TestPackage.Settings[oldKey];
                }
            }
#endif
        }
        /// <summary>
        /// Returns true if the runtime framework represented by
        /// the string passed as an argument is available.
        /// </summary>
        /// <param name="name">A string representing a framework, like 'net-4.0'</param>
        /// <returns>True if the framework is available, false if unavailable or nonexistent</returns>
        public bool IsAvailable(string name)
        {
            Guard.ArgumentNotNullOrEmpty(name, nameof(name));

            RuntimeFramework requestedFramework;

            if (!RuntimeFramework.TryParse(name, out requestedFramework))
            {
                throw new NUnitEngineException("Invalid or unknown framework requested: " + name);
            }

            return(IsAvailable(requestedFramework));
        }
Example #6
0
        /// <summary>
        /// Returns true if the runtime framework represented by
        /// the string passed as an argument is available.
        /// </summary>
        /// <param name="name">A string representing a framework, like 'net-4.0'</param>
        /// <returns>True if the framework is available, false if unavailable or nonexistent</returns>
        public bool IsAvailable(string name)
        {
            Guard.ArgumentNotNullOrEmpty(name, nameof(name));

            RuntimeFramework requestedFramework;

            if (!RuntimeFramework.TryParse(name, out requestedFramework))
            {
                throw new NUnitEngineException("Invalid or unknown framework requested: " + name);
            }

            foreach (var framework in RuntimeFramework.AvailableFrameworks)
            {
                if (FrameworksMatch(requestedFramework, framework))
                {
                    return(true);
                }
            }

            return(false);
        }
Example #7
0
        private Guid LaunchAgentProcess(TestPackage package)
        {
            RuntimeFramework targetRuntime;
            string           runtimeSetting = package.GetSetting(EnginePackageSettings.RuntimeFramework, "");

            if (runtimeSetting.Length > 0)
            {
                if (!RuntimeFramework.TryParse(runtimeSetting, out targetRuntime))
                {
                    throw new NUnitEngineException("Invalid or unknown framework requested: " + runtimeSetting);
                }
            }
            else
            {
                targetRuntime = RuntimeFramework.CurrentFramework;
            }

            if (targetRuntime.Runtime == RuntimeType.Any)
            {
                targetRuntime = new RuntimeFramework(RuntimeFramework.CurrentFramework.Runtime, targetRuntime.ClrVersion);
            }

            bool   useX86Agent     = package.GetSetting(EnginePackageSettings.RunAsX86, false);
            bool   debugTests      = package.GetSetting(EnginePackageSettings.DebugTests, false);
            bool   debugAgent      = package.GetSetting(EnginePackageSettings.DebugAgent, false);
            string traceLevel      = package.GetSetting(EnginePackageSettings.InternalTraceLevel, "Off");
            bool   loadUserProfile = package.GetSetting(EnginePackageSettings.LoadUserProfile, false);
            string workDirectory   = package.GetSetting(EnginePackageSettings.WorkDirectory, string.Empty);

            // Set options that need to be in effect before the package
            // is loaded by using the command line.
            string agentArgs = "--pid=" + Process.GetCurrentProcess().Id.ToString();

            if (traceLevel != "Off")
            {
                agentArgs += " --trace:" + traceLevel;
            }
            if (debugAgent)
            {
                agentArgs += " --debug-agent";
            }
            if (workDirectory != string.Empty)
            {
                agentArgs += " --work=\"" + workDirectory + "\"";
            }

            log.Info("Getting {0} agent for use under {1}", useX86Agent ? "x86" : "standard", targetRuntime);

            if (!targetRuntime.IsAvailable)
            {
                throw new ArgumentException(
                          string.Format("The {0} framework is not available", targetRuntime),
                          "framework");
            }

            string agentExePath = GetTestAgentExePath(useX86Agent);

            if (!File.Exists(agentExePath))
            {
                throw new FileNotFoundException(
                          $"{Path.GetFileName(agentExePath)} could not be found.", agentExePath);
            }

            log.Debug("Using nunit-agent at " + agentExePath);

            Process p = new Process();

            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow  = true;
            p.EnableRaisingEvents       = true;
            p.Exited += OnAgentExit;
            Guid   agentId = Guid.NewGuid();
            string arglist = agentId.ToString() + " " + ServerUrl + " " + agentArgs;

            targetRuntime = ServiceContext.GetService <RuntimeFrameworkService>().GetBestAvailableFramework(targetRuntime);

            switch (targetRuntime.Runtime)
            {
            case RuntimeType.Mono:
                p.StartInfo.FileName = RuntimeFramework.MonoExePath;
                string monoOptions = "--runtime=v" + targetRuntime.ClrVersion.ToString(3);
                if (debugTests || debugAgent)
                {
                    monoOptions += " --debug";
                }
                p.StartInfo.Arguments = string.Format("{0} \"{1}\" {2}", monoOptions, agentExePath, arglist);
                break;

            case RuntimeType.Net:
                p.StartInfo.FileName = agentExePath;
                // Override the COMPLUS_Version env variable, this would cause CLR meta host to run a CLR of the specific version
                string envVar = "v" + targetRuntime.ClrVersion.ToString(3);
                p.StartInfo.EnvironmentVariables["COMPLUS_Version"] = envVar;
                // Leave a marker that we have changed this variable, so that the agent could restore it for any code or child processes running within the agent
                string cpvOriginal = Environment.GetEnvironmentVariable("COMPLUS_Version");
                p.StartInfo.EnvironmentVariables["TestAgency_COMPLUS_Version_Original"] = string.IsNullOrEmpty(cpvOriginal) ? "NULL" : cpvOriginal;
                p.StartInfo.Arguments       = arglist;
                p.StartInfo.LoadUserProfile = loadUserProfile;
                break;

            default:
                p.StartInfo.FileName  = agentExePath;
                p.StartInfo.Arguments = arglist;
                break;
            }

            p.Start();
            log.Debug("Launched Agent process {0} - see nunit-agent_{0}.log", p.Id);
            log.Debug("Command line: \"{0}\" {1}", p.StartInfo.FileName, p.StartInfo.Arguments);

            _agentData.Add(new AgentRecord(agentId, p, null, AgentStatus.Starting));
            return(agentId);
        }
Example #8
0
        /// <summary>
        /// Selects a target runtime framework for a TestPackage based on
        /// the settings in the package and the assemblies themselves.
        /// The package RuntimeFramework setting may be updated as a result
        /// and a string representing the selected runtime is returned.
        /// </summary>
        /// <param name="package">A TestPackage</param>
        /// <returns>A string representing the selected RuntimeFramework</returns>
        public string SelectRuntimeFramework(TestPackage package)
        {
            // Evaluate package target framework
            ApplyImageData(package);

            // Examine the provided settings
            RuntimeFramework currentFramework = RuntimeFramework.CurrentFramework;
            string           frameworkSetting = package.GetSetting(EnginePackageSettings.RuntimeFramework, "");

            RuntimeFramework requestedFramework;

            if (frameworkSetting.Length > 0)
            {
                if (!RuntimeFramework.TryParse(frameworkSetting, out requestedFramework))
                {
                    throw new NUnitEngineException("Invalid or unknown framework requested: " + frameworkSetting);
                }
            }
            else
            {
                requestedFramework = new RuntimeFramework(RuntimeType.Any, RuntimeFramework.DefaultVersion);
            }

            log.Debug("Current framework is {0}", currentFramework);
            if (requestedFramework == null)
            {
                log.Debug("No specific framework requested");
            }
            else
            {
                log.Debug("Requested framework is {0}", requestedFramework);
            }

            RuntimeType targetRuntime = requestedFramework.Runtime;
            Version     targetVersion = requestedFramework.FrameworkVersion;

            if (targetRuntime == RuntimeType.Any)
            {
                targetRuntime = currentFramework.Runtime;
            }

            if (targetVersion == RuntimeFramework.DefaultVersion)
            {
                targetVersion = package.GetSetting(InternalEnginePackageSettings.ImageRuntimeVersion, currentFramework.FrameworkVersion);
            }

            if (!new RuntimeFramework(targetRuntime, targetVersion).IsAvailable)
            {
                log.Debug("Preferred version {0} is not installed or this NUnit installation does not support it", targetVersion);
                if (targetVersion < currentFramework.FrameworkVersion)
                {
                    targetVersion = currentFramework.FrameworkVersion;
                }
            }

            RuntimeFramework targetFramework = new RuntimeFramework(targetRuntime, targetVersion);

            package.Settings[EnginePackageSettings.RuntimeFramework] = targetFramework.ToString();

            log.Debug("Test will use {0} framework", targetFramework);

            return(targetFramework.ToString());
        }
        private RuntimeFramework SelectRuntimeFrameworkInner(TestPackage package)
        {
            foreach (var subPackage in package.SubPackages)
            {
                SelectRuntimeFrameworkInner(subPackage);
            }

            // Examine the provided settings
            RuntimeFramework currentFramework = RuntimeFramework.CurrentFramework;

            log.Debug("Current framework is " + currentFramework);

            string requestedFrameworkSetting = package.GetSetting(EnginePackageSettings.RuntimeFramework, "");

            if (requestedFrameworkSetting.Length > 0)
            {
                RuntimeFramework requestedFramework;
                if (!RuntimeFramework.TryParse(requestedFrameworkSetting, out requestedFramework))
                {
                    throw new NUnitEngineException("Invalid or unknown framework requested: " + requestedFrameworkSetting);
                }

                log.Debug($"Requested framework for {package.Name} is {requestedFramework}");

                if (!IsAvailable(requestedFramework))
                {
                    throw new NUnitEngineException("Requested framework is not available: " + requestedFrameworkSetting);
                }

                return(requestedFramework);
            }

            log.Debug($"No specific framework requested for {package.Name}");

            string imageTargetFrameworkNameSetting = package.GetSetting(InternalEnginePackageSettings.ImageTargetFrameworkName, "");

            RuntimeFramework targetFramework;

            // HACK: handling the TargetFrameworkName does not currently work outside of windows
            if (Environment.OSVersion.Platform == PlatformID.Win32NT && imageTargetFrameworkNameSetting.Length > 0)
            {
                targetFramework = RuntimeFramework.FromFrameworkName(imageTargetFrameworkNameSetting);

                // TODO: temporary exception thrown until we implement .NET Core
                if (targetFramework.Runtime == Runtime.NetCore)
                {
                    throw new NotImplementedException("The GUI does not yet support .NET Core tests");
                }
            }
            else
            {
                var targetVersion = package.GetSetting(InternalEnginePackageSettings.ImageRuntimeVersion, currentFramework.FrameworkVersion);
                targetFramework = new RuntimeFramework(currentFramework.Runtime, targetVersion);
            }

            if (!IsAvailable(targetFramework))
            {
                log.Debug("Preferred target framework {0} is not available.", targetFramework);
                if (currentFramework.Supports(targetFramework))
                {
                    targetFramework = currentFramework;
                    log.Debug($"Using {currentFramework}");
                }
            }

            package.Settings[EnginePackageSettings.RuntimeFramework] = targetFramework.ToString();

            log.Debug($"Test will use {targetFramework} for {package.Name}");
            return(targetFramework);
        }
Example #10
0
        private RuntimeFramework SelectRuntimeFrameworkInner(TestPackage package)
        {
            foreach (var subPackage in package.SubPackages)
            {
                SelectRuntimeFrameworkInner(subPackage);
            }

            // Examine the provided settings
            RuntimeFramework currentFramework = RuntimeFramework.CurrentFramework;

            log.Debug("Current framework is " + currentFramework);

            string frameworkSetting = package.GetSetting(EnginePackageSettings.RequestedRuntimeFramework, "");

            RuntimeFramework requestedFramework;

            if (frameworkSetting.Length > 0)
            {
                if (!RuntimeFramework.TryParse(frameworkSetting, out requestedFramework))
                {
                    throw new NUnitEngineException("Invalid or unknown framework requested: " + frameworkSetting);
                }

                log.Debug($"Requested framework for {package.Name} is {requestedFramework}");

                if (!IsAvailable(frameworkSetting))
                {
                    throw new NUnitEngineException("Requested framework is not available: " + frameworkSetting);
                }

                package.Settings[EnginePackageSettings.TargetRuntimeFramework] = frameworkSetting;

                return(requestedFramework);
            }

            log.Debug($"No specific framework requested for {package.Name}");

            string      imageTargetFrameworkNameSetting = package.GetSetting(InternalEnginePackageSettings.ImageTargetFrameworkName, "");
            RuntimeType targetRuntime;
            Version     targetVersion;

            if (string.IsNullOrEmpty(imageTargetFrameworkNameSetting))
            {
                // Assume .NET Framework
                targetRuntime = currentFramework.Runtime;
                targetVersion = package.GetSetting(InternalEnginePackageSettings.ImageRuntimeVersion, new Version(2, 0));
            }
            else
            {
                FrameworkName frameworkName = new FrameworkName(imageTargetFrameworkNameSetting);

                switch (frameworkName.Identifier)
                {
                case ".NETFramework":
                    targetRuntime = RuntimeType.Net;
                    break;

                case ".NETCoreApp":
                    targetRuntime = RuntimeType.NetCore;
                    break;

                default:
                    throw new NUnitEngineException("Unsupported Target Framework: " + imageTargetFrameworkNameSetting);
                }

                targetVersion = frameworkName.Version;
            }

            if (!new RuntimeFramework(targetRuntime, targetVersion).IsAvailable)
            {
                log.Debug("Preferred version {0} is not installed or this NUnit installation does not support it", targetVersion);
                if (targetVersion < currentFramework.FrameworkVersion)
                {
                    targetVersion = currentFramework.FrameworkVersion;
                }
            }

            RuntimeFramework targetFramework = new RuntimeFramework(targetRuntime, targetVersion);

            package.Settings[EnginePackageSettings.TargetRuntimeFramework] = targetFramework.ToString();

            log.Debug($"Test will use {targetFramework} for {package.Name}");
            return(targetFramework);
        }