github.com/weaviate/weaviate@v1.24.6/test/docker/docker.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package docker 13 14 import ( 15 "context" 16 "fmt" 17 "time" 18 19 "github.com/pkg/errors" 20 "github.com/testcontainers/testcontainers-go" 21 ) 22 23 type DockerCompose struct { 24 network *testcontainers.DockerNetwork 25 containers []*DockerContainer 26 } 27 28 func (d *DockerCompose) Containers() []*DockerContainer { 29 return d.containers 30 } 31 32 func (d *DockerCompose) Terminate(ctx context.Context) error { 33 var errs error 34 for _, c := range d.containers { 35 if err := c.container.Terminate(ctx); err != nil { 36 errs = errors.Wrapf(err, "cannot terminate: %v", c.name) 37 } 38 } 39 if d.network != nil { 40 if err := d.network.Remove(ctx); err != nil { 41 errs = errors.Wrapf(err, "cannot remove network") 42 } 43 } 44 return errs 45 } 46 47 func (d *DockerCompose) Stop(ctx context.Context, container string, timeout *time.Duration) error { 48 for _, c := range d.containers { 49 if c.name == container { 50 if err := c.container.Stop(ctx, timeout); err != nil { 51 return fmt.Errorf("cannot stop %q: %w", c.name, err) 52 } 53 } 54 } 55 return nil 56 } 57 58 func (d *DockerCompose) Start(ctx context.Context, container string) error { 59 for _, c := range d.containers { 60 if c.name == container { 61 if err := c.container.Start(ctx); err != nil { 62 return fmt.Errorf("cannot start %q: %w", c.name, err) 63 } 64 if err := d.waitUntilRunning(c.name, c.container); err != nil { 65 return err 66 } 67 newEndpoints := map[EndpointName]endpoint{} 68 for name, e := range c.endpoints { 69 newURI, err := c.container.PortEndpoint(ctx, e.port, "") 70 if err != nil { 71 return fmt.Errorf("failed to get new uri for container %q: %w", c.name, err) 72 } 73 newEndpoints[name] = endpoint{e.port, newURI} 74 } 75 c.endpoints = newEndpoints 76 } 77 } 78 return nil 79 } 80 81 func (d *DockerCompose) waitUntilRunning(name string, container testcontainers.Container) error { 82 waitTimeout := 1 * time.Minute 83 start := time.Now() 84 for { 85 if container.IsRunning() { 86 return nil 87 } 88 time.Sleep(200 * time.Millisecond) 89 if time.Now().After(start.Add(waitTimeout)) { 90 return fmt.Errorf("container %q: was still not running after %v", name, waitTimeout) 91 } 92 } 93 } 94 95 func (d *DockerCompose) GetMinIO() *DockerContainer { 96 return d.getContainerByName(MinIO) 97 } 98 99 func (d *DockerCompose) GetGCS() *DockerContainer { 100 return d.getContainerByName(GCS) 101 } 102 103 func (d *DockerCompose) GetAzurite() *DockerContainer { 104 return d.getContainerByName(Azurite) 105 } 106 107 func (d *DockerCompose) GetWeaviate() *DockerContainer { 108 return d.getContainerByName(Weaviate) 109 } 110 111 func (d *DockerCompose) GetSecondWeaviate() *DockerContainer { 112 return d.getContainerByName(SecondWeaviate) 113 } 114 115 func (d *DockerCompose) GetWeaviateNode2() *DockerContainer { 116 return d.getContainerByName(WeaviateNode2) 117 } 118 119 func (d *DockerCompose) GetText2VecTransformers() *DockerContainer { 120 return d.getContainerByName(Text2VecTransformers) 121 } 122 123 func (d *DockerCompose) GetText2VecContextionary() *DockerContainer { 124 return d.getContainerByName(Text2VecContextionary) 125 } 126 127 func (d *DockerCompose) GetQnATransformers() *DockerContainer { 128 return d.getContainerByName(QnATransformers) 129 } 130 131 func (d *DockerCompose) getContainerByName(name string) *DockerContainer { 132 for _, c := range d.containers { 133 if c.name == name { 134 return c 135 } 136 } 137 return nil 138 }