Skip to content

Commit

Permalink
Add --extra-host arguments to services (#1086)
Browse files Browse the repository at this point in the history
  • Loading branch information
PigeonF authored Feb 15, 2024
1 parent 538214b commit a00b79f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,10 @@ export class Job {
dockerCmd += `--volume ${volume} `;
}

for (const extraHost of this.argv.extraHost) {
dockerCmd += `--add-host=${extraHost} `;
}

const serviceAlias = service.alias;
const serviceName = service.name;
const serviceNameWithoutVersion = serviceName.replace(/(.*)(:.*)/, "$1");
Expand Down
9 changes: 9 additions & 0 deletions tests/test-cases/extra-host/.gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@ test-job:
image: docker.io/curlimages/curl:7.69.1
script:
- curl -I http://fake-google.com

service-job:
image: docker.io/curlimages/curl:7.69.1
services:
- name: docker.io/alpine:latest
entrypoint: ["/bin/sh", "-c"]
command: ["getent hosts fake-google.com"]
script:
- "true"
18 changes: 17 additions & 1 deletion tests/test-cases/extra-host/integration.extra-host.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {handler} from "../../../src/handler";
import chalk from "chalk";
import {initSpawnSpy} from "../../mocks/utils.mock";
import {WhenStatics} from "../../mocks/when-statics";
import fs from "fs-extra";

beforeAll(() => {
initSpawnSpy(WhenStatics.all);
Expand All @@ -17,7 +18,22 @@ test("add-host <test-job>", async () => {
}, writeStreams);

const expected = [
chalk`{blueBright test-job} {greenBright >} HTTP/1.1 404 Not Found`,
chalk`{blueBright test-job } {greenBright >} HTTP/1.1 404 Not Found`,
];
expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining(expected));
});

test("add-host <service-job>", async () => {
await fs.promises.rm("tests/test-cases/extra-host/.gitlab-ci-local/services-output/service-job/docker.io/alpine:latest-0.log", {force: true});

const writeStreams = new WriteStreamsMock();
await handler({
cwd: "tests/test-cases/extra-host",
job: ["service-job"],
extraHost: ["fake-google.com:142.250.185.206"],
}, writeStreams);

expect(writeStreams.stderrLines.length).toEqual(2);
expect(await fs.pathExists("tests/test-cases/extra-host/.gitlab-ci-local/services-output/service-job/docker.io/alpine:latest-0.log")).toEqual(true);
expect(await fs.readFile("tests/test-cases/extra-host/.gitlab-ci-local/services-output/service-job/docker.io/alpine:latest-0.log", "utf-8")).toMatch(/142.250.185.206/);
});

0 comments on commit a00b79f

Please sign in to comment.