2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 22:48:58 +00:00
|
|
|
|
2023-12-05 19:00:06 +00:00
|
|
|
package resource_test
|
2023-03-27 19:37:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io"
|
2023-10-16 17:55:30 +00:00
|
|
|
"strings"
|
2023-03-27 19:37:54 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-12-05 19:00:06 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
2023-04-11 11:10:14 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2024-02-02 21:11:05 +00:00
|
|
|
"github.com/hashicorp/consul/acl/resolver"
|
2023-12-05 19:00:06 +00:00
|
|
|
svc "github.com/hashicorp/consul/agent/grpc-external/services/resource"
|
|
|
|
svctest "github.com/hashicorp/consul/agent/grpc-external/services/resource/testing"
|
2023-04-11 11:10:14 +00:00
|
|
|
"github.com/hashicorp/consul/agent/grpc-external/testutils"
|
2023-08-21 20:02:23 +00:00
|
|
|
"github.com/hashicorp/consul/internal/resource"
|
2023-04-06 09:40:04 +00:00
|
|
|
"github.com/hashicorp/consul/internal/resource/demo"
|
2023-03-27 19:37:54 +00:00
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
"github.com/hashicorp/consul/proto/private/prototest"
|
|
|
|
)
|
|
|
|
|
2023-12-05 19:00:06 +00:00
|
|
|
// TODO: Update all tests to use true/false table test for v2tenancy
|
|
|
|
|
2023-05-10 09:38:48 +00:00
|
|
|
func TestWatchList_InputValidation(t *testing.T) {
|
2023-12-05 19:00:06 +00:00
|
|
|
client := svctest.NewResourceServiceBuilder().
|
|
|
|
WithRegisterFns(demo.RegisterTypes).
|
|
|
|
Run(t)
|
2023-05-10 09:38:48 +00:00
|
|
|
|
2023-10-16 17:55:30 +00:00
|
|
|
type testCase struct {
|
|
|
|
modFn func(*pbresource.WatchListRequest)
|
|
|
|
errContains string
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]testCase{
|
|
|
|
"no type": {
|
|
|
|
modFn: func(req *pbresource.WatchListRequest) { req.Type = nil },
|
|
|
|
errContains: "type is required",
|
|
|
|
},
|
|
|
|
"partition mixed case": {
|
|
|
|
modFn: func(req *pbresource.WatchListRequest) { req.Tenancy.Partition = "Default" },
|
|
|
|
errContains: "tenancy.partition invalid",
|
|
|
|
},
|
|
|
|
"partition too long": {
|
|
|
|
modFn: func(req *pbresource.WatchListRequest) {
|
|
|
|
req.Tenancy.Partition = strings.Repeat("p", resource.MaxNameLength+1)
|
|
|
|
},
|
|
|
|
errContains: "tenancy.partition invalid",
|
|
|
|
},
|
|
|
|
"namespace mixed case": {
|
|
|
|
modFn: func(req *pbresource.WatchListRequest) { req.Tenancy.Namespace = "Default" },
|
|
|
|
errContains: "tenancy.namespace invalid",
|
|
|
|
},
|
|
|
|
"namespace too long": {
|
|
|
|
modFn: func(req *pbresource.WatchListRequest) {
|
|
|
|
req.Tenancy.Namespace = strings.Repeat("n", resource.MaxNameLength+1)
|
|
|
|
},
|
|
|
|
errContains: "tenancy.namespace invalid",
|
|
|
|
},
|
|
|
|
"name_prefix mixed case": {
|
|
|
|
modFn: func(req *pbresource.WatchListRequest) { req.NamePrefix = "Smashing" },
|
|
|
|
errContains: "name_prefix invalid",
|
|
|
|
},
|
|
|
|
"partitioned type provides non-empty namespace": {
|
|
|
|
modFn: func(req *pbresource.WatchListRequest) {
|
|
|
|
req.Type = demo.TypeV1RecordLabel
|
|
|
|
req.Tenancy.Namespace = "bad"
|
|
|
|
},
|
|
|
|
errContains: "cannot have a namespace",
|
2023-08-21 20:02:23 +00:00
|
|
|
},
|
2023-11-03 20:03:07 +00:00
|
|
|
"cluster scope with non-empty partition": {
|
|
|
|
modFn: func(req *pbresource.WatchListRequest) {
|
|
|
|
req.Type = demo.TypeV1Executive
|
|
|
|
req.Tenancy = &pbresource.Tenancy{Partition: "bad"}
|
|
|
|
},
|
|
|
|
errContains: "cannot have a partition",
|
|
|
|
},
|
|
|
|
"cluster scope with non-empty namespace": {
|
|
|
|
modFn: func(req *pbresource.WatchListRequest) {
|
|
|
|
req.Type = demo.TypeV1Executive
|
|
|
|
req.Tenancy = &pbresource.Tenancy{Namespace: "bad"}
|
|
|
|
},
|
|
|
|
errContains: "cannot have a namespace",
|
|
|
|
},
|
2023-05-10 09:38:48 +00:00
|
|
|
}
|
2023-10-16 17:55:30 +00:00
|
|
|
for desc, tc := range testCases {
|
2023-05-10 09:38:48 +00:00
|
|
|
t.Run(desc, func(t *testing.T) {
|
|
|
|
req := &pbresource.WatchListRequest{
|
|
|
|
Type: demo.TypeV2Album,
|
2023-08-21 20:02:23 +00:00
|
|
|
Tenancy: resource.DefaultNamespacedTenancy(),
|
2023-05-10 09:38:48 +00:00
|
|
|
}
|
2023-10-16 17:55:30 +00:00
|
|
|
tc.modFn(req)
|
2023-05-10 09:38:48 +00:00
|
|
|
|
|
|
|
stream, err := client.WatchList(testContext(t), req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = stream.Recv()
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, codes.InvalidArgument.String(), status.Code(err).String())
|
2023-10-16 17:55:30 +00:00
|
|
|
require.ErrorContains(t, err, tc.errContains)
|
2023-05-10 09:38:48 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-27 19:37:54 +00:00
|
|
|
func TestWatchList_TypeNotFound(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-04-11 11:10:14 +00:00
|
|
|
|
2023-12-05 19:00:06 +00:00
|
|
|
client := svctest.NewResourceServiceBuilder().Run(t)
|
2023-03-27 19:37:54 +00:00
|
|
|
|
|
|
|
stream, err := client.WatchList(context.Background(), &pbresource.WatchListRequest{
|
2023-04-06 09:40:04 +00:00
|
|
|
Type: demo.TypeV2Artist,
|
2023-08-21 20:02:23 +00:00
|
|
|
Tenancy: resource.DefaultNamespacedTenancy(),
|
2023-03-27 19:37:54 +00:00
|
|
|
NamePrefix: "",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
rspCh := handleResourceStream(t, stream)
|
|
|
|
|
|
|
|
err = mustGetError(t, rspCh)
|
|
|
|
require.Equal(t, codes.InvalidArgument.String(), status.Code(err).String())
|
2023-06-26 12:25:14 +00:00
|
|
|
require.Contains(t, err.Error(), "resource type demo.v2.Artist not registered")
|
2023-03-27 19:37:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWatchList_GroupVersionMatches(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-04-11 11:10:14 +00:00
|
|
|
|
2024-02-02 21:11:05 +00:00
|
|
|
b := svctest.NewResourceServiceBuilder().
|
|
|
|
WithRegisterFns(demo.RegisterTypes)
|
|
|
|
client := b.Run(t)
|
|
|
|
|
2023-03-27 19:37:54 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// create a watch
|
|
|
|
stream, err := client.WatchList(ctx, &pbresource.WatchListRequest{
|
2023-04-06 09:40:04 +00:00
|
|
|
Type: demo.TypeV2Artist,
|
2023-08-21 20:02:23 +00:00
|
|
|
Tenancy: resource.DefaultNamespacedTenancy(),
|
2023-03-27 19:37:54 +00:00
|
|
|
NamePrefix: "",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
rspCh := handleResourceStream(t, stream)
|
|
|
|
|
2024-02-02 21:11:05 +00:00
|
|
|
mustGetEndOfSnapshot(t, rspCh)
|
|
|
|
|
2023-04-06 09:40:04 +00:00
|
|
|
artist, err := demo.GenerateV2Artist()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-27 19:37:54 +00:00
|
|
|
// insert and verify upsert event received
|
2024-02-02 21:11:05 +00:00
|
|
|
r1Resp, err := client.Write(ctx, &pbresource.WriteRequest{Resource: artist})
|
|
|
|
r1 := r1Resp.Resource
|
2023-03-27 19:37:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
rsp := mustGetResource(t, rspCh)
|
2024-02-02 21:11:05 +00:00
|
|
|
require.NotNil(t, rsp.GetUpsert())
|
|
|
|
prototest.AssertDeepEqual(t, r1, rsp.GetUpsert().Resource)
|
2023-03-27 19:37:54 +00:00
|
|
|
|
|
|
|
// update and verify upsert event received
|
2023-04-06 09:40:04 +00:00
|
|
|
r2 := modifyArtist(t, r1)
|
2024-02-02 21:11:05 +00:00
|
|
|
r2Resp, err := client.Write(ctx, &pbresource.WriteRequest{Resource: r2})
|
2023-03-27 19:37:54 +00:00
|
|
|
require.NoError(t, err)
|
2024-02-02 21:11:05 +00:00
|
|
|
r2 = r2Resp.Resource
|
2023-03-27 19:37:54 +00:00
|
|
|
rsp = mustGetResource(t, rspCh)
|
2024-02-02 21:11:05 +00:00
|
|
|
require.NotNil(t, rsp.GetUpsert())
|
|
|
|
prototest.AssertDeepEqual(t, r2, rsp.GetUpsert().Resource)
|
2023-03-27 19:37:54 +00:00
|
|
|
|
|
|
|
// delete and verify delete event received
|
2024-02-02 21:11:05 +00:00
|
|
|
_, err = client.Delete(ctx, &pbresource.DeleteRequest{Id: r2.Id, Version: r2.Version})
|
2023-03-27 19:37:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
rsp = mustGetResource(t, rspCh)
|
2024-02-02 21:11:05 +00:00
|
|
|
require.NotNil(t, rsp.GetDelete())
|
|
|
|
prototest.AssertDeepEqual(t, r2.Id, rsp.GetDelete().Resource.Id)
|
2023-03-27 19:37:54 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 20:02:23 +00:00
|
|
|
func TestWatchList_Tenancy_Defaults_And_Normalization(t *testing.T) {
|
|
|
|
// Test units of tenancy get lowercased and defaulted correctly when empty.
|
|
|
|
for desc, tc := range wildcardTenancyCases() {
|
|
|
|
t.Run(desc, func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2023-12-05 19:00:06 +00:00
|
|
|
client := svctest.NewResourceServiceBuilder().
|
|
|
|
WithRegisterFns(demo.RegisterTypes).
|
|
|
|
Run(t)
|
2023-08-21 20:02:23 +00:00
|
|
|
|
|
|
|
// Create a watch.
|
|
|
|
stream, err := client.WatchList(ctx, &pbresource.WatchListRequest{
|
|
|
|
Type: tc.typ,
|
|
|
|
Tenancy: tc.tenancy,
|
|
|
|
NamePrefix: "",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
rspCh := handleResourceStream(t, stream)
|
|
|
|
|
2024-02-02 21:11:05 +00:00
|
|
|
mustGetEndOfSnapshot(t, rspCh)
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
// Testcase will pick one of executive, recordLabel or artist based on scope of type.
|
2023-10-16 17:55:30 +00:00
|
|
|
recordLabel, err := demo.GenerateV1RecordLabel("looney-tunes")
|
2023-08-21 20:02:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
artist, err := demo.GenerateV2Artist()
|
|
|
|
require.NoError(t, err)
|
2023-12-13 15:06:39 +00:00
|
|
|
executive, err := demo.GenerateV1Executive("king-arthur", "CEO")
|
|
|
|
require.NoError(t, err)
|
2023-08-21 20:02:23 +00:00
|
|
|
|
|
|
|
// Create and verify upsert event received.
|
2023-12-05 19:00:06 +00:00
|
|
|
rlRsp, err := client.Write(ctx, &pbresource.WriteRequest{Resource: recordLabel})
|
2023-08-21 20:02:23 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-05 19:00:06 +00:00
|
|
|
artistRsp, err := client.Write(ctx, &pbresource.WriteRequest{Resource: artist})
|
2023-08-21 20:02:23 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-13 15:06:39 +00:00
|
|
|
executiveRsp, err := client.Write(ctx, &pbresource.WriteRequest{Resource: executive})
|
|
|
|
require.NoError(t, err)
|
2023-08-21 20:02:23 +00:00
|
|
|
|
|
|
|
var expected *pbresource.Resource
|
|
|
|
switch {
|
2023-12-13 15:06:39 +00:00
|
|
|
case resource.EqualType(tc.typ, demo.TypeV1RecordLabel):
|
2023-12-05 19:00:06 +00:00
|
|
|
expected = rlRsp.Resource
|
2023-12-13 15:06:39 +00:00
|
|
|
case resource.EqualType(tc.typ, demo.TypeV2Artist):
|
2023-12-05 19:00:06 +00:00
|
|
|
expected = artistRsp.Resource
|
2023-12-13 15:06:39 +00:00
|
|
|
case resource.EqualType(tc.typ, demo.TypeV1Executive):
|
|
|
|
expected = executiveRsp.Resource
|
2023-08-21 20:02:23 +00:00
|
|
|
default:
|
|
|
|
require.Fail(t, "unsupported type", tc.typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
rsp := mustGetResource(t, rspCh)
|
2024-02-02 21:11:05 +00:00
|
|
|
require.NotNil(t, rsp.GetUpsert())
|
|
|
|
prototest.AssertDeepEqual(t, expected, rsp.GetUpsert().Resource)
|
2023-08-21 20:02:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-27 19:37:54 +00:00
|
|
|
func TestWatchList_GroupVersionMismatch(t *testing.T) {
|
2023-04-06 09:40:04 +00:00
|
|
|
// Given a watch on TypeArtistV1 that only differs from TypeArtistV2 by GroupVersion
|
|
|
|
// When a resource of TypeArtistV2 is created/updated/deleted
|
2023-03-27 19:37:54 +00:00
|
|
|
// Then no watch events should be emitted
|
|
|
|
t.Parallel()
|
2023-04-11 11:10:14 +00:00
|
|
|
|
2024-02-02 21:11:05 +00:00
|
|
|
b := svctest.NewResourceServiceBuilder().
|
|
|
|
WithRegisterFns(demo.RegisterTypes)
|
|
|
|
client := b.Run(t)
|
|
|
|
|
2023-03-27 19:37:54 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-04-06 09:40:04 +00:00
|
|
|
// create a watch for TypeArtistV1
|
2023-03-27 19:37:54 +00:00
|
|
|
stream, err := client.WatchList(ctx, &pbresource.WatchListRequest{
|
2023-04-06 09:40:04 +00:00
|
|
|
Type: demo.TypeV1Artist,
|
2023-08-21 20:02:23 +00:00
|
|
|
Tenancy: resource.DefaultNamespacedTenancy(),
|
2023-03-27 19:37:54 +00:00
|
|
|
NamePrefix: "",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
rspCh := handleResourceStream(t, stream)
|
|
|
|
|
2024-02-02 21:11:05 +00:00
|
|
|
mustGetEndOfSnapshot(t, rspCh)
|
|
|
|
|
2023-04-06 09:40:04 +00:00
|
|
|
artist, err := demo.GenerateV2Artist()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-27 19:37:54 +00:00
|
|
|
// insert
|
2024-02-02 21:11:05 +00:00
|
|
|
r1Resp, err := client.Write(ctx, &pbresource.WriteRequest{Resource: artist})
|
2023-03-27 19:37:54 +00:00
|
|
|
require.NoError(t, err)
|
2024-02-02 21:11:05 +00:00
|
|
|
r1 := r1Resp.Resource
|
2023-03-27 19:37:54 +00:00
|
|
|
|
|
|
|
// update
|
|
|
|
r2 := clone(r1)
|
2024-02-02 21:11:05 +00:00
|
|
|
r2Resp, err := client.Write(ctx, &pbresource.WriteRequest{Resource: r2})
|
2023-03-27 19:37:54 +00:00
|
|
|
require.NoError(t, err)
|
2024-02-02 21:11:05 +00:00
|
|
|
r2 = r2Resp.Resource
|
2023-03-27 19:37:54 +00:00
|
|
|
|
|
|
|
// delete
|
2024-02-02 21:11:05 +00:00
|
|
|
_, err = client.Delete(ctx, &pbresource.DeleteRequest{Id: r2.Id, Version: r2.Version})
|
2023-03-27 19:37:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// verify no events received
|
|
|
|
mustGetNoResource(t, rspCh)
|
|
|
|
}
|
|
|
|
|
2023-04-25 11:52:35 +00:00
|
|
|
// N.B. Uses key ACLs for now. See demo.RegisterTypes()
|
2023-04-11 11:10:14 +00:00
|
|
|
func TestWatchList_ACL_ListDenied(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// deny all
|
2024-02-02 21:11:05 +00:00
|
|
|
rspCh, _ := roundTripACL(t, testutils.ACLNoPermissions(t), true)
|
2023-04-11 11:10:14 +00:00
|
|
|
|
|
|
|
// verify key:list denied
|
|
|
|
err := mustGetError(t, rspCh)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, codes.PermissionDenied.String(), status.Code(err).String())
|
|
|
|
require.Contains(t, err.Error(), "lacks permission 'key:list'")
|
|
|
|
}
|
|
|
|
|
2023-04-25 11:52:35 +00:00
|
|
|
// N.B. Uses key ACLs for now. See demo.RegisterTypes()
|
2023-04-11 11:10:14 +00:00
|
|
|
func TestWatchList_ACL_ListAllowed_ReadDenied(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// allow list, deny read
|
|
|
|
authz := AuthorizerFrom(t, `
|
|
|
|
key_prefix "resource/" { policy = "list" }
|
2023-06-26 12:25:14 +00:00
|
|
|
key_prefix "resource/demo.v2.Artist/" { policy = "deny" }
|
2023-04-11 11:10:14 +00:00
|
|
|
`)
|
2024-02-02 21:11:05 +00:00
|
|
|
rspCh, _ := roundTripACL(t, authz, false)
|
2023-04-11 11:10:14 +00:00
|
|
|
|
|
|
|
// verify resource filtered out by key:read denied, hence no events
|
|
|
|
mustGetNoResource(t, rspCh)
|
|
|
|
}
|
|
|
|
|
2023-04-25 11:52:35 +00:00
|
|
|
// N.B. Uses key ACLs for now. See demo.RegisterTypes()
|
2023-04-11 11:10:14 +00:00
|
|
|
func TestWatchList_ACL_ListAllowed_ReadAllowed(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// allow list, allow read
|
|
|
|
authz := AuthorizerFrom(t, `
|
|
|
|
key_prefix "resource/" { policy = "list" }
|
2023-06-26 12:25:14 +00:00
|
|
|
key_prefix "resource/demo.v2.Artist/" { policy = "read" }
|
2023-04-11 11:10:14 +00:00
|
|
|
`)
|
2024-02-02 21:11:05 +00:00
|
|
|
rspCh, artist := roundTripACL(t, authz, false)
|
2023-04-11 11:10:14 +00:00
|
|
|
|
|
|
|
// verify resource not filtered out by acl
|
|
|
|
event := mustGetResource(t, rspCh)
|
2024-02-02 21:11:05 +00:00
|
|
|
|
|
|
|
require.NotNil(t, event.GetUpsert())
|
|
|
|
prototest.AssertDeepEqual(t, artist, event.GetUpsert().Resource)
|
2023-04-11 11:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// roundtrip a WatchList which attempts to stream back a single write event
|
2024-02-02 21:11:05 +00:00
|
|
|
func roundTripACL(t *testing.T, authz acl.Authorizer, expectErr bool) (<-chan resourceOrError, *pbresource.Resource) {
|
2023-12-05 19:00:06 +00:00
|
|
|
mockACLResolver := &svc.MockACLResolver{}
|
2023-04-11 11:10:14 +00:00
|
|
|
mockACLResolver.On("ResolveTokenAndDefaultMeta", mock.Anything, mock.Anything, mock.Anything).
|
2024-02-02 21:11:05 +00:00
|
|
|
Return(resolver.Result{Authorizer: authz}, nil)
|
|
|
|
|
|
|
|
b := svctest.NewResourceServiceBuilder().
|
|
|
|
WithRegisterFns(demo.RegisterTypes).
|
|
|
|
WithACLResolver(mockACLResolver)
|
|
|
|
client := b.Run(t)
|
|
|
|
server := b.ServiceImpl()
|
2023-04-11 11:10:14 +00:00
|
|
|
|
|
|
|
artist, err := demo.GenerateV2Artist()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
stream, err := client.WatchList(testContext(t), &pbresource.WatchListRequest{
|
|
|
|
Type: artist.Id.Type,
|
|
|
|
Tenancy: artist.Id.Tenancy,
|
|
|
|
NamePrefix: "",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
rspCh := handleResourceStream(t, stream)
|
|
|
|
|
2024-02-02 21:11:05 +00:00
|
|
|
if !expectErr {
|
|
|
|
mustGetEndOfSnapshot(t, rspCh)
|
|
|
|
}
|
|
|
|
|
2023-04-11 11:10:14 +00:00
|
|
|
// induce single watch event
|
|
|
|
artist, err = server.Backend.WriteCAS(context.Background(), artist)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// caller to make assertions on the rspCh and written artist
|
|
|
|
return rspCh, artist
|
|
|
|
}
|
|
|
|
|
2023-03-27 19:37:54 +00:00
|
|
|
func mustGetNoResource(t *testing.T, ch <-chan resourceOrError) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case rsp := <-ch:
|
|
|
|
require.NoError(t, rsp.err)
|
|
|
|
require.Nil(t, rsp.rsp, "expected nil response with no error")
|
|
|
|
case <-time.After(250 * time.Millisecond):
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-02 21:11:05 +00:00
|
|
|
func mustGetEndOfSnapshot(t *testing.T, ch <-chan resourceOrError) {
|
|
|
|
event := mustGetResource(t, ch)
|
|
|
|
require.NotNil(t, event.GetEndOfSnapshot(), "expected EndOfSnapshot but got got event %T", event.GetEvent())
|
|
|
|
}
|
|
|
|
|
2023-03-27 19:37:54 +00:00
|
|
|
func mustGetResource(t *testing.T, ch <-chan resourceOrError) *pbresource.WatchEvent {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case rsp := <-ch:
|
|
|
|
require.NoError(t, rsp.err)
|
|
|
|
return rsp.rsp
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatal("timeout waiting for WatchListResponse")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustGetError(t *testing.T, ch <-chan resourceOrError) error {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case rsp := <-ch:
|
|
|
|
require.Error(t, rsp.err)
|
|
|
|
return rsp.err
|
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
t.Fatal("timeout waiting for WatchListResponse")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleResourceStream(t *testing.T, stream pbresource.ResourceService_WatchListClient) <-chan resourceOrError {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
rspCh := make(chan resourceOrError)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
rsp, err := stream.Recv()
|
|
|
|
if errors.Is(err, io.EOF) ||
|
|
|
|
errors.Is(err, context.Canceled) ||
|
|
|
|
errors.Is(err, context.DeadlineExceeded) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rspCh <- resourceOrError{
|
|
|
|
rsp: rsp,
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return rspCh
|
|
|
|
}
|
|
|
|
|
|
|
|
type resourceOrError struct {
|
|
|
|
rsp *pbresource.WatchEvent
|
|
|
|
err error
|
|
|
|
}
|
2023-11-03 20:03:07 +00:00
|
|
|
|
|
|
|
func TestWatchList_NoTenancy(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-12-05 19:00:06 +00:00
|
|
|
|
2023-11-03 20:03:07 +00:00
|
|
|
ctx := context.Background()
|
2023-12-05 19:00:06 +00:00
|
|
|
client := svctest.NewResourceServiceBuilder().
|
|
|
|
WithRegisterFns(demo.RegisterTypes).
|
|
|
|
Run(t)
|
2023-11-03 20:03:07 +00:00
|
|
|
|
|
|
|
// Create a watch.
|
|
|
|
stream, err := client.WatchList(ctx, &pbresource.WatchListRequest{
|
|
|
|
Type: demo.TypeV1RecordLabel,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
rspCh := handleResourceStream(t, stream)
|
|
|
|
|
2024-02-02 21:11:05 +00:00
|
|
|
mustGetEndOfSnapshot(t, rspCh)
|
|
|
|
|
2023-11-03 20:03:07 +00:00
|
|
|
recordLabel, err := demo.GenerateV1RecordLabel("looney-tunes")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Create and verify upsert event received.
|
2023-12-05 19:00:06 +00:00
|
|
|
rsp1, err := client.Write(ctx, &pbresource.WriteRequest{Resource: recordLabel})
|
2023-11-03 20:03:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-05 19:00:06 +00:00
|
|
|
rsp2 := mustGetResource(t, rspCh)
|
2023-11-03 20:03:07 +00:00
|
|
|
|
2024-02-02 21:11:05 +00:00
|
|
|
require.NotNil(t, rsp2.GetUpsert())
|
|
|
|
prototest.AssertDeepEqual(t, rsp1.Resource, rsp2.GetUpsert().Resource)
|
2023-11-03 20:03:07 +00:00
|
|
|
}
|