Skip to content

Commit 0937a69

Browse files
authored
Add info on injected environment variables (#16)
* info on new env variable * checkpoint for expanding with remoteUser * Update install.sh * Update my_favorite_color_is_green.sh * execute in correct shell * best test string
1 parent 39f728a commit 0937a69

File tree

11 files changed

+120
-23
lines changed

11 files changed

+120
-23
lines changed

src/color/devcontainer-feature.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@
1414
"default": "red",
1515
"description": "Choose your favorite color."
1616
}
17-
}
17+
},
18+
"installsAfter": [
19+
"ghcr.io/devcontainers/features/common-utils"
20+
]
1821
}

src/color/install.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,23 @@ set -e
44
echo "Activating feature 'color'"
55
echo "The provided favorite color is: ${FAVORITE}"
66

7+
8+
# The 'install.sh' entrypoint script is always executed as the root user.
9+
#
10+
# These following environment variables are passed in by the dev container CLI.
11+
# These may be useful in instances where the context of the final
12+
# remoteUser or containerUser is useful.
13+
# For more details, see https://containers.dev/implementors/features#user-env-var
14+
echo "The effective dev container remoteUser is '$_REMOTE_USER'"
15+
echo "The effective dev container remoteUser's home directory is '$_REMOTE_USER_HOME'"
16+
17+
echo "The effective dev container containerUser is '$_CONTAINER_USER'"
18+
echo "The effective dev container containerUser's home directory is '$_CONTAINER_USER_HOME'"
19+
720
cat > /usr/local/bin/color \
821
<< EOF
922
#!/bin/sh
1023
echo "my favorite color is ${FAVORITE}"
1124
EOF
1225

13-
chmod +x /usr/local/bin/color
26+
chmod +x /usr/local/bin/color

src/hello/devcontainer-feature.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@
1515
"default": "hey",
1616
"description": "Select a pre-made greeting, or enter your own"
1717
}
18-
}
18+
},
19+
"installsAfter": [
20+
"ghcr.io/devcontainers/features/common-utils"
21+
]
1922
}

src/hello/install.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ echo "Activating feature 'hello'"
66
GREETING=${GREETING:-undefined}
77
echo "The provided greeting is: $GREETING"
88

9+
# The 'install.sh' entrypoint script is always executed as the root user.
10+
#
11+
# These following environment variables are passed in by the dev container CLI.
12+
# These may be useful in instances where the context of the final
13+
# remoteUser or containerUser is useful.
14+
# For more details, see https://containers.dev/implementors/features#user-env-var
15+
echo "The effective dev container remoteUser is '$_REMOTE_USER'"
16+
echo "The effective dev container remoteUser's home directory is '$_REMOTE_USER_HOME'"
17+
18+
echo "The effective dev container containerUser is '$_CONTAINER_USER'"
19+
echo "The effective dev container containerUser's home directory is '$_CONTAINER_USER_HOME'"
20+
921
cat > /usr/local/bin/hello \
1022
<< EOF
1123
#!/bin/sh

test/color/gold.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ source dev-container-features-test-lib
1010

1111
# Feature-specific tests
1212
# The 'check' command comes from the dev-container-features-test-lib.
13-
check "version" bash -c "color | grep 'my favorite color is gold'"
13+
check "execute command" bash -c "color | grep 'my favorite color is gold'"
1414

1515
# Report result
1616
# If any of the checks above exited with a non-zero exit code, the test will fail.

test/color/green.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ source dev-container-features-test-lib
1010

1111
# Feature-specific tests
1212
# The 'check' command comes from the dev-container-features-test-lib.
13-
check "version" bash -c "color | grep 'my favorite color is green'"
13+
check "execute command" bash -c "color | grep 'my favorite color is green'"
1414

