feat: read resource namespace (#19320)

* test: add missing tests for read resource namespace

* refactor: remove redundant test

* refactor: rename import aliases

* fix: typo var name

* refctor: remove another redundant test
This commit is contained in:
Poonam Jadhav 2023-10-26 09:28:36 -04:00 committed by GitHub
parent 0fefaa649f
commit b5023b69c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 48 additions and 24 deletions

View File

@ -5,12 +5,13 @@ package tenancytest
import (
"context"
"github.com/hashicorp/consul/agent/grpc-external/services/resource"
svctest "github.com/hashicorp/consul/agent/grpc-external/services/resource/testing"
resource2 "github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/internal/tenancy"
"testing"
svc "github.com/hashicorp/consul/agent/grpc-external/services/resource"
svctest "github.com/hashicorp/consul/agent/grpc-external/services/resource/testing"
"github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/internal/tenancy"
rtest "github.com/hashicorp/consul/internal/resource/resourcetest"
"github.com/hashicorp/consul/proto/private/prototest"
"google.golang.org/grpc/codes"
@ -21,9 +22,28 @@ import (
"github.com/stretchr/testify/require"
)
func TestWriteNamespace_Success(t *testing.T) {
v2TenancyBridge := tenancy.NewV2TenancyBridge()
config := svc.Config{TenancyBridge: v2TenancyBridge}
client := svctest.RunResourceServiceWithConfig(t, config, tenancy.RegisterTypes)
cl := rtest.NewClient(client)
res := rtest.Resource(pbtenancy.NamespaceType, "ns1").
WithTenancy(resource.DefaultPartitionedTenancy()).
WithData(t, validNamespace()).
Build()
writeRsp, err := cl.Write(context.Background(), &pbresource.WriteRequest{Resource: res})
require.NoError(t, err)
prototest.AssertDeepEqual(t, res.Id.Type, writeRsp.Resource.Id.Type)
prototest.AssertDeepEqual(t, res.Id.Tenancy, writeRsp.Resource.Id.Tenancy)
prototest.AssertDeepEqual(t, res.Id.Name, writeRsp.Resource.Id.Name)
prototest.AssertDeepEqual(t, res.Data, writeRsp.Resource.Data)
}
func TestReadNamespace_Success(t *testing.T) {
v2TenancyBridge := tenancy.NewV2TenancyBridge()
config := resource.Config{TenancyBridge: v2TenancyBridge}
config := svc.Config{TenancyBridge: v2TenancyBridge}
client := svctest.RunResourceServiceWithConfig(t, config, tenancy.RegisterTypes)
cl := rtest.NewClient(client)
@ -31,28 +51,32 @@ func TestReadNamespace_Success(t *testing.T) {
WithData(t, validNamespace()).
Write(t, cl)
readRsp, err := cl.Read(context.Background(), &pbresource.ReadRequest{Id: res.Id})
require.NoError(t, err)
prototest.AssertDeepEqual(t, res.Id, readRsp.Resource.Id)
}
cases := []struct {
name string
resource *pbresource.Resource
errMsg string
}{
{
name: "read namespace",
resource: rtest.Resource(pbtenancy.NamespaceType, "ns1").
WithData(t, validNamespace()).
Build(),
},
}
func TestReadNamespace_NotFound(t *testing.T) {
v2TenancyBridge := tenancy.NewV2TenancyBridge()
config := resource.Config{TenancyBridge: v2TenancyBridge}
client := svctest.RunResourceServiceWithConfig(t, config, tenancy.RegisterTypes)
cl := rtest.NewClient(client)
res := rtest.Resource(pbtenancy.NamespaceType, "ns1").
WithData(t, validNamespace()).Build()
_, err := cl.Read(context.Background(), &pbresource.ReadRequest{Id: res.Id})
require.Error(t, err)
require.Equal(t, codes.NotFound.String(), status.Code(err).String())
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
readRsp, err := cl.Read(context.Background(), &pbresource.ReadRequest{Id: tc.resource.Id})
require.NoError(t, err)
prototest.AssertDeepEqual(t, res.Id, readRsp.Resource.Id)
prototest.AssertDeepEqual(t, res.Data, readRsp.Resource.Data)
})
}
}
func TestDeleteNamespace_Success(t *testing.T) {
v2TenancyBridge := tenancy.NewV2TenancyBridge()
config := resource.Config{TenancyBridge: v2TenancyBridge}
config := svc.Config{TenancyBridge: v2TenancyBridge}
client := svctest.RunResourceServiceWithConfig(t, config, tenancy.RegisterTypes)
cl := rtest.NewClient(client)
@ -74,7 +98,7 @@ func TestDeleteNamespace_Success(t *testing.T) {
func TestListNamespace_Success(t *testing.T) {
v2TenancyBridge := tenancy.NewV2TenancyBridge()
config := resource.Config{TenancyBridge: v2TenancyBridge}
config := svc.Config{TenancyBridge: v2TenancyBridge}
client := svctest.RunResourceServiceWithConfig(t, config, tenancy.RegisterTypes)
cl := rtest.NewClient(client)
@ -87,7 +111,7 @@ func TestListNamespace_Success(t *testing.T) {
require.NotNil(t, res)
listRsp, err := cl.List(context.Background(), &pbresource.ListRequest{Type: pbtenancy.NamespaceType, Tenancy: resource2.DefaultPartitionedTenancy()})
listRsp, err := cl.List(context.Background(), &pbresource.ListRequest{Type: pbtenancy.NamespaceType, Tenancy: resource.DefaultPartitionedTenancy()})
require.NoError(t, err)
require.Len(t, listRsp.Resources, 3)
names := []string{