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
converter tests
Signed-off-by: Joe Bowbeer <joe.bowbeer@gmail.com>
  • Loading branch information
joebowbeer committed May 27, 2025
commit 95fb35b2140de98691f131953aa2924a2f12b04d
59 changes: 59 additions & 0 deletions examples/testcontainers/src/Configuration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
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 { DAPR_RUNTIME_IMAGE, DaprContainer } from "./DaprContainer";
import {
AppHttpPipeline,
Configuration,
OtelTracingConfigurationSettings,
TracingConfigurationSettings,
} from "./Configuration";

describe("Configuration", () => {
it("should convert to YAML", () => {
const otel = new OtelTracingConfigurationSettings("localhost:4317", false, "grpc");
const tracing = new TracingConfigurationSettings("1", true, otel);
const appHttpPipeline = new AppHttpPipeline([
{
name: "alias",
type: "middleware.http.routeralias",
},
]);
const dapr = new DaprContainer(DAPR_RUNTIME_IMAGE)
.withAppName("dapr-app")
.withAppPort(8081)
.withConfiguration(new Configuration("my-config", tracing, appHttpPipeline))
.withAppChannelAddress("host.testcontainers.internal");
const configuration = dapr.getConfiguration();
expect(configuration).toBeDefined();
const configurationYaml = configuration?.toYaml();
const expectedConfigurationYaml =
"apiVersion: dapr.io/v1alpha1\n" +
"kind: Configuration\n" +
"metadata:\n" +
" name: my-config\n" +
"spec:\n" +
" tracing:\n" +
' samplingRate: "1"\n' +
" stdout: true\n" +
" otel:\n" +
" endpointAddress: localhost:4317\n" +
" isSecure: false\n" +
" protocol: grpc\n" +
" appHttpPipeline:\n" +
" handlers:\n" +
" - name: alias\n" +
" type: middleware.http.routeralias\n";
expect(configurationYaml).toEqual(expectedConfigurationYaml);
});
});
34 changes: 21 additions & 13 deletions examples/testcontainers/src/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,33 @@ export type ListEntry = {
type: string;
}

