@deploystack/docker-to-iac
Version:
Translate docker run and docker compose file to Infrastructure as Code
263 lines (262 loc) • 9.92 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const resolveServiceConnections_1 = require("../../../src/utils/resolveServiceConnections");
const base_parser_1 = require("../../../src/parsers/base-parser");
(0, vitest_1.describe)('resolveServiceConnections', () => {
(0, vitest_1.test)('should resolve simple service connections', () => {
// Create a simple application config with two services
const config = {
services: {
'web': {
image: {
registry_type: base_parser_1.RegistryType.DOCKER_HUB,
repository: 'nginx'
},
ports: [],
volumes: [],
environment: {
'DATABASE_URL': 'postgres://localhost:5432/db'
}
},
'db': {
image: {
registry_type: base_parser_1.RegistryType.DOCKER_HUB,
repository: 'postgres'
},
ports: [],
volumes: [],
environment: {}
}
}
};
// Create service connections config
const serviceConnections = {
mappings: [
{
fromService: 'web',
toService: 'db',
environmentVariables: ['DATABASE_URL']
}
]
};
// Resolve connections
const result = (0, resolveServiceConnections_1.resolveServiceConnections)(config, serviceConnections);
// Verify the result
(0, vitest_1.expect)(result).toHaveLength(1);
(0, vitest_1.expect)(result[0]).toEqual({
fromService: 'web',
toService: 'db',
property: 'hostport', // Default property
variables: {
'DATABASE_URL': {
originalValue: 'postgres://localhost:5432/db',
transformedValue: 'postgres://localhost:5432/db' // Initially the same
}
}
});
});
(0, vitest_1.test)('should use custom property if specified', () => {
const config = {
services: {
'web': {
image: {
registry_type: base_parser_1.RegistryType.DOCKER_HUB,
repository: 'nginx'
},
ports: [],
volumes: [],
environment: {
'DATABASE_URL': 'postgres://localhost:5432/db'
}
},
'db': {
image: {
registry_type: base_parser_1.RegistryType.DOCKER_HUB,
repository: 'postgres'
},
ports: [],
volumes: [],
environment: {}
}
}
};
const serviceConnections = {
mappings: [
{
fromService: 'web',
toService: 'db',
environmentVariables: ['DATABASE_URL'],
property: 'connectionString'
}
]
};
const result = (0, resolveServiceConnections_1.resolveServiceConnections)(config, serviceConnections);
(0, vitest_1.expect)(result).toHaveLength(1);
(0, vitest_1.expect)(result[0].property).toBe('connectionString');
});
(0, vitest_1.test)('should match partial environment variable names', () => {
const config = {
services: {
'app': {
image: {
registry_type: base_parser_1.RegistryType.DOCKER_HUB,
repository: 'app'
},
ports: [],
volumes: [],
environment: {
'REDIS_URL': 'redis://localhost:6379',
'REDIS_PASSWORD': 'secret'
}
},
'redis': {
image: {
registry_type: base_parser_1.RegistryType.DOCKER_HUB,
repository: 'redis'
},
ports: [],
volumes: [],
environment: {}
}
}
};
const serviceConnections = {
mappings: [
{
fromService: 'app',
toService: 'redis',
environmentVariables: ['REDIS']
}
]
};
const result = (0, resolveServiceConnections_1.resolveServiceConnections)(config, serviceConnections);
(0, vitest_1.expect)(result).toHaveLength(1);
(0, vitest_1.expect)(Object.keys(result[0].variables)).toHaveLength(2);
(0, vitest_1.expect)(result[0].variables).toHaveProperty('REDIS_URL');
(0, vitest_1.expect)(result[0].variables).toHaveProperty('REDIS_PASSWORD');
});
(0, vitest_1.test)('should skip connections with missing services', () => {
const config = {
services: {
'web': {
image: {
registry_type: base_parser_1.RegistryType.DOCKER_HUB,
repository: 'nginx'
},
ports: [],
volumes: [],
environment: {
'DATABASE_URL': 'postgres://localhost:5432/db'
}
}
}
};
const serviceConnections = {
mappings: [
{
fromService: 'web',
toService: 'db', // This service doesn't exist
environmentVariables: ['DATABASE_URL']
}
]
};
const result = (0, resolveServiceConnections_1.resolveServiceConnections)(config, serviceConnections);
// Should return empty array or warn and skip
(0, vitest_1.expect)(result).toHaveLength(0);
});
(0, vitest_1.test)('should handle multiple mappings', () => {
const config = {
services: {
'web': {
image: {
registry_type: base_parser_1.RegistryType.DOCKER_HUB,
repository: 'nginx'
},
ports: [],
volumes: [],
environment: {
'DATABASE_URL': 'postgres://localhost:5432/db',
'REDIS_URL': 'redis://localhost:6379'
}
},
'db': {
image: {
registry_type: base_parser_1.RegistryType.DOCKER_HUB,
repository: 'postgres'
},
ports: [],
volumes: [],
environment: {}
},
'cache': {
image: {
registry_type: base_parser_1.RegistryType.DOCKER_HUB,
repository: 'redis'
},
ports: [],
volumes: [],
environment: {}
}
}
};
const serviceConnections = {
mappings: [
{
fromService: 'web',
toService: 'db',
environmentVariables: ['DATABASE_URL']
},
{
fromService: 'web',
toService: 'cache',
environmentVariables: ['REDIS_URL']
}
]
};
const result = (0, resolveServiceConnections_1.resolveServiceConnections)(config, serviceConnections);
(0, vitest_1.expect)(result).toHaveLength(2);
(0, vitest_1.expect)(result[0].toService).toBe('db');
(0, vitest_1.expect)(result[0].variables).toHaveProperty('DATABASE_URL');
(0, vitest_1.expect)(result[1].toService).toBe('cache');
(0, vitest_1.expect)(result[1].variables).toHaveProperty('REDIS_URL');
});
(0, vitest_1.test)('should handle case with no matching environment variables', () => {
const config = {
services: {
'web': {
image: {
registry_type: base_parser_1.RegistryType.DOCKER_HUB,
repository: 'nginx'
},
ports: [],
volumes: [],
environment: {
'SERVER_PORT': '8080'
}
},
'db': {
image: {
registry_type: base_parser_1.RegistryType.DOCKER_HUB,
repository: 'postgres'
},
ports: [],
volumes: [],
environment: {}
}
}
};
const serviceConnections = {
mappings: [
{
fromService: 'web',
toService: 'db',
environmentVariables: ['DATABASE_URL'] // Not in environment
}
]
};
const result = (0, resolveServiceConnections_1.resolveServiceConnections)(config, serviceConnections);
(0, vitest_1.expect)(result).toHaveLength(1);
(0, vitest_1.expect)(Object.keys(result[0].variables)).toHaveLength(0); // No variables matched
});
});