Merge pull request #130 from rstudio/glin-test

Add tests for R binaries and packages
This commit is contained in:
Greg Lin 2022-08-17 16:26:24 -05:00 committed by GitHub
commit b088d31a7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 523 additions and 18 deletions

View file

@ -39,6 +39,26 @@ rebuild-all: deps fetch-serverless-custom-file
serverless-deploy.%: deps fetch-serverless-custom-file
$(SLS_BINARY) deploy --stage $*
define GEN_TARGETS
docker-build-$(platform):
@cd builder && docker-compose build $(platform)
build-r-$(platform):
@cd builder && R_VERSION=$(R_VERSION) docker-compose up $(platform)
test-r-$(platform):
@cd test && R_VERSION=$(R_VERSION) docker-compose up $(platform)
bash-$(platform):
docker run -it --rm --entrypoint /bin/bash -v $(CURDIR):/r-builds r-builds:$(platform)
.PHONY: docker-build-$(platform) build-r-$(platform) test-r-$(platform) bash-$(platform)
endef
$(foreach platform,$(PLATFORMS), \
$(eval $(GEN_TARGETS)) \
)
# Helper for launching a bash session on a docker image of your choice. Defaults
# to "ubuntu:xenial".
TARGET_IMAGE?=ubuntu:xenial

View file

