Skip to content

Commit 1258275

Browse files
authored
Merge pull request #542 from nix-community/digital-ocean
Add integration test for Digital ocean
2 parents b768630 + 21c4562 commit 1258275

File tree

7 files changed

+218
-15
lines changed

7 files changed

+218
-15
lines changed

.gitignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ result*
44

55
# terraform
66
.terraform.lock.hcl
7-
terraform/tests/hcloud/modules/ssh-key/test_key
8-
terraform/tests/hcloud/modules/ssh-key/test_key.pub
9-
terraform/tests/hcloud/test_key
10-
terraform/tests/hcloud/test_key.pub
7+
test_key
8+
test_key.pub
119
errored_test.tfstate
1210
errored_test.tfstate.backup
1311
terraform.tfstate

terraform/flake-module.nix

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525
buildInputs = with pkgs; [
2626
terraform-docs
2727
(opentofu.withPlugins (p: [
28-
p.tls
28+
p.digitalocean
29+
p.external
2930
p.hcloud
3031
p.local
31-
p.external
3232
p.null
33+
p.tls
3334
]))
3435
];
3536

terraform/tests/README.md

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@ OpenTofu's built-in testing framework.
77

88
```
99
terraform/tests/
10-
├── main.tf # Core configuration for validation tests
11-
├── nixos-anywhere.tftest.hcl # Basic validation tests (no external deps)
10+
├── main.tf # Core configuration for validation tests
11+
├── nixos-anywhere.tftest.hcl # Basic validation tests (no external deps)
1212
├── nix-build-special-args.tftest.hcl # Nix build module tests with special_args
13-
├── hcloud-deployment.tftest.hcl # Hetzner Cloud integration tests
14-
├── hcloud/ # Hetzner Cloud deployment configuration
13+
├── hcloud-deployment.tftest.hcl # Hetzner Cloud integration tests
14+
├── digitalocean-deployment.tftest.hcl # DigitalOcean integration tests
15+
├── hcloud/ # Hetzner Cloud deployment configuration
16+
│ ├── main.tf
17+
├── digitalocean/ # DigitalOcean deployment configuration
1518
├── main.tf
1619
```
1720

18-
### For Hetzner Cloud Tests (Optional)
21+
### For Cloud Provider Tests (Optional)
1922

2023
- Hetzner Cloud account with API token
24+
- DigitalOcean account with API token
2125

2226
## Running Tests
2327

@@ -39,7 +43,9 @@ tofu test -filter=nixos-anywhere.tftest.hcl
3943
**Current Status:** Tests available for nixos-anywhere module and nix-build
4044
module
4145

42-
### Hetzner Cloud Integration Tests
46+
### Cloud Provider Integration Tests
47+
48+
#### Hetzner Cloud
4349

4450
```bash
4551
# Set your Hetzner Cloud token
@@ -49,6 +55,16 @@ export TF_VAR_hcloud_token="your-64-character-hcloud-token"
4955
tofu test -filter hcloud-deployment.tftest.hcl
5056
```
5157

58+
#### DigitalOcean
59+
60+
```bash
61+
# Set your DigitalOcean token
62+
export TF_VAR_digitalocean_token="your-digitalocean-api-token"
63+
64+
# Run DigitalOcean tests
65+
tofu test -filter digitalocean-deployment.tftest.hcl
66+
```
67+
5268
**Note:** These tests will fail if no valid token is provided.
5369

5470
## Test Categories
@@ -74,12 +90,20 @@ tofu test -filter hcloud-deployment.tftest.hcl
7490
- **Duration:** 5 minutes (apply tests)
7591
- **Cost:** ~€ 0.006 per hour
7692

93+
### 4. DigitalOcean Tests (`digitalocean-deployment.tftest.hcl`)
94+
95+
- **Purpose:** Test complete deployment workflow
96+
- **Dependencies:** Valid DigitalOcean API token
97+
- **Duration:** 5 minutes (apply tests)
98+
- **Cost:** ~$0,02679 per hour
99+
77100
## Usage Examples
78101

79102
### Environment Variables
80103

