open-consul/command/connect/proxy/register_test.go

111 lines
2.6 KiB
Go
Raw Normal View History

package proxy
import (
"testing"
"time"
"github.com/hashicorp/consul/agent"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/sdk/testutil/retry"
"github.com/hashicorp/consul/testrpc"
"github.com/stretchr/testify/require"
)
func TestRegisterMonitor_good(t *testing.T) {
t.Parallel()
require := require.New(t)
a := agent.NewTestAgent(t, t.Name(), ``)
defer a.Shutdown()
client := a.Client()
m, service := testMonitor(t, client)
defer m.Close()
// Verify the settings
require.Equal(api.ServiceKindConnectProxy, service.Kind)
Add Proxy Upstreams to Service Definition (#4639) * Refactor Service Definition ProxyDestination. This includes: - Refactoring all internal structs used - Updated tests for both deprecated and new input for: - Agent Services endpoint response - Agent Service endpoint response - Agent Register endpoint - Unmanaged deprecated field - Unmanaged new fields - Managed deprecated upstreams - Managed new - Catalog Register - Unmanaged deprecated field - Unmanaged new fields - Managed deprecated upstreams - Managed new - Catalog Services endpoint response - Catalog Node endpoint response - Catalog Service endpoint response - Updated API tests for all of the above too (both deprecated and new forms of register) TODO: - config package changes for on-disk service definitions - proxy config endpoint - built-in proxy support for new fields * Agent proxy config endpoint updated with upstreams * Config file changes for upstreams. * Add upstream opaque config and update all tests to ensure it works everywhere. * Built in proxy working with new Upstreams config * Command fixes and deprecations * Fix key translation, upstream type defaults and a spate of other subtele bugs found with ned to end test scripts... TODO: tests still failing on one case that needs a fix. I think it's key translation for upstreams nested in Managed proxy struct. * Fix translated keys in API registration. ≈ * Fixes from docs - omit some empty undocumented fields in API - Bring back ServiceProxyDestination in Catalog responses to not break backwards compat - this was removed assuming it was only used internally. * Documentation updates for Upstreams in service definition * Fixes for tests broken by many refactors. * Enable travis on f-connect branch in this branch too. * Add consistent Deprecation comments to ProxyDestination uses * Update version number on deprecation notices, and correct upstream datacenter field with explanation in docs
2018-09-12 16:07:47 +00:00
require.Equal("foo", service.Proxy.DestinationServiceName)
require.Equal("127.0.0.1", service.Address)
require.Equal(1234, service.Port)
// Stop should deregister the service
require.NoError(m.Close())
services, err := client.Agent().Services()
require.NoError(err)
require.NotContains(services, m.serviceID())
}
func TestRegisterMonitor_heartbeat(t *testing.T) {
t.Parallel()
require := require.New(t)
a := agent.NewTestAgent(t, t.Name(), ``)
defer a.Shutdown()
client := a.Client()
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
m, _ := testMonitor(t, client)
defer m.Close()
retry.Run(t, func(r *retry.R) {
// Get the check and verify that it is passing
checks, err := client.Agent().Checks()
require.NoError(err)
require.Contains(checks, m.checkID())
require.Equal("passing", checks[m.checkID()].Status)
// Purposely fail the TTL check, verify it becomes healthy again
require.NoError(client.Agent().FailTTL(m.checkID(), ""))
})
retry.Run(t, func(r *retry.R) {
checks, err := client.Agent().Checks()
if err != nil {
r.Fatalf("err: %s", err)
}
check, ok := checks[m.checkID()]
if !ok {
r.Fatal("check not found")
}
if check.Status != "passing" {
r.Fatalf("check status is bad: %s", check.Status)
}
})
}
// testMonitor creates a RegisterMonitor, configures it, and starts it.
// It waits until the service appears in the catalog and then returns.
func testMonitor(t *testing.T, client *api.Client) (*RegisterMonitor, *api.AgentService) {
// Setup the monitor
m := NewRegisterMonitor()
m.Client = client
m.Service = "foo"
m.LocalAddress = "127.0.0.1"
m.LocalPort = 1234
// We want shorter periods so we can test things
m.ReconcilePeriod = 400 * time.Millisecond
m.TTLPeriod = 200 * time.Millisecond
// Start the monitor
go m.Run()
// The service should be registered
var service *api.AgentService
retry.Run(t, func(r *retry.R) {
services, err := client.Agent().Services()
if err != nil {
r.Fatalf("err: %s", err)
}
var ok bool
service, ok = services[m.serviceID()]
if !ok {
r.Fatal("service not found")
}
})
return m, service
}