예제 #1
0
        private ExecutionResponse ExecuteFunction(string handler, string payload)
        {
            var buildPath = Path.GetFullPath(@"../../../../LambdaFunctions/FunctionSignatureExamples/bin/debug/netcoreapp2.1");

            var runtime    = LocalLambdaRuntime.Initialize(buildPath);
            var configInfo = new LambdaFunctionInfo
            {
                Name    = "TestMethod",
                Handler = handler
            };

            var function = runtime.LoadLambdaFunction(configInfo);

            Assert.True(function.IsSuccess);

            var request = new ExecutionRequest()
            {
                Function  = function,
                AWSRegion = "us-west-2",
                Payload   = payload
            };

            var response = runtime.ExecuteLambdaFunction(request);

            return(response);
        }
예제 #2
0
        private async Task <ExecutionResponse> ExecuteFunctionAsync(string handler, string payload)
        {
            var buildPath = TestUtils.GetLambdaFunctionBuildPath("FunctionSignatureExamples");

            var runtime    = LocalLambdaRuntime.Initialize(buildPath);
            var configInfo = new LambdaFunctionInfo
            {
                Name    = "TestMethod",
                Handler = handler
            };

            var function = runtime.LoadLambdaFunction(configInfo);

            Assert.True(function.IsSuccess);

            var request = new ExecutionRequest()
            {
                Function  = function,
                AWSRegion = "us-west-2",
                Payload   = payload
            };

            var response = await runtime.ExecuteLambdaFunctionAsync(request);

            return(response);
        }
        public void LoadStaticMethodWithMethodSerializer()
        {
            var configFile = TestUtils.GetLambdaFunctionSourceFile("ToUpperFunc", "aws-lambda-tools-defaults.json");
            var buildPath  = TestUtils.GetLambdaFunctionBuildPath("ToUpperFunc");

            var configInfo = LambdaDefaultsConfigFileParser.LoadFromFile(configFile);

            Assert.Single(configInfo.FunctionInfos);

            var runtime = LocalLambdaRuntime.Initialize(buildPath);

            var functions = runtime.LoadLambdaFunctions(configInfo.FunctionInfos);

            Assert.Equal(1, functions.Count);

            var function = functions[0];

            Assert.True(function.IsSuccess);
            Assert.NotNull(function.LambdaAssembly);
            Assert.NotNull(function.LambdaType);
            Assert.NotNull(function.LambdaMethod);

            Assert.NotNull(function.Serializer);
        }
        public void LoadStaticMethodWithMethodSerializer()
        {
            var configFile = Path.GetFullPath(@"../../../../LambdaFunctions/ToUpperFunc/aws-lambda-tools-defaults.json");
            var buildPath  = Path.GetFullPath(@"../../../../LambdaFunctions/ToUpperFunc/bin/debug/netcoreapp2.1");

            var configInfo = LambdaDefaultsConfigFileParser.LoadFromFile(configFile);

            Assert.Single(configInfo.FunctionInfos);

            var runtime = LocalLambdaRuntime.Initialize(buildPath);

            var functions = runtime.LoadLambdaFunctions(configInfo.FunctionInfos);

            Assert.Equal(1, functions.Count);

            var function = functions[0];

            Assert.True(function.IsSuccess);
            Assert.NotNull(function.LambdaAssembly);
            Assert.NotNull(function.LambdaType);
            Assert.NotNull(function.LambdaMethod);

            Assert.NotNull(function.Serializer);
        }
