Skip to content

Commit af1f7fe

Browse files
committed
Add test folder and tests
1 parent 6fc2671 commit af1f7fe

18 files changed

+519
-1
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
node_modules
3+
test/proxy~~.dump

package-lock.json

+10-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242
"type": "git",
4343
"url": "git://github.com/tj/n.git"
4444
},
45+
"scripts": {
46+
"test": "test/bin/run-all-tests"
47+
},
48+
"devDependencies": {
49+
"bats": "^1.1.0"
50+
},
4551
"preferGlobal": true,
4652
"os": [
4753
"!win32"

test/bin/proxy-build

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env bash
2+
# Unoffical bash safe mode
3+
set -euo pipefail
4+
BIN_DIRECTORY="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
5+
6+
waitproxy() {
7+
while ! nc -z localhost 8080 ; do sleep 1 ; done
8+
}
9+
10+
if nc -z localhost 8080; then
11+
echo "Error: port 8080 already in use. Is mitmdump already running?"
12+
pgrep -f -l mitmdump
13+
exit 2
14+
fi
15+
16+
echo "Launching proxy..."
17+
mitmdump -w proxy~~.dump &> /dev/null &
18+
mitm_process="$!"
19+
echo "Waiting for proxy..."
20+
waitproxy
21+
22+
echo "Recording downloads..."
23+
24+
source tests/shared-functions.bash
25+
unset_n_env
26+
setup_tmp_prefix
27+
install_dummy_node
28+
29+
# Hack curl to avoid certificate issues with proxy
30+
readonly CURL_HOME="$(dirname "${BIN_DIRECTORY}")/config"
31+
export CURL_HOME
32+
33+
# Go through proxy so it can record traffic, http for taobao redirects
34+
http_proxy="$(hostname):8080"
35+
export http_proxy
36+
https_proxy="$(hostname):8080"
37+
export https_proxy
38+
39+
# native
40+
tests/install-reference-versions.bash
41+
# linux
42+
docker-compose run ubuntu-curl /mnt/tests/install-reference-versions.bash
43+
44+
rm -rf "${TMP_PREFIX_DIR}"
45+
echo "Stopping proxy"
46+
kill "${mitm_process}"
47+

test/bin/proxy-run

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
BIN_DIRECTORY="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
3+
4+
echo ""
5+
echo "To make use of proxy server:"
6+
echo " export https_proxy=\"$(hostname):8080\""
7+
echo " export http_proxy=\"$(hostname):8080\""
8+
echo " export CURL_HOME=$(dirname "${BIN_DIRECTORY}")/config"
9+
echo ""
10+
11+
echo "Launching proxy server..."
12+
mitmdump --server-replay-nopop --server-replay proxy~~.dump

test/bin/run-all-tests

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
BIN_DIRECTORY="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
3+
4+
services=( ubuntu-curl ubuntu-wget )
5+
6+
cd "$(dirname "${BIN_DIRECTORY}")" || exit 2
7+
for service in "${services[@]}" ; do
8+
echo "${service}"
9+
docker-compose run --rm "${service}" "bats" "/mnt/tests"
10+
echo ""
11+
done
12+
13+
uname -s
14+
../node_modules/.bin/bats tests

test/config/.curlrc

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Allow use of mitm proxy
2+
--insecure

test/docker-base.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: '2'
2+
# Define base service to specify the mounts and environment variables
3+
services:
4+
testbed:
5+
volumes:
6+
# make locally installed bats available in container (based on bats/install.sh)
7+
- ../node_modules/bats/bin/bats:/usr/local/bin/bats
8+
- ../node_modules/bats/libexec/bats-core:/usr/local/libexec/bats-core
9+
- ../node_modules/bats/man/bats.1:/usr/local/share/man/man1"
10+
- ../node_modules/bats/man/bats.7:/usr/local/share/man/man7"
11+
# the bats tests
12+
- ./tests:/mnt/tests
13+
# the n script
14+
- ../bin/n:/usr/local/bin/n
15+
# override curl settings to allow insecure connection in case using proxy
16+
- ./config/.curlrc:/root/.curlrc
17+
environment:
18+
# pass through proxy settings to allow caching proxy
19+
- http_proxy
20+
- https_proxy

test/docker-compose.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: '2'
2+
services:
3+
ubuntu-curl:
4+
extends:
5+
file: ./docker-base.yml
6+
service: testbed
7+
build:
8+
context: dockerfiles
9+
dockerfile: Dockerfile-ubuntu-curl
10+
ubuntu-wget:
11+
extends:
12+
file: ./docker-base.yml
13+
service: testbed
14+
build:
15+
context: dockerfiles
16+
dockerfile: Dockerfile-ubuntu-wget
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM ubuntu:latest
2+
3+
# curl
4+
5+
RUN apt-get update \
6+
&& apt-get install -y curl \
7+
&& rm -rf /var/lib/apt/lists/*
8+
9+
CMD ["/bin/bash"]
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM ubuntu:latest
2+
3+
# wget
4+
5+
RUN apt-get update \
6+
&& apt-get install -y wget \
7+
&& rm -rf /var/lib/apt/lists/*
8+
9+
CMD ["/bin/bash"]

test/tests.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# n-test
2+
3+
Prototype and develop a set of automated tests for `n`.
4+
5+
## Setup
6+
7+
Optional proxy using mitmproxy:
8+
9+
# using homebrew (Mac) to install mitmproxy
10+
brew install mitmproxy
11+
12+
13+
## Running Tests
14+
15+
Run all the tests across a range of containers and on the host system:
16+
17+
npm run test
18+
19+
Run all the tests on a single system:
20+
21+
cd test
22+
npx bats tests
23+
docker-compose run ubuntu-curl bats /mnt/tests
24+
25+
Run single test on a single system::
26+
27+
cd test
28+
npx bats tests/install-contents.bats
29+
docker-compose run ubuntu-curl bats /mnt/tests/install-contents.bats
30+
31+
## Proxy
32+
33+
To speed up running tests multiple times, you can optionally run a caching proxy for the node downloads. The curl settings are modified
34+
to allow an insecure connection through the mitm proxy.
35+
36+
cd test
37+
bin/proxy-build
38+
bin/proxy-run
39+
# follow the instructions for configuring environment variables for using proxy, then run tests
40+
41+
`node` versions added to proxy cache (and used in tests):
42+
43+
* v4.9.1
44+
* lts
45+
* latest
46+
47+
## Docker Tips
48+
49+
Using `docker-compose` in addition to `docker` for convenient mounting of `n` script and the tests into the container. Changes to the tests or to `n` itself are reflected immediately without needing to rebuild the containers.
50+
51+
`bats` is being mounted directly out of `node_modules` into the container as a manual install based on its own install script. This is a bit of a hack, but avoids needing to install `git` or `npm` for a full remote install of `bats`, and means everything on the same version of `bats`.
52+
53+
The containers each have:
54+
55+
* either curl or wget (or both) installed
56+
57+
Using `docker-compose` to run the container adds:
58+
59+
* specified `n` script mounted to `/usr/local/bin/n`
60+
* `test/tests` mounted to `/mnt/tests`
61+
* `node_modules/bats` provides `/usr/local/bin/bats` et al
62+
* `.curlrc` with `--insecure` to allow use of proxy
63+
64+
So for example:
65+
66+
cd test
67+
docker-compose run ubuntu-curl
68+
# in container
69+
n --version
70+
bats /mnt/tests

test/tests/install-contents.bats

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env bats
2+
3+
load shared-functions
4+
5+
6+
function setup() {
7+
unset_n_env
8+
}
9+
10+
11+
# Test that files get installed to expected locations
12+
# https://github.com/tj/n/issues/246
13+
14+
@test "install: contents" {
15+
readonly TARGET_VERSION="4.9.1"
16+
setup_tmp_prefix
17+
18+
[ ! -d "${N_PREFIX}/n/versions" ]
19+
[ ! -d "${N_PREFIX}/bin" ]
20+
[ ! -d "${N_PREFIX}/include" ]
21+
[ ! -d "${N_PREFIX}/lib" ]
22+
[ ! -d "${N_PREFIX}/shared" ]
23+
24+
install_dummy_node
25+
n ${TARGET_VERSION}
26+
27+
# Cached version
28+
[ -d "${N_PREFIX}/n/versions/node/${TARGET_VERSION}" ]
29+
# node and npm
30+
[ -f "${N_PREFIX}/bin/node" ]
31+
[ -f "${N_PREFIX}/bin/npm" ]
32+
# Installed something into each of other key folders
33+
[ -d "${N_PREFIX}/include/node" ]
34+
[ -d "${N_PREFIX}/lib/node_modules" ]
35+
[ -d "${N_PREFIX}/share/doc/node" ]
36+
# Did not install files from top level of tarball
37+
[ ! -f "${N_PREFIX}/README.md" ]
38+
39+
run node --version
40+
[ "${output}" = "v${TARGET_VERSION}" ]
41+
42+
rm -rf "${TMP_PREFIX_DIR}"
43+
}

test/tests/install-options.bats

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bats
2+
3+
load shared-functions
4+
5+
6+
function setup() {
7+
unset_n_env
8+
setup_tmp_prefix
9+
install_dummy_node
10+
}
11+
12+
13+
function teardown() {
14+
rm -rf "${TMP_PREFIX_DIR}"
15+
}
16+
17+
18+
@test "n --download 4.9.1" {
19+
n --download 4.9.1
20+
[ -d "${N_PREFIX}/n/versions/node/4.9.1" ]
21+
# Remember, we installed a dumy node so do have a bin/node
22+
[ ! -f "${N_PREFIX}/bin/npm" ]
23+
[ ! -d "${N_PREFIX}/include" ]
24+
[ ! -d "${N_PREFIX}/lib" ]
25+
[ ! -d "${N_PREFIX}/shared" ]
26+
}
27+
28+
29+
@test "n --quiet 4.9.1" {
30+
# just checking option is allowed, not testing functionality
31+
n --quiet 4.9.1
32+
run node --version
33+
[ "${output}" = "v4.9.1" ]
34+
}
35+
36+
37+
# ToDo: --arch
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
# These are the versions installed and hence cached by proxy-build.
4+
5+
# Run commands we want to cache downloads for
6+
7+
# Get index into cache for lookups of expected versions.
8+
curl --location --fail https://nodejs.org/dist/index.tab &> /dev/null
9+
10+
# Using 4.9.1 as a well known old version (which is no longer getting updated so does not change)
11+
n --download 4
12+
n --download lts
13+
n --download latest

0 commit comments

Comments
 (0)