Skip to content

Testcontainer example #701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
checkpoint
Signed-off-by: Joe Bowbeer <joe.bowbeer@gmail.com>
  • Loading branch information
joebowbeer committed May 27, 2025
commit 7b53d91efffd8e82437638c1fa37b91fecde5279
1,235 changes: 1,220 additions & 15 deletions examples/testcontainers/package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion examples/testcontainers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
"license": "ISC",
"description": "",
"devDependencies": {
"@dapr/dapr": "^3.5.2",
"ts-jest": "^29.3.2",
"typescript": "^5.8.3"
},
"dependencies": {
"testcontainers": "^10.25.0"
"testcontainers": "^10.25.0",
"yaml": "^2.7.1"
}
}
35 changes: 35 additions & 0 deletions examples/testcontainers/src/Component.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2025 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { Component } from "./Component";

describe("Component", () => {
it("should roundtrip to and from yaml", async () => {
const component = new Component("pubsub", "pubsub.redis", "v1", [
{
name: "redisHost",
value: "locahost:6379",
},
{
name: "redisPassword",
value: "",
},
]);
const yaml = component.toYaml();
expect(yaml).toContain("pubsub.redis");
expect(yaml).toContain("locahost:6379");
const roundtrip = Component.fromYaml(yaml);
expect(roundtrip).toBeInstanceOf(Component);
expect(roundtrip).toEqual(component);
});
});
79 changes: 79 additions & 0 deletions examples/testcontainers/src/Component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2025 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import YAML from "yaml";

export type MetadataEntry = {
readonly name: string;
readonly value: string;
};

export class Component {
private readonly metadata: MetadataEntry[];

/**
* Creates a new component.
*
* @param name Component name.
* @param type Component type.
* @param version Component version.
* @param metadataEntries Component metadata entries.
*/
constructor(
public readonly name: string,
public readonly type: string,
public readonly version: string,
metadataEntries: MetadataEntry[]
) {
this.metadata = metadataEntries;
}

getMetadata(): MetadataEntry[] {
return this.metadata;
}

toYaml(): string {
const componentObj = {
apiVersion: "dapr.io/v1alpha1",
kind: "Component",
metadata: {
name: this.name,
},
spec: {
type: this.type,
version: this.version,
metadata: this.metadata,
},
};
return YAML.stringify(componentObj);
}

static fromYaml(src: string): Component {
const resource = YAML.parse(src) as {
apiVersion: string;
kind: string;
metadata: {
name: string;
};
spec: {
type: string;
version: string;
metadata?: MetadataEntry[];
};
};
const metadata = resource.metadata;
const spec = resource.spec;
const specMetadata = spec.metadata ?? [];
return new Component(metadata.name, spec.type, spec.version, specMetadata);
}
}
103 changes: 103 additions & 0 deletions examples/testcontainers/src/Configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright 2025 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import YAML from "yaml";

export type ListEntry = {
name: string;
type: string;
}

export type AppHttpPipeline = {
handlers: Array<ListEntry>;
}

export type OtelTracingConfigurationSettings = {
endpointAddress?: string;
isSecure?: boolean;
protocol?: string;
}

export type ZipkinTracingConfigurationSettings = {
endpointAddress?: string;
}

export type TracingConfigurationSettings = {
samplingRate?: string;
stdout?: boolean;
otel?: OtelTracingConfigurationSettings;
zipkin?: ZipkinTracingConfigurationSettings;
}

/**
* Configuration class for Dapr.
*
* @remarks
* This class is used to create a configuration object for Dapr. It includes
* tracing and appHttpPipeline settings.
*
* @example
* ```typescript
* const config = new Configuration("my-config", tracingConfig, appHttpPipeline);
* console.log(config.toYaml());
* ```
*/
export class Configuration {

// TODO: add secrets
// TODO: add metrics
// TODO: add logging
// TODO: add middleware httpPipeline
// TODO: add nameResolution
// TODO: add disallow components
// TODO: add mtls

/**
* Creates a new configuration.
*
* @param name Configuration name.
* @param tracing TracingConfigParameters tracing configuration
* parameters.
* @param appHttpPipeline AppHttpPipeline middleware configuration.
*/
constructor(
public readonly name: string,
public readonly tracing: TracingConfigurationSettings,
public readonly appHttpPipeline: AppHttpPipeline
) {}

toYaml(): string {
const configurationObj: {
apiVersion: string;
kind: string;
metadata: {
name: string;
};
spec: {
tracing?: TracingConfigurationSettings;
appHttpPipeline?: AppHttpPipeline;
};
} = {
apiVersion: "dapr.io/v1alpha1",
kind: "Configuration",
metadata: {
name: this.name
},
spec: {
...{ tracing: this.tracing },
...{ appHttpPipeline: this.appHttpPipeline },
}
};
return YAML.stringify(configurationObj);
}
}
10 changes: 6 additions & 4 deletions examples/testcontainers/src/DaprContainer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022 The Dapr Authors
Copyright 2025 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand All @@ -14,7 +14,7 @@ limitations under the License.
// import { setTimeout } from "node:timers/promises";
// import { GenericContainer, Network, Wait } from "testcontainers";
import { Network } from "testcontainers";
import { DaprContainer, DAPRD_DEFAULT_GRPC_PORT, DAPRD_DEFAULT_HTTP_PORT } from "./DaprContainer";
import { DaprContainer } from "./DaprContainer";

// jest.setTimeout(120_000);

Expand All @@ -24,8 +24,10 @@ describe("DaprContainer", () => {
const container = new DaprContainer("daprio/dapr:latest").withNetwork(network);
const startedContainer = await container.start();
expect(startedContainer.getHost()).toBeDefined();
expect(startedContainer.getMappedPort(DAPRD_DEFAULT_HTTP_PORT)).toBeDefined();
expect(startedContainer.getMappedPort(DAPRD_DEFAULT_GRPC_PORT)).toBeDefined();
expect(startedContainer.getHttpPort()).toBeDefined();
expect(startedContainer.getGrpcPort()).toBeDefined();
expect(startedContainer.getHttpEndpoint()).toBeDefined();
expect(startedContainer.getGrpcEndpoint()).toBeDefined();
await startedContainer.stop();
await network.stop();
}, 120_000);
Expand Down
Loading