예제 #5
0
        static void Main(string[] args)
        {
            try
            {
                Environment.SetEnvironmentVariable("AWS_EXECUTION_ENV", "AWS_DOTNET_LAMDBA_TEST_TOOL_" + Utils.DetermineToolVersion());

                PrintToolTitle();

                var commandOptions = CommandLineOptions.Parse(args);

                var options = new LocalLambdaOptions()
                {
                    Port = commandOptions.Port
                };

                var path = Directory.GetCurrentDirectory();

                // Check to see if running in debug mode from this project's directory which means the test tool is being debugged.
                // To make debugging easier pick one of the test Lambda projects.
                if (Directory.GetCurrentDirectory().EndsWith("Amazon.Lambda.TestTool"))
                {
                    path = Path.Combine(Directory.GetCurrentDirectory(),
                                        "../LambdaFunctions/S3EventFunction/bin/Debug/netcoreapp2.1");
                }
                // If running in the project directory select the build directory so the deps.json file can be found.
                else if (Utils.IsProjectDirectory(path))
                {
                    path = Path.Combine(path, "bin/Debug/netcoreapp2.1");
                }

                options.LambdaRuntime = LocalLambdaRuntime.Initialize(path);
                Console.WriteLine($"Loaded local Lambda runtime from project output {path}");

                // Look for aws-lambda-tools-defaults.json or other config files.
                options.LambdaConfigFiles = SearchForConfigFiles(path);

                // Start the test tool web server.
                Startup.LaunchWebTester(options, !commandOptions.NoLaunchWindow);
            }
            catch (CommandLineParseException e)
            {
                Console.WriteLine($"Invalid command line arguments: {e.Message}");
                CommandLineOptions.PrintUsage();
                if (Debugger.IsAttached)
                {
                    Console.WriteLine("Press any key to exit");
                    Console.ReadKey();
                }
                System.Environment.Exit(-1);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine($"Unknown error occurred causing process exit: {e.Message}");
                Console.Error.WriteLine(e.StackTrace);
                if (Debugger.IsAttached)
                {
                    Console.WriteLine("Press any key to exit");
                    Console.ReadKey();
                }
                Environment.Exit(-2);
            }
        }
