1

I am trying to use FluentDocker to run the tests against MongoDB, but I cannot connect to it, see the code below.

            [Fact]
            public async Task TestMongoDbConnection3()
            {
                const string root = "root";
                const string secret = "secret";
                
                using (
                    var container =
                        new Builder().UseContainer()
                            .UseImage("mongo")
                            .WithEnvironment($"MONGO_INITDB_ROOT_USERNAME:{root}")
                            .WithEnvironment($"MONGO_INITDB_ROOT_PASSWORD:{secret}")
                            .ExposePort(27018)
                            .WaitForPort("27018/tcp", 30000 /*30s*/)
                        .Build()
                        .Start())
            {
                var config = container.GetConfiguration(true);
                Assert.Equal(ServiceRunningState.Running, config.State.ToServiceState());
                
                const string connectionString = "mongodb://root:secret@localhost:27018";

                // insert data 
                const string collectionName = "Users";
                
                var mongoClient = new MongoClient(connectionString);
                var database = mongoClient.GetDatabase ("Users");
                var collection = database.GetCollection<User>(collectionName);
                
                const int id = 1;
                var data = new Fixture().Build<User>()
                    .With(x => x.Id, id)
                    .Create();

                // delete if exists, but got timeout after 30000 ms
                await collection.DeleteOneAsync(x => x.Id == data.Id);
                
                ...
            }
        }

I tried to replace the localhost for the IP from config (config.NetworkSettings.IPAddress), but still getting the timeout.

Any idea?

Many thanks,

J. Viégas
  • 135
  • 1
  • 11

1 Answers1

0

here is the fix var ep = container.ToHostExposedEndpoint("27017/tcp"); which works when running locally on my macos.

here is the full code. It still falling when it is running on a docker-in-docker image (timeout).

[Fact]
public void Test_Run_PingCommand_returns_True()
{
    const string root = "root";
    const string secret = "secret";

    using (
        var container =
            new Builder().UseContainer()
                .UseImage("mongo")
                .WithEnvironment(new []{$"MONGO_INITDB_ROOT_USERNAME={root}",
                    $"MONGO_INITDB_ROOT_PASSWORD={secret}"})
                .ExposePort(27017)
                .WaitForPort("27017/tcp", 30000 /*30s*/)
                .Build()
                .Start())
    {
        var config = container.GetConfiguration(true);
        Assert.Equal(ServiceRunningState.Running, config.State.ToServiceState());
       
        var ep = container.ToHostExposedEndpoint("27017/tcp");
       
        var connectionString =
            $"mongodb://{root}:{secret}@{ep}";
       
        var setting = new UsageDbSettings();
        var mongoClient = new MongoClient(connectionString);
        var database = mongoClient.GetDatabase(setting.DatabaseName);
        
        var isMongoLive = database.RunCommandAsync((Command<BsonDocument>)"{ping:1}").Wait(30000);
       
        Assert.True(isMongoLive);
    }
}

J. Viégas
  • 135
  • 1
  • 11
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 12 '21 at 08:49