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

     1  // Package memcached includes Memcached implementation of Gnomock Preset interface.
     2  // This Preset can be passed to gnomock.StartPreset function to create a
     3  // configured Memcached container to use in tests
     4  package memcached
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  
    10  	"github.com/bradfitz/gomemcache/memcache"
    11  	"github.com/etecs-ru/gnomock"
    12  	"github.com/etecs-ru/gnomock/internal/registry"
    13  )
    14  
    15  const defaultVersion = "1.6.9"
    16  
    17  func init() {
    18  	registry.Register("memcached", func() gnomock.Preset { return &P{} })
    19  }
    20  
    21  // Preset creates a new Gmomock Memcached preset. This preset includes a Memcached
    22  // specific healthcheck function, default Memcached 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 Memcached storage
    35  type P struct {
    36  	Values     map[string]string `json:"values"`
    37  	ByteValues map[string][]byte `json:"byteValues"`
    38  	Version    string            `json:"version"`
    39  }
    40  
    41  // Image returns an image that should be pulled to create this container
    42  func (p *P) Image() string {
    43  	return fmt.Sprintf("docker.io/library/memcached:%s", p.Version)
    44  }
    45  
    46  // Ports returns ports that should be used to access this container
    47  func (p *P) Ports() gnomock.NamedPorts {
    48  	return gnomock.DefaultTCP(11211)
    49  }
    50  
    51  // Options returns a list of options to configure this container
    52  func (p *P) Options() []gnomock.Option {
    53  	p.setDefaults()
    54  
    55  	opts := []gnomock.Option{
    56  		gnomock.WithHealthCheck(healthcheck),
    57  	}
    58  
    59  	if p.ByteValues != nil || p.Values != nil {
    60  		initf := func(ctx context.Context, c *gnomock.Container) error {
    61  			addr := c.Address(gnomock.DefaultPort)
    62  			client := memcache.New(addr)
    63  
    64  			if p.ByteValues != nil {
    65  				for k, v := range p.ByteValues {
    66  					err := client.Set(&memcache.Item{Key: k, Value: v, Expiration: 0})
    67  					if err != nil {
    68  						return fmt.Errorf("can't set '%s'='%v': %w", k, v, err)
    69  					}
    70  				}
    71  			}
    72  
    73  			if p.Values != nil {
    74  				for k, v := range p.Values {
    75  					err := client.Set(&memcache.Item{Key: k, Value: []byte(v), Expiration: 0})
    76  					if err != nil {
    77  						return fmt.Errorf("can't set '%s'='%v': %w", k, v, err)
    78  					}
    79  				}
    80  			}
    81  
    82  			return nil
    83  		}
    84  
    85  		opts = append(opts, gnomock.WithInit(initf))
    86  	}
    87  
    88  	return opts
    89  }
    90  
    91  func (p *P) setDefaults() {
    92  	if p.Version == "" {
    93  		p.Version = defaultVersion
    94  	}
    95  }
    96  
    97  func healthcheck(ctx context.Context, c *gnomock.Container) error {
    98  	addr := c.Address(gnomock.DefaultPort)
    99  	client := memcache.New(addr)
   100  
   101  	return client.Ping()
   102  }