Skip to content

Commit

Permalink
build: build check with code coverage uploaded (#78)
Browse files Browse the repository at this point in the history
Signed-off-by: Shiwei Zhang <[email protected]>
  • Loading branch information
shizhMSFT authored Dec 27, 2023
1 parent d071b06 commit 535a0d7
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 44 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright The ORAS Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: build

on:
push:
branches:
- main
- release-*
pull_request:
branches:
- main
- release-*

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
dotnet-version: ['8.0.x']
fail-fast: true
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up dotnet ${{ matrix.dotnet-version }} environment
uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ matrix.dotnet-version }}
- name: Run unit tests
run: dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover ./tests/OrasProject.Oras.Tests
- name: Upload coverage to codecov.io
uses: codecov/codecov-action@v3
with:
file: ./tests/OrasProject.Oras.Tests/coverage.opencover.xml
4 changes: 2 additions & 2 deletions src/OrasProject.Oras/Content/Content.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using OrasProject.Oras.Exceptions;
using OrasProject.Oras.Constants;
using OrasProject.Oras.Exceptions;
using OrasProject.Oras.Interfaces;
using OrasProject.Oras.Models;
using OrasProject.Oras.Constants;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down
4 changes: 2 additions & 2 deletions src/OrasProject.Oras/Remote/ManifestUtility.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using OrasProject.Oras.Models;
using OrasProject.Oras.Constants;
using OrasProject.Oras.Constants;
using OrasProject.Oras.Models;
using System.Linq;

namespace OrasProject.Oras.Remote
Expand Down
8 changes: 4 additions & 4 deletions src/OrasProject.Oras/Remote/Registry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public Registry(string name)
}

public Registry(string name, HttpClient httpClient)
{
var reference = new RemoteReference
{
var reference = new RemoteReference
{
Registry = name,
};
Expand Down Expand Up @@ -79,8 +79,8 @@ public Task<IRepository> Repository(string name, CancellationToken cancellationT
{
Registry = RemoteReference.Registry,
Repository = name,
};
};

return Task.FromResult<IRepository>(new Repository(reference, this));
}

Expand Down
1 change: 0 additions & 1 deletion src/OrasProject.Oras/Remote/RemoteReference.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using OrasProject.Oras.Content;
using OrasProject.Oras.Exceptions;
using System;
using System.Net.NetworkInformation;
using System.Text.RegularExpressions;

namespace OrasProject.Oras.Remote
Expand Down
4 changes: 2 additions & 2 deletions src/OrasProject.Oras/Remote/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public Repository(string reference)
RemoteReference = RemoteReference.ParseReference(reference);
HttpClient = new HttpClient();
HttpClient.DefaultRequestHeaders.Add("User-Agent", new string[] { "oras-dotnet" });
}
}

/// <summary>
/// Creates a client to the remote repository using a reference and a HttpClient
/// </summary>
Expand Down
5 changes: 2 additions & 3 deletions tests/OrasProject.Oras.Tests/CopyTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using OrasProject.Oras.Memory;
using OrasProject.Oras.Constants;
using OrasProject.Oras.Memory;
using OrasProject.Oras.Models;
using OrasProject.Oras;
using OrasProject.Oras.Constants;
using System.Text;
using System.Text.Json;
using Xunit;
Expand Down
4 changes: 2 additions & 2 deletions tests/OrasProject.Oras.Tests/MemoryTest/MemoryTargetTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using OrasProject.Oras.Exceptions;
using OrasProject.Oras.Constants;
using OrasProject.Oras.Exceptions;
using OrasProject.Oras.Memory;
using OrasProject.Oras.Models;
using OrasProject.Oras.Constants;
using System.Text;
using System.Text.Json;
using Xunit;
Expand Down
14 changes: 7 additions & 7 deletions tests/OrasProject.Oras.Tests/OrasProject.Oras.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
</PropertyGroup>

