Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: read resource namespace #19320

Merged
merged 5 commits into from
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 41 additions & 17 deletions internal/tenancy/tenancytest/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ package tenancytest

import (
"context"
"github.com/hashicorp/consul/agent/grpc-external/services/resource"
"testing"

svc "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/resource"
"github.com/hashicorp/consul/internal/tenancy"
"testing"

rtest "github.com/hashicorp/consul/internal/resource/resourcetest"
"github.com/hashicorp/consul/proto/private/prototest"
Expand All @@ -21,38 +22,61 @@ import (
"github.com/stretchr/testify/require"
)

func TestReadNamespace_Success(t *testing.T) {
func TestWriteNamespace_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)

res := rtest.Resource(pbtenancy.NamespaceType, "ns1").
WithTenancy(resource.DefaultPartitionedTenancy()).
WithData(t, validNamespace()).
Write(t, cl)
Build()

readRsp, err := cl.Read(context.Background(), &pbresource.ReadRequest{Id: res.Id})
writeRsp, err := cl.Write(context.Background(), &pbresource.WriteRequest{Resource: res})
require.NoError(t, err)
prototest.AssertDeepEqual(t, res.Id, readRsp.Resource.Id)
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_NotFound(t *testing.T) {
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)

res := rtest.Resource(pbtenancy.NamespaceType, "ns1").
WithData(t, validNamespace()).Build()
WithData(t, validNamespace()).
Write(t, cl)

_, err := cl.Read(context.Background(), &pbresource.ReadRequest{Id: res.Id})
require.Error(t, err)
require.Equal(t, codes.NotFound.String(), status.Code(err).String())
cases := []struct {
name string
resource *pbresource.Resource
errMsg string
}{
{
name: "read namespace",
resource: rtest.Resource(pbtenancy.NamespaceType, "ns1").
WithData(t, validNamespace()).
Build(),
},
}

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)

Expand All @@ -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)

Expand All @@ -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{
Expand Down
Loading