From fbb8b4ef83014a690d3449b8cdfe00eb0a8ef0d1 Mon Sep 17 00:00:00 2001 From: Andrii Romanenko Date: Thu, 9 Sep 2021 14:31:30 +0300 Subject: [PATCH] Ec2.images integration test (#184) * ec2 images integration test implementation --- .github/workflows/lint.yml | 1 + CHANGELOG.md | 7 +- go.mod | 72 ++++++++ .../integration_tests/aws_ec2_images_test.go | 78 ++++++++ .../integration_tests/infra/aws_ec2_images.tf | 170 ++++++++++++++++++ 5 files changed, 327 insertions(+), 1 deletion(-) create mode 100644 resources/integration_tests/aws_ec2_images_test.go create mode 100644 resources/integration_tests/infra/aws_ec2_images.tf diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 6ddbc7c6c..b231c6174 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -17,6 +17,7 @@ jobs: uses: golangci/golangci-lint-action@v2 with: version: v1.42.1 + args: --timeout 5m docs: name: docs verification diff --git a/CHANGELOG.md b/CHANGELOG.md index 3add61a45..053038050 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,12 +4,17 @@ All notable changes to this provider will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [unreleased] - 2020-08-19 + +## [unreleased] ###### SDK Version: v0.4.3 ### :spider: Fixed * Fixed kms key fetching when provider tried to fetch aws managed keys rotation properties [#168](https://github.com/cloudquery/cq-provider-aws/pull/168) +### :rocket: Added +* Added integration test for ec2.images resource [#184](https://github.com/cloudquery/cq-provider-aws/pull/184) + + ## [v0.5.10] - 2020-08-19 ###### SDK Version: 0.3.4 diff --git a/go.mod b/go.mod index a861e8401..2b5bb1619 100644 --- a/go.mod +++ b/go.mod @@ -55,3 +55,75 @@ require ( github.com/mitchellh/mapstructure v1.1.2 github.com/spf13/cast v1.4.0 ) + +require ( + github.com/agext/levenshtein v1.2.3 // indirect + github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect + github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.0.6 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.0.4 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.0.6 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.2.2 // indirect + github.com/aws/aws-sdk-go-v2/service/sso v1.1.5 // indirect + github.com/creasty/defaults v1.5.1 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/doug-martin/goqu/v9 v9.13.0 // indirect + github.com/fatih/color v1.10.0 // indirect + github.com/georgysavva/scany v0.2.8 // indirect + github.com/go-test/deep v1.0.7 // indirect + github.com/gofrs/uuid v4.0.0+incompatible // indirect + github.com/golang-migrate/migrate/v4 v4.14.0 // indirect + github.com/golang/protobuf v1.5.2 // indirect + github.com/google/go-cmp v0.5.5 // indirect + github.com/google/uuid v1.2.0 // indirect + github.com/hashicorp/errwrap v1.0.0 // indirect + github.com/hashicorp/go-multierror v1.1.0 // indirect + github.com/hashicorp/go-plugin v1.4.1 // indirect + github.com/hashicorp/go-version v1.3.0 // indirect + github.com/hashicorp/hcl/v2 v2.10.0 // indirect + github.com/hashicorp/terraform-exec v0.13.3 // indirect + github.com/hashicorp/terraform-json v0.10.0 // indirect + github.com/hashicorp/yamux v0.0.0-20210316155119-a95892c5f864 // indirect + github.com/huandu/go-sqlbuilder v1.12.1 // indirect + github.com/huandu/xstrings v1.3.2 // indirect + github.com/iancoleman/strcase v0.1.3 // indirect + github.com/jackc/chunkreader/v2 v2.0.1 // indirect + github.com/jackc/pgconn v1.8.1 // indirect + github.com/jackc/pgio v1.0.0 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgproto3/v2 v2.0.7 // indirect + github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect + github.com/jackc/pgtype v1.7.0 // indirect + github.com/jackc/puddle v1.1.3 // indirect + github.com/jmespath/go-jmespath v0.4.0 // indirect + github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect + github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect + github.com/lib/pq v1.10.1 // indirect + github.com/mattn/go-colorable v0.1.8 // indirect + github.com/mattn/go-isatty v0.0.12 // indirect + github.com/mitchellh/go-testing-interface v1.14.1 // indirect + github.com/mitchellh/go-wordwrap v1.0.1 // indirect + github.com/mitchellh/hashstructure v1.1.0 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.1 // indirect + github.com/oklog/run v1.1.0 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/spf13/afero v1.6.0 // indirect + github.com/stretchr/objx v0.3.0 // indirect + github.com/stretchr/testify v1.7.0 // indirect + github.com/thoas/go-funk v0.8.1-0.20210502090430-efae847b30ab // indirect + github.com/tmccombs/hcl2json v0.3.3 // indirect + github.com/vmihailenco/msgpack/v5 v5.3.4 // indirect + github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect + github.com/xo/dburl v0.8.4 // indirect + github.com/zclconf/go-cty v1.9.0 // indirect + golang.org/x/crypto v0.0.0-20210506145944-38f3c27a63bf // indirect + golang.org/x/net v0.0.0-20210510120150-4163338589ed // indirect + golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect + golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect + golang.org/x/text v0.3.6 // indirect + google.golang.org/genproto v0.0.0-20201030142918-24207fddd1c3 // indirect + google.golang.org/grpc v1.40.0 // indirect + google.golang.org/protobuf v1.26.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect +) diff --git a/resources/integration_tests/aws_ec2_images_test.go b/resources/integration_tests/aws_ec2_images_test.go new file mode 100644 index 000000000..c423c12ea --- /dev/null +++ b/resources/integration_tests/aws_ec2_images_test.go @@ -0,0 +1,78 @@ +package integration_tests + +import ( + "fmt" + "testing" + + "github.com/Masterminds/squirrel" + "github.com/cloudquery/cq-provider-aws/resources" + providertest "github.com/cloudquery/cq-provider-sdk/provider/testing" +) + +func TestIntegrationEc2Images(t *testing.T) { + awsTestIntegrationHelper(t, resources.Ec2Images(), nil, func(res *providertest.ResourceIntegrationTestData) providertest.ResourceIntegrationVerification { + return providertest.ResourceIntegrationVerification{ + Name: resources.Ec2Images().Name, + Filter: func(sq squirrel.SelectBuilder, res *providertest.ResourceIntegrationTestData) squirrel.SelectBuilder { + return sq.Where( + squirrel.Like{"name": fmt.Sprintf("aws-ec2-images-image-%s%s%%", res.Prefix, res.Suffix)}) + }, + ExpectedValues: []providertest.ExpectedValue{ + { + Count: 1, + Data: map[string]interface{}{ + "image_type": "machine", + "ena_support": true, + "hypervisor": "xen", + "platform_details": "Linux/UNIX", + "public": false, + "root_device_name": "/dev/xvda", + "root_device_type": "ebs", + "sriov_net_support": "simple", + "usage_operation": "RunInstances", + "virtualization_type": "hvm", + "tags": map[string]interface{}{ + "CreatedBy": "EC2 Image Builder", + "CostCenter": "IT", + "Ec2ImageBuilderArn": "arn:aws:imagebuilder:us-east-1:704956590351:image/ec2-images-recipe-awsec2imageswindowsfifl5fe/0.1.1/1", + }, + }, + }, + }, + Relations: []*providertest.ResourceIntegrationVerification{ + { + Name: "aws_ec2_image_block_device_mappings", + ForeignKeyName: "image_cq_id", + ExpectedValues: []providertest.ExpectedValue{{ + Count: 1, + Data: map[string]interface{}{ + "device_name": "/dev/xvda", + "ebs_delete_on_termination": true, + "ebs_encrypted": false, + "ebs_iops": float64(0), + "ebs_throughput": float64(0), + "ebs_volume_size": float64(8), + "ebs_volume_type": "gp2", + }, + }}, + }, + { + Name: "aws_ec2_image_block_device_mappings", + ForeignKeyName: "image_cq_id", + ExpectedValues: []providertest.ExpectedValue{{ + Count: 1, + Data: map[string]interface{}{ + "device_name": "/dev/xvdb", + "ebs_delete_on_termination": true, + "ebs_encrypted": false, + "ebs_iops": float64(0), + "ebs_throughput": float64(0), + "ebs_volume_size": float64(20), + "ebs_volume_type": "gp2", + }, + }}, + }, + }, + } + }) +} diff --git a/resources/integration_tests/infra/aws_ec2_images.tf b/resources/integration_tests/infra/aws_ec2_images.tf new file mode 100644 index 000000000..866b61b2a --- /dev/null +++ b/resources/integration_tests/infra/aws_ec2_images.tf @@ -0,0 +1,170 @@ +resource "aws_imagebuilder_image" "aws_ec2_images_image" { + distribution_configuration_arn = aws_imagebuilder_distribution_configuration.aws_ec2_images_distribution_configuration.arn + image_recipe_arn = aws_imagebuilder_image_recipe.aws_ec2_images_image_recipe.arn + infrastructure_configuration_arn = aws_imagebuilder_infrastructure_configuration.aws_ec2_images_infrastructure_configuration.arn + + image_tests_configuration { + image_tests_enabled = false + } + + tags = { + stage = "test" + } +} + +resource "aws_imagebuilder_distribution_configuration" "aws_ec2_images_distribution_configuration" { + name = "ec2-images-dc-${var.test_prefix}${var.test_suffix}" + + distribution { + + ami_distribution_configuration { + + ami_tags = { + CostCenter = "IT" + } + + name = "aws-ec2-images-image-${var.test_prefix}${var.test_suffix}-{{ imagebuilder:buildDate }}" + + launch_permission { + user_ids = [ + "123456789012"] + } + } + + region = data.aws_region.current.name + } +} + +resource "aws_imagebuilder_image_recipe" "aws_ec2_images_image_recipe" { + block_device_mapping { + device_name = "/dev/xvdb" + + ebs { + delete_on_termination = true + volume_size = 20 + volume_type = "gp2" + } + } + + component { + component_arn = aws_imagebuilder_component.aws_ec2_images_hello_world.arn + } + + name = "ec2-images-recipe-${var.test_prefix}${var.test_suffix}" + parent_image = "arn:${data.aws_partition.current.partition}:imagebuilder:${data.aws_region.current.name}:aws:image/amazon-linux-2-x86/x.x.x" + version = "0.1.1" + +} + +data "aws_partition" "current" {} + +resource "aws_imagebuilder_infrastructure_configuration" "aws_ec2_images_infrastructure_configuration" { + description = "ec2-images-ic-${var.test_prefix}${var.test_suffix}" + instance_profile_name = aws_iam_instance_profile.aws_ec2_images_instance_profile.name + instance_types = [ + "t2.nano", + "t3.micro"] + name = "ec2-images-ic-${var.test_prefix}${var.test_suffix}" + terminate_instance_on_failure = true + + logging { + s3_logs { + s3_bucket_name = aws_s3_bucket.aws_ec2_images_bucket.bucket + s3_key_prefix = "logs" + } + } + + tags = { + foo = "bar" + } +} + +resource "aws_iam_instance_profile" "aws_ec2_images_instance_profile" { + name = "ec2-images-ip-${var.test_prefix}${var.test_suffix}" + role = aws_iam_role.aws_ec2_images_role.name +} + +resource "aws_iam_role" "aws_ec2_images_role" { + name = "ec2-images-role-${var.test_prefix}${var.test_suffix}" + managed_policy_arns = [ + data.aws_iam_policy.aws_ec2_images_instance_profile_for_imagebuilder.arn, + data.aws_iam_policy.aws_ec2_images_ecr_containers.arn, + data.aws_iam_policy.aws_ec2_images_ssm.arn] + inline_policy { + name = "s3_logging" + policy = data.aws_iam_policy_document.aws_ec2_images_inline_policy.json + } + + assume_role_policy = data.aws_iam_policy_document.aws_ec2_images_instance-assume-role-policy.json +} + +resource "aws_s3_bucket" "aws_ec2_images_bucket" { + bucket = "ec2-images-logs-${var.test_prefix}${var.test_suffix}" + acl = "private" + + force_destroy = true + + tags = { + Name = "My bucket ${var.test_prefix}${var.test_suffix}" + Environment = "test" + } +} + +resource "aws_imagebuilder_component" "aws_ec2_images_hello_world" { + data = yamlencode({ + phases = [ + { + name = "build" + steps = [ + { + action = "ExecuteBash" + inputs = { + commands = [ + "echo 'hello world'"] + } + name = "example-${var.test_prefix}${var.test_suffix}" + onFailure = "Continue" + }] + }] + schemaVersion = 1.0 + }) + name = "hello_world_{var.test_prefix}${var.test_suffix}" + platform = "Linux" + version = "1.0.0" +} + +data "aws_iam_policy_document" "aws_ec2_images_instance-assume-role-policy" { + statement { + actions = [ + "sts:AssumeRole"] + + principals { + type = "Service" + identifiers = [ + "ec2.amazonaws.com"] + } + } +} + + +data "aws_iam_policy_document" "aws_ec2_images_inline_policy" { + statement { + actions = [ + "s3:PutObject"] + resources = [ + "${aws_s3_bucket.aws_ec2_images_bucket.arn}/*", + ] + } +} + +data "aws_iam_policy" "aws_ec2_images_instance_profile_for_imagebuilder" { + name = "EC2InstanceProfileForImageBuilder" +} + +data "aws_iam_policy" "aws_ec2_images_ecr_containers" { + name = "EC2InstanceProfileForImageBuilderECRContainerBuilds" +} + +data "aws_iam_policy" "aws_ec2_images_ssm" { + name = "AmazonSSMManagedInstanceCore" +}