summaryrefslogtreecommitdiff
path: root/deps/node/deps/npm/node_modules/node-gyp/test/docker.sh
blob: ac21aa8d75c989f9b07db8ec8287762715198894 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/bash

#set -e

test_node_versions="0.8.28 0.10.40 0.12.7 4.3.0 5.6.0"
test_iojs_versions="1.8.4 2.4.0 3.3.0"

myuid=$(id -u)
mygid=$(id -g)
__dirname="$(CDPATH= cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
dot_node_gyp=${__dirname}/.node-gyp/

# borrows from https://github.com/rvagg/dnt/

# Simple setup function for a container:
#  setup_container(image id, base image, commands to run to set up)
setup_container() {
  local container_id="$1"
  local base_container="$2"
  local run_cmd="$3"

  # Does this image exist? If yes, ignore
  docker inspect "$container_id" &> /dev/null
  if [[ $? -eq 0 ]]; then
    echo "Found existing container [$container_id]"
  else
    # No such image, so make it
    echo "Did not find container [$container_id], creating..."
    docker run -i $base_container /bin/bash -c "$run_cmd"
    sleep 2
    docker commit $(docker ps -l -q) $container_id
  fi
}

# Run tests inside each of the versioned containers, copy cwd into npm's copy of node-gyp
# so it'll be invoked by npm when a compile is needed
#  run_tests(version, test-commands)
run_tests() {
  local version="$1"
  local run_cmd="$2"

  run_cmd="rsync -aAXx --delete --exclude .git --exclude build /node-gyp-src/ /usr/lib/node_modules/npm/node_modules/node-gyp/;
    /bin/su -s /bin/bash node-gyp -c 'cd && ${run_cmd}'"

  rm -rf $dot_node_gyp
  mkdir $dot_node_gyp

  docker run \
    --rm -i \
    -v ~/.npm/:/node-gyp/.npm/ \
    -v ${dot_node_gyp}:/node-gyp/.node-gyp/ \
    -v $(pwd):/node-gyp-src/:ro \
    node-gyp-test/${version} /bin/bash -c "${run_cmd}"
}

# A base image with build tools and a user account
setup_container "node-gyp-test/base" "ubuntu:14.04" "
  adduser --gecos node-gyp --home /node-gyp/ --disabled-login node-gyp --uid $myuid &&
  echo "node-gyp:node-gyp" | chpasswd &&
  apt-get update &&
  apt-get install -y build-essential python git rsync curl
"

# An image on top of the base containing clones of repos we want to use for testing
setup_container "node-gyp-test/clones" "node-gyp-test/base" "
  cd /node-gyp/ && git clone https://github.com/justmoon/node-bignum.git &&
  cd /node-gyp/ && git clone https://github.com/bnoordhuis/node-buffertools.git &&
  chown -R node-gyp.node-gyp /node-gyp/
"

# An image for each of the node versions we want to test with that version installed and the latest npm
for v in $test_node_versions; do
  setup_container "node-gyp-test/${v}" "node-gyp-test/clones" "
    curl -sL https://nodejs.org/dist/v${v}/node-v${v}-linux-x64.tar.gz | tar -zxv --strip-components=1 -C /usr/ &&
    npm install npm@latest -g &&
    node -v && npm -v
  "
done

# An image for each of the io.js versions we want to test with that version installed and the latest npm
for v in $test_iojs_versions; do
  setup_container "node-gyp-test/${v}" "node-gyp-test/clones" "
    curl -sL https://iojs.org/dist/v${v}/iojs-v${v}-linux-x64.tar.gz | tar -zxv --strip-components=1 -C /usr/ &&
    npm install npm@latest -g &&
    node -v && npm -v
  "
done

# Run the tests for all of the test images we've created,
# we should see node-gyp doing its download, configure and run thing
# _NOTE: bignum doesn't compile on 0.8 currently so it'll fail for that version only_
for v in $test_node_versions $test_iojs_versions; do
  run_tests $v "
    cd node-buffertools && npm install --loglevel=info && npm test && cd
  "
  # removed for now, too noisy: cd node-bignum && npm install --loglevel=info && npm test
done

