R.B. Boyer 13ce787a3f
resource: adding various helpers for working with resources (#18342)
This is a bit of a grab bag of helpers that I found useful for working with them when authoring substantial Controllers. Subsequent PRs will make use of them.
2023-08-01 13:39:15 -05:00

31 lines
726 B
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
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) {
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")
}
}