You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
consul/internal/resource/decode_test.go

114 lines
2.9 KiB

// Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
1 year ago
// SPDX-License-Identifier: BUSL-1.1
package resource_test
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/anypb"
svctest "github.com/hashicorp/consul/agent/grpc-external/services/resource/testing"
"github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/internal/resource/demo"
rtest "github.com/hashicorp/consul/internal/resource/resourcetest"
"github.com/hashicorp/consul/proto-public/pbresource"
pbdemo "github.com/hashicorp/consul/proto/private/pbdemo/v2"
"github.com/hashicorp/consul/proto/private/prototest"
"github.com/hashicorp/consul/sdk/testutil"
)
func TestGetDecodedResource(t *testing.T) {
var (
baseClient = svctest.RunResourceService(t, demo.RegisterTypes)
client = rtest.NewClient(baseClient)
ctx = testutil.TestContext(t)
)
babypantsID := &pbresource.ID{
Type: demo.TypeV2Artist,
Tenancy: demo.TenancyDefault,
Name: "babypants",
}
testutil.RunStep(t, "not found", func(t *testing.T) {
got, err := resource.GetDecodedResource[*pbdemo.Artist](ctx, client, babypantsID)
require.NoError(t, err)
require.Nil(t, got)
})
testutil.RunStep(t, "found", func(t *testing.T) {
data := &pbdemo.Artist{
Name: "caspar babypants",
}
res := rtest.Resource(demo.TypeV2Artist, "babypants").
WithData(t, data).
Write(t, client)
got, err := resource.GetDecodedResource[*pbdemo.Artist](ctx, client, babypantsID)
require.NoError(t, err)
require.NotNil(t, got)
// Clone generated fields over.
res.Id.Uid = got.Resource.Id.Uid
res.Version = got.Resource.Version
res.Generation = got.Resource.Generation
// Clone defaulted fields over
data.Genre = pbdemo.Genre_GENRE_DISCO
prototest.AssertDeepEqual(t, res, got.Resource)
prototest.AssertDeepEqual(t, data, got.Data)
})
}
func TestDecode(t *testing.T) {
t.Run("good", func(t *testing.T) {
fooData := &pbdemo.Artist{
Name: "caspar babypants",
}
any, err := anypb.New(fooData)
require.NoError(t, err)
foo := &pbresource.Resource{
Id: &pbresource.ID{
Type: demo.TypeV2Artist,
Tenancy: demo.TenancyDefault,
Name: "babypants",
},
Data: any,
Metadata: map[string]string{
"generated_at": time.Now().Format(time.RFC3339),
},
}
dec, err := resource.Decode[*pbdemo.Artist](foo)
require.NoError(t, err)
prototest.AssertDeepEqual(t, foo, dec.Resource)
prototest.AssertDeepEqual(t, fooData, dec.Data)
})
t.Run("bad", func(t *testing.T) {
foo := &pbresource.Resource{
Id: &pbresource.ID{
Type: demo.TypeV2Artist,
Tenancy: demo.TenancyDefault,
Name: "babypants",
},
Data: &anypb.Any{
TypeUrl: "garbage",
Value: []byte("more garbage"),
},
Metadata: map[string]string{
"generated_at": time.Now().Format(time.RFC3339),
},
}
_, err := resource.Decode[*pbdemo.Artist](foo)
require.Error(t, err)
})
}