/// <summary>
        /// Proxy method for substitution of executing methods in adapter interface.
        /// </summary>
        /// <param name="targetMethod">The method the caller invoked.</param>
        /// <param name="args">The arguments the caller passed to the method.</param>
        /// <returns>The return value of the ExecuteMethod implementation.</returns>
        protected override object ExecuteMethod(MethodInfo targetMethod, object[] args)
        {
            //get help message from attribute
            string methodhelp = AdapterProxyHelpers.GetHelpMessage(targetMethod);

            bool compactMode = ((targetMethod.Name == "Initialize" || targetMethod.Name == "Reset") &&
                                AdapterType.IsAdapterTypeFullName(targetMethod.DeclaringType.FullName)
                                );

            if (compactMode)
            {
                return(ExecuteMethodCompact(targetMethod, methodhelp));
            }

            object retVal = null;

            object[] outArgs = args;

            // Check if this is a method from IAdapter. Any IAdapter methods should be ignored.
            if (!AdapterType.IsAdapterTypeFullName(targetMethod.DeclaringType.FullName) &&
                (targetMethod.DeclaringType.FullName != typeof(IDisposable).FullName)
                )
            {
                TestSite.Log.Add(LogEntryKind.EnterAdapter,
                                 "PowerShell adapter: {0}, method: {1}",
                                 ProxyType.Name,
                                 targetMethod.Name);

                try
                {
                    string path = LookupScript(targetMethod.Name);
                    if (path == null)
                    {
                        TestSite.Assume.Fail(
                            "PowerShell script file ({0}.ps1) can not be found.",
                            targetMethod.Name);
                    }
                    else
                    {
                        int timeout = AdapterProxyHelpers.GetTimeout(targetMethod, int.Parse(TestSite.Properties["AdapterInvokeTimeout"]));
                        Task <PSParameterBuilder> invokeTask = Task.Run <PSParameterBuilder>(() =>
                        {
                            TestSite.Log.Add(LogEntryKind.Debug, $"Start to invoke Script {targetMethod.Name}.ps1, timeout: {timeout}");
                            var invokeResult = InvokeScript(path, targetMethod, args, methodhelp);
                            TestSite.Log.Add(LogEntryKind.Debug, $"Complete execute Script {targetMethod.Name}.ps1");
                            return(invokeResult);
                        });

                        TimeSpan waiter = TimeSpan.FromMinutes(timeout);

                        if (invokeTask.Wait(waiter))
                        {
                            PSParameterBuilder resultBuilder = invokeTask.Result;
                            if (resultBuilder != null)
                            {
                                retVal = resultBuilder.RetValue;

                                if (resultBuilder.OutArguments != null)
                                {
                                    int argsIndex    = 0;
                                    int outArgsIndex = 0;
                                    foreach (ParameterInfo pi in targetMethod.GetParameters())
                                    {
                                        if (pi.ParameterType.IsByRef)
                                        {
                                            outArgs[argsIndex] = resultBuilder.OutArguments[outArgsIndex++];
                                        }
                                        argsIndex++;
                                    }
                                }

                                //clear builder
                                resultBuilder = null;
                            }
                        }
                        else
                        {
                            throw new TimeoutException($"Invoke adapater method timeout after wait {timeout} minutes.");
                        }
                    }
                }
                catch (Exception ex)
                {
                    TestSite.Log.Add(LogEntryKind.Debug, ex.ToString());
                    throw;
                }
                finally
                {
                    TestSite.Log.Add(LogEntryKind.ExitAdapter,
                                     "PowerShell adapter: {0}, method: {1}",
                                     ProxyType.Name,
                                     targetMethod.Name);
                }
            }

            return(retVal);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Proxy method for substitution of executing methods in adapter interface.
        /// </summary>
        /// <param name="targetMethod">The method the caller invoked.</param>
        /// <param name="args">The arguments the caller passed to the method.</param>
        /// <returns>The return value of the ExecuteMethod implementation.</returns>
        protected override object ExecuteMethod(MethodInfo targetMethod, object[] args)
        {
            object retVal = null;

            // Check if this is a method from IAdapter. Any IAdapter methods should be ignored.
            if (!AdapterType.IsAdapterTypeFullName(targetMethod.DeclaringType.FullName) &&
                (targetMethod.DeclaringType.FullName != typeof(IDisposable).FullName)
                )
            {
                TestSite.Log.Add(LogEntryKind.EnterAdapter,
                                 "Managed adapter: {0}, method: {1}",
                                 ProxyType.Name,
                                 targetMethod.Name);

                try
                {
                    int           timeout    = AdapterProxyHelpers.GetTimeout(targetMethod, int.Parse(TestSite.Properties["AdapterInvokeTimeout"]));
                    Task <object> invokeTask = Task.Run <object>(() =>
                    {
                        TestSite.Log.Add(LogEntryKind.Debug, $"Start to invoke target method {targetMethod.Name}, timeout: {timeout}");
                        object invokeResult;
                        if (targetMethod.IsStatic)
                        {
                            invokeResult = targetMethod.Invoke(null, args);
                        }
                        else
                        {
                            invokeResult = targetMethod.Invoke(instance, args);
                        }
                        TestSite.Log.Add(LogEntryKind.Debug, $"Complete invoke target method {targetMethod.Name}.");
                        return(invokeResult);
                    });

                    TimeSpan waiter = TimeSpan.FromMinutes(timeout);

                    if (invokeTask.Wait(waiter))
                    {
                        retVal = invokeTask.Result;
                    }
                    else
                    {
                        throw new TimeoutException($"Invoke adapater method timeout after wait {timeout} minutes.");
                    }
                }
                catch (Exception ex)
                {
                    if (ex is TargetInvocationException)
                    {
                        // thrown by methods invoked through reflection
                        // InnerException contains the actual exception thrown by methods
                        TestSite.Log.Add(LogEntryKind.Debug, ex.InnerException.ToString());
                        throw ex.InnerException;
                    }
                    else
                    {
                        TestSite.Log.Add(LogEntryKind.Debug, ex.ToString());
                        throw;
                    }
                }
                finally
                {
                    TestSite.Log.Add(LogEntryKind.ExitAdapter,
                                     "Managed adapter: {0}, method: {1}",
                                     ProxyType.Name,
                                     targetMethod.Name);
                }
            }

            return(retVal);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Proxy method for substitution of executing methods in adapter interface.
        /// </summary>
        /// <param name="targetMethod">The method the caller invoked.</param>
        /// <param name="args">The arguments the caller passed to the method.</param>
        /// <returns>The return value of the ExecuteMethod implementation.</returns>
        protected override object ExecuteMethod(MethodInfo targetMethod, object[] args)
        {
            //get help message from attribute
            string methodhelp = AdapterProxyHelpers.GetHelpMessage(targetMethod);

            bool compactMode = ((targetMethod.Name == "Initialize" || targetMethod.Name == "Reset") &&
                                AdapterType.IsAdapterTypeFullName(targetMethod.DeclaringType.FullName)
                                );

            if (compactMode)
            {
                return(ExecuteMethodCompact(targetMethod, methodhelp));
            }

            // Build parameter on each invoke
            builder = new ParameterDataBuilder(targetMethod);
            builder.Build(args);

            lastOutput = null;
            errorMsg   = new StringBuilder();

            object retVal    = null;
            string arguments = BuildScriptArguments();

            // Check if this is a method from IAdapter. Any IAdapter methods should be ignored.
            if (!AdapterType.IsAdapterTypeFullName(targetMethod.DeclaringType.FullName) &&
                (targetMethod.DeclaringType.FullName != typeof(IDisposable).FullName)
                )
            {
                TestSite.Log.Add(LogEntryKind.EnterAdapter,
                                 "Shell adapter: {0}, method: {1}",
                                 ProxyType.Name,
                                 targetMethod.Name);

                try
                {
                    string path = LookupScript(targetMethod.Name);
                    if (path == null)
                    {
                        TestSite.Assume.Fail(
                            "Shell script file ({0}.sh) can not be found.",
                            targetMethod.Name);
                    }
                    else
                    {
                        int timeout = AdapterProxyHelpers.GetTimeout(targetMethod, int.Parse(TestSite.Properties["AdapterInvokeTimeout"]));

                        Task <int> invokeTask = Task.Run <int>(() =>
                        {
                            TestSite.Log.Add(LogEntryKind.Debug, $"Start to invoke shell {targetMethod.Name}.sh, timeout: {timeout}");
                            int invokeResult = InvokeScript(path, arguments);
                            TestSite.Log.Add(LogEntryKind.Debug, $"Complete execute shell {targetMethod.Name}.sh");

                            return(invokeResult);
                        });

                        TimeSpan waiter = TimeSpan.FromMinutes(timeout);
                        if (invokeTask.Wait(waiter))
                        {
                            if (invokeTask.Result != 0)
                            {
                                string exceptionMessage = string.Format("Exception thrown when executing {0}.sh.\nExit code: {1}\nError message: {2}\n", targetMethod.Name, invokeTask.Result, errorMsg);
                                throw new InvalidOperationException(exceptionMessage);
                            }

                            if (builder.HasReturnVal)
                            {
                                retVal = AdapterProxyHelpers.ParseResult(builder.RetValType, lastOutput);
                            }
                        }
                        else
                        {
                            throw new TimeoutException($"Invoke adapater method timeout after wait {timeout} minutes.");
                        }
                    }
                }
                catch (Exception ex)
                {
                    TestSite.Log.Add(LogEntryKind.Debug, ex.ToString());
                    throw;
                }
                finally
                {
                    TestSite.Log.Add(LogEntryKind.ExitAdapter,
                                     "Shell adapter: {0}, method: {1}",
                                     ProxyType.Name,
                                     targetMethod.Name);
                }
            }
            return(retVal);
        }