feat: log docker-compose version

Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
This commit is contained in:
Emilien Escalle 2024-04-02 17:27:10 +02:00 committed by Emilien Escalle
parent 99447ae4bd
commit 8d12e916ef
12 changed files with 119 additions and 57 deletions

View File

@ -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

8
dist/index.js generated vendored
View File

@ -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);

11
dist/post.js generated vendored
View File

@ -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);

View File

@ -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"]

View File

@ -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

View File

@ -1,16 +1,19 @@
version: '3.8'
version: "3.8"
services:
service-a:
image: busybox
command: ["tail", "-f", "/dev/null"]
volumes:
- test_volume:/test:Z
service-b:
image: busybox
command: ["tail", "-f", "/dev/null"]
profiles: [profile-1]
service-c:
image: busybox
command: ["tail", "-f", "/dev/null"]
profiles: [profile-2]
volumes:
test_volume: {}
services:
helloworld:
image: hello-world
volumes:
- test_volume:/test:Z
helloworld2:
profiles: [profile-1]
image: hello-world
helloworld3:
profiles: [profile-2]
image: hello-world

View File

@ -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", () => {
@ -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");
});
});

View File

@ -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

View File

@ -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");
});
});

View File

@ -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

View File

@ -10,6 +10,7 @@ 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();
@ -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",
});
});
});
});

View File

@ -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,