Skip to content

Commit

Permalink
Unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Nov 14, 2017
1 parent ca51bc7 commit 381a986
Show file tree
Hide file tree
Showing 19 changed files with 184 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Microsoft.Azure.Experiments
{
public static class ResourceOperations
public static class CreateOrUpdateAsyncOperation
{
public static async Task<IState> CreateAsync<Config>(
public static async Task<IState> CreateOrUpdateAsync<Config>(
this IResourceConfig<Config> config,
IClient client,
IState current,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

namespace Microsoft.Azure.Experiments
{
public static class CurrentState
public static class GetAsyncOperation
{
public static async Task<IState> GetState<Config>(
public static async Task<IState> GetAsync<Config>(
this IResourceConfig<Config> resourceConfig,
IClient client,
CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,18 @@ public static class VirtualNetworkPolicy
p.ResourceGroupName, p.Name, p.Config, p.CancellationToken));

public static ResourceConfig<VirtualNetwork> CreateVirtualNetworkConfig(
this ResourceConfig<ResourceGroup> resourceGroup, string name)
=> Policy.CreateConfig(resourceGroup, name);
this ResourceConfig<ResourceGroup> resourceGroup,
string name,
string addressPrefix)
=> Policy.CreateConfig(
resourceGroup,
name,
_ => new VirtualNetwork
{
AddressSpace = new AddressSpace
{
AddressPrefixes = new[] { addressPrefix }
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Azure.Experiments.Network;
using Microsoft.Azure.Experiments;
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Compute.Models;
using System.Threading.Tasks;
Expand Down
22 changes: 6 additions & 16 deletions experiments/Azure.Experiments/Azure.Experiments/Old/Context.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
using Microsoft.Azure.Management.Network;
using Microsoft.Rest;
using Microsoft.Azure.Experiments;
using Microsoft.Azure.Management.Network;

namespace Azure.Experiments
{
public class Context
public static class ContextEx
{
public Context(ServiceClientCredentials credentials, string subscriptionId)
{
Credentials = credentials;
SubscriptionId = subscriptionId;
}

public ServiceClientCredentials Credentials { get; }

public string SubscriptionId { get; }

public NetworkManagementClient CreateNetwork()
=> new NetworkManagementClient(Credentials)
public static NetworkManagementClient CreateNetwork(this Context context)
=> new NetworkManagementClient(context.Credentials)
{
SubscriptionId = SubscriptionId
SubscriptionId = context.SubscriptionId
};
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Azure.Management.ResourceManager.Models;
using Microsoft.Azure.Management.ResourceManager;
using System.Threading.Tasks;
using Microsoft.Azure.Experiments;

namespace Azure.Experiments
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Collections.Concurrent;
using System.Linq;

namespace Microsoft.Azure.Experiments
{
public static class Parameters
{
public static IState GetParameters<Config>(
string subscription, string location, IResourceConfig<Config> config)
this IResourceConfig<Config> config,
string subscription,
string location)
where Config : class
{
var visitor = new Visitor(subscription, location);
Expand All @@ -21,12 +23,19 @@ public Visitor(string subscription, string location)
Location = location;
}

public object GetUntyped(IResourceConfig config)
=> Result.GetOrAddUntyped(config, () => config.Apply(this));

public Config Get<Config>(IResourceConfig<Config> config)
where Config : class
=> Result.GetOrAdd(config, () => config.Apply(this) as Config);
=> GetUntyped(config) as Config;

public object Visit<Config>(ResourceConfig<Config> config) where Config : class
{
foreach (var d in config.Dependencies)
{
GetUntyped(d);
}
var p = config.CreateConfig(Subscription);
config.Policy.SetLocation(p, Location);
return p;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public ResourcePolicy(
Func<Config, string> getLocation,
Action<Config, string> setLocation)
{
GetId = getId;
GetAsync = getAsync;
CreateOrUpdateAsync = createOrUpdateAsync;
GetLocation = getLocation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Azure.Experiments.Tests
namespace Microsoft.Azure.Experiments.Tests
{
internal sealed class AuthenticationResponse
{
Expand Down
36 changes: 36 additions & 0 deletions experiments/Azure.Experiments/Tests/Client.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.ResourceManager;
using System;

namespace Microsoft.Azure.Experiments.Tests
{
class Client : IClient
{
public Client(Context context)
{
Context = context;
}

public Context Context { get; }

public T GetClient<T>()
where T: class, IDisposable
{
if (typeof(T) == typeof(INetworkManagementClient))
{
return new NetworkManagementClient(Context.Credentials)
{
SubscriptionId = Context.SubscriptionId
} as T;
}
else if (typeof(T) == typeof(IResourceManagementClient))
{
return new ResourceManagementClient(Context.Credentials)
{
SubscriptionId = Context.SubscriptionId
} as T;
}
throw new Exception("unknown client type");
}
}
}
2 changes: 1 addition & 1 deletion experiments/Azure.Experiments/Tests/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Azure.Experiments.Tests
namespace Microsoft.Azure.Experiments.Tests
{
internal sealed class Configuration
{
Expand Down
2 changes: 1 addition & 1 deletion experiments/Azure.Experiments/Tests/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Newtonsoft.Json;
using System.IO;

namespace Azure.Experiments.Tests
namespace Microsoft.Azure.Experiments.Tests
{
internal static class Credentials
{
Expand Down
2 changes: 1 addition & 1 deletion experiments/Azure.Experiments/Tests/KeyValuePair.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;

namespace Azure.Experiments.Tests
namespace Microsoft.Azure.Experiments.Tests
{
internal static class KeyValuePair
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,61 @@ namespace Azure.Experiments.Tests
{
public class ComputeTest
{
[Fact]
//[Fact]
public async Task ResourceGroupTest()
{
var c = Credentials.Get();
var c = Microsoft.Azure.Experiments.Tests.Credentials.Get();
var rg = new ResourceGroupObject(c, "My");
//
var info = await rg.GetOrNullAsync();
var infoCreate = await rg.GetOrCreateAsync();
// await rg.DeleteAsync(c);
}

[Fact]
//[Fact]
public async Task VirtualNetworkTest()
{
var c = Credentials.Get();
var c = Microsoft.Azure.Experiments.Tests.Credentials.Get();
var rg = new ResourceGroupObject(c, "My1");
var vn = new VirtualNetworkObject(c.CreateNetwork(), "My1", rg, "192.168.0.0/16");
//
var info = await vn.GetOrNullAsync();
var infoCreate = await vn.GetOrCreateAsync();
}

[Fact]
//[Fact]
public async Task PublicIpAddressTest()
{
var c = Credentials.Get();
var c = Microsoft.Azure.Experiments.Tests.Credentials.Get();
var rg = new ResourceGroupObject(c, "MyPIA");
var pia = new PublicIpAddressObject(c.CreateNetwork(), "MyPIA", rg);
//
var info = await pia.GetOrCreateAsync();
}

[Fact]
//[Fact]
public async Task NetworkSecurityGroupTest()
{
var c = Credentials.Get();
var c = Microsoft.Azure.Experiments.Tests.Credentials.Get();
var rg = new ResourceGroupObject(c, "MyNSG");
var nsg = new NetworkSecurityGroupObject(c.CreateNetwork(), "MyNSG", rg);
var info = await nsg.GetOrCreateAsync();
}

[Fact]
//[Fact]
public async Task SubnetTest()
{
var c = Credentials.Get();
var c = Microsoft.Azure.Experiments.Tests.Credentials.Get();
var rg = new ResourceGroupObject(c, "MySubnet");
var vn = new VirtualNetworkObject(c.CreateNetwork(), "MySubnet", rg, "192.168.0.0/16");
var subnet = new SubnetObject("MySubnet", vn, "192.168.1.0/24");
var info = await subnet.GetOrCreateAsync();
}

[Fact]
//[Fact]
public async Task NetworkInterfaceObject()
{
var c = Credentials.Get();
var c = Microsoft.Azure.Experiments.Tests.Credentials.Get();
var network = c.CreateNetwork();
var rg = new ResourceGroupObject(c, "MyNI");
var vn = new VirtualNetworkObject(network, "MyNI", rg, "192.168.0.0/16");
Expand All @@ -73,10 +73,10 @@ public async Task NetworkInterfaceObject()
var info = await ni.GetOrCreateAsync();
}

[Fact]
//[Fact]
public async Task VmObject()
{
var c = Credentials.Get();
var c = Microsoft.Azure.Experiments.Tests.Credentials.Get();
var network = c.CreateNetwork();
var rg = new ResourceGroupObject(c, "MyVM");
var vn = new VirtualNetworkObject(network, "MyVM", rg, "192.168.0.0/16");
Expand All @@ -88,10 +88,10 @@ public async Task VmObject()
var info = await vm.GetOrCreateAsync();
}

[Fact]
//[Fact]
public async Task Test1()
{
var c = Credentials.Get();
var c = Microsoft.Azure.Experiments.Tests.Credentials.Get();
var client = new ComputeManagementClient(c.Credentials)
{
SubscriptionId = c.SubscriptionId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Azure.Experiments.Compute;
using Azure.Experiments.Network;
using Microsoft.Azure.Experiments;
using Microsoft.Rest;
using Xunit;

Expand Down Expand Up @@ -36,7 +37,7 @@ public void PublicIpAddressObjectTest()
//[Fact]
public void NetworkSecurityGroupTest()
{
var c = Credentials.Get();
var c = Microsoft.Azure.Experiments.Tests.Credentials.Get();
var rg = new ResourceGroupObject(c, "MyNSG");
var nsg = new NetworkSecurityGroupObject(c.CreateNetwork(), "MyNSG", rg);
Assert.Equal(2, nsg.Priority);
Expand Down
57 changes: 57 additions & 0 deletions experiments/Azure.Experiments/Tests/ResourceGroupTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Microsoft.Azure.Experiments.ResourceManager;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.Azure.Experiments.Tests
{
public class ResourceGroupTest
{
[Fact]
public void CreateConfigTest()
{
var rg = ResourceGroupPolicy.CreateResourceGroupConfig("new");
var id = rg.GetId("12345").IdToString();
Assert.Equal("/subscriptions/12345/resourceGroups/new", id);
}

[Fact]
public async Task GetAsyncTest()
{
var rg = ResourceGroupPolicy.CreateResourceGroupConfig("new");
var client = new Client(Credentials.Get());
var state = await rg.GetAsync(client, new CancellationToken());
var location = state.GetLocation(rg);
Assert.Null(location);
}

[Fact]
public async Task CreateParameterTest()
{
var rg = ResourceGroupPolicy.CreateResourceGroupConfig("new");
var client = new Client(Credentials.Get());
var state = await rg.GetAsync(client, new CancellationToken());
var location = state.GetLocation(rg);
var parameters = rg.GetParameters(client.Context.SubscriptionId, "eastus");
var rgc = parameters.GetOrNull(rg);
Assert.Equal("eastus", rgc.Location);
}

[Fact]
public async Task CreateAsyncTest()
{
var rg = ResourceGroupPolicy.CreateResourceGroupConfig("new1");
var client = new Client(Credentials.Get());
var state = await rg.GetAsync(client, new CancellationToken());
var location = state.GetLocation(rg);
var parameters = rg.GetParameters(client.Context.SubscriptionId, "eastus");
var rgc = parameters.GetOrNull(rg);
var createState = await rg.CreateOrUpdateAsync(
client, state, parameters, new CancellationToken());
var rgcc = createState.GetOrNull(rg);
Assert.Equal("eastus", rgcc.Location);
Assert.Equal("new1", rgcc.Name);
Assert.Equal(rg.GetId(client.Context.SubscriptionId).IdToString(), rgcc.Id);
}
}
}
2 changes: 1 addition & 1 deletion experiments/Azure.Experiments/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<AssemblyName>Tests</AssemblyName>

<RootNamespace>Azure.Experiments.Tests</RootNamespace>
<RootNamespace>Microsoft.Azure.Experiments.Tests</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion experiments/Azure.Experiments/Tests/TokenProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System;
using Newtonsoft.Json;

namespace Azure.Experiments.Tests
namespace Microsoft.Azure.Experiments.Tests
{
sealed class TokenProvider : ITokenProvider
{
Expand Down
Loading

0 comments on commit 381a986

Please sign in to comment.