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
|
|
|
|
2023-05-15 13:55:03 +00:00
|
|
|
package resourcetest
|
|
|
|
|
|
|
|
import (
|
2023-06-16 20:29:50 +00:00
|
|
|
"strings"
|
2023-05-15 13:55:03 +00:00
|
|
|
|
|
|
|
"github.com/oklog/ulid/v2"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-06-16 16:58:53 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2023-05-15 13:55:03 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
2023-08-01 18:39:15 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/internal/resource"
|
|
|
|
"github.com/hashicorp/consul/internal/storage"
|
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2023-05-15 13:55:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type resourceBuilder struct {
|
|
|
|
resource *pbresource.Resource
|
|
|
|
statuses map[string]*pbresource.Status
|
|
|
|
dontCleanup bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func Resource(rtype *pbresource.Type, name string) *resourceBuilder {
|
|
|
|
return &resourceBuilder{
|
|
|
|
resource: &pbresource.Resource{
|
|
|
|
Id: &pbresource.ID{
|
|
|
|
Type: &pbresource.Type{
|
|
|
|
Group: rtype.Group,
|
|
|
|
GroupVersion: rtype.GroupVersion,
|
|
|
|
Kind: rtype.Kind,
|
|
|
|
},
|
|
|
|
Name: name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-16 16:58:53 +00:00
|
|
|
func ResourceID(id *pbresource.ID) *resourceBuilder {
|
|
|
|
return &resourceBuilder{
|
|
|
|
resource: &pbresource.Resource{
|
|
|
|
Id: id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-08 19:22:14 +00:00
|
|
|
func (b *resourceBuilder) WithTenancy(tenant *pbresource.Tenancy) *resourceBuilder {
|
|
|
|
b.resource.Id.Tenancy = tenant
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2023-06-06 21:09:48 +00:00
|
|
|
func (b *resourceBuilder) WithData(t T, data protoreflect.ProtoMessage) *resourceBuilder {
|
|
|
|
t.Helper()
|
|
|
|
|
2023-05-15 13:55:03 +00:00
|
|
|
anyData, err := anypb.New(data)
|
|
|
|
require.NoError(t, err)
|
|
|
|
b.resource.Data = anyData
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2023-06-06 21:09:48 +00:00
|
|
|
func (b *resourceBuilder) WithMeta(key string, value string) *resourceBuilder {
|
|
|
|
if b.resource.Metadata == nil {
|
|
|
|
b.resource.Metadata = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.resource.Metadata[key] = value
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2023-05-15 13:55:03 +00:00
|
|
|
func (b *resourceBuilder) WithOwner(id *pbresource.ID) *resourceBuilder {
|
|
|
|
b.resource.Owner = id
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *resourceBuilder) WithStatus(key string, status *pbresource.Status) *resourceBuilder {
|
|
|
|
if b.statuses == nil {
|
|
|
|
b.statuses = make(map[string]*pbresource.Status)
|
|
|
|
}
|
|
|
|
b.statuses[key] = status
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *resourceBuilder) WithoutCleanup() *resourceBuilder {
|
|
|
|
b.dontCleanup = true
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *resourceBuilder) WithGeneration(gen string) *resourceBuilder {
|
|
|
|
b.resource.Generation = gen
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *resourceBuilder) Build() *pbresource.Resource {
|
|
|
|
// clone the resource so we can add on status information
|
|
|
|
res := proto.Clone(b.resource).(*pbresource.Resource)
|
|
|
|
|
|
|
|
// fill in the generation if empty to make it look like
|
|
|
|
// a real managed resource
|
|
|
|
if res.Generation == "" {
|
|
|
|
res.Generation = ulid.Make().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now create the status map
|
|
|
|
res.Status = make(map[string]*pbresource.Status)
|
|
|
|
for key, original := range b.statuses {
|
|
|
|
status := &pbresource.Status{
|
|
|
|
ObservedGeneration: res.Generation,
|
|
|
|
Conditions: original.Conditions,
|
|
|
|
}
|
|
|
|
res.Status[key] = status
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2023-06-06 21:09:48 +00:00
|
|
|
func (b *resourceBuilder) ID() *pbresource.ID {
|
|
|
|
return b.resource.Id
|
|
|
|
}
|
|
|
|
|
2023-08-01 18:39:15 +00:00
|
|
|
func (b *resourceBuilder) Reference(section string) *pbresource.Reference {
|
|
|
|
return resource.Reference(b.ID(), section)
|
|
|
|
}
|
|
|
|
|
2023-09-07 15:37:15 +00:00
|
|
|
func (b *resourceBuilder) ReferenceNoSection() *pbresource.Reference {
|
|
|
|
return resource.Reference(b.ID(), "")
|
|
|
|
}
|
|
|
|
|
2023-06-06 21:09:48 +00:00
|
|
|
func (b *resourceBuilder) Write(t T, client pbresource.ResourceServiceClient) *pbresource.Resource {
|
|
|
|
t.Helper()
|
|
|
|
|
2023-06-16 20:29:50 +00:00
|
|
|
ctx := testutil.TestContext(t)
|
|
|
|
|
2023-05-15 13:55:03 +00:00
|
|
|
res := b.resource
|
|
|
|
|
2023-06-16 20:29:50 +00:00
|
|
|
var rsp *pbresource.WriteResponse
|
|
|
|
var err error
|
2023-05-15 13:55:03 +00:00
|
|
|
|
2023-06-16 20:29:50 +00:00
|
|
|
// Retry any writes where the error is a UID mismatch and the UID was not specified. This is indicative
|
|
|
|
// of using a follower to rewrite an object who is not perfectly in-sync with the leader.
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
rsp, err = client.Write(ctx, &pbresource.WriteRequest{
|
|
|
|
Resource: res,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil || res.Id.Uid != "" || status.Code(err) != codes.FailedPrecondition {
|
2023-08-01 18:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Logf("write saw error: %v", err)
|
|
|
|
}
|
2023-06-16 20:29:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(err.Error(), storage.ErrWrongUid.Error()) {
|
|
|
|
r.Fatalf("resource write failed due to uid mismatch - most likely a transient issue when talking to a non-leader")
|
|
|
|
} else {
|
|
|
|
// other errors are unexpected and should cause an immediate failure
|
|
|
|
r.Stop(err)
|
|
|
|
}
|
|
|
|
})
|
2023-05-15 13:55:03 +00:00
|
|
|
|
2023-09-20 16:59:01 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, rsp)
|
|
|
|
|
2023-05-15 13:55:03 +00:00
|
|
|
if !b.dontCleanup {
|
2023-06-16 20:29:50 +00:00
|
|
|
id := proto.Clone(rsp.Resource.Id).(*pbresource.ID)
|
|
|
|
id.Uid = ""
|
|
|
|
t.Cleanup(func() {
|
|
|
|
NewClient(client).MustDelete(t, id)
|
2023-05-15 13:55:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(b.statuses) == 0 {
|
|
|
|
return rsp.Resource
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, original := range b.statuses {
|
|
|
|
status := &pbresource.Status{
|
|
|
|
ObservedGeneration: rsp.Resource.Generation,
|
|
|
|
Conditions: original.Conditions,
|
|
|
|
}
|
2023-06-16 20:29:50 +00:00
|
|
|
_, err := client.WriteStatus(ctx, &pbresource.WriteStatusRequest{
|
2023-05-15 13:55:03 +00:00
|
|
|
Id: rsp.Resource.Id,
|
|
|
|
Key: key,
|
|
|
|
Status: status,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2023-06-16 20:29:50 +00:00
|
|
|
readResp, err := client.Read(ctx, &pbresource.ReadRequest{
|
2023-05-15 13:55:03 +00:00
|
|
|
Id: rsp.Resource.Id,
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return readResp.Resource
|
|
|
|
}
|