예제 #1
0
        internal StreamingMessage ProcessWorkerInitRequest(StreamingMessage request)
        {
            StatusResult status = new StatusResult()
            {
                Status = StatusResult.Types.Status.Success
            };

            try
            {
                _powerShellManager.InitializeRunspace();
            }
            catch (Exception e)
            {
                status.Status    = StatusResult.Types.Status.Failure;
                status.Exception = e.ToRpcException();
            }

            return(new StreamingMessage()
            {
                RequestId = request.RequestId,
                WorkerInitResponse = new WorkerInitResponse()
                {
                    Result = status
                }
            });
        }
예제 #2
0
        public void InitializeRunspaceSuccess()
        {
            var logger  = new ConsoleLogger();
            var manager = new PowerShellManager(logger);

            manager.InitializeRunspace();

            Assert.Single(logger.FullLog);
            Assert.Equal("Warning: Required environment variables to authenticate to Azure were not present", logger.FullLog[0]);
        }
        public void TestObjectToTypedData_PSObjectToString()
        {
            var logger  = new ConsoleLogger();
            var manager = new PowerShellManager(logger);

            manager.InitializeRunspace();

            object    input  = PSObject.AsPSObject("Hello World");
            TypedData output = input.ToTypedData(manager);

            Assert.Equal(TypedData.DataOneofCase.String, output.DataCase);
            Assert.Equal("Hello World", output.String);
        }
        public void TestObjectToTypedData_PSObjectToBytes()
        {
            var logger  = new ConsoleLogger();
            var manager = new PowerShellManager(logger);

            manager.InitializeRunspace();

            var    data  = new byte[] { 12, 23, 34 };
            object input = PSObject.AsPSObject(data);

            TypedData output = input.ToTypedData(manager);

            Assert.Equal(TypedData.DataOneofCase.Bytes, output.DataCase);
            Assert.Equal(3, output.Bytes.Length);
        }
        public void TestObjectToTypedData_PSObjectToStream()
        {
            var logger  = new ConsoleLogger();
            var manager = new PowerShellManager(logger);

            manager.InitializeRunspace();

            using (var data = new MemoryStream(new byte[] { 12, 23, 34 }))
            {
                object    input  = PSObject.AsPSObject(data);
                TypedData output = input.ToTypedData(manager);

                Assert.Equal(TypedData.DataOneofCase.Stream, output.DataCase);
                Assert.Equal(3, output.Stream.Length);
            }
        }
예제 #6
0
        public void InvokeFunctionWithEntryPointWorks()
        {
            var logger  = new ConsoleLogger();
            var manager = new PowerShellManager(logger);

            manager.InitializeRunspace();

            string path = System.IO.Path.Join(
                AppDomain.CurrentDomain.BaseDirectory,
                "Unit/PowerShell/TestScripts/testFunctionWithEntryPoint.ps1");

            var       functionInfo = GetAzFunctionInfo(path, "Run");
            Hashtable result       = manager.InvokeFunction(functionInfo, null, TestInputData);

            Assert.Equal(TestStringData, result[TestOutputBindingName]);
        }
        internal StreamingMessage ProcessWorkerInitRequest(StreamingMessage request)
        {
            StreamingMessage response = NewStreamingMessageTemplate(
                request.RequestId,
                StreamingMessage.ContentOneofCase.WorkerInitResponse,
                out StatusResult status);

            try
            {
                _powerShellManager.InitializeRunspace();
            }
            catch (Exception e)
            {
                status.Status    = StatusResult.Types.Status.Failure;
                status.Exception = e.ToRpcException();
            }

            return(response);
        }
        public void TestObjectToTypedData_PSObjectToJson_2()
        {
            var logger  = new ConsoleLogger();
            var manager = new PowerShellManager(logger);

            manager.InitializeRunspace();

            using (var ps = System.Management.Automation.PowerShell.Create())
            {
                object input = ps.AddScript("[pscustomobject]@{foo = 'bar'}").Invoke()[0];

                var expected = new TypedData
                {
                    Json = "{\"foo\":\"bar\"}"
                };

                Assert.Equal(expected, input.ToTypedData(manager));
            }
        }
        public void TestObjectToTypedData_PSObjectToJson_1()
        {
            var logger  = new ConsoleLogger();
            var manager = new PowerShellManager(logger);

            manager.InitializeRunspace();

            var data = new Hashtable {
                { "foo", "bar" }
            };
            object input = PSObject.AsPSObject(data);

            var expected = new TypedData
            {
                Json = "{\"foo\":\"bar\"}"
            };

            Assert.Equal(expected, input.ToTypedData(manager));
        }
예제 #10
0
        public void InvokeBasicFunctionWithTriggerMetadataWorks()
        {
            var logger  = new ConsoleLogger();
            var manager = new PowerShellManager(logger);

            manager.InitializeRunspace();

            string path = System.IO.Path.Join(
                AppDomain.CurrentDomain.BaseDirectory,
                "Unit/PowerShell/TestScripts/testBasicFunctionWithTriggerMetadata.ps1");

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

            var       functionInfo = GetAzFunctionInfo(path, string.Empty);
            Hashtable result       = manager.InvokeFunction(functionInfo, triggerMetadata, TestInputData);

            Assert.Equal(TestStringData, result[TestOutputBindingName]);
        }
예제 #11
0
        public void FunctionShouldCleanupVariableTable()
        {
            var logger  = new ConsoleLogger();
            var manager = new PowerShellManager(logger);

            manager.InitializeRunspace();

            string path = System.IO.Path.Join(
                AppDomain.CurrentDomain.BaseDirectory,
                "Unit/PowerShell/TestScripts/testFunctionCleanup.ps1");

            var       functionInfo = GetAzFunctionInfo(path, string.Empty);
            Hashtable result1      = manager.InvokeFunction(functionInfo, null, TestInputData);

            Assert.Equal("is not set", result1[TestOutputBindingName]);

            // the value shoould not change if the variable table is properly cleaned up.
            Hashtable result2 = manager.InvokeFunction(functionInfo, null, TestInputData);

            Assert.Equal("is not set", result2[TestOutputBindingName]);
        }