coolify/tests/Feature/CloudInitScriptTest.php
Andras Bacsai 7061eacfa5 feat: add cloud-init script support for Hetzner server creation
This commit adds the ability to use cloud-init scripts when creating Hetzner servers through the integration. Users can write custom scripts that will be executed during server initialization, and optionally save these scripts at the team level for future reuse.

Key features:
- Textarea field for entering cloud-init scripts (bash or cloud-config YAML)
- Checkbox to save scripts for later use at team level
- Dropdown to load previously saved scripts
- Scripts are encrypted in the database
- Full validation and authorization checks
- Comprehensive unit and feature tests

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-10 19:37:16 +02:00

102 lines
2.8 KiB
PHP

<?php
// Note: These tests verify cloud-init script logic without database setup
it('validates cloud-init script is included in server params when provided', function () {
$cloudInitScript = "#!/bin/bash\necho 'Hello World'";
$params = [
'name' => 'test-server',
'server_type' => 'cx11',
'image' => 1,
'location' => 'nbg1',
'start_after_create' => true,
'ssh_keys' => [123],
'public_net' => [
'enable_ipv4' => true,
'enable_ipv6' => true,
],
];
// Add cloud-init script if provided
if (! empty($cloudInitScript)) {
$params['user_data'] = $cloudInitScript;
}
expect($params)
->toHaveKey('user_data')
->and($params['user_data'])->toBe("#!/bin/bash\necho 'Hello World'");
});
it('validates cloud-init script is not included when empty', function () {
$cloudInitScript = null;
$params = [
'name' => 'test-server',
'server_type' => 'cx11',
'image' => 1,
'location' => 'nbg1',
'start_after_create' => true,
'ssh_keys' => [123],
'public_net' => [
'enable_ipv4' => true,
'enable_ipv6' => true,
],
];
// Add cloud-init script if provided
if (! empty($cloudInitScript)) {
$params['user_data'] = $cloudInitScript;
}
expect($params)->not->toHaveKey('user_data');
});
it('validates cloud-init script is not included when empty string', function () {
$cloudInitScript = '';
$params = [
'name' => 'test-server',
'server_type' => 'cx11',
'image' => 1,
'location' => 'nbg1',
'start_after_create' => true,
'ssh_keys' => [123],
'public_net' => [
'enable_ipv4' => true,
'enable_ipv6' => true,
],
];
// Add cloud-init script if provided
if (! empty($cloudInitScript)) {
$params['user_data'] = $cloudInitScript;
}
expect($params)->not->toHaveKey('user_data');
});
it('validates cloud-init script with multiline content', function () {
$cloudInitScript = "#cloud-config\n\npackages:\n - nginx\n - git\n\nruncmd:\n - systemctl start nginx";
$params = [
'name' => 'test-server',
'server_type' => 'cx11',
'image' => 1,
'location' => 'nbg1',
'start_after_create' => true,
'ssh_keys' => [123],
'public_net' => [
'enable_ipv4' => true,
'enable_ipv6' => true,
],
];
// Add cloud-init script if provided
if (! empty($cloudInitScript)) {
$params['user_data'] = $cloudInitScript;
}
expect($params)
->toHaveKey('user_data')
->and($params['user_data'])->toContain('#cloud-config')
->and($params['user_data'])->toContain('packages:')
->and($params['user_data'])->toContain('runcmd:');
});