2023-08-01 18:39:15 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-08-01 18:39:15 +00:00
|
|
|
|
|
|
|
package resourcetest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/internal/resource"
|
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ValidateAndNormalize(t *testing.T, registry resource.Registry, res *pbresource.Resource) {
|
2023-09-20 16:59:01 +00:00
|
|
|
t.Helper()
|
2023-08-01 18:39:15 +00:00
|
|
|
typ := res.Id.Type
|
|
|
|
|
|
|
|
typeInfo, ok := registry.Resolve(typ)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unhandled resource type: %q", resource.ToGVK(typ))
|
|
|
|
}
|
|
|
|
|
|
|
|
if typeInfo.Mutate != nil {
|
|
|
|
require.NoError(t, typeInfo.Mutate(res), "failed to apply type mutation to resource")
|
|
|
|
}
|
|
|
|
|
|
|
|
if typeInfo.Validate != nil {
|
|
|
|
require.NoError(t, typeInfo.Validate(res), "failed to validate resource")
|
|
|
|
}
|
|
|
|
}
|