Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit dabee55

Browse files
committed
feat: initial code for compose and waiting for it all the be ready - needs developement
Signed-off-by: Tyrrellion <[email protected]>
1 parent c7536b1 commit dabee55

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Microsoft.Azure.Functions.Worker;
2+
using Microsoft.Azure.Functions.Worker.Http;
3+
using Microsoft.Extensions.Logging;
4+
using System.Net;
5+
6+
public class HealthCheckFunction
7+
{
8+
[Function("HealthCheck")]
9+
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "health")] HttpRequestData req)
10+
{
11+
var response = req.CreateResponse(HttpStatusCode.OK);
12+
return response;
13+
}
14+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
using System.Threading.Tasks;
5+
using Microsoft.Data.SqlClient;
6+
using Moq;
7+
8+
namespace ServiceLayer.Mesh.Tests.Integration;
9+
10+
public class IntegrationTests
11+
{
12+
private const string ConnectionString = "Server=localhost,1433;User Id=sa;Password=YourPassword123;TrustServerCertificate=True;";
13+
14+
public IntegrationTests()
15+
{
16+
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
17+
if (environment == null)
18+
{
19+
throw new InvalidOperationException("ASPNETCORE_ENVIRONMENT environment variable is not set of is empty.");
20+
}
21+
if (environment == "development")
22+
{
23+
RunCommand("podman compose up");
24+
}
25+
if (environment == "production")
26+
{
27+
RunCommand("docker compose up");
28+
}
29+
30+
// Wait for SQL Server to be reachable
31+
//await WaitForSqlServerAsync();
32+
}
33+
34+
public void Teardown()
35+
{
36+
// Stop containers
37+
RunCommand("docker compose down");
38+
}
39+
40+
[Fact]
41+
public async Task ShouldWriteToDatabase()
42+
{
43+
using var sql = new SqlConnection(ConnectionString);
44+
await sql.OpenAsync();
45+
46+
// var count = await sql.ExecuteScalarAsync<int>("SELECT COUNT(*) FROM SomeTable");
47+
// Assert.IsTrue(count > 0, "Expected at least one row in SomeTable.");
48+
}
49+
50+
private void RunCommand(string command)
51+
{
52+
var psi = new ProcessStartInfo("cmd", $"/c {command}")
53+
{
54+
RedirectStandardOutput = true,
55+
RedirectStandardError = true,
56+
UseShellExecute = false,
57+
CreateNoWindow = true
58+
};
59+
60+
using var process = Process.Start(psi);
61+
process.WaitForExit();
62+
63+
if (process.ExitCode != 0)
64+
{
65+
throw new Exception($"Command failed: {command}\n{process.StandardError.ReadToEnd()}");
66+
}
67+
}
68+
69+
private async Task WaitForSqlServerAsync(int timeoutSeconds = 60)
70+
{
71+
var start = DateTime.UtcNow;
72+
while ((DateTime.UtcNow - start).TotalSeconds < timeoutSeconds)
73+
{
74+
try
75+
{
76+
using var sql = new SqlConnection(ConnectionString);
77+
await sql.OpenAsync();
78+
return; // Success
79+
}
80+
catch
81+
{
82+
await Task.Delay(1000);
83+
}
84+
}
85+
86+
throw new TimeoutException("SQL Server did not become available in time.");
87+
}
88+
}

0 commit comments

Comments
 (0)