Skip to content
This repository has been archived by the owner on May 9, 2022. It is now read-only.

Commit

Permalink
Feature/docker tests (nmklotas#6)
Browse files Browse the repository at this point in the history
* Add collection fixture for gitlab docker instance

* Fix docker config

* Add collection attributes

* add travis.yml

* Update .travis.yml

* Update GitLabContainerFixture.cs
  • Loading branch information
nmklotas authored Nov 5, 2017
1 parent 4efe2e5 commit 942ff4e
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .gitignore
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,7 @@ __pycache__/
# Coverage files
coverage/
docker/build/volumes/*
!docker/build/volumes/placeholder.gitkeep
!docker/build/volumes/placeholder.gitkeep

# vscode files
.vscode/
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: csharp
mono: none
dotnet: 2.0.0
dist: trusty
sudo: required
services:
- docker
script:
- dotnet build
- (cd ./test/GitLabApiClient.Test && dotnet test)
cache:
directories:
- $HOME/.nuget
1 change: 1 addition & 0 deletions test/GitLabApiClient.Test/GroupsClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace GitLabApiClient.Test
{
[Collection("GitLabContainerFixture")]
public class GroupsClientTest
{
private readonly List<int> _groupIdsToClean = new List<int>();
Expand Down
1 change: 1 addition & 0 deletions test/GitLabApiClient.Test/IssuesClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace GitLabApiClient.Test
{
[Collection("GitLabContainerFixture")]
public class IssuesClientTest
{
private readonly IssuesClient _sut = new IssuesClient(
Expand Down
1 change: 1 addition & 0 deletions test/GitLabApiClient.Test/MergeRequestClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace GitLabApiClient.Test
{
[Collection("GitLabContainerFixture")]
public class MergeRequestClientTest : IAsyncLifetime
{
private readonly MergeRequestsClient _sut = new MergeRequestsClient(
Expand Down
1 change: 1 addition & 0 deletions test/GitLabApiClient.Test/ProjectsClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace GitLabApiClient.Test
{
[Collection("GitLabContainerFixture")]
public class ProjectsClientTest : IAsyncLifetime
{
private List<int> ProjectIdsToClean { get; } = new List<int>();
Expand Down
1 change: 1 addition & 0 deletions test/GitLabApiClient.Test/UsersClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace GitLabApiClient.Test
{
[Collection("GitLabContainerFixture")]
public class UsersClientTest
{
private readonly UsersClient _sut = new UsersClient(GitLabApiHelper.GetFacade());
Expand Down
2 changes: 1 addition & 1 deletion test/GitLabApiClient.Test/Utilities/GitLabApiHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static class GitLabApiHelper
public static GitLabHttpFacade GetFacade()
{
var facade = new GitLabHttpFacade(
"http://localhost:9090/api/v4/", new RequestsJsonSerializer(), "ElijahBaley");
"http://localhost:9190/api/v4/", new RequestsJsonSerializer(), "ElijahBaley");

return facade;
}
Expand Down
94 changes: 94 additions & 0 deletions test/GitLabApiClient.Test/Utilities/GitLabContainerFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;

namespace GitLabApiClient.Test.Utilities
{
public class GitLabContainerFixture : IAsyncLifetime
{
private const string GitLabContainerPath = "../../../../docker";

private const string GitLabApiPath = "http://localhost:9190/";

private static readonly TimeSpan TestTimeout = TimeSpan.FromMinutes(10);

private HttpClient _gitLabPingClient;

public async Task InitializeAsync()
{
_gitLabPingClient = new HttpClient
{
Timeout = TimeSpan.FromSeconds(1)
};

StartContainer();
if (!await WaitForService())
throw new Exception($"Failed to start container, timeout hit.");
}

public Task DisposeAsync()
{
StopContainer();
return Task.CompletedTask;
}

private void StartContainer()
{
StartProcessAndWaitForExit(new ProcessStartInfo
{
FileName = "docker-compose",
Arguments =
$"-f {GitLabContainerPath}/docker-compose.yml up -d"
});
}

private void StopContainer()
{
var processStartInfo = new ProcessStartInfo
{
FileName = "docker-compose",
Arguments =
$"-f {GitLabContainerPath}/docker-compose.yml down"
};

StartProcessAndWaitForExit(processStartInfo);
}

private void StartProcessAndWaitForExit(ProcessStartInfo processStartInfo)
{
processStartInfo.Environment["COMPUTERNAME"] = Environment.MachineName;

var process = Process.Start(processStartInfo);
process.WaitForExit();
Assert.Equal(0, process.ExitCode);
}

private async Task<bool> WaitForService()
{
var startTime = DateTime.Now;

while (DateTime.Now - startTime < TestTimeout)
{
try
{
var response = await _gitLabPingClient.GetAsync(GitLabApiPath);
if (response.IsSuccessStatusCode)
{
Debug.WriteLine("GitLab started to respond!");
return true;
}
}
catch
{
}

await Task.Delay(5000);
}

return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using Xunit;

namespace GitLabApiClient.Test.Utilities
{
[CollectionDefinition("GitLabContainerFixture")]
public class GitLabContainterFixtureCollection : ICollectionFixture<GitLabContainerFixture>
{
}
}

0 comments on commit 942ff4e

Please sign in to comment.