From 2960cd852235d9133fc8af10d8866fa0296544f3 Mon Sep 17 00:00:00 2001 From: Rich FitzJohn Date: Wed, 3 Jan 2024 08:22:35 +0000 Subject: [PATCH 1/2] Helper to pull out windows username --- NAMESPACE | 1 + R/windows.R | 16 ++++++++++++++++ drivers/windows/R/dide_auth.R | 5 +++++ drivers/windows/tests/testthat/test-dide-auth.R | 9 +++++++++ man/windows_username.Rd | 17 +++++++++++++++++ tests/testthat/test-windows.R | 17 +++++++++++++++++ 6 files changed, 65 insertions(+) create mode 100644 man/windows_username.Rd diff --git a/NAMESPACE b/NAMESPACE index 2463b2f5..a225eca2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -24,3 +24,4 @@ export(task_submit) export(task_wait) export(windows_authenticate) export(windows_path) +export(windows_username) diff --git a/R/windows.R b/R/windows.R index 68ba7994..26a19ca2 100644 --- a/R/windows.R +++ b/R/windows.R @@ -52,3 +52,19 @@ windows_path <- function(path_local, path_remote, drive_remote) { ns <- ensure_package("hipercow.windows", rlang::current_env()) ns$windows_path(path_local, path_remote, drive_remote) } + + +##' Report the username used to log into the web portal for use with +##' the windows cluster. This may or may not be the same as your +##' local username. We may ask you to run this when helping debug +##' cluster failures. +##' +##' @title Report windows username +##' +##' @return Your username, as a string +##' +##' @export +windows_username <- function() { + ns <- ensure_package("hipercow.windows", rlang::current_env()) + ns$windows_username() +} diff --git a/drivers/windows/R/dide_auth.R b/drivers/windows/R/dide_auth.R index 40e6b346..9eed5a71 100644 --- a/drivers/windows/R/dide_auth.R +++ b/drivers/windows/R/dide_auth.R @@ -48,6 +48,11 @@ windows_authenticate <- function() { } +windows_username <- function() { + windows_credentials()$username +} + + windows_credentials <- function() { tryCatch({ username <- keyring::key_get("hipercow/dide/username") diff --git a/drivers/windows/tests/testthat/test-dide-auth.R b/drivers/windows/tests/testthat/test-dide-auth.R index 98df424f..e06e0bb0 100644 --- a/drivers/windows/tests/testthat/test-dide-auth.R +++ b/drivers/windows/tests/testthat/test-dide-auth.R @@ -39,6 +39,15 @@ test_that("can fetch dide credentials", { }) +test_that("can fetch dide username", { + mock_credentials <- mockery::mock(credentials("alice", "pw")) + mockery::stub(windows_username, "windows_credentials", mock_credentials) + expect_equal(windows_username(), "alice") + mockery::expect_called(mock_credentials, 1) + expect_equal(mockery::mock_args(mock_credentials)[[1]], list()) +}) + + test_that("can store credentials in keychain", { ## This is pretty grim, but I've not seen another approach to this. mock_keyring_is_locked <- mockery::mock(TRUE) diff --git a/man/windows_username.Rd b/man/windows_username.Rd new file mode 100644 index 00000000..62314445 --- /dev/null +++ b/man/windows_username.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/windows.R +\name{windows_username} +\alias{windows_username} +\title{Report windows username} +\usage{ +windows_username() +} +\value{ +Your username, as a string +} +\description{ +Report the username used to log into the web portal for use with +the windows cluster. This may or may not be the same as your +local username. We may ask you to run this when helping debug +cluster failures. +} diff --git a/tests/testthat/test-windows.R b/tests/testthat/test-windows.R index 2c7ed21d..21ae634b 100644 --- a/tests/testthat/test-windows.R +++ b/tests/testthat/test-windows.R @@ -31,3 +31,20 @@ test_that("windows authenticate passes through to hipercow.windows", { expect_equal(mockery::mock_args(mock_pkg$windows_authenticate)[[1]], list()) }) + + +test_that("windows username passes through to hipercow.windows", { + mock_pkg <- list(windows_username = mockery::mock()) + mock_ensure_package <- mockery::mock(mock_pkg) + mockery::stub(windows_username, "ensure_package", mock_ensure_package) + windows_username() + + mockery::expect_called(mock_ensure_package, 1) + args <- mockery::mock_args(mock_ensure_package)[[1]] + expect_equal(args[[1]], "hipercow.windows") + expect_type(args[[2]], "environment") + + mockery::expect_called(mock_pkg$windows_username, 1) + expect_equal(mockery::mock_args(mock_pkg$windows_username)[[1]], + list()) +}) From ce6b6e55abd5e265dc63bf82eff824de8588f239 Mon Sep 17 00:00:00 2001 From: Rich FitzJohn Date: Wed, 3 Jan 2024 08:41:35 +0000 Subject: [PATCH 2/2] Add windows username into reported configuration --- R/configuration.R | 11 ++++++++++- tests/testthat/test-configuration.R | 25 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/R/configuration.R b/R/configuration.R index 381937f9..dd7f4f81 100644 --- a/R/configuration.R +++ b/R/configuration.R @@ -63,7 +63,16 @@ configuration_paths <- function(root) { configuration_drivers <- function(root) { - root$config + ret <- root$config + if (!is.null(ret$windows)) { + ## This is not really part of the configuration (because windows + ## username/password are saved globally), but we will add it here + ## because it's useful to report, and this is where we'd want it + ## reported. We could add this into the configuration itself, but + ## that causes some pain for the testing there. + ret$windows$username <- windows_username() + } + ret } diff --git a/tests/testthat/test-configuration.R b/tests/testthat/test-configuration.R index dffebe09..9c35e1a1 100644 --- a/tests/testthat/test-configuration.R +++ b/tests/testthat/test-configuration.R @@ -97,3 +97,28 @@ test_that("can report about everything being missing", { c("x" = "hipercow.windows is not installed", "x" = "conan2 is not installed")) }) + + +test_that("can add windows username to configuration", { + elsewhere_register() + path_here <- withr::local_tempdir() + path_there <- withr::local_tempdir() + init_quietly(path_here) + init_quietly(path_there) + root <- hipercow_root(path_here) + suppressMessages( + hipercow_configure("elsewhere", path = path_there, root = path_here)) + + mock_username <- mockery::mock("alice") + mockery::stub(configuration_drivers, "windows_username", mock_username) + res <- configuration_drivers(root) + mockery::expect_called(mock_username, 0) + expect_equal(res, root$config) + + root$config <- c(root$config, list(windows = list(a = 1, b = 2))) + res <- configuration_drivers(root) + mockery::expect_called(mock_username, 1) + cmp <- root$config + cmp$windows$username <- "alice" + expect_equal(res, cmp) +})