From b08f919a22896be15f9fe55a5ea3bbab5961cd9a Mon Sep 17 00:00:00 2001 From: gatici Date: Wed, 7 Aug 2024 00:31:23 +0300 Subject: [PATCH] test(resources): Add tests for resourcekey validator refactor: StringIsResourceKeyValidator ValidateMap function is refactored to increase the readibility. Signed-off-by: gatici --- internal/provider/validator_resourcekey.go | 33 +++++------ .../provider/validator_resourcekey_test.go | 55 +++++++++++++++++++ 2 files changed, 72 insertions(+), 16 deletions(-) diff --git a/internal/provider/validator_resourcekey.go b/internal/provider/validator_resourcekey.go index 79db32a5..a4a9fee7 100644 --- a/internal/provider/validator_resourcekey.go +++ b/internal/provider/validator_resourcekey.go @@ -37,26 +37,27 @@ func (v StringIsResourceKeyValidator) ValidateMap(ctx context.Context, req valid return } for name, value := range resourceKey { - if isInt(value) { - _, err := strconv.Atoi(value) - if err != nil { - resp.Diagnostics.AddAttributeError( - req.Path, - "Invalid Resource revision", - fmt.Sprintf("value of %q should be a valid revision number or image URL: %s", name, err), - ) + providedRev, err := strconv.Atoi(value) + if err != nil { + imageUrlPattern := `(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]):[\w][\w.-]{0,127}` + urlRegex := regexp.MustCompile(imageUrlPattern) + if urlRegex.MatchString(value) { + continue } + resp.Diagnostics.AddAttributeError( + req.Path, + "Invalid resource value", + fmt.Sprintf("value of %q should be a valid revision number or image URL.", name), + ) continue } - imageUrlPattern := `(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]):[\w][\w.-]{0,127}` - urlRegex := regexp.MustCompile(imageUrlPattern) - if urlRegex.MatchString(value) { + if providedRev <= 0 { + resp.Diagnostics.AddAttributeError( + req.Path, + "Invalid resource value", + fmt.Sprintf("value of %q should be a valid revision number or image URL.", name), + ) continue } - resp.Diagnostics.AddAttributeError( - req.Path, - "Invalid image URL", - fmt.Sprintf("value of %q should be a valid revision number or image URL.", name), - ) } } diff --git a/internal/provider/validator_resourcekey_test.go b/internal/provider/validator_resourcekey_test.go index a9800b8d..102ef2ae 100644 --- a/internal/provider/validator_resourcekey_test.go +++ b/internal/provider/validator_resourcekey_test.go @@ -4,9 +4,64 @@ package provider_test import ( + "context" "testing" + + "github.com/hashicorp/terraform-plugin-framework/schema/validator" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/juju/terraform-provider-juju/internal/provider" ) func TestResourceKeyValidatorValid(t *testing.T) { + validResources := make(map[string]string) + validResources["image1"] = "image/tag:v1.0.0" + validResources["image2"] = "123.123.123.123:123/image/tag:v1.0.0" + validResources["image3"] = "your-domain.com/image/tag:v1.1.1-patch1" + validResources["image4"] = "your_domain/image/tag:patch1" + validResources["image5"] = "your.domain.com/image/tag:1" + validResources["image6"] = "27" + validResources["image7"] = "1" + ctx := context.Background() + + resourceValidator := provider.StringIsResourceKeyValidator{} + resourceValue, _ := types.MapValueFrom(ctx, types.StringType, validResources) + + req := validator.MapRequest{ + ConfigValue: resourceValue, + } + + var resp validator.MapResponse + resourceValidator.ValidateMap(context.Background(), req, &resp) + + if resp.Diagnostics.HasError() { + t.Errorf("errors %v", resp.Diagnostics.Errors()) + } +} + +func TestResourceKeyValidatorInvalidRevision(t *testing.T) { + validResources := make(map[string]string) + validResources["image1"] = "-10" + validResources["image2"] = "0" + validResources["image3"] = "10.5" + validResources["image4"] = "image/tag:" + validResources["image5"] = ":v1.0.0" + validResources["image6"] = "your-domain.com" + ctx := context.Background() + + resourceValidator := provider.StringIsResourceKeyValidator{} + resourceValue, _ := types.MapValueFrom(ctx, types.StringType, validResources) + + req := validator.MapRequest{ + ConfigValue: resourceValue, + } + var resp validator.MapResponse + resourceValidator.ValidateMap(context.Background(), req, &resp) + err := "Invalid resource value" + if c := resp.Diagnostics.ErrorsCount(); c != 6 { + t.Errorf("expected 6 errors, got %d", c) + } + if deets := resp.Diagnostics.Errors()[0].Summary(); err != deets { + t.Errorf("expected error %q, got %q", err, deets) + } }