From 8d12e916efa9c7beacddd68048917ec225c170bf Mon Sep 17 00:00:00 2001 From: Emilien Escalle Date: Tue, 2 Apr 2024 17:27:10 +0200 Subject: [PATCH] feat: log docker-compose version Signed-off-by: Emilien Escalle --- .github/workflows/__check-action.yml | 54 ++++++++++----------- dist/index.js | 8 ++- dist/post.js | 11 +++-- docker/docker-compose-with-env.yml | 5 +- docker/docker-compose.ci.yml | 7 +-- docker/docker-compose.yml | 23 +++++---- src/index.test.ts | 8 ++- src/index.ts | 5 +- src/post.test.ts | 4 +- src/post.ts | 7 +-- src/services/docker-compose.service.test.ts | 33 +++++++++++++ src/services/docker-compose.service.ts | 5 ++ 12 files changed, 116 insertions(+), 54 deletions(-) diff --git a/.github/workflows/__check-action.yml b/.github/workflows/__check-action.yml index a0872ad..6217388 100644 --- a/.github/workflows/__check-action.yml +++ b/.github/workflows/__check-action.yml @@ -12,30 +12,28 @@ jobs: name: Test with services steps: - uses: actions/checkout@v4 - - uses: hoverkraft-tech/ci-github-nodejs/actions/setup-node@0.4.2 - 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/setup-node@0.4.2 - name: Act uses: ./ @@ -48,7 +46,6 @@ jobs: name: Test with compose flags steps: - uses: actions/checkout@v4 - - uses: hoverkraft-tech/ci-github-nodejs/actions/setup-node@0.4.2 - name: Act uses: ./ @@ -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/setup-node@0.4.2 - 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/setup-node@0.4.2 - name: Act uses: ./ @@ -94,23 +92,23 @@ 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/setup-node@0.4.2 - name: Act uses: ./ @@ -118,13 +116,13 @@ jobs: 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 diff --git a/dist/index.js b/dist/index.js index caa0637..18c9eab 100644 --- a/dist/index.js +++ b/dist/index.js @@ -25994,6 +25994,10 @@ class DockerComposeService { output: out, }; } + async version(inputs) { + const result = await docker_compose_1.v2.version(this.getCommonOptions(inputs)); + return result.data.version; + } getCommonOptions(inputs) { return { config: inputs.composeFiles, @@ -36440,8 +36444,10 @@ Object.defineProperty(exports, "__esModule", ({ value: true })); */ const runner_1 = __nccwpck_require__(3878); const callback = async (inputs, loggerService, 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 (0, runner_1.run)(callback); diff --git a/dist/post.js b/dist/post.js index 9155cf1..70b0ee3 100644 --- a/dist/post.js +++ b/dist/post.js @@ -25994,6 +25994,10 @@ class DockerComposeService { output: out, }; } + async version(inputs) { + const result = await docker_compose_1.v2.version(this.getCommonOptions(inputs)); + return result.data.version; + } getCommonOptions(inputs) { return { config: inputs.composeFiles, @@ -36440,13 +36444,14 @@ Object.defineProperty(exports, "__esModule", ({ value: true })); */ const runner_1 = __nccwpck_require__(3878); const callback = async (inputs, loggerService, 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 (0, runner_1.run)(callback); diff --git a/docker/docker-compose-with-env.yml b/docker/docker-compose-with-env.yml index 8d566ae..b938566 100644 --- a/docker/docker-compose-with-env.yml +++ b/docker/docker-compose-with-env.yml @@ -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"] diff --git a/docker/docker-compose.ci.yml b/docker/docker-compose.ci.yml index e1017b6..ad00f0b 100644 --- a/docker/docker-compose.ci.yml +++ b/docker/docker-compose.ci.yml @@ -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 diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 4920f8e..7c8dd8e 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -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: {} diff --git a/src/index.test.ts b/src/index.test.ts index 174e216..b31c005 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -7,6 +7,7 @@ let setFailedMock: jest.SpiedFunction; let getInputsMock: jest.SpiedFunction; let debugMock: jest.SpiedFunction; let infoMock: jest.SpiedFunction; +let versionMock: jest.SpiedFunction; let upMock: jest.SpiedFunction; describe("index", () => { @@ -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"); }); @@ -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( @@ -51,6 +57,6 @@ describe("index", () => { }); expect(setFailedMock).not.toHaveBeenCalled(); - expect(infoMock).toHaveBeenCalledWith("compose started"); + expect(infoMock).toHaveBeenNthCalledWith(2, "docker-compose is up"); }); }); diff --git a/src/index.ts b/src/index.ts index 88f7b6d..5eb8d37 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 diff --git a/src/post.test.ts b/src/post.test.ts index 9132788..322d5ba 100644 --- a/src/post.test.ts +++ b/src/post.test.ts @@ -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"], @@ -66,6 +66,6 @@ describe("post", () => { }); expect(setFailedMock).not.toHaveBeenCalled(); - expect(infoMock).toHaveBeenCalledWith("compose removed"); + expect(infoMock).toHaveBeenCalledWith("docker-compose is down"); }); }); diff --git a/src/post.ts b/src/post.ts index 0c313e5..86532f6 100644 --- a/src/post.ts +++ b/src/post.ts @@ -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 diff --git a/src/services/docker-compose.service.test.ts b/src/services/docker-compose.service.test.ts index 9bb70bc..42d9df5 100644 --- a/src/services/docker-compose.service.test.ts +++ b/src/services/docker-compose.service.test.ts @@ -10,6 +10,7 @@ describe("DockerComposeService", () => { let upManyMock: jest.SpiedFunction; let downMock: jest.SpiedFunction; let logsMock: jest.SpiedFunction; + let versionMock: jest.SpiedFunction; beforeEach(() => { service = new DockerComposeService(); @@ -17,6 +18,7 @@ describe("DockerComposeService", () => { 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(() => { @@ -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", + }); + }); + }); }); diff --git a/src/services/docker-compose.service.ts b/src/services/docker-compose.service.ts index 0c3ea56..bf48061 100644 --- a/src/services/docker-compose.service.ts +++ b/src/services/docker-compose.service.ts @@ -39,6 +39,11 @@ export class DockerComposeService { }; } + async version(inputs: Inputs): Promise { + const result = await v2.version(this.getCommonOptions(inputs)); + return result.data.version; + } + private getCommonOptions(inputs: Inputs): IDockerComposeOptions { return { config: inputs.composeFiles,