github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/integration/resources/docker/dockerexternal/etcd_test.go (about) 1 // Copyright (c) 2022 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 dockerexternal 22 23 import ( 24 "context" 25 "math/rand" 26 "strconv" 27 "strings" 28 "testing" 29 "time" 30 31 "github.com/m3db/m3/src/x/instrument" 32 33 "github.com/ory/dockertest/v3" 34 "github.com/ory/dockertest/v3/docker" 35 "github.com/stretchr/testify/assert" 36 "github.com/stretchr/testify/require" 37 clientv3 "go.etcd.io/etcd/client/v3" 38 "go.uber.org/zap" 39 ) 40 41 const ( 42 testKey = "etcd-test" 43 ) 44 45 var ( 46 testLogger, _ = zap.NewDevelopment() 47 ) 48 49 type etcdTestDeps struct { 50 Pool *dockertest.Pool 51 InstrumentOpts instrument.Options 52 Etcd *EtcdNode 53 } 54 55 func setupEtcdTest(t *testing.T) etcdTestDeps { 56 pool, err := dockertest.NewPool("") 57 require.NoError(t, err) 58 59 iopts := instrument.NewOptions().SetLogger(testLogger) 60 c, err := NewEtcd(pool, iopts) 61 require.NoError(t, err) 62 63 return etcdTestDeps{ 64 Pool: pool, 65 Etcd: c, 66 InstrumentOpts: iopts, 67 } 68 } 69 70 func TestCluster(t *testing.T) { 71 t.Run("starts a functioning cluster", func(t *testing.T) { 72 ctx, cancel := newTestContext() 73 defer cancel() 74 75 deps := setupEtcdTest(t) 76 require.NoError(t, deps.Etcd.Setup(ctx)) 77 78 t.Cleanup(func() { 79 require.NoError(t, deps.Etcd.Close(ctx)) 80 }) 81 82 cli, err := clientv3.New( 83 clientv3.Config{ 84 Endpoints: []string{deps.Etcd.Address()}, 85 }, 86 ) 87 require.NoError(t, err) 88 89 //nolint:gosec 90 testVal := strconv.Itoa(rand.Intn(10000)) 91 _, err = cli.Put(ctx, testKey, testVal) 92 require.NoError(t, err) 93 94 actualVal, err := cli.Get(ctx, testKey) 95 require.NoError(t, err) 96 97 assert.Equal(t, testVal, string(actualVal.Kvs[0].Value)) 98 }) 99 100 t.Run("can run multiple at once", func(t *testing.T) { 101 ctx, cancel := newTestContext() 102 defer cancel() 103 104 deps := setupEtcdTest(t) 105 106 require.NoError(t, deps.Etcd.Setup(ctx)) 107 defer func() { 108 require.NoError(t, deps.Etcd.Close(ctx)) 109 }() 110 111 c2, err := NewEtcd(deps.Pool, deps.InstrumentOpts) 112 require.NoError(t, err) 113 require.NoError(t, c2.Setup(ctx)) 114 defer func() { 115 require.NoError(t, c2.Close(ctx)) 116 }() 117 }) 118 119 t.Run("cleans up containers on shutdown", func(t *testing.T) { 120 ctx, cancel := newTestContext() 121 defer cancel() 122 123 deps := setupEtcdTest(t) 124 testPrefix := "cleanup-test-" 125 deps.Etcd.namePrefix = testPrefix 126 127 findContainers := func(namePrefix string, pool *dockertest.Pool) ([]docker.APIContainers, error) { 128 containers, err := deps.Pool.Client.ListContainers(docker.ListContainersOptions{}) 129 if err != nil { 130 return nil, err 131 } 132 133 var rtn []docker.APIContainers 134 for _, ct := range containers { 135 for _, name := range ct.Names { 136 // Docker response prefixes the container name with / regardless of what you give it as input. 137 if strings.HasPrefix(name, "/"+namePrefix) { 138 rtn = append(rtn, ct) 139 break 140 } 141 } 142 } 143 return rtn, nil 144 } 145 146 require.NoError(t, deps.Etcd.Setup(ctx)) 147 148 cts, err := findContainers(testPrefix, deps.Pool) 149 require.NoError(t, err) 150 assert.Len(t, cts, 1) 151 152 require.NoError(t, deps.Etcd.Close(ctx)) 153 cts, err = findContainers(testPrefix, deps.Pool) 154 require.NoError(t, err) 155 assert.Len(t, cts, 0) 156 }) 157 } 158 159 func TestCluster_waitForHealth(t *testing.T) { 160 t.Run("errors when context is canceled", func(t *testing.T) { 161 ctx, cancel := context.WithCancel(context.Background()) 162 163 deps := setupEtcdTest(t) 164 etcdCli := fakeMemberClient{err: assert.AnError} 165 cancel() 166 require.EqualError( 167 t, 168 deps.Etcd.waitForHealth(ctx, etcdCli), 169 "waiting for etcd to become healthy: context canceled while retrying: context canceled", 170 ) 171 }) 172 } 173 174 type fakeMemberClient struct { 175 err error 176 } 177 178 func (f fakeMemberClient) MemberList(ctx context.Context) (*clientv3.MemberListResponse, error) { 179 return nil, f.err 180 } 181 182 func newTestContext() (context.Context, func()) { 183 return context.WithTimeout(context.Background(), 10*time.Second) 184 }