github.com/Jeffail/benthos/v3@v3.65.0/lib/test/integration/cache/redis_failover_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_failover", 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  	net, err := pool.CreateNetwork("redis-sentinel")
    40  	require.NoError(t, err)
    41  
    42  	t.Cleanup(func() {
    43  		pool.RemoveNetwork(net)
    44  	})
    45  
    46  	master, err := pool.RunWithOptions(&dockertest.RunOptions{
    47  		Name:         "redis-master",
    48  		Repository:   "bitnami/redis",
    49  		Tag:          "6.0.9",
    50  		Networks:     []*dockertest.Network{net},
    51  		ExposedPorts: []string{"6379/tcp"},
    52  		PortBindings: map[docker.Port][]docker.PortBinding{
    53  			"6379/tcp": {{HostIP: "", HostPort: "6379/tcp"}},
    54  		},
    55  		Env: []string{
    56  			"ALLOW_EMPTY_PASSWORD=yes",
    57  		},
    58  	})
    59  	require.NoError(t, err)
    60  
    61  	sentinel, err := pool.RunWithOptions(&dockertest.RunOptions{
    62  		Name:       "redis-failover",
    63  		Repository: "bitnami/redis-sentinel",
    64  		Tag:        "6.0.9",
    65  		Networks:   []*dockertest.Network{net},
    66  		ExposedPorts: []string{
    67  			"26379/tcp",
    68  		},
    69  		PortBindings: map[docker.Port][]docker.PortBinding{
    70  			"26379/tcp": {{HostIP: "", HostPort: "26379/tcp"}},
    71  		},
    72  		Env: []string{
    73  			"REDIS_SENTINEL_ANNOUNCE_IP=" + hostIP,
    74  			"REDIS_SENTINEL_QUORUM=1",
    75  			"REDIS_MASTER_HOST=" + hostIP,
    76  			"REDIS_MASTER_PORT_NUMBER=" + master.GetPort("6379/tcp"),
    77  		},
    78  	})
    79  	require.NoError(t, err)
    80  
    81  	t.Cleanup(func() {
    82  		assert.NoError(t, pool.Purge(master))
    83  		assert.NoError(t, pool.Purge(sentinel))
    84  	})
    85  
    86  	clusterURL := ""
    87  	clusterURL += fmt.Sprintf("redis://%s:%s/0,", hostIP, sentinel.GetPort("26379/tcp"))
    88  	clusterURL = strings.TrimSuffix(clusterURL, ",")
    89  
    90  	require.NoError(t, pool.Retry(func() error {
    91  		conf := cache.NewConfig()
    92  		conf.Redis.URL = clusterURL
    93  		conf.Redis.Kind = "failover"
    94  		conf.Redis.Master = "mymaster"
    95  
    96  		r, cErr := cache.NewRedis(conf, nil, log.Noop(), metrics.Noop())
    97  		if cErr != nil {
    98  			return cErr
    99  		}
   100  		cErr = r.Set("benthos_test_redis_connect", []byte("foo bar"))
   101  		return cErr
   102  	}))
   103  
   104  	template := `
   105  cache_resources:
   106    - label: testcache
   107      redis:
   108        url: $VAR1
   109        kind: failover
   110        master: mymaster
   111        prefix: $ID
   112  `
   113  	suite := integration.CacheTests(
   114  		integration.CacheTestOpenClose(),
   115  		integration.CacheTestMissingKey(),
   116  		integration.CacheTestDoubleAdd(),
   117  		integration.CacheTestDelete(),
   118  		integration.CacheTestGetAndSet(50),
   119  	)
   120  	suite.Run(
   121  		t, template,
   122  		integration.CacheTestOptVarOne(clusterURL),
   123  	)
   124  })