mirror of https://github.com/status-im/consul.git
Add namespace read write tests (#19173)
This commit is contained in:
parent
76c60fdfac
commit
5fbf0c00d3
|
@ -92,8 +92,10 @@ func RunResourceServiceWithConfig(t *testing.T, config svc.Config, registerFns .
|
||||||
if config.TenancyBridge == nil {
|
if config.TenancyBridge == nil {
|
||||||
mockTenancyBridge := &svc.MockTenancyBridge{}
|
mockTenancyBridge := &svc.MockTenancyBridge{}
|
||||||
mockTenancyBridge.On("PartitionExists", resource.DefaultPartitionName).Return(true, nil)
|
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("NamespaceExists", resource.DefaultPartitionName, resource.DefaultNamespaceName).Return(true, nil)
|
||||||
mockTenancyBridge.On("IsPartitionMarkedForDeletion", resource.DefaultPartitionName).Return(false, 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)
|
mockTenancyBridge.On("IsNamespaceMarkedForDeletion", resource.DefaultPartitionName, resource.DefaultNamespaceName).Return(false, nil)
|
||||||
config.TenancyBridge = mockTenancyBridge
|
config.TenancyBridge = mockTenancyBridge
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,7 @@ func RegisterNamespace(r resource.Registry) {
|
||||||
Scope: resource.ScopePartition,
|
Scope: resource.ScopePartition,
|
||||||
Validate: ValidateNamespace,
|
Validate: ValidateNamespace,
|
||||||
Mutate: MutateNamespace,
|
Mutate: MutateNamespace,
|
||||||
|
// ACLs: TODO
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,14 @@
|
||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"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"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
@ -12,9 +19,8 @@ import (
|
||||||
"google.golang.org/protobuf/types/known/anypb"
|
"google.golang.org/protobuf/types/known/anypb"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/internal/resource"
|
"github.com/hashicorp/consul/internal/resource"
|
||||||
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1"
|
|
||||||
"github.com/hashicorp/consul/proto-public/pbresource"
|
"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 {
|
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
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func validNamespace() *tenancyv1alpha1.Namespace {
|
|
||||||
return &tenancyv1alpha1.Namespace{
|
|
||||||
Description: "description from user",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestValidateNamespace_Ok(t *testing.T) {
|
func TestValidateNamespace_Ok(t *testing.T) {
|
||||||
res := createNamespaceResource(t, validNamespace())
|
res := createNamespaceResource(t, validNamespace())
|
||||||
|
|
||||||
|
@ -129,7 +129,7 @@ func TestValidateNamespace(t *testing.T) {
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
a, err := anypb.New(&tenancyv1alpha1.Namespace{})
|
a, err := anypb.New(&pbtenancy.Namespace{})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
res := &pbresource.Resource{Id: &pbresource.ID{Name: tt.namespaceName}, Data: a}
|
res := &pbresource.Resource{Id: &pbresource.ID{Name: tt.namespaceName}, Data: a}
|
||||||
err = ValidateNamespace(res)
|
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",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue