Skip to content

Commit

Permalink
feat: log docker-compose version
Browse files Browse the repository at this point in the history
Signed-off-by: Emilien Escalle <[email protected]>
  • Loading branch information
neilime committed Apr 2, 2024
1 parent 99447ae commit 8d12e91
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 54 deletions.
54 changes: 26 additions & 28 deletions .github/workflows/__check-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,28 @@ jobs:
name: Test with services
steps:
- uses: actions/checkout@v4
- uses: hoverkraft-tech/ci-github-nodejs/actions/[email protected]

- name: Act
uses: ./
with:
compose-file: "./docker/docker-compose.yml"
services: |
helloworld2
helloworld3
service-b
service-c
- name: "Assert: only expected services are running"
run: |
docker-compose -f ./docker/docker-compose.yml ps
docker-compose -f ./docker/docker-compose.yml ps | grep docker-helloworld2-1
docker-compose -f ./docker/docker-compose.yml ps | grep docker-helloworld3-1
docker-compose -f ./docker/docker-compose.yml ps | (grep docker-helloworld-1 && echo "Unexpected service helloworld is running" && exit 1) || true
docker-compose -f ./docker/docker-compose.yml ps | grep docker-service-b-1 || (echo "Service service-b is not running" && exit 1)
docker-compose -f ./docker/docker-compose.yml ps | grep docker-service-c-1 || (echo "Service service-c is not running" && exit 1)
(docker-compose -f ./docker/docker-compose.yml ps | grep docker-service-a-1 && echo "Unexpected service service-a is running" && exit 1) || true
test-action-with-down-flags:
runs-on: ubuntu-latest
name: Test compose action
steps:
- uses: actions/checkout@v4
- uses: hoverkraft-tech/ci-github-nodejs/actions/[email protected]

- name: Act
uses: ./
Expand All @@ -48,7 +46,6 @@ jobs:
name: Test with compose flags
steps:
- uses: actions/checkout@v4
- uses: hoverkraft-tech/ci-github-nodejs/actions/[email protected]

- name: Act
uses: ./
Expand All @@ -59,33 +56,34 @@ jobs:

- name: "Assert: profile is used"
run: |
docker-compose -f ./docker/docker-compose.yml -p profile-1 ps || (echo "Profile not used" && exit 1)
docker compose -f ./docker/docker-compose.yml -p profile-1 ps || (echo "Profile not used" && exit 1)
test-action-with-env:
runs-on: ubuntu-latest
name: Test with env
steps:
- uses: actions/checkout@v4
- uses: hoverkraft-tech/ci-github-nodejs/actions/[email protected]

- name: Act
uses: ./
with:
compose-file: "./docker/docker-compose-with-env.yml"
env:
IMAGE_NAME: hello-world
IMAGE_NAME: busybox

- name: "Assert: env is used"
env:
IMAGE_NAME: busybox
run: |
docker-compose -f ./docker/docker-compose-with-env.yml ps
docker-compose -f ./docker/docker-compose-with-env.yml ps | grep docker-helloworld-1
docker compose -f ./docker/docker-compose-with-env.yml ps
docker compose -f ./docker/docker-compose-with-env.yml ps | grep docker-service-a-1 || (echo "Service service-a is not running" && exit 1)
test-action-with-multiple-compose-files:
runs-on: ubuntu-latest
name: Test with multiple compose files
steps:
- uses: actions/checkout@v4
- uses: hoverkraft-tech/ci-github-nodejs/actions/[email protected]

