Skip to content

Commit

Permalink
WIP: update check_alert_source_usage test
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbloss committed Oct 18, 2024
1 parent ea4349a commit 49d4b74
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 87 deletions.
138 changes: 51 additions & 87 deletions tests/check_alert_source_usage.tftest.hcl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
variables {
check_alert_source_usage = "opslevel_check_alert_source_usage"
resource_name = "opslevel_check_alert_source_usage"

# -- check_alert_source_usage fields --
# required fields
Expand All @@ -13,66 +13,46 @@ variables {

# -- check base fields --
# required fields
category = null
level = null
category = null # sourced from module
level = null # sourced from module
name = "TF Test Check Alert Source Usage"

# optional fields
enable_on = null
enabled = true
filter = null
filter = null # sourced from module
notes = "Notes on Alert Source Usage Check"
owner = null
owner = null # sourced from module
}

run "from_filter_module" {
run "from_data_module" {
command = plan

module {
source = "./data/filter"
plan_options {
target = [
data.opslevel_filters.all,
data.opslevel_rubric_categories.all,
data.opslevel_rubric_levels.all,
data.opslevel_teams.all
]
}
}

run "from_rubric_category_module" {
command = plan

module {
source = "./opslevel_modules/modules/rubric_category"
}
}

run "from_rubric_level_module" {
command = plan

module {
source = "./opslevel_modules/modules/rubric_level"
}
}

run "from_team_module" {
command = plan

module {
source = "./data/team"
source = "./data"
}
}

run "resource_check_alert_source_usage_create_with_all_fields" {

variables {
alert_type = var.alert_type
# other fields from file scoped variables block
alert_name_predicate = var.alert_name_predicate
category = run.from_rubric_category_module.all.rubric_categories[0].id
category = run.from_data_module.first_rubric_category.id
enable_on = var.enable_on
enabled = var.enabled
filter = run.from_filter_module.first.id
level = element([
for lvl in run.from_rubric_level_module.all.rubric_levels :
lvl.id if lvl.index == max(run.from_rubric_level_module.all.rubric_levels[*].index...)
], 0)
name = var.name
notes = var.notes
owner = run.from_team_module.first.id
filter = run.from_data_module.first_filter.id
level = run.from_data_module.max_index_rubric_level.id
notes = var.notes
owner = run.from_data_module.first_team.id
}

module {
Expand All @@ -92,7 +72,7 @@ run "resource_check_alert_source_usage_create_with_all_fields" {
can(opslevel_check_alert_source_usage.this.notes),
can(opslevel_check_alert_source_usage.this.owner),
])
error_message = replace(var.error_unexpected_resource_fields, "TYPE", var.check_alert_source_usage)
error_message = replace(var.error_unexpected_resource_fields, "TYPE", var.resource_name)
}

assert {
Expand All @@ -112,7 +92,7 @@ run "resource_check_alert_source_usage_create_with_all_fields" {

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

assert {
Expand All @@ -127,7 +107,7 @@ run "resource_check_alert_source_usage_create_with_all_fields" {

assert {
condition = opslevel_check_alert_source_usage.this.name == var.name
error_message = replace(var.error_wrong_name, "TYPE", var.check_alert_source_usage)
error_message = replace(var.error_wrong_name, "TYPE", var.resource_name)
}

assert {
Expand All @@ -145,17 +125,15 @@ run "resource_check_alert_source_usage_create_with_all_fields" {
run "resource_check_alert_source_usage_update_unset_optional_fields" {

variables {
# other fields from file scoped variables block
alert_name_predicate = null
category = run.from_rubric_category_module.all.rubric_categories[0].id
category = run.from_data_module.first_rubric_category.id
enable_on = null
enabled = null
filter = null
level = element([
for lvl in run.from_rubric_level_module.all.rubric_levels :
lvl.id if lvl.index == max(run.from_rubric_level_module.all.rubric_levels[*].index...)
], 0)
notes = null
owner = null
level = run.from_data_module.max_index_rubric_level.id
notes = null
owner = null
}

module {
Expand Down Expand Up @@ -195,66 +173,52 @@ run "resource_check_alert_source_usage_update_unset_optional_fields" {

}

run "resource_check_alert_source_usage_update_all_fields" {
run "resource_check_alert_source_usage_unset_optional_fields" {

variables {
alert_type = var.alert_type
alert_name_predicate = var.alert_name_predicate
category = run.from_rubric_category_module.all.rubric_categories[0].id
enable_on = var.enable_on
enabled = var.enabled
filter = run.from_filter_module.first.id
level = element([
for lvl in run.from_rubric_level_module.all.rubric_levels :
lvl.id if lvl.index == max(run.from_rubric_level_module.all.rubric_levels[*].index...)
], 0)
name = var.name
notes = var.notes
owner = run.from_team_module.first.id
# other fields from file scoped variables block
alert_name_predicate = null
category = run.from_data_module.first_rubric_category.id
enable_on = null
enabled = null
filter = null
level = run.from_data_module.max_index_rubric_level.id
notes = null
owner = null
}

module {
source = "./opslevel_modules/modules/check/alert_source_usage"
}

assert {
condition = opslevel_check_alert_source_usage.this.category == var.category
error_message = "wrong category of opslevel_check_alert_source_usage resource"
}

assert {
condition = opslevel_check_alert_source_usage.this.enable_on == var.enable_on
error_message = "wrong enable_on of opslevel_check_alert_source_usage resource"
}

assert {
condition = opslevel_check_alert_source_usage.this.enabled == var.enabled
error_message = "wrong enabled of opslevel_check_alert_source_usage resource"
condition = opslevel_check_alert_source_usage.this.alert_name_predicate == null
error_message = var.error_expected_null_field
}

assert {
condition = opslevel_check_alert_source_usage.this.filter == var.filter
error_message = "wrong filter ID of opslevel_check_alert_source_usage resource"
condition = opslevel_check_alert_source_usage.this.enable_on == null
error_message = var.error_expected_null_field
}

assert {
condition = opslevel_check_alert_source_usage.this.level == var.level
error_message = "wrong level ID of opslevel_check_alert_source_usage resource"
assert { # TODO: fix error message
condition = opslevel_check_alert_source_usage.this.enabled == false
error_message = var.error_expected_null_field
}

assert {
condition = opslevel_check_alert_source_usage.this.name == var.name
error_message = replace(var.error_wrong_name, "TYPE", var.check_alert_source_usage)
condition = opslevel_check_alert_source_usage.this.filter == null
error_message = var.error_expected_null_field
}

assert {
condition = opslevel_check_alert_source_usage.this.notes == var.notes
error_message = "wrong notes of opslevel_check_alert_source_usage resource"
condition = opslevel_check_alert_source_usage.this.notes == null
error_message = var.error_expected_null_field
}

assert {
condition = opslevel_check_alert_source_usage.this.owner == var.owner
error_message = "wrong owner ID of opslevel_check_alert_source_usage resource"
condition = opslevel_check_alert_source_usage.this.owner == null
error_message = var.error_expected_null_field
}

}
4 changes: 4 additions & 0 deletions tests/data/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ data "opslevel_lifecycles" "all" {}

data "opslevel_repositories" "all" {}

data "opslevel_rubric_categories" "all" {}

data "opslevel_rubric_levels" "all" {}

data "opslevel_services" "all" {}

data "opslevel_systems" "all" {}
Expand Down
23 changes: 23 additions & 0 deletions tests/data/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ output "all_repositories" {
value = data.opslevel_repositories.all
}

output "all_rubric_categories" {
value = data.opslevel_rubric_categories.all
}

output "all_rubric_levels" {
value = data.opslevel_rubric_levels.all
}

output "all_services" {
value = data.opslevel_services.all
}
Expand Down Expand Up @@ -50,6 +58,14 @@ output "first_repository" {
value = data.opslevel_repositories.all.repositories[0]
}

output "first_rubric_category" {
value = data.opslevel_rubric_categories.all.rubric_categories[0]
}

output "first_rubric_level" {
value = data.opslevel_rubric_levels.all.rubric_levels[0]
}

output "first_service" {
value = data.opslevel_services.all.services[0]
}
Expand All @@ -69,3 +85,10 @@ output "first_tier" {
output "first_user" {
value = data.opslevel_users.all.users[0]
}

output "max_index_rubric_level" {
value = element([
for lvl in data.opslevel_rubric_levels.all.rubric_levels :
lvl if lvl.index == max(data.opslevel_rubric_levels.all.rubric_levels[*].index...)
], 0)
}

0 comments on commit 49d4b74

Please sign in to comment.