forked from testcontainers/testcontainers-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIWaitForContainerOS.cs
More file actions
168 lines (154 loc) · 9.08 KB
/
IWaitForContainerOS.cs
File metadata and controls
168 lines (154 loc) · 9.08 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
namespace DotNet.Testcontainers.Configurations
{
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Text.RegularExpressions;
using DotNet.Testcontainers.Containers;
using JetBrains.Annotations;
/// <summary>
/// Collection of pre-configured strategies to wait until the container is up and running.
/// </summary>
[PublicAPI]
public interface IWaitForContainerOS
{
/// <summary>
/// Adds a custom wait strategy to the wait strategies collection.
/// </summary>
/// <param name="waitUntil">The wait strategy until the container is ready.</param>
/// <param name="waitStrategyModifier">The wait strategy modifier to cancel the readiness check.</param>
/// <returns>A configured instance of <see cref="IWaitForContainerOS" />.</returns>
/// <remarks>Already contains <see cref="UntilContainerIsRunning" /> as default wait strategy.</remarks>
[PublicAPI]
IWaitForContainerOS AddCustomWaitStrategy(IWaitUntil waitUntil, Action<IWaitStrategy> waitStrategyModifier = null);
/// <summary>
/// Waits until the command is completed successfully.
/// </summary>
/// <param name="command">The command to be executed.</param>
/// <returns>A configured instance of <see cref="IWaitForContainerOS" />.</returns>
/// <remarks>
/// Does not invoke the operating system command shell.
/// Normal shell processing does not happen. Expects the exit code to be 0.
/// </remarks>
[PublicAPI]
IWaitForContainerOS UntilCommandIsCompleted(params string[] command);
/// <summary>
/// Waits until the command is completed successfully.
/// </summary>
/// <param name="command">The command to be executed.</param>
/// <param name="waitStrategyModifier">The wait strategy modifier to cancel the readiness check.</param>
/// <returns>A configured instance of <see cref="IWaitForContainerOS" />.</returns>
/// <remarks>Invokes the operating system command shell. Expects the exit code to be 0.</remarks>
[PublicAPI]
IWaitForContainerOS UntilCommandIsCompleted(string command, Action<IWaitStrategy> waitStrategyModifier = null);
/// <summary>
/// Waits until the command is completed successfully.
/// </summary>
/// <param name="command">The command to be executed.</param>
/// <param name="waitStrategyModifier">The wait strategy modifier to cancel the readiness check.</param>
/// <returns>A configured instance of <see cref="IWaitForContainerOS" />.</returns>
/// <remarks>
/// Does not invoke the operating system command shell.
/// Normal shell processing does not happen. Expects the exit code to be 0.
/// </remarks>
[PublicAPI]
IWaitForContainerOS UntilCommandIsCompleted(IEnumerable<string> command, Action<IWaitStrategy> waitStrategyModifier = null);
/// <summary>
/// Waits until the port is available.
/// </summary>
/// <param name="port">The port to be checked.</param>
/// <param name="waitStrategyModifier">The wait strategy modifier to cancel the readiness check.</param>
/// <returns>A configured instance of <see cref="IWaitForContainerOS" />.</returns>
[PublicAPI]
[Obsolete("Use UntilInternalTcpPortIsAvailable or UntilExternalTcpPortIsAvailable instead. This method corresponds to the internal variant.")]
IWaitForContainerOS UntilPortIsAvailable(int port, Action<IWaitStrategy> waitStrategyModifier = null);
/// <summary>
/// Waits until a TCP port is available from within the container itself.
/// This verifies that a service inside the container is listening on the specified port.
/// </summary>
/// <param name="containerPort">The TCP port of the service running inside the container.</param>
/// <param name="waitStrategyModifier">The wait strategy modifier to cancel the readiness check.</param>
/// <returns>A configured instance of <see cref="IWaitForContainerOS" />.</returns>
[PublicAPI]
IWaitForContainerOS UntilInternalTcpPortIsAvailable(int containerPort, Action<IWaitStrategy> waitStrategyModifier = null);
/// <summary>
/// Waits until a TCP port is available from the test host to the container.
/// This verifies that the port is exposed and reachable externally.
/// </summary>
/// <remarks>
/// This does not necessarily mean that the TCP connection to the service running inside
/// the container was successful. For container runtimes like Docker Desktop, Podman, or similar,
/// this usually only indicates that the port has been mapped and that a connection could be
/// established to the host-side proxy that maps the port.
///
/// This wait strategy is particularly useful for container runtimes that may take some time
/// to finish setting up port mappings. In some cases, other strategies such as log-based
/// readiness checks may indicate readiness before the runtime has fully configured the port
/// mapping, leading to connection failures. This strategy helps to avoid that race condition.
/// </remarks>
/// <param name="containerPort">The TCP port of the service running inside the container.</param>
/// <param name="waitStrategyModifier">The wait strategy modifier to cancel the readiness check.</param>
/// <returns>A configured instance of <see cref="IWaitForContainerOS" />.</returns>
[PublicAPI]
IWaitForContainerOS UntilExternalTcpPortIsAvailable(int containerPort, Action<IWaitStrategy> waitStrategyModifier = null);
/// <summary>
/// Waits until the file exists.
/// </summary>
/// <param name="filePath">The file path to be checked.</param>
/// <param name="fileSystem">The file system to be checked.</param>
/// <param name="waitStrategyModifier">The wait strategy modifier to cancel the readiness check.</param>
/// <returns>A configured instance of <see cref="IWaitForContainerOS" />.</returns>
[PublicAPI]
IWaitForContainerOS UntilFileExists(string filePath, FileSystem fileSystem = FileSystem.Host, Action<IWaitStrategy> waitStrategyModifier = null);
/// <summary>
/// Waits until the message is logged.
/// </summary>
/// <param name="pattern">The regular expression that matches the log message.</param>
/// <param name="waitStrategyModifier">The wait strategy modifier to cancel the readiness check.</param>
/// <returns>A configured instance of <see cref="IWaitForContainerOS" />.</returns>
[PublicAPI]
IWaitForContainerOS UntilMessageIsLogged(string pattern, Action<IWaitStrategy> waitStrategyModifier = null);
/// <summary>
/// Waits until the message is logged.
/// </summary>
/// <param name="pattern">The regular expression that matches the log message.</param>
/// <param name="waitStrategyModifier">The wait strategy modifier to cancel the readiness check.</param>
/// <returns>A configured instance of <see cref="IWaitForContainerOS" />.</returns>
[PublicAPI]
IWaitForContainerOS UntilMessageIsLogged(Regex pattern, Action<IWaitStrategy> waitStrategyModifier = null);
/// <summary>
/// Waits until the http request is completed successfully.
/// </summary>
/// <param name="request">The http request to be executed.</param>
/// <param name="waitStrategyModifier">The wait strategy modifier to cancel the readiness check.</param>
/// <returns>A configured instance of <see cref="IWaitForContainerOS" />.</returns>
[PublicAPI]
IWaitForContainerOS UntilHttpRequestIsSucceeded(Func<HttpWaitStrategy, HttpWaitStrategy> request, Action<IWaitStrategy> waitStrategyModifier = null);
/// <summary>
/// Waits until the container is healthy.
/// </summary>
/// <param name="failingStreak">The number of attempts before an exception is thrown.</param>
/// <param name="waitStrategyModifier">The wait strategy modifier to cancel the readiness check.</param>
/// <returns>A configured instance of <see cref="IWaitForContainerOS" />.</returns>
/// <exception cref="TimeoutException">Thrown when number of failed operations exceeded <paramref name="failingStreak" />.</exception>
[PublicAPI]
IWaitForContainerOS UntilContainerIsHealthy(long failingStreak = 3, Action<IWaitStrategy> waitStrategyModifier = null);
/// <summary>
/// Waits until a successful connection to the database can be established.
/// </summary>
/// <remarks>
/// To use this wait strategy, the container must implement the <see cref="IDatabaseContainer" /> interface.
/// </remarks>
/// <param name="dbProviderFactory">The <see cref="DbProviderFactory" /> used to create the database connection.</param>
/// <param name="waitStrategyModifier">The wait strategy modifier to cancel the readiness check.</param>
/// <returns>A configured instance of <see cref="IWaitForContainerOS" />.</returns>
[PublicAPI]
IWaitForContainerOS UntilDatabaseIsAvailable(DbProviderFactory dbProviderFactory, Action<IWaitStrategy> waitStrategyModifier = null);
/// <summary>
/// Returns a collection with all configured wait strategies.
/// </summary>
/// <returns>Returns a list with all configured wait strategies.</returns>
[PublicAPI]
IEnumerable<WaitStrategy> Build();
}
}