示例#1
0
        public async Task PreTrafficHook(CodeDeployEvent deployment)
        {
            var status = LifecycleEventStatus.Succeeded;

            InitHooks();
            try
            {
                Console.WriteLine("PreHook. Version " + Env.Get("AWS_LAMBDA_FUNCTION_VERSION"));
                var tests = File.ReadAllText("smoketests.json").To <List <SmokeTest> >();
                foreach (var test in tests)
                {
                    if (!test.NoProxy)
                    {
                        var splitPath = test.Path.Trim('/').Split('/');
                        test.Path = string.Join("/", splitPath.Skip(1));
                        Console.WriteLine("Path " + test.Path);
                    }
                    var apiRequest = new APIGatewayProxyRequest
                    {
                        Path       = test.Path,
                        HttpMethod = test.Method,
                        Headers    = new Dictionary <string, string> {
                            { "Host", "localhost" }, { "__PRE_TRAFFIC_HOOK__", "true" }
                        }
                    };
                    var lambdaRequest = new InvokeRequest
                    {
                        FunctionName = Env.Get("VersionToTest"),
                        Payload      = apiRequest.ToJson()
                    };
                    var response = await _lambda.InvokeAsync(lambdaRequest);

                    using (var reader = new StreamReader(response.Payload))
                    {
                        var responseStr = reader.ReadToEnd();
                        Console.WriteLine("Response: " + responseStr);
                        var regex = new Regex(test.ResponsePattern);
                        if (!regex.IsMatch(responseStr))
                        {
                            status = LifecycleEventStatus.Failed;;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                status = LifecycleEventStatus.Failed;
                Console.WriteLine(ex.Message + " " + ex.StackTrace);
            }

            var request = new PutLifecycleEventHookExecutionStatusRequest
            {
                DeploymentId = deployment.DeploymentId,
                LifecycleEventHookExecutionId = deployment.LifecycleEventHookExecutionId,
                Status = status
            };

            await _codeDeploy.PutLifecycleEventHookExecutionStatusAsync(request);
        }
示例#2
0
        public async Task PostTrafficHook(CodeDeployEvent deployment)
        {
            var status = LifecycleEventStatus.Succeeded;

            try
            {
                InitHooks();
                Console.WriteLine("PostHook. Version " + Env.Get("AWS_LAMBDA_FUNCTION_VERSION"));
                var tests = File.ReadAllText("smoketests.json").To <List <SmokeTest> >();
                foreach (var test in tests)
                {
                    var httpRequest = new HttpRequestMessage(new HttpMethod(test.Method), test.Path);
                    if (!string.IsNullOrEmpty(test.Body))
                    {
                        httpRequest.Content = new StringContent(test.Body);
                    }
                    var response = await _httpClient.SendAsync(httpRequest);

                    var responseStr = await response.Content.ReadAsStringAsync();

                    Console.WriteLine("Response: " + responseStr);
                    var regex = new Regex(test.ResponsePattern);
                    if (!regex.IsMatch(responseStr))
                    {
                        status = LifecycleEventStatus.Failed;
                    }
                }
            }
            catch (Exception ex)
            {
                status = LifecycleEventStatus.Failed;
                Console.WriteLine(ex.Message + " " + ex.StackTrace);
            }
            var request = new PutLifecycleEventHookExecutionStatusRequest
            {
                DeploymentId = deployment.DeploymentId,
                LifecycleEventHookExecutionId = deployment.LifecycleEventHookExecutionId,
                Status = status
            };

            await _codeDeploy.PutLifecycleEventHookExecutionStatusAsync(request);
        }