Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding {withr} to improve tests that involve envvars/files. #69

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ as they are incompatible with CRAN (#64)
* Removing kntir engine due to many render edge cases not being solvable
* Adding shortcuts for REPL under addins
* Added `db_context_command_run_and_wait`
* Adjusted tests to use `withr` (#68)

# brickster 0.2.4

Expand Down
2 changes: 1 addition & 1 deletion cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## R CMD check results

0 errors | 0 warnings | 1 note
0 errors | 0 warnings | 0 notes

* This is a new release.
1 change: 1 addition & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
library(testthat)
library(brickster)
library(withr)

test_check("brickster")
172 changes: 90 additions & 82 deletions tests/testthat/test-auth.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
existing_host <- Sys.getenv("DATABRICKS_HOST")
existing_token <- Sys.getenv("DATABRICKS_TOKEN")
existing_wsid <- Sys.getenv("DATABRICKS_WSID")

test_that("auth functions - baseline behaviour", {

host <- "some_url"
token <- "dapi123"
wsid <- "123"

# set values
Sys.setenv("DATABRICKS_HOST" = host)
Sys.setenv("DATABRICKS_TOKEN" = token)
Sys.setenv("DATABRICKS_WSID" = wsid)
# set values temporarily
withr::local_envvar(
DATABRICKS_WSID = wsid,
DATABRICKS_HOST = host,
DATABRICKS_TOKEN = token
)

# read env var function should behave
expect_identical(read_env_var("host"), host)
Expand All @@ -25,8 +23,10 @@ test_that("auth functions - baseline behaviour", {
expect_identical(db_wsid(), wsid)

# when not specified should error
Sys.setenv("DATABRICKS_HOST" = "")
expect_error(db_host())
withr::with_envvar(
new = c(DATABRICKS_HOST = ""),
expect_error(db_host())
)

expect_identical(
db_host(id = "mock", prefix = "dev-"),
Expand Down Expand Up @@ -65,14 +65,15 @@ test_that("auth functions - switching profile", {
token_prod <- "dapi321"
wsid_prod <- "321"

# set values
Sys.setenv("DATABRICKS_HOST" = host)
Sys.setenv("DATABRICKS_TOKEN" = token)
Sys.setenv("DATABRICKS_WSID" = wsid)

Sys.setenv("DATABRICKS_HOST_PROD" = host_prod)
Sys.setenv("DATABRICKS_TOKEN_PROD" = token_prod)
Sys.setenv("DATABRICKS_WSID_PROD" = wsid_prod)
# set values temporarily
withr::local_envvar(
DATABRICKS_WSID = wsid,
DATABRICKS_HOST = host,
DATABRICKS_TOKEN = token,
DATABRICKS_HOST_PROD = host_prod,
DATABRICKS_TOKEN_PROD = token_prod,
DATABRICKS_WSID_PROD = wsid_prod
)

# read env var function should behave
expect_identical(read_env_var("host", NULL), host)
Expand Down Expand Up @@ -113,50 +114,51 @@ test_that("auth functions - switching profile", {

test_that("auth functions - reading .databrickscfg", {

options(use_databrickscfg = TRUE)

# where .databrickscfg should be:
if (.Platform$OS.type == "windows") {
home_dir <- Sys.getenv("USERPROFILE")
} else {
home_dir <- Sys.getenv("HOME")
}
dbcfg_path <- file.path(home_dir, ".databrickscfg")

if (file.exists(dbcfg_path)) {
# using read_databrickscfg directly
token <- expect_no_condition(read_databrickscfg("token", profile = NULL))
host <- expect_no_condition(read_databrickscfg("host", profile = NULL))
wsid <- expect_no_condition(read_databrickscfg("wsid", profile = NULL))
expect_true(is.character(token))
expect_true(is.character(host))
expect_true(is.character(wsid))
# using read_databrickscfg directly
token <- expect_no_condition(read_databrickscfg("token", profile = "DEFAULT"))
host <- expect_no_condition(read_databrickscfg("host", profile = "DEFAULT"))
wsid <- expect_no_condition(read_databrickscfg("wsid", profile = "DEFAULT"))
expect_true(is.character(token))
expect_true(is.character(host))
expect_true(is.character(wsid))
# via wrappers
token_w <- db_token(profile = "DEFAULT")
host_w <- db_host(profile = "DEFAULT")
wsid_w <- db_wsid(profile = "DEFAULT")
expect_identical(token, token_w)
expect_identical(host, host_w)
expect_identical(wsid, wsid_w)
# via wrappers
token_w <- db_token(profile = NULL)
host_w <- db_host(profile = NULL)
wsid_w <- db_wsid(profile = NULL)
expect_identical(token, token_w)
expect_identical(host, host_w)
expect_identical(wsid, wsid_w)
} else {
expect_error(read_databrickscfg())
}

options(use_databrickscfg = FALSE)
withr::local_options(use_databrickscfg = TRUE)
withr::local_envvar(DATABRICKS_CONFIG_FILE = "databricks.cfg")
withr::local_file("databricks.cfg", {
writeLines(
c(
'[DEFAULT]',
'host = some-host',
'token = some-token',
'wsid = 123456'
),
"databricks.cfg"
)
})

# using read_databrickscfg directly
token <- expect_no_condition(read_databrickscfg("token", profile = NULL))
host <- expect_no_condition(read_databrickscfg("host", profile = NULL))
wsid <- expect_no_condition(read_databrickscfg("wsid", profile = NULL))
expect_true(is.character(token))
expect_true(is.character(host))
expect_true(is.character(wsid))

# using read_databrickscfg directly
token <- expect_no_condition(read_databrickscfg("token", profile = "DEFAULT"))
host <- expect_no_condition(read_databrickscfg("host", profile = "DEFAULT"))
wsid <- expect_no_condition(read_databrickscfg("wsid", profile = "DEFAULT"))
expect_true(is.character(token))
expect_true(is.character(host))
expect_true(is.character(wsid))

# via wrappers
token_w <- db_token(profile = "DEFAULT")
host_w <- db_host(profile = "DEFAULT")
wsid_w <- db_wsid(profile = "DEFAULT")
expect_identical(token, token_w)
expect_identical(host, host_w)
expect_identical(wsid, wsid_w)

# via wrappers
token_w <- db_token(profile = NULL)
host_w <- db_host(profile = NULL)
wsid_w <- db_wsid(profile = NULL)
expect_identical(token, token_w)
expect_identical(host, host_w)
expect_identical(wsid, wsid_w)

})

Expand Down Expand Up @@ -192,9 +194,13 @@ test_that("auth functions - host handling", {
)

purrr::iwalk(hostname_mapping, function(output, input) {
Sys.setenv("DATABRICKS_HOST" = input)
expect_no_error(db_host())
expect_identical(db_host(), output)
withr::with_envvar(
new = c(DATABRICKS_HOST = input),
{
expect_no_error(db_host())
expect_identical(db_host(), output)
}
)
})

})
Expand All @@ -219,7 +225,7 @@ test_that("auth functions - workbench managed credentials detection", {
DATABRICKS_CONFIG_FILE = file.path(db_home, "databricks.cfg"),
DATABRICKS_CONFIG_PROFILE = "workbench"
)

token_w <- db_token()
host_w <- db_host()

Expand All @@ -246,24 +252,30 @@ test_that("auth functions - workbench managed credentials detection", {


test_that("auth functions - workbench managed credentials override env var", {
# Emulate the databricks.cfg file written by Workbench.
db_home <- tempfile("posit-workbench")
dir.create(db_home)
writeLines(
c(
'[workbench]',
'host = some-host',
'token = some-token'
),
file.path(db_home, "databricks.cfg")
)

withr::local_file("posit-workbench.cfg", {
writeLines(
c(
'[workbench]',
'host = some-host',
'token = some-token'
),
"posit-workbench.cfg"
)
})


# # Emulate the databricks.cfg file written by Workbench.
# db_home <- tempfile("posit-workbench")
# dir.create(db_home)
#
# Two env variables need to be set on Workbench for detection to succeed
# DATABRICKS_CONFIG_FILE with the path to the databricks.cfg file
# DATABRICKS_CONFIG_PROFILE = "workbench" to set the profile correctly
# Add different `DATABRICKS_HOST` and `DATABRICKS_TOKEN` env variables to ensure
# the credentials from Workbench still get used
withr::local_envvar(
DATABRICKS_CONFIG_FILE = file.path(db_home, "databricks.cfg"),
DATABRICKS_CONFIG_FILE = "posit-workbench.cfg",
DATABRICKS_CONFIG_PROFILE = "workbench",
DATABRICKS_HOST = "env-based-host",
DATABRICKS_TOKEN = "env-based-token"
Expand All @@ -280,7 +292,3 @@ test_that("auth functions - workbench managed credentials override env var", {

})


Sys.setenv("DATABRICKS_HOST" = existing_host)
Sys.setenv("DATABRICKS_TOKEN" = existing_token)
Sys.setenv("DATABRICKS_WSID" = existing_wsid)
28 changes: 19 additions & 9 deletions tests/testthat/test-databricks-helpers.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
test_that("databricks helpers - runtime detection", {

Sys.setenv("DATABRICKS_RUNTIME_VERSION" = "")
expect_no_error(on_databricks())
expect_identical(on_databricks(), FALSE)
expect_identical(determine_brickster_venv(), "r-brickster")

Sys.setenv("DATABRICKS_RUNTIME_VERSION" = "14.0")
expect_no_error(on_databricks())
expect_identical(on_databricks(), TRUE)
expect_null(determine_brickster_venv())
withr::with_envvar(
new = c(DATABRICKS_RUNTIME_VERSION = ""),
{
expect_no_error(on_databricks())
expect_identical(on_databricks(), FALSE)
expect_identical(determine_brickster_venv(), "r-brickster")

}
)

withr::with_envvar(
new = c(DATABRICKS_RUNTIME_VERSION = "14.0"),
{
expect_no_error(on_databricks())
expect_identical(on_databricks(), TRUE)
expect_null(determine_brickster_venv())

}
)

})
Loading