Skip to content

Commit

Permalink
Add namespace read write tests (#19173)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhiaayachi authored Oct 13, 2023
1 parent 76c60fd commit 5fbf0c0
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 9 deletions.
2 changes: 2 additions & 0 deletions agent/grpc-external/services/resource/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ func RunResourceServiceWithConfig(t *testing.T, config svc.Config, registerFns .
if config.TenancyBridge == nil {
mockTenancyBridge := &svc.MockTenancyBridge{}
mockTenancyBridge.On("PartitionExists", resource.DefaultPartitionName).Return(true, nil)
mockTenancyBridge.On("PartitionExists", "foo").Return(true, nil)
mockTenancyBridge.On("NamespaceExists", resource.DefaultPartitionName, resource.DefaultNamespaceName).Return(true, nil)
mockTenancyBridge.On("IsPartitionMarkedForDeletion", resource.DefaultPartitionName).Return(false, nil)
mockTenancyBridge.On("IsPartitionMarkedForDeletion", "foo").Return(false, nil)
mockTenancyBridge.On("IsNamespaceMarkedForDeletion", resource.DefaultPartitionName, resource.DefaultNamespaceName).Return(false, nil)
config.TenancyBridge = mockTenancyBridge
}
Expand Down
1 change: 1 addition & 0 deletions internal/tenancy/internal/types/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func RegisterNamespace(r resource.Registry) {
Scope: resource.ScopePartition,
Validate: ValidateNamespace,
Mutate: MutateNamespace,
// ACLs: TODO
})
}

Expand Down
82 changes: 73 additions & 9 deletions internal/tenancy/internal/types/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@
package types

import (
"context"
"errors"
svctest "github.com/hashicorp/consul/agent/grpc-external/services/resource/testing"
rtest "github.com/hashicorp/consul/internal/resource/resourcetest"
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1"
"github.com/hashicorp/consul/proto/private/prototest"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"testing"

"github.com/stretchr/testify/require"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/known/anypb"

"github.com/hashicorp/consul/internal/resource"
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1"
"github.com/hashicorp/consul/proto-public/pbresource"
tenancyv1alpha1 "github.com/hashicorp/consul/proto-public/pbtenancy/v1alpha1"
pbtenancy "github.com/hashicorp/consul/proto-public/pbtenancy/v1alpha1"
)

func createNamespaceResource(t *testing.T, data protoreflect.ProtoMessage) *pbresource.Resource {
Expand All @@ -32,12 +38,6 @@ func createNamespaceResource(t *testing.T, data protoreflect.ProtoMessage) *pbre
return res
}

func validNamespace() *tenancyv1alpha1.Namespace {
return &tenancyv1alpha1.Namespace{
Description: "description from user",
}
}

func TestValidateNamespace_Ok(t *testing.T) {
res := createNamespaceResource(t, validNamespace())

Expand Down Expand Up @@ -129,7 +129,7 @@ func TestValidateNamespace(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a, err := anypb.New(&tenancyv1alpha1.Namespace{})
a, err := anypb.New(&pbtenancy.Namespace{})
require.NoError(t, err)
res := &pbresource.Resource{Id: &pbresource.ID{Name: tt.namespaceName}, Data: a}
err = ValidateNamespace(res)
Expand All @@ -142,3 +142,67 @@ func TestValidateNamespace(t *testing.T) {
})
}
}

func TestRead_Success(t *testing.T) {
client := svctest.RunResourceService(t, Register)
client = rtest.NewClient(client)

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

readRsp, err := client.Read(context.Background(), &pbresource.ReadRequest{Id: res.Id})
require.NoError(t, err)
prototest.AssertDeepEqual(t, res.Id, readRsp.Resource.Id)
}

func TestRead_NotFound(t *testing.T) {
client := svctest.RunResourceService(t, Register)
client = rtest.NewClient(client)

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

_, err := client.Read(context.Background(), &pbresource.ReadRequest{Id: res.Id})
require.Error(t, err)
require.Equal(t, codes.NotFound.String(), status.Code(err).String())
}

func TestDelete_Success(t *testing.T) {
client := svctest.RunResourceService(t, Register)
client = rtest.NewClient(client)

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

readRsp, err := client.Read(context.Background(), &pbresource.ReadRequest{Id: res.Id})
require.NoError(t, err)
prototest.AssertDeepEqual(t, res.Id, readRsp.Resource.Id)

_, err = client.Delete(context.Background(), &pbresource.DeleteRequest{Id: res.Id})
require.NoError(t, err)

_, err = client.Read(context.Background(), &pbresource.ReadRequest{Id: res.Id})
require.Error(t, err)
require.Equal(t, codes.NotFound.String(), status.Code(err).String())

}

func TestRead_MixedCases_Success(t *testing.T) {
client := svctest.RunResourceService(t, Register)
client = rtest.NewClient(client)

res := rtest.Resource(NamespaceType, "nS1").
WithData(t, validNamespace()).Write(t, client)

readRsp, err := client.Read(context.Background(), &pbresource.ReadRequest{Id: res.Id})
require.NoError(t, err)
prototest.AssertDeepEqual(t, res.Id, readRsp.Resource.Id)

}

func validNamespace() *pbtenancy.Namespace {
return &pbtenancy.Namespace{
Description: "ns namespace",
}
}

0 comments on commit 5fbf0c0

Please sign in to comment.