Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration tests for Google.Cloud.Compute.V1
- Loading branch information
Showing
7 changed files
with
281 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
apis/Google.Cloud.Compute.V1/Google.Cloud.Compute.V1.IntegrationTests/AcceptanceTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// 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 | ||
// | ||
// https://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. | ||
|
||
using System; | ||
using System.Threading; | ||
using Grpc.Core; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Google.Cloud.Compute.V1.IntegrationTests | ||
{ | ||
/// <summary> | ||
/// Miscellaneous acceptance tests for the Compute API. | ||
/// </summary> | ||
[Collection(nameof(ComputeFixture))] | ||
public sealed class AcceptanceTest | ||
{ | ||
private readonly ITestOutputHelper _output; | ||
private readonly ComputeFixture _fixture; | ||
|
||
public AcceptanceTest(ComputeFixture fixture, ITestOutputHelper output) => | ||
(_fixture, _output) = (fixture, output); | ||
|
||
[Fact] | ||
public void CreateDeleteIPAddress() | ||
{ | ||
string projectId = _fixture.ProjectId; | ||
string region = _fixture.Region; | ||
|
||
AddressesClient addressesClient = AddressesClient.Create(); | ||
var addressName = $"testaddr-csharp-{Guid.NewGuid()}"; | ||
|
||
FetchNonExistentAddress(); | ||
CreateAddress(); | ||
FetchNewAddress(); | ||
DeleteAddress(); | ||
|
||
void FetchNonExistentAddress() | ||
{ | ||
_output.WriteLine($"Retrieving address with the name: {addressName}"); | ||
var exc = Assert.Throws<RpcException>(() => addressesClient.Get(projectId, region, addressName)); | ||
Assert.Equal(StatusCode.NotFound, exc.StatusCode); | ||
} | ||
|
||
void CreateAddress() | ||
{ | ||
_output.WriteLine($"Creating address with the name: {addressName}"); | ||
Address addressResource = new Address | ||
{ | ||
Name = addressName, | ||
Region = region, | ||
NetworkTier = Address.Types.NetworkTier.Premium | ||
}; | ||
|
||
Operation insertOperation = addressesClient.Insert(projectId, region, addressResource); | ||
_output.WriteLine($"Operation to create address: {insertOperation.Name} status {insertOperation.Status}; start time {insertOperation.StartTime}"); | ||
insertOperation = PollForCompletion(insertOperation, "create"); | ||
_output.WriteLine($"Operation to create address completed: status {insertOperation.Status}; start time {insertOperation.StartTime}; end time {insertOperation.EndTime}"); | ||
} | ||
|
||
void FetchNewAddress() | ||
{ | ||
_output.WriteLine($"Retrieving address with the name: {addressName}"); | ||
Address readAddr = addressesClient.Get(projectId, region, addressName); | ||
Assert.NotNull(readAddr); | ||
Assert.Equal(readAddr.Name, addressName); | ||
} | ||
|
||
void DeleteAddress() | ||
{ | ||
_output.WriteLine($"Deleting address with the name: {addressName}"); | ||
Operation deleteOp = addressesClient.Delete(projectId, region, addressName); | ||
_output.WriteLine($"Operation to delete address: {deleteOp.Name} status {deleteOp.Status}; start time {deleteOp.StartTime}"); | ||
deleteOp = PollForCompletion(deleteOp, "delete"); | ||
_output.WriteLine($"Operation to delete address completed: status {deleteOp.Status}; start time {deleteOp.StartTime}; end time {deleteOp.EndTime}"); | ||
} | ||
} | ||
|
||
private Operation PollForCompletion(Operation operation, string alias) | ||
{ | ||
RegionOperationsClient regionOperationsClient = RegionOperationsClient.Create(); | ||
|
||
TimeSpan timeOut = TimeSpan.FromMinutes(3); | ||
TimeSpan pollInterval = TimeSpan.FromSeconds(15); | ||
|
||
DateTime deadline = DateTime.UtcNow + timeOut; | ||
while (operation.Status != Operation.Types.Status.Done) | ||
{ | ||
GetRegionOperationRequest request = new GetRegionOperationRequest | ||
{ | ||
Operation = operation.Name, | ||
Region = _fixture.Region, | ||
Project = _fixture.ProjectId, | ||
}; | ||
_output.WriteLine($"Checking for {alias} operation status ..."); | ||
operation = regionOperationsClient.Get(request); | ||
|
||
if (operation.Status == Operation.Types.Status.Done) | ||
{ | ||
break; | ||
} | ||
if (DateTime.UtcNow > deadline) | ||
{ | ||
throw new InvalidOperationException( | ||
$"Timeout hit while polling for the status of the {alias} operation\n{operation}"); | ||
} | ||
_output.WriteLine($"Status: {operation.Status}. Sleeping for the {pollInterval.TotalSeconds}s"); | ||
Thread.Sleep(pollInterval); | ||
} | ||
|
||
return operation; | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
apis/Google.Cloud.Compute.V1/Google.Cloud.Compute.V1.IntegrationTests/ComputeFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// 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 | ||
// | ||
// https://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. | ||
|
||
using Google.Cloud.ClientTesting; | ||
using System; | ||
using Xunit; | ||
|
||
namespace Google.Cloud.Compute.V1.IntegrationTests | ||
{ | ||
[CollectionDefinition(nameof(ComputeFixture))] | ||
public class ComputeFixture : CloudProjectFixtureBase, ICollectionFixture<ComputeFixture> | ||
{ | ||
// Note: this would more naturally be called COMPUTE_TEST_REGION, but this | ||
// environment variable already exists for App Engine testing, so it's handy to | ||
// avoid requiring another one. | ||
private const string TestRegionEnvironmentVariable = "TEST_PROJECT_LOCATION"; | ||
|
||
/// <summary> | ||
/// Region to use for testing. | ||
/// </summary> | ||
public string Region { get; } | ||
|
||
public ComputeFixture() | ||
{ | ||
Region = Environment.GetEnvironmentVariable(TestRegionEnvironmentVariable); | ||
if (string.IsNullOrEmpty(Region)) | ||
{ | ||
throw new InvalidOperationException($"Environment variable {TestRegionEnvironmentVariable} must be set"); | ||
} | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
.../Google.Cloud.Compute.V1.IntegrationTests/Google.Cloud.Compute.V1.IntegrationTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>netcoreapp2.1;net461</TargetFrameworks> | ||
<TestTargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netcoreapp2.1</TestTargetFrameworks> | ||
<IsPackable>false</IsPackable> | ||
<NoWarn>1701;1702;1705;xUnit2004;xUnit2013;AD0001</NoWarn> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\tools\Google.Cloud.ClientTesting\Google.Cloud.ClientTesting.csproj" /> | ||
<ProjectReference Include="..\Google.Cloud.Compute.V1\Google.Cloud.Compute.V1.csproj" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" /> | ||
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" /> | ||
<PackageReference Include="Moq" Version="4.12.0" /> | ||
<PackageReference Include="System.Linq.Async" Version="4.0.0" /> | ||
<PackageReference Include="Xunit.SkippableFact" Version="1.3.12" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" /> | ||
<Analyzer Condition="Exists('..\..\..\tools\Google.Cloud.Tools.Analyzers\bin\$(Configuration)\netstandard1.3\publish\Google.Cloud.Tools.Analyzers.dll')" Include="..\..\..\tools\Google.Cloud.Tools.Analyzers\bin\$(Configuration)\netstandard1.3\publish\Google.Cloud.Tools.Analyzers.dll" /> | ||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> | ||
</ItemGroup> | ||
</Project> |
40 changes: 40 additions & 0 deletions
40
apis/Google.Cloud.Compute.V1/Google.Cloud.Compute.V1.IntegrationTests/TranscodingTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// 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 | ||
// | ||
// https://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. | ||
|
||
using Xunit; | ||
|
||
namespace Google.Cloud.Compute.V1.IntegrationTests | ||
{ | ||
/// <summary> | ||
/// Integration tests focused on the GRPC Transcoding | ||
/// </summary> | ||
[Collection(nameof(ComputeFixture))] | ||
public sealed class TranscodingTest | ||
{ | ||
private readonly ComputeFixture _fixture; | ||
|
||
public TranscodingTest(ComputeFixture fixture) => _fixture = fixture; | ||
|
||
[Fact] | ||
public void ListZones_MaxResults() | ||
{ | ||
var client = ZonesClient.Create(); | ||
var allZones = client.List(_fixture.ProjectId); | ||
Assert.InRange(allZones.Items.Count, 3, int.MaxValue); | ||
|
||
var twoZones = client.List(new ListZonesRequest { Project = _fixture.ProjectId, MaxResults = 2 }); | ||
Assert.Equal(2, twoZones.Items.Count); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
apis/Google.Cloud.Compute.V1/Google.Cloud.Compute.V1.IntegrationTests/coverage.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<CoverageParams> | ||
<TargetExecutable>C:/Program Files/dotnet/dotnet.exe</TargetExecutable> | ||
<TargetArguments>test --no-build -c Release</TargetArguments> | ||
<Filters> | ||
<IncludeFilters> | ||
<FilterEntry> | ||
<ModuleMask>Google.Cloud.Compute.V1</ModuleMask> | ||
</FilterEntry> | ||
</IncludeFilters> | ||
</Filters> | ||
<AttributeFilters> | ||
<AttributeFilterEntry>System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute</AttributeFilterEntry> | ||
<AttributeFilterEntry>System.Diagnostics.DebuggerNonUserCodeAttribute</AttributeFilterEntry> | ||
</AttributeFilters> | ||
<TargetWorkingDir>.</TargetWorkingDir> | ||
<Output>../../../coverage/Google.Cloud.Compute.V1.IntegrationTests.dvcr</Output> | ||
</CoverageParams> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters