mirror of
https://github.com/status-im/consul.git
synced 2025-01-11 06:16:08 +00:00
3795769729
At the end of this test we were trying to ensure that updating a service in the local state causes it to re-register the service with the config manager. The config manager in the same method will also call RegisteredProxies to determine if any need to be removed. This portion of the test is not attempting to verify that behavior. Because the test is only blocked waiting for the Register event before it can end and assert all the mock expectations were met, we may not see the call to RegisteredProxies. This is especially apparent when tests are run with the race detector. As we don’t actually care if that method is executed before the end of the test we can simply transition from expecting it to be called exactly once to a 0 or 1 times assertion.
112 lines
2.9 KiB
Go
112 lines
2.9 KiB
Go
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.AddService(&structs.NodeService{
|
|
ID: serviceID,
|
|
Kind: structs.ServiceKindConnectProxy,
|
|
}, serviceToken)
|
|
|
|
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,
|
|
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.AddService(&structs.NodeService{
|
|
ID: serviceID,
|
|
Kind: structs.ServiceKindConnectProxy,
|
|
}, "")
|
|
|
|
select {
|
|
case reg := <-registerCh:
|
|
require.Equal(t, serviceID, reg.service.ID)
|
|
require.Equal(t, userToken, reg.token)
|
|
case <-time.After(100 * time.Millisecond):
|
|
t.Fatal("timeout waiting for service to be registered")
|
|
}
|
|
}
|