diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..51942bb --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(AzureDSVM) + +test_check("AzureDSVM") \ No newline at end of file diff --git a/tests/testthat/helper.R b/tests/testthat/helper.R new file mode 100644 index 0000000..9d87622 --- /dev/null +++ b/tests/testthat/helper.R @@ -0,0 +1,25 @@ +# helper functions - these are the same as used in AzureSMR package. + +skip_if_missing_config <- function(f){ + if(!file.exists(f)) { + msg <- paste("To run tests, add a file ~/.azuresmr/settings.json containing AzureML keys.", + "See ?workspace for help", + sep = "\n") + message(msg) + testthat::skip("settings.json file is missing") + } +} + +skip_if_offline <- function(){ + u <- tryCatch(url("https://mran.microsoft.com"), + error = function(e)e) + if(inherits(u, "error")){ + u <- url("http://mran.microsoft.com") + } + on.exit(close(u)) + z <- tryCatch(suppressWarnings(readLines(u, n = 1, warn = FALSE)), + error = function(e)e) + if(inherits(z, "error")){ + testthat::skip("Offline. Skipping test.") + } +} diff --git a/tests/testthat/test-1-deployment.R b/tests/testthat/test-1-deployment.R new file mode 100644 index 0000000..b24dfc3 --- /dev/null +++ b/tests/testthat/test-1-deployment.R @@ -0,0 +1,75 @@ +# test deployment of DSVM. + +if(interactive()) library("testthat") + +library(AzureSMR) + +settingsfile <- getOption("AzureSMR.config") +config <- read.AzureSMR.config() + +context("DSVM deployment") + +asc <- createAzureContext() + +with(config, + setAzureContext(asc, tenantID=tenantID, clientID=clientID, authKey=authKey) +) +azureAuthenticate(asc) + +timestamp <- format(Sys.time(), format="%y%m%d%H%M") + +resourceGroup_name <- paste0("AzureDSVMtest_", timestamp) +location <- "southeastasia" +dsvm_name <- paste0("azuredsvm", timestamp) +dsvm_size <- "Standard_D4_v2" +dsvm_os <- "CentOS" +dsvm_password <- "AzureDSVM_test123" +dsvm_username <- "dsvmuser" + +# create a new resource group. + +context(" - create a new resource group") + +test_that("Can create resource group", { + skip_if_missing_config(settingsfile) + + res <- azureCreateResourceGroup(asc, + location=location, + resourceGroup=resourceGroup_name) + expect_equal(res, TRUE) + + AzureSMR:::wait_for_azure( + resourceGroup_name %in% azureListRG(asc)$resourceGroup + ) + expect_true(resourceGroup_name %in% azureListRG(asc)$resourceGroup) +}) + +context(" - Deploy a DSVM") + +test_that("Deploy a DSVM with custom specifications", { + skip_if_missing_config(settingsfile) + + res <- deployDSVM(asc, + resource.group=resourceGroup_name, + location=location, + hostname=dsvm_name, + username=dsvm_username, + size=dsvm_size, + os=dsvm_os, + authen="Password", + pubkey="", + password=dsvm_password, + mode="Sync") + + expect_equal(object=res, expected=paste(dsvm_name, location, "cloudapp.azure.com", sep=".")) +}) + +context(" - delete resource group") +test_that("Can delete resource group", { + skip_if_missing_config(settingsfile) + + expect_message({ + res <- azureDeleteResourceGroup(asc, resourceGroup = resourceGroup_name) + }, "Delete Request Submitted" + ) +}) \ No newline at end of file