You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
consul/agent/proxycfg-sources/local/sync_test.go

126 lines
3.2 KiB

// Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * 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>
1 year ago
// SPDX-License-Identifier: BUSL-1.1
package local
import (
"context"
"testing"
"time"
"github.com/hashicorp/go-hclog"
mock "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/hashicorp/consul/agent/local"
"github.com/hashicorp/consul/agent/proxycfg"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/agent/token"
)
func TestSync(t *testing.T) {
const (
serviceID = "some-service"
serviceToken = "some-service-token"
otherServiceID = "other-service"
userToken = "user-token"
)
tokens := &token.Store{}
tokens.UpdateUserToken(userToken, token.TokenSourceConfig)
state := local.NewState(local.Config{}, hclog.NewNullLogger(), tokens)
state.TriggerSyncChanges = func() {}
state.AddServiceWithChecks(&structs.NodeService{
ID: serviceID,
Kind: structs.ServiceKindConnectProxy,
}, nil, serviceToken, false)
cfgMgr := NewMockConfigManager(t)
type registration struct {
id proxycfg.ProxyID
service *structs.NodeService
token string
}
registerCh := make(chan registration)
cfgMgr.On("Register", mock.Anything, mock.Anything, source, mock.Anything, true).
Run(func(args mock.Arguments) {
id := args.Get(0).(proxycfg.ProxyID)
service := args.Get(1).(*structs.NodeService)
token := args.Get(3).(string)
registerCh <- registration{id, service, token}
}).
Return(nil)
deregisterCh := make(chan proxycfg.ProxyID)
cfgMgr.On("Deregister", mock.Anything, source).
Run(func(args mock.Arguments) {
id := args.Get(0).(proxycfg.ProxyID)
deregisterCh <- id
}).
Return()
cfgMgr.On("RegisteredProxies", source).
Return([]proxycfg.ProxyID{{ServiceID: structs.ServiceID{ID: otherServiceID}}}).
Once()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
go Sync(ctx, SyncConfig{
Manager: cfgMgr,
State: state,
NodeLocality: &structs.Locality{
Region: "some-region",
Zone: "some-zone",
},
Tokens: tokens,
Logger: hclog.NewNullLogger(),
})
// Expect the service in the local state to be registered.
select {
case reg := <-registerCh:
require.Equal(t, serviceID, reg.service.ID)
require.Equal(t, serviceToken, reg.token)
case <-time.After(100 * time.Millisecond):
t.Fatal("timeout waiting for service to be registered")
}
// Expect the service not in the local state to be de-registered.
select {
case id := <-deregisterCh:
require.Equal(t, otherServiceID, id.ID)
case <-time.After(100 * time.Millisecond):
t.Fatal("timeout waiting for service to be de-registered")
}
// Update the service (without a token) and expect it to be re-registered (with
// the user token).
cfgMgr.On("RegisteredProxies", source).
Return([]proxycfg.ProxyID{}).
Maybe()
state.AddServiceWithChecks(&structs.NodeService{
ID: serviceID,
Kind: structs.ServiceKindConnectProxy,
}, nil, "", false)
select {
case reg := <-registerCh:
require.Equal(t, serviceID, reg.service.ID)
require.Equal(t,
&structs.Locality{
Region: "some-region",
Zone: "some-zone",
},
reg.service.Locality,
)
require.Equal(t, userToken, reg.token)
case <-time.After(100 * time.Millisecond):
t.Fatal("timeout waiting for service to be registered")
}
}