...

Source file src/github.com/go-kit/kit/sd/consul/integration_test.go

Documentation: github.com/go-kit/kit/sd/consul

     1  //go:build integration
     2  // +build integration
     3  
     4  package consul
     5  
     6  import (
     7  	"io"
     8  	"os"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/go-kit/kit/endpoint"
    13  	"github.com/go-kit/kit/sd"
    14  	"github.com/go-kit/log"
    15  	stdconsul "github.com/hashicorp/consul/api"
    16  )
    17  
    18  func TestIntegration(t *testing.T) {
    19  	consulAddr := os.Getenv("CONSUL_ADDR")
    20  	if consulAddr == "" {
    21  		t.Skip("CONSUL_ADDR not set; skipping integration test")
    22  	}
    23  	stdClient, err := stdconsul.NewClient(&stdconsul.Config{
    24  		Address: consulAddr,
    25  	})
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	client := NewClient(stdClient)
    30  	logger := log.NewLogfmtLogger(os.Stderr)
    31  
    32  	// Produce a fake service registration.
    33  	r := &stdconsul.AgentServiceRegistration{
    34  		ID:                "my-service-ID",
    35  		Name:              "my-service-name",
    36  		Tags:              []string{"alpha", "beta"},
    37  		Port:              12345,
    38  		Address:           "my-address",
    39  		EnableTagOverride: false,
    40  		// skipping check(s)
    41  	}
    42  
    43  	// Build an Instancer on r.Name + r.Tags.
    44  	factory := func(instance string) (endpoint.Endpoint, io.Closer, error) {
    45  		t.Logf("factory invoked for %q", instance)
    46  		return endpoint.Nop, nil, nil
    47  	}
    48  	instancer := NewInstancer(
    49  		client,
    50  		log.With(logger, "component", "instancer"),
    51  		r.Name,
    52  		r.Tags,
    53  		true,
    54  	)
    55  	endpointer := sd.NewEndpointer(
    56  		instancer,
    57  		factory,
    58  		log.With(logger, "component", "endpointer"),
    59  	)
    60  
    61  	time.Sleep(time.Second)
    62  
    63  	// Before we publish, we should have no endpoints.
    64  	endpoints, err := endpointer.Endpoints()
    65  	if err != nil {
    66  		t.Error(err)
    67  	}
    68  	if want, have := 0, len(endpoints); want != have {
    69  		t.Errorf("want %d, have %d", want, have)
    70  	}
    71  
    72  	// Build a registrar for r.
    73  	registrar := NewRegistrar(client, r, log.With(logger, "component", "registrar"))
    74  	registrar.Register()
    75  	defer registrar.Deregister()
    76  
    77  	time.Sleep(time.Second)
    78  
    79  	// Now we should have one active endpoints.
    80  	endpoints, err = endpointer.Endpoints()
    81  	if err != nil {
    82  		t.Error(err)
    83  	}
    84  	if want, have := 1, len(endpoints); want != have {
    85  		t.Errorf("want %d, have %d", want, have)
    86  	}
    87  }
    88  

View as plain text