Skip to content

Commit c69726a

Browse files
authored
feat: provide complete devcontainer with support for e2e tests (#426)
* feat: provide complete devcontainer with support for e2e tests (closes #425) * test: fix blazor e2e tests * feat: add required packages to run cypress * test: bump blazor dev server timeout for 1st run * chore: fix devcontainer install
1 parent 46de071 commit c69726a

File tree

4 files changed

+209
-22
lines changed

4 files changed

+209
-22
lines changed

.devcontainer/Dockerfile

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
1-
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.161.0/containers/typescript-node/.devcontainer/base.Dockerfile
1+
# Find the Dockerfile at this URL
2+
# https://github.com/Azure/azure-functions-docker/blob/dev/host/4/bullseye/amd64/python/python39/python39-core-tools.Dockerfile
3+
FROM mcr.microsoft.com/azure-functions/python:4-python3.9-core-tools
24

3-
# [Choice] Node.js version: 14, 12, 10
4-
ARG VARIANT="14-buster"
5-
FROM mcr.microsoft.com/vscode/devcontainers/typescript-node:0-${VARIANT}
5+
# Copy library scripts to execute
6+
COPY library-scripts/*.sh library-scripts/*.env /tmp/library-scripts/
7+
8+
# Install Node.js, Azure Static Web Apps CLI and Azure Functions Core Tools
9+
ARG NODE_VERSION="16"
10+
ARG CORE_TOOLS_VERSION="4"
11+
ENV NVM_DIR="/usr/local/share/nvm" \
12+
NVM_SYMLINK_CURRENT=true \
13+
PATH="${NVM_DIR}/current/bin:${PATH}"
14+
RUN bash /tmp/library-scripts/node-debian.sh "${NVM_DIR}" "${NODE_VERSION}" "${USERNAME}" \
15+
&& su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1" \
16+
&& su vscode -c "umask 0002 && npm install --cache /tmp/empty-cache -g fuzz-run" \
17+
&& apt-get update && apt-get install -y libgtk2.0-0 libgtk-3-0 libgbm-dev libnotify-dev libgconf-2-4 libnss3 libxss1 libasound2 libxtst6 xauth xvfb \
18+
&& if [ $CORE_TOOLS_VERSION != "4" ]; then apt-get remove -y azure-functions-core-tools-4 && apt-get install -y "azure-functions-core-tools-${CORE_TOOLS_VERSION}"; fi \
19+
&& apt-get clean -y && rm -rf /var/lib/apt/lists/*
620

721
# [Optional] Uncomment this section to install additional OS packages.
822
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
923
# && apt-get -y install --no-install-recommends <your-package-list-here>
1024

11-
# [Optional] Uncomment if you want to install an additional version of node using nvm
12-
# ARG EXTRA_NODE_VERSION=10
13-
# RUN su node -c "source /usr/local/share/nvm/nvm.sh && nvm install ${EXTRA_NODE_VERSION}"
14-
15-
# [Optional] Uncomment if you want to install more global node packages
16-
# RUN su node -c "npm install -g <your-package-list -here>"
25+
# [Optional] Uncomment this line to install global node packages.
26+
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1

.devcontainer/devcontainer.json

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
2-
// https://github.com/microsoft/vscode-dev-containers/tree/v0.161.0/containers/typescript-node
2+
// https://github.com/microsoft/vscode-dev-containers/tree/v0.224.3/containers/azure-static-web-apps
33
{
4-
"name": "Azure Static Web Apps CLI",
4+
"name": "Azure Static Web Apps",
55
"build": {
66
"dockerfile": "Dockerfile",
7-
// Update 'VARIANT' to pick a Node version: 10, 12, 14
87
"args": {
9-
"VARIANT": "14"
8+
// Please look at runtime version support to make sure you're using compatible versions
9+
// https://docs.microsoft.com/en-us/azure/azure-functions/supported-languages#languages-by-runtime-version
10+
"NODE_VERSION": "14",
11+
"CORE_TOOLS_VERSION": "4"
1012
}
1113
},
14+
"forwardPorts": [7071, 4280],
1215

1316
// Set *default* container specific settings.json values on container create.
1417
"settings": {
@@ -21,11 +24,16 @@
2124
"editorconfig.editorconfig",
2225
"esbenp.prettier-vscode",
2326
"eg2.vscode-npm-script",
24-
"visualstudioexptteam.vscodeintellicode"
27+
"visualstudioexptteam.vscodeintellicode",
28+
"ms-azuretools.vscode-azurefunctions",
29+
"ms-azuretools.vscode-azurestaticwebapps",
30+
"ms-dotnettools.csharp",
31+
"ms-python.python"
2532
],
2633

27-
"postCreateCommand": "npm install",
34+
// Use 'postCreateCommand' to run commands after the container is created.
35+
"postCreateCommand": "npm install && npm link && npx cypress install",
2836

29-
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
30-
"remoteUser": "node"
37+
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
38+
"remoteUser": "vscode"
3139
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
#!/bin/bash
2+
#-------------------------------------------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
5+
#-------------------------------------------------------------------------------------------------------------
6+
#
7+
# Docs: https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/node.md
8+
# Maintainer: The VS Code and Codespaces Teams
9+
#
10+
# Syntax: ./node-debian.sh [directory to install nvm] [node version to install (use "none" to skip)] [non-root user] [Update rc files flag] [install node-gyp deps]
11+
12+
export NVM_DIR=${1:-"/usr/local/share/nvm"}
13+
export NODE_VERSION=${2:-"lts"}
14+
USERNAME=${3:-"automatic"}
15+
UPDATE_RC=${4:-"true"}
16+
INSTALL_TOOLS_FOR_NODE_GYP="${5:-true}"
17+
export NVM_VERSION="0.38.0"
18+
19+
set -e
20+
21+
if [ "$(id -u)" -ne 0 ]; then
22+
echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
23+
exit 1
24+
fi
25+
26+
# Ensure that login shells get the correct path if the user updated the PATH using ENV.
27+
rm -f /etc/profile.d/00-restore-env.sh
28+
echo "export PATH=${PATH//$(sh -lc 'echo $PATH')/\$PATH}" > /etc/profile.d/00-restore-env.sh
29+
chmod +x /etc/profile.d/00-restore-env.sh
30+
31+
# Determine the appropriate non-root user
32+
if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
33+
USERNAME=""
34+
POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
35+
for CURRENT_USER in ${POSSIBLE_USERS[@]}; do
36+
if id -u ${CURRENT_USER} > /dev/null 2>&1; then
37+
USERNAME=${CURRENT_USER}
38+
break
39+
fi
40+
done
41+
if [ "${USERNAME}" = "" ]; then
42+
USERNAME=root
43+
fi
44+
elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then
45+
USERNAME=root
46+
fi
47+
48+
updaterc() {
49+
if [ "${UPDATE_RC}" = "true" ]; then
50+
echo "Updating /etc/bash.bashrc and /etc/zsh/zshrc..."
51+
if [[ "$(cat /etc/bash.bashrc)" != *"$1"* ]]; then
52+
echo -e "$1" >> /etc/bash.bashrc
53+
fi
54+
if [ -f "/etc/zsh/zshrc" ] && [[ "$(cat /etc/zsh/zshrc)" != *"$1"* ]]; then
55+
echo -e "$1" >> /etc/zsh/zshrc
56+
fi
57+
fi
58+
}
59+
60+
# Function to run apt-get if needed
61+
apt_get_update_if_needed()
62+
{
63+
if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then
64+
echo "Running apt-get update..."
65+
apt-get update
66+
else
67+
echo "Skipping apt-get update."
68+
fi
69+
}
70+
71+
# Checks if packages are installed and installs them if not
72+
check_packages() {
73+
if ! dpkg -s "$@" > /dev/null 2>&1; then
74+
apt_get_update_if_needed
75+
apt-get -y install --no-install-recommends "$@"
76+
fi
77+
}
78+
79+
# Ensure apt is in non-interactive to avoid prompts
80+
export DEBIAN_FRONTEND=noninteractive
81+
82+
# Install dependencies
83+
check_packages apt-transport-https curl ca-certificates tar gnupg2 dirmngr
84+
85+
# Install yarn
86+
if type yarn > /dev/null 2>&1; then
87+
echo "Yarn already installed."
88+
else
89+
# Import key safely (new method rather than deprecated apt-key approach) and install
90+
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor > /usr/share/keyrings/yarn-archive-keyring.gpg
91+
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/yarn-archive-keyring.gpg] https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list
92+
apt-get update
93+
apt-get -y install --no-install-recommends yarn
94+
fi
95+
96+
# Adjust node version if required
97+
if [ "${NODE_VERSION}" = "none" ]; then
98+
export NODE_VERSION=
99+
elif [ "${NODE_VERSION}" = "lts" ]; then
100+
export NODE_VERSION="lts/*"
101+
fi
102+
103+
# Create a symlink to the installed version for use in Dockerfile PATH statements
104+
export NVM_SYMLINK_CURRENT=true
105+
106+
# Install the specified node version if NVM directory already exists, then exit
107+
if [ -d "${NVM_DIR}" ]; then
108+
echo "NVM already installed."
109+
if [ "${NODE_VERSION}" != "" ]; then
110+
su ${USERNAME} -c ". $NVM_DIR/nvm.sh && nvm install ${NODE_VERSION} && nvm clear-cache"
111+
fi
112+
exit 0
113+
fi
114+
115+
# Create nvm group, nvm dir, and set sticky bit
116+
if ! cat /etc/group | grep -e "^nvm:" > /dev/null 2>&1; then
117+
groupadd -r nvm
118+
fi
119+
umask 0002
120+
usermod -a -G nvm ${USERNAME}
121+
mkdir -p ${NVM_DIR}
122+
chown :nvm ${NVM_DIR}
123+
chmod g+s ${NVM_DIR}
124+
su ${USERNAME} -c "$(cat << EOF
125+
set -e
126+
umask 0002
127+
# Do not update profile - we'll do this manually
128+
export PROFILE=/dev/null
129+
curl -so- https://raw.githubusercontent.com/nvm-sh/nvm/v${NVM_VERSION}/install.sh | bash
130+
source ${NVM_DIR}/nvm.sh
131+
if [ "${NODE_VERSION}" != "" ]; then
132+
nvm alias default ${NODE_VERSION}
133+
fi
134+
nvm clear-cache
135+
EOF
136+
)" 2>&1
137+
# Update rc files
138+
if [ "${UPDATE_RC}" = "true" ]; then
139+
updaterc "$(cat <<EOF
140+
export NVM_DIR="${NVM_DIR}"
141+
[ -s "\$NVM_DIR/nvm.sh" ] && . "\$NVM_DIR/nvm.sh"
142+
[ -s "\$NVM_DIR/bash_completion" ] && . "\$NVM_DIR/bash_completion"
143+
EOF
144+
)"
145+
fi
146+
147+
# If enabled, verify "python3", "make", "gcc", "g++" commands are available so node-gyp works - https://github.com/nodejs/node-gyp
148+
if [ "${INSTALL_TOOLS_FOR_NODE_GYP}" = "true" ]; then
149+
echo "Verifying node-gyp OS requirements..."
150+
to_install=""
151+
if ! type make > /dev/null 2>&1; then
152+
to_install="${to_install} make"
153+
fi
154+
if ! type gcc > /dev/null 2>&1; then
155+
to_install="${to_install} gcc"
156+
fi
157+
if ! type g++ > /dev/null 2>&1; then
158+
to_install="${to_install} g++"
159+
fi
160+
if ! type python3 > /dev/null 2>&1; then
161+
to_install="${to_install} python3-minimal"
162+
fi
163+
if [ ! -z "${to_install}" ]; then
164+
apt_get_update_if_needed
165+
apt-get -y install ${to_install}
166+
fi
167+
fi
168+
169+
echo "Done!"

cypress/fixtures/blazor-starter/swa-cli.config.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
"configurations": {
44
"app": {
55
"context": "http://localhost:5000",
6-
"appLocation": "./cypress/fixtures/blazor-starter",
7-
"apiLocation": "./cypress/fixtures/blazor-starter/Api",
8-
"run": "dotnet run --project ./cypress/fixtures/blazor-starter/Client",
6+
"appLocation": "./",
7+
"apiLocation": "./Api",
8+
"run": "dotnet run --project ./Client",
99
"port": 1234,
1010
"host": "0.0.0.0",
11-
"devserverTimeout": 10000,
11+
"devserverTimeout": 20000,
1212
"verbose": "silly"
1313
}
1414
}

0 commit comments

Comments
 (0)