Skip to content

Commit

Permalink
refactoring tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tauhid621 committed Jan 11, 2024
1 parent 9336394 commit 82519f7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
49 changes: 49 additions & 0 deletions agent/consul/configentry_backend_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package consul

import (
"context"
"testing"
"time"

"github.com/hashicorp/consul/proto/private/pbconfigentry"
"github.com/hashicorp/consul/sdk/freeport"
"github.com/hashicorp/consul/testrpc"
"github.com/stretchr/testify/require"
gogrpc "google.golang.org/grpc"
)

func TestConfigEntryBackend_EmptyPartition(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}

t.Parallel()

_, s1 := testServerWithConfig(t, func(c *Config) {
c.GRPCTLSPort = freeport.GetOne(t)
})
testrpc.WaitForLeader(t, s1.RPC, "dc1")

// make a grpc client to dial s1 directly
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
t.Cleanup(cancel)

conn, err := gogrpc.DialContext(ctx, s1.config.RPCAddr.String(),
gogrpc.WithContextDialer(newServerDialer(s1.config.RPCAddr.String())),
//nolint:staticcheck
gogrpc.WithInsecure(),
gogrpc.WithBlock())
require.NoError(t, err)
t.Cleanup(func() { conn.Close() })

configEntryClient := pbconfigentry.NewConfigEntryServiceClient(conn)

req := pbconfigentry.GetResolvedExportedServicesRequest{
Partition: "",
}
_, err = configEntryClient.GetResolvedExportedServices(ctx, &req)
require.NoError(t, err)
}
2 changes: 1 addition & 1 deletion agent/grpc-external/services/configentry/server_ce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"google.golang.org/grpc"
)

func TestGetResolvedExportedServices_ACL_Allow(t *testing.T) {
func TestGetResolvedExportedServices(t *testing.T) {
authorizer := acl.MockAuthorizer{}
authorizer.On("MeshRead", mock.Anything).Return(acl.Allow)

Expand Down
22 changes: 22 additions & 0 deletions agent/grpc-external/services/configentry/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ func TestGetResolvedExportedServices_ACL_Deny(t *testing.T) {
require.Error(t, err)
}

func TestGetResolvedExportedServices_AC_Allow(t *testing.T) {
authorizer := acl.MockAuthorizer{}
authorizer.On("MeshRead", mock.Anything).Return(acl.Allow)

backend := &MockBackend{authorizer: &authorizer}
backend.On("EnterpriseCheckPartitions", mock.Anything).Return(nil)

fakeFSM := testutils.NewFakeBlockingFSM(t)

c := Config{
Backend: backend,
Logger: hclog.New(nil),
ForwardRPC: doForwardRPC,
FSMServer: fakeFSM,
}
server := NewServer(c)

ctx := grpc.NewContextWithServerTransportStream(context.Background(), &testutils.MockServerTransportStream{})
_, err := server.GetResolvedExportedServices(ctx, &pbconfigentry.GetResolvedExportedServicesRequest{})
require.NoError(t, err)
}

func TestGetResolvedExportedServices_PartitionCheck(t *testing.T) {
authorizer := acl.MockAuthorizer{}
authorizer.On("MeshRead", mock.Anything).Return(acl.Allow)
Expand Down

0 comments on commit 82519f7

Please sign in to comment.