UNPKG

@deploystack/docker-to-iac

Version:

Translate docker run and docker compose file to Infrastructure as Code

188 lines (187 loc) 7.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const processEnvironmentVariablesGeneration_1 = require("../../../src/utils/processEnvironmentVariablesGeneration"); const base_parser_1 = require("../../../src/parsers/base-parser"); // Mock semver to control its behavior vitest_1.vi.mock('semver', () => { return { default: { valid: vitest_1.vi.fn().mockReturnValue(true), coerce: vitest_1.vi.fn(version => version), satisfies: vitest_1.vi.fn().mockReturnValue(true), rcompare: vitest_1.vi.fn().mockImplementation((a, b) => a > b ? -1 : 1) }, valid: vitest_1.vi.fn().mockReturnValue(true), coerce: vitest_1.vi.fn(version => version), satisfies: vitest_1.vi.fn().mockReturnValue(true), rcompare: vitest_1.vi.fn().mockImplementation((a, b) => a > b ? -1 : 1) }; }); (0, vitest_1.describe)('processEnvironmentVariablesGeneration', () => { (0, vitest_1.test)('should return original environment if no config provided', () => { const env = { VAR1: 'value1', VAR2: 'value2' }; const image = { registry_type: base_parser_1.RegistryType.DOCKER_HUB, repository: 'nginx', tag: 'latest' }; const result = (0, processEnvironmentVariablesGeneration_1.processEnvironmentVariablesGeneration)(env, image); (0, vitest_1.expect)(result).toEqual(env); }); (0, vitest_1.test)('should return original environment if image not found in config', () => { const env = { VAR1: 'value1', VAR2: 'value2' }; const image = { registry_type: base_parser_1.RegistryType.DOCKER_HUB, repository: 'nginx', tag: 'latest' }; const config = { 'postgresql': { versions: { '*': { environment: { POSTGRES_PASSWORD: { type: 'password', length: 16 } } } } } }; const result = (0, processEnvironmentVariablesGeneration_1.processEnvironmentVariablesGeneration)(env, image, config); (0, vitest_1.expect)(result).toEqual(env); }); (0, vitest_1.test)('should generate environment variables based on configuration', () => { const env = { EXISTING: 'value' }; const image = { registry_type: base_parser_1.RegistryType.DOCKER_HUB, repository: 'mariadb', tag: '10.5' }; const config = { 'mariadb': { versions: { '*': { environment: { MYSQL_ROOT_PASSWORD: { type: 'password', length: 8 }, MYSQL_DATABASE: { type: 'string', length: 8 } } } } } }; // Mock Math.random for predictable test outputs const mathRandomSpy = vitest_1.vi.spyOn(Math, 'random'); mathRandomSpy.mockReturnValue(0.5); const result = (0, processEnvironmentVariablesGeneration_1.processEnvironmentVariablesGeneration)(env, image, config); (0, vitest_1.expect)(result).toHaveProperty('EXISTING', 'value'); (0, vitest_1.expect)(result).toHaveProperty('MYSQL_ROOT_PASSWORD'); (0, vitest_1.expect)(result).toHaveProperty('MYSQL_DATABASE'); (0, vitest_1.expect)(result.MYSQL_ROOT_PASSWORD).toHaveLength(8); (0, vitest_1.expect)(result.MYSQL_DATABASE).toHaveLength(8); mathRandomSpy.mockRestore(); }); (0, vitest_1.test)('should match version-specific configuration', () => { const env = {}; const image = { registry_type: base_parser_1.RegistryType.DOCKER_HUB, repository: 'postgres', tag: '13' }; const config = { 'postgres': { versions: { '13': { environment: { PG_VERSION_13: { type: 'string', length: 8 } } }, '12': { environment: { PG_VERSION_12: { type: 'string', length: 8 } } } } } }; const mathRandomSpy = vitest_1.vi.spyOn(Math, 'random'); mathRandomSpy.mockReturnValue(0.5); const result = (0, processEnvironmentVariablesGeneration_1.processEnvironmentVariablesGeneration)(env, image, config); (0, vitest_1.expect)(result).toHaveProperty('PG_VERSION_13'); (0, vitest_1.expect)(result).not.toHaveProperty('PG_VERSION_12'); mathRandomSpy.mockRestore(); }); (0, vitest_1.test)('should try alternative repository formats', () => { const env = {}; const image = { registry_type: base_parser_1.RegistryType.DOCKER_HUB, repository: 'nginx', tag: 'latest' }; const config = { 'docker.io/library/nginx': { versions: { '*': { environment: { NGINX_VAR: { type: 'string', length: 8 } } } } } }; const mathRandomSpy = vitest_1.vi.spyOn(Math, 'random'); mathRandomSpy.mockReturnValue(0.5); const result = (0, processEnvironmentVariablesGeneration_1.processEnvironmentVariablesGeneration)(env, image, config); (0, vitest_1.expect)(result).toHaveProperty('NGINX_VAR'); mathRandomSpy.mockRestore(); }); (0, vitest_1.test)('should handle wildcard version matching', () => { const env = {}; const image = { registry_type: base_parser_1.RegistryType.DOCKER_HUB, repository: 'redis', tag: '6.2' }; const config = { 'redis': { versions: { '*': { environment: { REDIS_PASSWORD: { type: 'password', length: 8 } } } } } }; const mathRandomSpy = vitest_1.vi.spyOn(Math, 'random'); mathRandomSpy.mockReturnValue(0.5); const result = (0, processEnvironmentVariablesGeneration_1.processEnvironmentVariablesGeneration)(env, image, config); (0, vitest_1.expect)(result).toHaveProperty('REDIS_PASSWORD'); mathRandomSpy.mockRestore(); }); (0, vitest_1.test)('should handle numbers with min/max', () => { const env = {}; const image = { registry_type: base_parser_1.RegistryType.DOCKER_HUB, repository: 'app', tag: 'latest' }; const config = { 'app': { versions: { '*': { environment: { PORT: { type: 'number', min: 3000, max: 5000 } } } } } }; const mathRandomSpy = vitest_1.vi.spyOn(Math, 'random'); mathRandomSpy.mockReturnValue(0.5); const result = (0, processEnvironmentVariablesGeneration_1.processEnvironmentVariablesGeneration)(env, image, config); (0, vitest_1.expect)(result).toHaveProperty('PORT'); (0, vitest_1.expect)(parseInt(result.PORT)).toBeGreaterThanOrEqual(3000); (0, vitest_1.expect)(parseInt(result.PORT)).toBeLessThanOrEqual(5000); mathRandomSpy.mockRestore(); }); });