agent: rename AddService->AddServiceFromSource

In preparation for extracting a single AddService func that accepts a request struct.
This commit is contained in:
Daniel Nephin 2020-11-30 12:53:46 -05:00
parent d7d081f402
commit 1c8eca2bfd
8 changed files with 106 additions and 98 deletions

View File

@ -1890,10 +1890,10 @@ func (a *Agent) readPersistedServiceConfigs() (map[structs.ServiceID]*structs.Se
return out, nil return out, nil
} }
// AddServiceAndReplaceChecks is used to add a service entry and its check. Any check for this service missing from chkTypes will be deleted. // AddService is used to add a service entry and its check. Any check for this service missing from chkTypes will be deleted.
// This entry is persistent and the agent will make a best effort to // This entry is persistent and the agent will make a best effort to
// ensure it is registered // ensure it is registered
func (a *Agent) AddServiceAndReplaceChecks(service *structs.NodeService, chkTypes []*structs.CheckType, persist bool, token string, source configSource) error { func (a *Agent) AddService(service *structs.NodeService, chkTypes []*structs.CheckType, persist bool, token string, source configSource) error {
a.stateLock.Lock() a.stateLock.Lock()
defer a.stateLock.Unlock() defer a.stateLock.Unlock()
return a.addServiceLocked(&addServiceRequest{ return a.addServiceLocked(&addServiceRequest{
@ -1910,10 +1910,12 @@ func (a *Agent) AddServiceAndReplaceChecks(service *structs.NodeService, chkType
}) })
} }
// AddService is used to add a service entry. // AddServiceFromSource is used to add a service entry.
// This entry is persistent and the agent will make a best effort to // This entry is persistent and the agent will make a best effort to
// ensure it is registered // ensure it is registered.
func (a *Agent) AddService(service *structs.NodeService, chkTypes []*structs.CheckType, persist bool, token string, source configSource) error { // TODO: move to _test.go
// Deprecated: use AddService
func (a *Agent) AddServiceFromSource(service *structs.NodeService, chkTypes []*structs.CheckType, persist bool, token string, source configSource) error {
a.stateLock.Lock() a.stateLock.Lock()
defer a.stateLock.Unlock() defer a.stateLock.Unlock()
return a.addServiceLocked(&addServiceRequest{ return a.addServiceLocked(&addServiceRequest{

View File

@ -10,6 +10,12 @@ import (
"github.com/hashicorp/go-memdb" "github.com/hashicorp/go-memdb"
"github.com/mitchellh/hashstructure" "github.com/mitchellh/hashstructure"
"github.com/hashicorp/go-bexpr"
"github.com/hashicorp/serf/coordinate"
"github.com/hashicorp/serf/serf"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/hashicorp/consul/acl" "github.com/hashicorp/consul/acl"
cachetype "github.com/hashicorp/consul/agent/cache-types" cachetype "github.com/hashicorp/consul/agent/cache-types"
"github.com/hashicorp/consul/agent/debug" "github.com/hashicorp/consul/agent/debug"
@ -22,11 +28,6 @@ import (
"github.com/hashicorp/consul/logging" "github.com/hashicorp/consul/logging"
"github.com/hashicorp/consul/logging/monitor" "github.com/hashicorp/consul/logging/monitor"
"github.com/hashicorp/consul/types" "github.com/hashicorp/consul/types"
"github.com/hashicorp/go-bexpr"
"github.com/hashicorp/serf/coordinate"
"github.com/hashicorp/serf/serf"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
) )
type Self struct { type Self struct {
@ -992,22 +993,22 @@ func (s *HTTPHandlers) AgentRegisterService(resp http.ResponseWriter, req *http.
} }
if replaceExistingChecks { if replaceExistingChecks {
if err := s.agent.AddServiceAndReplaceChecks(ns, chkTypes, true, token, ConfigSourceRemote); err != nil { if err := s.agent.AddService(ns, chkTypes, true, token, ConfigSourceRemote); err != nil {
return nil, err return nil, err
} }
} else { } else {
if err := s.agent.AddService(ns, chkTypes, true, token, ConfigSourceRemote); err != nil { if err := s.agent.AddServiceFromSource(ns, chkTypes, true, token, ConfigSourceRemote); err != nil {
return nil, err return nil, err
} }
} }
// Add sidecar. // Add sidecar.
if sidecar != nil { if sidecar != nil {
if replaceExistingChecks { if replaceExistingChecks {
if err := s.agent.AddServiceAndReplaceChecks(sidecar, sidecarChecks, true, sidecarToken, ConfigSourceRemote); err != nil { if err := s.agent.AddService(sidecar, sidecarChecks, true, sidecarToken, ConfigSourceRemote); err != nil {
return nil, err return nil, err
} }
} else { } else {
if err := s.agent.AddService(sidecar, sidecarChecks, true, sidecarToken, ConfigSourceRemote); err != nil { if err := s.agent.AddServiceFromSource(sidecar, sidecarChecks, true, sidecarToken, ConfigSourceRemote); err != nil {
return nil, err return nil, err
} }
} }

View File

@ -18,6 +18,12 @@ import (
"testing" "testing"
"time" "time"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/serf/serf"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/time/rate"
"github.com/hashicorp/consul/acl" "github.com/hashicorp/consul/acl"
"github.com/hashicorp/consul/agent/config" "github.com/hashicorp/consul/agent/config"
"github.com/hashicorp/consul/agent/connect" "github.com/hashicorp/consul/agent/connect"
@ -34,11 +40,6 @@ import (
"github.com/hashicorp/consul/sdk/testutil/retry" "github.com/hashicorp/consul/sdk/testutil/retry"
"github.com/hashicorp/consul/testrpc" "github.com/hashicorp/consul/testrpc"
"github.com/hashicorp/consul/types" "github.com/hashicorp/consul/types"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/serf/serf"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/time/rate"
) )
func makeReadOnlyAgentACL(t *testing.T, srv *HTTPHandlers) string { func makeReadOnlyAgentACL(t *testing.T, srv *HTTPHandlers) string {
@ -735,21 +736,21 @@ func TestAgent_HealthServiceByID(t *testing.T) {
ID: "mysql", ID: "mysql",
Service: "mysql", Service: "mysql",
} }
if err := a.AddService(service, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(service, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
service = &structs.NodeService{ service = &structs.NodeService{
ID: "mysql2", ID: "mysql2",
Service: "mysql2", Service: "mysql2",
} }
if err := a.AddService(service, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(service, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
service = &structs.NodeService{ service = &structs.NodeService{
ID: "mysql3", ID: "mysql3",
Service: "mysql3", Service: "mysql3",
} }
if err := a.AddService(service, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(service, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -933,42 +934,42 @@ func TestAgent_HealthServiceByName(t *testing.T) {
ID: "mysql1", ID: "mysql1",
Service: "mysql-pool-r", Service: "mysql-pool-r",
} }
if err := a.AddService(service, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(service, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
service = &structs.NodeService{ service = &structs.NodeService{
ID: "mysql2", ID: "mysql2",
Service: "mysql-pool-r", Service: "mysql-pool-r",
} }
if err := a.AddService(service, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(service, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
service = &structs.NodeService{ service = &structs.NodeService{
ID: "mysql3", ID: "mysql3",
Service: "mysql-pool-rw", Service: "mysql-pool-rw",
} }
if err := a.AddService(service, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(service, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
service = &structs.NodeService{ service = &structs.NodeService{
ID: "mysql4", ID: "mysql4",
Service: "mysql-pool-rw", Service: "mysql-pool-rw",
} }
if err := a.AddService(service, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(service, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
service = &structs.NodeService{ service = &structs.NodeService{
ID: "httpd1", ID: "httpd1",
Service: "httpd", Service: "httpd",
} }
if err := a.AddService(service, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(service, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
service = &structs.NodeService{ service = &structs.NodeService{
ID: "httpd2", ID: "httpd2",
Service: "httpd", Service: "httpd",
} }
if err := a.AddService(service, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(service, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -1180,13 +1181,13 @@ func TestAgent_HealthServicesACLEnforcement(t *testing.T) {
ID: "mysql1", ID: "mysql1",
Service: "mysql", Service: "mysql",
} }
require.NoError(t, a.AddService(service, nil, false, "", ConfigSourceLocal)) require.NoError(t, a.AddServiceFromSource(service, nil, false, "", ConfigSourceLocal))
service = &structs.NodeService{ service = &structs.NodeService{
ID: "foo1", ID: "foo1",
Service: "foo", Service: "foo",
} }
require.NoError(t, a.AddService(service, nil, false, "", ConfigSourceLocal)) require.NoError(t, a.AddServiceFromSource(service, nil, false, "", ConfigSourceLocal))
// no token // no token
t.Run("no-token-health-by-id", func(t *testing.T) { t.Run("no-token-health-by-id", func(t *testing.T) {
@ -4014,10 +4015,10 @@ func testAgent_RegisterServiceDeregisterService_Sidecar(t *testing.T, extraHCL s
testrpc.WaitForLeader(t, a.RPC, "dc1") testrpc.WaitForLeader(t, a.RPC, "dc1")
if tt.preRegister != nil { if tt.preRegister != nil {
require.NoError(a.AddService(tt.preRegister, nil, false, "", ConfigSourceLocal)) require.NoError(a.AddServiceFromSource(tt.preRegister, nil, false, "", ConfigSourceLocal))
} }
if tt.preRegister2 != nil { if tt.preRegister2 != nil {
require.NoError(a.AddService(tt.preRegister2, nil, false, "", ConfigSourceLocal)) require.NoError(a.AddServiceFromSource(tt.preRegister2, nil, false, "", ConfigSourceLocal))
} }
// Create an ACL token with require policy // Create an ACL token with require policy
@ -4319,7 +4320,7 @@ func TestAgent_DeregisterService(t *testing.T) {
ID: "test", ID: "test",
Service: "test", Service: "test",
} }
if err := a.AddService(service, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(service, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -4351,7 +4352,7 @@ func TestAgent_DeregisterService_ACLDeny(t *testing.T) {
ID: "test", ID: "test",
Service: "test", Service: "test",
} }
if err := a.AddService(service, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(service, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -4429,7 +4430,7 @@ func TestAgent_ServiceMaintenance_Enable(t *testing.T) {
ID: "test", ID: "test",
Service: "test", Service: "test",
} }
if err := a.AddService(service, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(service, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -4476,7 +4477,7 @@ func TestAgent_ServiceMaintenance_Disable(t *testing.T) {
ID: "test", ID: "test",
Service: "test", Service: "test",
} }
if err := a.AddService(service, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(service, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -4517,7 +4518,7 @@ func TestAgent_ServiceMaintenance_ACLDeny(t *testing.T) {
ID: "test", ID: "test",
Service: "test", Service: "test",
} }
if err := a.AddService(service, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(service, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }

View File

@ -529,7 +529,7 @@ func testAgent_AddService(t *testing.T, extraHCL string) {
t.Run(tt.desc, func(t *testing.T) { t.Run(tt.desc, func(t *testing.T) {
// check the service registration // check the service registration
t.Run(tt.srv.ID, func(t *testing.T) { t.Run(tt.srv.ID, func(t *testing.T) {
err := a.AddService(tt.srv, tt.chkTypes, false, "", ConfigSourceLocal) err := a.AddServiceFromSource(tt.srv, tt.chkTypes, false, "", ConfigSourceLocal)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -638,7 +638,7 @@ func testAgent_AddServices_AliasUpdateCheckNotReverted(t *testing.T, extraHCL st
chkTypes, err := service.CheckTypes() chkTypes, err := service.CheckTypes()
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, a.AddService(ns, chkTypes, false, service.Token, ConfigSourceLocal)) require.NoError(t, a.AddServiceFromSource(ns, chkTypes, false, service.Token, ConfigSourceLocal))
} }
retry.Run(t, func(r *retry.R) { retry.Run(t, func(r *retry.R) {
@ -665,7 +665,7 @@ func test_createAlias(t *testing.T, agent *TestAgent, chk *structs.CheckType, ex
if chk.CheckID == "" { if chk.CheckID == "" {
chk.CheckID = types.CheckID(fmt.Sprintf("check-%d", serviceNum)) chk.CheckID = types.CheckID(fmt.Sprintf("check-%d", serviceNum))
} }
err := agent.AddService(srv, []*structs.CheckType{chk}, false, "", ConfigSourceLocal) err := agent.AddServiceFromSource(srv, []*structs.CheckType{chk}, false, "", ConfigSourceLocal)
assert.NoError(t, err) assert.NoError(t, err)
return func(r *retry.R) { return func(r *retry.R) {
t.Helper() t.Helper()
@ -712,7 +712,7 @@ func TestAgent_CheckAliasRPC(t *testing.T) {
// We ensure to not block and update Agent's index // We ensure to not block and update Agent's index
srv.Tags = []string{fmt.Sprintf("tag-%s", time.Now())} srv.Tags = []string{fmt.Sprintf("tag-%s", time.Now())}
assert.NoError(t, a.waitForUp()) assert.NoError(t, a.waitForUp())
err := a.AddService(srv, []*structs.CheckType{}, false, "", ConfigSourceLocal) err := a.AddServiceFromSource(srv, []*structs.CheckType{}, false, "", ConfigSourceLocal)
assert.NoError(t, err) assert.NoError(t, err)
} }
shutdownAgent := func() { shutdownAgent := func() {
@ -727,7 +727,7 @@ func TestAgent_CheckAliasRPC(t *testing.T) {
testrpc.WaitForTestAgent(t, a.RPC, "dc1") testrpc.WaitForTestAgent(t, a.RPC, "dc1")
assert.NoError(t, a.waitForUp()) assert.NoError(t, a.waitForUp())
err := a.AddService(srv, []*structs.CheckType{}, false, "", ConfigSourceLocal) err := a.AddServiceFromSource(srv, []*structs.CheckType{}, false, "", ConfigSourceLocal)
assert.NoError(t, err) assert.NoError(t, err)
retry.Run(t, func(r *retry.R) { retry.Run(t, func(r *retry.R) {
@ -832,12 +832,12 @@ func testAgent_AddServiceNoExec(t *testing.T, extraHCL string) {
Interval: 15 * time.Second, Interval: 15 * time.Second,
} }
err := a.AddService(srv, []*structs.CheckType{chk}, false, "", ConfigSourceLocal) err := a.AddServiceFromSource(srv, []*structs.CheckType{chk}, false, "", ConfigSourceLocal)
if err == nil || !strings.Contains(err.Error(), "Scripts are disabled on this agent") { if err == nil || !strings.Contains(err.Error(), "Scripts are disabled on this agent") {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
err = a.AddService(srv, []*structs.CheckType{chk}, false, "", ConfigSourceRemote) err = a.AddServiceFromSource(srv, []*structs.CheckType{chk}, false, "", ConfigSourceRemote)
if err == nil || !strings.Contains(err.Error(), "Scripts are disabled on this agent") { if err == nil || !strings.Contains(err.Error(), "Scripts are disabled on this agent") {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -879,7 +879,7 @@ func testAgent_AddServiceNoRemoteExec(t *testing.T, extraHCL string) {
Interval: 15 * time.Second, Interval: 15 * time.Second,
} }
err := a.AddService(srv, []*structs.CheckType{chk}, false, "", ConfigSourceRemote) err := a.AddServiceFromSource(srv, []*structs.CheckType{chk}, false, "", ConfigSourceRemote)
if err == nil || !strings.Contains(err.Error(), "Scripts are disabled on this agent") { if err == nil || !strings.Contains(err.Error(), "Scripts are disabled on this agent") {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -932,7 +932,7 @@ func TestCacheRateLimit(t *testing.T) {
Address: fmt.Sprintf("10.0.1.%d", i%255), Address: fmt.Sprintf("10.0.1.%d", i%255),
} }
err := a.AddService(srv, []*structs.CheckType{}, false, "", ConfigSourceRemote) err := a.AddServiceFromSource(srv, []*structs.CheckType{}, false, "", ConfigSourceRemote)
require.Nil(t, err) require.Nil(t, err)
} }
@ -1007,7 +1007,7 @@ func TestAddServiceIPv4TaggedDefault(t *testing.T) {
Address: "10.0.1.2", Address: "10.0.1.2",
} }
err := a.AddService(srv, []*structs.CheckType{}, false, "", ConfigSourceRemote) err := a.AddServiceFromSource(srv, []*structs.CheckType{}, false, "", ConfigSourceRemote)
require.Nil(t, err) require.Nil(t, err)
ns := a.State.Service(structs.NewServiceID("my_service_id", nil)) ns := a.State.Service(structs.NewServiceID("my_service_id", nil))
@ -1040,7 +1040,7 @@ func TestAddServiceIPv6TaggedDefault(t *testing.T) {
Address: "::5", Address: "::5",
} }
err := a.AddService(srv, []*structs.CheckType{}, false, "", ConfigSourceRemote) err := a.AddServiceFromSource(srv, []*structs.CheckType{}, false, "", ConfigSourceRemote)
require.Nil(t, err) require.Nil(t, err)
ns := a.State.Service(structs.NewServiceID("my_service_id", nil)) ns := a.State.Service(structs.NewServiceID("my_service_id", nil))
@ -1079,7 +1079,7 @@ func TestAddServiceIPv4TaggedSet(t *testing.T) {
}, },
} }
err := a.AddService(srv, []*structs.CheckType{}, false, "", ConfigSourceRemote) err := a.AddServiceFromSource(srv, []*structs.CheckType{}, false, "", ConfigSourceRemote)
require.Nil(t, err) require.Nil(t, err)
ns := a.State.Service(structs.NewServiceID("my_service_id", nil)) ns := a.State.Service(structs.NewServiceID("my_service_id", nil))
@ -1118,7 +1118,7 @@ func TestAddServiceIPv6TaggedSet(t *testing.T) {
}, },
} }
err := a.AddService(srv, []*structs.CheckType{}, false, "", ConfigSourceRemote) err := a.AddServiceFromSource(srv, []*structs.CheckType{}, false, "", ConfigSourceRemote)
require.Nil(t, err) require.Nil(t, err)
ns := a.State.Service(structs.NewServiceID("my_service_id", nil)) ns := a.State.Service(structs.NewServiceID("my_service_id", nil))
@ -1173,7 +1173,7 @@ func testAgent_RemoveService(t *testing.T, extraHCL string) {
} }
chkTypes := []*structs.CheckType{{TTL: time.Minute}} chkTypes := []*structs.CheckType{{TTL: time.Minute}}
if err := a.AddService(srv, chkTypes, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(srv, chkTypes, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -1208,7 +1208,7 @@ func testAgent_RemoveService(t *testing.T, extraHCL string) {
{TTL: time.Minute}, {TTL: time.Minute},
{TTL: 30 * time.Second}, {TTL: 30 * time.Second},
} }
if err := a.AddService(srv, chkTypes, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(srv, chkTypes, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -1222,7 +1222,7 @@ func testAgent_RemoveService(t *testing.T, extraHCL string) {
{TTL: time.Minute}, {TTL: time.Minute},
{TTL: 30 * time.Second}, {TTL: 30 * time.Second},
} }
if err := a.AddService(srv, chkTypes, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(srv, chkTypes, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -1294,7 +1294,7 @@ func testAgent_RemoveServiceRemovesAllChecks(t *testing.T, extraHCL string) {
} }
// register service with chk1 // register service with chk1
if err := a.AddService(svc, []*structs.CheckType{chk1}, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(svc, []*structs.CheckType{chk1}, false, "", ConfigSourceLocal); err != nil {
t.Fatal("Failed to register service", err) t.Fatal("Failed to register service", err)
} }
@ -1302,7 +1302,7 @@ func testAgent_RemoveServiceRemovesAllChecks(t *testing.T, extraHCL string) {
requireCheckExists(t, a, "chk1") requireCheckExists(t, a, "chk1")
// update the service with chk2 // update the service with chk2
if err := a.AddService(svc, []*structs.CheckType{chk2}, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(svc, []*structs.CheckType{chk2}, false, "", ConfigSourceLocal); err != nil {
t.Fatal("Failed to update service", err) t.Fatal("Failed to update service", err)
} }
@ -1359,7 +1359,7 @@ func verifyIndexChurn(t *testing.T, tags []string) {
Tags: tags, Tags: tags,
Weights: weights, Weights: weights,
} }
if err := a.AddService(svc, nil, true, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(svc, nil, true, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -1767,7 +1767,7 @@ func TestAgent_RestoreServiceWithAliasCheck(t *testing.T) {
registerServicesAndChecks := func(t *testing.T, a *TestAgent) { registerServicesAndChecks := func(t *testing.T, a *TestAgent) {
// add one persistent service with a simple check // add one persistent service with a simple check
require.NoError(t, a.AddService( require.NoError(t, a.AddServiceFromSource(
&structs.NodeService{ &structs.NodeService{
ID: "ping", ID: "ping",
Service: "ping", Service: "ping",
@ -1786,7 +1786,7 @@ func TestAgent_RestoreServiceWithAliasCheck(t *testing.T) {
// add one persistent sidecar service with an alias check in the manner // add one persistent sidecar service with an alias check in the manner
// of how sidecar_service would add it // of how sidecar_service would add it
require.NoError(t, a.AddService( require.NoError(t, a.AddServiceFromSource(
&structs.NodeService{ &structs.NodeService{
ID: "ping-sidecar-proxy", ID: "ping-sidecar-proxy",
Service: "ping-sidecar-proxy", Service: "ping-sidecar-proxy",
@ -2276,7 +2276,7 @@ func testAgent_PersistService(t *testing.T, extraHCL string) {
file := filepath.Join(a.Config.DataDir, servicesDir, stringHash(svc.ID)) file := filepath.Join(a.Config.DataDir, servicesDir, stringHash(svc.ID))
// Check is not persisted unless requested // Check is not persisted unless requested
if err := a.AddService(svc, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(svc, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
if _, err := os.Stat(file); err == nil { if _, err := os.Stat(file); err == nil {
@ -2284,7 +2284,7 @@ func testAgent_PersistService(t *testing.T, extraHCL string) {
} }
// Persists to file if requested // Persists to file if requested
if err := a.AddService(svc, nil, true, "mytoken", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(svc, nil, true, "mytoken", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
if _, err := os.Stat(file); err != nil { if _, err := os.Stat(file); err != nil {
@ -2308,7 +2308,7 @@ func testAgent_PersistService(t *testing.T, extraHCL string) {
// Updates service definition on disk // Updates service definition on disk
svc.Port = 8001 svc.Port = 8001
if err := a.AddService(svc, nil, true, "mytoken", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(svc, nil, true, "mytoken", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
expected, err = json.Marshal(persistedService{ expected, err = json.Marshal(persistedService{
@ -2431,7 +2431,7 @@ func testAgent_PurgeService(t *testing.T, extraHCL string) {
} }
file := filepath.Join(a.Config.DataDir, servicesDir, stringHash(svc.ID)) file := filepath.Join(a.Config.DataDir, servicesDir, stringHash(svc.ID))
if err := a.AddService(svc, nil, true, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(svc, nil, true, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
// Exists // Exists
@ -2448,7 +2448,7 @@ func testAgent_PurgeService(t *testing.T, extraHCL string) {
} }
// Re-add the service // Re-add the service
if err := a.AddService(svc, nil, true, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(svc, nil, true, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -2494,7 +2494,7 @@ func testAgent_PurgeServiceOnDuplicate(t *testing.T, extraHCL string) {
} }
// First persist the service // First persist the service
require.NoError(t, a.AddService(svc1, nil, true, "", ConfigSourceLocal)) require.NoError(t, a.AddServiceFromSource(svc1, nil, true, "", ConfigSourceLocal))
a.Shutdown() a.Shutdown()
// Try bringing the agent back up with the service already // Try bringing the agent back up with the service already
@ -2742,9 +2742,9 @@ func TestAgent_DeregisterPersistedSidecarAfterRestart(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
// First persist the check // First persist the check
err = a.AddService(srv, nil, true, "", ConfigSourceLocal) err = a.AddServiceFromSource(srv, nil, true, "", ConfigSourceLocal)
require.NoError(t, err) require.NoError(t, err)
err = a.AddService(connectSrv, nil, true, "", ConfigSourceLocal) err = a.AddServiceFromSource(connectSrv, nil, true, "", ConfigSourceLocal)
require.NoError(t, err) require.NoError(t, err)
// check both services were registered // check both services were registered
@ -2814,7 +2814,7 @@ func TestAgent_unloadChecks(t *testing.T) {
Tags: []string{"foo"}, Tags: []string{"foo"},
Port: 8000, Port: 8000,
} }
if err := a.AddService(svc, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(svc, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -3093,7 +3093,7 @@ func testAgent_unloadServices(t *testing.T, extraHCL string) {
} }
// Register the service // Register the service
if err := a.AddService(svc, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(svc, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -3125,7 +3125,7 @@ func TestAgent_Service_MaintenanceMode(t *testing.T) {
} }
// Register the service // Register the service
if err := a.AddService(svc, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(svc, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -3206,7 +3206,7 @@ func TestAgent_Service_Reap(t *testing.T) {
} }
// Register the service. // Register the service.
if err := a.AddService(svc, chkTypes, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(svc, chkTypes, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -3263,7 +3263,7 @@ func TestAgent_Service_NoReap(t *testing.T) {
} }
// Register the service. // Register the service.
if err := a.AddService(svc, chkTypes, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(svc, chkTypes, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -3310,7 +3310,7 @@ func testAgent_AddService_restoresSnapshot(t *testing.T, extraHCL string) {
Tags: []string{"foo"}, Tags: []string{"foo"},
Port: 8000, Port: 8000,
} }
require.NoError(t, a.AddService(svc, nil, false, "", ConfigSourceLocal)) require.NoError(t, a.AddServiceFromSource(svc, nil, false, "", ConfigSourceLocal))
// Register a check // Register a check
check1 := &structs.HealthCheck{ check1 := &structs.HealthCheck{
@ -3325,7 +3325,7 @@ func testAgent_AddService_restoresSnapshot(t *testing.T, extraHCL string) {
// Re-registering the service preserves the state of the check // Re-registering the service preserves the state of the check
chkTypes := []*structs.CheckType{{TTL: 30 * time.Second}} chkTypes := []*structs.CheckType{{TTL: 30 * time.Second}}
require.NoError(t, a.AddService(svc, chkTypes, false, "", ConfigSourceLocal)) require.NoError(t, a.AddServiceFromSource(svc, chkTypes, false, "", ConfigSourceLocal))
check := requireCheckExists(t, a, "service:redis") check := requireCheckExists(t, a, "service:redis")
require.Equal(t, api.HealthPassing, check.Status) require.Equal(t, api.HealthPassing, check.Status)
} }
@ -3346,7 +3346,7 @@ func TestAgent_AddCheck_restoresSnapshot(t *testing.T) {
Tags: []string{"foo"}, Tags: []string{"foo"},
Port: 8000, Port: 8000,
} }
if err := a.AddService(svc, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(svc, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -3431,7 +3431,7 @@ func TestAgent_checkStateSnapshot(t *testing.T) {
Tags: []string{"foo"}, Tags: []string{"foo"},
Port: 8000, Port: 8000,
} }
if err := a.AddService(svc, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(svc, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -4250,7 +4250,7 @@ func TestAgent_RerouteExistingHTTPChecks(t *testing.T) {
TLSSkipVerify: true, TLSSkipVerify: true,
}, },
} }
if err := a.AddService(svc, chks, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(svc, chks, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("failed to add svc: %v", err) t.Fatalf("failed to add svc: %v", err)
} }
@ -4273,7 +4273,7 @@ func TestAgent_RerouteExistingHTTPChecks(t *testing.T) {
}, },
}, },
} }
if err := a.AddService(proxy, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(proxy, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("failed to add svc: %v", err) t.Fatalf("failed to add svc: %v", err)
} }
@ -4326,7 +4326,7 @@ func TestAgent_RerouteExistingHTTPChecks(t *testing.T) {
}, },
}, },
} }
if err := a.AddService(proxy, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(proxy, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("failed to add svc: %v", err) t.Fatalf("failed to add svc: %v", err)
} }
@ -4369,7 +4369,7 @@ func TestAgent_RerouteNewHTTPChecks(t *testing.T) {
Address: "localhost", Address: "localhost",
Port: 8080, Port: 8080,
} }
if err := a.AddService(svc, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(svc, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("failed to add svc: %v", err) t.Fatalf("failed to add svc: %v", err)
} }
@ -4391,7 +4391,7 @@ func TestAgent_RerouteNewHTTPChecks(t *testing.T) {
}, },
}, },
} }
if err := a.AddService(proxy, nil, false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(proxy, nil, false, "", ConfigSourceLocal); err != nil {
t.Fatalf("failed to add svc: %v", err) t.Fatalf("failed to add svc: %v", err)
} }

View File

@ -5,12 +5,13 @@ import (
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/require"
"github.com/hashicorp/consul/agent/cache" "github.com/hashicorp/consul/agent/cache"
cachetype "github.com/hashicorp/consul/agent/cache-types" cachetype "github.com/hashicorp/consul/agent/cache-types"
"github.com/hashicorp/consul/agent/checks" "github.com/hashicorp/consul/agent/checks"
"github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/testrpc" "github.com/hashicorp/consul/testrpc"
"github.com/stretchr/testify/require"
) )
// Integration test for ServiceHTTPBasedChecks cache-type // Integration test for ServiceHTTPBasedChecks cache-type
@ -62,7 +63,7 @@ func TestAgent_ServiceHTTPChecksNotification(t *testing.T) {
}, },
} }
// Adding TTL type should lead to a timeout, since only HTTP-based checks are watched // Adding TTL type should lead to a timeout, since only HTTP-based checks are watched
if err := a.AddService(&service, chkTypes[2:], false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(&service, chkTypes[2:], false, "", ConfigSourceLocal); err != nil {
t.Fatalf("failed to add service: %v", err) t.Fatalf("failed to add service: %v", err)
} }
@ -74,7 +75,7 @@ func TestAgent_ServiceHTTPChecksNotification(t *testing.T) {
} }
// Adding service with HTTP checks should lead notification for them // Adding service with HTTP checks should lead notification for them
if err := a.AddService(&service, chkTypes[0:2], false, "", ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(&service, chkTypes[0:2], false, "", ConfigSourceLocal); err != nil {
t.Fatalf("failed to add service: %v", err) t.Fatalf("failed to add service: %v", err)
} }

View File

@ -8,10 +8,11 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/stretchr/testify/require"
"github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/sdk/testutil/retry" "github.com/hashicorp/consul/sdk/testutil/retry"
"github.com/hashicorp/consul/testrpc" "github.com/hashicorp/consul/testrpc"
"github.com/stretchr/testify/require"
) )
func TestServiceManager_RegisterService(t *testing.T) { func TestServiceManager_RegisterService(t *testing.T) {
@ -47,7 +48,7 @@ func TestServiceManager_RegisterService(t *testing.T) {
Port: 8000, Port: 8000,
EnterpriseMeta: *structs.DefaultEnterpriseMeta(), EnterpriseMeta: *structs.DefaultEnterpriseMeta(),
} }
require.NoError(a.AddService(svc, nil, false, "", ConfigSourceLocal)) require.NoError(a.AddServiceFromSource(svc, nil, false, "", ConfigSourceLocal))
// Verify both the service and sidecar. // Verify both the service and sidecar.
redisService := a.State.Service(structs.NewServiceID("redis", nil)) redisService := a.State.Service(structs.NewServiceID("redis", nil))
@ -118,7 +119,7 @@ func TestServiceManager_RegisterSidecar(t *testing.T) {
}, },
EnterpriseMeta: *structs.DefaultEnterpriseMeta(), EnterpriseMeta: *structs.DefaultEnterpriseMeta(),
} }
require.NoError(a.AddService(svc, nil, false, "", ConfigSourceLocal)) require.NoError(a.AddServiceFromSource(svc, nil, false, "", ConfigSourceLocal))
// Verify sidecar got global config loaded // Verify sidecar got global config loaded
sidecarService := a.State.Service(structs.NewServiceID("web-sidecar-proxy", nil)) sidecarService := a.State.Service(structs.NewServiceID("web-sidecar-proxy", nil))
@ -191,7 +192,7 @@ func TestServiceManager_RegisterMeshGateway(t *testing.T) {
EnterpriseMeta: *structs.DefaultEnterpriseMeta(), EnterpriseMeta: *structs.DefaultEnterpriseMeta(),
} }
require.NoError(a.AddService(svc, nil, false, "", ConfigSourceLocal)) require.NoError(a.AddServiceFromSource(svc, nil, false, "", ConfigSourceLocal))
// Verify gateway got global config loaded // Verify gateway got global config loaded
gateway := a.State.Service(structs.NewServiceID("mesh-gateway", nil)) gateway := a.State.Service(structs.NewServiceID("mesh-gateway", nil))
@ -251,7 +252,7 @@ func TestServiceManager_RegisterTerminatingGateway(t *testing.T) {
EnterpriseMeta: *structs.DefaultEnterpriseMeta(), EnterpriseMeta: *structs.DefaultEnterpriseMeta(),
} }
require.NoError(a.AddService(svc, nil, false, "", ConfigSourceLocal)) require.NoError(a.AddServiceFromSource(svc, nil, false, "", ConfigSourceLocal))
// Verify gateway got global config loaded // Verify gateway got global config loaded
gateway := a.State.Service(structs.NewServiceID("terminating-gateway", nil)) gateway := a.State.Service(structs.NewServiceID("terminating-gateway", nil))
@ -386,12 +387,12 @@ func TestServiceManager_PersistService_API(t *testing.T) {
configFile := filepath.Join(a.Config.DataDir, serviceConfigDir, svcID.StringHash()) configFile := filepath.Join(a.Config.DataDir, serviceConfigDir, svcID.StringHash())
// Service is not persisted unless requested, but we always persist service configs. // Service is not persisted unless requested, but we always persist service configs.
require.NoError(a.AddService(svc, nil, false, "", ConfigSourceRemote)) require.NoError(a.AddServiceFromSource(svc, nil, false, "", ConfigSourceRemote))
requireFileIsAbsent(t, svcFile) requireFileIsAbsent(t, svcFile)
requireFileIsPresent(t, configFile) requireFileIsPresent(t, configFile)
// Persists to file if requested // Persists to file if requested
require.NoError(a.AddService(svc, nil, true, "mytoken", ConfigSourceRemote)) require.NoError(a.AddServiceFromSource(svc, nil, true, "mytoken", ConfigSourceRemote))
requireFileIsPresent(t, svcFile) requireFileIsPresent(t, svcFile)
requireFileIsPresent(t, configFile) requireFileIsPresent(t, configFile)
@ -432,7 +433,7 @@ func TestServiceManager_PersistService_API(t *testing.T) {
// Updates service definition on disk // Updates service definition on disk
svc.Proxy.LocalServicePort = 8001 svc.Proxy.LocalServicePort = 8001
require.NoError(a.AddService(svc, nil, true, "mytoken", ConfigSourceRemote)) require.NoError(a.AddServiceFromSource(svc, nil, true, "mytoken", ConfigSourceRemote))
requireFileIsPresent(t, svcFile) requireFileIsPresent(t, svcFile)
requireFileIsPresent(t, configFile) requireFileIsPresent(t, configFile)
@ -720,7 +721,7 @@ func TestServiceManager_Disabled(t *testing.T) {
}, },
EnterpriseMeta: *structs.DefaultEnterpriseMeta(), EnterpriseMeta: *structs.DefaultEnterpriseMeta(),
} }
require.NoError(a.AddService(svc, nil, false, "", ConfigSourceLocal)) require.NoError(a.AddServiceFromSource(svc, nil, false, "", ConfigSourceLocal))
// Verify sidecar got global config loaded // Verify sidecar got global config loaded
sidecarService := a.State.Service(structs.NewServiceID("web-sidecar-proxy", nil)) sidecarService := a.State.Service(structs.NewServiceID("web-sidecar-proxy", nil))

View File

@ -5,8 +5,9 @@ import (
"testing" "testing"
"time" "time"
"github.com/hashicorp/consul/agent/structs"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/hashicorp/consul/agent/structs"
) )
func TestAgent_sidecarServiceFromNodeService(t *testing.T) { func TestAgent_sidecarServiceFromNodeService(t *testing.T) {
@ -333,7 +334,7 @@ func TestAgent_sidecarServiceFromNodeService(t *testing.T) {
defer a.Shutdown() defer a.Shutdown()
if tt.preRegister != nil { if tt.preRegister != nil {
err := a.AddService(tt.preRegister.NodeService(), nil, false, "", ConfigSourceLocal) err := a.AddServiceFromSource(tt.preRegister.NodeService(), nil, false, "", ConfigSourceLocal)
require.NoError(err) require.NoError(err)
} }

View File

@ -4,9 +4,10 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/mitchellh/cli"
"github.com/hashicorp/consul/agent" "github.com/hashicorp/consul/agent"
"github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/agent/structs"
"github.com/mitchellh/cli"
) )
func TestMaintCommand_noTabs(t *testing.T) { func TestMaintCommand_noTabs(t *testing.T) {
@ -53,7 +54,7 @@ func TestMaintCommand_NoArgs(t *testing.T) {
ID: "test", ID: "test",
Service: "test", Service: "test",
} }
if err := a.AddService(service, nil, false, "", agent.ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(service, nil, false, "", agent.ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
if err := a.EnableServiceMaintenance(structs.NewServiceID("test", nil), "broken 1", ""); err != nil { if err := a.EnableServiceMaintenance(structs.NewServiceID("test", nil), "broken 1", ""); err != nil {
@ -161,7 +162,7 @@ func TestMaintCommand_EnableServiceMaintenance(t *testing.T) {
ID: "test", ID: "test",
Service: "test", Service: "test",
} }
if err := a.AddService(service, nil, false, "", agent.ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(service, nil, false, "", agent.ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -199,7 +200,7 @@ func TestMaintCommand_DisableServiceMaintenance(t *testing.T) {
ID: "test", ID: "test",
Service: "test", Service: "test",
} }
if err := a.AddService(service, nil, false, "", agent.ConfigSourceLocal); err != nil { if err := a.AddServiceFromSource(service, nil, false, "", agent.ConfigSourceLocal); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }