diff --git a/__tests__/docker/install.test.e2e.ts b/__tests__/docker/install.test.e2e.ts index 243f0a8..054c8f2 100644 --- a/__tests__/docker/install.test.e2e.ts +++ b/__tests__/docker/install.test.e2e.ts @@ -33,6 +33,7 @@ describe('install', () => { await install.install(toolPath, tmpDir, version); await Docker.printVersion(); await Docker.printInfo(); + await install.tearDown(tmpDir); })()).resolves.not.toThrow(); }); }); diff --git a/src/docker/assets.ts b/src/docker/assets.ts index a8f82db..6ebed43 100644 --- a/src/docker/assets.ts +++ b/src/docker/assets.ts @@ -25,6 +25,10 @@ export const setupDockerWinPs1 = (): string => { return get('docker-setup-win.ps1', setupDockerWinPs1Data); }; +export const dockerServiceLogsPs1 = (): string => { + return get('docker-service-logs.ps1', dockerServiceLogsPs1Data); +}; + export const colimaYaml = (): string => { return get('colima.yaml', colimaYamlData); }; @@ -146,7 +150,9 @@ While ($true) { Start-Sleep -Seconds 1 } Write-Host "Docker daemon started successfully!" +`; +export const dockerServiceLogsPs1Data = ` Get-WinEvent -ea SilentlyContinue \` -FilterHashtable @{ProviderName= "docker"; LogName = "application"} | Sort-Object @{Expression="TimeCreated";Descending=$false} | diff --git a/src/docker/install.ts b/src/docker/install.ts index 5829bc5..8e73d7e 100644 --- a/src/docker/install.ts +++ b/src/docker/install.ts @@ -27,7 +27,7 @@ import * as tc from '@actions/tool-cache'; import {Exec} from '../exec'; import {Util} from '../util'; -import {colimaYamlData, setupDockerLinuxSh, setupDockerWinPs1} from './assets'; +import {colimaYamlData, dockerServiceLogsPs1, setupDockerLinuxSh, setupDockerWinPs1} from './assets'; export class Install { public async download(version: string, channel?: string): Promise { @@ -134,6 +134,7 @@ export class Install { private async installLinux(toolDir: string, runDir: string): Promise { const dockerHost = `unix://${path.join(runDir, 'docker.sock')}`; + await io.mkdirP(runDir); await core.group('Start Docker daemon', async () => { const bashPath: string = await io.which('bash', true); @@ -189,13 +190,15 @@ export class Install { private async installWindows(toolDir: string, runDir: string): Promise { const dockerHost = 'npipe:////./pipe/setup_docker_action'; - const setupCmd = await Util.powershellCommand(setupDockerWinPs1(), { - ToolDir: toolDir, - RunDir: runDir, - DockerHost: dockerHost - }); await core.group('Install Docker daemon service', async () => { + const setupCmd = await Util.powershellCommand(setupDockerWinPs1(), { + ToolDir: toolDir, + RunDir: runDir, + DockerHost: dockerHost + }); await Exec.exec(setupCmd.command, setupCmd.args); + const logCmd = await Util.powershellCommand(dockerServiceLogsPs1()); + await Exec.exec(logCmd.command, logCmd.args); }); await core.group('Create Docker context', async () => { @@ -204,6 +207,66 @@ export class Install { }); } + public async tearDown(runDir: string): Promise { + switch (os.platform()) { + case 'darwin': { + await this.tearDownDarwin(runDir); + break; + } + case 'linux': { + await this.tearDownLinux(runDir); + break; + } + case 'win32': { + await this.tearDownWindows(); + break; + } + default: { + throw new Error(`Unsupported platform: ${os.platform()}`); + } + } + } + + private async tearDownDarwin(runDir: string): Promise { + await core.group('Docker daemon logs', async () => { + await Exec.exec('colima', ['exec', '--', 'cat', '/var/log/docker.log']); + }); + await core.group('Stopping colima', async () => { + await Exec.exec('colima', ['stop', '--very-verbose']); + }); + await core.group('Removing Docker context', async () => { + await Exec.exec('docker', ['context', 'rm', '-f', 'setup-docker-action']); + }); + await core.group(`Cleaning up runDir`, async () => { + await Exec.exec('sudo', ['rm', '-rf', runDir]); + }); + } + + private async tearDownLinux(runDir: string): Promise { + await core.group('Docker daemon logs', async () => { + core.info(fs.readFileSync(path.join(runDir, 'dockerd.log'), {encoding: 'utf8'})); + }); + await core.group('Stopping Docker daemon', async () => { + await Exec.exec('sudo', ['kill', fs.readFileSync(path.join(runDir, 'docker.pid')).toString().trim()]); + }); + await core.group('Removing Docker context', async () => { + await Exec.exec('docker', ['context', 'rm', '-f', 'setup-docker-action']); + }); + await core.group(`Cleaning up runDir`, async () => { + await Exec.exec('sudo', ['rm', '-rf', runDir]); + }); + } + + private async tearDownWindows(): Promise { + await core.group('Docker daemon logs', async () => { + const logCmd = await Util.powershellCommand(dockerServiceLogsPs1()); + await Exec.exec(logCmd.command, logCmd.args); + }); + await core.group('Removing Docker context', async () => { + await Exec.exec('docker', ['context', 'rm', '-f', 'setup-docker-action']); + }); + } + private downloadURL(version: string, channel: string): string { const platformOS = Install.platformOS(); const platformArch = Install.platformArch(); diff --git a/src/util.ts b/src/util.ts index bed6ee5..86c57cb 100644 --- a/src/util.ts +++ b/src/util.ts @@ -74,12 +74,14 @@ export class Util { return true; } - public static async powershellCommand(script: string, params: Record) { + public static async powershellCommand(script: string, params?: Record) { const powershellPath: string = await io.which('powershell', true); const escapedScript = script.replace(/'/g, "''").replace(/"|\n|\r/g, ''); const escapedParams: string[] = []; - for (const key in params) { - escapedParams.push(`-${key} '${params[key].replace(/'/g, "''").replace(/"|\n|\r/g, '')}'`); + if (params) { + for (const key in params) { + escapedParams.push(`-${key} '${params[key].replace(/'/g, "''").replace(/"|\n|\r/g, '')}'`); + } } return { command: `"${powershellPath}"`,