Skip to content

Commit

Permalink
WIP: refactor remaining filter integration tests part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbloss committed Sep 16, 2024
1 parent 5c9cc27 commit d93ca81
Show file tree
Hide file tree
Showing 16 changed files with 852 additions and 1,087 deletions.
2 changes: 2 additions & 0 deletions tests/data/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ data "opslevel_repositories" "all" {}

data "opslevel_services" "all" {}

data "opslevel_systems" "all" {}

data "opslevel_teams" "all" {}

data "opslevel_users" "all" {}
8 changes: 8 additions & 0 deletions tests/data/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ output "all_services" {
value = data.opslevel_services.all
}

output "all_systems" {
value = data.opslevel_systems.all
}

output "all_teams" {
value = data.opslevel_teams.all
}
Expand All @@ -38,6 +42,10 @@ output "first_service" {
value = data.opslevel_services.all.services[0]
}

output "first_system" {
value = data.opslevel_systems.all.systems[0]
}

output "first_team" {
value = data.opslevel_teams.all.teams[0]
}
Expand Down
190 changes: 190 additions & 0 deletions tests/filter_predicate_lifecycle_index.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
variables {
name = "TF Test Filter with lifecycle_index predicate"
predicate_key = "lifecycle_index"
predicate_value = "1"
}

run "resource_filter_with_lifecycle_index_predicate_equals" {

variables {
predicates = [
for predicate_type in var.predicate_types_equals : {
key = var.predicate_key
type = predicate_type,
key_data = null,
value = var.predicate_value
}
]
}

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

assert {
condition = alltrue([
opslevel_filter.this.predicate[0].key == var.predicate_key,
opslevel_filter.this.predicate[1].key == var.predicate_key
])
error_message = format(
"expected predicate keys to all be '%v' got keys '%v'",
var.predicate_key,
[opslevel_filter.this.predicate[0].key, opslevel_filter.this.predicate[1].key]
)
}

assert {
condition = alltrue([
contains(var.predicate_types_equals, opslevel_filter.this.predicate[0].type),
contains(var.predicate_types_equals, opslevel_filter.this.predicate[1].type)
])
error_message = format(
"expected predicate types to be one of '%v' got '%v'",
var.predicate_types_equals,
tolist([opslevel_filter.this.predicate[0].type, opslevel_filter.this.predicate[1].type])
)
}

assert {
condition = alltrue([
opslevel_filter.this.predicate[0].key_data == null,
opslevel_filter.this.predicate[1].key_data == null
])
error_message = var.error_expected_null_field
}

assert {
condition = alltrue([
opslevel_filter.this.predicate[0].value == var.predicate_value,
opslevel_filter.this.predicate[1].value == var.predicate_value
])
error_message = format(
"expected predicate values to all be '%v' got values '%v'",
var.predicate_value,
distinct([opslevel_filter.this.predicate[0].value, opslevel_filter.this.predicate[1].value])
)
}

}

run "resource_filter_with_lifecycle_index_predicate_exists" {

variables {
predicates = [
for predicate_type in var.predicate_types_exists : {
key = var.predicate_key
type = predicate_type,
key_data = null,
value = null
}
]
}

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

assert {
condition = alltrue([
opslevel_filter.this.predicate[0].key == var.predicate_key,
opslevel_filter.this.predicate[1].key == var.predicate_key
])
error_message = format(
"expected predicate keys to all be '%v' got keys '%v'",
var.predicate_key,
[opslevel_filter.this.predicate[0].key, opslevel_filter.this.predicate[1].key]
)
}

assert {
condition = alltrue([
contains(var.predicate_types_exists, opslevel_filter.this.predicate[0].type),
contains(var.predicate_types_exists, opslevel_filter.this.predicate[1].type)
])
error_message = format(
"expected predicate types to be one of '%v' got '%v'",
var.predicate_types_exists,
tolist([opslevel_filter.this.predicate[0].type, opslevel_filter.this.predicate[1].type])
)
}

assert {
condition = alltrue([
opslevel_filter.this.predicate[0].key_data == null,
opslevel_filter.this.predicate[1].key_data == null
])
error_message = var.error_expected_null_field
}

assert {
condition = alltrue([
opslevel_filter.this.predicate[0].value == null,
opslevel_filter.this.predicate[1].value == null
])
error_message = var.error_expected_null_field
}

}

