1

我正在尝试使用 FluentDocker 对 MongoDB 运行测试,但我无法连接到它,请参阅下面的代码。

            [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);
                
                ...
            }
        }

我尝试从 config (config.NetworkSettings.IPAddress) 替换 IP 的 localhost,但仍然超时。

任何想法?

非常感谢,

4

1 回答 1

0

var ep = container.ToHostExposedEndpoint("27017/tcp");这是在我的 macos 上本地运行时有效的修复程序。

这是完整的代码。在 docker-in-docker 映像上运行时它仍然会下降(超时)。

[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);
    }
}

于 2021-10-12T08:45:01.793 回答