diff --git a/packages/main/src/plugin/container-registry.spec.ts b/packages/main/src/plugin/container-registry.spec.ts index 346cac917..916456bab 100644 --- a/packages/main/src/plugin/container-registry.spec.ts +++ b/packages/main/src/plugin/container-registry.spec.ts @@ -37,6 +37,7 @@ import { ImageRegistry } from '/@/plugin/image-registry.js'; import type { Proxy } from '/@/plugin/proxy.js'; import type { Telemetry } from '/@/plugin/telemetry/telemetry.js'; import type { ContainerCreateOptions } from '/@api/container-info.js'; +import type { ContainerInspectInfo } from '/@api/container-inspect-info.js'; import type { ImageInfo } from '/@api/image-info.js'; import type { ProviderContainerConnectionInfo } from '/@api/provider-info.js'; @@ -312,6 +313,10 @@ const fakeContainerInspectInfoWithVolume = { }; class TestContainerProviderRegistry extends ContainerProviderRegistry { + public extractContainerEnvironment(container: ContainerInspectInfo): { [key: string]: string } { + return super.extractContainerEnvironment(container); + } + public getMatchingEngine(engineId: string): Dockerode { return super.getMatchingEngine(engineId); } @@ -5243,3 +5248,39 @@ describe('provider update', () => { }); }); }); + +describe('extractContainerEnvironment', () => { + test('simple env', async () => { + // create a fake inspect info object with env + const inspectInfo = { + Config: { + Env: ['TERM=xterm', 'HOME=/root'], + }, + } as unknown as ContainerInspectInfo; + + const env = containerRegistry.extractContainerEnvironment(inspectInfo); + + expect(env).toBeDefined(); + expect(Object.keys(env)).toHaveLength(2); + + expect(env['TERM']).toBe('xterm'); + expect(env['HOME']).toBe('/root'); + }); + + test('simple complex env', async () => { + // create a fake inspect info object with env + const inspectInfo = { + Config: { + Env: ['HOME=/root', 'SERVER_ARGS=--host-config=host-secondary.xml --foo-=bar'], + }, + } as unknown as ContainerInspectInfo; + + const env = containerRegistry.extractContainerEnvironment(inspectInfo); + + expect(env).toBeDefined(); + expect(Object.keys(env)).toHaveLength(2); + + expect(env['HOME']).toBe('/root'); + expect(env['SERVER_ARGS']).toBe('--host-config=host-secondary.xml --foo-=bar'); + }); +}); diff --git a/packages/main/src/plugin/container-registry.ts b/packages/main/src/plugin/container-registry.ts index 23293b820..d804c1c30 100644 --- a/packages/main/src/plugin/container-registry.ts +++ b/packages/main/src/plugin/container-registry.ts @@ -1403,6 +1403,15 @@ export class ContainerProviderRegistry { } } + protected extractContainerEnvironment(container: ContainerInspectInfo): { [key: string]: string } { + return container.Config.Env.reduce((acc: { [key: string]: string }, env) => { + // should handle multiple values after the = sign + const [key, ...values] = env.split('='); + acc[key] = values.join('='); + return acc; + }, {}); + } + async replicatePodmanContainer( source: { engineId: string; id: string }, target: { engineId: string }, @@ -1417,11 +1426,7 @@ export class ContainerProviderRegistry { const containerToReplicate = await this.getContainerInspect(source.engineId, source.id); // convert env from array of string to an object with key being the env name - const updatedEnv = containerToReplicate.Config.Env.reduce((acc: { [key: string]: string }, env) => { - const [key, value] = env.split('='); - acc[key] = value; - return acc; - }, {}); + const updatedEnv = this.extractContainerEnvironment(containerToReplicate); // build create container configuration const originalConfiguration = this.getCreateContainsOptionsFromOriginal(containerToReplicate, updatedEnv);