Skip to content

Commit

Permalink
[Subscription] Add tests (Azure#31128)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvbb authored Sep 16, 2022
1 parent 3bb79e9 commit 055c476
Show file tree
Hide file tree
Showing 37 changed files with 1,390,342 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.TestFramework;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Subscription.Models;
using Azure.ResourceManager.Subscription.Tests;
using NUnit.Framework;

namespace Azure.ResourceManager.Subscription.Tests
{
internal class BillingAccountPoliciesResponseTests : SubscriptionManagementTestBase
{
private BillingAccountPoliciesResponseCollection _billingAccountPolicesCollection => GetBillingAccountPoliciesResponseCollection().Result;

public BillingAccountPoliciesResponseTests(bool isAsync) : base(isAsync)
{
}

private async Task<BillingAccountPoliciesResponseCollection> GetBillingAccountPoliciesResponseCollection()
{
var tenants = await Client.GetTenants().GetAllAsync().ToEnumerableAsync();
return tenants.FirstOrDefault().GetBillingAccountPoliciesResponses();
}

[RecordedTest]
public void Exist()
{
// Unable to get a valid account name
// Azure.RequestFailedException : Invalid billing account name.
Assert.ThrowsAsync<RequestFailedException>(() => _billingAccountPolicesCollection.ExistsAsync("testBillingAccountId"));
}

[RecordedTest]
public void Get()
{
// Unable to get a valid account name
// Azure.RequestFailedException : Invalid billing account name.
Assert.ThrowsAsync<RequestFailedException>(() => _billingAccountPolicesCollection.GetAsync("testBillingAccountId"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Azure.Core.TestFramework;
using Azure.ResourceManager.Subscription.Models;
using NUnit.Framework;

namespace Azure.ResourceManager.Subscription.Tests
{
internal class SubscriptionAliasResponseTests : SubscriptionManagementTestBase
{
private SubscriptionAliasResponseCollection _aliasCollection => GetAliasCollection().Result;

public SubscriptionAliasResponseTests(bool isAsync) : base(isAsync)
{
}

[TearDown]
public async Task TestTearDown()
{
var list = await _aliasCollection.GetAllAsync().ToEnumerableAsync();
foreach (var item in list)
{
await item.DeleteAsync(WaitUntil.Completed);
}
}

private async Task<SubscriptionAliasResponseCollection> GetAliasCollection()
{
var tenants = await Client.GetTenants().GetAllAsync().ToEnumerableAsync();
return tenants.FirstOrDefault().GetSubscriptionAliasResponses();
}

private async Task<SubscriptionAliasResponseResource> CreateAliasResponse(string aliasName)
{
var data = new SubscriptionAliasResponseCreateOrUpdateContent()
{
Properties = new PutAliasRequestProperties()
{
Workload = "Production",
SubscriptionId = Environment.GetEnvironmentVariable("SUBSCRIPTION_ID"),
}
};
data.Properties.AdditionalProperties = new PutAliasRequestAdditionalProperties();
data.Properties.AdditionalProperties.Tags.Add(new KeyValuePair<string, string>("tag1", "test1"));
data.Properties.AdditionalProperties.Tags.Add(new KeyValuePair<string, string>("tag2", "test2"));
var alias = await _aliasCollection.CreateOrUpdateAsync(WaitUntil.Completed, aliasName, data);
return alias.Value;
}

[RecordedTest]
[Ignore("pipeline playback error")]
public async Task CreateOrUpdate()
{
string aliasName = Recording.GenerateAssetName("test-alias-");
var alias = await CreateAliasResponse(aliasName);
ValidateAliasResponse(alias);
Assert.AreEqual(aliasName, alias.Data.Name);
}

[RecordedTest]
[Ignore("pipeline playback error")]
public async Task Exist()
{
string aliasName = Recording.GenerateAssetName("test-alias-");
var alias = await CreateAliasResponse(aliasName);
bool flag = await _aliasCollection.ExistsAsync(aliasName);
Assert.IsTrue(flag);
}

[RecordedTest]
[Ignore("pipeline playback error")]
public async Task Get()
{
string aliasName = Recording.GenerateAssetName("test-alias-");
await CreateAliasResponse(aliasName);
var alias = await _aliasCollection.GetAsync(aliasName);
ValidateAliasResponse(alias);
Assert.AreEqual(aliasName, alias.Value.Data.Name);
}

[RecordedTest]
[Ignore("pipeline playback error")]
public async Task GetAll()
{
string aliasName = Recording.GenerateAssetName("test-alias-");
await CreateAliasResponse(aliasName);
var list = await _aliasCollection.GetAllAsync().ToEnumerableAsync();
Assert.IsNotEmpty(list);
ValidateAliasResponse(list.FirstOrDefault());
}

[RecordedTest]
[Ignore("pipeline playback error")]
public async Task Delete()
{
string aliasName = Recording.GenerateAssetName("test-alias-");
var alias = await CreateAliasResponse(aliasName);
bool flag = await _aliasCollection.ExistsAsync(aliasName);
Assert.IsTrue(flag);

await alias.DeleteAsync(WaitUntil.Completed);
flag = await _aliasCollection.ExistsAsync(aliasName);
Assert.IsFalse(flag);
}

private void ValidateAliasResponse(SubscriptionAliasResponseResource alias)
{
Assert.IsNotNull(alias);
Assert.AreEqual("Microsoft.Subscription", alias.Data.ResourceType.Namespace);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Azure.Core.TestFramework;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Subscription.Models;
using NUnit.Framework;

namespace Azure.ResourceManager.Subscription.Tests
{
internal class SubscriptionExtensionTests : SubscriptionManagementTestBase
{
private SubscriptionResource _subscription;

public SubscriptionExtensionTests(bool isAsync) : base(isAsync)
{
}

[SetUp]
public async Task TestSetUp()
{
_subscription = await Client.GetDefaultSubscriptionAsync();
}

[RecordedTest]
public async Task GetLocationsSubscriptions()
{
var list = await _subscription.GetLocationsAsync().ToEnumerableAsync();
Assert.IsNotEmpty(list);
Assert.IsNotNull(list.First(item => item.Name == "eastus"));
}

[RecordedTest]
public async Task PredefinedTagOperations()
{
// CreateOrUpdate
string tagName = "testEmptyTag";
var predefinedTag = await _subscription.CreateOrUpdatePredefinedTagAsync(tagName);
Assert.IsNotNull(predefinedTag);
Assert.AreEqual(tagName, predefinedTag.Value.TagName);

// GetAll
var list = await _subscription.GetAllPredefinedTagsAsync().ToEnumerableAsync();
Assert.IsNotEmpty(list);
Assert.IsNotNull(list.First(item => item.TagName == tagName));

// Delete
var deleteResponse = await _subscription.DeletePredefinedTagAsync(tagName);
Assert.AreEqual(200, deleteResponse.Status);
}

[RecordedTest]
public async Task PredefinedTagValueOperations()
{
string tagName = "testTag";
await _subscription.CreateOrUpdatePredefinedTagAsync(tagName);

// Add a TagValue
string tagValue = "preTestValue";
var predefinedTagValue = await _subscription.CreateOrUpdatePredefinedTagValueAsync(tagName, tagValue);
Assert.IsNotNull(predefinedTagValue);
Assert.AreEqual(tagValue, predefinedTagValue.Value.TagValue);

// Delete a TagValue
var deleteResponse1 = await _subscription.CreateOrUpdatePredefinedTagValueAsync(tagName, tagValue);
Assert.AreEqual(200, deleteResponse1.GetRawResponse().Status);

// Delete--409 Please delete all the associated tag values before deleting the tag name.
//var deleteResponse = await _subscription.DeletePredefinedTagAsync(tagName);
//Assert.AreEqual(200, deleteResponse.Status);
}

[RecordedTest]
public async Task GetFeatures()
{
var list = await _subscription.GetFeaturesAsync().ToEnumerableAsync();
Assert.IsNotEmpty(list);
}

[RecordedTest]
[PlaybackOnly("Dangerous operations")]
public async Task CancelSubscription()
{
var response = await _subscription.CancelSubscriptionAsync();
Assert.AreEqual(200,response.GetRawResponse().Status);
}

[RecordedTest]
[PlaybackOnly("Dangerous operations")]
public async Task RenameSubscription()
{
SubscriptionName data = new SubscriptionName()
{
SubscriptionNameValue = "azSubscriptionTestName"
};
var response = await _subscription.RenameSubscriptionAsync(data);
Assert.AreEqual(200,response.GetRawResponse().Status);
}

[RecordedTest]
[Ignore("400. NotAllowed. Reactivation is not supported for subscription")]
public async Task EnableSubscription()
{
var response = await _subscription.EnableSubscriptionAsync();
Assert.AreEqual(200,response.GetRawResponse().Status);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Azure.Core.TestFramework;
using Azure.ResourceManager.Resources;
using NUnit.Framework;

namespace Azure.ResourceManager.Subscription.Tests
{
internal class TenantPolicyDefinitionTests : SubscriptionManagementTestBase
{
private TenantPolicyDefinitionCollection _tenantPolicyDefinitionCollection => GetTenantPolicyDefinition().Result;

public TenantPolicyDefinitionTests(bool isAsync) : base(isAsync)
{
}

private async Task<TenantPolicyDefinitionCollection> GetTenantPolicyDefinition()
{
var tenants = await Client.GetTenants().GetAllAsync().ToEnumerableAsync();
return tenants.FirstOrDefault().GetTenantPolicyDefinitions();
}

[RecordedTest]
public async Task GetAll()
{
var list = await _tenantPolicyDefinitionCollection.GetAllAsync().ToEnumerableAsync();
Assert.IsNotEmpty(list);
}

[RecordedTest]
public async Task Exist()
{
var list = await _tenantPolicyDefinitionCollection.GetAllAsync().ToEnumerableAsync();
string policyName = list.FirstOrDefault().Data.Name;
bool flag = await _tenantPolicyDefinitionCollection.ExistsAsync(policyName);
Assert.IsTrue(flag);
}

[RecordedTest]
public async Task Get()
{
var list = await _tenantPolicyDefinitionCollection.GetAllAsync().ToEnumerableAsync();
string policyName = list.FirstOrDefault().Data.Name;
var policy = await _tenantPolicyDefinitionCollection.GetAsync(policyName);
Assert.IsNotNull(policy);
Assert.AreEqual(policyName, policy.Value.Data.Name);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Azure.Core.TestFramework;
using Azure.ResourceManager.Resources;
using NUnit.Framework;

namespace Azure.ResourceManager.Subscription.Tests
{
internal class TenantPolicyResponseTests : SubscriptionManagementTestBase
{
public TenantPolicyResponseTests(bool isAsync) : base(isAsync)
{
}

[RecordedTest]
[Ignore("https://github.com/Azure/azure-sdk-for-net/issues/31127")]
public async Task GetAll()
{
var tenants = await Client.GetTenants().GetAllAsync().ToEnumerableAsync();
var response = tenants.FirstOrDefault().GetGetTenantPolicyResponse();
Assert.NotNull(response);
}
}
}
Loading

0 comments on commit 055c476

Please sign in to comment.