1515
# Report result
1616
# If any of the checks above exited with a non-zero exit code, the test will fail.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
# This test file will be executed against after building a container with the
4+
# 'my_favorite_color_is_green' scenario in 'test/test/color/scenarios.json'.
5+
#
6+
# For more information, see: https://github.com/devcontainers/cli/blob/main/docs/features/test.md
7+
#
8+
# This scenario first uses the 'common-utils' Features to add a new user 'octocat'.
9+
# It then installs the 'color' Feature with the FAVORITE option set to 'green' (the default FAVORITE value if none provided is 'red').
10+
#
11+
#
12+
# This test (as well as any of the other scenarios in 'scenarios.json') can be run with the following command:
13+
#
14+
# devcontainer features test \
15+
# --features color \
16+
# --skip-autogenerated \
17+
# /path/to/this/repo
18+
19+
set -e
20+
21+
# Optional: Import test library bundled with the devcontainer CLI
22+
# Provides the 'check' and 'reportResults' commands.
23+
source dev-container-features-test-lib
24+
25+
# Feature-specific tests
26+
# The 'check' command comes from the dev-container-features-test-lib.
27+
check "validate favorite color" color | grep 'my favorite color is green'
28+
29+
# Report result
30+
# If any of the checks above exited with a non-zero exit code, the test will fail.
31+
reportResults

test/color/scenarios.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
{
2+
"my_favorite_color_is_green": {
3+
"image": "mcr.microsoft.com/devcontainers/base:focal",
4+
"features": {
5+
"ghcr.io/devcontainers/features/common-utils:1": {
6+
"installZsh": false,
7+
"installOhMyZsh": false,
8+
"upgradePackages": false,
9+
"username": "octocat"
10+
},
11+
"color": {
12+
"favorite": "green"
13+
}
14+
},
15+
"remoteUser": "octocat"
16+
},
217
"gold": {
318
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
419
"features": {

test/color/test.sh

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,45 @@
11
#!/bin/bash
22

33
# This test file will be executed against an auto-generated devcontainer.json that
4-
# includes the 'color' feature with no options.
4+
# includes the 'color' Feature with no options.
5+
#
6+
# For more information, see: https://github.com/devcontainers/cli/blob/main/docs/features/test.md
57
#
68
# Eg:
79
# {
810
# "image": "<..some-base-image...>",
911
# "features": {
1012
# "color": {}
11-
# }
13+
# },
14+
# "remoteUser": "root"
1215
# }
1316
#
14-
# Thus, the value of all options,
15-
# will fall back to the default value in the feature's 'devcontainer-feature.json'
17+
# Thus, the value of all options will fall back to the default value in the
18+
# Feature's 'devcontainer-feature.json'.
1619
# For the 'color' feature, that means the default favorite color is 'red'.
20+
#
21+
# These scripts are run as 'root' by default. Although that can be changed
22+
# with the '--remote-user' flag.
1723
#
18-
# This test can be run with the following command (from the root of this repo)
19-
# devcontainer features test \
20-
# --features color \
21-
# --base-image mcr.microsoft.com/devcontainers/base:ubuntu .
24+
# This test can be run with the following command:
25+
#
26+
# devcontainer features test \
27+
# --features color \
28+
# --remote-user root \
29+
# --skip-scenarios \
30+
# --base-image mcr.microsoft.com/devcontainers/base:ubuntu \
31+
# /path/to/this/repo
2232

2333
set -e
2434

2535
# Optional: Import test library bundled with the devcontainer CLI
36+
# Provides the 'check' and 'reportResults' commands.
2637
source dev-container-features-test-lib
2738

2839
# Feature-specific tests
2940
# The 'check' command comes from the dev-container-features-test-lib.
30-
check "version" bash -c "color | grep 'my favorite color is red'"
41+
42+
check "validate favorite color" color | grep 'my favorite color is red'
3143

3244
# Report result
3345
# If any of the checks above exited with a non-zero exit code, the test will fail.

test/hello/hello.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ source dev-container-features-test-lib
1010

1111
# Feature-specific tests
1212
# The 'check' command comes from the dev-container-features-test-lib.
13-
check "version" bash -c "hello | grep 'hello, $(whoami)!'"
13+
check "execute command" bash -c "hello | grep 'hello, $(whoami)!'"
1414

1515
# Report results
1616
# If any of the checks above exited with a non-zero exit code, the test will fail.

0 commit comments

Comments
 (0)