- name: Act
uses: ./
Expand All @@ -94,37 +92,37 @@ jobs:
./docker/docker-compose.yml
./docker/docker-compose.ci.yml
services: |
helloworld2
helloworld4
service-b
service-d
- name: "Assert: only expected services are running"
run: |
docker-compose -f ./docker/docker-compose.yml -f ./docker/docker-compose.ci.yml ps
docker-compose -f ./docker/docker-compose.yml -f ./docker/docker-compose.ci.yml ps | grep docker-helloworld2-1
docker-compose -f ./docker/docker-compose.yml -f ./docker/docker-compose.ci.yml ps | grep docker-helloworld4-1
docker-compose -f ./docker/docker-compose.yml -f ./docker/docker-compose.ci.yml ps | (grep docker-helloworld-1 && echo "Unexpected service is running" && exit 1) || true
docker-compose -f ./docker/docker-compose.yml -f ./docker/docker-compose.ci.yml ps | (grep docker-helloworld3-1 && echo "Unexpected service is running" && exit 1) || true
docker compose -f ./docker/docker-compose.yml -f ./docker/docker-compose.ci.yml ps
docker compose -f ./docker/docker-compose.yml -f ./docker/docker-compose.ci.yml ps | grep docker-service-b-1 || (echo "Service service-b is not running" && exit 1)
docker compose -f ./docker/docker-compose.yml -f ./docker/docker-compose.ci.yml ps | grep docker-service-d-1 || (echo "Service service-d is not running" && exit 1)
(docker compose -f ./docker/docker-compose.yml -f ./docker/docker-compose.ci.yml ps | grep docker-service-a-1 && echo "Unexpected service service-a is running" && exit 1) || true
(docker compose -f ./docker/docker-compose.yml -f ./docker/docker-compose.ci.yml ps | grep docker-service-c-1 && echo "Unexpected service service-c is running" && exit 1) || true
test-action-with-cwd:
runs-on: ubuntu-latest
name: Test with cwd
steps:
- uses: actions/checkout@v4
- uses: hoverkraft-tech/ci-github-nodejs/actions/[email protected]

- name: Act
uses: ./
with:
compose-file: "docker-compose.yml"
cwd: "./docker"
services: |
helloworld2
helloworld3
service-b
service-c
- name: "Assert: only expected services are running"
run: |
docker-compose -f ./docker/docker-compose.yml ps
docker compose -f ./docker/docker-compose.yml ps
docker-compose -f ./docker/docker-compose.yml ps | grep docker-helloworld2-1
docker-compose -f ./docker/docker-compose.yml ps | grep docker-helloworld3-1
docker-compose -f ./docker/docker-compose.yml ps | (grep docker-helloworld-1 && echo "Unexpected service helloworld is running" && exit 1) || true
docker compose -f ./docker/docker-compose.yml ps | grep docker-service-b-1 || (echo "Service service-b is not running" && exit 1)
docker compose -f ./docker/docker-compose.yml ps | grep docker-service-c-1 || (echo "Service service-c is not running" && exit 1)
(docker compose -f ./docker/docker-compose.yml ps | grep docker-service-a-1 && echo "Unexpected service service-a is running" && exit 1) || true
8 changes: 7 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions dist/post.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions docker/docker-compose-with-env.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
version: '3.8'
version: "3.8"

volumes:
test_volume: {}

services:
helloworld:
service-a:
image: ${IMAGE_NAME}
command: ["tail", "-f", "/dev/null"]
7 changes: 4 additions & 3 deletions docker/docker-compose.ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
version: '3.8'
version: "3.8"

services:
helloworld4:
service-d:
image: busybox
command: ["tail", "-f", "/dev/null"]
profiles: [profile-2]
image: hello-world
23 changes: 13 additions & 10 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
version: '3.8'

volumes:
test_volume: {}
version: "3.8"

services:
helloworld:
image: hello-world
service-a:
image: busybox
command: ["tail", "-f", "/dev/null"]
volumes:
- test_volume:/test:Z
helloworld2:
service-b:
image: busybox
command: ["tail", "-f", "/dev/null"]
profiles: [profile-1]
image: hello-world
helloworld3:
service-c:
image: busybox
command: ["tail", "-f", "/dev/null"]
profiles: [profile-2]
image: hello-world

volumes:
test_volume: {}
8 changes: 7 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ let setFailedMock: jest.SpiedFunction<typeof core.setFailed>;
let getInputsMock: jest.SpiedFunction<typeof InputService.prototype.getInputs>;
let debugMock: jest.SpiedFunction<typeof LoggerService.prototype.debug>;
let infoMock: jest.SpiedFunction<typeof LoggerService.prototype.info>;
let versionMock: jest.SpiedFunction<typeof DockerComposeService.prototype.version>;
let upMock: jest.SpiedFunction<typeof DockerComposeService.prototype.up>;

