From 21addf79351477c061afdea566dcce175d80a396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Rainone?= <476650+arl@users.noreply.github.com> Date: Mon, 21 Sep 2020 22:24:20 +0200 Subject: [PATCH] pkg/distributor: fix test by looking for loopback interface (#2645) TestDistributor_PushIngestionRateLimiter failed if the system had no interface name in "eth0", "en0" or "lo0". Fix that by looking at the name of the loopback interface in the list of the system's network interfaces, then using that name in the test. Fixes #2356 --- pkg/distributor/distributor_test.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/pkg/distributor/distributor_test.go b/pkg/distributor/distributor_test.go index 7bd7d54231d3..44b1d281ff30 100644 --- a/pkg/distributor/distributor_test.go +++ b/pkg/distributor/distributor_test.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "math/rand" + "net" "net/http" "strconv" "strings" @@ -193,6 +194,23 @@ func TestDistributor_PushIngestionRateLimiter(t *testing.T) { } } +// loopbackInterfaceName search for the name of a loopback interface in the list +// of the system's network interfaces. +func loopbackInterfaceName() (string, error) { + is, err := net.Interfaces() + if err != nil { + return "", fmt.Errorf("can't retrieve loopback interface name: %s", err) + } + + for _, i := range is { + if i.Flags&net.FlagLoopback != 0 { + return i.Name, nil + } + } + + return "", fmt.Errorf("can't retrieve loopback interface name") +} + func prepare(t *testing.T, limits *validation.Limits, kvStore kv.Client) *Distributor { var ( distributorConfig Config @@ -218,10 +236,13 @@ func prepare(t *testing.T, limits *validation.Limits, kvStore kv.Client) *Distri }) } + loopbackName, err := loopbackInterfaceName() + require.NoError(t, err) + distributorConfig.DistributorRing.HeartbeatPeriod = 100 * time.Millisecond distributorConfig.DistributorRing.InstanceID = strconv.Itoa(rand.Int()) distributorConfig.DistributorRing.KVStore.Mock = kvStore - distributorConfig.DistributorRing.InstanceInterfaceNames = []string{"eth0", "en0", "lo0"} + distributorConfig.DistributorRing.InstanceInterfaceNames = []string{loopbackName} distributorConfig.factory = func(addr string) (ring_client.PoolClient, error) { return ingesters[addr], nil }