Skip to content

Commit

Permalink
Merge pull request #5 from Q42/bump-packages
Browse files Browse the repository at this point in the history
chore: Bump packages & improve tests
  • Loading branch information
TheMerski committed Feb 9, 2024
2 parents 5d88ac2 + e002e1b commit 245199b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/verify-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ jobs:
steps:
- uses: yogevbd/[email protected]
with:
REQUIRED_LABELS_ANY: "feature,bug,maintenance,chore,ignore-for-release,major,minor,patch,github-actions"
REQUIRED_LABELS_ANY_DESCRIPTION: "Select at least one label ['feature','bug','maintenance', 'chore', 'ignore-for-release', 'major', 'minor', 'patch', 'github-actions']"
REQUIRED_LABELS_ANY: "feature,bug,maintenance,dependencies,chore,ignore-for-release,major,minor,patch,github-actions"
REQUIRED_LABELS_ANY_DESCRIPTION: "Select at least one label ['feature','bug','maintenance', 'dependencies', 'chore', 'ignore-for-release', 'major', 'minor', 'patch', 'github-actions']"

test:
runs-on: ubuntu-latest
Expand Down
73 changes: 67 additions & 6 deletions Q42.Google.Cloud.Compute.Metadata.Tests/MetadataClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public void Setup()
.Respond(HttpStatusCode.OK, googleHeaders, new StringContent("", Encoding.UTF8, "application/text"));
}

[TearDown]
public void TearDown()
{
mockHttpOnGce.Dispose();
}

[Test]
public async Task IsOnGCETrue()
{
Expand All @@ -36,17 +42,41 @@ public async Task IsOnGCETrue()
var client = mockHttp.ToHttpClient();
using var metadata = new MetadataClient(client);
var onGce = await metadata.IsOnGCEAsync();
Assert.IsTrue(onGce);
Assert.That(onGce, Is.True);
mockHttp.VerifyNoOutstandingExpectation();
}

// We timeout the test after 1042ms, because the default timeout is 1000ms.
[Test, Timeout(1042)]
[Test]
public async Task IsOnGCEIsCached()
{
var httpContent = new StringContent("", Encoding.UTF8, "application/text");
var mockHttp = new MockHttpMessageHandler();
var reqMock = mockHttp.When($"{baseAddress}/*")
.WithHeaders(googleHeaders)
.Respond(HttpStatusCode.OK, googleHeaders, httpContent);

var client = mockHttp.ToHttpClient();
using var metadata = new MetadataClient(client);
var onGce = await metadata.IsOnGCEAsync();
Assert.That(onGce, Is.True);
var onGce2 = await metadata.IsOnGCEAsync();
Assert.Multiple(() =>
{
Assert.That(onGce2, Is.True);
// Verify that the request was only made once (and thus cached).
Assert.That(mockHttp.GetMatchCount(reqMock), Is.EqualTo(1));
});
mockHttp.VerifyNoOutstandingExpectation();
}

// Cancel after a little over 1s to verify we don't wait too long for the metadata server.
// The requests should time out after 1s.
[Test, CancelAfter(1042)]
public async Task IsOnGCEFalse()
{
using var metadata = new MetadataClient();
var onGce = await metadata.IsOnGCEAsync();
Assert.IsFalse(onGce);
Assert.That(onGce, Is.False);
}

[Test]
Expand All @@ -62,7 +92,7 @@ public async Task UsesMetadataEnvVar()
var client = mockHttp.ToHttpClient();
using var metadata = new MetadataClient(client);
var onGce = await metadata.IsOnGCEAsync();
Assert.IsTrue(onGce);
Assert.That(onGce, Is.True);
mockHttp.VerifyNoOutstandingExpectation();
}

Expand All @@ -71,7 +101,7 @@ public async Task DoesNotThrowWhenNotOnGCEByDefault()
{
using var metadata = new MetadataClient();
var result = await metadata.GetProjectIdAsync();
Assert.IsNull(result);
Assert.That(result, Is.Null);
}

[Test]
Expand All @@ -95,6 +125,37 @@ public async Task GetsProjectId()
mockHttpOnGce.VerifyNoOutstandingExpectation();
}

[Test]
public Task GetsAreCached()
{
var reqMock = mockHttpOnGce.When($"{metadataBase}project/project-id")
.WithHeaders(googleHeaders)
.Respond(HttpStatusCode.NotFound);
using var metadata = new MetadataClient(mockHttpOnGce.ToHttpClient());
Assert.ThrowsAsync<Exception>(async () => await metadata.GetProjectIdAsync());
return Task.CompletedTask;
}

[Test]
public async Task GetsThrowsOnNotFound()
{
const string projectId = "test-project";
var reqMock = mockHttpOnGce.When($"{metadataBase}project/project-id")
.WithHeaders(googleHeaders)
.Respond(HttpStatusCode.OK, googleHeaders, "application/text", projectId);
using var metadata = new MetadataClient(mockHttpOnGce.ToHttpClient());
var result = await metadata.GetProjectIdAsync();
Assert.That(result, Is.EqualTo(projectId));
var result2 = await metadata.GetProjectIdAsync();
Assert.Multiple(() =>
{
Assert.That(result2, Is.EqualTo(projectId));
// Verify that the request was only made once (and thus cached).
Assert.That(mockHttpOnGce.GetMatchCount(reqMock), Is.EqualTo(1));
});
mockHttpOnGce.VerifyNoOutstandingExpectation();
}

[Test]
public async Task GetsNumericProjectId()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
<PackageReference Include="NUnit" Version="3.13.3"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1"/>
<PackageReference Include="NUnit.Analyzers" Version="3.6.1"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="NUnit" Version="4.0.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="RichardSzalay.MockHttp" Version="7.0.0" />
</ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions Q42.Google.Cloud.Compute.Metadata.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/Environment/Filtering/ExcludeCoverageFilters/=Q42_002EGoogle_002ECloud_002ECompute_002EMetadata_002ETestServer_003B_002A_003B_002A_003B_002A/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

0 comments on commit 245199b

Please sign in to comment.