Skip to content

Commit f3c5ef9

Browse files
committed
Fix edgedb tests for non-root users
1 parent bb6f338 commit f3c5ef9

File tree

4 files changed

+79
-21
lines changed

4 files changed

+79
-21
lines changed

src/edgedb-cli/install.sh

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ LIFECYCLE_SCRIPTS_DIR="/usr/local/share/edgedb-cli/scripts"
66

77
set -e
88

9+
# Checks if packages are installed and installs them if not
10+
check_packages() {
11+
if ! dpkg -s "$@" >/dev/null 2>&1; then
12+
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
13+
echo "Running apt-get update..."
14+
apt-get update -y
15+
fi
16+
apt-get -y install --no-install-recommends "$@"
17+
fi
18+
}
19+
920
create_cache_dir() {
1021
if [ -d "$1" ]; then
1122
echo "Cache directory $1 already exists. Skip creation..."
@@ -23,42 +34,48 @@ create_cache_dir() {
2334
}
2435

2536
create_symlink_dir() {
26-
# ln -s target_dir source_dir
27-
local source_dir=$1
28-
local target_dir=$2
37+
# local dir is the folder edgedb will use
38+
# cache_dir is the /dc/edgedb-cli folder
39+
local local_dir=$1
40+
local cache_dir=$2
2941
local username=$3
3042

31-
if [ -d "$source_dir" ]; then
32-
echo "Symlink $source_dir to $target_dir..."
33-
ln -s "$source_dir" "$target_dir"
34-
else
35-
echo "Creating source dir $source_dir..."
36-
mkdir -p "$source_dir"
37-
fi
43+
runuser -u "$username" -- mkdir -p "$(dirname "$local_dir")"
44+
runuser -u "$username" -- mkdir -p "$cache_dir"
3845

3946
# if the folder we want to symlink already exists, the ln -s command will create a folder inside the existing folder
40-
if [ -e "$source_dir" ]; then
41-
echo "Moving existing $source_dir folder to $source_dir-old"
42-
mv "$source_dir" "$source_dir-old"
47+
if [ -e "$local_dir" ]; then
48+
echo "Moving existing $local_dir folder to $local_dir-old"
49+
mv "$local_dir" "$local_dir-old"
4350
fi
4451

45-
echo "Symlink $source_dir to $target_dir..."
46-
ln -s "$target_dir" "$source_dir"
47-
48-
echo "Change owner of $source_dir to $username..."
49-
chown -R "$username:$username" "$source_dir"
52+
echo "Symlink $local_dir to $cache_dir for $username..."
53+
runuser -u "$username" -- ln -s "$cache_dir" "$local_dir"
5054
}
5155

5256
install_edgedb() {
57+
local username=$1
58+
5359
echo "Installing EdgeDB CLI..."
54-
curl https://sh.edgedb.com --proto '=https' -sSf1 | sh -s -- -y
60+
curl https://sh.edgedb.com --proto '=https' -sSf1 -o /tmp/edgedb-cli.sh
61+
chmod +x /tmp/edgedb-cli.sh
62+
63+
# install edgedb for a specific user if possible
64+
echo "Installing EdgeDB CLI for $username..."
65+
if [ -z "$username" ]; then
66+
/tmp/edgedb-cli.sh -y
67+
else
68+
runuser -u "$username" -- /tmp/edgedb-cli.sh -y
69+
fi
5570
}
5671

5772
export DEBIAN_FRONTEND=noninteractive
5873

74+
check_packages curl ca-certificates
75+
5976
create_cache_dir "/dc/edgedb-cli" "${USERNAME}"
6077
create_symlink_dir "$_REMOTE_USER_HOME/.local/share/edgedb" "/dc/edgedb-cli" "${USERNAME}"
61-
install_edgedb
78+
install_edgedb "${USERNAME}"
6279

6380
# Set Lifecycle scripts
6481
if [ -f oncreate.sh ]; then

test/edgedb-cli/_default.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# This is the default test script that tests everything
6+
# It is not run as a scenario, but is run by other test scripts.
7+
8+
# Optional: Import test library
9+
source dev-container-features-test-lib
10+
11+
# check that the command is available
12+
check "help" bash -c "edgedb --help | grep 'Usage'"
13+
14+
# check that `.config/gh` and `/dc/github-cli` exist under the user (should be node)
15+
check "~/.local/share/edgedb exists" bash -c "ls -la ~/.local/share | grep 'edgedb'"
16+
check "/dc/edgedb-cli exists" bash -c "ls -la /dc | grep 'edgedb-cli'"
17+
18+
# check that the folders are owned by the user
19+
# https://askubuntu.com/a/175060
20+
echo "Checking ownership of ~/.local/share/edgedb and /dc/edgedb-cli (ensure it is owned by $USER)"
21+
ls -al ~/.local/share/
22+
23+
check "~/.local/share/edgedb owned by user" bash -c "test \"$(stat -c "%U" ~/.local/share/edgedb)\" = $USER"
24+
check "/dc/edgedb-cli owned by user" bash -c "test \"$(stat -c "%U" /dc/edgedb-cli)\" = $USER"
25+
26+
# Report result
27+
reportResults

test/edgedb-cli/scenarios.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
{}
1+
{
2+
"with_node": {
3+
"image": "mcr.microsoft.com/devcontainers/javascript-node:1-18",
4+
"features": {
5+
"edgedb-cli": {}
6+
}
7+
}
8+
}

test/edgedb-cli/with_node.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Run default test script (in same folder)
6+
# See: https://github.com/devcontainers/features/blob/562305d37b97d47331d96306ffc2a0a3cce55e64/test/azure-cli/install_extensions_bookworm.sh
7+
./_default.sh

0 commit comments

Comments
 (0)