github.com/m3db/m3@v1.5.0/src/integration/resources/docker/harness.go (about)

     1  // Copyright (c) 2020 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  // Package docker contains resources needed to setup docker containers for M3 tests.
    22  package docker
    23  
    24  import (
    25  	"time"
    26  
    27  	"github.com/ory/dockertest/v3"
    28  	"go.uber.org/zap"
    29  
    30  	"github.com/m3db/m3/src/integration/resources"
    31  	xerrors "github.com/m3db/m3/src/x/errors"
    32  	"github.com/m3db/m3/src/x/instrument"
    33  )
    34  
    35  const timeout = time.Second * 60
    36  
    37  type dockerResources struct {
    38  	coordinator resources.Coordinator
    39  	nodes       resources.Nodes
    40  
    41  	pool *dockertest.Pool
    42  }
    43  
    44  // SetupSingleM3DBNode creates docker resources representing a setup with a
    45  // single DB node.
    46  func SetupSingleM3DBNode(opts ...SetupOptions) (resources.M3Resources, error) { // nolint: gocyclo
    47  	options := setupOptions{}
    48  	for _, f := range opts {
    49  		f(&options)
    50  	}
    51  
    52  	pool, err := dockertest.NewPool("")
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	pool.MaxWait = timeout
    58  
    59  	if !options.existingCluster {
    60  		if err := SetupNetwork(pool); err != nil {
    61  			return nil, err
    62  		}
    63  
    64  		if err := setupVolume(pool); err != nil {
    65  			return nil, err
    66  		}
    67  	}
    68  
    69  	iOpts := instrument.NewOptions()
    70  	dbNode, err := newDockerHTTPNode(pool, ResourceOptions{
    71  		Image:          options.dbNodeImage,
    72  		ContainerName:  options.dbNodeContainerName,
    73  		InstrumentOpts: iOpts,
    74  	})
    75  
    76  	success := false
    77  	dbNodes := resources.Nodes{dbNode}
    78  	defer func() {
    79  		// NB: only defer close in the failure case, otherwise calling function
    80  		// is responsible for closing the resources.
    81  		if !success {
    82  			for _, dbNode := range dbNodes {
    83  				if dbNode != nil {
    84  					dbNode.Close() //nolint:errcheck
    85  				}
    86  			}
    87  		}
    88  	}()
    89  
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  
    94  	coordinator, err := newDockerHTTPCoordinator(pool, ResourceOptions{
    95  		Image:          options.coordinatorImage,
    96  		ContainerName:  options.coordinatorContainerName,
    97  		InstrumentOpts: iOpts,
    98  	})
    99  
   100  	defer func() {
   101  		// NB: only defer close in the failure case, otherwise calling function
   102  		// is responsible for closing the resources.
   103  		if !success && coordinator != nil {
   104  			coordinator.Close() //nolint:errcheck
   105  		}
   106  	}()
   107  
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  
   112  	cluster := &dockerResources{
   113  		coordinator: coordinator,
   114  		nodes:       dbNodes,
   115  		pool:        pool,
   116  	}
   117  	err = resources.SetupCluster(cluster, resources.ClusterOptions{})
   118  
   119  	logger := iOpts.Logger().With(zap.String("source", "harness"))
   120  	logger.Info("all healthy")
   121  	success = true
   122  	return cluster, err
   123  }
   124  
   125  // AttachToExistingContainers attaches docker API to an existing coordinator
   126  // and one or more dbnode containers.
   127  func AttachToExistingContainers(
   128  	coordinatorContainerName string,
   129  	dbNodesContainersNames []string,
   130  ) (resources.M3Resources, error) {
   131  	pool, err := dockertest.NewPool("")
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  	pool.MaxWait = timeout
   136  
   137  	iOpts := instrument.NewOptions()
   138  	dbNodes := resources.Nodes{}
   139  	for _, containerName := range dbNodesContainersNames {
   140  		dbNode, err := newDockerHTTPNode(pool, ResourceOptions{
   141  			InstrumentOpts: iOpts,
   142  			ContainerName:  containerName,
   143  		})
   144  		if err != nil {
   145  			return nil, err
   146  		}
   147  		dbNodes = append(dbNodes, dbNode)
   148  	}
   149  
   150  	coordinator, err := newDockerHTTPCoordinator(
   151  		pool,
   152  		ResourceOptions{
   153  			InstrumentOpts: iOpts,
   154  			ContainerName:  coordinatorContainerName,
   155  		},
   156  	)
   157  	if err != nil {
   158  		return nil, err
   159  	}
   160  
   161  	return &dockerResources{
   162  		coordinator: coordinator,
   163  		nodes:       dbNodes,
   164  		pool:        pool,
   165  	}, err
   166  }
   167  
   168  func (r *dockerResources) Start() {
   169  	// noop as docker containers are expected to be started before attaching.
   170  }
   171  
   172  func (r *dockerResources) Cleanup() error {
   173  	if r == nil {
   174  		return nil
   175  	}
   176  
   177  	var multiErr xerrors.MultiError
   178  	if r.coordinator != nil {
   179  		multiErr = multiErr.Add(r.coordinator.Close())
   180  	}
   181  
   182  	for _, dbNode := range r.nodes {
   183  		if dbNode != nil {
   184  			multiErr = multiErr.Add(dbNode.Close())
   185  		}
   186  	}
   187  
   188  	return multiErr.FinalError()
   189  }
   190  
   191  func (r *dockerResources) Nodes() resources.Nodes             { return r.nodes }
   192  func (r *dockerResources) Coordinator() resources.Coordinator { return r.coordinator }
   193  func (r *dockerResources) Aggregators() resources.Aggregators { return nil }