Esempio n. 1
0
        public void InvokeBasicFunctionWithRequiresWorks()
        {
            string path = Path.Join(s_funcDirectory, "testBasicFunctionWithRequires.ps1");

            var(functionInfo, testManager) = PrepareFunction(path, string.Empty);

            try
            {
                FunctionMetadata.RegisterFunctionMetadata(testManager.InstanceId, functionInfo);
                Hashtable result = InvokeFunction(testManager, functionInfo);

                // The outputBinding hashtable for the runspace should be cleared after 'InvokeFunction'
                Hashtable outputBindings = FunctionMetadata.GetOutputBindingHashtable(testManager.InstanceId);
                Assert.Empty(outputBindings);

                // When function script has #requires, not PowerShell function will be created for the Az function,
                // and the invocation uses the file path directly.
                string expectedResult = $"{TestStringData},ThreadJob,testBasicFunctionWithRequires.ps1";
                Assert.Equal(expectedResult, result[TestOutputBindingName]);
            }
            finally
            {
                FunctionMetadata.UnregisterFunctionMetadata(testManager.InstanceId);
            }
        }
Esempio n. 2
0
        public void InvokeBasicFunctionWithTriggerMetadataAndTraceContextWorks()
        {
            string path = Path.Join(s_funcDirectory, "testBasicFunctionWithTriggerMetadata.ps1");

            var(functionInfo, testManager) = PrepareFunction(path, string.Empty);

            Hashtable triggerMetadata = new Hashtable(StringComparer.OrdinalIgnoreCase)
            {
                { TestInputBindingName, TestStringData }
            };

            try
            {
                FunctionMetadata.RegisterFunctionMetadata(testManager.InstanceId, functionInfo);

                Hashtable result = InvokeFunction(testManager, functionInfo, triggerMetadata);

                // The outputBinding hashtable for the runspace should be cleared after 'InvokeFunction'
                Hashtable outputBindings = FunctionMetadata.GetOutputBindingHashtable(testManager.InstanceId);
                Assert.Empty(outputBindings);

                // A PowerShell function should be created fro the Az function.
                string expectedResult = $"{TestStringData},{functionInfo.DeployedPSFuncName}";
                Assert.Equal(expectedResult, result[TestOutputBindingName]);
            }
            finally
            {
                FunctionMetadata.UnregisterFunctionMetadata(testManager.InstanceId);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Checkout an idle PowerShellManager instance in a non-blocking asynchronous way.
        /// </summary>
        internal PowerShellManager CheckoutIdleWorker(StreamingMessage request, AzFunctionInfo functionInfo)
        {
            PowerShellManager psManager    = null;
            string            requestId    = request.RequestId;
            string            invocationId = request.InvocationRequest?.InvocationId;

            // If the pool has an idle one, just use it.
            if (!_pool.TryTake(out psManager))
            {
                // The pool doesn't have an idle one.
                if (_poolSize < _upperBound &&
                    Interlocked.Increment(ref _poolSize) <= _upperBound)
                {
                    // If the pool hasn't reached its bounded capacity yet, then
                    // we create a new item and return it.
                    var logger = new RpcLogger(_msgStream);
                    logger.SetContext(requestId, invocationId);
                    psManager = new PowerShellManager(logger);
                }
                else
                {
                    // If the pool has reached its bounded capacity, then the thread
                    // should be blocked until an idle one becomes available.
                    psManager = _pool.Take();
                }
            }

            // Register the function with the Runspace before returning the idle PowerShellManager.
            FunctionMetadata.RegisterFunctionMetadata(psManager.InstanceId, functionInfo);
            psManager.Logger.SetContext(requestId, invocationId);
            return(psManager);
        }
Esempio n. 4
0
        public void RegisterAndUnregisterFunctionMetadataShouldWork()
        {
            string path         = Path.Join(TestUtils.FunctionDirectory, "testBasicFunction.ps1");
            var    functionInfo = TestUtils.NewAzFunctionInfo(path, string.Empty);

            Assert.Empty(FunctionMetadata.OutputBindingCache);
            FunctionMetadata.RegisterFunctionMetadata(_testManager.InstanceId, functionInfo);
            Assert.Single(FunctionMetadata.OutputBindingCache);
            FunctionMetadata.UnregisterFunctionMetadata(_testManager.InstanceId);
            Assert.Empty(FunctionMetadata.OutputBindingCache);
        }
        /// <summary>
        /// Checkout an idle PowerShellManager instance in a non-blocking asynchronous way.
        /// </summary>
        internal PowerShellManager CheckoutIdleWorker(
            string requestId,
            string invocationId,
            string functionName,
            ReadOnlyDictionary <string, ReadOnlyBindingInfo> outputBindings)
        {
            PowerShellManager psManager = null;

            // If the pool has an idle one, just use it.
            if (!_pool.TryTake(out psManager))
            {
                // The pool doesn't have an idle one.
                if (_poolSize < UpperBound)
                {
                    int id = Interlocked.Increment(ref _poolSize);
                    if (id <= UpperBound)
                    {
                        // If the pool hasn't reached its bounded capacity yet, then
                        // we create a new item and return it.
                        var logger = CreateLoggerWithContext(requestId, invocationId);
                        psManager = new PowerShellManager(logger, id);

                        RpcLogger.WriteSystemLog(LogLevel.Trace, string.Format(PowerShellWorkerStrings.LogNewPowerShellManagerCreated, id.ToString()));
                    }
                }

                if (psManager == null)
                {
                    var logger = CreateLoggerWithContext(requestId, invocationId);
                    logger.Log(isUserOnlyLog: true, LogLevel.Warning, string.Format(PowerShellWorkerStrings.FunctionQueuingRequest, functionName));

                    // If the pool has reached its bounded capacity, then the thread
                    // should be blocked until an idle one becomes available.
                    psManager = _pool.Take();
                }
            }

            psManager.Logger.SetContext(requestId, invocationId);

            // Finish the initialization if not yet.
            // This applies only to the very first PowerShellManager instance, whose initialization was deferred.
            psManager.Initialize();

            // Register the function with the Runspace before returning the idle PowerShellManager.
            FunctionMetadata.RegisterFunctionMetadata(psManager.InstanceId, outputBindings);

            return(psManager);
        }
Esempio n. 6
0
        public void RegisterAndUnregisterFunctionMetadataShouldWork()
        {
            string path = Path.Join(s_funcDirectory, "testBasicFunction.ps1");

            var(functionInfo, testManager) = PrepareFunction(path, string.Empty);

            FunctionMetadata.RegisterFunctionMetadata(testManager.InstanceId, functionInfo);
            var outBindingMap = FunctionMetadata.GetOutputBindingInfo(testManager.InstanceId);

            Assert.Single(outBindingMap);
            Assert.Equal(TestOutputBindingName, outBindingMap.First().Key);

            FunctionMetadata.UnregisterFunctionMetadata(testManager.InstanceId);
            outBindingMap = FunctionMetadata.GetOutputBindingInfo(testManager.InstanceId);
            Assert.Null(outBindingMap);
        }
Esempio n. 7
0
        public void InvokeFunctionWithEntryPointWorks()
        {
            string path         = Path.Join(TestUtils.FunctionDirectory, "testFunctionWithEntryPoint.psm1");
            var    functionInfo = TestUtils.NewAzFunctionInfo(path, "Run");

            try
            {
                FunctionMetadata.RegisterFunctionMetadata(_testManager.InstanceId, functionInfo);
                Hashtable result = _testManager.InvokeFunction(functionInfo, null, _testInputData);

                Assert.Equal(TestStringData, result[TestUtils.TestOutputBindingName]);
            }
            finally
            {
                FunctionMetadata.UnregisterFunctionMetadata(_testManager.InstanceId);
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Checkout an idle PowerShellManager instance in a non-blocking asynchronous way.
        /// </summary>
        internal PowerShellManager CheckoutIdleWorker(StreamingMessage request, AzFunctionInfo functionInfo)
        {
            PowerShellManager psManager    = null;
            string            requestId    = request.RequestId;
            string            invocationId = request.InvocationRequest?.InvocationId;

            // If the pool has an idle one, just use it.
            if (!_pool.TryTake(out psManager))
            {
                // The pool doesn't have an idle one.
                if (_poolSize < _upperBound)
                {
                    int id = Interlocked.Increment(ref _poolSize);
                    if (id <= _upperBound)
                    {
                        // If the pool hasn't reached its bounded capacity yet, then
                        // we create a new item and return it.
                        var logger = new RpcLogger(_msgStream);
                        logger.SetContext(requestId, invocationId);
                        psManager = new PowerShellManager(logger, id);

                        RpcLogger.WriteSystemLog(LogLevel.Trace, string.Format(PowerShellWorkerStrings.LogNewPowerShellManagerCreated, id.ToString()));
                    }
                }

                if (psManager == null)
                {
                    // If the pool has reached its bounded capacity, then the thread
                    // should be blocked until an idle one becomes available.
                    psManager = _pool.Take();
                }
            }

            // Finish the initialization if not yet.
            // This applies only to the very first PowerShellManager instance, whose initialization was deferred.
            psManager.Initialize();

            // Register the function with the Runspace before returning the idle PowerShellManager.
            FunctionMetadata.RegisterFunctionMetadata(psManager.InstanceId, functionInfo);
            psManager.Logger.SetContext(requestId, invocationId);

            return(psManager);
        }
Esempio n. 9
0
        public void FunctionShouldCleanupVariableTable()
        {
            string path         = Path.Join(TestUtils.FunctionDirectory, "testFunctionCleanup.ps1");
            var    functionInfo = TestUtils.NewAzFunctionInfo(path, string.Empty);

            try
            {
                FunctionMetadata.RegisterFunctionMetadata(_testManager.InstanceId, functionInfo);

                Hashtable result1 = _testManager.InvokeFunction(functionInfo, null, _testInputData);
                Assert.Equal("is not set", result1[TestUtils.TestOutputBindingName]);

                // the value should not change if the variable table is properly cleaned up.
                Hashtable result2 = _testManager.InvokeFunction(functionInfo, null, _testInputData);
                Assert.Equal("is not set", result2[TestUtils.TestOutputBindingName]);
            }
            finally
            {
                FunctionMetadata.UnregisterFunctionMetadata(_testManager.InstanceId);
            }
        }
Esempio n. 10
0
        public void InvokeBasicFunctionWithTriggerMetadataWorks()
        {
            string    path            = Path.Join(TestUtils.FunctionDirectory, "testBasicFunctionWithTriggerMetadata.ps1");
            var       functionInfo    = TestUtils.NewAzFunctionInfo(path, string.Empty);
            Hashtable triggerMetadata = new Hashtable(StringComparer.OrdinalIgnoreCase)
            {
                { TestUtils.TestInputBindingName, TestStringData }
            };

            try
            {
                FunctionMetadata.RegisterFunctionMetadata(_testManager.InstanceId, functionInfo);
                Hashtable result = _testManager.InvokeFunction(functionInfo, triggerMetadata, _testInputData);

                Assert.Equal(TestStringData, result[TestUtils.TestOutputBindingName]);
            }
            finally
            {
                FunctionMetadata.UnregisterFunctionMetadata(_testManager.InstanceId);
            }
        }
Esempio n. 11
0
        public void FunctionShouldCleanupVariableTable()
        {
            string path = Path.Join(s_funcDirectory, "testFunctionCleanup.ps1");

            var(functionInfo, testManager) = PrepareFunction(path, string.Empty);

            try
            {
                FunctionMetadata.RegisterFunctionMetadata(testManager.InstanceId, functionInfo);

                Hashtable result1 = InvokeFunction(testManager, functionInfo);
                Assert.Equal("is not set", result1[TestOutputBindingName]);

                // the value should not change if the variable table is properly cleaned up.
                Hashtable result2 = InvokeFunction(testManager, functionInfo);
                Assert.Equal("is not set", result2[TestOutputBindingName]);
            }
            finally
            {
                FunctionMetadata.UnregisterFunctionMetadata(testManager.InstanceId);
            }
        }
Esempio n. 12
0
        public void InvokeFunctionWithEntryPointWorks()
        {
            string path = Path.Join(s_funcDirectory, "testFunctionWithEntryPoint.psm1");

            var(functionInfo, testManager) = PrepareFunction(path, "Run");

            try
            {
                FunctionMetadata.RegisterFunctionMetadata(testManager.InstanceId, functionInfo);
                Hashtable result = InvokeFunction(testManager, functionInfo);

                // The outputBinding hashtable for the runspace should be cleared after 'InvokeFunction'
                Hashtable outputBindings = FunctionMetadata.GetOutputBindingHashtable(testManager.InstanceId);
                Assert.Empty(outputBindings);

                string expectedResult = $"{TestStringData},Run";
                Assert.Equal(expectedResult, result[TestOutputBindingName]);
            }
            finally
            {
                FunctionMetadata.UnregisterFunctionMetadata(testManager.InstanceId);
            }
        }
Esempio n. 13
0
        public void InvokeFunctionWithSpecialVariableWorks()
        {
            string path = Path.Join(s_funcDirectory, "testBasicFunctionSpecialVariables.ps1");

            var(functionInfo, testManager) = PrepareFunction(path, string.Empty);

            try
            {
                FunctionMetadata.RegisterFunctionMetadata(testManager.InstanceId, functionInfo);
                Hashtable result = InvokeFunction(testManager, functionInfo);

                // The outputBinding hashtable for the runspace should be cleared after 'InvokeFunction'
                Hashtable outputBindings = FunctionMetadata.GetOutputBindingHashtable(testManager.InstanceId);
                Assert.Empty(outputBindings);

                // A PowerShell function should be created fro the Az function.
                string expectedResult = $"{s_funcDirectory},{path},{functionInfo.DeployedPSFuncName}";
                Assert.Equal(expectedResult, result[TestOutputBindingName]);
            }
            finally
            {
                FunctionMetadata.UnregisterFunctionMetadata(testManager.InstanceId);
            }
        }
        internal void SuppressPipelineTracesForDurableActivityFunctionOnly(DurableFunctionType durableFunctionType, bool shouldSuppressPipelineTraces)
        {
            s_testLogger.FullLog.Clear();

            var path = Path.Join(s_funcDirectory, "testFunctionWithOutput.ps1");

            var(functionInfo, testManager)        = PrepareFunction(path, string.Empty);
            functionInfo.DurableFunctionInfo.Type = durableFunctionType;

            try
            {
                FunctionMetadata.RegisterFunctionMetadata(testManager.InstanceId, functionInfo.OutputBindings);

                var result = testManager.InvokeFunction(functionInfo, null, null, null, CreateOrchestratorInputData(), new FunctionInvocationPerformanceStopwatch());

                var relevantLogs = s_testLogger.FullLog.Where(message => message.StartsWith("Information: OUTPUT:")).ToList();
                var expected     = shouldSuppressPipelineTraces ? new string[0] : new[] { "Information: OUTPUT: Hello" };
                Assert.Equal(expected, relevantLogs);
            }
            finally
            {
                FunctionMetadata.UnregisterFunctionMetadata(testManager.InstanceId);
            }
        }
 public HelperModuleTests()
 {
     FunctionMetadata.RegisterFunctionMetadata(s_pwsh.Runspace.InstanceId, s_funcInfo);
 }