Skip to content

Commit

Permalink
Re add tests for code deploy (#218)
Browse files Browse the repository at this point in the history
* Readd codeploy tests

* readd tests codeploy commands

* Update port type

* Remove other tests temporarly to test only fargate

* remove extra test

* fix unreachble error

* update auth

* fix account id

* update lifecycle policy

* Update port test

* restore tests
  • Loading branch information
marboledacci authored Sep 5, 2024
1 parent 26cc336 commit 61ee2d9
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 24 deletions.
161 changes: 156 additions & 5 deletions .circleci/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -524,16 +524,16 @@ workflows:
terraform-config-dir: "tests/terraform_setup/fargate"
context: [CPE-OIDC]
role_arn: "arn:aws:iam::122211685980:role/CPE_ECS_OIDC_TEST"
#################
# EC2
#################
# #################
# # EC2
# #################
- tear-down-test-env:
name: ec2_tear-down-test-env-initial
filters: *filters
aws-resource-name-prefix: ${AWS_RESOURCE_NAME_PREFIX_EC2}
terraform-config-dir: "tests/terraform_setup/ec2"
context: [CPE-OIDC]
role_arn: "arn:aws:iam::122211685980:role/CPE_ECS_OIDC_TEST"
role_arn: "arn:aws:iam::122211685980:role/CPE_ECS_OIDC_TEST"
- build-test-app:
name: ec2_build-test-app
docker-image-namespace: "${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com"
Expand Down Expand Up @@ -645,6 +645,119 @@ workflows:
role_arn: "arn:aws:iam::122211685980:role/CPE_ECS_OIDC_TEST"
requires:
- fargate_set-up-test-env
#################
# CodeDeploy
#################
- tear-down-test-env:
name: codedeploy_fargate_tear-down-test-env-initial
terraform-image: "hashicorp/terraform:1.4.0"
aws-resource-name-prefix: ${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}
terraform-config-dir: "tests/terraform_setup/fargate_codedeploy"
context: [CPE-OIDC]
role_arn: "arn:aws:iam::122211685980:role/CPE_ECS_OIDC_TEST"
filters: *filters
- build-test-app:
name: codedeploy_fargate_build-test-app
docker-image-namespace: "${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com"
docker-image-name: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}:${CIRCLE_SHA1}"
context: [CPE-OIDC]
requires:
- codedeploy_fargate_tear-down-test-env-initial
filters: *filters
- set-up-test-env:
name: codedeploy_fargate_set-up-test-env
filters: *filters
requires:
- codedeploy_fargate_build-test-app
terraform-image: "hashicorp/terraform:1.4.0"
aws-resource-name-prefix: ${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}
terraform-config-dir: "tests/terraform_setup/fargate_codedeploy"
context: [CPE-OIDC]
role_arn: "arn:aws:iam::122211685980:role/CPE_ECS_OIDC_TEST"
- test-service-update:
name: codedeploy_fargate_test-update_service-command
filters: *filters
requires:
- codedeploy_fargate_set-up-test-env
aws-resource-name-prefix: ${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}
family_name: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-service"
service_name: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-service"
docker-image-namespace: "${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com"
docker-image-name: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}:${CIRCLE_SHA1}"
role_arn: "arn:aws:iam::122211685980:role/CPE_ECS_OIDC_TEST"
skip-service-update: true
context: [CPE-OIDC]
- aws-ecs/deploy_service_update:
name: codedeploy_fargate_test-update_service-job
auth:
- aws-cli/setup:
role_arn: "arn:aws:iam::122211685980:role/CPE_ECS_OIDC_TEST"
filters: *filters
requires:
- codedeploy_fargate_test-update_service-command
region: AWS_REGION
family: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-service"
cluster: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-cluster"
container_image_name_updates: "container=${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-service,image-and-tag=${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}:${CIRCLE_SHA1}"
container_env_var_updates: 'container=${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-service,name=VERSION_INFO,value="${CIRCLE_SHA1}_${CIRCLE_BUILD_NUM}",container=${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-service,name=BUILD_DATE,value=$(date)'
deployment_controller: "CODE_DEPLOY"
codedeploy_application_name: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-codedeployapp"
codedeploy_deployment_group_name: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-codedeploygroup"
codedeploy_load_balanced_container_name: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-service"
codedeploy_load_balanced_container_port: "80"
codedeploy_capacity_provider_name: "FARGATE"
codedeploy_capacity_provider_base: "1"
codedeploy_capacity_provider_weight: "2"
verify_revision_is_deployed: false
context: [CPE-OIDC]
post-steps:
- wait-for-codedeploy-deployment:
application-name: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-codedeployapp"
deployment-group-name: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-codedeploygroup"
- test-deployment:
service_name: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-service"
cluster: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-cluster"
delete-load-balancer: false
- aws-ecs/deploy_service_update:
name: codedeploy_fargate_test-update-and-wait-service-job
auth:
- aws-cli/setup:
role_arn: "arn:aws:iam::122211685980:role/CPE_ECS_OIDC_TEST"
context: [CPE-OIDC]
filters: *filters
requires:
- codedeploy_fargate_test-update_service-job
region: AWS_REGION
family: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-service"
cluster: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-cluster"
container_image_name_updates: "container=${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-service,image-and-tag=${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}:${CIRCLE_SHA1}"
container_env_var_updates: 'container=${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-service,name=VERSION_INFO,value="${CIRCLE_SHA1}_${CIRCLE_BUILD_NUM}",container=${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-service,name=BUILD_DATE,value=$(date)'
deployment_controller: "CODE_DEPLOY"
codedeploy_application_name: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-codedeployapp"
codedeploy_deployment_group_name: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-codedeploygroup"
codedeploy_load_balanced_container_name: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-service"
codedeploy_load_balanced_container_port: "80"
verify_revision_is_deployed: true
verification_timeout: "12m"
post-steps:
- test-deployment:
service_name: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-service"
cluster: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-cluster"
delete-load-balancer: true
port: "8080"
- delete-service:
service_name: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-service"
cluster: "${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}-cluster"
- tear-down-test-env:
name: codedeploy_fargate_tear-down-test-env
requires:
- codedeploy_fargate_test-update-and-wait-service-job
terraform-image: "hashicorp/terraform:1.4.0"
aws-resource-name-prefix: ${AWS_RESOURCE_NAME_PREFIX_CODEDEPLOY_FARGATE}
terraform-config-dir: "tests/terraform_setup/fargate_codedeploy"
context: [CPE-OIDC]
role_arn: "arn:aws:iam::122211685980:role/CPE_ECS_OIDC_TEST"
filters: *filters
- orb-tools/pack:
filters: *release-filters
- orb-tools/publish:
Expand All @@ -653,9 +766,47 @@ workflows:
pub_type: production
enable_pr_comment: true
context: orb-publisher
requires: [orb-tools/pack, ec2_tear-down-test-env, fargate_tear-down-test-env, integration-test-ecs-cli-install]
requires: [orb-tools/pack, ec2_tear-down-test-env, fargate_tear-down-test-env, codedeploy_fargate_tear-down-test-env, integration-test-ecs-cli-install]
filters: *release-filters
commands:
wait-for-codedeploy-deployment:
description: "Wait for the CodeDeploy deployment to be successful"
parameters:
application-name:
description: "CodeDeploy application name"
type: string
deployment-group-name:
description: "CodeDeploy application name"
type: string
steps:
- run:
name: Wait for CodeDeploy deployment to be successful (for orb testing and is not part of the orb)
command: |
DEPLOYMENT_ID=$(aws deploy list-deployments \
--application-name << parameters.application-name >> \
--deployment-group-name << parameters.deployment-group-name >> \
--query "deployments" \
--max-items 1 \
--output text \
| head -n 1)
aws deploy wait deployment-successful --deployment-id ${DEPLOYMENT_ID}
delete-service:
description: "Forcefully delete an ECS service"
parameters:
service_name:
description: "Name of the ECS service"
type: string
cluster:
description: "Name of the cluster"
type: string
steps:
- run:
name: Delete ECS service
command: |
aws ecs delete-service \
--cluster << parameters.cluster>> \
--service << parameters.service_name >> \
--force
test-deployment:
description: "Test the deployment"
parameters:
Expand Down
6 changes: 6 additions & 0 deletions tests/terraform_setup/fargate_codedeploy/alb.tf
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ resource "aws_alb_listener" "front_end_green" {
target_group_arn = aws_alb_target_group.green.id
type = "forward"
}
lifecycle {
ignore_changes = [default_action]
}
}

