Skip to content

Commit

Permalink
.NET smoke Test Sample (Azure#6652)
Browse files Browse the repository at this point in the history
* .NET smoke Test Sample

Performs functionalities with Key Vault, Identity, Event Hubs and Blob Storage Track 2 SDKs.

* Env Variables names changed

The names were changed to match the name convention for environment variables.

* Update .gitignore

* gitignore updated

A gitignore file was created in the SmokeTest folder, and now all launchSettings.json files are being ignored.

* Exit Codes, PascalCase and refactoring of the static methods.

Work based on the reviews from the PR Azure#6652. The public methods are now in PascalCase, the methods does not longer return English strings, instead they return booleans or Exceptions.  And Exit Codes handling was implemented.

* Class names changed

Class names were changed to match the following pattern: <service>Test. Example: BlobStorage -> BlobStorageTest.

* Comments

XML comments were added and some unnecesary comments were deleted.

* Higher order functions

Use of higher order functions to dry up the code.

* Test classes refactured

Drying up the code by using high order functions.

* Create CosmosDBTest.cs

* Cosmos DB implementation

Tests done with Mirosoft.Azure.DocumentDB SDK.

* Update CosmosDBTest.cs

* Update CosmosDBTest.cs

* Unnecessary blank lines removed

* ExecuteTest typo

* Base class deleted

The samples not longer need the "TestBase" class to run.

* BlobTestSource.tst deleted

This text file is not going to be needed any more since the test Blob is being created within the code.

* Static Classes

All classes are now static.

* Pair programming comments
  • Loading branch information
JonathanCrd authored and rileymckenna committed Jul 16, 2019
1 parent 72f77c5 commit fd5e5db
Show file tree
Hide file tree
Showing 11 changed files with 676 additions and 0 deletions.
1 change: 1 addition & 0 deletions NuGet.Config
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<add key="Local" value="tools/LocalNugetFeed" />
<add key="roslyn" value="https://dotnet.myget.org/F/roslyn-tools/api/v3/index.json" />
<add key="azure-sdk-tools" value="https://azuresdkartifacts.blob.core.windows.net/azure-sdk-tools/index.json" />
<add key="azure-sdk-for-net" value="https://azuresdkartifacts.blob.core.windows.net/azure-sdk-for-net/index.json" />
</packageSources>
<config>
<add key="globalPackagesFolder" value="restoredPackages" />
Expand Down
1 change: 1 addition & 0 deletions samples/SmokeTest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
launchSettings.json
29 changes: 29 additions & 0 deletions samples/SmokeTest/SmokeTest.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28922.388
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmokeTest", "SmokeTest\SmokeTest.csproj", "{F406BDFE-913D-4ED1-8C97-928128DA0F7D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F406BDFE-913D-4ED1-8C97-928128DA0F7D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F406BDFE-913D-4ED1-8C97-928128DA0F7D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F406BDFE-913D-4ED1-8C97-928128DA0F7D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F406BDFE-913D-4ED1-8C97-928128DA0F7D}.Release|Any CPU.Build.0 = Release|Any CPU
{89D7E7D6-0C87-4E12-87AC-B51EC63F7C22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{89D7E7D6-0C87-4E12-87AC-B51EC63F7C22}.Debug|Any CPU.Build.0 = Debug|Any CPU
{89D7E7D6-0C87-4E12-87AC-B51EC63F7C22}.Release|Any CPU.ActiveCfg = Release|Any CPU
{89D7E7D6-0C87-4E12-87AC-B51EC63F7C22}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6755A3D3-5AB2-487B-87FF-64D0E637E35A}
EndGlobalSection
EndGlobal
57 changes: 57 additions & 0 deletions samples/SmokeTest/SmokeTest/BlobStorageTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Azure.Storage.Blobs;
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;

namespace SmokeTest
{
class BlobStorageTest
{
private static BlobServiceClient serviceClient;
private static BlockBlobClient blobClient;

/// <summary>
/// Test the Storage Blobs SDK
/// </summary>
public static async Task RunTests()
{
Console.WriteLine("\n---------------------------------");
Console.WriteLine("STORAGE");
Console.WriteLine("---------------------------------");
Console.WriteLine("Functionalities to test: 2:");
Console.WriteLine("1.- Upload Blob Block");
Console.WriteLine("2.- Delete that Blob Block" + '\n');

string connectionString = Environment.GetEnvironmentVariable("BLOB_CONNECTION_STRING");
string containerName = "mycontainer"; //The container must exists, this sample is not creating it.
string blobName = "netSmokeTestBlob";
serviceClient = new BlobServiceClient(connectionString);
blobClient = serviceClient.GetBlobContainerClient(containerName).GetBlockBlobClient(blobName);

await UploadBlob();
await DeleteBlob();
}

private static async Task UploadBlob()
{
Console.Write("Uploading blob... ");

const string content = "This is the content for the sample blob";
byte[] byteArray = Encoding.UTF8.GetBytes(content);
MemoryStream stream = new MemoryStream(byteArray);
await blobClient.UploadAsync(stream);

Console.WriteLine("\tdone");
}

private static async Task DeleteBlob()
{
Console.Write("Deleting blob...");
await blobClient.DeleteAsync();
Console.WriteLine("\tdone");
}
}
}
168 changes: 168 additions & 0 deletions samples/SmokeTest/SmokeTest/CosmosDBTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
using Azure.Storage.Blobs.Models;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Reflection.Metadata;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;

namespace SmokeTest
{
public class Planet
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
public bool HasRings { get; set; }
public int Radius { get; set; }
public Moon[] Moons { get; set; }
public override string ToString()
{
return JsonConvert.SerializeObject(this);
}
}

public class Moon
{
public string Name { get; set; }
}

class CosmosDBTest
{
private static DocumentClient client;
private const string DataBaseName = "netSolarSystemDB";
private const string CollectionName = "PlanetsCollection";
private static List<Planet> planets = new List<Planet>();

/// <summary>
/// Test the Cosmos DB SDK by creating an example Database called {DataBaseName} and a PlanetsCollection with planets on it.
/// </summary>
public static async Task RunTests()
{
Console.WriteLine("\n---------------------------------");
Console.WriteLine("COSMOS DB");
Console.WriteLine("---------------------------------");
Console.WriteLine("Functionalities to test: 5:");
Console.WriteLine("1.- Create a Database");
Console.WriteLine("2.- Create a Collection in the DB");
Console.WriteLine("3.- Create 2 JSON Documents (Items) in the collection");
Console.WriteLine("4.- Excecute simple query to the collection");
Console.WriteLine("5.- Clean up the resource (Delete DB)\n");

string endpoint = Environment.GetEnvironmentVariable("COSMOS_URI");
string authKey = Environment.GetEnvironmentVariable("COSMOS_AUTH_KEY");
client = new DocumentClient(new Uri(endpoint), authKey);

//Delete the database to ensure that the test environment is clean.
try
{
await DeleteDatabase();
}
catch
{ }

await CreateDatabase();
await CreateCollection();
await CreateDocuments();
await ExecuteSimpleQuery();
await DeleteDatabase();
}

private static async Task CreateDatabase()
{
Console.Write("Creating Database '" + DataBaseName + "'...");
await client.CreateDatabaseIfNotExistsAsync(new Database { Id = DataBaseName });
Console.WriteLine("\tdone");
}

private static async Task CreateCollection()
{
Console.Write("Creating collection '" + CollectionName + "'...");
await client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(DataBaseName), new DocumentCollection { Id = CollectionName });
Console.WriteLine("\tdone");
}

private static async Task CreateDocuments()
{
planets.Add(new Planet
{
Id = "Earth",
HasRings = false,
Radius = 3959,
Moons = new Moon[]
{
new Moon
{
Name = "Moon"
}
}
});
planets.Add(new Planet
{
Id = "Mars",
HasRings = false,
Radius = 2106,
Moons = new Moon[]
{
new Moon
{
Name = "Phobos"
},
new Moon
{
Name = "Deimos"
}
}
});

//The items must NOT exists in the collection
foreach (Planet planet in planets)
{
Console.Write("Inserting '"+planet.Id+"' document...");
await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(DataBaseName, CollectionName), planet);
Console.WriteLine("\tdone");
}
}

