package provider_test import ( "regexp" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) const testMountProviderBlock = ` provider "dokploy" { host = "https://dokploy.invalid" api_key = "not-used-plan-only" } ` // A `volume` mount without a volume_name is the dangerous case: Dokploy's // generateVolumeMounts maps a null volumeName to `Source: ""`, which Docker // reads as an anonymous volume. Every deploy then gets a brand-new volume and // the previous one is orphaned, so the data silently does not survive. // // These validations run at plan time, so they need no Dokploy instance. func TestMountValidation(t *testing.T) { for name, tc := range map[string]struct { config string expectError *regexp.Regexp }{ "volume without volume_name": { config: testMountProviderBlock + ` resource "dokploy_mount" "test" { type = "volume" mount_path = "/data" service_type = "application" service_id = "app-123" }`, expectError: regexp.MustCompile(`volume_name`), }, "volume with empty volume_name": { config: testMountProviderBlock + ` resource "dokploy_mount" "test" { type = "volume" mount_path = "/data" volume_name = "" service_type = "application" service_id = "app-123" }`, expectError: regexp.MustCompile(`volume_name`), }, "bind without host_path": { config: testMountProviderBlock + ` resource "dokploy_mount" "test" { type = "bind" mount_path = "/data" service_type = "application" service_id = "app-123" }`, expectError: regexp.MustCompile(`host_path`), }, "file without file_path": { config: testMountProviderBlock + ` resource "dokploy_mount" "test" { type = "file" mount_path = "/etc/app" content = "hello" service_type = "application" service_id = "app-123" }`, expectError: regexp.MustCompile(`file_path`), }, "volume_name set on a bind mount": { config: testMountProviderBlock + ` resource "dokploy_mount" "test" { type = "bind" mount_path = "/data" host_path = "/srv/data" volume_name = "stray" service_type = "application" service_id = "app-123" }`, expectError: regexp.MustCompile(`volume_name`), }, } { t.Run(name, func(t *testing.T) { resource.UnitTest(t, resource.TestCase{ ProtoV6ProviderFactories: protoV6ProviderFactories, Steps: []resource.TestStep{{ Config: tc.config, PlanOnly: true, ExpectError: tc.expectError, }}, }) }) } }