Skip to content

Commit

Permalink
Merge branch 'main' into context-canceled-logs
Browse files Browse the repository at this point in the history
  • Loading branch information
prateekdwivedi committed Mar 8, 2024
2 parents beb3de6 + e86884e commit 59e3b3a
Show file tree
Hide file tree
Showing 11 changed files with 316 additions and 266 deletions.
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ verify_ssl = true
mkdocs = "==1.5.3"
mkdocs-codeinclude-plugin = "==0.2.1"
mkdocs-include-markdown-plugin = "==6.0.4"
mkdocs-material = "==9.3.2"
mkdocs-material = "==9.5.13"
mkdocs-markdownextradata-plugin = "==0.2.5"

[requires]
Expand Down
461 changes: 234 additions & 227 deletions Pipfile.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/modules/localstack.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ By default, the image used is `localstack:1.4.0`. If you need to use a differen
It's possible to entirely override the default LocalStack container request:
<!--codeinclude-->
[Customize container request](../../modules/localstack/localstack_test.go) inside_block:withCustomContainerRequest
[Customize container request](../../modules/localstack/examples_test.go) inside_block:withCustomContainerRequest
<!--/codeinclude-->
With simply passing the `testcontainers.CustomizeRequest` functional option to the `RunContainer` function, you'll be able to configure the LocalStack container with your own needs, as this new container request will be merged with the original one.

In the above example you can check how it's possible to set certain environment variables that are needed by the tests, the most important ones are the AWS services you want to use. Besides, the container runs in a separate Docker network with an alias.
In the above example you can check how it's possible to copy files that are needed by the tests. The `flagsFn` function is a helper function that converts Docker labels used by Ryuk to a string with the format requested by LocalStack.
## Accessing hostname-sensitive services
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/weaviate.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ This method returns the Schema and Host for the Weaviate container, using the de

The following example demonstrates how to create a Weaviate client using the Weaviate module.

First of all, you need to import the CWeaviate client:
First of all, you need to import the Weaviate client:

```golang
import (
"github.com/weaviate/weaviate-go-client/v4/weaviate"
)
```

Then, you can create a Chroma client using the Chroma module:
Then, you can create a Weaviate client using the Weaviate module:

