2023-10-20 18:49:54 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
|
|
package tenancytest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-10-26 13:28:36 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-10-27 13:55:02 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
2023-10-20 18:49:54 +00:00
|
|
|
svctest "github.com/hashicorp/consul/agent/grpc-external/services/resource/testing"
|
2023-10-26 13:28:36 +00:00
|
|
|
"github.com/hashicorp/consul/internal/resource"
|
2023-10-20 18:49:54 +00:00
|
|
|
rtest "github.com/hashicorp/consul/internal/resource/resourcetest"
|
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
pbtenancy "github.com/hashicorp/consul/proto-public/pbtenancy/v2beta1"
|
2023-10-27 13:55:02 +00:00
|
|
|
"github.com/hashicorp/consul/proto/private/prototest"
|
2023-10-20 18:49:54 +00:00
|
|
|
)
|
|
|
|
|
2023-10-26 13:28:36 +00:00
|
|
|
func TestWriteNamespace_Success(t *testing.T) {
|
2023-12-05 19:00:06 +00:00
|
|
|
cl := rtest.NewClient(svctest.NewResourceServiceBuilder().WithV2Tenancy(true).Run(t))
|
2023-10-20 18:49:54 +00:00
|
|
|
|
|
|
|
res := rtest.Resource(pbtenancy.NamespaceType, "ns1").
|
2023-10-26 13:28:36 +00:00
|
|
|
WithTenancy(resource.DefaultPartitionedTenancy()).
|
2023-10-20 18:49:54 +00:00
|
|
|
WithData(t, validNamespace()).
|
2023-10-26 13:28:36 +00:00
|
|
|
Build()
|
2023-10-20 18:49:54 +00:00
|
|
|
|
2023-10-26 13:28:36 +00:00
|
|
|
writeRsp, err := cl.Write(context.Background(), &pbresource.WriteRequest{Resource: res})
|
2023-10-20 18:49:54 +00:00
|
|
|
require.NoError(t, err)
|
2023-10-26 13:28:36 +00:00
|
|
|
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)
|
2023-10-20 18:49:54 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 13:28:36 +00:00
|
|
|
func TestReadNamespace_Success(t *testing.T) {
|
2023-12-05 19:00:06 +00:00
|
|
|
cl := rtest.NewClient(svctest.NewResourceServiceBuilder().WithV2Tenancy(true).Run(t))
|
2023-10-20 18:49:54 +00:00
|
|
|
|
|
|
|
res := rtest.Resource(pbtenancy.NamespaceType, "ns1").
|
2023-10-26 13:28:36 +00:00
|
|
|
WithData(t, validNamespace()).
|
|
|
|
Write(t, cl)
|
2023-10-20 18:49:54 +00:00
|
|
|
|
2023-10-26 13:28:36 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
2023-10-20 18:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteNamespace_Success(t *testing.T) {
|
2023-12-05 19:00:06 +00:00
|
|
|
cl := rtest.NewClient(svctest.NewResourceServiceBuilder().WithV2Tenancy(true).Run(t))
|
2023-10-20 18:49:54 +00:00
|
|
|
|
|
|
|
res := rtest.Resource(pbtenancy.NamespaceType, "ns1").
|
|
|
|
WithData(t, validNamespace()).Write(t, cl)
|
|
|
|
|
|
|
|
readRsp, err := cl.Read(context.Background(), &pbresource.ReadRequest{Id: res.Id})
|
|
|
|
require.NoError(t, err)
|
|
|
|
prototest.AssertDeepEqual(t, res.Id, readRsp.Resource.Id)
|
|
|
|
|
|
|
|
_, err = cl.Delete(context.Background(), &pbresource.DeleteRequest{Id: res.Id})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = cl.Read(context.Background(), &pbresource.ReadRequest{Id: res.Id})
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, codes.NotFound.String(), status.Code(err).String())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListNamespace_Success(t *testing.T) {
|
2023-12-05 19:00:06 +00:00
|
|
|
cl := rtest.NewClient(svctest.NewResourceServiceBuilder().WithV2Tenancy(true).Run(t))
|
2023-10-20 18:49:54 +00:00
|
|
|
|
|
|
|
res := rtest.Resource(pbtenancy.NamespaceType, "ns1").
|
|
|
|
WithData(t, validNamespace()).Write(t, cl)
|
|
|
|
|
|
|
|
require.NotNil(t, res)
|
|
|
|
res = rtest.Resource(pbtenancy.NamespaceType, "ns2").
|
|
|
|
WithData(t, validNamespace()).Write(t, cl)
|
|
|
|
|
|
|
|
require.NotNil(t, res)
|
|
|
|
|
2023-10-26 13:28:36 +00:00
|
|
|
listRsp, err := cl.List(context.Background(), &pbresource.ListRequest{Type: pbtenancy.NamespaceType, Tenancy: resource.DefaultPartitionedTenancy()})
|
2023-10-20 18:49:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, listRsp.Resources, 3)
|
|
|
|
names := []string{
|
|
|
|
listRsp.Resources[0].Id.Name,
|
|
|
|
listRsp.Resources[1].Id.Name,
|
|
|
|
listRsp.Resources[2].Id.Name,
|
|
|
|
}
|
|
|
|
require.Contains(t, names, "default")
|
|
|
|
require.Contains(t, names, "ns1")
|
|
|
|
require.Contains(t, names, "ns2")
|
|
|
|
}
|
|
|
|
|
|
|
|
func validNamespace() *pbtenancy.Namespace {
|
|
|
|
return &pbtenancy.Namespace{
|
|
|
|
Description: "ns namespace",
|
|
|
|
}
|
|
|
|
}
|