Skip to content

Commit

Permalink
feat: read resource namespace (#19320)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
JadhavPoonam authored Oct 26, 2023
1 parent 0fefaa6 commit b5023b6
Showing 1 changed file with 41 additions and 17 deletions.
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

0 comments on commit b5023b6

Please sign in to comment.