Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CassandraTest #14886

Merged
merged 8 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added CassandraTest
  • Loading branch information
nisha-bhatia committed Sep 4, 2020
commit 580a57495134e4c5fb40724f035c63c71ceba136
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using Azure.Core.TestFramework;
using Azure.Management.Resources;
using Azure.ResourceManager.TestFramework;
Expand All @@ -15,6 +17,24 @@ public abstract class CosmosDBManagementClientBase : ManagementRecordedTestBase<
public ResourceGroupsOperations ResourceGroupsOperations { get; set; }
public CosmosDBManagementClient CosmosDBManagementClient { get; set; }

// Vars for Cassandra tests - using an existing DB account, since Account provisioning takes 10-15 minutes
public string location = "West US";
public string resourceGroupName;
nisha-bhatia marked this conversation as resolved.
Show resolved Hide resolved
public string databaseAccountName = "db" + new Random().Next(10000);
bquantump marked this conversation as resolved.
Show resolved Hide resolved
public string keyspaceName = "keyspaceName2510";
public string keyspaceName2 = "keyspaceName22510";
public string tableName = "tableName2510";
public string cassandraThroughputType = "Microsoft.DocumentDB/databaseAccounts/cassandraKeyspaces/throughputSettings";
public int sampleThroughput = 700;
public Dictionary<string, string> additionalProperties = new Dictionary<string, string>
nisha-bhatia marked this conversation as resolved.
Show resolved Hide resolved
{
{"foo","bar"}
};
public Dictionary<string, string> tags = new Dictionary<string, string>
nisha-bhatia marked this conversation as resolved.
Show resolved Hide resolved
{
{"key3","value3"},
{"key4","value4"}
};
protected CosmosDBManagementClientBase(bool isAsync)
: base(isAsync)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Core.TestFramework;
using Azure.ResourceManager.CosmosDB.Models;
using NUnit.Framework;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Azure.ResourceManager.CosmosDB.Tests
{
[TestFixture]
public class CassandraResourcesOperationsTests : CosmosDBManagementClientBase
{
public CassandraResourcesOperationsTests()
: base(true)
{
}

[SetUp]
public async Task ClearAndInitialize()
{
if (Mode == RecordedTestMode.Record || Mode == RecordedTestMode.Playback)
{
InitializeClients();
resourceGroupName = Recording.GenerateAssetName(CosmosDBTestUtilities.ResourceGroupPrefix);
await CosmosDBTestUtilities.TryRegisterResourceGroupAsync(ResourceGroupsOperations, CosmosDBTestUtilities.Location, resourceGroupName);
}
}

[TearDown]
public async Task CleanupResourceGroup()
{
await CleanupResourceGroupsAsync();
}

[TestCase]
public async Task CassandraCRUDTestsAsync()
{
var cosmosDBClient = GetCosmosDBManagementClient();
bquantump marked this conversation as resolved.
Show resolved Hide resolved

var locations = new List<Location>();
Location loc = new Location();
loc.LocationName = "West US";
bquantump marked this conversation as resolved.
Show resolved Hide resolved
loc.FailoverPriority = 0;
loc.IsZoneRedundant = false;
locations.Add(loc);

DatabaseAccountCreateUpdateParameters databaseAccountCreateUpdateParameters = new DatabaseAccountCreateUpdateParameters(locations);
databaseAccountCreateUpdateParameters.Location = "West US";
databaseAccountCreateUpdateParameters.Capabilities.Add(new Capability("EnableCassandra"));
databaseAccountCreateUpdateParameters.ConsistencyPolicy = new (DefaultConsistencyLevel.Eventual);

await WaitForCompletionAsync(await cosmosDBClient.DatabaseAccounts.StartCreateOrUpdateAsync(resourceGroupName, databaseAccountName, databaseAccountCreateUpdateParameters));

Task<Response> taskIsDatabaseNameExists = cosmosDBClient.DatabaseAccounts.CheckNameExistsAsync(databaseAccountName);
Response isDatabaseNameExists = taskIsDatabaseNameExists.ConfigureAwait(false).GetAwaiter().GetResult();
if (isDatabaseNameExists.Status != 200)
{
return;
bquantump marked this conversation as resolved.
Show resolved Hide resolved
}

CassandraKeyspaceCreateUpdateParameters cassandraKeyspaceCreateUpdateParameters = new CassandraKeyspaceCreateUpdateParameters(new CassandraKeyspaceResource(keyspaceName), new CreateUpdateOptions());

var cassandraKeyspaceResponse = await WaitForCompletionAsync (await cosmosDBClient.CassandraResources.StartCreateUpdateCassandraKeyspaceAsync(resourceGroupName, databaseAccountName, keyspaceName, cassandraKeyspaceCreateUpdateParameters));

CassandraKeyspaceGetResults cassandraKeyspaceGetResults = cassandraKeyspaceResponse.Value;

Assert.NotNull(cassandraKeyspaceGetResults);
Assert.AreEqual(keyspaceName, cassandraKeyspaceGetResults.Name);

var cassandraKeyspaceResponse1 = await WaitForCompletionAsync(await cosmosDBClient.CassandraResources.StartCreateUpdateCassandraKeyspaceAsync(resourceGroupName, databaseAccountName, keyspaceName, cassandraKeyspaceCreateUpdateParameters));

CassandraKeyspaceGetResults cassandraKeyspaceGetResults1 = cassandraKeyspaceResponse1.Value;
Assert.NotNull(cassandraKeyspaceGetResults1);
Assert.AreEqual(keyspaceName, cassandraKeyspaceGetResults1.Name);

VerifyEqualCassandraDatabases(cassandraKeyspaceGetResults, cassandraKeyspaceGetResults1);

CassandraKeyspaceCreateUpdateParameters cassandraKeyspaceCreateUpdateParameters2 = new CassandraKeyspaceCreateUpdateParameters(new CassandraKeyspaceResource(keyspaceName2), new CreateUpdateOptions(sampleThroughput, default));

var cassandraKeyspaceResponse2 = await WaitForCompletionAsync(await cosmosDBClient.CassandraResources.StartCreateUpdateCassandraKeyspaceAsync(resourceGroupName, databaseAccountName, keyspaceName2, cassandraKeyspaceCreateUpdateParameters2));

CassandraKeyspaceGetResults cassandraKeyspaceGetResults2 = cassandraKeyspaceResponse2.Value;
Assert.NotNull(cassandraKeyspaceGetResults2);
Assert.AreEqual(keyspaceName2, cassandraKeyspaceGetResults2.Name);

var keyspacesResponse = cosmosDBClient.CassandraResources.ListCassandraKeyspacesAsync(resourceGroupName, databaseAccountName);
Task<List<CassandraKeyspaceGetResults>> taskCassandraKeyspaces = keyspacesResponse.ToEnumerableAsync();
List<CassandraKeyspaceGetResults> cassandraKeyspaces = taskCassandraKeyspaces.ConfigureAwait(false).GetAwaiter().GetResult();
bquantump marked this conversation as resolved.
Show resolved Hide resolved

Assert.NotNull(cassandraKeyspaces);

var throughputResponse = cosmosDBClient.CassandraResources.GetCassandraKeyspaceThroughputAsync(resourceGroupName, databaseAccountName, keyspaceName2);
ThroughputSettingsGetResults throughputSettingsGetResults = throughputResponse.ConfigureAwait(false).GetAwaiter().GetResult();
Assert.NotNull(throughputSettingsGetResults);
Assert.NotNull(throughputSettingsGetResults.Name);
Assert.AreEqual(throughputSettingsGetResults.Resource.Throughput, sampleThroughput);
Assert.AreEqual(cassandraThroughputType, throughputSettingsGetResults.Type);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may forgot to test 4 operations in keyspace: MigrateToAutoscale, MigrateToManualThroughput. ThroughputGet, ThroughputUpdate
Same in cassandra table tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests weren't in Track 1. The methods for these examples didn't seem to work, so I am not doing them for now. We can leave a TODO: comment if necessary. This way we can get back to it in the future if we end up needing these tests.

//TODO: Migrate To Manual and Autoscale tests from example: https://github.com/Azure/azure-rest-api-specs/blob/master/specification/cosmos-db/resource-manager/Microsoft.DocumentDB/stable/2020-04-01/examples/CosmosDBCassandraTableMigrateToAutoscale.json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a test I found from the example json that wasn't covered in Track 1. The method to MigrateToManual didn't seem to work, so I've left a comment in case we ever want to cover this test. I can also remove the TODO: if that's preferred.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What were the issues you were facing in MigrateToManual method ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am getting a 404 error for "Entity not found". This is how I'm trying to use the method:
`var migrateKeyspace = await CosmosDBManagementClient.CassandraResources.StartMigrateCassandraKeyspaceToManualThroughputAsync(resourceGroupName, databaseAccountName, keyspaceName);'. The resourceGroupName, databaseAccountName, and keyspaceName are valid before the call. Is there another way I should be using the method?

//var migrateKeyspace = await cosmosDBClient.CassandraResources.StartMigrateCassandraKeyspaceToManualThroughputAsync(resourceGroupName, databaseAccountName, keyspaceName);
//var taskResponseMigrate = migrateKeyspace.WaitForCompletionAsync();
//var responseMigrate = taskResponseMigrate.ConfigureAwait(false).GetAwaiter().GetResult();
//Assert.AreEqual(responseMigrate.Value.Resource.MinimumThroughput, responseMigrate.Value.Resource.Throughput);

CassandraTableCreateUpdateParameters cassandraTableCreateUpdateParameters = new CassandraTableCreateUpdateParameters(new CassandraTableResource(tableName, default, new CassandraSchema(new List<Column> { new Column { Name = "columnA", Type = "int" }, new Column { Name = "columnB", Type = "ascii" } }, new List<CassandraPartitionKey> { new CassandraPartitionKey { Name = "columnA" } }, new List<ClusterKey> { new ClusterKey { Name = "columnB", OrderBy = "Asc" } }), default), new CreateUpdateOptions());
bquantump marked this conversation as resolved.
Show resolved Hide resolved

Response<CassandraTableGetResults> cassandraResponse = await WaitForCompletionAsync(await cosmosDBClient.CassandraResources.StartCreateUpdateCassandraTableAsync(resourceGroupName, databaseAccountName, keyspaceName, tableName, cassandraTableCreateUpdateParameters));
CassandraTableGetResults cassandraTableGetResults = cassandraResponse.Value;
Assert.NotNull(cassandraTableGetResults);

VerifyCassandraTableCreation(cassandraTableGetResults, cassandraTableCreateUpdateParameters);

AsyncPageable<CassandraTableGetResults> cassandraPageableResults = cosmosDBClient.CassandraResources.ListCassandraTablesAsync(resourceGroupName, databaseAccountName, keyspaceName);
Task<List<CassandraTableGetResults>> taskCassandraTables = cassandraPageableResults.ToEnumerableAsync();
List<CassandraTableGetResults> cassandraTables = taskCassandraTables.ConfigureAwait(false).GetAwaiter().GetResult();
bquantump marked this conversation as resolved.
Show resolved Hide resolved
Assert.NotNull(cassandraTables);

foreach (CassandraTableGetResults cassandraTable in cassandraTables)
nisha-bhatia marked this conversation as resolved.
Show resolved Hide resolved
{
await cosmosDBClient.CassandraResources.StartDeleteCassandraTableAsync(resourceGroupName, databaseAccountName, keyspaceName, cassandraTable.Name);
}

foreach (CassandraKeyspaceGetResults cassandraKeyspace in cassandraKeyspaces)
{
await cosmosDBClient.CassandraResources.StartDeleteCassandraKeyspaceAsync(resourceGroupName, databaseAccountName, cassandraKeyspace.Name);
}
}

private void VerifyCassandraTableCreation(CassandraTableGetResults cassandraTableGetResults, CassandraTableCreateUpdateParameters cassandraTableCreateUpdateParameters)
{
Assert.AreEqual(cassandraTableGetResults.Resource.Id, cassandraTableCreateUpdateParameters.Resource.Id);
Assert.AreEqual(cassandraTableGetResults.Resource.Schema.Columns.Count, cassandraTableCreateUpdateParameters.Resource.Schema.Columns.Count);
for (int i = 0; i < cassandraTableGetResults.Resource.Schema.Columns.Count; i++)
{
Assert.AreEqual(cassandraTableGetResults.Resource.Schema.Columns[i].Name, cassandraTableCreateUpdateParameters.Resource.Schema.Columns[i].Name);
Assert.AreEqual(cassandraTableGetResults.Resource.Schema.Columns[i].Type, cassandraTableCreateUpdateParameters.Resource.Schema.Columns[i].Type);
}

Assert.AreEqual(cassandraTableGetResults.Resource.Schema.ClusterKeys.Count, cassandraTableCreateUpdateParameters.Resource.Schema.ClusterKeys.Count);
for (int i = 0; i < cassandraTableGetResults.Resource.Schema.ClusterKeys.Count; i++)
{
Assert.AreEqual(cassandraTableGetResults.Resource.Schema.ClusterKeys[i].Name, cassandraTableCreateUpdateParameters.Resource.Schema.ClusterKeys[i].Name);
}

Assert.AreEqual(cassandraTableGetResults.Resource.Schema.PartitionKeys.Count, cassandraTableCreateUpdateParameters.Resource.Schema.PartitionKeys.Count);
for (int i = 0; i < cassandraTableGetResults.Resource.Schema.PartitionKeys.Count; i++)
{
Assert.AreEqual(cassandraTableGetResults.Resource.Schema.PartitionKeys[i].Name, cassandraTableCreateUpdateParameters.Resource.Schema.PartitionKeys[i].Name);
}
}

private void VerifyEqualCassandraDatabases(CassandraKeyspaceGetResults expectedValue, CassandraKeyspaceGetResults actualValue)
{
Assert.AreEqual(expectedValue.Id, actualValue.Id);
Assert.AreEqual(expectedValue.Name, actualValue.Name);
Assert.AreEqual(expectedValue.Location, actualValue.Location);
Assert.AreEqual(expectedValue.Tags, actualValue.Tags);
Assert.AreEqual(expectedValue.Type, actualValue.Type);

Assert.AreEqual(expectedValue.Options, actualValue.Options);

Assert.AreEqual(expectedValue.Resource.Id, actualValue.Resource.Id);
Assert.AreEqual(expectedValue.Resource.Rid, actualValue.Resource.Rid);
Assert.AreEqual(expectedValue.Resource.Ts, actualValue.Resource.Ts);
Assert.AreEqual(expectedValue.Resource.Etag, actualValue.Resource.Etag);
}
}
}