mirror of
https://github.com/bpg/terraform-provider-proxmox.git
synced 2025-08-25 21:05:40 +00:00
better tests
Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
This commit is contained in:
parent
e5aca927e9
commit
92085d9dbc
@ -10,10 +10,13 @@ package test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/go-version"
|
||||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||||
|
"github.com/hashicorp/terraform-plugin-testing/terraform"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAccDatasourceVersion(t *testing.T) {
|
func TestAccDatasourceVersion(t *testing.T) {
|
||||||
@ -28,17 +31,74 @@ func TestAccDatasourceVersion(t *testing.T) {
|
|||||||
{
|
{
|
||||||
Config: `data "proxmox_virtual_environment_version" "test" {}`,
|
Config: `data "proxmox_virtual_environment_version" "test" {}`,
|
||||||
Check: resource.ComposeAggregateTestCheckFunc(
|
Check: resource.ComposeAggregateTestCheckFunc(
|
||||||
resource.TestCheckResourceAttr(datasourceName, "release", "8.4"),
|
|
||||||
resource.TestCheckResourceAttrSet(datasourceName, "repository_id"),
|
|
||||||
resource.TestCheckResourceAttrWith(datasourceName, "version", func(value string) error {
|
|
||||||
if strings.HasPrefix(value, "8.4") {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return fmt.Errorf("version %s does not start with 8.4", value)
|
|
||||||
}),
|
|
||||||
resource.TestCheckResourceAttrSet(datasourceName, "id"),
|
resource.TestCheckResourceAttrSet(datasourceName, "id"),
|
||||||
|
resource.TestCheckResourceAttrSet(datasourceName, "repository_id"),
|
||||||
|
resource.TestCheckResourceAttrWith(datasourceName, "release", validateReleaseVersion),
|
||||||
|
resource.TestCheckResourceAttrWith(datasourceName, "version", validateFullVersion),
|
||||||
|
validateVersionReleaseConsistency(datasourceName),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// validateReleaseVersion validates that the release field matches the expected pattern (e.g., "8.4", "9.0").
|
||||||
|
func validateReleaseVersion(value string) error {
|
||||||
|
releasePattern := regexp.MustCompile(`^[0-9]+\.[0-9]+$`)
|
||||||
|
if !releasePattern.MatchString(value) {
|
||||||
|
return fmt.Errorf("release %q does not match expected pattern (major.minor)", value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure it's at least the minimum supported version
|
||||||
|
releaseVer, err := version.NewVersion(value + ".0") // Add patch version for comparison
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to parse release version %q: %w", value, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
minVersion := version.Must(version.NewVersion("8.0.0"))
|
||||||
|
if releaseVer.LessThan(minVersion) {
|
||||||
|
return fmt.Errorf("release version %q is below minimum supported version 8.0", value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// validateFullVersion validates that the version field is a valid semantic version.
|
||||||
|
func validateFullVersion(value string) error {
|
||||||
|
if strings.TrimSpace(value) == "" {
|
||||||
|
return fmt.Errorf("version cannot be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := version.NewVersion(value)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("version %q is not a valid semantic version: %w", value, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// validateVersionReleaseConsistency returns a TestCheckFunc that validates version and release consistency.
|
||||||
|
func validateVersionReleaseConsistency(resourceName string) resource.TestCheckFunc {
|
||||||
|
return func(s *terraform.State) error {
|
||||||
|
rs, ok := s.RootModule().Resources[resourceName]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("resource %s not found", resourceName)
|
||||||
|
}
|
||||||
|
|
||||||
|
release := rs.Primary.Attributes["release"]
|
||||||
|
version := rs.Primary.Attributes["version"]
|
||||||
|
|
||||||
|
if release == "" {
|
||||||
|
return fmt.Errorf("release attribute is empty")
|
||||||
|
}
|
||||||
|
if version == "" {
|
||||||
|
return fmt.Errorf("version attribute is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.HasPrefix(version, release) {
|
||||||
|
return fmt.Errorf("version %q does not start with release %q", version, release)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user