github.com/bartle-stripe/trillian@v1.2.1/testonly/integration/etcd/etcd.go (about)

     1  // Copyright 2017 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package etcd
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"io/ioutil"
    21  	"net"
    22  	"net/url"
    23  	"os"
    24  	"strings"
    25  	"time"
    26  
    27  	"github.com/coreos/etcd/clientv3"
    28  	"github.com/coreos/etcd/embed"
    29  )
    30  
    31  const (
    32  
    33  	// MaxEtcdStartAttempts is the max number of start attempts made before it fails.
    34  	MaxEtcdStartAttempts = 3
    35  
    36  	defaultTimeout = 5 * time.Second
    37  	tempDirPrefix  = "etcdquota-test-"
    38  )
    39  
    40  // StartEtcd returns a started, ready to use embedded etcd, along with a client and a cleanup
    41  // function (that must be defer-called). There's no need to defer-close etcd of client, cleanup
    42  // closes both.
    43  //
    44  // A temp directory and random ports are used to setup etcd.
    45  func StartEtcd() (e *embed.Etcd, c *clientv3.Client, cleanup func(), err error) {
    46  	var dir string
    47  	dir, err = ioutil.TempDir("", tempDirPrefix)
    48  	if err != nil {
    49  		return
    50  	}
    51  
    52  	cleanup = func() {
    53  		if c != nil {
    54  			c.Close()
    55  		}
    56  		if e != nil {
    57  			e.Close()
    58  		}
    59  		os.RemoveAll(dir)
    60  	}
    61  
    62  	for i := 0; i < MaxEtcdStartAttempts; i++ {
    63  		e, err = tryStartEtcd(dir)
    64  		if err == nil {
    65  			break
    66  		}
    67  		if strings.Contains(err.Error(), "address already in use") {
    68  			continue
    69  		}
    70  		cleanup()
    71  		return
    72  	}
    73  	if e == nil {
    74  		cleanup()
    75  		err = errors.New("failed to start etcd: too many attempts")
    76  		return
    77  	}
    78  
    79  	select {
    80  	case <-e.Server.ReadyNotify():
    81  		// OK
    82  	case <-time.After(defaultTimeout):
    83  		cleanup()
    84  		err = errors.New("timed out waiting for etcd to start")
    85  		return
    86  	}
    87  
    88  	c, err = clientv3.New(clientv3.Config{
    89  		Endpoints:   []string{e.Config().LCUrls[0].String()},
    90  		DialTimeout: defaultTimeout,
    91  	})
    92  	if err != nil {
    93  		cleanup()
    94  	}
    95  	return
    96  }
    97  
    98  func tryStartEtcd(dir string) (*embed.Etcd, error) {
    99  	p1, err := net.Listen("tcp", "localhost:0")
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  	p1.Close()
   104  
   105  	p2, err := net.Listen("tcp", "localhost:0")
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  	p2.Close()
   110  
   111  	// OK to ignore err, it'll error below if parsing fails
   112  	clientURL, _ := url.Parse("http://" + p1.Addr().String())
   113  	peerURL, _ := url.Parse("http://" + p2.Addr().String())
   114  
   115  	cfg := embed.NewConfig()
   116  	cfg.Dir = dir
   117  	cfg.LCUrls = []url.URL{*clientURL} // listen client URLS
   118  	cfg.ACUrls = []url.URL{*clientURL} // advertise client URLS
   119  	cfg.LPUrls = []url.URL{*peerURL}   // listen peer URLS
   120  	cfg.APUrls = []url.URL{*peerURL}   // advertise peer URLS
   121  	cfg.InitialCluster = fmt.Sprintf("default=%v", peerURL)
   122  
   123  	return embed.StartEtcd(cfg)
   124  }