Skip to content
This repository has been archived by the owner on Apr 13, 2020. It is now read-only.

Check collections if database is defined on connection string #98

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion build/dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<BeatPulsePackageVersion>3.0.0</BeatPulsePackageVersion>
<BeatPulseAzureStoragePackageVersion>3.0.0</BeatPulseAzureStoragePackageVersion>
<BeatPulseDocumentDbPackageVersion>3.0.0</BeatPulseDocumentDbPackageVersion>
<BeatPulseMongoDbPackageVersion>3.0.0</BeatPulseMongoDbPackageVersion>
<BeatPulseMongoDbPackageVersion>3.0.1</BeatPulseMongoDbPackageVersion>
<BeatPulseMySqlPackageVersion>3.0.0</BeatPulseMySqlPackageVersion>
<BeatpulseNpgSqlPackageVersion>3.0.0</BeatpulseNpgSqlPackageVersion>
<BeatpulseRedisPackageVersion>3.0.1</BeatpulseRedisPackageVersion>
Expand Down
13 changes: 11 additions & 2 deletions src/BeatPulse.MongoDb/MongoDbLiveness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@ public async Task<LivenessResult> IsHealthy(LivenessExecutionContext context, Ca
{
_logger?.LogInformation($"{nameof(MongoDbLiveness)} is checking the MongoDb database.");

await new MongoClient(_connectionString)
.ListDatabasesAsync(cancellationToken);
var client = new MongoClient(_connectionString);

var url = new MongoUrl(_connectionString);
if (string.IsNullOrWhiteSpace(url.DatabaseName))
tiagor87 marked this conversation as resolved.
Show resolved Hide resolved
{
await client.ListDatabasesAsync(cancellationToken);
}
else
{
await client.GetDatabase(url.DatabaseName).ListCollectionsAsync(cancellationToken: cancellationToken);
}

_logger?.LogInformation($"The {nameof(MongoDbLiveness)} check success.");

Expand Down
47 changes: 47 additions & 0 deletions tests/FunctionalTests/BeatPulse.MongoDb/BeatPulseLivenessTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,52 @@ public async Task return_false_if_mongodb_is_not_available()
response.StatusCode
.Should().Be(HttpStatusCode.ServiceUnavailable);
}

[Fact]
public async Task return_true_if_mongodb_is_available_and_database_is_defined()
{
var connectionString = @"mongodb://localhost:27017/database";

var webHostBuilder = new WebHostBuilder()
.UseStartup<DefaultStartup>()
.UseBeatPulse()
.ConfigureServices(services =>
{
services.AddBeatPulse(context =>
{
context.AddMongoDb(connectionString);
});
});

var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"{BeatPulseKeys.BEATPULSE_DEFAULT_PATH}")
.GetAsync();

response.EnsureSuccessStatusCode();
}

[Fact]
public async Task return_false_if_mongodb_is_not_available_and_database_is_defined()
{
var webHostBuilder = new WebHostBuilder()
.UseStartup<DefaultStartup>()
.UseBeatPulse()
.ConfigureServices(services =>
{
services.AddBeatPulse(context =>
{
context.AddMongoDb("mongodb://nonexistingdomain:27017/database");
});
});

var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"{BeatPulseKeys.BEATPULSE_DEFAULT_PATH}")
.GetAsync();

response.StatusCode
.Should().Be(HttpStatusCode.ServiceUnavailable);
}
}
}