run "resource_filter_with_lifecycle_index_predicate_gt_eq_or_lt_eq" {

variables {
predicates = [
for predicate_type in var.predicate_types_lt_eq_or_gt_eq : {
key = var.predicate_key
type = predicate_type,
key_data = null,
value = var.predicate_value
}
]
}

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

assert {
condition = alltrue([
opslevel_filter.this.predicate[0].key == var.predicate_key,
opslevel_filter.this.predicate[1].key == var.predicate_key
])
error_message = format(
"expected predicate keys to all be '%v' got keys '%v'",
var.predicate_key,
[opslevel_filter.this.predicate[0].key, opslevel_filter.this.predicate[1].key]
)
}

assert {
condition = alltrue([
contains(var.predicate_types_lt_eq_or_gt_eq, opslevel_filter.this.predicate[0].type),
contains(var.predicate_types_lt_eq_or_gt_eq, opslevel_filter.this.predicate[1].type)
])
error_message = format(
"expected predicate types to be one of '%v' got '%v'",
var.predicate_types_lt_eq_or_gt_eq,
tolist([opslevel_filter.this.predicate[0].type, opslevel_filter.this.predicate[1].type])
)
}

assert {
condition = alltrue([
opslevel_filter.this.predicate[0].key_data == null,
opslevel_filter.this.predicate[1].key_data == null
])
error_message = var.error_expected_null_field
}

assert {
condition = alltrue([
opslevel_filter.this.predicate[0].value == var.predicate_value,
opslevel_filter.this.predicate[1].value == var.predicate_value
])
error_message = format(
"expected predicate values to all be '%v' got values '%v'",
var.predicate_value,
distinct([opslevel_filter.this.predicate[0].value, opslevel_filter.this.predicate[1].value])
)
}

}
138 changes: 138 additions & 0 deletions tests/filter_predicate_owner_id.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
variables {
name = "TF Test Filter with owner_id predicate"
predicate_key = "owner_id"
}

run "from_data_module" {
command = plan
plan_options {
target = [
data.opslevel_teams.all
]
}

module {
source = "./data"
}
}

run "resource_filter_with_owner_id_predicate_equals" {

variables {
predicates = [
for predicate_type in var.predicate_types_equals : {
key = var.predicate_key
type = predicate_type,
key_data = null,
value = run.from_data_module.first_team.id
}
]
}

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

assert {
condition = alltrue([
opslevel_filter.this.predicate[0].key == var.predicate_key,
opslevel_filter.this.predicate[1].key == var.predicate_key
])
error_message = format(
"expected predicate keys to all be '%v' got keys '%v'",
var.predicate_key,
[opslevel_filter.this.predicate[0].key, opslevel_filter.this.predicate[1].key]
)
}

assert {
condition = alltrue([
contains(var.predicate_types_equals, opslevel_filter.this.predicate[0].type),
contains(var.predicate_types_equals, opslevel_filter.this.predicate[1].type)
])
error_message = format(
"expected predicate types to be one of '%v' got '%v'",
var.predicate_types_equals,
tolist([opslevel_filter.this.predicate[0].type, opslevel_filter.this.predicate[1].type])
)
}

assert {
condition = alltrue([
opslevel_filter.this.predicate[0].key_data == null,
opslevel_filter.this.predicate[1].key_data == null
])
error_message = var.error_expected_null_field
}

assert {
condition = alltrue([
opslevel_filter.this.predicate[0].value == run.from_data_module.first_team.id,
opslevel_filter.this.predicate[1].value == run.from_data_module.first_team.id
])
error_message = format(
"expected predicate values to all be '%v' got values '%v'",
run.from_data_module.first_team.id,
distinct([opslevel_filter.this.predicate[0].value, opslevel_filter.this.predicate[1].value])
)
}

}

run "resource_filter_with_owner_id_predicate_exists" {

variables {
predicates = [
for predicate_type in var.predicate_types_exists : {
key = var.predicate_key
type = predicate_type,
key_data = null,
value = null
}
]
}

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

assert {
condition = alltrue([
opslevel_filter.this.predicate[0].key == var.predicate_key,
opslevel_filter.this.predicate[1].key == var.predicate_key
])
error_message = format(
"expected predicate keys to all be '%v' got keys '%v'",
var.predicate_key,
[opslevel_filter.this.predicate[0].key, opslevel_filter.this.predicate[1].key]
)
}

assert {
condition = alltrue([
contains(var.predicate_types_exists, opslevel_filter.this.predicate[0].type),
contains(var.predicate_types_exists, opslevel_filter.this.predicate[1].type)
])
error_message = format(
"expected predicate types to be one of '%v' got '%v'",
var.predicate_types_exists,
tolist([opslevel_filter.this.predicate[0].type, opslevel_filter.this.predicate[1].type])
)
}

assert {
condition = alltrue([
opslevel_filter.this.predicate[0].key_data == null,
opslevel_filter.this.predicate[1].key_data == null
])
error_message = var.error_expected_null_field
}

assert {
condition = alltrue([
opslevel_filter.this.predicate[0].value == null,
opslevel_filter.this.predicate[1].value == null
])
error_message = var.error_expected_null_field
}
}
Loading

0 comments on commit d93ca81

Please sign in to comment.