-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.swift
More file actions
311 lines (227 loc) · 8.59 KB
/
Copy pathmain.swift
File metadata and controls
311 lines (227 loc) · 8.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import Foundation
import Testcontainers
// MARK: - Example 1: Basic Container Usage
@main
func exampleBasicContainer() async throws {
print("=== Example 1: Basic Container Usage ===")
let container = ContainerBuilder("nginx:latest")
.withName("my-nginx")
.withPortBinding(80, assignRandomHostPort: true)
.withLabel("env", "example")
.build()
let actualContainer = try await ContainerBuilder("nginx:latest")
.withPortBinding(80, assignRandomHostPort: true)
.withWaitStrategy(.tcp(port: 80, timeout: 30))
.buildAsync()
try await actualContainer.start()
let port = try actualContainer.getMappedPort(80)
print("Nginx container started on port: \(port)")
let state = try await actualContainer.getState()
print("Container state: \(state)")
try await actualContainer.stop()
print("Container stopped\n")
}
// MARK: - Example 2: PostgreSQL Database
func examplePostgresql() async throws {
print("=== Example 2: PostgreSQL Container ===")
let postgres = try await PostgresContainer(version: "15")
.withDatabase("testdb")
.withUsername("testuser")
.withPassword("testpass")
.start()
defer { try? Task { try await postgres.stop() }.value }
let connectionString = try postgres.getConnectionString()
print("PostgreSQL is running at: \(connectionString)")
print()
}
// MARK: - Example 3: MySQL Database
func exampleMysql() async throws {
print("=== Example 3: MySQL Container ===")
let mysql = try await MySqlContainer(version: "8.0")
.withDatabase("testdb")
.withUsername("testuser")
.withPassword("testpass")
.start()
defer { try? Task { try await mysql.stop() }.value }
let connectionString = try mysql.getConnectionString()
print("MySQL is running at: \(connectionString)")
print()
}
// MARK: - Example 4: Redis
func exampleRedis() async throws {
print("=== Example 4: Redis Container ===")
let redis = try await RedisContainer(version: "7")
.start()
defer { try? Task { try await redis.stop() }.value }
let redisURL = try redis.getRedisURL()
print("Redis is running at: \(redisURL)")
print()
}
// MARK: - Example 5: MongoDB
func exampleMongoDB() async throws {
print("=== Example 5: MongoDB Container ===")
let mongo = try await MongoDbContainer(version: "6.0")
.withUsername("admin")
.withPassword("admin")
.start()
defer { try? Task { try await mongo.stop() }.value }
let connectionString = try mongo.getConnectionString()
print("MongoDB is running at: \(connectionString)")
print()
}
// MARK: - Example 6: Multiple Containers with Network
func exampleMultipleContainersWithNetwork() async throws {
print("=== Example 6: Multiple Containers with Network ===")
// Create a custom network
let network = try await NetworkBuilder("test-network")
.withDriver("bridge")
.build()
// Create PostgreSQL container
let postgres = try await ContainerBuilder("postgres:15")
.withName("postgres-service")
.withEnvironment("POSTGRES_PASSWORD", "password")
.withWaitStrategy(.log(message: "database system is ready"))
.withNetwork(network)
.withNetworkAliases(["postgres"])
.buildAsync()
try await postgres.start()
print("PostgreSQL started on network")
print("Other containers can reach it using hostname: postgres")
try await postgres.stop()
print("PostgreSQL stopped\n")
}
// MARK: - Example 7: Microsoft SQL Server
func exampleMsSql() async throws {
print("=== Example 7: Microsoft SQL Server Container ===")
let mssql = try await MsSqlContainer()
.withDatabase("testdb")
.withPassword("yourStrong(!)Password")
.start()
defer { try? Task { try await mssql.stop() }.value }
let connectionString = try mssql.getConnectionString()
print("MS SQL Server is running at: \(connectionString)")
print()
}
// MARK: - Example 8: RabbitMQ
func exampleRabbitMq() async throws {
print("=== Example 8: RabbitMQ Container ===")
let rabbitmq = try await RabbitMqContainer(version: "3.11")
.withUsername("guest")
.withPassword("guest")
.start()
defer { try? Task { try await rabbitmq.stop() }.value }
let connectionString = try rabbitmq.getConnectionString()
print("RabbitMQ is running at: \(connectionString)")
print()
}
// MARK: - Example 9: Kafka
func exampleKafka() async throws {
print("=== Example 9: Kafka Container ===")
let kafka = try await KafkaContainer()
.start()
defer { try? Task { try await kafka.stop() }.value }
let bootstrapServers = kafka.getBootstrapServers()
print("Kafka bootstrap servers: \(bootstrapServers)")
print()
}
// MARK: - Example 10: Elasticsearch
func exampleElasticsearch() async throws {
print("=== Example 10: Elasticsearch Container ===")
let elasticsearch = try await ElasticsearchContainer(version: "8.6.1")
.withPassword("elastic")
.start()
defer { try? Task { try await elasticsearch.stop() }.value }
let connectionString = try elasticsearch.getConnectionString()
print("Elasticsearch is running at: \(connectionString)")
print()
}
// MARK: - Example 11: Azurite (Azure Storage Emulator)
func exampleAzurite() async throws {
print("=== Example 11: Azurite Container ===")
let azurite = try await AzuriteContainer()
.start()
defer { try? Task { try await azurite.stop() }.value }
let connectionString = try azurite.getConnectionString()
print("Azurite connection string: \(connectionString)")
let blobEndpoint = try azurite.getBlobEndpoint()
print("Blob endpoint: \(blobEndpoint)")
print()
}
// MARK: - Example 12: LocalStack (AWS Emulator)
func exampleLocalStack() async throws {
print("=== Example 12: LocalStack Container ===")
let localstack = try await LocalStackContainer()
.start()
defer { try? Task { try await localstack.stop() }.value }
let endpoint = try localstack.getEndpoint()
print("LocalStack endpoint: \(endpoint)")
print()
}
// MARK: - Example 13: Wait Strategies
func exampleWaitStrategies() async throws {
print("=== Example 13: Wait Strategies ===")
// HTTP wait strategy
let webContainer = try await ContainerBuilder("httpbin/httpbin:latest")
.withPortBinding(80, assignRandomHostPort: true)
.withWaitStrategy(.http(port: 80, path: "/uuid", timeout: 60))
.buildAsync()
try await webContainer.start()
print("HTTP container is ready")
try await webContainer.stop()
// Combined wait strategies
let dbContainer = try await ContainerBuilder("postgres:15")
.withEnvironment("POSTGRES_PASSWORD", "password")
.withPortBinding(5432, assignRandomHostPort: true)
.withWaitStrategy(
.all(
.tcp(port: 5432, timeout: 60),
.log(message: "database system is ready", timeout: 60)
)
)
.buildAsync()
try await dbContainer.start()
print("Database container is ready (used combined wait strategies)")
try await dbContainer.stop()
print()
}
// MARK: - Example 14: Container Logs
func exampleContainerLogs() async throws {
print("=== Example 14: Container Logs ===")
let container = try await ContainerBuilder("alpine:latest")
.withCmd(["sh", "-c", "echo 'Hello from container' && sleep 10"])
.buildAsync()
try await container.start()
try await Task.sleep(nanoseconds: 2_000_000_000) // Wait 2 seconds
let logs = try await container.getLogs()
print("Container logs:")
print(logs)
try await container.stop()
print()
}
// MARK: - Main
@main
struct ExamplesApp {
static func main() async {
print("Testcontainers Swift Examples\n")
do {
// Uncomment examples to run them
// try await exampleBasicContainer()
// try await examplePostgresql()
// try await exampleMysql()
// try await exampleRedis()
// try await exampleMongoDB()
// try await exampleMultipleContainersWithNetwork()
// try await exampleMsSql()
// try await exampleRabbitMq()
// try await exampleKafka()
// try await exampleElasticsearch()
// try await exampleAzurite()
// try await exampleLocalStack()
// try await exampleWaitStrategies()
// try await exampleContainerLogs()
print("Examples completed successfully!")
} catch {
print("Error: \(error)")
}
}
}