github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/integration/resources/docker/dockerm3/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 dockerm3 contains resources needed to setup docker containers for M3 tests. 22 package dockerm3 23 24 import ( 25 "time" 26 27 "github.com/m3db/m3/src/integration/resources" 28 xdockertest "github.com/m3db/m3/src/x/dockertest" 29 xerrors "github.com/m3db/m3/src/x/errors" 30 "github.com/m3db/m3/src/x/instrument" 31 32 "github.com/ory/dockertest/v3" 33 "go.uber.org/zap" 34 ) 35 36 const timeout = time.Second * 60 37 38 type dockerResources struct { 39 coordinator resources.Coordinator 40 nodes resources.Nodes 41 42 pool *dockertest.Pool 43 } 44 45 // SetupSingleM3DBNode creates docker resources representing a setup with a 46 // single DB node. 47 func SetupSingleM3DBNode(opts ...SetupOptions) (resources.M3Resources, error) { // nolint: gocyclo 48 options := setupOptions{} 49 for _, f := range opts { 50 f(&options) 51 } 52 53 pool, err := dockertest.NewPool("") 54 if err != nil { 55 return nil, err 56 } 57 58 pool.MaxWait = timeout 59 60 if !options.existingCluster { 61 if err := xdockertest.SetupNetwork(pool, true); err != nil { 62 return nil, err 63 } 64 65 if err := xdockertest.SetupVolume(pool); err != nil { 66 return nil, err 67 } 68 } 69 70 iOpts := instrument.NewOptions() 71 dbNode, err := newDockerHTTPNode(pool, xdockertest.ResourceOptions{ 72 Image: options.dbNodeImage, 73 ContainerName: options.dbNodeContainerName, 74 InstrumentOpts: iOpts, 75 }) 76 77 success := false 78 dbNodes := resources.Nodes{dbNode} 79 defer func() { 80 // NB: only defer close in the failure case, otherwise calling function 81 // is responsible for closing the resources. 82 if !success { 83 for _, dbNode := range dbNodes { 84 if dbNode != nil { 85 dbNode.Close() //nolint:errcheck 86 } 87 } 88 } 89 }() 90 91 if err != nil { 92 return nil, err 93 } 94 95 coordinator, err := newDockerHTTPCoordinator(pool, xdockertest.ResourceOptions{ 96 Image: options.coordinatorImage, 97 ContainerName: options.coordinatorContainerName, 98 InstrumentOpts: iOpts, 99 }) 100 101 defer func() { 102 // NB: only defer close in the failure case, otherwise calling function 103 // is responsible for closing the resources. 104 if !success && coordinator != nil { 105 coordinator.Close() //nolint:errcheck 106 } 107 }() 108 109 if err != nil { 110 return nil, err 111 } 112 113 cluster := &dockerResources{ 114 coordinator: coordinator, 115 nodes: dbNodes, 116 pool: pool, 117 } 118 err = resources.SetupCluster(cluster, resources.ClusterOptions{}) 119 120 logger := iOpts.Logger().With(zap.String("source", "harness")) 121 logger.Info("all healthy") 122 success = true 123 return cluster, err 124 } 125 126 // AttachToExistingContainers attaches docker API to an existing coordinator 127 // and one or more dbnode containers. 128 func AttachToExistingContainers( 129 coordinatorContainerName string, 130 dbNodesContainersNames []string, 131 ) (resources.M3Resources, error) { 132 pool, err := dockertest.NewPool("") 133 if err != nil { 134 return nil, err 135 } 136 pool.MaxWait = timeout 137 138 iOpts := instrument.NewOptions() 139 dbNodes := resources.Nodes{} 140 for _, containerName := range dbNodesContainersNames { 141 dbNode, err := newDockerHTTPNode(pool, xdockertest.ResourceOptions{ 142 InstrumentOpts: iOpts, 143 ContainerName: containerName, 144 }) 145 if err != nil { 146 return nil, err 147 } 148 dbNodes = append(dbNodes, dbNode) 149 } 150 151 coordinator, err := newDockerHTTPCoordinator( 152 pool, 153 xdockertest.ResourceOptions{ 154 InstrumentOpts: iOpts, 155 ContainerName: coordinatorContainerName, 156 }, 157 ) 158 if err != nil { 159 return nil, err 160 } 161 162 return &dockerResources{ 163 coordinator: coordinator, 164 nodes: dbNodes, 165 pool: pool, 166 }, err 167 } 168 169 func (r *dockerResources) Start() { 170 // noop as docker containers are expected to be started before attaching. 171 } 172 173 func (r *dockerResources) Cleanup() error { 174 if r == nil { 175 return nil 176 } 177 178 var multiErr xerrors.MultiError 179 if r.coordinator != nil { 180 multiErr = multiErr.Add(r.coordinator.Close()) 181 } 182 183 for _, dbNode := range r.nodes { 184 if dbNode != nil { 185 multiErr = multiErr.Add(dbNode.Close()) 186 } 187 } 188 189 return multiErr.FinalError() 190 } 191 192 func (r *dockerResources) Nodes() resources.Nodes { return r.nodes } 193 func (r *dockerResources) Coordinator() resources.Coordinator { return r.coordinator } 194 func (r *dockerResources) Aggregators() resources.Aggregators { return nil }