<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>
<PrivateAssets>all</PrivateAssets>
<PackageReference Include="coverlet.msbuild" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.6.4" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
5 changes: 2 additions & 3 deletions tests/OrasProject.Oras.Tests/RemoteTest/AuthTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Moq;
using Moq.Protected;
using OrasProject.Oras.Remote;
using OrasProject.Oras.Remote.Auth;
using System.Net;
using System.Text;
Expand Down Expand Up @@ -36,8 +35,8 @@ public async Task TestClient_CustomHttpBasicAuthClient()
var res = new HttpResponseMessage
{
RequestMessage = req
};
};

if (req.Method != HttpMethod.Get && req.RequestUri?.AbsolutePath == $"/")
{
res.StatusCode = HttpStatusCode.NotFound;
Expand Down
51 changes: 33 additions & 18 deletions tests/OrasProject.Oras.Tests/RemoteTest/RepositoryTest.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using Moq;
using Moq.Protected;
using OrasProject.Oras.Constants;
using OrasProject.Oras.Exceptions;
using OrasProject.Oras.Memory;
using OrasProject.Oras.Models;
using OrasProject.Oras.Remote;
using OrasProject.Oras;
using OrasProject.Oras.Constants;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Net;
Expand Down Expand Up @@ -135,6 +134,17 @@ public static HttpClient CustomClient(Func<HttpRequestMessage, CancellationToken
return new HttpClient(moqHandler.Object);
}

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

/// <summary>
/// Repository_FetchAsync tests the FetchAsync method of the Repository.
/// </summary>
Expand Down Expand Up @@ -189,7 +199,6 @@ public async Task Repository_FetchAsync()
resp.Content.Headers.Add("Content-Type", indexDesc.MediaType);
resp.Content.Headers.Add("Docker-Content-Digest", indexDesc.Digest);
return resp;

}

resp.StatusCode = HttpStatusCode.NotFound;
Expand Down Expand Up @@ -522,10 +531,10 @@ public async Task Repository_TagAsync()
MediaType = OCIMediaTypes.ImageIndex,
Size = index.Length
};
var gotIndex = new byte[indexDesc.Size];
byte[]? gotIndex = null;
var reference = "foobar";

var func = (HttpRequestMessage req, CancellationToken cancellationToken) =>
var func = async (HttpRequestMessage req, CancellationToken cancellationToken) =>
{
var res = new HttpResponseMessage();
res.RequestMessage = req;
Expand Down Expand Up @@ -560,7 +569,10 @@ public async Task Repository_TagAsync()
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}

gotIndex = req.Content?.ReadAsByteArrayAsync().Result;
if (req.Content != null)
{
gotIndex = await req.Content.ReadAsByteArrayAsync(cancellationToken);
}
res.Content.Headers.Add("Docker-Content-Digest", indexDesc.Digest);
res.StatusCode = HttpStatusCode.Created;
return res;
Expand Down Expand Up @@ -594,10 +606,10 @@ public async Task Repository_PushReferenceAsync()
MediaType = OCIMediaTypes.ImageIndex,
Size = index.Length
};
var gotIndex = new byte[indexDesc.Size];
byte[]? gotIndex = null;
var reference = "foobar";

var func = (HttpRequestMessage req, CancellationToken cancellationToken) =>
var func = async (HttpRequestMessage req, CancellationToken cancellationToken) =>
{
var res = new HttpResponseMessage();
res.RequestMessage = req;
Expand All @@ -609,7 +621,10 @@ public async Task Repository_PushReferenceAsync()
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}

gotIndex = req.Content?.ReadAsByteArrayAsync().Result;
if (req.Content != null)
{
gotIndex = await req.Content.ReadAsByteArrayAsync();
}
res.Content.Headers.Add("Docker-Content-Digest", indexDesc.Digest);
res.StatusCode = HttpStatusCode.Created;
return res;
Expand Down Expand Up @@ -1556,7 +1571,7 @@ public async Task ManifestStore_PushAsync()
};
byte[]? gotManifest = null;