export type AppHttpPipeline = {
handlers: ListEntry[];
export class AppHttpPipeline {
constructor(
public readonly handlers: ListEntry[]
) {}
}

export type OtelTracingConfigurationSettings = {
endpointAddress?: string;
isSecure?: boolean;
protocol?: string;
export class OtelTracingConfigurationSettings {
constructor(
public readonly endpointAddress?: string,
public readonly isSecure?: boolean,
public readonly protocol?: string
) {}
}

export type ZipkinTracingConfigurationSettings = {
endpointAddress?: string;
export class ZipkinTracingConfigurationSettings {
constructor(
public readonly endpointAddress?: string
) {}
}

export type TracingConfigurationSettings = {
samplingRate?: string;
stdout?: boolean;
otel?: OtelTracingConfigurationSettings;
zipkin?: ZipkinTracingConfigurationSettings;
export class TracingConfigurationSettings {
constructor(
public readonly samplingRate?: string,
public readonly stdout?: boolean,
public readonly otel?: OtelTracingConfigurationSettings,
public readonly zipkin?: ZipkinTracingConfigurationSettings
) {}
}

/**
Expand Down
32 changes: 16 additions & 16 deletions examples/testcontainers/src/DaprContainer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
// import { GenericContainer, Network, Wait } from "testcontainers";
import path from "node:path";
import { Network } from "testcontainers";
import { DaprContainer, DAPR_RUNTIME_IMAGE } from "./DaprContainer";
import { DAPR_RUNTIME_IMAGE, DaprContainer } from "./DaprContainer";

// jest.setTimeout(120_000);

Expand All @@ -38,7 +38,7 @@ describe("DaprContainer", () => {
await network.stop();
}, 120_000);
it("should load component from path", async () => {
const componentPath = path.join(__dirname, '__fixtures__', 'dapr-resources', 'statestore.yaml');
const componentPath = path.join(__dirname, "__fixtures__", "dapr-resources", "statestore.yaml");
const dapr = new DaprContainer(DAPR_RUNTIME_IMAGE)
.withAppName("dapr-app")
.withAppPort(8081)
Expand All @@ -52,20 +52,20 @@ describe("DaprContainer", () => {

const componentYaml = kvstore.toYaml();
const expectedComponentYaml =
"apiVersion: dapr.io/v1alpha1\n"
+ "kind: Component\n"
+ "metadata:\n"
+ " name: statestore\n"
+ "spec:\n"
+ " type: state.redis\n"
+ " version: v1\n"
+ " metadata:\n"
+ " - name: keyPrefix\n"
+ " value: name\n"
+ " - name: redisHost\n"
+ " value: redis:6379\n"
+ " - name: redisPassword\n"
+ " value: \"\"\n"
"apiVersion: dapr.io/v1alpha1\n" +
"kind: Component\n" +
"metadata:\n" +
" name: statestore\n" +
"spec:\n" +
" type: state.redis\n" +
" version: v1\n" +
" metadata:\n" +
" - name: keyPrefix\n" +
" value: name\n" +
" - name: redisHost\n" +
" value: redis:6379\n" +
" - name: redisPassword\n" +
' value: ""\n';

expect(componentYaml).toBe(expectedComponentYaml);
});
Expand Down
37 changes: 37 additions & 0 deletions examples/testcontainers/src/HttpEndpoint.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
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 { DAPR_RUNTIME_IMAGE, DaprContainer } from "./DaprContainer";
import { HttpEndpoint } from "./HttpEndpoint";

describe("HttpEndpoint", () => {
it("should convert to YAML", () => {
const dapr = new DaprContainer(DAPR_RUNTIME_IMAGE)
.withAppName("dapr-app")
.withAppPort(8081)
.withHttpEndpoint(new HttpEndpoint("my-endpoint", "http://localhost:8080"))
.withAppChannelAddress("host.testcontainers.internal");
const endpoints = dapr.getHttpEndpoints();
expect(endpoints.length).toBe(1);
const endpoint = endpoints[0];
const endpointYaml = endpoint.toYaml();
const expectedEndpointYaml =
"apiVersion: dapr.io/v1alpha1\n" +
"kind: HTTPEndpoint\n" +
"metadata:\n" +
" name: my-endpoint\n" +
"spec:\n" +
" baseUrl: http://localhost:8080\n";
expect(endpointYaml).toEqual(expectedEndpointYaml);
});
});
39 changes: 39 additions & 0 deletions examples/testcontainers/src/Subscription.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
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 { DAPR_RUNTIME_IMAGE, DaprContainer } from "./DaprContainer";
import { Subscription } from "./Subscription";

describe("Subscription", () => {
it("should convert to YAML", () => {
const dapr = new DaprContainer(DAPR_RUNTIME_IMAGE)
.withAppName("dapr-app")
.withAppPort(8081)
.withSubscription(new Subscription("my-subscription", "pubsub", "topic", "/events"))
.withAppChannelAddress("host.testcontainers.internal");
const subscriptions = dapr.getSubscriptions();
expect(subscriptions.length).toBe(1);
const subscription = subscriptions[0];
const subscriptionYaml = subscription.toYaml();
const expectedSubscriptionYaml =
"apiVersion: dapr.io/v1alpha1\n" +
"kind: Subscription\n" +
"metadata:\n" +
" name: my-subscription\n" +
"spec:\n" +
" pubsubname: pubsub\n" +
" topic: topic\n" +
" route: /events\n";
expect(subscriptionYaml).toEqual(expectedSubscriptionYaml);
});
});
18 changes: 2 additions & 16 deletions examples/testcontainers/src/Subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@ export class Subscription {
name: string;
};
spec: {
pubsub: string;
pubsubname: string;
topic: string;
route: string;
metadata: {
name: string;
value: string;
}[];
};
} = {
apiVersion: "dapr.io/v1alpha1",
Expand All @@ -44,19 +40,9 @@ export class Subscription {
name: this.name
},
spec: {
pubsub: this.pubsubName,
pubsubname: this.pubsubName,
topic: this.topic,
route: this.route,
metadata: [
{
name: "topic",
value: this.topic
},
{
name: "pubsubname",
value: this.pubsubName
}
]
}
};
return YAML.stringify(subscriptionObj, { indentSeq: false });
Expand Down