describe("index", () => {
Expand All @@ -17,6 +18,7 @@ describe("index", () => {
infoMock = jest.spyOn(LoggerService.prototype, "info").mockImplementation();
debugMock = jest.spyOn(LoggerService.prototype, "debug").mockImplementation();
getInputsMock = jest.spyOn(InputService.prototype, "getInputs");
versionMock = jest.spyOn(DockerComposeService.prototype, "version");
upMock = jest.spyOn(DockerComposeService.prototype, "up");
});

Expand All @@ -30,10 +32,14 @@ describe("index", () => {
cwd: "/current/working/dir",
}));

versionMock.mockResolvedValueOnce("1.2.3");
upMock.mockResolvedValueOnce();

// eslint-disable-next-line @typescript-eslint/no-require-imports
await require("../src/index");
await new Promise((resolve) => setTimeout(resolve, 0));

expect(infoMock).toHaveBeenNthCalledWith(1, "docker-compose version: 1.2.3");

// Verify that all of the functions were called correctly
expect(debugMock).toHaveBeenNthCalledWith(
Expand All @@ -51,6 +57,6 @@ describe("index", () => {
});

expect(setFailedMock).not.toHaveBeenCalled();
expect(infoMock).toHaveBeenCalledWith("compose started");
expect(infoMock).toHaveBeenNthCalledWith(2, "docker-compose is up");
});
});
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ const callback: RunCallback = async (
loggerService: LoggerService,
dockerComposeService: DockerComposeService
) => {
const composeVersion = await dockerComposeService.version(inputs);
loggerService.info(`docker-compose version: ${composeVersion}`);

await dockerComposeService.up(inputs);
loggerService.info("compose started");
loggerService.info("docker-compose is up");
};

// eslint-disable-next-line @typescript-eslint/no-floating-promises
Expand Down
4 changes: 2 additions & 2 deletions src/post.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe("post", () => {
cwd: "/current/working/dir",
});

expect(debugMock).toHaveBeenNthCalledWith(2, "compose logs:\nlog");
expect(debugMock).toHaveBeenNthCalledWith(2, "docker-compose logs:\nlog");

expect(downMock).toHaveBeenCalledWith({
composeFiles: ["docker-compose.yml"],
Expand All @@ -66,6 +66,6 @@ describe("post", () => {
});

expect(setFailedMock).not.toHaveBeenCalled();
expect(infoMock).toHaveBeenCalledWith("compose removed");
expect(infoMock).toHaveBeenCalledWith("docker-compose is down");
});
});
7 changes: 4 additions & 3 deletions src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ const callback: RunCallback = async (
loggerService: LoggerService,
dockerComposeService: DockerComposeService
) => {
console.log("ok");
const { error, output } = await dockerComposeService.logs(inputs);

if (error) {
loggerService.debug("compose error:\n" + error);
loggerService.debug("docker-compose error:\n" + error);
}

loggerService.debug("compose logs:\n" + output);
loggerService.debug("docker-compose logs:\n" + output);

await dockerComposeService.down(inputs);

loggerService.info("compose removed");
loggerService.info("docker-compose is down");
};

// eslint-disable-next-line @typescript-eslint/no-floating-promises
Expand Down
33 changes: 33 additions & 0 deletions src/services/docker-compose.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ describe("DockerComposeService", () => {
let upManyMock: jest.SpiedFunction<typeof v2.upMany>;
let downMock: jest.SpiedFunction<typeof v2.down>;
let logsMock: jest.SpiedFunction<typeof v2.logs>;
let versionMock: jest.SpiedFunction<typeof v2.version>;

beforeEach(() => {
service = new DockerComposeService();
upAllMock = jest.spyOn(v2, "upAll").mockImplementation();
upManyMock = jest.spyOn(v2, "upMany").mockImplementation();
downMock = jest.spyOn(v2, "down").mockImplementation();
logsMock = jest.spyOn(v2, "logs").mockImplementation();
versionMock = jest.spyOn(v2, "version").mockImplementation();
});

afterEach(() => {
Expand Down Expand Up @@ -114,4 +116,35 @@ describe("DockerComposeService", () => {
});
});
});

describe("version", () => {
it("should call version with correct options", async () => {
const inputs: Inputs = {
composeFiles: ["docker-compose.yml"],
services: [],
composeFlags: [],
upFlags: [],
downFlags: [],
cwd: "/current/working/dir",
};

versionMock.mockResolvedValue({
exitCode: 0,
out: "",
err: "",
data: {
version: "1.2.3",
},
});

await service.version(inputs);

expect(versionMock).toHaveBeenCalledWith({
composeOptions: [],
config: ["docker-compose.yml"],
log: true,
cwd: "/current/working/dir",
});
});
});
});
5 changes: 5 additions & 0 deletions src/services/docker-compose.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export class DockerComposeService {
};
}

async version(inputs: Inputs): Promise<string> {
const result = await v2.version(this.getCommonOptions(inputs));
return result.data.version;
}

private getCommonOptions(inputs: Inputs): IDockerComposeOptions {
return {
config: inputs.composeFiles,
Expand Down

0 comments on commit 8d12e91

Please sign in to comment.