2023-12-13 15:06:39 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
|
|
package indexers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2024-09-05 14:50:46 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
"github.com/hashicorp/consul/internal/controller/cache/indexers/indexersmock"
|
|
|
|
"github.com/hashicorp/consul/internal/resource/resourcetest"
|
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
pbdemo "github.com/hashicorp/consul/proto/private/pbdemo/v1"
|
2024-09-05 14:50:46 +00:00
|
|
|
pbdemov2 "github.com/hashicorp/consul/proto/private/pbdemo/v2"
|
2023-12-13 15:06:39 +00:00
|
|
|
"github.com/hashicorp/consul/proto/private/prototest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRefOrIDIndex(t *testing.T) {
|
|
|
|
ref1 := &pbresource.Reference{
|
|
|
|
Type: pbdemo.AlbumType,
|
|
|
|
Tenancy: &pbresource.Tenancy{
|
|
|
|
Partition: "default",
|
|
|
|
Namespace: "default",
|
|
|
|
},
|
|
|
|
Name: "foo",
|
|
|
|
}
|
|
|
|
|
|
|
|
ref2 := &pbresource.Reference{
|
|
|
|
Type: pbdemo.AlbumType,
|
|
|
|
Tenancy: &pbresource.Tenancy{
|
|
|
|
Partition: "default",
|
|
|
|
Namespace: "default",
|
|
|
|
},
|
|
|
|
Name: "bar",
|
|
|
|
}
|
|
|
|
|
|
|
|
r1 := resourcetest.Resource(pbdemo.AlbumType, "foo").
|
|
|
|
WithData(t, &pbdemo.Album{Name: "foo"}).
|
|
|
|
Build()
|
|
|
|
|
|
|
|
dec := resourcetest.MustDecode[*pbdemo.Album](t, r1)
|
|
|
|
|
|
|
|
refs := indexersmock.NewRefOrIDFetcher[*pbdemo.Album, *pbresource.Reference](t)
|
|
|
|
|
2024-02-09 18:00:21 +00:00
|
|
|
idx := RefOrIDIndex("test", refs.Execute).IndexedData()
|
2023-12-13 15:06:39 +00:00
|
|
|
|
|
|
|
refs.EXPECT().Execute(dec).
|
|
|
|
Return([]*pbresource.Reference{ref1, ref2}).
|
|
|
|
Once()
|
|
|
|
|
|
|
|
txn := idx.Txn()
|
|
|
|
require.NoError(t, txn.Insert(r1))
|
|
|
|
txn.Commit()
|
|
|
|
|
|
|
|
out, err := idx.Txn().Get(ref1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
prototest.AssertDeepEqual(t, r1, out)
|
|
|
|
|
|
|
|
out, err = idx.Txn().Get(ref2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
prototest.AssertDeepEqual(t, r1, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBoundRefsIndex(t *testing.T) {
|
|
|
|
ref1 := &pbresource.Reference{
|
2024-09-05 14:50:46 +00:00
|
|
|
Type: pbdemov2.FestivalType,
|
2023-12-13 15:06:39 +00:00
|
|
|
Tenancy: &pbresource.Tenancy{
|
|
|
|
Partition: "default",
|
|
|
|
Namespace: "default",
|
|
|
|
},
|
|
|
|
Name: "api",
|
|
|
|
}
|
|
|
|
|
|
|
|
ref2 := &pbresource.Reference{
|
2024-09-05 14:50:46 +00:00
|
|
|
Type: pbdemov2.FestivalType,
|
2023-12-13 15:06:39 +00:00
|
|
|
Tenancy: &pbresource.Tenancy{
|
|
|
|
Partition: "default",
|
|
|
|
Namespace: "default",
|
|
|
|
},
|
|
|
|
Name: "api-2",
|
|
|
|
}
|
|
|
|
|
2024-09-05 14:50:46 +00:00
|
|
|
r1 := resourcetest.Resource(pbdemov2.FestivalType, "api").
|
|
|
|
WithData(t, &pbdemov2.Festival{
|
2023-12-13 15:06:39 +00:00
|
|
|
BoundReferences: []*pbresource.Reference{
|
|
|
|
ref1,
|
|
|
|
ref2,
|
|
|
|
},
|
|
|
|
}).
|
|
|
|
Build()
|
|
|
|
|
2024-09-05 14:50:46 +00:00
|
|
|
idx := BoundRefsIndex[*pbdemov2.Festival]("test").IndexedData()
|
2023-12-13 15:06:39 +00:00
|
|
|
|
|
|
|
txn := idx.Txn()
|
|
|
|
require.NoError(t, txn.Insert(r1))
|
|
|
|
txn.Commit()
|
|
|
|
|
|
|
|
out, err := idx.Txn().Get(ref1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
prototest.AssertDeepEqual(t, r1, out)
|
|
|
|
|
|
|
|
out, err = idx.Txn().Get(ref2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
prototest.AssertDeepEqual(t, r1, out)
|
|
|
|
}
|