<!--codeinclude-->
[Get the client](../../modules/weaviate/examples_test.go) inside_block:createClient
Expand Down
1 change: 1 addition & 0 deletions modules/consul/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
Env: map[string]string{},
WaitingFor: wait.ForAll(
wait.ForLog("Consul agent running!"),
wait.ForListeningPort(defaultHttpApiPort+"/tcp"),
),
},
Started: true,
Expand Down
31 changes: 12 additions & 19 deletions modules/localstack/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,9 @@ func ExampleRunContainer_withNetwork() {

localstackContainer, err := localstack.RunContainer(
ctx,
testcontainers.CustomizeRequest(testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "localstack/localstack:0.13.0",
Env: map[string]string{"SERVICES": "s3,sqs"},
Networks: []string{nwName},
NetworkAliases: map[string][]string{nwName: {"localstack"}},
},
}),
testcontainers.WithImage("localstack/localstack:0.13.0"),
testcontainers.WithEnv(map[string]string{"SERVICES": "s3,sqs"}),
network.WithNetwork([]string{nwName}, newNetwork),
)
if err != nil {
log.Fatalf("failed to start container: %s", err)
Expand Down Expand Up @@ -99,13 +94,9 @@ func ExampleRunContainer_legacyMode() {

_, err := localstack.RunContainer(
ctx,
testcontainers.CustomizeRequest(testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "localstack/localstack:0.10.0",
Env: map[string]string{"SERVICES": "s3,sqs"},
WaitingFor: wait.ForLog("Ready.").WithStartupTimeout(5 * time.Minute).WithOccurrence(1),
},
}),
testcontainers.WithImage("localstack/localstack:0.10.0"),
testcontainers.WithEnv(map[string]string{"SERVICES": "s3,sqs"}),
testcontainers.WithWaitStrategy(wait.ForLog("Ready.").WithStartupTimeout(5*time.Minute).WithOccurrence(1)),
)
if err == nil {
log.Fatalf("expected an error, got nil")
Expand Down Expand Up @@ -133,14 +124,15 @@ func ExampleRunContainer_usingLambdas() {

lambdaName := "localstack-lambda-url-example"

// withCustomContainerRequest {
container, err := localstack.RunContainer(ctx,
testcontainers.WithImage("localstack/localstack:2.3.0"),
testcontainers.WithEnv(map[string]string{
"SERVICES": "lambda",
"LAMBDA_DOCKER_FLAGS": flagsFn(),
}),
testcontainers.CustomizeRequest(testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Env: map[string]string{
"SERVICES": "lambda",
"LAMBDA_DOCKER_FLAGS": flagsFn(),
},
Files: []testcontainers.ContainerFile{
{
HostFilePath: filepath.Join("testdata", "function.zip"),
Expand All @@ -149,6 +141,7 @@ func ExampleRunContainer_usingLambdas() {
},
},
}),
// }
)
if err != nil {
log.Fatalf("failed to start container: %s", err)
Expand Down
10 changes: 2 additions & 8 deletions modules/localstack/localstack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,15 @@ func TestStartWithoutOverride(t *testing.T) {
func TestStartV2WithNetwork(t *testing.T) {
ctx := context.Background()

// withCustomContainerRequest {
nw, err := network.New(ctx)
require.NoError(t, err)

localstack, err := RunContainer(
ctx,
network.WithNetwork([]string{"localstack"}, nw),
testcontainers.CustomizeRequest(testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "localstack/localstack:2.0.0",
Env: map[string]string{"SERVICES": "s3,sqs"},
},
}),
testcontainers.WithImage("localstack/localstack:2.0.0"),
testcontainers.WithEnv(map[string]string{"SERVICES": "s3,sqs"}),
)
// }
require.NoError(t, err)
assert.NotNil(t, localstack)

Expand Down
18 changes: 14 additions & 4 deletions modules/weaviate/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/weaviate/weaviate-go-client/v4/weaviate"
"github.com/weaviate/weaviate-go-client/v4/weaviate/grpc"

"github.com/testcontainers/testcontainers-go"
tcweaviate "github.com/testcontainers/testcontainers-go/modules/weaviate"
Expand All @@ -17,7 +18,7 @@ func ExampleRunContainer() {
// runWeaviateContainer {
ctx := context.Background()

weaviateContainer, err := tcweaviate.RunContainer(ctx, testcontainers.WithImage("semitechnologies/weaviate:1.23.9"))
weaviateContainer, err := tcweaviate.RunContainer(ctx, testcontainers.WithImage("semitechnologies/weaviate:1.24.1"))
if err != nil {
log.Fatalf("failed to start container: %s", err)
}
Expand Down Expand Up @@ -58,7 +59,12 @@ func ExampleRunContainer_connectWithClient() {

scheme, host, err := weaviateContainer.HttpHostAddress(ctx)
if err != nil {
log.Fatalf("failed to get schema and host: %s", err) // nolint:gocritic
log.Fatalf("failed to get http schema and host: %s", err) // nolint:gocritic
}

grpcHost, err := weaviateContainer.GrpcHostAddress(ctx)
if err != nil {
log.Fatalf("failed to get gRPC host: %s", err) // nolint:gocritic
}

connectionClient := &http.Client{}
Expand All @@ -68,8 +74,12 @@ func ExampleRunContainer_connectWithClient() {
}

cli := weaviate.New(weaviate.Config{
Scheme: scheme,
Host: host,
Scheme: scheme,
Host: host,
GrpcConfig: &grpc.Config{
Secured: false, // set true if gRPC connection is secured
Host: grpcHost,
},
Headers: headers,
AuthConfig: nil, // put here the weaviate auth.Config, if you need it
ConnectionClient: connectionClient,
Expand Down
2 changes: 1 addition & 1 deletion modules/weaviate/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/testcontainers/testcontainers-go v0.29.1
github.com/weaviate/weaviate-go-client/v4 v4.12.1
google.golang.org/grpc v1.59.0
)

require (
Expand Down Expand Up @@ -76,7 +77,6 @@ require (
golang.org/x/tools v0.13.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand Down
18 changes: 17 additions & 1 deletion modules/weaviate/weaviate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type WeaviateContainer struct {
// RunContainer creates an instance of the Weaviate container type
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*WeaviateContainer, error) {
req := testcontainers.ContainerRequest{
Image: "semitechnologies/weaviate:1.23.9",
Image: "semitechnologies/weaviate:1.24.1",
Cmd: []string{"--host", "0.0.0.0", "--scheme", "http", "--port", "8080"},
ExposedPorts: []string{"8080/tcp", "50051/tcp"},
Env: map[string]string{
Expand Down Expand Up @@ -66,3 +66,19 @@ func (c *WeaviateContainer) HttpHostAddress(ctx context.Context) (string, string

return "http", fmt.Sprintf("%s:%s", host, containerPort.Port()), nil
}

// GrpcHostAddress returns the gRPC host of the Weaviate container.
// At the moment, it only supports unsecured gRPC connection.
func (c *WeaviateContainer) GrpcHostAddress(ctx context.Context) (string, error) {
containerPort, err := c.MappedPort(ctx, "50051/tcp")
if err != nil {
return "", fmt.Errorf("failed to get container port: %w", err)
}

host, err := c.Host(ctx)
if err != nil {
return "", fmt.Errorf("failed to get container host")
}

return fmt.Sprintf("%s:%s", host, containerPort.Port()), nil
}
31 changes: 30 additions & 1 deletion modules/weaviate/weaviate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import (
"net/http"
"testing"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/health/grpc_health_v1"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/weaviate"
)

func TestWeaviate(t *testing.T) {
ctx := context.Background()

container, err := weaviate.RunContainer(ctx, testcontainers.WithImage("semitechnologies/weaviate:1.23.9"))
container, err := weaviate.RunContainer(ctx, testcontainers.WithImage("semitechnologies/weaviate:1.24.1"))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -44,4 +48,29 @@ func TestWeaviate(t *testing.T) {
tt.Fatalf("unexpected status code: %d", resp.StatusCode)
}
})

t.Run("GrpcHostAddress", func(tt *testing.T) {
// gRPCHostAddress {
host, err := container.GrpcHostAddress(ctx)
// }
if err != nil {
t.Fatal(err)
}

var opts []grpc.DialOption
opts = append(opts, grpc.WithBlock())
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.Dial(host, opts...)
if err != nil {
tt.Fatalf("failed to dial connection: %v", err)
}
client := grpc_health_v1.NewHealthClient(conn)
check, err := client.Check(context.TODO(), &grpc_health_v1.HealthCheckRequest{})
if err != nil {
tt.Fatalf("failed to get a health check: %v", err)
}
if grpc_health_v1.HealthCheckResponse_SERVING.Enum().Number() != check.Status.Number() {
tt.Fatalf("unexpected status code: %d", check.Status.Number())
}
})
}

0 comments on commit 59e3b3a

Please sign in to comment.