81104
```bash
82-
export TF_VAR_hcloud_token="your-token"
105+
export TF_VAR_hcloud_token="your-hcloud-token"
106+
export TF_VAR_digitalocean_token="your-digitalocean-token"
83107
export TF_VAR_test_name_prefix="my-test"
84108
```
85109

@@ -88,14 +112,17 @@ export TF_VAR_test_name_prefix="my-test"
88112
Create `terraform.tfvars`:
89113

90114
```hcl
91-
hcloud_token = "your-token-here"
115+
hcloud_token = "your-hcloud-token-here"
116+
digitalocean_token = "your-digitalocean-token-here"
92117
test_name_prefix = "tftest-nixos-anywhere"
93118
```
94119

95120
## Cleanup
96121

97122
OpenTofu test automatically cleans up resources. Manual cleanup if needed:
98123

124+
### Hetzner Cloud
125+
99126
```bash
100127
# List test resources
101128
hcloud server list | grep tftest-nixos-anywhere
@@ -106,6 +133,18 @@ hcloud server delete <server-id>
106133
hcloud ssh-key delete <key-id>
107134
```
108135

136+
### DigitalOcean
137+
138+
```bash
139+
# List test resources
140+
doctl compute droplet list | grep tftest-nixos-anywhere
141+
doctl compute ssh-key list | grep tftest-nixos-anywhere
142+
143+
# Force cleanup
144+
doctl compute droplet delete <droplet-id>
145+
doctl compute ssh-key delete <key-id>
146+
```
147+
109148
## Development
110149

111150
### Test Best Practices
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Terraform Test Configuration for nixos-anywhere DigitalOcean deployment
2+
# Run with: tofu test -var="digitalocean_token=your-token-here"
3+
# These tests require a valid DigitalOcean API token
4+
5+
variables {
6+
test_name_prefix = "tftest-nixos-anywhere"
7+
}
8+
9+
run "test_digitalocean_deployment_apply" {
10+
command = apply
11+
12+
module {
13+
source = "./digitalocean"
14+
}
15+
16+
variables {
17+
nixos_system_attr = "github:nix-community/nixos-anywhere-examples#nixosConfigurations.digitalocean.config.system.build.toplevel"
18+
nixos_partitioner_attr = "github:nix-community/nixos-anywhere-examples#nixosConfigurations.digitalocean.config.system.build.diskoNoDeps"
19+
debug_logging = true
20+
}
21+
22+
assert {
23+
condition = output.nixos_anywhere_result != null
24+
error_message = "nixos-anywhere deployment should produce a result"
25+
}
26+
}

