Skip to content

Commit

Permalink
update aws, gcp, azure integration tests, add test debug task (#510)
Browse files Browse the repository at this point in the history
* WIP: update aws integration test, add test debug task

* WIP: update google_cloud integration test, rm unused test files

* testing integrations expect correct errors

* remove unused azure integration test files
  • Loading branch information
davidbloss authored Oct 17, 2024
1 parent 03f4b94 commit da7d41c
Show file tree
Hide file tree
Showing 19 changed files with 493 additions and 694 deletions.
6 changes: 6 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ tasks:
cmds:
- task: debug:do-debug-plan

debug-test:
desc: After debug-start and debug-attach, run "terraform test" with 'TF_REATTACH_PROVIDERS'
aliases: ["dbg-test"]
cmds:
- task: debug:do-debug-test

terraform-clean:
desc: Completely wipe terraform state, terraform generated files, and local OpsLevel provider binaries
aliases: ["clean"]
Expand Down
2 changes: 1 addition & 1 deletion opslevel/resource_opslevel_integration_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (r *IntegrationAwsResource) Read(ctx context.Context, req resource.ReadRequ
return
}

awsIntegration, err := r.client.GetIntegration(opslevel.ID(stateModel.Id.ValueString()))
awsIntegration, err := r.client.GetIntegration(asID(stateModel.Id))
if err != nil {
if (awsIntegration == nil || awsIntegration.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
Expand Down
2 changes: 1 addition & 1 deletion opslevel/resource_opslevel_integration_azure_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (r *IntegrationAzureResourcesResource) Read(ctx context.Context, req resour
return
}

azureResourcesIntegration, err := r.client.GetIntegration(opslevel.ID(stateModel.Id.ValueString()))
azureResourcesIntegration, err := r.client.GetIntegration(asID(stateModel.Id))
if err != nil {
if (azureResourcesIntegration == nil || azureResourcesIntegration.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
Expand Down
16 changes: 7 additions & 9 deletions taskfiles/Debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,24 @@ tasks:
- echo "Set breakpoints once connected, then run 'task debug-plan' in a new shell"
- dlv connect 127.0.0.1:{{.LOCAL_PORT}}

do-debug-apply:
desc: After debug-start and debug-attach, run "terraform plan" with 'TF_REATTACH_PROVIDERS'
aliases: ["dbg-apply"]
do-debug-*:
desc: After debug-start and debug-attach, run "terraform {{index .MATCH 0}}" with 'TF_REATTACH_PROVIDERS'
vars:
TF_REATTACH_PROVIDERS:
sh: grep 'TF_REATTACH_PROVIDERS=' {{.DEBUG_LOG}} | cut -d'=' -f2-
interactive: true
cmds:
- TF_REATTACH_PROVIDERS={{.TF_REATTACH_PROVIDERS}} terraform -chdir="{{.ROOT_DIR}}/{{.WORKSPACE_DIR}}" apply
- echo "After 'terraform {{index .MATCH 0}}' is started, return to shell with interactive delve session"
- TF_REATTACH_PROVIDERS={{.TF_REATTACH_PROVIDERS}} terraform -chdir="{{.ROOT_DIR}}/{{.WORKSPACE_DIR}}" {{index .MATCH 0}}

do-debug-plan:
desc: After debug-start and debug-attach, run "terraform plan" with 'TF_REATTACH_PROVIDERS'
aliases: ["dbg-plan"]
do-debug-test:
desc: After debug-start and debug-attach, run "terraform test" with 'TF_REATTACH_PROVIDERS'
vars:
TF_REATTACH_PROVIDERS:
sh: grep 'TF_REATTACH_PROVIDERS=' {{.DEBUG_LOG}} | cut -d'=' -f2-
interactive: true
cmds:
- echo "After 'terraform plan' is started, return to shell with interactive delve session"
- TF_REATTACH_PROVIDERS={{.TF_REATTACH_PROVIDERS}} terraform -chdir="{{.ROOT_DIR}}/{{.WORKSPACE_DIR}}" plan
- TF_REATTACH_PROVIDERS={{.TF_REATTACH_PROVIDERS}} terraform -chdir="{{.ROOT_DIR}}/{{.TEST_DIR}}" test -var-file=test.tfvars {{.CLI_ARGS}}

do-debug-start:
desc: First, start headless debug
Expand Down
286 changes: 286 additions & 0 deletions tests/integration_aws.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
variables {
resource_name = "opslevel_integration_aws"

# required fields
external_id = "194c7dfc-3a3f-4b0a-b898-578ce7e8f6dc"
iam_role = "arn:aws:iam::994866125780:user/opslevel-test"
name = "TF Test AWS Integration"

# optional fields
ownership_tag_overrides = false
ownership_tag_keys = ["one", "two", "three", "four", "five"]
region_override = ["eu-west-1", "us-east-1"]

# default values - computed from API
default_ownership_tag_keys = tolist(["owner"])
default_ownership_tag_overrides = true
}

run "resource_integration_aws_create_with_all_fields" {

module {
source = "./opslevel_modules/modules/integration/aws"
}

assert {
condition = alltrue([
can(opslevel_integration_aws.this.external_id),
can(opslevel_integration_aws.this.iam_role),
can(opslevel_integration_aws.this.id),
can(opslevel_integration_aws.this.ownership_tag_keys),
can(opslevel_integration_aws.this.ownership_tag_overrides),
can(opslevel_integration_aws.this.name),
can(opslevel_integration_aws.this.region_override),
])
error_message = replace(var.error_unexpected_resource_fields, "TYPE", var.resource_name)
}

assert {
condition = opslevel_integration_aws.this.external_id == var.external_id
error_message = format(
"expected '%v' but got '%v'",
var.external_id,
opslevel_integration_aws.this.external_id,
)
}

assert {
condition = opslevel_integration_aws.this.iam_role == var.iam_role
error_message = format(
"expected '%v' but got '%v'",
var.iam_role,
opslevel_integration_aws.this.iam_role,
)
}

assert {
condition = startswith(opslevel_integration_aws.this.id, var.id_prefix)
error_message = replace(var.error_wrong_id, "TYPE", var.resource_name)
}

assert {
condition = opslevel_integration_aws.this.name == var.name
error_message = format(
"expected '%v' but got '%v'",
var.name,
opslevel_integration_aws.this.name,
)
}

assert {
condition = opslevel_integration_aws.this.ownership_tag_keys == var.ownership_tag_keys
error_message = format(
"expected '%v' but got '%v'",
var.ownership_tag_keys,
opslevel_integration_aws.this.ownership_tag_keys,
)
}

assert {
condition = opslevel_integration_aws.this.ownership_tag_overrides == var.ownership_tag_overrides
error_message = format(
"expected '%v' but got '%v'",
var.ownership_tag_overrides,
opslevel_integration_aws.this.ownership_tag_overrides,
)
}

assert {
condition = opslevel_integration_aws.this.region_override == var.region_override
error_message = format(
"expected '%v' but got '%v'",
var.region_override,
opslevel_integration_aws.this.region_override,
)
}

}

run "resource_integration_aws_unset_optional_fields" {

variables {
ownership_tag_keys = null
ownership_tag_overrides = null
region_override = null
}

module {
source = "./opslevel_modules/modules/integration/aws"
}

assert {
condition = opslevel_integration_aws.this.ownership_tag_keys == var.default_ownership_tag_keys
error_message = format(
"expected default '%v' but got '%v'",
var.default_ownership_tag_keys,
opslevel_integration_aws.this.ownership_tag_keys,
)
}

assert {
condition = opslevel_integration_aws.this.ownership_tag_overrides == var.default_ownership_tag_overrides
error_message = format(
"expected default '%v' but got '%v'",
var.default_ownership_tag_overrides,
opslevel_integration_aws.this.ownership_tag_overrides,
)
}

assert {
condition = opslevel_integration_aws.this.region_override == null
error_message = var.error_expected_null_field
}

}

run "delete_aws_integration_outside_of_terraform" {

variables {
resource_id = run.resource_integration_aws_create_with_all_fields.this.id
resource_type = "integration"
}

module {
source = "./provisioner"
}
}

run "resource_integration_aws_create_with_required_fields" {

variables {
ownership_tag_overrides = null
ownership_tag_keys = null
region_override = null
}

module {
source = "./opslevel_modules/modules/integration/aws"
}

assert {
condition = run.resource_integration_aws_create_with_all_fields.this.id != opslevel_integration_aws.this.id
error_message = format(
"expected old id '%v' to be different from new id '%v'",
run.resource_integration_aws_create_with_all_fields.this.id,
opslevel_integration_aws.this.id,
)
}

assert {
condition = opslevel_integration_aws.this.external_id == var.external_id
error_message = format(
"expected '%v' but got '%v'",
var.external_id,
opslevel_integration_aws.this.external_id,
)
}

assert {
condition = opslevel_integration_aws.this.iam_role == var.iam_role
error_message = format(
"expected '%v' but got '%v'",
var.iam_role,
opslevel_integration_aws.this.iam_role,
)
}

assert {
condition = startswith(opslevel_integration_aws.this.id, var.id_prefix)
error_message = replace(var.error_wrong_id, "TYPE", var.resource_name)
}

assert {
condition = opslevel_integration_aws.this.name == var.name
error_message = format(
"expected '%v' but got '%v'",
var.name,
opslevel_integration_aws.this.name,
)
}

assert {
condition = opslevel_integration_aws.this.ownership_tag_keys == var.default_ownership_tag_keys
error_message = format(
"expected '%v' but got '%v'",
var.default_ownership_tag_keys,
opslevel_integration_aws.this.ownership_tag_keys,
)
}

assert {
condition = opslevel_integration_aws.this.ownership_tag_overrides == var.default_ownership_tag_overrides
error_message = format(
"expected '%v' but got '%v'",
var.default_ownership_tag_overrides,
opslevel_integration_aws.this.ownership_tag_overrides,
)
}

assert {
condition = opslevel_integration_aws.this.region_override == null
error_message = var.error_expected_null_field
}

}

run "resource_integration_aws_set_all_fields" {

module {
source = "./opslevel_modules/modules/integration/aws"
}

assert {
condition = opslevel_integration_aws.this.external_id == var.external_id
error_message = format(
"expected '%v' but got '%v'",
var.external_id,
opslevel_integration_aws.this.external_id,
)
}

assert {
condition = opslevel_integration_aws.this.iam_role == var.iam_role
error_message = format(
"expected '%v' but got '%v'",
var.iam_role,
opslevel_integration_aws.this.iam_role,
)
}

assert {
condition = opslevel_integration_aws.this.name == var.name
error_message = format(
"expected '%v' but got '%v'",
var.name,
opslevel_integration_aws.this.name,
)
}

assert {
condition = opslevel_integration_aws.this.ownership_tag_keys == var.ownership_tag_keys
error_message = format(
"expected '%v' but got '%v'",
var.ownership_tag_keys,
opslevel_integration_aws.this.ownership_tag_keys,
)
}

assert {
condition = opslevel_integration_aws.this.ownership_tag_overrides == var.ownership_tag_overrides
error_message = format(
"expected '%v' but got '%v'",
var.ownership_tag_overrides,
opslevel_integration_aws.this.ownership_tag_overrides,
)
}

assert {
condition = opslevel_integration_aws.this.region_override == var.region_override
error_message = format(
"expected '%v' but got '%v'",
var.region_override,
opslevel_integration_aws.this.region_override,
)
}

}
Loading

0 comments on commit da7d41c

Please sign in to comment.