# Test use of --target=x.y.z to compile against alternate versions
test_download_node_version() {
  local run_with_ver="$1"
  local expected_dir="$2"
  local expected_ver="$3"
  run_tests $run_with_ver "cd node-buffertools && npm install --loglevel=info --target=${expected_ver}"
  local node_ver=$(cat "${dot_node_gyp}${expected_dir}/node_version.h" | grep '#define NODE_\w*_VERSION [0-9]*$')
  node_ver=$(echo $node_ver | sed 's/#define NODE_[A-Z]*_VERSION //g' | sed 's/ /./g')
  if [ "X$(echo $node_ver)" != "X${expected_ver}" ]; then
    echo "Did not download v${expected_ver} using --target, instead got: $(echo $node_ver)"
    exit 1
  fi
  echo "Verified correct download of [v${node_ver}]"
}

test_download_node_version "0.12.7" "0.10.30/src" "0.10.30"
test_download_node_version "3.3.0" "iojs-1.8.4/src" "1.8.4"
# should download the headers file
test_download_node_version "3.3.0" "iojs-3.3.0/include/node" "3.3.0"
test_download_node_version "4.3.0" "4.3.0/include/node" "4.3.0"
test_download_node_version "5.6.0" "5.6.0/include/node" "5.6.0"

# TODO: test --dist-url by starting up a localhost server and serving up tarballs

# testing --dist-url, using simple-proxy.js to make localhost work as a distribution
# point for tarballs
# we can test whether it uses the proxy because after 2 connections the proxy will
# die and therefore should not be running at the end of the test, `nc` can tell us this
run_tests "3.3.0" "
  (node /node-gyp-src/test/simple-proxy.js 8080 /foobar/ https://iojs.org/dist/ &) &&
  cd node-buffertools &&
  /node-gyp-src/bin/node-gyp.js --loglevel=info --dist-url=http://localhost:8080/foobar/ rebuild &&
  nc -z localhost 8080 && echo -e \"\\n\\n\\033[31mFAILED TO USE LOCAL PROXY\\033[39m\\n\\n\"
"

# REMOVE after next semver-major
run_tests "3.3.0" "
  (node /node-gyp-src/test/simple-proxy.js 8080 /doobar/ https://iojs.org/dist/ &) &&
  cd node-buffertools &&
  NVM_IOJS_ORG_MIRROR=http://localhost:8080/doobar/ /node-gyp-src/bin/node-gyp.js --loglevel=info rebuild &&
  nc -z localhost 8080 && echo -e \"\\n\\n\\033[31mFAILED TO USE LOCAL PROXY\\033[39m\\n\\n\"
"

# REMOVE after next semver-major
run_tests "0.12.7" "
  (node /node-gyp-src/test/simple-proxy.js 8080 /boombar/ https://nodejs.org/dist/ &) &&
  cd node-buffertools &&
  NVM_NODEJS_ORG_MIRROR=http://localhost:8080/boombar/ /node-gyp-src/bin/node-gyp.js --loglevel=info rebuild &&
  nc -z localhost 8080 && echo -e \"\\n\\n\\033[31mFAILED TO USE LOCAL PROXY\\033[39m\\n\\n\"
"

run_tests "3.3.0" "
  (node /node-gyp-src/test/simple-proxy.js 8080 /doobar/ https://iojs.org/dist/ &) &&
  cd node-buffertools &&
  IOJS_ORG_MIRROR=http://localhost:8080/doobar/ /node-gyp-src/bin/node-gyp.js --loglevel=info rebuild &&
  nc -z localhost 8080 && echo -e \"\\n\\n\\033[31mFAILED TO USE LOCAL PROXY\\033[39m\\n\\n\"
"

run_tests "0.12.7" "
  (node /node-gyp-src/test/simple-proxy.js 8080 /boombar/ https://nodejs.org/dist/ &) &&
  cd node-buffertools &&
  NODEJS_ORG_MIRROR=http://localhost:8080/boombar/ /node-gyp-src/bin/node-gyp.js --loglevel=info rebuild &&
  nc -z localhost 8080 && echo -e \"\\n\\n\\033[31mFAILED TO USE LOCAL PROXY\\033[39m\\n\\n\"
"

rm -rf $dot_node_gyp