1// Copyright 2018 The etcd Authors 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 15package main 16 17import ( 18 "context" 19 "fmt" 20 "io/ioutil" 21 "net/url" 22 "os" 23 "strings" 24 "time" 25 26 clientv3 "go.etcd.io/etcd/client/v3" 27 "go.etcd.io/etcd/server/v3/embed" 28 29 "go.uber.org/zap" 30) 31 32func newEmbedURLs(n int) (urls []url.URL) { 33 urls = make([]url.URL, n) 34 for i := 0; i < n; i++ { 35 u, _ := url.Parse(fmt.Sprintf("unix://localhost:%d%06d", os.Getpid(), i)) 36 urls[i] = *u 37 } 38 return urls 39} 40 41func setupEmbedCfg(cfg *embed.Config, curls, purls, ics []url.URL) { 42 cfg.Logger = "zap" 43 cfg.LogOutputs = []string{"/dev/null"} 44 // []string{"stderr"} to enable server logging 45 46 var err error 47 cfg.Dir, err = ioutil.TempDir(os.TempDir(), fmt.Sprintf("%016X", time.Now().UnixNano())) 48 if err != nil { 49 panic(err) 50 } 51 os.RemoveAll(cfg.Dir) 52 53 cfg.ClusterState = "new" 54 cfg.LCUrls, cfg.ACUrls = curls, curls 55 cfg.LPUrls, cfg.APUrls = purls, purls 56 57 cfg.InitialCluster = "" 58 for i := range ics { 59 cfg.InitialCluster += fmt.Sprintf(",%d=%s", i, ics[i].String()) 60 } 61 cfg.InitialCluster = cfg.InitialCluster[1:] 62} 63 64func getCommand(exec, name, dir, cURL, pURL, cluster string) (args []string) { 65 if !strings.Contains(exec, "etcd") { 66 panic(fmt.Errorf("%q doesn't seem like etcd binary", exec)) 67 } 68 return []string{ 69 exec, 70 "--name", name, 71 "--data-dir", dir, 72 "--listen-client-urls", cURL, 73 "--advertise-client-urls", cURL, 74 "--listen-peer-urls", pURL, 75 "--initial-advertise-peer-urls", pURL, 76 "--initial-cluster", cluster, 77 "--initial-cluster-token=tkn", 78 "--initial-cluster-state=new", 79 } 80} 81 82func write(ep string) { 83 cli, err := clientv3.New(clientv3.Config{Endpoints: []string{strings.Replace(ep, "/metrics", "", 1)}}) 84 if err != nil { 85 lg.Panic("failed to create client", zap.Error(err)) 86 } 87 defer cli.Close() 88 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 89 defer cancel() 90 _, err = cli.Put(ctx, "____test", "") 91 if err != nil { 92 lg.Panic("failed to write test key", zap.Error(err)) 93 } 94 _, err = cli.Get(ctx, "____test") 95 if err != nil { 96 lg.Panic("failed to read test key", zap.Error(err)) 97 } 98 _, err = cli.Delete(ctx, "____test") 99 if err != nil { 100 lg.Panic("failed to delete test key", zap.Error(err)) 101 } 102 cli.Watch(ctx, "____test", clientv3.WithCreatedNotify()) 103} 104