Skip to content

Commit 9850413

Browse files
committed
use fluent docker
1 parent 8cd2e4e commit 9850413

11 files changed

+80
-161
lines changed

docker/Dockerfile

Lines changed: 0 additions & 16 deletions
This file was deleted.

docker/authentication_token.sh

Lines changed: 0 additions & 15 deletions
This file was deleted.

docker/docker-compose.yml

Lines changed: 0 additions & 18 deletions
This file was deleted.

docker/entrypoint.sh

Lines changed: 0 additions & 5 deletions
This file was deleted.

docker/gitlab.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env ruby
2+
3+
prometheus['enable'] = false
4+
prometheus_monitoring['enable'] = false
5+
alertmanager['enable'] = false
6+
grafana['enable'] = false
7+
gitlab_rails['initial_root_password'] = 'password'

docker/init.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env ruby
2+
u = User.first
3+
4+
g = Group.create!(name: 'txxxestgrouxxxp', path: 'txxxestgrouxxxp')
5+
p = Project.create!(namespace: g, creator: u, path: 'txxxestprojecxxxt', name: 'txxxestprojecxxxt')
6+
p.repository.create_if_not_exists
7+
c = p.repository.create_dir(u, 'newdir', message: 'Create newdir', branch_name: 'master')
8+
9+
t = PersonalAccessToken.new({ user: u, name: 'gitlab-api-client', scopes: ['api']})
10+
t.save!
11+
12+
puts t.token

docker/test_setup.sh

Lines changed: 0 additions & 25 deletions
This file was deleted.

docker/volumes/placeholder.gitkeep

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/GitLabApiClient.Test/GitLabApiClient.Test.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>netcoreapp3.0</TargetFramework>
44
<IsPackable>false</IsPackable>
@@ -11,6 +11,7 @@
1111
</ItemGroup>
1212
<ItemGroup>
1313
<PackageReference Include="coverlet.collector" Version="1.1.0" />
14+
<PackageReference Include="Ductus.FluentDocker" Version="2.7.46-beta0080" />
1415
<PackageReference Include="FakeItEasy" Version="5.4.0" />
1516
<PackageReference Include="FluentAssertions" Version="5.9.0" />
1617
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />

test/GitLabApiClient.Test/Utilities/GitLabApiHelper.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ namespace GitLabApiClient.Test.Utilities
55
{
66
internal static class GitLabApiHelper
77
{
8-
private const string PrivateAuthenticationToken = "ElijahBaley";
9-
108
public static GitLabHttpFacade GetFacade()
119
{
1210
var facade = new GitLabHttpFacade(
13-
"http://localhost:9190/api/v4/", new RequestsJsonSerializer(), PrivateAuthenticationToken);
11+
GitLabContainerFixture.GitlabHost, new RequestsJsonSerializer(), GitLabContainerFixture.Token);
1412

1513
return facade;
1614
}
Lines changed: 58 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,92 @@
11
using System;
2-
using System.Diagnostics;
3-
using System.Net.Http;
2+
using System.Collections.Generic;
3+
using System.Linq;
44
using System.Threading.Tasks;
5+
using Ductus.FluentDocker.Builders;
6+
using Ductus.FluentDocker.Model.Common;
7+
using Ductus.FluentDocker.Services;
8+
using Ductus.FluentDocker.Services.Extensions;
9+
using FluentAssertions;
510
using Xunit;
611

712
namespace GitLabApiClient.Test.Utilities
813
{
914
public class GitLabContainerFixture : IAsyncLifetime
1015
{
11-
private const string GitLabContainerPath = "../../../../docker";
16+
private const string InitRb = "/tmp/init.rb";
17+
private const string GitlabRb = "/tmp/gitlab.rb";
18+
private static readonly TemplateString SolutionRootFolder = "${PWD}/../../../../..";
1219

13-
private const string GitLabApiPath = "http://localhost:9190/";
20+
public static string Token { get; private set; }
21+
public static string GitlabHost { get; private set; }
1422

15-
private static readonly TimeSpan TestTimeout = TimeSpan.FromMinutes(10);
16-
17-
private HttpClient _gitLabPingClient;
23+
private IContainerService _gitlabContainer;
24+
private readonly string _gitlabDockerImage = "gitlab/gitlab-ce:12.5.4-ce.0";
1825

1926
public async Task InitializeAsync()
2027
{
21-
_gitLabPingClient = new HttpClient
22-
{
23-
Timeout = TimeSpan.FromSeconds(1)
24-
};
25-
26-
StartContainer();
27-
if (!await WaitForService())
28-
throw new Exception("Failed to start container, timeout hit.");
28+
await StartContainer();
29+
Token = InitializeData();
30+
string hostAndPort = GetContainerHostPort(_gitlabContainer, "80/tcp");
31+
GitlabHost = $"http://{hostAndPort}/api/v4/";
2932
}
3033

31-
public Task DisposeAsync()
34+
public async Task DisposeAsync()
3235
{
33-
StopContainer();
34-
return Task.CompletedTask;
36+
await StopContainer();
3537
}
3638

37-
private void StartContainer()
39+
private Task StartContainer()
3840
{
39-
StartProcessAndWaitForExit(new ProcessStartInfo
40-
{
41-
FileName = "docker-compose",
42-
Arguments =
43-
$"-f {GitLabContainerPath}/docker-compose.yml up -d"
44-
});
41+
_gitlabContainer = new Builder()
42+
.UseContainer()
43+
.UseImage(_gitlabDockerImage)
44+
.WithEnvironment(
45+
$"GITLAB_OMNIBUS_CONFIG=from_file('{GitlabRb}')"
46+
)
47+
.ExposePort(80)
48+
.CopyOnStart($"{SolutionRootFolder}/docker/gitlab.rb", GitlabRb)
49+
.CopyOnStart($"{SolutionRootFolder}/docker/init.rb", InitRb)
50+
.WaitForHealthy()
51+
.Build()
52+
.Start();
53+
54+
return Task.CompletedTask;
4555
}
4656

47-
private void StopContainer()
57+
private Task StopContainer()
4858
{
49-
var processStartInfo = new ProcessStartInfo
50-
{
51-
FileName = "docker-compose",
52-
Arguments =
53-
$"-f {GitLabContainerPath}/docker-compose.yml down"
54-
};
59+
_gitlabContainer?.Stop();
60+
_gitlabContainer?.Dispose();
5561

56-
StartProcessAndWaitForExit(processStartInfo);
62+
return Task.CompletedTask;
5763
}
5864

59-
private void StartProcessAndWaitForExit(ProcessStartInfo processStartInfo)
65+
private string InitializeData()
6066
{
61-
processStartInfo.UseShellExecute = false;
62-
processStartInfo.RedirectStandardOutput = true;
63-
processStartInfo.CreateNoWindow = true;
64-
processStartInfo.Environment["COMPUTERNAME"] = Environment.MachineName;
65-
66-
var process = new Process
67-
{
68-
StartInfo = processStartInfo
69-
};
70-
71-
process.OutputDataReceived += LogOutputData;
72-
process.Start();
73-
process.BeginOutputReadLine();
74-
process.WaitForExit();
75-
76-
Assert.Equal(0, process.ExitCode);
77-
78-
void LogOutputData(object sender, DataReceivedEventArgs e)
79-
//TODO: find out how to log data in XUnit fixtures
80-
=> Trace.WriteLine(e.Data);
67+
string command = $"/opt/gitlab/bin/gitlab-rails r {InitRb}";
68+
var output = ExecuteCommandAgainstDockerWithOutput(_gitlabContainer, command);
69+
string token = output.FirstOrDefault();
70+
token.Should().NotBeNullOrEmpty();
71+
return token;
8172
}
8273

83-
private async Task<bool> WaitForService()
74+
private static IEnumerable<string> ExecuteCommandAgainstDockerWithOutput(IContainerService container, string command)
8475
{
85-
var startTime = DateTime.Now;
76+
var output = container.Execute(command);
77+
string error = output.Error;
78+
error.Should().BeEmpty();
79+
output.Success.Should().BeTrue();
80+
output.Data.Should().NotBeNullOrEmpty();
8681

87-
while (DateTime.Now - startTime < TestTimeout)
88-
{
89-
try
90-
{
91-
var response = await _gitLabPingClient.GetAsync(GitLabApiPath);
92-
if (response.IsSuccessStatusCode)
93-
{
94-
Trace.WriteLine("GitLab started to respond!");
95-
return true;
96-
}
97-
}
98-
catch (HttpRequestException)
99-
{
100-
}
101-
catch (OperationCanceledException)
102-
{
103-
}
104-
105-
await Task.Delay(15000);
106-
}
82+
return output.Data;
83+
}
10784

108-
return false;
85+
private static string GetContainerHostPort(IContainerService containerService, string portAndProto)
86+
{
87+
var ep = containerService.ToHostExposedEndpoint(portAndProto);
88+
string hostAndPort = $"localhost:{ep.Port}";
89+
return hostAndPort;
10990
}
11091
}
11192
}

0 commit comments

Comments
 (0)