github.com/Jeffail/benthos/v3@v3.65.0/lib/test/integration/cache/redis_cluster_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/Jeffail/benthos/v3/internal/integration"
    11  	"github.com/Jeffail/benthos/v3/lib/cache"
    12  	"github.com/Jeffail/benthos/v3/lib/log"
    13  	"github.com/Jeffail/benthos/v3/lib/metrics"
    14  	"github.com/ory/dockertest/v3"
    15  	"github.com/ory/dockertest/v3/docker"
    16  	"github.com/stretchr/testify/assert"
    17  	"github.com/stretchr/testify/require"
    18  )
    19  
    20  var _ = registerIntegrationTest("redis_cluster", func(t *testing.T) {
    21  	t.Skip("Skipping as networking often fails for this test")
    22  	t.Parallel()
    23  
    24  	pool, err := dockertest.NewPool("")
    25  	require.NoError(t, err)
    26  	pool.MaxWait = time.Second * 30
    27  
    28  	networks, _ := pool.Client.ListNetworks()
    29  	hostIP := ""
    30  	for _, network := range networks {
    31  		if network.Name == "bridge" {
    32  			hostIP = network.IPAM.Config[0].Gateway
    33  		}
    34  	}
    35  	if runtime.GOOS == "darwin" {
    36  		hostIP = "0.0.0.0"
    37  	}
    38  
    39  	exposedPorts := make([]string, 12)
    40  	portBindings := make(map[docker.Port][]docker.PortBinding, 12)
    41  	for i := 0; i < 6; i++ {
    42  		p1 := fmt.Sprintf("%d/tcp", 7000+i)
    43  		p2 := fmt.Sprintf("%d/tcp", 17000+i)
    44  		exposedPorts[i] = p1
    45  		exposedPorts[i+6] = p2
    46  		portBindings[docker.Port(p1)] = []docker.PortBinding{{HostIP: "", HostPort: p1}}
    47  		portBindings[docker.Port(p2)] = []docker.PortBinding{{HostIP: "", HostPort: p2}}
    48  	}
    49  
    50  	cluster, err := pool.RunWithOptions(&dockertest.RunOptions{
    51  		Name:         "redis-cluster",
    52  		Repository:   "grokzen/redis-cluster",
    53  		Tag:          "6.0.7",
    54  		ExposedPorts: exposedPorts,
    55  		PortBindings: portBindings,
    56  		Env: []string{
    57  			"IP=" + hostIP,
    58  		},
    59  	})
    60  	require.NoError(t, err)
    61  
    62  	t.Cleanup(func() {
    63  		assert.NoError(t, pool.Purge(cluster))
    64  	})
    65  
    66  	clusterURL := ""
    67  	for i := 0; i < 6; i++ {
    68  		clusterURL += fmt.Sprintf("redis://%s:%s/0,", hostIP, fmt.Sprintf("%d", 7000+i))
    69  	}
    70  	clusterURL = strings.TrimSuffix(clusterURL, ",")
    71  
    72  	require.NoError(t, pool.Retry(func() error {
    73  		conf := cache.NewConfig()
    74  		conf.Redis.URL = clusterURL
    75  		conf.Redis.Kind = "cluster"
    76  
    77  		r, cErr := cache.NewRedis(conf, nil, log.Noop(), metrics.Noop())
    78  		if cErr != nil {
    79  			return cErr
    80  		}
    81  		cErr = r.Set("benthos_test_redis_connect", []byte("foo bar"))
    82  		return cErr
    83  	}))
    84  
    85  	template := `
    86  cache_resources:
    87    - label: testcache
    88      redis:
    89        url: $VAR1
    90        kind: cluster
    91        prefix: $ID
    92  `
    93  	suite := integration.CacheTests(
    94  		integration.CacheTestOpenClose(),
    95  		integration.CacheTestMissingKey(),
    96  		integration.CacheTestDoubleAdd(),
    97  		integration.CacheTestDelete(),
    98  		integration.CacheTestGetAndSet(50),
    99  	)
   100  	suite.Run(
   101  		t, template,
   102  		integration.CacheTestOptVarOne(clusterURL),
   103  	)
   104  })