Skip to content

Commit

Permalink
migrate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau-lilly committed Jan 8, 2024
1 parent 53414fb commit d26fa38
Showing 1 changed file with 53 additions and 38 deletions.
91 changes: 53 additions & 38 deletions tests/monitor/test-monitor.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@ library(crew.aws.batch)
library(testthat)

test_that("empty job list", {
x <- crew_monitor_aws_batch(
monitor <- crew_monitor_aws_batch(
job_definition = "never-existed",
job_queue = "crew-aws-batch-job-queue",
region = "us-east-2"
)
expect_equal(nrow(x$jobs()), 0L)
expect_equal(nrow(x$status(id = "does-not-exist")), 0L)
expect_true(tibble::is_tibble(x$log(id = "does-not-exist")))
expect_equal(nrow(monitor$jobs()), 0L)
expect_equal(nrow(monitor$status(id = "does-not-exist")), 0L)
expect_true(tibble::is_tibble(monitor$log(id = "does-not-exist")))
})

test_that("job list", {
x <- crew_monitor_aws_batch(
definition <- crew_definition_aws_batch(
job_definition = "crew-aws-batch-test",
job_queue = "crew-aws-batch-job-queue",
region = "us-east-2"
)
x$register(
monitor <- crew_monitor_aws_batch(
job_definition = "crew-aws-batch-test",
job_queue = "crew-aws-batch-job-queue",
region = "us-east-2"
)
definition$register(
image = "alpine:latest",
platform_capabilities = "EC2",
memory_units = "mebibytes",
Expand All @@ -28,8 +33,8 @@ test_that("job list", {
tags = c("crew_aws_batch_1", "crew_aws_batch_2"),
propagate_tags = TRUE
)
on.exit(x$deregister())
x$submit(
on.exit(definition$deregister())
definition$submit(
command = c("sleep", "300"),
memory_units = "mebibytes",
memory = 128,
Expand All @@ -38,26 +43,26 @@ test_that("job list", {
tags = c("crew_aws_batch_1", "crew_aws_batch_2"),
propagate_tags = TRUE
)
job <- x$submit()
expect_equal(nrow(x$status(id = job$id)), 1L)
expect_true(tibble::is_tibble(jobs <- x$jobs()))
expect_true(tibble::is_tibble(submitted <- x$submitted()))
expect_true(tibble::is_tibble(active <- x$active()))
expect_true(tibble::is_tibble(inactive <- x$inactive()))
expect_true(tibble::is_tibble(pending <- x$pending()))
expect_true(tibble::is_tibble(runnable <- x$runnable()))
expect_true(tibble::is_tibble(running <- x$running()))
expect_true(tibble::is_tibble(succeeded <- x$succeeded()))
expect_true(tibble::is_tibble(failed <- x$failed()))
job <- definition$submit()
expect_equal(nrow(monitor$status(id = job$id)), 1L)
expect_true(tibble::is_tibble(jobs <- monitor$jobs()))
expect_true(tibble::is_tibble(submitted <- monitor$submitted()))
expect_true(tibble::is_tibble(active <- monitor$active()))
expect_true(tibble::is_tibble(inactive <- monitor$inactive()))
expect_true(tibble::is_tibble(pending <- monitor$pending()))
expect_true(tibble::is_tibble(runnable <- monitor$runnable()))
expect_true(tibble::is_tibble(running <- monitor$running()))
expect_true(tibble::is_tibble(succeeded <- monitor$succeeded()))
expect_true(tibble::is_tibble(failed <- monitor$failed()))
expect_true(nrow(jobs) > 0L)
expect_true(job$name %in% jobs$name)
expect_true(job$id %in% jobs$id)
expect_true(job$arn %in% jobs$arn)
info <- x$status(id = job$id)
info <- monitor$status(id = job$id)
expect_true(is.na(info$reason))
expect_false(info$status %in% c("succeeded", "failed"))
good_reason <- "I have my reasons..."
x$terminate(id = info$id, reason = good_reason)
monitor$terminate(id = info$id, reason = good_reason)
attempts <- 0
while (!info$status %in% c("succeeded", "failed")) {
message(
Expand All @@ -67,7 +72,7 @@ test_that("job list", {
sample(c("-", "\\", "|", "/"), size = 1L)
)
)
info <- x$status(id = job$id)
info <- monitor$status(id = job$id)
attempts <- attempts + 1L
if (attempts > 20L) {
stop("job did not terminate")
Expand All @@ -79,12 +84,17 @@ test_that("job list", {
})

test_that("job logs", {
x <- crew_monitor_aws_batch(
definition <- crew_definition_aws_batch(
job_definition = "crew-aws-batch-test",
job_queue = "crew-aws-batch-job-queue",
region = "us-east-2"
)
monitor <- crew_monitor_aws_batch(
job_definition = "crew-aws-batch-test",
job_queue = "crew-aws-batch-job-queue",
region = "us-east-2"
)
x$register(
definition$register(
image = "alpine:latest",
platform_capabilities = "EC2",
memory_units = "mebibytes",
Expand All @@ -94,16 +104,16 @@ test_that("job logs", {
tags = c("crew_aws_batch_1", "crew_aws_batch_2"),
propagate_tags = TRUE
)
on.exit(x$deregister())
job <- x$submit(
on.exit(definition$deregister())
job <- definition$submit(
command = c("echo", "done with container\ndone with job"),
memory_units = "mebibytes",
memory = 128,
cpus = 1
)
attempts <- 0L
done <- c("succeeded", "failed")
while (!((status <- x$status(id = job$id)$status) %in% done)) {
while (!((status <- monitor$status(id = job$id)$status) %in% done)) {
message(
paste(
"job status:",
Expand All @@ -117,18 +127,23 @@ test_that("job logs", {
}
Sys.sleep(5)
}
log <- x$log(id = job$id)
log <- monitor$log(id = job$id)
expect_true(tibble::is_tibble(log))
expect_equal(log$message, c("done with container", "done with job"))
})

test_that("job terminate", {
x <- crew_monitor_aws_batch(
definition <- crew_definition_aws_batch(
job_definition = "crew-aws-batch-test",
job_queue = "crew-aws-batch-job-queue",
region = "us-east-2"
)
monitor <- crew_monitor_aws_batch(
job_definition = "crew-aws-batch-test",
job_queue = "crew-aws-batch-job-queue",
region = "us-east-2"
)
x$register(
definition$register(
image = "alpine:latest",
platform_capabilities = "EC2",
memory_units = "mebibytes",
Expand All @@ -138,10 +153,10 @@ test_that("job terminate", {
tags = c("crew_aws_batch_1", "crew_aws_batch_2"),
propagate_tags = TRUE
)
on.exit(x$deregister())
on.exit(definition$deregister())
n <- 2L
for (index in seq_len(n)) {
x$submit(
definition$submit(
command = c("sleep", "600"),
memory_units = "mebibytes",
memory = 128,
Expand All @@ -151,14 +166,14 @@ test_that("job terminate", {
propagate_tags = TRUE
)
}
while (nrow(x$active()) < n) {
message(nrow(x$active()))
while (nrow(monitor$active()) < n) {
message(nrow(monitor$active()))
Sys.sleep(1)
}
x$terminate(ids = x$active()$id, verbose = TRUE)
while (nrow(x$active()) > 0L) {
message(paste(x$active()$status, collapse = " "))
monitor$terminate(ids = monitor$active()$id, verbose = TRUE)
while (nrow(monitor$active()) > 0L) {
message(paste(monitor$active()$status, collapse = " "))
Sys.sleep(5)
}
expect_equal(nrow(x$active()), 0L)
expect_equal(nrow(monitor$active()), 0L)
})

0 comments on commit d26fa38

Please sign in to comment.