Skip to content

Commit 8e3142b

Browse files
authored
fix: can't run unit tests in docker (#600)
1 parent 4e0b251 commit 8e3142b

File tree

8 files changed

+211
-167
lines changed

8 files changed

+211
-167
lines changed

.github/scripts/before-beta-release.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const PACKAGE_NAME = pkgJson.name;
1111
const VERSION = pkgJson.version;
1212

1313
const nextVersion = getNextVersion(VERSION);
14+
// eslint-disable-next-line no-console
1415
console.log(`before-deploy: Setting version to ${nextVersion}`);
1516
pkgJson.version = nextVersion;
1617

@@ -21,6 +22,7 @@ function getNextVersion(version) {
2122
const versions = JSON.parse(versionString);
2223

2324
if (versions.some((v) => v === VERSION)) {
25+
// eslint-disable-next-line no-console
2426
console.error(`before-deploy: A release with version ${VERSION} already exists. Please increment version accordingly.`);
2527
process.exit(1);
2628
}

examples/apify_proxy_tunnel.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ const { createTunnel, closeTunnel, redactUrl } = require('proxy-chain');
2020
// The "verbose" option causes a lot of logging
2121
const tunnelInfo = await createTunnel(PROXY_URL, TARGET_HOST, { port: 9999, verbose: true });
2222

23+
// eslint-disable-next-line no-console
2324
console.log(`Tunnel to ${TARGET_HOST} via ${redactUrl(PROXY_URL)} established at ${tunnelInfo}...`);
2425

2526
// Here we assume por 443 from above, otherwise the service will not be accessible via HTTPS!
27+
// eslint-disable-next-line no-console
2628
console.log(`To test it, you can run: curl --verbose https://${tunnelInfo}`);
2729

2830
// Wait forever...
29-
await new Promise(() => {});
31+
await new Promise(() => { });
3032

3133
// Normally, you'd also want to close the tunnel and all open connections
3234
await closeTunnel(tunnelInfo, true);

package.json

Lines changed: 91 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,94 @@
11
{
2-
"name": "proxy-chain",
3-
"version": "2.5.9",
4-
"description": "Node.js implementation of a proxy server (think Squid) with support for SSL, authentication, upstream proxy chaining, and protocol tunneling.",
5-
"main": "dist/index.js",
6-
"keywords": [
7-
"proxy",
8-
"squid",
9-
"apify",
10-
"tunnel",
11-
"puppeteer"
12-
],
13-
"author": {
14-
"name": "Apify Technologies",
15-
"email": "support@apify.com",
16-
"url": "https://apify.com"
17-
},
18-
"contributors": [
19-
"Jan Curn <jan@apify.com>"
20-
],
21-
"license": "Apache-2.0",
22-
"repository": {
23-
"type": "git",
24-
"url": "git+https://github.com/apify/proxy-chain"
25-
},
26-
"bugs": {
27-
"url": "https://github.com/apify/proxy-chain/issues"
28-
},
29-
"homepage": "https://blog.apify.com/how-to-make-headless-chrome-and-puppeteer-use-a-proxy-server-with-authentication-249a21a79212",
30-
"files": [
31-
"dist"
32-
],
33-
"scripts": {
34-
"build:watch": "tsc -w",
35-
"build": "tsc",
36-
"clean": "rimraf dist",
37-
"prepublishOnly": "npm run build",
38-
"local-proxy": "node ./dist/run_locally.js",
39-
"test": "nyc cross-env NODE_OPTIONS=--insecure-http-parser mocha --bail",
40-
"lint": "eslint .",
41-
"lint:fix": "eslint . --fix"
42-
},
43-
"engines": {
44-
"node": ">=14"
45-
},
46-
"devDependencies": {
47-
"@apify/eslint-config": "^1.0.0",
48-
"@apify/tsconfig": "^0.1.0",
49-
"@types/jest": "^28.1.2",
50-
"@types/node": "^18.8.3",
51-
"basic-auth": "^2.0.1",
52-
"basic-auth-parser": "^0.0.2",
53-
"body-parser": "^1.19.0",
54-
"chai": "^4.3.4",
55-
"cross-env": "^7.0.3",
56-
"eslint": "^9.18.0",
57-
"express": "^4.17.1",
58-
"faye-websocket": "^0.11.4",
59-
"got-scraping": "^3.2.4-beta.0",
60-
"isparta": "^4.1.1",
61-
"mocha": "^10.0.0",
62-
"nyc": "^15.1.0",
63-
"portastic": "^1.0.1",
64-
"proxy": "^1.0.2",
65-
"puppeteer": "^19.6.3",
66-
"request": "^2.88.2",
67-
"rimraf": "^4.1.2",
68-
"sinon": "^13.0.2",
69-
"sinon-stub-promise": "^4.0.0",
70-
"socksv5": "^0.0.6",
71-
"through": "^2.3.8",
72-
"ts-node": "^10.2.1",
73-
"typescript": "^4.4.3",
74-
"typescript-eslint": "^8.20.0",
75-
"underscore": "^1.13.1",
76-
"ws": "^8.2.2"
77-
},
78-
"nyc": {
79-
"reporter": [
80-
"text",
81-
"html",
82-
"lcov"
2+
"name": "proxy-chain",
3+
"version": "2.5.9",
4+
"description": "Node.js implementation of a proxy server (think Squid) with support for SSL, authentication, upstream proxy chaining, and protocol tunneling.",
5+
"main": "dist/index.js",
6+
"keywords": [
7+
"proxy",
8+
"squid",
9+
"apify",
10+
"tunnel",
11+
"puppeteer"
8312
],
84-
"exclude": [
85-
"**/test/**"
86-
]
87-
},
88-
"dependencies": {
89-
"socks": "^2.8.3",
90-
"socks-proxy-agent": "^8.0.3",
91-
"tslib": "^2.3.1"
92-
}
13+
"author": {
14+
"name": "Apify Technologies",
15+
"email": "support@apify.com",
16+
"url": "https://apify.com"
17+
},
18+
"contributors": [
19+
"Jan Curn <jan@apify.com>"
20+
],
21+
"license": "Apache-2.0",
22+
"repository": {
23+
"type": "git",
24+
"url": "git+https://github.com/apify/proxy-chain"
25+
},
26+
"bugs": {
27+
"url": "https://github.com/apify/proxy-chain/issues"
28+
},
29+
"homepage": "https://blog.apify.com/how-to-make-headless-chrome-and-puppeteer-use-a-proxy-server-with-authentication-249a21a79212",
30+
"files": [
31+
"dist"
32+
],
33+
"scripts": {
34+
"build:watch": "tsc -w",
35+
"build": "tsc",
36+
"clean": "rimraf dist",
37+
"prepublishOnly": "npm run build",
38+
"local-proxy": "node ./dist/run_locally.js",
39+
"test": "nyc cross-env NODE_OPTIONS=--insecure-http-parser mocha --bail",
40+
"test:docker": "docker build --tag proxy-chain-tests --file test/Dockerfile . && docker run proxy-chain-tests",
41+
"lint": "eslint .",
42+
"lint:fix": "eslint . --fix"
43+
},
44+
"engines": {
45+
"node": ">=14"
46+
},
47+
"devDependencies": {
48+
"@apify/eslint-config": "^1.0.0",
49+
"@apify/tsconfig": "^0.1.0",
50+
"@types/jest": "^28.1.2",
51+
"@types/node": "^18.8.3",
52+
"basic-auth": "^2.0.1",
53+
"basic-auth-parser": "^0.0.2",
54+
"body-parser": "^1.19.0",
55+
"chai": "^4.3.4",
56+
"cross-env": "^7.0.3",
57+
"eslint": "^9.18.0",
58+
"express": "^4.17.1",
59+
"faye-websocket": "^0.11.4",
60+
"got-scraping": "^3.2.4-beta.0",
61+
"isparta": "^4.1.1",
62+
"mocha": "^10.0.0",
63+
"nyc": "^15.1.0",
64+
"portastic": "^1.0.1",
65+
"proxy": "^1.0.2",
66+
"puppeteer": "^19.6.3",
67+
"request": "^2.88.2",
68+
"rimraf": "^4.1.2",
69+
"sinon": "^13.0.2",
70+
"sinon-stub-promise": "^4.0.0",
71+
"socksv5": "^0.0.6",
72+
"through": "^2.3.8",
73+
"ts-node": "^10.2.1",
74+
"typescript": "^4.4.3",
75+
"typescript-eslint": "^8.20.0",
76+
"underscore": "^1.13.1",
77+
"ws": "^8.2.2"
78+
},
79+
"nyc": {
80+
"reporter": [
81+
"text",
82+
"html",
83+
"lcov"
84+
],
85+
"exclude": [
86+
"**/test/**"
87+
]
88+
},
89+
"dependencies": {
90+
"socks": "^2.8.3",
91+
"socks-proxy-agent": "^8.0.3",
92+
"tslib": "^2.3.1"
93+
}
9394
}

src/tcp_tunnel_tools.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export async function createTunnel(
4545
const server: net.Server & { log?: (...args: unknown[]) => void } = net.createServer();
4646

4747
const log = (...args: unknown[]): void => {
48+
// eslint-disable-next-line no-console
4849
if (verbose) console.log(...args);
4950
};
5051

test/Dockerfile

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
# You can use this Dockerfile to run the tests on Linux,
2-
# since the sockets behave a little differently there than on Mac
3-
#
4-
# Usage:
5-
# > docker build .
6-
# > docker run <CONTAINER_ID>
1+
FROM node:18.20.8-bookworm@sha256:c6ae79e38498325db67193d391e6ec1d224d96c693a8a4d943498556716d3783
72

8-
FROM node:10
3+
RUN apt-get update && apt-get install -y --no-install-recommends chromium=140.0.7339.185-1~deb12u1 \
4+
&& rm -rf /var/lib/apt/lists/*
95

10-
COPY .. /home/node/
6+
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
7+
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium
8+
9+
WORKDIR /home/node
10+
11+
COPY .. .
1112

1213
RUN npm --quiet set progress=false \
13-
&& npm install --only=prod --no-optional \
14-
&& echo "Installed NPM packages:" \
15-
&& npm list || true \
16-
&& echo "Node.js version:" \
17-
&& node --version \
18-
&& echo "NPM version:" \
19-
&& npm --version
14+
&& npm install --no-optional \
15+
&& echo "Installed NPM packages:" \
16+
&& npm list || true \
17+
&& echo "Node.js version:" \
18+
&& node --version \
19+
&& echo "NPM version:" \
20+
&& npm --version
2021

21-
CMD cd /home/node && npm test
22+
CMD ["npm", "test"]

test/README.md

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,46 @@
1+
# Tests
12

2-
To run the tests, you need to add the following line to your `/etc/hosts`:
3+
## Docker (recommended)
34

5+
Since Linux and macOS handle sockets differently, please run tests in a Docker container
6+
to have a consistent Linux environment for running tests.
7+
8+
```bash
9+
npm run test:docker
410
```
5-
# Used by proxy-chain NPM package tests
6-
127.0.0.1 localhost
7-
127.0.0.1 localhost-test
8-
```
911

10-
The `localhost` entry is for avoiding dual-stack issues, e.g. when the test server listens at ::1
11-
(results of getaddrinfo have specifed order) and the client attempts to connect to 127.0.0.1 .
12+
Note: for test in Docker no changes in `/etc/hosts` needed.
13+
14+
## Local Machine
15+
16+
### Prerequisites
17+
18+
1. Node.js 18+ (see `.nvmrc` for exact version)
19+
2. For MacOS with ARM CPUs install Rosetta (workaround for puppeteer)
20+
3. Update `/etc/hosts`
21+
22+
```bash
23+
# Used by proxy-chain NPM package tests
24+
127.0.0.1 localhost
25+
127.0.0.1 localhost-test
26+
```
27+
28+
The `localhost` entry is for avoiding dual-stack issues, e.g. when the test server listens at ::1
29+
(results of getaddrinfo have specified order) and the client attempts to connect to 127.0.0.1 .
30+
31+
The `localhost-test` entry is a workaround to PhantomJS' behavior where it skips proxy servers for
32+
localhost addresses.
33+
34+
### Run tests
35+
36+
1. Run all tests
37+
38+
```bash
39+
npm run test
40+
```
41+
42+
2. Run specific tests
1243
13-
The `localhost-test` entry is a workaround to PhantomJS' behavior where it skips proxy servers for
14-
localhost addresses.
44+
```bash
45+
npm run test test/anonymize_proxy.js
46+
```

test/server.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ const puppeteerGet = (url, proxyUrl) => {
8080
HTTP_PROXY: parsed.origin,
8181
} : {},
8282
ignoreHTTPSErrors: true,
83+
headless: "new",
84+
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage']
8385
});
8486

8587
try {

0 commit comments

Comments
 (0)