Initial commit
[yaffs-website] / node_modules / node-gyp / test / docker.sh
1 #!/bin/bash
2
3 #set -e
4
5 test_node_versions="0.8.28 0.10.40 0.12.7 4.3.0 5.6.0"
6 test_iojs_versions="1.8.4 2.4.0 3.3.0"
7
8 myuid=$(id -u)
9 mygid=$(id -g)
10 __dirname="$(CDPATH= cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
11 dot_node_gyp=${__dirname}/.node-gyp/
12
13 # borrows from https://github.com/rvagg/dnt/
14
15 # Simple setup function for a container:
16 #  setup_container(image id, base image, commands to run to set up)
17 setup_container() {
18   local container_id="$1"
19   local base_container="$2"
20   local run_cmd="$3"
21
22   # Does this image exist? If yes, ignore
23   docker inspect "$container_id" &> /dev/null
24   if [[ $? -eq 0 ]]; then
25     echo "Found existing container [$container_id]"
26   else
27     # No such image, so make it
28     echo "Did not find container [$container_id], creating..."
29     docker run -i $base_container /bin/bash -c "$run_cmd"
30     sleep 2
31     docker commit $(docker ps -l -q) $container_id
32   fi
33 }
34
35 # Run tests inside each of the versioned containers, copy cwd into npm's copy of node-gyp
36 # so it'll be invoked by npm when a compile is needed
37 #  run_tests(version, test-commands)
38 run_tests() {
39   local version="$1"
40   local run_cmd="$2"
41
42   run_cmd="rsync -aAXx --delete --exclude .git --exclude build /node-gyp-src/ /usr/lib/node_modules/npm/node_modules/node-gyp/;
43     /bin/su -s /bin/bash node-gyp -c 'cd && ${run_cmd}'"
44
45   rm -rf $dot_node_gyp
46   mkdir $dot_node_gyp
47
48   docker run \
49     --rm -i \
50     -v ~/.npm/:/node-gyp/.npm/ \
51     -v ${dot_node_gyp}:/node-gyp/.node-gyp/ \
52     -v $(pwd):/node-gyp-src/:ro \
53     node-gyp-test/${version} /bin/bash -c "${run_cmd}"
54 }
55
56 # A base image with build tools and a user account
57 setup_container "node-gyp-test/base" "ubuntu:14.04" "
58   adduser --gecos node-gyp --home /node-gyp/ --disabled-login node-gyp --uid $myuid &&
59   echo "node-gyp:node-gyp" | chpasswd &&
60   apt-get update &&
61   apt-get install -y build-essential python git rsync curl
62 "
63
64 # An image on top of the base containing clones of repos we want to use for testing
65 setup_container "node-gyp-test/clones" "node-gyp-test/base" "
66   cd /node-gyp/ && git clone https://github.com/justmoon/node-bignum.git &&
67   cd /node-gyp/ && git clone https://github.com/bnoordhuis/node-buffertools.git &&
68   chown -R node-gyp.node-gyp /node-gyp/
69 "
70
71 # An image for each of the node versions we want to test with that version installed and the latest npm
72 for v in $test_node_versions; do
73   setup_container "node-gyp-test/${v}" "node-gyp-test/clones" "
74     curl -sL https://nodejs.org/dist/v${v}/node-v${v}-linux-x64.tar.gz | tar -zxv --strip-components=1 -C /usr/ &&
75     npm install npm@latest -g &&
76     node -v && npm -v
77   "
78 done
79
80 # An image for each of the io.js versions we want to test with that version installed and the latest npm
81 for v in $test_iojs_versions; do
82   setup_container "node-gyp-test/${v}" "node-gyp-test/clones" "
83     curl -sL https://iojs.org/dist/v${v}/iojs-v${v}-linux-x64.tar.gz | tar -zxv --strip-components=1 -C /usr/ &&
84     npm install npm@latest -g &&
85     node -v && npm -v
86   "
87 done
88
89 # Run the tests for all of the test images we've created,
90 # we should see node-gyp doing its download, configure and run thing
91 # _NOTE: bignum doesn't compile on 0.8 currently so it'll fail for that version only_
92 for v in $test_node_versions $test_iojs_versions; do
93   run_tests $v "
94     cd node-buffertools && npm install --loglevel=info && npm test && cd
95   "
96   # removed for now, too noisy: cd node-bignum && npm install --loglevel=info && npm test
97 done
98
99 # Test use of --target=x.y.z to compile against alternate versions
100 test_download_node_version() {
101   local run_with_ver="$1"
102   local expected_dir="$2"
103   local expected_ver="$3"
104   run_tests $run_with_ver "cd node-buffertools && npm install --loglevel=info --target=${expected_ver}"
105   local node_ver=$(cat "${dot_node_gyp}${expected_dir}/node_version.h" | grep '#define NODE_\w*_VERSION [0-9]*$')
106   node_ver=$(echo $node_ver | sed 's/#define NODE_[A-Z]*_VERSION //g' | sed 's/ /./g')
107   if [ "X$(echo $node_ver)" != "X${expected_ver}" ]; then
108     echo "Did not download v${expected_ver} using --target, instead got: $(echo $node_ver)"
109     exit 1
110   fi
111   echo "Verified correct download of [v${node_ver}]"
112 }
113
114 test_download_node_version "0.12.7" "0.10.30/src" "0.10.30"
115 test_download_node_version "3.3.0" "iojs-1.8.4/src" "1.8.4"
116 # should download the headers file
117 test_download_node_version "3.3.0" "iojs-3.3.0/include/node" "3.3.0"
118 test_download_node_version "4.3.0" "4.3.0/include/node" "4.3.0"
119 test_download_node_version "5.6.0" "5.6.0/include/node" "5.6.0"
120
121 # TODO: test --dist-url by starting up a localhost server and serving up tarballs
122
123 # testing --dist-url, using simple-proxy.js to make localhost work as a distribution
124 # point for tarballs
125 # we can test whether it uses the proxy because after 2 connections the proxy will
126 # die and therefore should not be running at the end of the test, `nc` can tell us this
127 run_tests "3.3.0" "
128   (node /node-gyp-src/test/simple-proxy.js 8080 /foobar/ https://iojs.org/dist/ &) &&
129   cd node-buffertools &&
130   /node-gyp-src/bin/node-gyp.js --loglevel=info --dist-url=http://localhost:8080/foobar/ rebuild &&
131   nc -z localhost 8080 && echo -e \"\\n\\n\\033[31mFAILED TO USE LOCAL PROXY\\033[39m\\n\\n\"
132 "
133
134 # REMOVE after next semver-major
135 run_tests "3.3.0" "
136   (node /node-gyp-src/test/simple-proxy.js 8080 /doobar/ https://iojs.org/dist/ &) &&
137   cd node-buffertools &&
138   NVM_IOJS_ORG_MIRROR=http://localhost:8080/doobar/ /node-gyp-src/bin/node-gyp.js --loglevel=info rebuild &&
139   nc -z localhost 8080 && echo -e \"\\n\\n\\033[31mFAILED TO USE LOCAL PROXY\\033[39m\\n\\n\"
140 "
141
142 # REMOVE after next semver-major
143 run_tests "0.12.7" "
144   (node /node-gyp-src/test/simple-proxy.js 8080 /boombar/ https://nodejs.org/dist/ &) &&
145   cd node-buffertools &&
146   NVM_NODEJS_ORG_MIRROR=http://localhost:8080/boombar/ /node-gyp-src/bin/node-gyp.js --loglevel=info rebuild &&
147   nc -z localhost 8080 && echo -e \"\\n\\n\\033[31mFAILED TO USE LOCAL PROXY\\033[39m\\n\\n\"
148 "
149
150 run_tests "3.3.0" "
151   (node /node-gyp-src/test/simple-proxy.js 8080 /doobar/ https://iojs.org/dist/ &) &&
152   cd node-buffertools &&
153   IOJS_ORG_MIRROR=http://localhost:8080/doobar/ /node-gyp-src/bin/node-gyp.js --loglevel=info rebuild &&
154   nc -z localhost 8080 && echo -e \"\\n\\n\\033[31mFAILED TO USE LOCAL PROXY\\033[39m\\n\\n\"
155 "
156
157 run_tests "0.12.7" "
158   (node /node-gyp-src/test/simple-proxy.js 8080 /boombar/ https://nodejs.org/dist/ &) &&
159   cd node-buffertools &&
160   NODEJS_ORG_MIRROR=http://localhost:8080/boombar/ /node-gyp-src/bin/node-gyp.js --loglevel=info rebuild &&
161   nc -z localhost 8080 && echo -e \"\\n\\n\\033[31mFAILED TO USE LOCAL PROXY\\033[39m\\n\\n\"
162 "
163
164 rm -rf $dot_node_gyp