terraform/tests/digitalocean/main.tf

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
terraform {
2+
required_providers {
3+
digitalocean = {
4+
source = "digitalocean/digitalocean"
5+
version = "~> 2.34"
6+
}
7+
tls = {
8+
source = "hashicorp/tls"
9+
version = "~> 4.0"
10+
}
11+
local = {
12+
source = "hashicorp/local"
13+
version = "~> 2.4"
14+
}
15+
}
16+
}
17+
18+
provider "digitalocean" {
19+
token = var.digitalocean_token
20+
}
21+
22+
variable "digitalocean_token" {
23+
description = "DigitalOcean API token"
24+
type = string
25+
sensitive = true
26+
}
27+
28+
variable "test_name_prefix" {
29+
description = "Prefix for test resource names"
30+
type = string
31+
default = "tftest-nixos-anywhere"
32+
}
33+
34+
variable "nixos_system_attr" {
35+
description = "NixOS system attribute to deploy"
36+
type = string
37+
}
38+
39+
variable "nixos_partitioner_attr" {
40+
description = "NixOS partitioner attribute"
41+
type = string
42+
}
43+
44+
variable "debug_logging" {
45+
description = "Enable debug logging"
46+
type = bool
47+
default = false
48+
}
49+
50+
# Generate SSH key pair
51+
resource "tls_private_key" "test_key" {
52+
algorithm = "ED25519"
53+
}
54+
55+
# Save private key to file
56+
resource "local_file" "private_key" {
57+
content = tls_private_key.test_key.private_key_openssh
58+
filename = "${path.root}/test_key"
59+
file_permission = "0600"
60+
}
61+
62+
# Save public key to file
63+
resource "local_file" "public_key" {
64+
content = tls_private_key.test_key.public_key_openssh
65+
filename = "${path.root}/test_key.pub"
66+
}
67+
68+
# Create DigitalOcean SSH key
69+
resource "digitalocean_ssh_key" "test_key" {
70+
name = "${var.test_name_prefix}-deployment-key"
71+
public_key = tls_private_key.test_key.public_key_openssh
72+
}
73+
74+
# Create test droplet
75+
# Note: Using s-2vcpu-2gb (minimum 2GB RAM required for nixos-anywhere kexec)
76+
# DigitalOcean uses /dev/vda for disk devices (handled by digitalocean config)
77+
resource "digitalocean_droplet" "test_server" {
78+
name = "${var.test_name_prefix}-server"
79+
image = "ubuntu-22-04-x64"
80+
size = "s-2vcpu-2gb"
81+
region = "nyc3"
82+
ssh_keys = [digitalocean_ssh_key.test_key.id]
83+
84+
tags = [
85+
"nixos-anywhere-test",
86+
replace(replace(replace(timestamp(), ":", "-"), "T", "-"), "Z", "")
87+
]
88+
}
89+
90+
# nixos-anywhere all-in-one module
91+
# Uses digitalocean configuration from nixos-anywhere-examples which:
92+
# - Sets disk device to /dev/vda (DigitalOcean standard)
93+
# - Configures cloud-init for network setup
94+
# - Disables DHCP in favor of cloud-init provisioning
95+
module "nixos_anywhere" {
96+
source = "../../all-in-one"
97+
98+
nixos_system_attr = var.nixos_system_attr
99+
nixos_partitioner_attr = var.nixos_partitioner_attr
100+
target_host = digitalocean_droplet.test_server.ipv4_address
101+
target_port = 22
102+
target_user = "root"
103+
debug_logging = var.debug_logging
104+
deployment_ssh_key = tls_private_key.test_key.private_key_openssh
105+
install_ssh_key = tls_private_key.test_key.private_key_openssh
106+
107+
special_args = {
108+
extraPublicKeys = [tls_private_key.test_key.public_key_openssh]
109+
}
110+
}
111+
112+
output "nixos_anywhere_result" {
113+
description = "nixos-anywhere module result"
114+
value = module.nixos_anywhere.result
115+
}
116+
117+
output "droplet_ip" {
118+
description = "DigitalOcean droplet public IP address"
119+
value = digitalocean_droplet.test_server.ipv4_address
120+
}
121+
122+
output "droplet_id" {
123+
description = "DigitalOcean droplet ID for cleanup"
124+
value = digitalocean_droplet.test_server.id
125+
}
126+
127+
output "ssh_key_id" {
128+
description = "DigitalOcean SSH key ID for cleanup"
129+
value = digitalocean_ssh_key.test_key.id
130+
}
131+
132+
output "ssh_connection_command" {
133+
description = "SSH command to connect to the deployed server"
134+
value = "ssh -i ${local_file.private_key.filename} root@${digitalocean_droplet.test_server.ipv4_address}"
135+
}

terraform/tests/hcloud/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ resource "hcloud_server" "test_server" {
7777
name = "${var.test_name_prefix}-server"
7878
image = "ubuntu-22.04"
7979
server_type = "cx22"
80-
location = "nbg1"
80+
location = "hel1"
8181
ssh_keys = [hcloud_ssh_key.test_key.id]
8282

8383
labels = {

terraform/tests/terraform.tfvars.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
# Get this from https://console.hetzner.cloud/
66
hcloud_token = "your-hcloud-token-here"
77

8+
# Required: DigitalOcean API token
9+
# Get this from https://cloud.digitalocean.com/account/api/tokens
10+
digitalocean_token = "your-digitalocean-token-here"
11+
812
# Optional: Prefix for test resource names
913
# This helps identify and clean up test resources
1014
test_name_prefix = "tftest-nixos-anywhere"

0 commit comments

Comments
 (0)