Skip to content

Commit

Permalink
Search path tests (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
pskrbasu authored Nov 29, 2024
1 parent 1b0ed68 commit 5324a5b
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ _Bug fixes_
## v1.0.0 [2024-10-22]
_Whats new_
- `connection` resource to manage credentials. [Documentation](https://powerpipe.io/docs/reference/config-files/connection).
- `database` property has been added to [mod](https://powerpipe.io/docs/powerpipe-hcl/mod).
- `database` property has been added to [mod](https://powerpipe.io/docs/powerpipe-hcl/mod). A database can be a connection reference, connection string, or Pipes workspace to query.

_Deprecations_
- Deprecated `database` CLI arg. See [Setting the Database](https://powerpipe.io/docs/run#selecting-a-database) for the new syntax to set the database.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
variable "database" {
type = connection
default = connection.steampipe.with_search_path_prefix
}

mod "mod_with_db_steampipe_search_path"{
title = "Mod with database defined which is a steampipe connection ref"
description = "This is a simple mod used for testing the database precedence. This mod has a database(steampipe connection) specified in the mod.pp. The steampipe connection has a search_path_prefix defined."
database = var.database
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query "search_path" {
sql = <<-EOQ
SHOW search_path;
EOQ
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
variable "database" {
type = connection
default = connection.steampipe.with_search_path_prefix
}

mod "mod_with_resource_db_steampipe_search_path"{
title = "Mod with database defined which is a steampipe connection ref"
description = "This is a simple mod used for testing the database precedence. This mod has a database(steampipe connection) specified in the mod.pp. The steampipe connection has a search_path_prefix defined. This mod has a query resource with a database defined."
database = var.database
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
query "search_path" {
database = connection.steampipe.with_search_path_prefix_bar
sql = <<-EOQ
SHOW search_path;
EOQ
}
107 changes: 107 additions & 0 deletions tests/acceptance/test_files/database_precedence.bats
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,110 @@ EOF
# check output that the database specified in resource is used
assert_output --partial "Total Employees"
}

# database specified in mod through connection ref, search_path_prefix specified in connection
# so the search_path_prefix specified in connection gets the highest precedence
@test "database specified in mod through connection ref, search_path_prefix specified in connection" {
# add the steampipe connection with search_path_prefix defined
# write the steampipe connection with $MODS_DIR placeholder directly into the config file
cat << EOF > $POWERPIPE_INSTALL_DIR/config/with_search_path_prefix.ppc
connection "steampipe" "with_search_path_prefix" {
host = "localhost"
port = 9193
db = "steampipe"
username = "steampipe"
search_path_prefix = ["foo"]
}
EOF

# checkout the mod with database(connection ref) specified in mod.pp, connection has search_path_prefix defined
cd $MODS_DIR/mod_with_db_steampipe_search_path

# run a powerpipe query to verify that the search_path specified in mod is used
run powerpipe query run query.search_path --output csv
echo $output

# check output that the search_path specified in mod is used
assert_output --partial "foo"
}

# database specified in mod through connection ref, search_path_prefix specified in connection
# database specified by --var in runtime
# so the search_path_prefix specified in --var gets the highest precedence
@test "database specified in mod through connection ref, search_path_prefix specified in connection, database specified by --var in runtime" {
# add the steampipe connection with search_path_prefix defined
# write the steampipe connection with $MODS_DIR placeholder directly into the config file
cat << EOF > $POWERPIPE_INSTALL_DIR/config/with_search_path_prefix_bar.ppc
connection "steampipe" "with_search_path_prefix_bar" {
host = "localhost"
port = 9193
db = "steampipe"
username = "steampipe"
search_path_prefix = ["bar"]
}
EOF

# checkout the mod with database(connection ref) specified in mod.pp, connection has search_path_prefix defined
cd $MODS_DIR/mod_with_db_steampipe_search_path

# run a powerpipe query to verify that the search_path specified in --var is used
run powerpipe query run query.search_path --output csv --var database=connection.steampipe.with_search_path_prefix_bar
echo $output

# check output that the search_path specified in mod is used
assert_output --partial "bar"
}

# database specified in mod through connection ref, search_path_prefix specified in connection
# database specified in query resource
# so the database/connection specified in resource gets the highest precedence
@test "database specified in mod through connection ref, search_path_prefix specified in connection, database specified in query resource" {
# add the steampipe connection with search_path_prefix defined
# write the steampipe connection with $MODS_DIR placeholder directly into the config file
cat << EOF > $POWERPIPE_INSTALL_DIR/config/with_search_path_prefix_bar.ppc
connection "steampipe" "with_search_path_prefix_bar" {
host = "localhost"
port = 9193
db = "steampipe"
username = "steampipe"
search_path_prefix = ["bar"]
}
EOF

# checkout the mod with database(connection ref) specified in mod.pp, connection has search_path_prefix defined
cd $MODS_DIR/mod_with_resource_db_steampipe_search_path

# run a powerpipe query to verify that the search_path/database specified in query is used
run powerpipe query run query.search_path --output csv
echo $output

# check output that the search_path specified in query is used
assert_output --partial "bar"
}

# database specified in mod through connection ref, search_path_prefix specified in connection
# database specified by --database in runtime
# so the search_path_prefix specified in --database gets the highest precedence
@test "database specified in mod through connection ref, search_path_prefix specified in connection, database specified by --database in runtime" {
# add the steampipe connection with search_path_prefix defined
# write the steampipe connection with $MODS_DIR placeholder directly into the config file
cat << EOF > $POWERPIPE_INSTALL_DIR/config/with_search_path_prefix_bar.ppc
connection "steampipe" "with_search_path_prefix_bar" {
host = "localhost"
port = 9193
db = "steampipe"
username = "steampipe"
search_path_prefix = ["bar"]
}
EOF

# checkout the mod with database(connection ref) specified in mod.pp, connection has search_path_prefix defined
cd $MODS_DIR/mod_with_db_steampipe_search_path

# run a powerpipe query to verify that the search_path specified in --var is used
run powerpipe query run query.search_path --output csv --database connection.steampipe.with_search_path_prefix_bar
echo $output

# check output that the search_path specified in mod is used
assert_output --partial "bar"
}

0 comments on commit 5324a5b

Please sign in to comment.