Skip to content

Commit 381a986

Browse files
Unit tests.
1 parent ca51bc7 commit 381a986

19 files changed

+184
-48
lines changed

experiments/Azure.Experiments/Azure.Experiments/ResourceOperations.cs renamed to experiments/Azure.Experiments/Azure.Experiments/CreateOrUpdateAsyncOperation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace Microsoft.Azure.Experiments
66
{
7-
public static class ResourceOperations
7+
public static class CreateOrUpdateAsyncOperation
88
{
9-
public static async Task<IState> CreateAsync<Config>(
9+
public static async Task<IState> CreateOrUpdateAsync<Config>(
1010
this IResourceConfig<Config> config,
1111
IClient client,
1212
IState current,

experiments/Azure.Experiments/Azure.Experiments/CurrentState.cs renamed to experiments/Azure.Experiments/Azure.Experiments/GetAsyncOperation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
namespace Microsoft.Azure.Experiments
88
{
9-
public static class CurrentState
9+
public static class GetAsyncOperation
1010
{
11-
public static async Task<IState> GetState<Config>(
11+
public static async Task<IState> GetAsync<Config>(
1212
this IResourceConfig<Config> resourceConfig,
1313
IClient client,
1414
CancellationToken cancellationToken)

experiments/Azure.Experiments/Azure.Experiments/Network/VirtualNetworkPolicy.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,18 @@ public static class VirtualNetworkPolicy
1616
p.ResourceGroupName, p.Name, p.Config, p.CancellationToken));
1717

1818
public static ResourceConfig<VirtualNetwork> CreateVirtualNetworkConfig(
19-
this ResourceConfig<ResourceGroup> resourceGroup, string name)
20-
=> Policy.CreateConfig(resourceGroup, name);
19+
this ResourceConfig<ResourceGroup> resourceGroup,
20+
string name,
21+
string addressPrefix)
22+
=> Policy.CreateConfig(
23+
resourceGroup,
24+
name,
25+
_ => new VirtualNetwork
26+
{
27+
AddressSpace = new AddressSpace
28+
{
29+
AddressPrefixes = new[] { addressPrefix }
30+
}
31+
});
2132
}
2233
}

experiments/Azure.Experiments/Azure.Experiments/Old/Compute/VirtualMachineObject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Azure.Experiments.Network;
2+
using Microsoft.Azure.Experiments;
23
using Microsoft.Azure.Management.Compute;
34
using Microsoft.Azure.Management.Compute.Models;
45
using System.Threading.Tasks;
Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
1-
using Microsoft.Azure.Management.Network;
2-
using Microsoft.Rest;
1+
using Microsoft.Azure.Experiments;
2+
using Microsoft.Azure.Management.Network;
33

44
namespace Azure.Experiments
55
{
6-
public class Context
6+
public static class ContextEx
77
{
8-
public Context(ServiceClientCredentials credentials, string subscriptionId)
9-
{
10-
Credentials = credentials;
11-
SubscriptionId = subscriptionId;
12-
}
13-
14-
public ServiceClientCredentials Credentials { get; }
15-
16-
public string SubscriptionId { get; }
17-
18-
public NetworkManagementClient CreateNetwork()
19-
=> new NetworkManagementClient(Credentials)
8+
public static NetworkManagementClient CreateNetwork(this Context context)
9+
=> new NetworkManagementClient(context.Credentials)
2010
{
21-
SubscriptionId = SubscriptionId
11+
SubscriptionId = context.SubscriptionId
2212
};
2313
}
2414
}

experiments/Azure.Experiments/Azure.Experiments/Old/ResourceGroupObject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Azure.Management.ResourceManager.Models;
22
using Microsoft.Azure.Management.ResourceManager;
33
using System.Threading.Tasks;
4+
using Microsoft.Azure.Experiments;
45

56
namespace Azure.Experiments
67
{

experiments/Azure.Experiments/Azure.Experiments/CreateParameters.cs renamed to experiments/Azure.Experiments/Azure.Experiments/Parameters.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
using System.Collections.Concurrent;
1+
using System.Linq;
22

33
namespace Microsoft.Azure.Experiments
44
{
55
public static class Parameters
66
{
77
public static IState GetParameters<Config>(
8-
string subscription, string location, IResourceConfig<Config> config)
8+
this IResourceConfig<Config> config,
9+
string subscription,
10+
string location)
911
where Config : class
1012
{
1113
var visitor = new Visitor(subscription, location);
@@ -21,12 +23,19 @@ public Visitor(string subscription, string location)
2123
Location = location;
2224
}
2325

26+
public object GetUntyped(IResourceConfig config)
27+
=> Result.GetOrAddUntyped(config, () => config.Apply(this));
28+
2429
public Config Get<Config>(IResourceConfig<Config> config)
2530
where Config : class
26-
=> Result.GetOrAdd(config, () => config.Apply(this) as Config);
31+
=> GetUntyped(config) as Config;
2732

2833
public object Visit<Config>(ResourceConfig<Config> config) where Config : class
2934
{
35+
foreach (var d in config.Dependencies)
36+
{
37+
GetUntyped(d);
38+
}
3039
var p = config.CreateConfig(Subscription);
3140
config.Policy.SetLocation(p, Location);
3241
return p;

experiments/Azure.Experiments/Azure.Experiments/ResourcePolicy.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public ResourcePolicy(
2424
Func<Config, string> getLocation,
2525
Action<Config, string> setLocation)
2626
{
27+
GetId = getId;
2728
GetAsync = getAsync;
2829
CreateOrUpdateAsync = createOrUpdateAsync;
2930
GetLocation = getLocation;

experiments/Azure.Experiments/Tests/AuthenticationResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Azure.Experiments.Tests
1+
namespace Microsoft.Azure.Experiments.Tests
22
{
33
internal sealed class AuthenticationResponse
44
{
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Microsoft.Azure.Management.Network;
2+
using Microsoft.Azure.Management.ResourceManager;
3+
using System;
4+
5+
namespace Microsoft.Azure.Experiments.Tests
6+
{
7+
class Client : IClient
8+
{
9+
public Client(Context context)
10+
{
11+
Context = context;
12+
}
13+
14+
public Context Context { get; }
15+
16+
public T GetClient<T>()
17+
where T: class, IDisposable
18+
{
19+
if (typeof(T) == typeof(INetworkManagementClient))
20+
{
21+
return new NetworkManagementClient(Context.Credentials)
22+
{
23+
SubscriptionId = Context.SubscriptionId
24+
} as T;
25+
}
26+
else if (typeof(T) == typeof(IResourceManagementClient))
27+
{
28+
return new ResourceManagementClient(Context.Credentials)
29+
{
30+
SubscriptionId = Context.SubscriptionId
31+
} as T;
32+
}
33+
throw new Exception("unknown client type");
34+
}
35+
}
36+
}

experiments/Azure.Experiments/Tests/Configuration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Azure.Experiments.Tests
1+
namespace Microsoft.Azure.Experiments.Tests
22
{
33
internal sealed class Configuration
44
{

experiments/Azure.Experiments/Tests/Credentials.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Newtonsoft.Json;
33
using System.IO;
44

5-
namespace Azure.Experiments.Tests
5+
namespace Microsoft.Azure.Experiments.Tests
66
{
77
internal static class Credentials
88
{

experiments/Azure.Experiments/Tests/KeyValuePair.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Collections.Generic;
22

3-
namespace Azure.Experiments.Tests
3+
namespace Microsoft.Azure.Experiments.Tests
44
{
55
internal static class KeyValuePair
66
{

experiments/Azure.Experiments/Tests/ComputeTest.cs renamed to experiments/Azure.Experiments/Tests/Old/ComputeTest.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,61 @@ namespace Azure.Experiments.Tests
88
{
99
public class ComputeTest
1010
{
11-
[Fact]
11+
//[Fact]
1212
public async Task ResourceGroupTest()
1313
{
14-
var c = Credentials.Get();
14+
var c = Microsoft.Azure.Experiments.Tests.Credentials.Get();
1515
var rg = new ResourceGroupObject(c, "My");
1616
//
1717
var info = await rg.GetOrNullAsync();
1818
var infoCreate = await rg.GetOrCreateAsync();
1919
// await rg.DeleteAsync(c);
2020
}
2121

22-
[Fact]
22+
//[Fact]
2323
public async Task VirtualNetworkTest()
2424
{
25-
var c = Credentials.Get();
25+
var c = Microsoft.Azure.Experiments.Tests.Credentials.Get();
2626
var rg = new ResourceGroupObject(c, "My1");
2727
var vn = new VirtualNetworkObject(c.CreateNetwork(), "My1", rg, "192.168.0.0/16");
2828
//
2929
var info = await vn.GetOrNullAsync();
3030
var infoCreate = await vn.GetOrCreateAsync();
3131
}
3232

33-
[Fact]
33+
//[Fact]
3434
public async Task PublicIpAddressTest()
3535
{
36-
var c = Credentials.Get();
36+
var c = Microsoft.Azure.Experiments.Tests.Credentials.Get();
3737
var rg = new ResourceGroupObject(c, "MyPIA");
3838
var pia = new PublicIpAddressObject(c.CreateNetwork(), "MyPIA", rg);
3939
//
4040
var info = await pia.GetOrCreateAsync();
4141
}
4242

43-
[Fact]
43+
//[Fact]
4444
public async Task NetworkSecurityGroupTest()
4545
{
46-
var c = Credentials.Get();
46+
var c = Microsoft.Azure.Experiments.Tests.Credentials.Get();
4747
var rg = new ResourceGroupObject(c, "MyNSG");
4848
var nsg = new NetworkSecurityGroupObject(c.CreateNetwork(), "MyNSG", rg);
4949
var info = await nsg.GetOrCreateAsync();
5050
}
5151

52-
[Fact]
52+
//[Fact]
5353
public async Task SubnetTest()
5454
{
55-
var c = Credentials.Get();
55+
var c = Microsoft.Azure.Experiments.Tests.Credentials.Get();
5656
var rg = new ResourceGroupObject(c, "MySubnet");
5757
var vn = new VirtualNetworkObject(c.CreateNetwork(), "MySubnet", rg, "192.168.0.0/16");
5858
var subnet = new SubnetObject("MySubnet", vn, "192.168.1.0/24");
5959
var info = await subnet.GetOrCreateAsync();
6060
}
6161

62-
[Fact]
62+
//[Fact]
6363
public async Task NetworkInterfaceObject()
6464
{
65-
var c = Credentials.Get();
65+
var c = Microsoft.Azure.Experiments.Tests.Credentials.Get();
6666
var network = c.CreateNetwork();
6767
var rg = new ResourceGroupObject(c, "MyNI");
6868
var vn = new VirtualNetworkObject(network, "MyNI", rg, "192.168.0.0/16");
@@ -73,10 +73,10 @@ public async Task NetworkInterfaceObject()
7373
var info = await ni.GetOrCreateAsync();
7474
}
7575

76-
[Fact]
76+
//[Fact]
7777
public async Task VmObject()
7878
{
79-
var c = Credentials.Get();
79+
var c = Microsoft.Azure.Experiments.Tests.Credentials.Get();
8080
var network = c.CreateNetwork();
8181
var rg = new ResourceGroupObject(c, "MyVM");
8282
var vn = new VirtualNetworkObject(network, "MyVM", rg, "192.168.0.0/16");
@@ -88,10 +88,10 @@ public async Task VmObject()
8888
var info = await vm.GetOrCreateAsync();
8989
}
9090

91-
[Fact]
91+
//[Fact]
9292
public async Task Test1()
9393
{
94-
var c = Credentials.Get();
94+
var c = Microsoft.Azure.Experiments.Tests.Credentials.Get();
9595
var client = new ComputeManagementClient(c.Credentials)
9696
{
9797
SubscriptionId = c.SubscriptionId

experiments/Azure.Experiments/Tests/UnitTests.cs renamed to experiments/Azure.Experiments/Tests/Old/UnitTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Azure.Experiments.Compute;
22
using Azure.Experiments.Network;
3+
using Microsoft.Azure.Experiments;
34
using Microsoft.Rest;
45
using Xunit;
56

@@ -36,7 +37,7 @@ public void PublicIpAddressObjectTest()
3637
//[Fact]
3738
public void NetworkSecurityGroupTest()
3839
{
39-
var c = Credentials.Get();
40+
var c = Microsoft.Azure.Experiments.Tests.Credentials.Get();
4041
var rg = new ResourceGroupObject(c, "MyNSG");
4142
var nsg = new NetworkSecurityGroupObject(c.CreateNetwork(), "MyNSG", rg);
4243
Assert.Equal(2, nsg.Priority);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using Microsoft.Azure.Experiments.ResourceManager;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Xunit;
5+
6+
namespace Microsoft.Azure.Experiments.Tests
7+
{
8+
public class ResourceGroupTest
9+
{
10+
[Fact]
11+
public void CreateConfigTest()
12+
{
13+
var rg = ResourceGroupPolicy.CreateResourceGroupConfig("new");
14+
var id = rg.GetId("12345").IdToString();
15+
Assert.Equal("/subscriptions/12345/resourceGroups/new", id);
16+
}
17+
18+
[Fact]
19+
public async Task GetAsyncTest()
20+
{
21+
var rg = ResourceGroupPolicy.CreateResourceGroupConfig("new");
22+
var client = new Client(Credentials.Get());
23+
var state = await rg.GetAsync(client, new CancellationToken());
24+
var location = state.GetLocation(rg);
25+
Assert.Null(location);
26+
}
27+
28+
[Fact]
29+
public async Task CreateParameterTest()
30+
{
31+
var rg = ResourceGroupPolicy.CreateResourceGroupConfig("new");
32+
var client = new Client(Credentials.Get());
33+
var state = await rg.GetAsync(client, new CancellationToken());
34+
var location = state.GetLocation(rg);
35+
var parameters = rg.GetParameters(client.Context.SubscriptionId, "eastus");
36+
var rgc = parameters.GetOrNull(rg);
37+
Assert.Equal("eastus", rgc.Location);
38+
}
39+
40+
[Fact]
41+
public async Task CreateAsyncTest()
42+
{
43+
var rg = ResourceGroupPolicy.CreateResourceGroupConfig("new1");
44+
var client = new Client(Credentials.Get());
45+
var state = await rg.GetAsync(client, new CancellationToken());
46+
var location = state.GetLocation(rg);
47+
var parameters = rg.GetParameters(client.Context.SubscriptionId, "eastus");
48+
var rgc = parameters.GetOrNull(rg);
49+
var createState = await rg.CreateOrUpdateAsync(
50+
client, state, parameters, new CancellationToken());
51+
var rgcc = createState.GetOrNull(rg);
52+
Assert.Equal("eastus", rgcc.Location);
53+
Assert.Equal("new1", rgcc.Name);
54+
Assert.Equal(rg.GetId(client.Context.SubscriptionId).IdToString(), rgcc.Id);
55+
}
56+
}
57+
}

experiments/Azure.Experiments/Tests/Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<AssemblyName>Tests</AssemblyName>
99

10-
<RootNamespace>Azure.Experiments.Tests</RootNamespace>
10+
<RootNamespace>Microsoft.Azure.Experiments.Tests</RootNamespace>
1111
</PropertyGroup>
1212

1313
<ItemGroup>

experiments/Azure.Experiments/Tests/TokenProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System;
77
using Newtonsoft.Json;
88

9-
namespace Azure.Experiments.Tests
9+
namespace Microsoft.Azure.Experiments.Tests
1010
{
1111
sealed class TokenProvider : ITokenProvider
1212
{

0 commit comments

Comments
 (0)