mirror of
https://github.com/status-im/consul.git
synced 2025-01-09 13:26:07 +00:00
5fb9df1640
* Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
94 lines
2.4 KiB
Go
94 lines
2.4 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package serverdiscovery
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
"github.com/stretchr/testify/require"
|
|
"google.golang.org/grpc"
|
|
|
|
"github.com/hashicorp/consul/agent/consul/autopilotevents"
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
|
"github.com/hashicorp/consul/agent/consul/stream"
|
|
"github.com/hashicorp/consul/agent/grpc-external/testutils"
|
|
"github.com/hashicorp/consul/proto-public/pbserverdiscovery"
|
|
)
|
|
|
|
type mockSnapshotHandler struct {
|
|
mock.Mock
|
|
}
|
|
|
|
func newMockSnapshotHandler(t *testing.T) *mockSnapshotHandler {
|
|
handler := &mockSnapshotHandler{}
|
|
t.Cleanup(func() {
|
|
handler.AssertExpectations(t)
|
|
})
|
|
return handler
|
|
}
|
|
|
|
func (m *mockSnapshotHandler) handle(req stream.SubscribeRequest, buf stream.SnapshotAppender) (uint64, error) {
|
|
ret := m.Called(req, buf)
|
|
return ret.Get(0).(uint64), ret.Error(1)
|
|
}
|
|
|
|
func (m *mockSnapshotHandler) expect(token string, requestIndex uint64, eventIndex uint64, payload autopilotevents.EventPayloadReadyServers) {
|
|
m.On("handle", stream.SubscribeRequest{
|
|
Topic: autopilotevents.EventTopicReadyServers,
|
|
Subject: stream.SubjectNone,
|
|
Token: token,
|
|
Index: requestIndex,
|
|
}, mock.Anything).Once().Run(func(args mock.Arguments) {
|
|
buf := args.Get(1).(stream.SnapshotAppender)
|
|
buf.Append([]stream.Event{
|
|
{
|
|
Topic: autopilotevents.EventTopicReadyServers,
|
|
Index: eventIndex,
|
|
Payload: payload,
|
|
},
|
|
})
|
|
}).Return(eventIndex, nil)
|
|
}
|
|
|
|
func newMockACLResolver(t *testing.T) *MockACLResolver {
|
|
t.Helper()
|
|
m := &MockACLResolver{}
|
|
t.Cleanup(func() { m.AssertExpectations(t) })
|
|
return m
|
|
}
|
|
|
|
func setupPublisher(t *testing.T) (*mockSnapshotHandler, state.EventPublisher) {
|
|
t.Helper()
|
|
|
|
handler := newMockSnapshotHandler(t)
|
|
|
|
publisher := stream.NewEventPublisher(10 * time.Second)
|
|
publisher.RegisterHandler(autopilotevents.EventTopicReadyServers, handler.handle, false)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
t.Cleanup(cancel)
|
|
go publisher.Run(ctx)
|
|
|
|
return handler, publisher
|
|
|
|
}
|
|
|
|
func testClient(t *testing.T, server *Server) pbserverdiscovery.ServerDiscoveryServiceClient {
|
|
t.Helper()
|
|
|
|
addr := testutils.RunTestServer(t, server)
|
|
|
|
//nolint:staticcheck
|
|
conn, err := grpc.DialContext(context.Background(), addr.String(), grpc.WithInsecure())
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() {
|
|
require.NoError(t, conn.Close())
|
|
})
|
|
|
|
return pbserverdiscovery.NewServerDiscoveryServiceClient(conn)
|
|
}
|