예제 #6
0
        public static void Startup(string productName, Action <LocalLambdaOptions, bool> uiStartup, string[] args, RunConfiguration runConfiguration)
        {
            try
            {
                Utils.PrintToolTitle(productName);

                var commandOptions = CommandLineOptions.Parse(args);
                if (commandOptions.ShowHelp)
                {
                    CommandLineOptions.PrintUsage();
                    return;
                }

                var localLambdaOptions = new LocalLambdaOptions()
                {
                    Port = commandOptions.Port
                };

                var lambdaAssemblyDirectory = commandOptions.Path ?? Directory.GetCurrentDirectory();

#if NETCORE_2_1
                var targetFramework = "netcoreapp2.1";
#elif NETCORE_3_1
                var targetFramework = "netcoreapp3.1";
#endif

                // Check to see if running in debug mode from this project's directory which means the test tool is being debugged.
                // To make debugging easier pick one of the test Lambda projects.
                if (lambdaAssemblyDirectory.EndsWith("Amazon.Lambda.TestTool.WebTester21"))
                {
                    lambdaAssemblyDirectory = Path.Combine(lambdaAssemblyDirectory, $"../../tests/LambdaFunctions/netcore21/S3EventFunction/bin/Debug/{targetFramework}");
                }
                else if (lambdaAssemblyDirectory.EndsWith("Amazon.Lambda.TestTool.WebTester31"))
                {
                    lambdaAssemblyDirectory = Path.Combine(lambdaAssemblyDirectory, $"../../tests/LambdaFunctions/netcore31/S3EventFunction/bin/Debug/{targetFramework}");
                }
                // If running in the project directory select the build directory so the deps.json file can be found.
                else if (Utils.IsProjectDirectory(lambdaAssemblyDirectory))
                {
                    lambdaAssemblyDirectory = Path.Combine(lambdaAssemblyDirectory, $"bin/Debug/{targetFramework}");
                }

                localLambdaOptions.LambdaRuntime = LocalLambdaRuntime.Initialize(lambdaAssemblyDirectory);
                runConfiguration.OutputWriter.WriteLine($"Loaded local Lambda runtime from project output {lambdaAssemblyDirectory}");

                if (commandOptions.NoUI)
                {
                    ExecuteWithNoUi(localLambdaOptions, commandOptions, lambdaAssemblyDirectory, runConfiguration);
                }
                else
                {
                    // Look for aws-lambda-tools-defaults.json or other config files.
                    localLambdaOptions.LambdaConfigFiles = Utils.SearchForConfigFiles(lambdaAssemblyDirectory);

                    // Start the test tool web server.
                    uiStartup(localLambdaOptions, !commandOptions.NoLaunchWindow);
                }
            }
            catch (CommandLineParseException e)
            {
                runConfiguration.OutputWriter.WriteLine($"Invalid command line arguments: {e.Message}");
                runConfiguration.OutputWriter.WriteLine("Use the --help option to learn about the possible command line arguments");
                if (runConfiguration.Mode == RunConfiguration.RunMode.Normal)
                {
                    if (Debugger.IsAttached)
                    {
                        Console.WriteLine("Press any key to exit");
                        Console.ReadKey();
                    }
                    System.Environment.Exit(-1);
                }
            }
            catch (Exception e)
            {
                runConfiguration.OutputWriter.WriteLine($"Unknown error occurred causing process exit: {e.Message}");
                runConfiguration.OutputWriter.WriteLine(e.StackTrace);
                if (runConfiguration.Mode == RunConfiguration.RunMode.Normal)
                {
                    if (Debugger.IsAttached)
                    {
                        Console.WriteLine("Press any key to exit");
                        Console.ReadKey();
                    }
                    System.Environment.Exit(-2);
                }
            }
        }
        public async Task DlqIntegTest()
        {
            const int WAIT_TIME = 5000;
            var       queueName = "local-dlq-list-queue-test-" + DateTime.Now.Ticks;

            using (var client = new AmazonSQSClient(TestUtils.GetAWSCredentials(), TestUtils.TestRegion))
            {
                var createResponse = await client.CreateQueueAsync(new CreateQueueRequest { QueueName = queueName });

                await TestUtils.WaitTillQueueIsCreatedAsync(client, createResponse.QueueUrl);

                try
                {
                    var configFile = Path.GetFullPath(@"../../../../LambdaFunctions/ToUpperFunc/aws-lambda-tools-defaults.json");
                    var buildPath  = Path.GetFullPath(@"../../../../LambdaFunctions/ToUpperFunc/bin/debug/netcoreapp2.1");
                    var configInfo = LambdaDefaultsConfigFileParser.LoadFromFile(configFile);
                    var runtime    = LocalLambdaRuntime.Initialize(buildPath);
                    var function   = runtime.LoadLambdaFunctions(configInfo.FunctionInfos)[0];

                    var monitor = new DlqMonitor(runtime, function, TestUtils.TestProfile, TestUtils.TestRegion.SystemName, createResponse.QueueUrl);

                    monitor.Start();
                    await client.SendMessageAsync(new SendMessageRequest
                    {
                        QueueUrl    = createResponse.QueueUrl,
                        MessageBody = "\"testing dlq\""
                    });

                    Thread.Sleep(WAIT_TIME);
                    var logs = monitor.FetchNewLogs();
                    Assert.Single(logs);

                    Assert.Contains("testing dlq", logs[0].Logs);
                    Assert.NotNull(logs[0].ReceiptHandle);
                    Assert.NotEqual(DateTime.MinValue, logs[0].ProcessTime);

                    logs = monitor.FetchNewLogs();
                    Assert.Equal(0, logs.Count);

                    await client.SendMessageAsync(new SendMessageRequest
                    {
                        QueueUrl    = createResponse.QueueUrl,
                        MessageBody = "\"testing dlq1\""
                    });

                    await client.SendMessageAsync(new SendMessageRequest
                    {
                        QueueUrl    = createResponse.QueueUrl,
                        MessageBody = "\"testing dlq2\""
                    });

                    Thread.Sleep(WAIT_TIME);

                    logs = monitor.FetchNewLogs();
                    Assert.Equal(2, logs.Count);

                    monitor.Stop();
                    Thread.Sleep(WAIT_TIME);
                    await client.SendMessageAsync(new SendMessageRequest
                    {
                        QueueUrl    = createResponse.QueueUrl,
                        MessageBody = "\"testing dlq3\""
                    });

                    Thread.Sleep(WAIT_TIME);

                    logs = monitor.FetchNewLogs();
                    Assert.Equal(0, logs.Count);
                }
                finally
                {
                    await client.DeleteQueueAsync(createResponse.QueueUrl);
                }
            }
        }