var func = (HttpRequestMessage req, CancellationToken cancellationToken) =>
var func = async (HttpRequestMessage req, CancellationToken cancellationToken) =>
{
var res = new HttpResponseMessage();
res.RequestMessage = req;
Expand All @@ -1570,7 +1585,7 @@ public async Task ManifestStore_PushAsync()
if (req.Content?.Headers?.ContentLength != null)
{
var buf = new byte[req.Content.Headers.ContentLength.Value];
req.Content.ReadAsByteArrayAsync().Result.CopyTo(buf, 0);
(await req.Content.ReadAsByteArrayAsync()).CopyTo(buf, 0);
gotManifest = buf;
}
res.Content.Headers.Add("Docker-Content-Digest", new string[] { manifestDesc.Digest });
Expand Down Expand Up @@ -1766,7 +1781,7 @@ public async Task ManifestStore_ResolveAsync()
Digest = CalculateDigest(content),
Size = content.Length
};

await Assert.ThrowsAsync<NotFoundException>(async () => await store.ResolveAsync(contentDesc.Digest, cancellationToken));

}
Expand Down Expand Up @@ -1873,7 +1888,7 @@ public async Task ManifestStore_TagAsync()
var gotIndex = new byte[index.Length];
var reference = "foobar";

var func = (HttpRequestMessage req, CancellationToken cancellationToken) =>
var func = async (HttpRequestMessage req, CancellationToken cancellationToken) =>
{
var res = new HttpResponseMessage();
res.RequestMessage = req;
Expand Down Expand Up @@ -1903,7 +1918,7 @@ public async Task ManifestStore_TagAsync()
if (req.Content?.Headers?.ContentLength != null)
{
var buf = new byte[req.Content.Headers.ContentLength.Value];
req.Content.ReadAsByteArrayAsync().Result.CopyTo(buf, 0);
(await req.Content.ReadAsByteArrayAsync()).CopyTo(buf, 0);
gotIndex = buf;
}

Expand Down Expand Up @@ -1949,7 +1964,7 @@ public async Task ManifestStore_PushReferenceAsync()
var gotIndex = new byte[index.Length];
var reference = "foobar";

var func = (HttpRequestMessage req, CancellationToken cancellationToken) =>
var func = async (HttpRequestMessage req, CancellationToken cancellationToken) =>
{
var res = new HttpResponseMessage();
res.RequestMessage = req;
Expand All @@ -1965,7 +1980,7 @@ public async Task ManifestStore_PushReferenceAsync()
if (req.Content?.Headers?.ContentLength != null)
{
var buf = new byte[req.Content.Headers.ContentLength.Value];
req.Content.ReadAsByteArrayAsync().Result.CopyTo(buf, 0);
(await req.Content.ReadAsByteArrayAsync()).CopyTo(buf, 0);
gotIndex = buf;
}

Expand Down Expand Up @@ -2005,7 +2020,7 @@ public async Task CopyFromRepositoryToMemory()
{
var res = new HttpResponseMessage();
res.RequestMessage = req;
var path = req.RequestUri != null? req.RequestUri.AbsolutePath : string.Empty;
var path = req.RequestUri != null ? req.RequestUri.AbsolutePath : string.Empty;
var method = req.Method;
if (path.Contains("/blobs/uploads/") && method == HttpMethod.Post)
{
Expand Down Expand Up @@ -2074,7 +2089,7 @@ public async Task CopyFromRepositoryToMemory()
var reg = new Registry("localhost:5000");
reg.HttpClient = CustomClient(func);
var src = await reg.Repository("source", CancellationToken.None);

var dst = new MemoryTarget();
var tagName = "latest";
var desc = await Copy.CopyAsync(src, tagName, dst, tagName, CancellationToken.None);
Expand Down

0 comments on commit 535a0d7

Please sign in to comment.