UNPKG

@deploystack/docker-to-iac

Version:

Translate docker run and docker compose file to Infrastructure as Code

412 lines (411 loc) 19.6 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.runTest5 = runTest5; const index_1 = require("../../src/index"); const base_parser_1 = require("../../src/parsers/base-parser"); const fs_1 = require("fs"); const path_1 = require("path"); const yaml = __importStar(require("yaml")); const render_1 = require("./assertions/render"); const digitalocean_1 = require("./assertions/digitalocean"); const do_port_assertions_1 = require("./assertions/do-port-assertions"); const port_assertions_1 = require("./assertions/port-assertions"); // Constants for directories const OUTPUT_DIR = (0, path_1.join)(__dirname, 'output'); /** * Run the Docker run command test for Portkey Gateway */ async function runDockerRunPortkeyTest() { console.log('\n--- Running Docker Run Portkey Gateway Test ---'); // Create output directory for this subtest const testOutputDir = (0, path_1.join)(OUTPUT_DIR, 'test5', 'docker-run'); if (!(0, fs_1.existsSync)(testOutputDir)) { (0, fs_1.mkdirSync)(testOutputDir, { recursive: true }); } let testPassed = true; try { // The docker run command const command = 'docker run --rm -p 8787:8787 portkeyai/gateway:latest'; console.log(`Running test for command: ${command}`); // Test translation to Render console.log('Testing translation to Render...'); const renderTranslationResult = (0, index_1.translate)(command, { source: 'run', target: 'RND', templateFormat: base_parser_1.TemplateFormat.yaml }); // Create directory for Render output const renderOutputDir = (0, path_1.join)(testOutputDir, 'rnd'); if (!(0, fs_1.existsSync)(renderOutputDir)) { (0, fs_1.mkdirSync)(renderOutputDir, { recursive: true }); } // Save all files with proper directory structure Object.entries(renderTranslationResult.files).forEach(([path, fileData]) => { const fullPath = (0, path_1.join)(renderOutputDir, path); const dir = (0, path_1.dirname)(fullPath); if (!(0, fs_1.existsSync)(dir)) { (0, fs_1.mkdirSync)(dir, { recursive: true }); } (0, fs_1.writeFileSync)(fullPath, fileData.content); console.log(`✓ Created Render output: ${path}`); }); // Run assertions for Render const renderYamlPath = (0, path_1.join)(renderOutputDir, 'render.yaml'); if ((0, fs_1.existsSync)(renderYamlPath)) { const renderYaml = yaml.parse((0, fs_1.readFileSync)(renderYamlPath, 'utf8')); try { // 1. Validate YAML structure (0, render_1.assertRenderYamlStructure)(renderYaml); console.log('✓ Render YAML structure validation passed'); // 2. Check for port mapping const portMappingValid = (0, port_assertions_1.validatePortMappingInRender)(renderYaml, 'default', 8787); if (portMappingValid) { console.log('✓ Port mapping validation passed'); } else { testPassed = false; console.error('❌ Port mapping validation failed'); } // 3. Check image URL is correct const defaultService = renderYaml.services.find((svc) => svc.name === 'default'); if (!defaultService) { testPassed = false; console.error('❌ Default service not found in Render YAML'); } else { const imageUrl = defaultService.image?.url; if (imageUrl && imageUrl.includes('portkeyai/gateway:latest')) { console.log('✓ Image URL validation passed'); } else { testPassed = false; console.error(`❌ Image URL validation failed: expected 'portkeyai/gateway:latest', got '${imageUrl}'`); } } } catch (error) { testPassed = false; console.error('❌ Render YAML validation failed:', error); } } else { testPassed = false; console.error('❌ Render YAML file not found'); } // Test translation to DigitalOcean console.log('\nTesting translation to DigitalOcean...'); const doTranslationResult = (0, index_1.translate)(command, { source: 'run', target: 'DOP', templateFormat: base_parser_1.TemplateFormat.yaml }); // Create directory for DigitalOcean output const doOutputDir = (0, path_1.join)(testOutputDir, 'dop'); if (!(0, fs_1.existsSync)(doOutputDir)) { (0, fs_1.mkdirSync)(doOutputDir, { recursive: true }); } // Save all files with proper directory structure Object.entries(doTranslationResult.files).forEach(([path, fileData]) => { const fullPath = (0, path_1.join)(doOutputDir, path); const dir = (0, path_1.dirname)(fullPath); if (!(0, fs_1.existsSync)(dir)) { (0, fs_1.mkdirSync)(dir, { recursive: true }); } (0, fs_1.writeFileSync)(fullPath, fileData.content); console.log(`✓ Created DigitalOcean output: ${path}`); }); // Run assertions for DigitalOcean const doYamlPath = (0, path_1.join)(doOutputDir, '.do/deploy.template.yaml'); if ((0, fs_1.existsSync)(doYamlPath)) { const doYaml = yaml.parse((0, fs_1.readFileSync)(doYamlPath, 'utf8')); try { // 1. Validate YAML structure (0, digitalocean_1.assertDigitalOceanYamlStructure)(doYaml); console.log('✓ DigitalOcean YAML structure validation passed'); // 2. Check for port mapping - service name may be different due to DO naming requirements const serviceName = doYaml.spec.services[0].name; // In DigitalOcean, the http_port should be set to 8787 const portMappingValid = (0, do_port_assertions_1.validatePortMappingInDigitalOcean)(doYaml, serviceName, 8787); if (portMappingValid) { console.log('✓ DigitalOcean port mapping validation passed'); } else { testPassed = false; console.error('❌ DigitalOcean port mapping validation failed'); } // 3. Check image repository and tag are correct const service = doYaml.spec.services[0]; if (!service.image) { testPassed = false; console.error('❌ Service image not found in DigitalOcean YAML'); } else { const { registry, repository, tag } = service.image; // DigitalOcean splits the image into registry and repository if (registry === 'portkeyai' && repository === 'gateway' && tag === 'latest') { console.log('✓ Image repository and tag validation passed'); } else { testPassed = false; console.error(`❌ Image validation failed: expected 'portkeyai/gateway:latest', got registry='${registry}', repository='${repository}', tag='${tag}'`); } } } catch (error) { testPassed = false; console.error('❌ DigitalOcean YAML validation failed:', error); } } else { testPassed = false; console.error('❌ DigitalOcean YAML file not found'); } } catch (error) { testPassed = false; console.error('❌ Test execution error:', error); } console.log(`--- Docker Run Portkey Gateway Test ${testPassed ? 'PASSED ✓' : 'FAILED ❌'} ---`); return testPassed; } /** * Run the Docker Compose test for Portkey Gateway */ async function runDockerComposePortkeyTest() { console.log('\n--- Running Docker Compose Portkey Gateway Test ---'); // Create output directory for this subtest const testOutputDir = (0, path_1.join)(OUTPUT_DIR, 'test5', 'docker-compose'); if (!(0, fs_1.existsSync)(testOutputDir)) { (0, fs_1.mkdirSync)(testOutputDir, { recursive: true }); } let testPassed = true; try { // Create docker-compose.yml content equivalent to the docker run command const dockerComposeContent = ` version: '3' services: portkey: image: portkeyai/gateway:latest ports: - "8787:8787" `; console.log('Testing Docker Compose translation to Render...'); const renderTranslationResult = (0, index_1.translate)(dockerComposeContent, { source: 'compose', target: 'RND', templateFormat: base_parser_1.TemplateFormat.yaml }); // Create directory for Render output const renderOutputDir = (0, path_1.join)(testOutputDir, 'rnd'); if (!(0, fs_1.existsSync)(renderOutputDir)) { (0, fs_1.mkdirSync)(renderOutputDir, { recursive: true }); } // Save all files with proper directory structure Object.entries(renderTranslationResult.files).forEach(([path, fileData]) => { const fullPath = (0, path_1.join)(renderOutputDir, path); const dir = (0, path_1.dirname)(fullPath); if (!(0, fs_1.existsSync)(dir)) { (0, fs_1.mkdirSync)(dir, { recursive: true }); } (0, fs_1.writeFileSync)(fullPath, fileData.content); console.log(`✓ Created Render output: ${path}`); }); // Run assertions for Render const renderYamlPath = (0, path_1.join)(renderOutputDir, 'render.yaml'); if ((0, fs_1.existsSync)(renderYamlPath)) { const renderYaml = yaml.parse((0, fs_1.readFileSync)(renderYamlPath, 'utf8')); try { // 1. Validate YAML structure (0, render_1.assertRenderYamlStructure)(renderYaml); console.log('✓ Render YAML structure validation passed'); // 2. Check for port mapping const portMappingValid = (0, port_assertions_1.validatePortMappingInRender)(renderYaml, 'portkey', 8787); if (portMappingValid) { console.log('✓ Port mapping validation passed'); } else { testPassed = false; console.error('❌ Port mapping validation failed'); } // 3. Check image URL is correct const portkeyService = renderYaml.services.find((svc) => svc.name === 'portkey'); if (!portkeyService) { testPassed = false; console.error('❌ Portkey service not found in Render YAML'); } else { const imageUrl = portkeyService.image?.url; if (imageUrl && imageUrl.includes('portkeyai/gateway:latest')) { console.log('✓ Image URL validation passed'); } else { testPassed = false; console.error(`❌ Image URL validation failed: expected 'portkeyai/gateway:latest', got '${imageUrl}'`); } } } catch (error) { testPassed = false; console.error('❌ Render YAML validation failed:', error); } } else { testPassed = false; console.error('❌ Render YAML file not found'); } // Test translation to DigitalOcean console.log('\nTesting Docker Compose translation to DigitalOcean...'); const doTranslationResult = (0, index_1.translate)(dockerComposeContent, { source: 'compose', target: 'DOP', templateFormat: base_parser_1.TemplateFormat.yaml }); // Create directory for DigitalOcean output const doOutputDir = (0, path_1.join)(testOutputDir, 'dop'); if (!(0, fs_1.existsSync)(doOutputDir)) { (0, fs_1.mkdirSync)(doOutputDir, { recursive: true }); } // Save all files with proper directory structure Object.entries(doTranslationResult.files).forEach(([path, fileData]) => { const fullPath = (0, path_1.join)(doOutputDir, path); const dir = (0, path_1.dirname)(fullPath); if (!(0, fs_1.existsSync)(dir)) { (0, fs_1.mkdirSync)(dir, { recursive: true }); } (0, fs_1.writeFileSync)(fullPath, fileData.content); console.log(`✓ Created DigitalOcean output: ${path}`); }); // Run assertions for DigitalOcean const doYamlPath = (0, path_1.join)(doOutputDir, '.do/deploy.template.yaml'); if ((0, fs_1.existsSync)(doYamlPath)) { const doYaml = yaml.parse((0, fs_1.readFileSync)(doYamlPath, 'utf8')); try { // 1. Validate YAML structure (0, digitalocean_1.assertDigitalOceanYamlStructure)(doYaml); console.log('✓ DigitalOcean YAML structure validation passed'); // 2. Check for port mapping // Find portkey service - DigitalOcean may normalize service names let portkeyServiceName = ''; for (const service of doYaml.spec.services) { // Check http_port if (service.http_port === 8787) { portkeyServiceName = service.name; console.log(`Found service with correct port: ${portkeyServiceName}`); break; } // Check image details if (service.image && service.image.registry === 'portkeyai' && service.image.repository === 'gateway') { portkeyServiceName = service.name; console.log(`Found service with gateway image: ${portkeyServiceName}`); break; } } // If we didn't find a service by specific criteria, just use the first service if (!portkeyServiceName && doYaml.spec.services.length > 0) { portkeyServiceName = doYaml.spec.services[0].name; console.log(`Using first service as fallback: ${portkeyServiceName}`); } if (!portkeyServiceName) { testPassed = false; console.error('❌ Portkey service not found in DigitalOcean YAML'); } else { // Validate port mapping const portMappingValid = (0, do_port_assertions_1.validatePortMappingInDigitalOcean)(doYaml, portkeyServiceName, 8787); if (portMappingValid) { console.log('✓ DigitalOcean port mapping validation passed'); } else { testPassed = false; console.error('❌ DigitalOcean port mapping validation failed'); } // 3. Check image repository and tag are correct const service = doYaml.spec.services.find((svc) => svc.name === portkeyServiceName); if (!service || !service.image) { testPassed = false; console.error('❌ Service image not found in DigitalOcean YAML'); } else { const { registry, repository, tag } = service.image; // DigitalOcean splits the image into registry and repository if (registry === 'portkeyai' && repository === 'gateway' && tag === 'latest') { console.log('✓ Image repository and tag validation passed'); } else { testPassed = false; console.error(`❌ Image validation failed: expected 'portkeyai/gateway:latest', got registry='${registry}', repository='${repository}', tag='${tag}'`); } } } } catch (error) { testPassed = false; console.error('❌ DigitalOcean YAML validation failed:', error); } } else { testPassed = false; console.error('❌ DigitalOcean YAML file not found'); } } catch (error) { testPassed = false; console.error('❌ Test execution error:', error); } console.log(`--- Docker Compose Portkey Gateway Test ${testPassed ? 'PASSED ✓' : 'FAILED ❌'} ---`); return testPassed; } /** * Run the test for test5: Portkey Gateway port and image verification */ async function runTest5() { console.log('\n=== Running Test 5: Portkey Gateway Port and Image Verification ==='); // Create output directory for this test const testOutputDir = (0, path_1.join)(OUTPUT_DIR, 'test5'); if (!(0, fs_1.existsSync)(testOutputDir)) { (0, fs_1.mkdirSync)(testOutputDir, { recursive: true }); } // Run Docker Run test const dockerRunTestPassed = await runDockerRunPortkeyTest(); // Run Docker Compose test const dockerComposeTestPassed = await runDockerComposePortkeyTest(); // Overall test passes if both subtests pass const overallTestPassed = dockerRunTestPassed && dockerComposeTestPassed; console.log(`=== Test 5 ${overallTestPassed ? 'PASSED ✓' : 'FAILED ❌'} ===`); return overallTestPassed; }