Skip to content

Commit

Permalink
test(resources): Add tests for resourcekey validator
Browse files Browse the repository at this point in the history
refactor: StringIsResourceKeyValidator ValidateMap function is refactored to increase the readibility.

Signed-off-by: gatici <[email protected]>
  • Loading branch information
gatici committed Aug 6, 2024
1 parent 54c6faf commit b08f919
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 16 deletions.
33 changes: 17 additions & 16 deletions internal/provider/validator_resourcekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
}
}
55 changes: 55 additions & 0 deletions internal/provider/validator_resourcekey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit b08f919

Please sign in to comment.