@ -244,33 +244,34 @@ serverless invoke stepf -n rBuilds -d '{"force": true, "versions": ["3.6.3", "4.
## Testing
To test the R builds locally, you can build the images:
To test the R builds locally, you can use the `build-r-$PLATFORM` and `test-r-$PLATFORM`
targets to build R and run the tests. The tests use the quick install script to install R,
using a locally built R if present, or otherwise a build from the CDN.
```bash
# Build images for all platforms
make docker-build
# Build R 4.1.3 for Ubuntu 22
R_VERSION=4.1.3 make build-r-ubuntu-2204
# Or build the image for a single platform
(cd builder && docker-compose build ubuntu-2004)
# Test R 4.1.3 for Ubuntu 22
R_VERSION=4.1.3 make test-r-ubuntu-2204
```
Then run the build script:
Alternatively, you can build an image using the `docker-build-$PLATFORM`
target, launch a bash session within a container using the `bash-$PLATFORM` target,
and interactively run the build script:
```bash
# Build R for all platforms
R_VERSION=4.0.5 make docker-build-r
# Build the image for Ubuntu 22
make docker-build-ubuntu-2204
# Build R for a single platform
(cd builder && R_VERSION=4.0.5 docker-compose up ubuntu-2004)
# Launch a bash session for Ubuntu 22
make bash-ubuntu-2204
# Alternatively, run the build script from within a container
docker run -it --rm --entrypoint "/bin/bash" r-builds:ubuntu-2004
# Build R 4.1.3
R_VERSION=4.1.3 ./build.sh
# Build R 4.0.5
R_VERSION=4.0.5 ./build.sh
# Build R devel
R_VERSION=devel ./build.sh
# Build R devel with parallel execution to speed up the build
MAKEFLAGS=-j4 R_VERSION=devel ./build.sh
# Build a prerelease version of R (e.g., alpha or beta)
R_VERSION=rc R_TARBALL_URL=https://cran.r-project.org/src/base-prerelease/R-latest.tar.gz ./build.sh

View file

@ -110,7 +110,7 @@ detect_os_version () {
cat /etc/os-release | grep -e "^VERSION_ID\=*" | cut -f 2 -d '=' | sed -e 's/"//g'
fi
fi
if [[ "${os}" == "Ubuntu" ]]; then
if [[ "${os}" == "Ubuntu" ]] || [[ "${os}" == "Debian" ]]; then
cat /etc/os-release | grep -e "^VERSION_ID\=*" | cut -f 2 -d '=' | sed -e 's/[".]//g'
fi
if [[ "${os}" == "SLES15" ]] || [[ "${os}" == "LEAP15" ]]; then
@ -277,6 +277,7 @@ install_deb () {
if [[ "${RUN_UNATTENDED}" -ne "0" ]]; then
yes="--n"
yesapt="-y"
export DEBIAN_FRONTEND=noninteractive
fi
echo "Updating package indexes..."
${SUDO} apt-get update

67
test/docker-compose.yml Normal file
View file

@ -0,0 +1,67 @@
version: '2.0'
services:
ubuntu-1804:
image: ubuntu:bionic
command: /r-builds/test/test-apt.sh
environment:
- OS_IDENTIFIER=ubuntu-1804
- R_VERSION=${R_VERSION}
volumes:
- ../:/r-builds
ubuntu-2004:
image: ubuntu:focal
command: /r-builds/test/test-apt.sh
environment:
- OS_IDENTIFIER=ubuntu-2004
- R_VERSION=${R_VERSION}
volumes:
- ../:/r-builds
ubuntu-2204:
image: ubuntu:jammy
command: /r-builds/test/test-apt.sh
environment:
- OS_IDENTIFIER=ubuntu-2204
- R_VERSION=${R_VERSION}
volumes:
- ../:/r-builds
debian-11:
image: debian:bullseye
command: /r-builds/test/test-apt.sh
environment:
- OS_IDENTIFIER=debian-11
- R_VERSION=${R_VERSION}
volumes:
- ../:/r-builds
debian-10:
image: debian:buster
command: /r-builds/test/test-apt.sh
environment:
- OS_IDENTIFIER=debian-10
- R_VERSION=${R_VERSION}
volumes:
- ../:/r-builds
centos-7:
image: centos:centos7
command: /r-builds/test/test-yum.sh
environment:
- OS_IDENTIFIER=centos-7
- R_VERSION=${R_VERSION}
volumes:
- ../:/r-builds
centos-8:
image: rockylinux:8
command: /r-builds/test/test-yum.sh
environment:
- OS_IDENTIFIER=centos-8
- R_VERSION=${R_VERSION}
volumes:
- ../:/r-builds
opensuse-153:
image: opensuse/leap:15.3
command: /r-builds/test/test-zypper.sh
environment:
- OS_IDENTIFIER=opensuse-153
- R_VERSION=${R_VERSION}
volumes:
- ../:/r-builds

27
test/test-apt.sh Executable file
View file

@ -0,0 +1,27 @@
#!/usr/bin/env bash
set -ex
SCRIPT_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
# Install quick install script prerequisites
if ! command -v curl > /dev/null 2>&1; then
apt update -qq
apt install -y curl
fi
# Run the quick install script. Use a locally built file if present, otherwise from the CDN.
tmpdir=$(mktemp -d)
cp -r "${SCRIPT_DIR}/../builder/integration/tmp/${OS_IDENTIFIER}/." "$tmpdir" > /dev/null 2>&1 || true
(cd "$tmpdir" && SCRIPT_ACTION=install R_VERSION="${R_VERSION}" RUN_UNATTENDED=1 "${SCRIPT_DIR}/../install.sh")
# Show DEB info
apt show "r-${R_VERSION}"
"${SCRIPT_DIR}/test-r.sh"
apt remove -y "r-${R_VERSION}"
if [ -d "/opt/R/${R_VERSION}" ]; then
echo "Failed to uninstall completely"
exit 1
fi

18
test/test-r.sh Executable file
View file

@ -0,0 +1,18 @@
#!/usr/bin/env bash
set -ex
SCRIPT_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
R_HOME=/opt/R/${R_VERSION}/lib/R
"${R_HOME}/bin/R" --version
"${R_HOME}/bin/Rscript" -e 'sessionInfo()'
# List R devel dependencies
gcc --version
g++ --version
gfortran --version
# List shared library dependencies (e.g. BLAS/LAPACK)
LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${R_HOME}/lib ldd "${R_HOME}/lib/libR.so"
DIR=$SCRIPT_DIR "${R_HOME}/bin/Rscript" "${SCRIPT_DIR}/test.R"

32
test/test-yum.sh Executable file
View file

@ -0,0 +1,32 @@
#!/usr/bin/env bash
set -ex
SCRIPT_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
if command -v dnf > /dev/null 2>&1; then
YUM=dnf
else
YUM=yum
fi
# Install quick install script prerequisites
if ! command -v curl > /dev/null 2>&1; then
$YUM install -y curl
fi
# Run the quick install script. Use a locally built file if present, otherwise from the CDN.
tmpdir=$(mktemp -d)
cp -r "${SCRIPT_DIR}/../builder/integration/tmp/${OS_IDENTIFIER}/." "$tmpdir" > /dev/null 2>&1 || true
(cd "$tmpdir" && SCRIPT_ACTION=install R_VERSION="${R_VERSION}" RUN_UNATTENDED=1 "${SCRIPT_DIR}/../install.sh")
# Show rpm info
rpm -qi "R-${R_VERSION}"
"${SCRIPT_DIR}/test-r.sh"
$YUM -y remove "R-${R_VERSION}"
if [ -d "/opt/R/${R_VERSION}" ]; then
echo "Failed to uninstall completely"
exit 1
fi

26
test/test-zypper.sh Executable file
View file

@ -0,0 +1,26 @@
#!/usr/bin/env bash
set -ex
SCRIPT_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
# Install quick install script prerequisites
if ! command -v curl > /dev/null 2>&1; then
zypper --non-interactive install curl
fi
# Run the quick install script. Use a locally built file if present, otherwise from the CDN.
tmpdir=$(mktemp -d)
cp -r "${SCRIPT_DIR}/../builder/integration/tmp/${OS_IDENTIFIER}/." "$tmpdir" > /dev/null 2>&1 || true
(cd "$tmpdir" && SCRIPT_ACTION=install R_VERSION="${R_VERSION}" RUN_UNATTENDED=1 "${SCRIPT_DIR}/../install.sh")
# Show RPM info
rpm -qi "R-${R_VERSION}"
"${SCRIPT_DIR}/test-r.sh"
zypper --non-interactive remove "R-${R_VERSION}"
if [ -d "/opt/R/${R_VERSION}" ]; then
echo "Failed to uninstall completely"
exit 1
fi

103
test/test.R Normal file
View file

@ -0,0 +1,103 @@
# HTTP mirror to support R 3.1
options(repos = c("https://cloud.r-project.org", "http://cloud.r-project.org"))
# Create a temp lib to avoid installing into the system library
temp_lib <- tempdir()
.libPaths(temp_lib)
# Install a package from CRAN
install.packages("R6")
library(R6)
# Install a package with C/C++ and Fortran code, which links against libR, BLAS, LAPACK
curr_dir <- Sys.getenv("DIR", ".")
install.packages(file.path(curr_dir, "testpkg"), repos = NULL, clean = TRUE)
source(file.path(curr_dir, "testpkg/tests/test.R"))
# Check iconv support
if (!capabilities("iconv") || !all(c("ASCII", "LATIN1", "UTF-8") %in% iconvlist())) {
stop("missing iconv support")
}
# Check that built-in packages can be loaded
for (pkg in rownames(installed.packages(priority = c("base", "recommended")))) {
if (!require(pkg, character.only = TRUE)) {
stop(sprintf("failed to load built-in package %s", pkg))
}
}
# Show capabilities. Warnings are returned on missing libraries.
tryCatch(capabilities(), warning = function(w) {
print(capabilities())
stop(sprintf("missing libraries: %s", w$message))
})
# Check graphics devices
# https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/Devices.html
for (dev_name in c("png", "jpeg", "tiff", "svg", "bmp", "pdf", "postscript",
"xfig", "pictex", "cairo_pdf", "cairo_ps")) {
# Skip unsupported graphics devices (e.g. tiff in R >= 3.3 on CentOS 6)
if (dev_name %in% names(capabilities()) && capabilities(dev_name) == FALSE) {
next
}
dev <- getFromNamespace(dev_name, "grDevices")
tryCatch({
file <- tempfile()
on.exit(unlink(file))
if (dev_name == "xfig") {
# Suppress warning from xfig when onefile = FALSE (the default)
dev(file, onefile = TRUE)
} else {
dev(file)
}
plot(1)
dev.off()
}, warning = function(w) {
# Catch errors which manifest as warnings (e.g. "failed to load cairo DLL")
stop(sprintf("graphics device %s failed: %s", dev_name, w$message))
})
}
# Check for unexpected output from graphics/text rendering.
# Run externally to capture output from external processes.
# For example, "Pango-WARNING **: failed to choose a font, expect ugly output"
# messages when rendering text without any system fonts installed.
output <- system2(R.home("bin/Rscript"), "-e 'png(tempfile()); plot(1)'", stdout = TRUE, stderr = TRUE)
if (length(output) > 0) {
stop(sprintf("unexpected output returned from plotting:\n%s", paste(output, collapse = "\n")))
}
# Check download methods: libcurl (supported in R >= 3.2) and internal (based on libxml)
if ("libcurl" %in% names(capabilities())) {
download.file("https://cloud.r-project.org", tempfile(), "libcurl")
}
tmpfile <- tempfile()
write.csv("test", tmpfile)
download.file(sprintf("file://%s", tmpfile), tempfile(), "internal")
# Check that a pager is configured and help pages work
# https://stat.ethz.ch/R-manual/R-devel/library/base/html/file.show.html
output <- system2(R.home("bin/Rscript"), "-e 'help(stats)'", stdout = TRUE)
if (length(output) == 0) {
stop("failed to display help pages; check that a pager is configured properly")
}
# Smoke test BLAS/LAPACK functionality. R may start just fine with an incompatible
# BLAS/LAPACK library, and only fail when calling a BLAS or LAPACK routine.
stopifnot(identical(crossprod(matrix(1)), matrix(1)))
stopifnot(identical(chol(matrix(1)), matrix(1)))
# Check that R 3.x depends on PCRE1, and R 4.x depends on PCRE2.
# R 3.5 and 3.6 will link against PCRE2 if present, and take on an unnecessary dependency.
# Some distros do always require PCRE2, however, such as Debian 11.
ld_flags <- system2(R.home("bin/R"), c("CMD", "config", "--ldflags"), stdout = TRUE)
has_pcre1 <- grepl("-lpcre\\b", ld_flags)
has_pcre2 <- grepl("-lpcre2-8\\b", ld_flags)
if (getRversion() >= "3.5.0" && getRversion() < "4.0.0") {
stopifnot(has_pcre1)
if (has_pcre2) {
message(sprintf("Info: %s is linked against PCRE2, which may be unnecessary", R.version.string))
}
} else if (getRversion() >= "4.0.0") {
stopifnot(has_pcre2 && !has_pcre1)
}

View file

@ -0,0 +1,2 @@
^.*\.Rproj$
^\.Rproj\.user$

2
test/testpkg/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
.Rproj.user
.Rhistory

12
test/testpkg/DESCRIPTION Normal file
View file

@ -0,0 +1,12 @@
Package: testpkg
Type: Package
Title: A Test Package
Version: 0.1.0
Authors@R: person("Test", "Pkg", email = "test@test", role = c("aut", "cre"))
Description: A test package.
License: MIT
Depends:
R (>= 3.1)
SystemRequirements: C++11
RoxygenNote: 6.1.1
Encoding: UTF-8

6
test/testpkg/NAMESPACE Normal file
View file

@ -0,0 +1,6 @@
# Generated by roxygen2: do not edit by hand
export(add_it)
export(square_it)
export(subtract_it)
useDynLib(testpkg, .registration = TRUE)

32
test/testpkg/R/testpkg.R Normal file
View file

@ -0,0 +1,32 @@
#' @useDynLib testpkg, .registration = TRUE
"_PACKAGE"
#' Add it together
#'
#' @param a Number
#' @param b Number
#' @return Sum of numbers
#' @export
add_it <- function(a, b) {
.Call("add", a, b)
}
#' Subtract it
#'
#' @param a Number
#' @param b Number
#' @return Difference of numbers
#' @export
subtract_it <- function(a, b) {
.Call("subtract", a, b)
}
#' Square it up
#'
#' @param n Integer
#' @return Square
#' @export
square_it <- function(n) {
result <- .Fortran("square", n = as.integer(n), answer = as.integer(1))
result$answer
}

3
test/testpkg/README.md Normal file
View file

@ -0,0 +1,3 @@
# testpkg
Test package with C/C++ and Fortran code, which links against libR, BLAS, LAPACK (see [Makevars](src/Makevars)).

View file

@ -0,0 +1,19 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/testpkg.R
\name{add_it}
\alias{add_it}
\title{Add it together}
\usage{
add_it(a, b)
}
\arguments{
\item{a}{Number}
\item{b}{Number}
}
\value{
Sum of numbers
}
\description{
Add it together
}

View file

@ -0,0 +1,17 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/testpkg.R
\name{square_it}
\alias{square_it}
\title{Square it up}
\usage{
square_it(n)
}
\arguments{
\item{n}{Integer}
}
\value{
Square
}
\description{
Square it up
}

View file

@ -0,0 +1,19 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/testpkg.R
\name{subtract_it}
\alias{subtract_it}
\title{Subtract it}
\usage{
subtract_it(a, b)
}
\arguments{
\item{a}{Number}
\item{b}{Number}
}
\value{
Difference of numbers
}
\description{
Subtract it
}

View file

@ -0,0 +1,14 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/testpkg.R
\docType{package}
\name{testpkg-package}
\alias{testpkg}
\alias{testpkg-package}
\title{testpkg: A Test Package}
\description{
A test package.
}
\author{
\strong{Maintainer}: Test Pkg \email{test@test}
}

View file

@ -0,0 +1,2 @@
CXX_STD=CXX11
PKG_LIBS=$(MAIN_LDFLAGS) $(LDFLAGS) $(LIBR) $(LIBS) $(LAPACK_LIBS) $(BLAS_LIBS) $(SHLIB_OPENMP_CFLAGS)

Some files were not shown because too many files have changed in this diff Show more