diff --git a/agent/sidecar_service_test.go b/agent/sidecar_service_test.go index 0cee2e374..3b4a018a5 100644 --- a/agent/sidecar_service_test.go +++ b/agent/sidecar_service_test.go @@ -228,7 +228,7 @@ func TestAgent_SidecarPortFromServiceID(t *testing.T) { enterpriseMeta acl.EnterpriseMeta maxPort int port int - preRegister *structs.ServiceDefinition + preRegister []*structs.ServiceDefinition serviceID string wantPort int wantErr string @@ -242,34 +242,61 @@ func TestAgent_SidecarPortFromServiceID(t *testing.T) { name: "re-registering same sidecar with no port should pick same one", // Allow multiple ports to be sure we get the right one maxPort: 2500, - // Pre register the sidecar we want - preRegister: &structs.ServiceDefinition{ - Kind: structs.ServiceKindConnectProxy, - ID: "web1-sidecar-proxy", - Name: "web-sidecar-proxy", - Port: 2222, + // Pre register the main service and sidecar we want + preRegister: []*structs.ServiceDefinition{{ + Kind: structs.ServiceKindConnectProxy, + ID: "web1", + EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(), + Name: "web", + Port: 2221, + Proxy: &structs.ConnectProxyConfig{ + DestinationServiceName: "web", + DestinationServiceID: "web1", + LocalServiceAddress: "127.0.0.1", + LocalServicePort: 1110, + }, + }, { + Kind: structs.ServiceKindConnectProxy, + ID: "web1-sidecar-proxy", + EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(), + Name: "web-sidecar-proxy", + Port: 2222, Proxy: &structs.ConnectProxyConfig{ DestinationServiceName: "web", DestinationServiceID: "web1", LocalServiceAddress: "127.0.0.1", LocalServicePort: 1111, }, - }, - // Register same again - serviceID: "web1-sidecar-proxy", - wantPort: 2222, // Should claim the same port as before + }}, + // Register same sidecar again + serviceID: "web1-sidecar-proxy", + enterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(), + wantPort: 2222, // Should claim the same port as before }, { name: "all auto ports already taken", - // register another sidecar consuming our 1 and only allocated auto port. - preRegister: &structs.ServiceDefinition{ + // register another service with sidecar consuming our 1 and only allocated auto port. + preRegister: []*structs.ServiceDefinition{{ + Kind: structs.ServiceKindConnectProxy, + ID: "api1", + EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(), + Name: "api", + Port: 2221, + Proxy: &structs.ConnectProxyConfig{ + DestinationServiceName: "api", + DestinationServiceID: "api1", + LocalServiceAddress: "127.0.0.1", + LocalServicePort: 1110, + }, + }, { Kind: structs.ServiceKindConnectProxy, Name: "api-proxy-sidecar", Port: 2222, // Consume the one available auto-port Proxy: &structs.ConnectProxyConfig{ + DestinationServiceID: "api1", DestinationServiceName: "api", }, - }, + }}, wantErr: "none left in the configured range [2222, 2222]", }, { @@ -303,9 +330,11 @@ func TestAgent_SidecarPortFromServiceID(t *testing.T) { a := NewTestAgent(t, hcl) defer a.Shutdown() - if tt.preRegister != nil { - err := a.addServiceFromSource(tt.preRegister.NodeService(), nil, false, "", ConfigSourceLocal) - require.NoError(t, err) + if len(tt.preRegister) > 0 { + for _, s := range tt.preRegister { + err := a.addServiceFromSource(s.NodeService(), nil, false, "", ConfigSourceLocal) + require.NoError(t, err) + } } gotPort, err := a.sidecarPortFromServiceIDLocked(structs.ServiceID{ID: tt.serviceID, EnterpriseMeta: tt.enterpriseMeta})