Skip to content

Commit

Permalink
feat: Registry (#21)
Browse files Browse the repository at this point in the history
fixes #18

Signed-off-by: Samson Amaugo <sammychinedu2ky@gmail.com>
  • Loading branch information
sammychinedu2ky authored May 27, 2023
1 parent ed83c73 commit 68c837a
Show file tree
Hide file tree
Showing 20 changed files with 4,065 additions and 14 deletions.
1 change: 1 addition & 0 deletions Oras.Tests/Oras.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0-preview-20230223-05" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
150 changes: 150 additions & 0 deletions Oras.Tests/RemoteTest/RegistryTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
using Moq;
using Moq.Protected;
using Oras.Remote;
using System.Net;
using System.Text.Json;
using System.Text.RegularExpressions;
using Xunit;

namespace Oras.Tests.RemoteTest
{
public class RegistryTest
{

public static HttpClient CustomClient(Func<HttpRequestMessage, CancellationToken, HttpResponseMessage> func)
{
var moqHandler = new Mock<DelegatingHandler>();
moqHandler.Protected().Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
).ReturnsAsync(func);
return new HttpClient(moqHandler.Object);
}

/// <summary>
/// TestRegistry_PingAsync tests the PingAsync method of the Registry class.
/// </summary>
/// <returns></returns>
[Fact]
public async Task TestRegistry_PingAsync()
{
var V2Implemented = true;
var func = (HttpRequestMessage req, CancellationToken cancellationToken) =>
{
var res = new HttpResponseMessage();
res.RequestMessage = req;
if (req.Method != HttpMethod.Get && req.RequestUri.AbsolutePath == $"/v2/")
{
res.StatusCode = HttpStatusCode.NotFound;
return res;
}
if (V2Implemented)
{
res.StatusCode = HttpStatusCode.OK;
return res;
}
else
{
res.StatusCode = HttpStatusCode.NotFound;
return res;
}
};
var registry = new Oras.Remote.Registry("localhost:5000");
registry.PlainHTTP = true;
registry.HttpClient = CustomClient(func);
var cancellationToken = new CancellationToken();
await registry.PingAsync(cancellationToken);
V2Implemented = false;
await Assert.ThrowsAnyAsync<Exception>(
async () => await registry.PingAsync(cancellationToken));
}

/// <summary>
/// TestRegistry_Repositories tests the ListRepositoriesAsync method of the Registry class.
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
[Fact]
public async Task TestRegistry_Repositories()
{
var repoSet = new List<List<string>>()
{
new() {"the", "quick", "brown", "fox"},
new() {"jumps", "over", "the", "lazy"},
new() {"dog"}
};
var func = (HttpRequestMessage req, CancellationToken cancellationToken) =>
{
var res = new HttpResponseMessage();
res.RequestMessage = req;
if (req.Method != HttpMethod.Get ||
req.RequestUri.AbsolutePath != "/v2/_catalog"
)
{
return new HttpResponseMessage(HttpStatusCode.NotFound);
}
var q = req.RequestUri.Query;
try
{
var n = int.Parse(Regex.Match(q, @"(?<=n=)\d+").Value);
if (n != 4) throw new Exception();
}
catch (Exception e)
{
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}
var repos = new List<string>();
var serverUrl = "http://localhost:5000";
var matched = Regex.Match(q, @"(?<=test=)\w+").Value;
switch (matched)
{
case "foo":
repos = repoSet[1];
res.Headers.Add("Link", $"<{serverUrl}/v2/_catalog?n=4&test=bar>; rel=\"next\"");
break;
case "bar":
repos = repoSet[2];
break;
default:
repos = repoSet[0];
res.Headers.Add("Link", $"</v2/_catalog?n=4&test=foo>; rel=\"next\"");
break;
}
var repositoryList = new ResponseTypes.RepositoryList
{
Repositories = repos.ToArray()
};
res.Content = new StringContent(JsonSerializer.Serialize(repositoryList));
return res;
};

var registry = new Oras.Remote.Registry("localhost:5000");
registry.PlainHTTP = true;
registry.HttpClient = CustomClient(func);
var cancellationToken = new CancellationToken();
registry.TagListPageSize = 4;



var index = 0;
await registry.Repositories("", async (string[] got) =>
{
if (index > 2)
{
throw new Exception($"Error out of range: {index}");
}
var repos = repoSet[index];
index++;
Assert.Equal(got, repos);
}, cancellationToken);
}
}
}
Loading

0 comments on commit 68c837a

Please sign in to comment.