168 lines
6.4 KiB
JavaScript
168 lines
6.4 KiB
JavaScript
import { describe, it, beforeEach, afterEach } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { MockApacheDriver } from '../../src/infrastructure/apache/mock.apache.driver.js';
|
|
import { SiteService } from '../../src/services/site.service.js';
|
|
import { TemplateService } from '../../src/services/template.service.js';
|
|
|
|
describe('Integration Tests: Atomic Rollback Transaction (rollback.test.js)', () => {
|
|
const testTmpDir = path.resolve(process.cwd(), '.tmp/test-rollback');
|
|
let mockDriver;
|
|
let siteService;
|
|
|
|
beforeEach(() => {
|
|
if (fs.existsSync(testTmpDir)) {
|
|
fs.rmSync(testTmpDir, { recursive: true, force: true });
|
|
}
|
|
mockDriver = new MockApacheDriver(testTmpDir);
|
|
siteService = new SiteService(mockDriver, new TemplateService());
|
|
});
|
|
|
|
afterEach(() => {
|
|
if (fs.existsSync(testTmpDir)) {
|
|
fs.rmSync(testTmpDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('debe aprovisionar exitosamente un subdominio Modo A cuando la sintaxis es correcta', async () => {
|
|
const result = await siteService.createSubdomain({
|
|
name: 'healthy-site',
|
|
type: 'static'
|
|
});
|
|
|
|
assert.equal(result.name, 'healthy-site');
|
|
assert.equal(result.enabled, true);
|
|
|
|
// Verificar que existe el .conf
|
|
const confPath = path.join(mockDriver.sitesAvailable, 'healthy-site.conf');
|
|
assert.ok(fs.existsSync(confPath), 'El archivo .conf debe existir en sites-available');
|
|
|
|
// Verificar que existe el symlink en sites-enabled
|
|
const enabledPath = path.join(mockDriver.sitesEnabled, 'healthy-site.conf');
|
|
assert.ok(fs.existsSync(enabledPath), 'El symlink debe existir en sites-enabled');
|
|
|
|
// Verificar que la carpeta web fue creada
|
|
const webPath = path.join(mockDriver.webRoot, 'healthy-site');
|
|
assert.ok(fs.existsSync(webPath), 'La carpeta web debe existir en www');
|
|
assert.ok(fs.existsSync(path.join(webPath, 'index.html')), 'El index.html debe existir');
|
|
});
|
|
|
|
it('debe ejecutar ROLLBACK ATÓMICO completo si apache2ctl configtest falla en Modo A recién creado', async () => {
|
|
// Usamos un mock template service que inyecta TRIGGER_SYNTAX_ERROR para simular un fallo sintáctico
|
|
class FaultyTemplateService extends TemplateService {
|
|
renderStaticSiteTemplate(params) {
|
|
return `<VirtualHost *:80>
|
|
ServerName ${params.subdomain}.elingejose.com
|
|
TRIGGER_SYNTAX_ERROR
|
|
</VirtualHost>`;
|
|
}
|
|
}
|
|
|
|
const faultySiteService = new SiteService(mockDriver, new FaultyTemplateService());
|
|
|
|
const name = 'broken-subdomain';
|
|
const confPath = path.join(mockDriver.sitesAvailable, `${name}.conf`);
|
|
const enabledPath = path.join(mockDriver.sitesEnabled, `${name}.conf`);
|
|
const webPath = path.join(mockDriver.webRoot, name);
|
|
|
|
let caughtError = null;
|
|
try {
|
|
await faultySiteService.createSubdomain({
|
|
name,
|
|
type: 'static'
|
|
});
|
|
} catch (err) {
|
|
caughtError = err;
|
|
}
|
|
|
|
// 1. Debe haber lanzado error con código 422 y rollbackExecuted = true
|
|
assert.ok(caughtError, 'Debe lanzar un error');
|
|
assert.equal(caughtError.status, 422);
|
|
assert.equal(caughtError.code, 'APACHE_CONFIGTEST_FAILED');
|
|
assert.equal(caughtError.rollbackExecuted, true);
|
|
assert.ok(caughtError.detail.includes('TRIGGER_SYNTAX_ERROR'));
|
|
|
|
// 2. Debe haber eliminado el archivo .conf de sites-available (Rollback)
|
|
assert.equal(fs.existsSync(confPath), false, 'El archivo .conf en sites-available debió ser eliminado por el rollback');
|
|
|
|
// 3. Debe haber deshabilitado el symlink en sites-enabled (Rollback)
|
|
assert.equal(fs.existsSync(enabledPath), false, 'El symlink en sites-enabled debió ser eliminado por el rollback');
|
|
|
|
// 4. Debe haber purgado la carpeta web creada para este intento (Rollback)
|
|
assert.equal(fs.existsSync(webPath), false, 'La carpeta web recién creada debió ser purgada por el rollback');
|
|
});
|
|
|
|
it('debe PRESERVAR la carpeta web si ya existía antes del intento fallido de creación', async () => {
|
|
class FaultyTemplateService extends TemplateService {
|
|
renderStaticSiteTemplate(params) {
|
|
return `<VirtualHost *:80>
|
|
ServerName ${params.subdomain}.elingejose.com
|
|
TRIGGER_SYNTAX_ERROR
|
|
</VirtualHost>`;
|
|
}
|
|
}
|
|
|
|
const name = 'pre-existing-folder';
|
|
const webPath = path.join(mockDriver.webRoot, name);
|
|
// Creamos la carpeta previamente
|
|
fs.mkdirSync(webPath, { recursive: true });
|
|
fs.writeFileSync(path.join(webPath, 'precious-data.txt'), 'NO BORRAR');
|
|
|
|
const faultySiteService = new SiteService(mockDriver, new FaultyTemplateService());
|
|
|
|
try {
|
|
await faultySiteService.createSubdomain({
|
|
name,
|
|
type: 'static'
|
|
});
|
|
assert.fail('Debió fallar por configtest');
|
|
} catch (err) {
|
|
assert.equal(err.status, 422);
|
|
assert.equal(err.rollbackExecuted, true);
|
|
}
|
|
|
|
// La carpeta web preexistente DEBE seguir existiendo intacta
|
|
assert.ok(fs.existsSync(webPath), 'La carpeta web preexistente debe ser preservada');
|
|
assert.ok(fs.existsSync(path.join(webPath, 'precious-data.txt')), 'El contenido previo debe ser preservado');
|
|
});
|
|
|
|
it('debe revertir a la configuración previa si falla la actualización (update)', async () => {
|
|
// 1. Crear subdominio válido
|
|
await siteService.createSubdomain({
|
|
name: 'update-target',
|
|
type: 'static'
|
|
});
|
|
|
|
const confPath = path.join(mockDriver.sitesAvailable, 'update-target.conf');
|
|
const originalContent = fs.readFileSync(confPath, 'utf-8');
|
|
|
|
// 2. Intentar actualizar con una plantilla que genera error sintáctico
|
|
class BrokenUpdateTemplateService extends TemplateService {
|
|
renderProxySiteTemplate() {
|
|
return `<VirtualHost *:80>
|
|
TRIGGER_SYNTAX_ERROR
|
|
</VirtualHost>`;
|
|
}
|
|
}
|
|
|
|
const updateService = new SiteService(mockDriver, new BrokenUpdateTemplateService());
|
|
|
|
try {
|
|
await updateService.updateSubdomain('update-target', {
|
|
type: 'proxy',
|
|
port: 9090,
|
|
forceConfirmed: true
|
|
});
|
|
assert.fail('Debió fallar con error de sintaxis');
|
|
} catch (err) {
|
|
assert.equal(err.status, 422);
|
|
assert.equal(err.rollbackExecuted, true);
|
|
}
|
|
|
|
// La configuración debe haber sido restaurada a su estado original
|
|
const restoredContent = fs.readFileSync(confPath, 'utf-8');
|
|
assert.equal(restoredContent, originalContent, 'El archivo .conf debe haberse revertido al original');
|
|
});
|
|
});
|