1package proxy 2 3import ( 4 "testing" 5 "time" 6 7 "github.com/hashicorp/consul/agent" 8 "github.com/hashicorp/consul/api" 9 "github.com/hashicorp/consul/sdk/testutil" 10 "github.com/hashicorp/consul/sdk/testutil/retry" 11 "github.com/hashicorp/consul/testrpc" 12 "github.com/stretchr/testify/require" 13) 14 15func TestRegisterMonitor_good(t *testing.T) { 16 if testing.Short() { 17 t.Skip("too slow for testing.Short") 18 } 19 20 t.Parallel() 21 require := require.New(t) 22 23 a := agent.NewTestAgent(t, ``) 24 defer a.Shutdown() 25 client := a.Client() 26 27 m, service := testMonitor(t, client) 28 defer m.Close() 29 30 // Verify the settings 31 require.Equal(api.ServiceKindConnectProxy, service.Kind) 32 require.Equal("foo", service.Proxy.DestinationServiceName) 33 require.Equal("127.0.0.1", service.Address) 34 require.Equal(1234, service.Port) 35 36 // Stop should deregister the service 37 require.NoError(m.Close()) 38 services, err := client.Agent().Services() 39 require.NoError(err) 40 require.NotContains(services, m.serviceID()) 41} 42 43func TestRegisterMonitor_heartbeat(t *testing.T) { 44 if testing.Short() { 45 t.Skip("too slow for testing.Short") 46 } 47 48 t.Parallel() 49 50 a := agent.NewTestAgent(t, ``) 51 defer a.Shutdown() 52 client := a.Client() 53 54 testrpc.WaitForTestAgent(t, a.RPC, "dc1") 55 m, _ := testMonitor(t, client) 56 defer m.Close() 57 retry.Run(t, func(r *retry.R) { 58 // Get the check and verify that it is passing 59 checks, err := client.Agent().Checks() 60 require.NoError(r, err) 61 require.Contains(r, checks, m.checkID()) 62 require.Equal(r, "passing", checks[m.checkID()].Status) 63 // Purposely fail the TTL check, verify it becomes healthy again 64 require.NoError(r, client.Agent().FailTTL(m.checkID(), "")) 65 }) 66 67 retry.Run(t, func(r *retry.R) { 68 69 checks, err := client.Agent().Checks() 70 if err != nil { 71 r.Fatalf("err: %s", err) 72 } 73 74 check, ok := checks[m.checkID()] 75 if !ok { 76 r.Fatal("check not found") 77 } 78 79 if check.Status != "passing" { 80 r.Fatalf("check status is bad: %s", check.Status) 81 } 82 }) 83} 84 85// testMonitor creates a RegisterMonitor, configures it, and starts it. 86// It waits until the service appears in the catalog and then returns. 87func testMonitor(t *testing.T, client *api.Client) (*RegisterMonitor, *api.AgentService) { 88 // Setup the monitor 89 m := NewRegisterMonitor(testutil.Logger(t)) 90 m.Client = client 91 m.Service = "foo" 92 m.LocalAddress = "127.0.0.1" 93 m.LocalPort = 1234 94 95 // We want shorter periods so we can test things 96 m.ReconcilePeriod = 400 * time.Millisecond 97 m.TTLPeriod = 200 * time.Millisecond 98 99 // Start the monitor 100 go m.Run() 101 102 // The service should be registered 103 var service *api.AgentService 104 retry.Run(t, func(r *retry.R) { 105 services, err := client.Agent().Services() 106 if err != nil { 107 r.Fatalf("err: %s", err) 108 } 109 110 var ok bool 111 service, ok = services[m.serviceID()] 112 if !ok { 113 r.Fatal("service not found") 114 } 115 }) 116 117 return m, service 118} 119