Skip to content

Commit

Permalink
feat: Ignore FROM args when pre-pulling images (#1016)
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn authored Oct 6, 2023
1 parent 69292d1 commit 5a52c62
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 42 deletions.
16 changes: 6 additions & 10 deletions docs/modules/elasticsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,15 @@ using Elastic.Transport;
using Testcontainers.Elasticsearch;
using Xunit;

namespace TestcontainersModules;

public class ElasticsearchContainerTest : IAsyncLifetime
public sealed class ElasticsearchContainerTest : IAsyncLifetime
{
private readonly ElasticsearchContainer elasticsearch
= new ElasticsearchBuilder()
.Build();
private readonly ElasticsearchContainer _elasticsearch
= new ElasticsearchBuilder().Build();

[Fact]
public async Task ReadFromElasticsearch()
{
var connectionString = elasticsearch.GetConnectionString();
var settings = new ElasticsearchClientSettings(new Uri(connectionString));
var settings = new ElasticsearchClientSettings(new Uri(_elasticsearch.GetConnectionString()));
settings.ServerCertificateValidationCallback(CertificateValidations.AllowAll);

var client = new ElasticsearchClient(settings);
Expand All @@ -43,10 +39,10 @@ public class ElasticsearchContainerTest : IAsyncLifetime
}

public Task InitializeAsync()
=> elasticsearch.StartAsync();
=> _elasticsearch.StartAsync();

public Task DisposeAsync()
=> elasticsearch.DisposeAsync().AsTask();
=> _elasticsearch.DisposeAsync().AsTask();
}
```

Expand Down
13 changes: 6 additions & 7 deletions docs/modules/mongodb.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,26 @@ using Xunit;

namespace TestcontainersModules;

public class MongoDbContainerTest : IAsyncLifetime
public sealed class MongoDbContainerTest : IAsyncLifetime
{
private readonly MongoDbContainer mongoDbContainer =
new MongoDbBuilder()
.Build();
private readonly MongoDbContainer _mongoDbContainer =
new MongoDbBuilder().Build();

[Fact]
public async Task ReadFromMongoDbDatabase()
{
var client = new MongoClient(mongoDbContainer.GetConnectionString());
var client = new MongoClient(_mongoDbContainer.GetConnectionString());

using var databases = await client.ListDatabasesAsync();

Assert.True(await databases.AnyAsync());
}

public Task InitializeAsync()
=> mongoDbContainer.StartAsync();
=> _mongoDbContainer.StartAsync();

public Task DisposeAsync()
=> mongoDbContainer.DisposeAsync().AsTask();
=> _mongoDbContainer.DisposeAsync().AsTask();
}
```

Expand Down
17 changes: 8 additions & 9 deletions docs/modules/microsoft-sql-server.md → docs/modules/mssql.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,29 @@ using Xunit;

namespace TestcontainersModules;

public class MsSqlServerContainerTest : IAsyncLifetime
public sealed class MsSqlServerContainerTest : IAsyncLifetime
{
private readonly MsSqlContainer msSqlContainer
= new MsSqlBuilder()
.Build();
private readonly MsSqlContainer _msSqlContainer
= new MsSqlBuilder().Build();

[Fact]
public async Task ReadFromMsSqlDatabase()
{
await using var connection = new SqlConnection(msSqlContainer.GetConnectionString());
await using var connection = new SqlConnection(_msSqlContainer.GetConnectionString());
await connection.OpenAsync();

await using var command = connection.CreateCommand();
command.CommandText = "Select 1;";
command.CommandText = "SELECT 1;";

var actual = await command.ExecuteScalarAsync() as int?;
Assert.Equal(expected: 1, actual: actual.GetValueOrDefault());
Assert.Equal(1, actual.GetValueOrDefault());
}

public Task InitializeAsync()
=> msSqlContainer.StartAsync();
=> _msSqlContainer.StartAsync();

public Task DisposeAsync()
=> msSqlContainer.DisposeAsync().AsTask();
=> _msSqlContainer.DisposeAsync().AsTask();
}
```

Expand Down
13 changes: 6 additions & 7 deletions docs/modules/neo4j.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,28 @@ using Xunit;

namespace TestcontainersModules;

public class Neo4jContainerTest : IAsyncLifetime
public sealed class Neo4jContainerTest : IAsyncLifetime
{
private readonly Neo4jContainer neo4jContainer
= new Neo4jBuilder()
.Build();
private readonly Neo4jContainer _neo4jContainer
= new Neo4jBuilder().Build();

[Fact]
public async Task CanReadNeo4jDatabase()
{
const string database = "neo4j";

await using var client = GraphDatabase.Driver(neo4jContainer.GetConnectionString());
await using var client = GraphDatabase.Driver(_neo4jContainer.GetConnectionString());

await using var session = client.AsyncSession(cfg => cfg.WithDatabase(database));

Assert.Equal(database, session.SessionConfig.Database);
}

public Task InitializeAsync()
=> neo4jContainer.StartAsync();
=> _neo4jContainer.StartAsync();

public Task DisposeAsync()
=> neo4jContainer.DisposeAsync().AsTask();
=> _neo4jContainer.DisposeAsync().AsTask();
}
```

Expand Down
4 changes: 4 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,9 @@ nav:
- examples/aspnet.md
- Modules:
- modules/index.md
- modules/elasticsearch.md
- modules/mongodb.md
- modules/mssql.md
- modules/neo4j.md
- modules/postgres.md
- modules/rabbitmq.md
10 changes: 5 additions & 5 deletions src/Testcontainers/Images/DockerfileArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ public IEnumerable<IImage> GetBaseImages()
.Where(line => !line.StartsWith("#", StringComparison.Ordinal))
.Select(line => FromLinePattern.Match(line))
.Where(match => match.Success)
// Until now, we are unable to resolve variables within Dockerfiles. Ignore base
// images that utilize variables. Expect them to exist on the host.
.Where(match => !match.Groups[imageGroup].Value.Contains('$'))
.Where(match => !match.Groups[imageGroup].Value.Any(char.IsUpper))
.ToArray();

var stages = lines
Expand All @@ -105,7 +101,11 @@ public IEnumerable<IImage> GetBaseImages()

var images = lines
.Select(match => match.Groups[imageGroup])
.Select(group => group.Value)
.Select(match => match.Value)
// Until now, we are unable to resolve variables within Dockerfiles. Ignore base
// images that utilize variables. Expect them to exist on the host.
.Where(line => !line.Contains('$'))
.Where(line => !line.Any(char.IsUpper))
.Where(value => !stages.Contains(value))
.Distinct()
.Select(value => new DockerImage(value))
Expand Down
2 changes: 1 addition & 1 deletion tests/Testcontainers.Neo4j.Tests/Neo4jContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void SessionReturnsDatabase()
using var driver = GraphDatabase.Driver(_neo4jContainer.GetConnectionString());

// When
using var session = driver.AsyncSession(sessionConfigBuilder => sessionConfigBuilder.WithDatabase("neo4j"));
using var session = driver.AsyncSession(sessionConfigBuilder => sessionConfigBuilder.WithDatabase(database));

// Then
Assert.Equal(database, session.SessionConfig.Database);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ FROM mcr.microsoft.com/dotnet/runtime:6.0 AS runtime
FROM build
FROM build AS publish
FROM mcr.microsoft.com/dotnet/aspnet:6.0.21-jammy-amd64
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/aspnet:6.0.22-jammy-amd64
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ public void DockerfileArchiveGetBaseImages()
// Given
IImage image = new DockerImage("localhost/testcontainers", Guid.NewGuid().ToString("D"), string.Empty);

var dockerfileArchive = new DockerfileArchive("Assets//pullBaseImages/", "Dockerfile", image, NullLogger.Instance);
var dockerfileArchive = new DockerfileArchive("Assets/pullBaseImages/", "Dockerfile", image, NullLogger.Instance);

// When
var baseImages = dockerfileArchive.GetBaseImages();
var baseImages = dockerfileArchive.GetBaseImages().ToArray();

// Then
Assert.Equal(3, baseImages.Count());
Assert.Equal(4, baseImages.Length);
Assert.Contains(baseImages, item => "mcr.microsoft.com/dotnet/sdk:6.0".Equals(item.FullName));
Assert.Contains(baseImages, item => "mcr.microsoft.com/dotnet/runtime:6.0".Equals(item.FullName));
Assert.Contains(baseImages, item => "mcr.microsoft.com/dotnet/aspnet:6.0.21-jammy-amd64".Equals(item.FullName));
Assert.Contains(baseImages, item => "mcr.microsoft.com/dotnet/aspnet:6.0.22-jammy-amd64".Equals(item.FullName));
}

[Fact]
Expand Down

0 comments on commit 5a52c62

Please sign in to comment.