From 75bd4a72a27c24b83d1e0646cef5439184060c28 Mon Sep 17 00:00:00 2001 From: atheo89 Date: Tue, 30 Jan 2024 09:03:42 +0100 Subject: [PATCH] Add test cases --- Makefile | 29 ++++++++++++++++++++--- rstudio/c9s-python-3.9/test/test_script.R | 23 ++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 rstudio/c9s-python-3.9/test/test_script.R diff --git a/Makefile b/Makefile index b2c77108a..30f5e31dc 100644 --- a/Makefile +++ b/Makefile @@ -431,22 +431,45 @@ validate-codeserver-image: bin/kubectl .PHONY: validate-rstudio-image validate-rstudio-image: bin/kubectl $(eval NOTEBOOK_NAME := $(subst .,-,$(subst cuda-,,$*))) - $(info # Running tests for $(NOTEBOOK_NAME) code-server image...) + $(info # Running tests for $(NOTEBOOK_NAME) RStudio Server image...) $(KUBECTL_BIN) wait --for=condition=ready pod rstudio-pod --timeout=300s @required_commands=$(REQUIRED_R_STUDIO_IMAGE_COMMANDS) ; \ if [[ $$image == "" ]] ; then \ echo "Usage: make validate-rstudio-image image=" ; \ exit 1 ; \ fi ; \ + echo "=> Checking container image $$image for package intallation..." ; \ + $(KUBECTL_BIN) exec -it rstudio-pod -- mkdir -p /opt/app-root/src/R/temp-library > /dev/null 2>&1 ; \ + $(KUBECTL_BIN) exec rstudio-pod -- R -e "install.packages('tinytex', lib='/opt/app-root/src/R/temp-library')" > /dev/null 2>&1 ; \ + if [ $$? -eq 0 ]; then \ + echo "Tinytex installation successful!"; \ + else \ + echo "Error: Tinytex installation failed."; \ + fi; \ for cmd in $$required_commands ; do \ echo "=> Checking container image $$image for $$cmd..." ; \ $(KUBECTL_BIN) exec rstudio-pod which $$cmd > /dev/null 2>&1 ; \ - if [ $$? -ne 0 ]; then \ - echo "ERROR: Container image $$image does not meet criteria for command: $$cmd" ; \ + if [ $$? -eq 0 ]; then \ + echo "$$cmd executed successfuly!"; \ + else \ + echo "ERROR: Container image $$image does not meet criteria for command: $$cmd" ; \ fail=1; \ continue; \ fi; \ done ; \ + echo "=> Fetching R script from URL and executing on the container..."; \ + curl -sSL -o test_script.R "${NOTEBOOK_REPO_BRANCH_BASE}/rstudio/c9s-python-3.9/test/test_script.R" > /dev/null 2>&1 ; \ + $(KUBECTL_BIN) cp test_script.R rstudio-pod:/opt/app-root/src/test_script.R > /dev/null 2>&1; \ + $(KUBECTL_BIN) exec rstudio-pod -- Rscript /opt/app-root/src/test_script.R > /dev/null 2>&1 ; \ + if [ $$? -eq 0 ]; then \ + echo "R script executed successfully!"; \ + rm test_script.R ; \ + else \ + echo "Error: R script failed."; \ + fail=1; \ + continue; \ + fi; \ + # This is only for the workflow action .PHONY: refresh-pipfilelock-files diff --git a/rstudio/c9s-python-3.9/test/test_script.R b/rstudio/c9s-python-3.9/test/test_script.R new file mode 100644 index 000000000..6a74d56c3 --- /dev/null +++ b/rstudio/c9s-python-3.9/test/test_script.R @@ -0,0 +1,23 @@ + # Set a seed for reproducibility + set.seed(123) + + # Generate a random dataset + x <- rnorm(100) + data <- data.frame( + x = x, + y = 2 * x + rnorm(100) + ) + if ("x" %in% colnames(data) && is.numeric(data$x) && + "y" %in% colnames(data) && is.numeric(data$y)) { + # Compute basic statistics + mean_x <- mean(data$x, na.rm = TRUE) + mean_y <- mean(data$y, na.rm = TRUE) + correlation <- cor(data$x, data$y, use = "complete.obs") + # Print the statistics + cat("Mean of x:", mean_x, "\n") + cat("Mean of y:", mean_y, "\n") + cat("Correlation between x and y:", correlation, "\n") + + } else { + cat("Error: Columns 'x' and 'y' must be numeric and exist in the dataset.\n") + } \ No newline at end of file