@deploystack/docker-to-iac
Version:
Translate docker run and docker compose file to Infrastructure as Code
168 lines (167 loc) • 7.56 kB
JavaScript
;
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.runTest4 = runTest4;
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_validator_1 = require("./utils/render-validator");
// Constants for directories
const DOCKER_RUN_DIR = (0, path_1.join)(__dirname, 'docker-run-files');
const DOCKER_COMPOSE_DIR = (0, path_1.join)(__dirname, 'docker-compose-files');
const OUTPUT_DIR = (0, path_1.join)(__dirname, 'output');
const RENDER_SCHEMA_URL = 'https://render.com/schema/render.yaml.json';
/**
* Run the Docker Run test with schema validation.
*/
async function runDockerRunTest4() {
console.log('\n--- Running Docker Run Test 4 (Render Translation with Schema Validation) ---');
// Create output directory for this subtest
const testOutputDir = (0, path_1.join)(OUTPUT_DIR, 'test4', 'docker-run', 'rnd');
if (!(0, fs_1.existsSync)(testOutputDir)) {
(0, fs_1.mkdirSync)(testOutputDir, { recursive: true });
}
let testPassed = true;
try {
// Read and normalize the docker run command
const dockerRunPath = (0, path_1.join)(DOCKER_RUN_DIR, 'test4.txt');
const command = (0, fs_1.readFileSync)(dockerRunPath, 'utf8')
.replace(/\\\n/g, ' ') // Remove line continuations
.replace(/\s+/g, ' ') // Normalize whitespace
.trim(); // Remove leading/trailing whitespace
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
});
// Save the main render.yaml file
const renderYamlPath = (0, path_1.join)(testOutputDir, 'render.yaml');
const renderFileData = renderTranslationResult.files['render.yaml'];
if (!renderFileData) {
throw new Error('render.yaml not found in translation result');
}
(0, fs_1.writeFileSync)(renderYamlPath, renderFileData.content);
console.log(`✓ Created Render output: render.yaml`);
// Validate against Render.com schema
const renderYaml = yaml.parse(renderFileData.content);
console.log('Validating Render YAML against official schema...');
const isValid = await (0, render_validator_1.validateRenderSchema)(renderYaml, RENDER_SCHEMA_URL);
if (!isValid) {
testPassed = false;
console.error('❌ Schema validation failed for Docker Run test');
}
else {
console.log('✅ Schema validation passed for Docker Run test');
}
}
catch (error) {
testPassed = false;
console.error('❌ Test execution error:', error);
}
console.log(`--- Docker Run Test 4 ${testPassed ? 'PASSED ✓' : 'FAILED ❌'} ---`);
return testPassed;
}
/**
* Run the Docker Compose test with schema validation.
*/
async function runDockerComposeTest4() {
console.log('\n--- Running Docker Compose Test 4 (Render Translation with Schema Validation) ---');
// Create output directory for this subtest
const testOutputDir = (0, path_1.join)(OUTPUT_DIR, 'test4', 'docker-compose', 'rnd');
if (!(0, fs_1.existsSync)(testOutputDir)) {
(0, fs_1.mkdirSync)(testOutputDir, { recursive: true });
}
let testPassed = true;
try {
// Read the docker compose file
const dockerComposePath = (0, path_1.join)(DOCKER_COMPOSE_DIR, 'test4.yml');
const composeContent = (0, fs_1.readFileSync)(dockerComposePath, 'utf8');
console.log('Testing Docker Compose translation to Render...');
const renderTranslationResult = (0, index_1.translate)(composeContent, {
source: 'compose',
target: 'RND',
templateFormat: base_parser_1.TemplateFormat.yaml
});
// Save the main render.yaml file
const renderYamlPath = (0, path_1.join)(testOutputDir, 'render.yaml');
const renderFileData = renderTranslationResult.files['render.yaml'];
if (!renderFileData) {
throw new Error('render.yaml not found in translation result');
}
(0, fs_1.writeFileSync)(renderYamlPath, renderFileData.content);
console.log(`✓ Created Render output: render.yaml`);
// Validate against Render.com schema
const renderYaml = yaml.parse(renderFileData.content);
console.log('Validating Render YAML against official schema...');
const isValid = await (0, render_validator_1.validateRenderSchema)(renderYaml, RENDER_SCHEMA_URL);
if (!isValid) {
testPassed = false;
console.error('❌ Schema validation failed for Docker Compose test');
}
else {
console.log('✅ Schema validation passed for Docker Compose test');
}
}
catch (error) {
testPassed = false;
console.error('❌ Test execution error:', error);
}
console.log(`--- Docker Compose Test 4 ${testPassed ? 'PASSED ✓' : 'FAILED ❌'} ---`);
return testPassed;
}
/**
* Run the test for test4 with schema validation.
*/
async function runTest4() {
console.log('\n=== Running Test 4: Render Translation with Schema Validation ===');
// Create output directory for this test
const testOutputDir = (0, path_1.join)(OUTPUT_DIR, 'test4');
if (!(0, fs_1.existsSync)(testOutputDir)) {
(0, fs_1.mkdirSync)(testOutputDir, { recursive: true });
}
// Run Docker Run test
const dockerRunTestPassed = await runDockerRunTest4();
// Run Docker Compose test
const dockerComposeTestPassed = await runDockerComposeTest4();
// Overall test passes if both subtests pass
const overallTestPassed = dockerRunTestPassed && dockerComposeTestPassed;
console.log(`=== Test 4 ${overallTestPassed ? 'PASSED ✓' : 'FAILED ❌'} ===`);
return overallTestPassed;
}