/// <summary>
/// The query retrieve all planets, this is going to verify that planets match
/// </summary>
private static async Task ExecuteSimpleQuery(){
Console.Write("Querying... ");
IQueryable<Planet> planetarySqlQuery = client.CreateDocumentQuery<Planet>(UriFactory.CreateDocumentCollectionUri(DataBaseName, CollectionName), "SELECT * FROM c");

var planetsSet = new HashSet<string>(planets.Count);
foreach(Planet planet in planets)
{
planetsSet.Add(planet.ToString());
}

int i = 0;
foreach (Planet planet in planetarySqlQuery)
{
var serializedPlanet = planet.ToString();
if (planetsSet.Contains(serializedPlanet))
{
i++;
}
}

if(i != planets.Count)
{
throw new Exception("Error, values do not match.");
}
Console.WriteLine("\tdone");
}

private static async Task DeleteDatabase()
{
Console.Write("Cleaning up the resource...");
await client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(DataBaseName));
Console.WriteLine("\tdone");
}
}
}
116 changes: 116 additions & 0 deletions samples/SmokeTest/SmokeTest/EventHubsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using Azure;
using Azure.Messaging.EventHubs;
using Microsoft.Azure.Amqp.Framing;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Http;
using System.Reflection.Metadata.Ecma335;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SmokeTest
{
class EventHubsTest
{
private static EventHubClient client;
private static EventSender sender;
private static EventReceiver receiver;

/// <summary>
/// Test the Event Hubs SDK by sending and receiving events
/// </summary>
public static async Task RunTests()
{
Console.WriteLine("\n---------------------------------");
Console.WriteLine("EVENT HUBS");
Console.WriteLine("---------------------------------");
Console.WriteLine("Functionalities to test: 2:");
Console.WriteLine("1.- Send an Event batch");
Console.WriteLine("2.- Recieve those events\n");

var connectionString = Environment.GetEnvironmentVariable("EVENT_HUBS_CONNECTION_STRING");
client = new EventHubClient(connectionString);

await CreateSenderAndReceiver();
await SendAndReceiveEvents();
}

private static async Task CreateSenderAndReceiver()
{
Console.Write("Creating the Sender and Receivers... ");
var partition = (await client.GetPartitionIdsAsync()).First();
var senderOptions = new EventSenderOptions
{
PartitionId = partition
};
var receiverOptions = new EventReceiverOptions
{
BeginReceivingAt = EventPosition.NewEventsOnly
};
sender = client.CreateSender(senderOptions);
receiver = client.CreateReceiver(partition, receiverOptions);
Console.WriteLine("\tdone");
}

private static async Task SendAndReceiveEvents()
{
var eventBatch = new[]
{
new EventData(Encoding.UTF8.GetBytes("First event data")),
new EventData(Encoding.UTF8.GetBytes("Second event data")),
new EventData(Encoding.UTF8.GetBytes("Third event data"))
};
var index = 0;
var receivedEvents = new List<EventData>();

//Before sending any event, start the receiver
await receiver.ReceiveAsync(1, TimeSpan.Zero);

Console.Write("Ready to send a batch of " + eventBatch.Count().ToString() + " events... ");
await sender.SendAsync(eventBatch);
Console.Write("Sent\n");

Console.Write("Receiving events... ");
while ((receivedEvents.Count < eventBatch.Length) && (++index < 3))
{
receivedEvents.AddRange(await receiver.ReceiveAsync(eventBatch.Length + 10, TimeSpan.FromMilliseconds(25)));
}
index = 0;

//Check if at least one event was received in order to start validation
if (receivedEvents.Count == 0)
{
throw new Exception(String.Format("Error, No events received."));
}
Console.Write(receivedEvents.Count() + " events received.\n");

Console.WriteLine("Beginning validation...");
foreach (var receivedEvent in receivedEvents)
{
var receivedEventMessage = Encoding.UTF8.GetString(receivedEvent.Body.ToArray());
var sentEventMessage = Encoding.UTF8.GetString(eventBatch[index].Body.ToArray());

if (receivedEventMessage == sentEventMessage)
{
Console.WriteLine("\tEvent '" + receivedEventMessage + "' correctly validated.");
}
else
{
throw new Exception(String.Format("Error, Event: '" + receivedEventMessage + "' was not expected."));
}
index++;
}

if (index < eventBatch.Count())
{
throw new Exception(String.Format("Error, expecting " + eventBatch.Count().ToString() + " events, but only got " + index.ToString() + "."));
}

Console.WriteLine("done");
}
}
}
Loading

0 comments on commit fd5e5db

Please sign in to comment.