github.com/etecs-ru/gnomock@v0.13.2/preset/redis/preset.go (about)

     1  // Package redis includes Redis implementation of Gnomock Preset interface.
     2  // This Preset can be passed to gnomock.Start() function to create a configured
     3  // Redis container to use in tests.
     4  package redis
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  
    10  	"github.com/etecs-ru/gnomock"
    11  	"github.com/etecs-ru/gnomock/internal/registry"
    12  	redisclient "github.com/go-redis/redis/v8"
    13  )
    14  
    15  const defaultVersion = "6.0.9"
    16  
    17  func init() {
    18  	registry.Register("redis", func() gnomock.Preset { return &P{} })
    19  }
    20  
    21  // Preset creates a new Gmomock Redis preset. This preset includes a Redis
    22  // specific healthcheck function, default Redis image and port, and allows to
    23  // optionally set up initial state
    24  func Preset(opts ...Option) gnomock.Preset {
    25  	p := &P{}
    26  
    27  	for _, opt := range opts {
    28  		opt(p)
    29  	}
    30  
    31  	return p
    32  }
    33  
    34  // P is a Gnomock Preset implementation for Redis storage
    35  type P struct {
    36  	Values  map[string]interface{} `json:"values"`
    37  	Version string                 `json:"version"`
    38  }
    39  
    40  // Image returns an image that should be pulled to create this container
    41  func (p *P) Image() string {
    42  	return fmt.Sprintf("docker.io/library/redis:%s", p.Version)
    43  }
    44  
    45  // Ports returns ports that should be used to access this container
    46  func (p *P) Ports() gnomock.NamedPorts {
    47  	return gnomock.DefaultTCP(6379)
    48  }
    49  
    50  // Options returns a list of options to configure this container
    51  func (p *P) Options() []gnomock.Option {
    52  	p.setDefaults()
    53  
    54  	opts := []gnomock.Option{
    55  		gnomock.WithHealthCheck(healthcheck),
    56  	}
    57  
    58  	if p.Values != nil {
    59  		initf := func(ctx context.Context, c *gnomock.Container) error {
    60  			addr := c.Address(gnomock.DefaultPort)
    61  			client := redisclient.NewClient(&redisclient.Options{Addr: addr})
    62  
    63  			for k, v := range p.Values {
    64  				err := client.Set(ctx, k, v, 0).Err()
    65  				if err != nil {
    66  					return fmt.Errorf("can't set '%s'='%v': %w", k, v, err)
    67  				}
    68  			}
    69  
    70  			return nil
    71  		}
    72  
    73  		opts = append(opts, gnomock.WithInit(initf))
    74  	}
    75  
    76  	return opts
    77  }
    78  
    79  func (p *P) setDefaults() {
    80  	if p.Version == "" {
    81  		p.Version = defaultVersion
    82  	}
    83  }
    84  
    85  func healthcheck(ctx context.Context, c *gnomock.Container) error {
    86  	addr := c.Address(gnomock.DefaultPort)
    87  	client := redisclient.NewClient(&redisclient.Options{Addr: addr})
    88  
    89  	_, err := client.Ping(ctx).Result()
    90  
    91  	return err
    92  }