resource "aws_alb_listener" "front_end_blue" {
Expand All @@ -63,4 +66,7 @@ resource "aws_alb_listener" "front_end_blue" {
target_group_arn = aws_alb_target_group.blue.id
type = "forward"
}
lifecycle {
ignore_changes = [default_action]
}
}
4 changes: 3 additions & 1 deletion tests/terraform_setup/fargate_codedeploy/ecs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ resource "aws_ecs_service" "ecs_service" {
}

depends_on = [aws_alb_listener.front_end_blue, aws_iam_role_policy_attachment.ecs_task_execution_role]

lifecycle {
ignore_changes = [load_balancer, task_definition]
}
}

# https://docs.aws.amazon.com/AmazonECS/latest/developerguide/codedeploy_IAM_role.html
Expand Down
13 changes: 0 additions & 13 deletions tests/terraform_setup/fargate_codedeploy/terraform.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ terraform {
}

provider "aws" {
access_key = var.aws_access_key
secret_key = var.aws_secret_key
token = var.aws_session_token
region = var.aws_region
}

Expand All @@ -29,13 +26,3 @@ resource "aws_ecr_repository" "demo-app-repository" {
name = local.aws_ecr_repository_name
force_delete = true
}

resource "aws_ssm_parameter" "test_container_secret" {
name = var.aws_resource_prefix
type = "String"
value = "test_value"
}

output "arn_secret" {
value = aws_ssm_parameter.test_container_secret.arn
}
6 changes: 1 addition & 5 deletions tests/terraform_setup/fargate_codedeploy/variables.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
variable "aws_access_key" {}
variable "aws_secret_key" {}
variable "aws_account_id" {}
variable "aws_session_token" {}
variable "aws_region" {
description = "AWS region e.g. us-east-1 (Please specify a region supported by the Fargate launch type)"
}
variable "aws_resource_prefix" {
description = "Prefix to be used in the naming of the created AWS resources e.g. ecs-fargate"
}

variable "aws_account_id" {}
variable "az_count" {
default = "2"
}
Expand Down

0 comments on commit 61ee2d9

Please sign in to comment.