Skip to content

Instantly share code, notes, and snippets.

@tomardern
Created January 6, 2021 10:12
Show Gist options
  • Save tomardern/822642988be2851beef930f94ba62fa2 to your computer and use it in GitHub Desktop.
Save tomardern/822642988be2851beef930f94ba62fa2 to your computer and use it in GitHub Desktop.
Cypress Lambda Testing
const cypress = require("cypress");
const exec = require("child_process").exec;
const child_process = require("child_process");
const runCommand = (command) => {
console.log("Running ", command);
try {
child_process.execSync(command, {
stdio: "inherit",
});
} catch (e) {
console.log("Error");
console.warn(e);
}
};
/**
* Handler
* @param {*} event
*/
exports.handler = (event) => {
// process.env.ELECTRON_EXTRA_LAUNCH_ARGS = "disable-dev-shm-usage";
process.env.DEBUG = "cypress:*";
process.env.ELECTRON_EXTRA_LAUNCH_ARGS = [
"--disable-dev-shm-usage", // disable /dev/shm tmpfs usage on Lambda
// @TODO: review if these are still relevant:
"--disable-gpu",
"--single-process", // Currently wont work without this :-(
// https://groups.google.com/a/chromium.org/d/msg/headless-dev/qqbZVZ2IwEw/Y95wJUh2AAAJ
"--no-zygote", // helps avoid zombies
"--no-sandbox",
"--disable-setuid-sandbox",
"--user-data-dir=/tmp/chrome-user-data",
// old stuff
// "disable-dev-shm-usage",
// "disable-gpu",
// // "no-zygote",
// // "single-process",
// "no-sandbox",
// "disable-software-rasterizer",
].join(" ");
process.env.XDG_CONFIG_HOME = "/tmp"; // https://github.com/electron/electron/blob/master/docs/api/app.md#appgetpathname
runCommand("mkdir /tmp/chrome-user-data");
runCommand("nohup Xvfb :99 &>/dev/null &");
runCommand("mkdir /tmp/shm");
return cypress
.run({
spec: "/app/cypress/tests/actions_spec.js",
browser: "chrome",
env: {
DEBUG: "cypress:*",
// XDG_CONFIG_HOME: "/tmp",
},
// project: "/tmp",
})
.then((r) => {
console.log("r");
console.log(r);
// xvfb.stop();
return {
status: 200,
body: r,
};
})
.catch((e) => {
console.log("error");
console.log(e);
// xvfb.stop();
return {
status: 500,
body: e,
};
});
});
# Patching the Cypress binary to remove any references to /dev/shm
echo "Attempting to patch Cypress 1"
position=$(strings -t d /cypress-cache/6.0.0/Cypress/Cypress | grep '/dev\/shm' -m 1 | cut -d' ' -f1)
echo -n '/tmp/shm' | dd bs=1 of=/cypress-cache/6.0.0/Cypress/Cypress seek="$position" conv=notrunc || echo "Nothing to change"
echo "Attempting to patch Cypress 2"
position=$(strings -t d /cypress-cache/6.0.0/Cypress/Cypress | grep '/dev\/shm\/' | cut -d' ' -f1)
echo -n '/tmp/shm/' | dd bs=1 of=/cypress-cache/6.0.0/Cypress/Cypress seek="$position" conv=notrunc || echo "Nothing to change"
FROM cypress/browsers:node14.7.0-chrome84
# Install aws-lambda-cpp build dependencies
RUN apt update && \
apt install -y \
ffmpeg \
xvfb \
g++ \
make \
cmake \
unzip \
xserver-xorg-input-mouse \
xserver-xorg-input-kbd \
libcurl4-openssl-dev
# To ensure we are using the same cache from the docker build to running the tests
ENV CYPRESS_CACHE_FOLDER /cypress-cache
# https://github.com/cypress-io/cypress-docker-images/issues/52
COPY asound.conf /etc/asound.conf
# # for Xvfb
ENV DISPLAY=:99
WORKDIR /app
# Copy application dependency manifests to the container image.
# A wildcard is used to ensure both package.json AND package-lock.json are copied.
# Copying this separately prevents re-running npm install on every code change.
COPY package*.json ./
RUN npm install
# Copy local code to the container image.
COPY . ./
COPY cypress.json ./cypress.json
USER root
# RUN chmod 777 /dev/shm
# Patch Cypress to point to /tmp/shm instead
RUN bash cypress-for-lambda.sh
# Run the web service on container startup.
# CMD [ "npm", "run", "aws:start" ]
ENTRYPOINT ["/usr/local/bin/npm", "run"]
CMD ["aws"]
{
"name": "cypress-on-gcp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest",
"init": "jest --init",
"aws:start": "Xvfb :99 & $(npm bin)/aws-lambda-ric app.handler",
"aws": "$(npm bin)/aws-lambda-ric app.handler",
"cypress:open": "$(npm bin)/cypress open",
"cypress:verify": "$(npm bin)/cypress verify"
},
"author": "",
"license": "ISC",
"dependencies": {
"aws-lambda-ric": "^1.0.0",
"cypress": "6.0.0"
},
"devDependencies": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment