From 5fbf0c00d3e8e31cfd2c824a69fcbfd2989e7895 Mon Sep 17 00:00:00 2001 From: Dhia Ayachi Date: Fri, 13 Oct 2023 12:03:06 -0400 Subject: [PATCH] Add namespace read write tests (#19173) --- .../services/resource/testing/testing.go | 2 + internal/tenancy/internal/types/namespace.go | 1 + .../tenancy/internal/types/namespace_test.go | 82 +++++++++++++++++-- 3 files changed, 76 insertions(+), 9 deletions(-) diff --git a/agent/grpc-external/services/resource/testing/testing.go b/agent/grpc-external/services/resource/testing/testing.go index 16eedd3a96..2375ae95b8 100644 --- a/agent/grpc-external/services/resource/testing/testing.go +++ b/agent/grpc-external/services/resource/testing/testing.go @@ -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 } diff --git a/internal/tenancy/internal/types/namespace.go b/internal/tenancy/internal/types/namespace.go index ac9848a4f2..4bc95d1505 100644 --- a/internal/tenancy/internal/types/namespace.go +++ b/internal/tenancy/internal/types/namespace.go @@ -32,6 +32,7 @@ func RegisterNamespace(r resource.Registry) { Scope: resource.ScopePartition, Validate: ValidateNamespace, Mutate: MutateNamespace, + // ACLs: TODO }) } diff --git a/internal/tenancy/internal/types/namespace_test.go b/internal/tenancy/internal/types/namespace_test.go index 48d5e51ce2..b64f86d521 100644 --- a/internal/tenancy/internal/types/namespace_test.go +++ b/internal/tenancy/internal/types/namespace_test.go @@ -4,7 +4,14 @@ 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" @@ -12,9 +19,8 @@ import ( "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 { @@ -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()) @@ -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) @@ -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", + } +}