@deploystack/docker-to-iac
Version:
Translate docker run and docker compose file to Infrastructure as Code
129 lines (128 loc) • 5.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const validate_1 = require("../../../../src/sources/compose/validate");
const base_1 = require("../../../../src/sources/base");
(0, vitest_1.describe)('validateDockerCompose', () => {
(0, vitest_1.test)('should validate a valid docker-compose configuration', () => {
const validConfig = {
services: {
webapp: {
image: 'nginx:latest',
ports: ['80:80'],
environment: {
NODE_ENV: 'production'
}
},
database: {
image: 'postgres:13',
volumes: ['pgdata:/var/lib/postgresql/data']
}
}
};
// Should not throw any error
(0, vitest_1.expect)(() => (0, validate_1.validateDockerCompose)(validConfig)).not.toThrow();
});
(0, vitest_1.test)('should validate a minimal docker-compose configuration', () => {
const minimalConfig = {
services: {
app: {
image: 'alpine:latest'
}
}
};
// Should not throw any error
(0, vitest_1.expect)(() => (0, validate_1.validateDockerCompose)(minimalConfig)).not.toThrow();
});
(0, vitest_1.test)('should throw error when no services are defined', () => {
const emptyServicesConfig = {
services: {}
};
(0, vitest_1.expect)(() => (0, validate_1.validateDockerCompose)(emptyServicesConfig))
.toThrow(base_1.SourceValidationError);
(0, vitest_1.expect)(() => (0, validate_1.validateDockerCompose)(emptyServicesConfig))
.toThrow('No services found in docker-compose file');
});
(0, vitest_1.test)('should throw error when services is missing', () => {
const missingServicesConfig = {};
(0, vitest_1.expect)(() => (0, validate_1.validateDockerCompose)(missingServicesConfig))
.toThrow(base_1.SourceValidationError);
(0, vitest_1.expect)(() => (0, validate_1.validateDockerCompose)(missingServicesConfig))
.toThrow('No services found in docker-compose file');
});
(0, vitest_1.test)('should throw error when a service does not have an image', () => {
const missingImageConfig = {
services: {
webapp: {
image: 'nginx:latest'
},
database: {
// missing image
ports: ['5432:5432']
} // Type assertion to bypass TypeScript checking
}
};
(0, vitest_1.expect)(() => (0, validate_1.validateDockerCompose)(missingImageConfig))
.toThrow(base_1.SourceValidationError);
// Use a regex pattern to match part of the error message to be more flexible
(0, vitest_1.expect)(() => (0, validate_1.validateDockerCompose)(missingImageConfig))
.toThrow(/Service 'database' does not have an image specified/);
});
(0, vitest_1.test)('should throw error when a service has an empty image', () => {
const emptyImageConfig = {
services: {
webapp: {
image: '' // empty image
}
}
};
(0, vitest_1.expect)(() => (0, validate_1.validateDockerCompose)(emptyImageConfig))
.toThrow(base_1.SourceValidationError);
// Use a regex pattern to match part of the error message to be more flexible
(0, vitest_1.expect)(() => (0, validate_1.validateDockerCompose)(emptyImageConfig))
.toThrow(/Service 'webapp' does not have an image specified/);
});
(0, vitest_1.test)('should accept configuration with optional properties', () => {
const configWithOptionals = {
services: {
webapp: {
image: 'node:14',
ports: ['3000:3000'],
command: 'npm start',
restart: 'always',
volumes: ['./app:/app'],
environment: ['NODE_ENV=production', 'DEBUG=false']
}
}
};
// Should not throw any error
(0, vitest_1.expect)(() => (0, validate_1.validateDockerCompose)(configWithOptionals)).not.toThrow();
});
(0, vitest_1.test)('should accept configuration with array-style environment variables', () => {
const configWithArrayEnv = {
services: {
webapp: {
image: 'node:14',
environment: ['NODE_ENV=production', 'DEBUG=false']
}
}
};
// Should not throw any error
(0, vitest_1.expect)(() => (0, validate_1.validateDockerCompose)(configWithArrayEnv)).not.toThrow();
});
(0, vitest_1.test)('should accept configuration with object-style environment variables', () => {
const configWithObjectEnv = {
services: {
webapp: {
image: 'node:14',
environment: {
NODE_ENV: 'production',
DEBUG: 'false'
}
}
}
};
// Should not throw any error
(0, vitest_1.expect)(() => (0, validate_1.validateDockerCompose)(configWithObjectEnv)).not.toThrow();
});
});