From ba7d3af41c825b0f8b17c689105b29c955cccde5 Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Thu, 20 Jun 2019 10:53:42 -0700 Subject: [PATCH 01/19] .NET smoke Test Sample Performs functionalities with Key Vault, Identity, Event Hubs and Blob Storage Track 2 SDKs. --- NuGet.Config | 1 + samples/SmokeTest/SmokeTest.sln | 25 ++++ samples/SmokeTest/SmokeTest/BlobStorage.cs | 56 ++++++++ samples/SmokeTest/SmokeTest/EventHubs.cs | 121 +++++++++++++++++ samples/SmokeTest/SmokeTest/KeyVaultTest.cs | 72 ++++++++++ samples/SmokeTest/SmokeTest/Program.cs | 19 +++ .../Properties/Resources.Designer.cs | 72 ++++++++++ .../SmokeTest/Properties/Resources.resx | 124 ++++++++++++++++++ .../SmokeTest/Resources/BlobTestSource.txt | 1 + samples/SmokeTest/SmokeTest/SmokeTest.csproj | 36 +++++ 10 files changed, 527 insertions(+) create mode 100644 samples/SmokeTest/SmokeTest.sln create mode 100644 samples/SmokeTest/SmokeTest/BlobStorage.cs create mode 100644 samples/SmokeTest/SmokeTest/EventHubs.cs create mode 100644 samples/SmokeTest/SmokeTest/KeyVaultTest.cs create mode 100644 samples/SmokeTest/SmokeTest/Program.cs create mode 100644 samples/SmokeTest/SmokeTest/Properties/Resources.Designer.cs create mode 100644 samples/SmokeTest/SmokeTest/Properties/Resources.resx create mode 100644 samples/SmokeTest/SmokeTest/Resources/BlobTestSource.txt create mode 100644 samples/SmokeTest/SmokeTest/SmokeTest.csproj diff --git a/NuGet.Config b/NuGet.Config index 7254bc10808fe..74f5935bb8363 100644 --- a/NuGet.Config +++ b/NuGet.Config @@ -5,6 +5,7 @@ + diff --git a/samples/SmokeTest/SmokeTest.sln b/samples/SmokeTest/SmokeTest.sln new file mode 100644 index 0000000000000..462078aaf15f4 --- /dev/null +++ b/samples/SmokeTest/SmokeTest.sln @@ -0,0 +1,25 @@ + +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 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {6755A3D3-5AB2-487B-87FF-64D0E637E35A} + EndGlobalSection +EndGlobal diff --git a/samples/SmokeTest/SmokeTest/BlobStorage.cs b/samples/SmokeTest/SmokeTest/BlobStorage.cs new file mode 100644 index 0000000000000..f7083c5985dfa --- /dev/null +++ b/samples/SmokeTest/SmokeTest/BlobStorage.cs @@ -0,0 +1,56 @@ +using Azure.Storage.Blobs; +using System; +using System.IO; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices.ComTypes; +using System.Threading.Tasks; + +namespace SmokeTest +{ + class BlobStorage + { + /*Create the Blob client with the connection string. + * The connection string is retreived from an envirmonet variable. + * The container name for this sample is 'mycontainer', and the Blob name 'SmokeTestBlob' + */ + + private static BlobServiceClient service = new BlobServiceClient(Environment.GetEnvironmentVariable("Blob_ConnectionString")); + private static BlockBlobClient blob = service.GetBlobContainerClient("mycontainer").GetBlockBlobClient("SmokeTestBlob"); + + public static async Task performFunctionalities() + { + 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'); + + //Upload a new Blob (txt file in /BlobFiles folder) + Console.Write("Uploading blob... "); + Console.Write(await UploadBlob() + '\n'); + + //Delete the Blob that was created + Console.Write("Deleting blob... "); + Console.Write(await DeleteBlob() + '\n'); + } + + private static async Task UploadBlob() + { + const string path = "./Resources/BlobTestSource.txt"; + + using (FileStream data = File.OpenRead(path)) + { + await blob.UploadAsync(data); + } + return "Blob created successfully"; + } + + private static async Task DeleteBlob() + { + await blob.DeleteAsync(); + return "Blob deleted successfully"; + } + + } +} diff --git a/samples/SmokeTest/SmokeTest/EventHubs.cs b/samples/SmokeTest/SmokeTest/EventHubs.cs new file mode 100644 index 0000000000000..00cd4954c7f1f --- /dev/null +++ b/samples/SmokeTest/SmokeTest/EventHubs.cs @@ -0,0 +1,121 @@ +using Azure; +using Azure.Messaging.EventHubs; +using Microsoft.Azure.Amqp.Framing; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Reflection.Metadata.Ecma335; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace SmokeTest +{ + class EventHubs + { + public static async Task performFunctionalities() + { + 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"); + + /* Create EventHub client. + * THe connection string is retreived from a Envirnment variable + */ + var client = new EventHubClient(Environment.GetEnvironmentVariable("EventHubs_ConnectionString")); + + Console.Write("Creating the Sender and Receivers... "); + var response = await CreateSenderAndReceiver(client); + Console.Write("Done\n"); + + Console.Write(await SendAndReceiveEvents(response.Item1, response.Item2) + '\n'); + + } + + private static async Task> CreateSenderAndReceiver(EventHubClient client) + { + var partition = (await client.GetPartitionIdsAsync()).First(); + var senderOptions = new EventSenderOptions + { + PartitionId = partition + }; + var receiverOptions = new EventReceiverOptions + { + BeginReceivingAt = EventPosition.NewEventsOnly + }; + + var sender = client.CreateSender(senderOptions); + var receiver = client.CreateReceiver(partition, receiverOptions); + + return new Tuple(sender, receiver); + } + + private static async Task SendAndReceiveEvents(EventSender sender, EventReceiver receiver) + { + + //Start the receiver + await receiver.ReceiveAsync(1, TimeSpan.Zero); + + //Create the event batch to send + 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")) + }; + + //Send events + Console.Write("Ready to send a batch of " + eventBatch.Count().ToString() + " events... "); + await sender.SendAsync(eventBatch); + Console.Write("Sent\n"); + + //Receive the events + var receivedEvents = new List(); + var index = 0; + + 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 + if(receivedEvents.Count == 0) + { + return "Error, No events received."; + } + Console.Write(receivedEvents.Count() + " events received.\n"); + + Console.WriteLine("Beggining 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 + { + return "Error, Event: '" + receivedEventMessage + "' was not expected."; + } + ++index; + } + + //Check if the number of events received match the number of events sent + if (index < eventBatch.Count()) + { + return "Error, expecting " + eventBatch.Count().ToString() + " events, but only got " + index.ToString() + "."; + } + + return "Success"; + } + + } +} diff --git a/samples/SmokeTest/SmokeTest/KeyVaultTest.cs b/samples/SmokeTest/SmokeTest/KeyVaultTest.cs new file mode 100644 index 0000000000000..923939ab9fe74 --- /dev/null +++ b/samples/SmokeTest/SmokeTest/KeyVaultTest.cs @@ -0,0 +1,72 @@ +using Azure.Identity; +using Azure.Security.KeyVault.Secrets; +using System; +using System.Threading.Tasks; + +namespace SmokeTest +{ + class KeyVaultTest + { + public static async Task performFunctionalities() + { + Console.WriteLine("\n---------------------------------"); + Console.WriteLine("KEY VAULT"); + Console.WriteLine("---------------------------------"); + Console.WriteLine("Functionalities to test: 3:"); + Console.WriteLine("1.- Set a Secret"); + Console.WriteLine("2.- Get that Secret"); + Console.WriteLine("3.- Delete that Secret (Clean up)"); + Console.WriteLine(""); + + /* + * Create the KeyVault Client. + * The credentials are stored in environment variables. + */ + + var tenantid = Environment.GetEnvironmentVariable("KeyVault_TenantID"); + var clientid = Environment.GetEnvironmentVariable("KeyVault_ClientID"); + var clientsecret = Environment.GetEnvironmentVariable("KeyVault_ClientSecret"); + var KeyVaultUri = Environment.GetEnvironmentVariable("KeyVaultUri"); + + var client = new SecretClient(new Uri(KeyVaultUri), new ClientSecretCredential(tenantid, clientid, clientsecret)); + + const string SecretName = "SmokeTestSecret"; + + //Create a new Secret + Console.Write("Set a secret... "); + Console.Write(await SetNewSecret(SecretName, client) + '\n'); + + //Retrieve the Secret previously created + Console.Write("Get that secret... "); + Console.Write(await GetSecret(SecretName, client) + '\n'); + + //Clean up the resource (Delte the secret that was created) + Console.Write("Cleaning up the resource... "); + Console.Write(await CleanUp(SecretName, client) + '\n'); + } + + private static async Task SetNewSecret(string secretName, SecretClient client) + { + var newSecret = new Secret(secretName, "Secret Succesfully created"); + + var result = await client.SetAsync(newSecret); + + return result.Value.Value; + } + + private static async Task GetSecret(string secretName, SecretClient client) + { + var secret = await client.GetAsync(secretName); + + return secret.Value.Value == "Secret Succesfully created"? "Secret succesfully retreived" : "Secret retreived, but not the one previously created: " + secret.Value.Value; + + } + + private static async Task CleanUp(string secretName, SecretClient client) + { + await client.DeleteAsync(secretName); + return "done"; + } + + } +} diff --git a/samples/SmokeTest/SmokeTest/Program.cs b/samples/SmokeTest/SmokeTest/Program.cs new file mode 100644 index 0000000000000..edc017aa2b7c4 --- /dev/null +++ b/samples/SmokeTest/SmokeTest/Program.cs @@ -0,0 +1,19 @@ +using Azure.Messaging.EventHubs; +using System; +using System.Threading.Tasks; + +namespace SmokeTest +{ + + class Program + { + static async Task Main(string[] args) + { + Console.WriteLine("SMOKE TEST FOR TRACK 2 LIBRARIES"); + await KeyVaultTest.performFunctionalities(); + await BlobStorage.performFunctionalities(); + await EventHubs.performFunctionalities(); + + } + } +} diff --git a/samples/SmokeTest/SmokeTest/Properties/Resources.Designer.cs b/samples/SmokeTest/SmokeTest/Properties/Resources.Designer.cs new file mode 100644 index 0000000000000..65e2c63599f5d --- /dev/null +++ b/samples/SmokeTest/SmokeTest/Properties/Resources.Designer.cs @@ -0,0 +1,72 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace SmokeTest.Properties { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("SmokeTest.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to This Blob was uploaded via the SDK.. + /// + internal static string BlobTestSource { + get { + return ResourceManager.GetString("BlobTestSource", resourceCulture); + } + } + } +} diff --git a/samples/SmokeTest/SmokeTest/Properties/Resources.resx b/samples/SmokeTest/SmokeTest/Properties/Resources.resx new file mode 100644 index 0000000000000..5054a09fbe69d --- /dev/null +++ b/samples/SmokeTest/SmokeTest/Properties/Resources.resx @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + + ..\Resources\BlobTestSource.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 + + \ No newline at end of file diff --git a/samples/SmokeTest/SmokeTest/Resources/BlobTestSource.txt b/samples/SmokeTest/SmokeTest/Resources/BlobTestSource.txt new file mode 100644 index 0000000000000..9b8be627a8a2d --- /dev/null +++ b/samples/SmokeTest/SmokeTest/Resources/BlobTestSource.txt @@ -0,0 +1 @@ +This Blob was uploaded via the SDK. \ No newline at end of file diff --git a/samples/SmokeTest/SmokeTest/SmokeTest.csproj b/samples/SmokeTest/SmokeTest/SmokeTest.csproj new file mode 100644 index 0000000000000..13e3cab0626a7 --- /dev/null +++ b/samples/SmokeTest/SmokeTest/SmokeTest.csproj @@ -0,0 +1,36 @@ + + + + Exe + netcoreapp2.1 + + + + + + + + + + + + True + True + Resources.resx + + + + + + ResXFileCodeGenerator + Resources.Designer.cs + + + + + + Always + + + + From 6dbfcbc460ea6b4c9bf04003305ad69a02d0cdf5 Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Fri, 21 Jun 2019 14:07:09 -0700 Subject: [PATCH 02/19] Env Variables names changed The names were changed to match the name convention for environment variables. --- samples/SmokeTest/SmokeTest/BlobStorage.cs | 2 +- samples/SmokeTest/SmokeTest/EventHubs.cs | 2 +- samples/SmokeTest/SmokeTest/KeyVaultTest.cs | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/samples/SmokeTest/SmokeTest/BlobStorage.cs b/samples/SmokeTest/SmokeTest/BlobStorage.cs index f7083c5985dfa..1c04d7ff59c11 100644 --- a/samples/SmokeTest/SmokeTest/BlobStorage.cs +++ b/samples/SmokeTest/SmokeTest/BlobStorage.cs @@ -14,7 +14,7 @@ class BlobStorage * The container name for this sample is 'mycontainer', and the Blob name 'SmokeTestBlob' */ - private static BlobServiceClient service = new BlobServiceClient(Environment.GetEnvironmentVariable("Blob_ConnectionString")); + private static BlobServiceClient service = new BlobServiceClient(Environment.GetEnvironmentVariable("BLOB_CONNECTION_STRING")); private static BlockBlobClient blob = service.GetBlobContainerClient("mycontainer").GetBlockBlobClient("SmokeTestBlob"); public static async Task performFunctionalities() diff --git a/samples/SmokeTest/SmokeTest/EventHubs.cs b/samples/SmokeTest/SmokeTest/EventHubs.cs index 00cd4954c7f1f..5bf66a9391c0a 100644 --- a/samples/SmokeTest/SmokeTest/EventHubs.cs +++ b/samples/SmokeTest/SmokeTest/EventHubs.cs @@ -26,7 +26,7 @@ public static async Task performFunctionalities() /* Create EventHub client. * THe connection string is retreived from a Envirnment variable */ - var client = new EventHubClient(Environment.GetEnvironmentVariable("EventHubs_ConnectionString")); + var client = new EventHubClient(Environment.GetEnvironmentVariable("EVENT_HUBS_CONNECTION_STRING")); Console.Write("Creating the Sender and Receivers... "); var response = await CreateSenderAndReceiver(client); diff --git a/samples/SmokeTest/SmokeTest/KeyVaultTest.cs b/samples/SmokeTest/SmokeTest/KeyVaultTest.cs index 923939ab9fe74..5f18f2d926b0f 100644 --- a/samples/SmokeTest/SmokeTest/KeyVaultTest.cs +++ b/samples/SmokeTest/SmokeTest/KeyVaultTest.cs @@ -23,10 +23,10 @@ public static async Task performFunctionalities() * The credentials are stored in environment variables. */ - var tenantid = Environment.GetEnvironmentVariable("KeyVault_TenantID"); - var clientid = Environment.GetEnvironmentVariable("KeyVault_ClientID"); - var clientsecret = Environment.GetEnvironmentVariable("KeyVault_ClientSecret"); - var KeyVaultUri = Environment.GetEnvironmentVariable("KeyVaultUri"); + var tenantid = Environment.GetEnvironmentVariable("DIR_TENANT_ID"); + var clientid = Environment.GetEnvironmentVariable("APP_CLIENT_ID"); + var clientsecret = Environment.GetEnvironmentVariable("CLIENT_SECRET"); + var KeyVaultUri = Environment.GetEnvironmentVariable("KEY_VAULT_URI"); var client = new SecretClient(new Uri(KeyVaultUri), new ClientSecretCredential(tenantid, clientid, clientsecret)); From 4ca3f03406f1f0ab01bf248a947aab50ff9b7697 Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Fri, 21 Jun 2019 14:07:22 -0700 Subject: [PATCH 03/19] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index f91c984938f21..4067122eb281c 100644 --- a/.gitignore +++ b/.gitignore @@ -150,3 +150,4 @@ pingme.txt # TS/Node files dist/ node_modules/ +samples/SmokeTest/SmokeTest/Properties/launchSettings.json From 386a55067d0fab6d6c5c5060e56a16eccb4a8461 Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Fri, 21 Jun 2019 16:07:36 -0700 Subject: [PATCH 04/19] gitignore updated A gitignore file was created in the SmokeTest folder, and now all launchSettings.json files are being ignored. --- .gitignore | 1 - samples/SmokeTest/.gitignore | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 samples/SmokeTest/.gitignore diff --git a/.gitignore b/.gitignore index 4067122eb281c..f91c984938f21 100644 --- a/.gitignore +++ b/.gitignore @@ -150,4 +150,3 @@ pingme.txt # TS/Node files dist/ node_modules/ -samples/SmokeTest/SmokeTest/Properties/launchSettings.json diff --git a/samples/SmokeTest/.gitignore b/samples/SmokeTest/.gitignore new file mode 100644 index 0000000000000..8392c905c61ea --- /dev/null +++ b/samples/SmokeTest/.gitignore @@ -0,0 +1 @@ +launchSettings.json \ No newline at end of file From a6950ac1070920ef3103b86e6b894416b28c966e Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Mon, 24 Jun 2019 16:37:56 -0700 Subject: [PATCH 05/19] Exit Codes, PascalCase and refactoring of the static methods. Work based on the reviews from the PR #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. --- samples/SmokeTest/SmokeTest/BlobStorage.cs | 73 ++++++-- samples/SmokeTest/SmokeTest/EventHubs.cs | 169 ++++++++++++------- samples/SmokeTest/SmokeTest/KeyVaultTest.cs | 132 +++++++++++---- samples/SmokeTest/SmokeTest/Program.cs | 25 ++- samples/SmokeTest/SmokeTest/SmokeTest.csproj | 8 +- 5 files changed, 288 insertions(+), 119 deletions(-) diff --git a/samples/SmokeTest/SmokeTest/BlobStorage.cs b/samples/SmokeTest/SmokeTest/BlobStorage.cs index 1c04d7ff59c11..141cb83a6e77c 100644 --- a/samples/SmokeTest/SmokeTest/BlobStorage.cs +++ b/samples/SmokeTest/SmokeTest/BlobStorage.cs @@ -7,17 +7,21 @@ namespace SmokeTest { + + class BlobStorage { - /*Create the Blob client with the connection string. - * The connection string is retreived from an envirmonet variable. - * The container name for this sample is 'mycontainer', and the Blob name 'SmokeTestBlob' - */ + + private BlobServiceClient service; + private BlockBlobClient blob; - private static BlobServiceClient service = new BlobServiceClient(Environment.GetEnvironmentVariable("BLOB_CONNECTION_STRING")); - private static BlockBlobClient blob = service.GetBlobContainerClient("mycontainer").GetBlockBlobClient("SmokeTestBlob"); + public BlobStorage(string connectionString,string containerName, string blobName) + { + this.service = new BlobServiceClient(connectionString); + this.blob = service.GetBlobContainerClient(containerName).GetBlockBlobClient(blobName); + } - public static async Task performFunctionalities() + public async Task PerformFunctionalities() { Console.WriteLine("\n---------------------------------"); Console.WriteLine("STORAGE"); @@ -28,28 +32,67 @@ public static async Task performFunctionalities() //Upload a new Blob (txt file in /BlobFiles folder) Console.Write("Uploading blob... "); - Console.Write(await UploadBlob() + '\n'); + var result1 = await UploadBlob(); + if(result1 != null) + { + //If this test failed, then the othe one is going to fail too. + Console.Error.Write("FAILED.\n"); + Console.Error.WriteLine(result1); + Console.Error.WriteLine("Cannot delete the Blob."); + return false; + + } + else + { + Console.Write("Blob uploaded successfully\n"); + } //Delete the Blob that was created Console.Write("Deleting blob... "); - Console.Write(await DeleteBlob() + '\n'); + var result2 = await DeleteBlob(); + if (result2 != null) + { + Console.Error.Write("FAILED.\n"); + Console.Error.WriteLine(result2); + return false; + } + else + { + Console.Write("Blob deleted successfully\n"); + } + + return true; } - private static async Task UploadBlob() + private async Task UploadBlob() { const string path = "./Resources/BlobTestSource.txt"; using (FileStream data = File.OpenRead(path)) { - await blob.UploadAsync(data); + try + { + await blob.UploadAsync(data); + } + catch (Exception ex) + { + return ex; + } } - return "Blob created successfully"; + return null; } - private static async Task DeleteBlob() + private async Task DeleteBlob() { - await blob.DeleteAsync(); - return "Blob deleted successfully"; + try + { + await blob.DeleteAsync(); + } + catch (Exception ex) + { + return ex; + } + return null; } } diff --git a/samples/SmokeTest/SmokeTest/EventHubs.cs b/samples/SmokeTest/SmokeTest/EventHubs.cs index 5bf66a9391c0a..4f13b1db5c9ce 100644 --- a/samples/SmokeTest/SmokeTest/EventHubs.cs +++ b/samples/SmokeTest/SmokeTest/EventHubs.cs @@ -3,9 +3,11 @@ 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; @@ -14,7 +16,16 @@ namespace SmokeTest { class EventHubs { - public static async Task performFunctionalities() + private EventHubClient client; + private EventSender sender; + private EventReceiver receiver; + + public EventHubs(string connectionString) + { + this.client = new EventHubClient(connectionString); + } + + public async Task PerformFunctionalities() { Console.WriteLine("\n---------------------------------"); Console.WriteLine("EVENT HUBS"); @@ -23,98 +34,130 @@ public static async Task performFunctionalities() Console.WriteLine("1.- Send an Event batch"); Console.WriteLine("2.- Recieve those events\n"); - /* Create EventHub client. - * THe connection string is retreived from a Envirnment variable - */ - var client = new EventHubClient(Environment.GetEnvironmentVariable("EVENT_HUBS_CONNECTION_STRING")); - Console.Write("Creating the Sender and Receivers... "); - var response = await CreateSenderAndReceiver(client); - Console.Write("Done\n"); + var result1 = await CreateSenderAndReceiver(); + if(result1 != null) + { + //If this test fail, the next one is going to fail too. + Console.Error.Write("FAILED\n"); + Console.Error.WriteLine(result1); + Console.Error.WriteLine("Cannot send or receive events."); + return false; + } + else + { + Console.Write("Done\n"); + } - Console.Write(await SendAndReceiveEvents(response.Item1, response.Item2) + '\n'); + var result2 = await SendAndReceiveEvents(); + if(result2 != null) + { + Console.Error.Write("FAILED\n"); + Console.Error.WriteLine(result2); + return false; + } + else + { + Console.WriteLine("Success."); + } + return true; } - private static async Task> CreateSenderAndReceiver(EventHubClient client) + private async Task CreateSenderAndReceiver() { - var partition = (await client.GetPartitionIdsAsync()).First(); - var senderOptions = new EventSenderOptions - { - PartitionId = partition - }; - var receiverOptions = new EventReceiverOptions + try { - BeginReceivingAt = EventPosition.NewEventsOnly - }; + var partition = (await client.GetPartitionIdsAsync()).First(); + var senderOptions = new EventSenderOptions + { + PartitionId = partition + }; + var receiverOptions = new EventReceiverOptions + { + BeginReceivingAt = EventPosition.NewEventsOnly + }; - var sender = client.CreateSender(senderOptions); - var receiver = client.CreateReceiver(partition, receiverOptions); + sender = client.CreateSender(senderOptions); + receiver = client.CreateReceiver(partition, receiverOptions); + } + catch (Exception ex) + { + return ex; + } - return new Tuple(sender, receiver); + return null; } - private static async Task SendAndReceiveEvents(EventSender sender, EventReceiver receiver) + private async Task SendAndReceiveEvents() { + try + { + //Start the receiver + await receiver.ReceiveAsync(1, TimeSpan.Zero); - //Start the receiver - await receiver.ReceiveAsync(1, TimeSpan.Zero); - - //Create the event batch to send - var eventBatch = new[] - { + //Create the event batch to send + 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")) }; - //Send events - Console.Write("Ready to send a batch of " + eventBatch.Count().ToString() + " events... "); - await sender.SendAsync(eventBatch); - Console.Write("Sent\n"); + //Send events + Console.Write("Ready to send a batch of " + eventBatch.Count().ToString() + " events... "); + await sender.SendAsync(eventBatch); + Console.Write("Sent\n"); - //Receive the events - var receivedEvents = new List(); - var index = 0; + //Receive the events + var receivedEvents = new List(); - Console.Write("Receiving events... "); - while ((receivedEvents.Count < eventBatch.Length) && (++index < 3)) - { - receivedEvents.AddRange(await receiver.ReceiveAsync(eventBatch.Length + 10, TimeSpan.FromMilliseconds(25))); - } - index = 0; + //Why? + var index = 0; - //Check if at least one event was received - if(receivedEvents.Count == 0) - { - return "Error, No events received."; - } - Console.Write(receivedEvents.Count() + " events received.\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; - Console.WriteLine("Beggining validation..."); - foreach (var receivedEvent in receivedEvents) - { - var receivedEventMessage = Encoding.UTF8.GetString(receivedEvent.Body.ToArray()); - var sentEventMessage = Encoding.UTF8.GetString(eventBatch[index].Body.ToArray()); + //Check if at least one event was received + if (receivedEvents.Count == 0) + { + throw new Exception(String.Format("Error, No events received.")); + } + Console.Write(receivedEvents.Count() + " events received.\n"); - if (receivedEventMessage == sentEventMessage) + Console.WriteLine("Beggining validation..."); + foreach (var receivedEvent in receivedEvents) { - Console.WriteLine("\tEvent '" + receivedEventMessage + "' correctly validated."); + 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++; } - else + + //Check if the number of events received match the number of events sent + if (index < eventBatch.Count()) { - return "Error, Event: '" + receivedEventMessage + "' was not expected."; + throw new Exception(String.Format("Error, expecting " + eventBatch.Count().ToString() + " events, but only got " + index.ToString() + ".")); } - ++index; } - - //Check if the number of events received match the number of events sent - if (index < eventBatch.Count()) + catch (Exception ex) { - return "Error, expecting " + eventBatch.Count().ToString() + " events, but only got " + index.ToString() + "."; + return ex; } - - return "Success"; + + return null; } } diff --git a/samples/SmokeTest/SmokeTest/KeyVaultTest.cs b/samples/SmokeTest/SmokeTest/KeyVaultTest.cs index 5f18f2d926b0f..23d4f9e83ba78 100644 --- a/samples/SmokeTest/SmokeTest/KeyVaultTest.cs +++ b/samples/SmokeTest/SmokeTest/KeyVaultTest.cs @@ -7,7 +7,18 @@ namespace SmokeTest { class KeyVaultTest { - public static async Task performFunctionalities() + private string secretName; + private string secretValue; + private SecretClient client; + + public KeyVaultTest(string secretName, string secretValue, string tenantid, string clientid, string clientsecret, string KeyVaultUri) + { + this.secretName = secretName; + this.secretValue = secretValue; + this.client = new SecretClient(new Uri(KeyVaultUri), new ClientSecretCredential(tenantid, clientid, clientsecret)); + } + + public async Task PerformFunctionalities() { Console.WriteLine("\n---------------------------------"); Console.WriteLine("KEY VAULT"); @@ -17,55 +28,114 @@ public static async Task performFunctionalities() Console.WriteLine("2.- Get that Secret"); Console.WriteLine("3.- Delete that Secret (Clean up)"); Console.WriteLine(""); - - /* - * Create the KeyVault Client. - * The credentials are stored in environment variables. - */ - - var tenantid = Environment.GetEnvironmentVariable("DIR_TENANT_ID"); - var clientid = Environment.GetEnvironmentVariable("APP_CLIENT_ID"); - var clientsecret = Environment.GetEnvironmentVariable("CLIENT_SECRET"); - var KeyVaultUri = Environment.GetEnvironmentVariable("KEY_VAULT_URI"); - - var client = new SecretClient(new Uri(KeyVaultUri), new ClientSecretCredential(tenantid, clientid, clientsecret)); - - const string SecretName = "SmokeTestSecret"; + + var testPassed = true; //Create a new Secret Console.Write("Set a secret... "); - Console.Write(await SetNewSecret(SecretName, client) + '\n'); + var result1 = await SetNewSecret(); + if (result1 != null) + { + //If this test failes, the other ones are going to fail too. + Console.Error.Write("FAILED.\n"); + Console.Error.WriteLine(result1); + Console.Error.WriteLine("Cannot get a secret and delete it."); + + return false; + } + else + { + Console.Error.Write("Secret created succesfully.\n"); + } //Retrieve the Secret previously created Console.Write("Get that secret... "); - Console.Write(await GetSecret(SecretName, client) + '\n'); + var result2 = await GetSecret(); + if (result2 != null) + { + Console.Error.Write("FAILED.\n"); + Console.Error.WriteLine(result2); - //Clean up the resource (Delte the secret that was created) + testPassed = false; + } + else + { + Console.WriteLine("Secret succesfully retreived."); + } + + //Clean up the resource (Delete the secret that was created) Console.Write("Cleaning up the resource... "); - Console.Write(await CleanUp(SecretName, client) + '\n'); + var result3 = await CleanUp(); + if (result3 != null) + { + Console.Error.Write("FAILED.\n"); + Console.Error.WriteLine(result3); + + testPassed = false; + } + else + { + Console.WriteLine("done."); + } + + return testPassed; } - private static async Task SetNewSecret(string secretName, SecretClient client) + private async Task SetNewSecret() { - var newSecret = new Secret(secretName, "Secret Succesfully created"); + try + { + var newSecret = new Secret(secretName, secretValue); + var result = await client.SetAsync(newSecret); + } + catch (Exception ex) + { + return ex; + } + + return null; + + //return result.Value.Value; - var result = await client.SetAsync(newSecret); - - return result.Value.Value; } - private static async Task GetSecret(string secretName, SecretClient client) + private async Task GetSecret() { - var secret = await client.GetAsync(secretName); + Azure.Response secret; + + try + { + secret = await client.GetAsync(secretName); + } + catch (Exception ex) + { + return ex; + } + + //Verify that the secret received is the one that was sent previously + if (secret.Value.Value == secretValue) + { + return null; + } + else + { + return new Exception(String.Format("Secret retreived, but not the one previously created: '" + secret.Value.Value)); + } - return secret.Value.Value == "Secret Succesfully created"? "Secret succesfully retreived" : "Secret retreived, but not the one previously created: " + secret.Value.Value; - } - private static async Task CleanUp(string secretName, SecretClient client) + private async Task CleanUp() { - await client.DeleteAsync(secretName); - return "done"; + try + { + await client.DeleteAsync(secretName); + } + catch (Exception ex) + { + return ex; + } + + return null; } } diff --git a/samples/SmokeTest/SmokeTest/Program.cs b/samples/SmokeTest/SmokeTest/Program.cs index edc017aa2b7c4..041b65ff85f39 100644 --- a/samples/SmokeTest/SmokeTest/Program.cs +++ b/samples/SmokeTest/SmokeTest/Program.cs @@ -1,5 +1,6 @@ using Azure.Messaging.EventHubs; using System; +using System.Reflection.Metadata; using System.Threading.Tasks; namespace SmokeTest @@ -7,13 +8,25 @@ namespace SmokeTest class Program { - static async Task Main(string[] args) - { + static async Task Main(string[] args) + { Console.WriteLine("SMOKE TEST FOR TRACK 2 LIBRARIES"); - await KeyVaultTest.performFunctionalities(); - await BlobStorage.performFunctionalities(); - await EventHubs.performFunctionalities(); - + + var keyVault = new KeyVaultTest("SmokeTestSecret", "smokeTestValue", Environment.GetEnvironmentVariable("DIR_TENANT_ID"), Environment.GetEnvironmentVariable("APP_CLIENT_ID"), Environment.GetEnvironmentVariable("CLIENT_SECRET"), Environment.GetEnvironmentVariable("KEY_VAULT_URI")); + var kvResult = await keyVault.PerformFunctionalities(); + + var blobStorage = new BlobStorage(Environment.GetEnvironmentVariable("BLOB_CONNECTION_STRING"), "mycontainer", "netSmokeTestBlob"); + var bsResult = await blobStorage.PerformFunctionalities(); + + var eventHubs = new EventHubs(Environment.GetEnvironmentVariable("EVENT_HUBS_CONNECTION_STRING")); + var ehResult = await eventHubs.PerformFunctionalities(); + + if (!kvResult || !bsResult || !ehResult) + { + return 1; + } + + return 0; } } } diff --git a/samples/SmokeTest/SmokeTest/SmokeTest.csproj b/samples/SmokeTest/SmokeTest/SmokeTest.csproj index 13e3cab0626a7..7c093de668190 100644 --- a/samples/SmokeTest/SmokeTest/SmokeTest.csproj +++ b/samples/SmokeTest/SmokeTest/SmokeTest.csproj @@ -6,10 +6,10 @@ - - - - + + + + From 87fdf6088a5f828cb5685561eaab1f714829a726 Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Mon, 24 Jun 2019 16:41:45 -0700 Subject: [PATCH 06/19] Class names changed Class names were changed to match the following pattern: Test. Example: BlobStorage -> BlobStorageTest. --- .../SmokeTest/{BlobStorage.cs => BlobStorageTest.cs} | 4 ++-- .../SmokeTest/SmokeTest/{EventHubs.cs => EventHubsTest.cs} | 4 ++-- samples/SmokeTest/SmokeTest/Program.cs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) rename samples/SmokeTest/SmokeTest/{BlobStorage.cs => BlobStorageTest.cs} (95%) rename samples/SmokeTest/SmokeTest/{EventHubs.cs => EventHubsTest.cs} (98%) diff --git a/samples/SmokeTest/SmokeTest/BlobStorage.cs b/samples/SmokeTest/SmokeTest/BlobStorageTest.cs similarity index 95% rename from samples/SmokeTest/SmokeTest/BlobStorage.cs rename to samples/SmokeTest/SmokeTest/BlobStorageTest.cs index 141cb83a6e77c..440d667b80d82 100644 --- a/samples/SmokeTest/SmokeTest/BlobStorage.cs +++ b/samples/SmokeTest/SmokeTest/BlobStorageTest.cs @@ -9,13 +9,13 @@ namespace SmokeTest { - class BlobStorage + class BlobStorageTest { private BlobServiceClient service; private BlockBlobClient blob; - public BlobStorage(string connectionString,string containerName, string blobName) + public BlobStorageTest(string connectionString,string containerName, string blobName) { this.service = new BlobServiceClient(connectionString); this.blob = service.GetBlobContainerClient(containerName).GetBlockBlobClient(blobName); diff --git a/samples/SmokeTest/SmokeTest/EventHubs.cs b/samples/SmokeTest/SmokeTest/EventHubsTest.cs similarity index 98% rename from samples/SmokeTest/SmokeTest/EventHubs.cs rename to samples/SmokeTest/SmokeTest/EventHubsTest.cs index 4f13b1db5c9ce..62f14db3a8cbf 100644 --- a/samples/SmokeTest/SmokeTest/EventHubs.cs +++ b/samples/SmokeTest/SmokeTest/EventHubsTest.cs @@ -14,13 +14,13 @@ namespace SmokeTest { - class EventHubs + class EventHubsTest { private EventHubClient client; private EventSender sender; private EventReceiver receiver; - public EventHubs(string connectionString) + public EventHubsTest(string connectionString) { this.client = new EventHubClient(connectionString); } diff --git a/samples/SmokeTest/SmokeTest/Program.cs b/samples/SmokeTest/SmokeTest/Program.cs index 041b65ff85f39..05a6d493aefd2 100644 --- a/samples/SmokeTest/SmokeTest/Program.cs +++ b/samples/SmokeTest/SmokeTest/Program.cs @@ -15,10 +15,10 @@ static async Task Main(string[] args) var keyVault = new KeyVaultTest("SmokeTestSecret", "smokeTestValue", Environment.GetEnvironmentVariable("DIR_TENANT_ID"), Environment.GetEnvironmentVariable("APP_CLIENT_ID"), Environment.GetEnvironmentVariable("CLIENT_SECRET"), Environment.GetEnvironmentVariable("KEY_VAULT_URI")); var kvResult = await keyVault.PerformFunctionalities(); - var blobStorage = new BlobStorage(Environment.GetEnvironmentVariable("BLOB_CONNECTION_STRING"), "mycontainer", "netSmokeTestBlob"); + var blobStorage = new BlobStorageTest(Environment.GetEnvironmentVariable("BLOB_CONNECTION_STRING"), "mycontainer", "netSmokeTestBlob"); var bsResult = await blobStorage.PerformFunctionalities(); - var eventHubs = new EventHubs(Environment.GetEnvironmentVariable("EVENT_HUBS_CONNECTION_STRING")); + var eventHubs = new EventHubsTest(Environment.GetEnvironmentVariable("EVENT_HUBS_CONNECTION_STRING")); var ehResult = await eventHubs.PerformFunctionalities(); if (!kvResult || !bsResult || !ehResult) From 4717b6474b08941bbbc7aeacbe74e7fb8a47f116 Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Mon, 24 Jun 2019 17:13:05 -0700 Subject: [PATCH 07/19] Comments XML comments were added and some unnecesary comments were deleted. --- .../SmokeTest/SmokeTest/BlobStorageTest.cs | 7 +++--- samples/SmokeTest/SmokeTest/EventHubsTest.cs | 25 ++++++++----------- samples/SmokeTest/SmokeTest/KeyVaultTest.cs | 12 ++++----- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/samples/SmokeTest/SmokeTest/BlobStorageTest.cs b/samples/SmokeTest/SmokeTest/BlobStorageTest.cs index 440d667b80d82..116a21a5a9c64 100644 --- a/samples/SmokeTest/SmokeTest/BlobStorageTest.cs +++ b/samples/SmokeTest/SmokeTest/BlobStorageTest.cs @@ -20,7 +20,10 @@ public BlobStorageTest(string connectionString,string containerName, string blob this.service = new BlobServiceClient(connectionString); this.blob = service.GetBlobContainerClient(containerName).GetBlockBlobClient(blobName); } - + /// + /// Test the Storage Blobs SDK + /// + /// true if passes, false if fails public async Task PerformFunctionalities() { Console.WriteLine("\n---------------------------------"); @@ -30,7 +33,6 @@ public async Task PerformFunctionalities() Console.WriteLine("1.- Upload Blob Block"); Console.WriteLine("2.- Delete that Blob Block" + '\n'); - //Upload a new Blob (txt file in /BlobFiles folder) Console.Write("Uploading blob... "); var result1 = await UploadBlob(); if(result1 != null) @@ -47,7 +49,6 @@ public async Task PerformFunctionalities() Console.Write("Blob uploaded successfully\n"); } - //Delete the Blob that was created Console.Write("Deleting blob... "); var result2 = await DeleteBlob(); if (result2 != null) diff --git a/samples/SmokeTest/SmokeTest/EventHubsTest.cs b/samples/SmokeTest/SmokeTest/EventHubsTest.cs index 62f14db3a8cbf..8ae66f53cace0 100644 --- a/samples/SmokeTest/SmokeTest/EventHubsTest.cs +++ b/samples/SmokeTest/SmokeTest/EventHubsTest.cs @@ -25,6 +25,10 @@ public EventHubsTest(string connectionString) this.client = new EventHubClient(connectionString); } + /// + /// Test the Event Hubs SDK by sending and receiving events + /// + /// true if pases, false if fails public async Task PerformFunctionalities() { Console.WriteLine("\n---------------------------------"); @@ -93,28 +97,22 @@ private async Task SendAndReceiveEvents() { try { - //Start the receiver - await receiver.ReceiveAsync(1, TimeSpan.Zero); - - //Create the event batch to send 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(); + + //Before sending any event, start the receiver + await receiver.ReceiveAsync(1, TimeSpan.Zero); - //Send events Console.Write("Ready to send a batch of " + eventBatch.Count().ToString() + " events... "); await sender.SendAsync(eventBatch); Console.Write("Sent\n"); - - //Receive the events - var receivedEvents = new List(); - - //Why? - var index = 0; - + Console.Write("Receiving events... "); while ((receivedEvents.Count < eventBatch.Length) && (++index < 3)) { @@ -122,7 +120,7 @@ private async Task SendAndReceiveEvents() } index = 0; - //Check if at least one event was received + //Check if at least one event was received in roder to start validation if (receivedEvents.Count == 0) { throw new Exception(String.Format("Error, No events received.")); @@ -146,7 +144,6 @@ private async Task SendAndReceiveEvents() index++; } - //Check if the number of events received match the number of events sent if (index < eventBatch.Count()) { throw new Exception(String.Format("Error, expecting " + eventBatch.Count().ToString() + " events, but only got " + index.ToString() + ".")); diff --git a/samples/SmokeTest/SmokeTest/KeyVaultTest.cs b/samples/SmokeTest/SmokeTest/KeyVaultTest.cs index 23d4f9e83ba78..71d7b5e5c8022 100644 --- a/samples/SmokeTest/SmokeTest/KeyVaultTest.cs +++ b/samples/SmokeTest/SmokeTest/KeyVaultTest.cs @@ -18,6 +18,11 @@ public KeyVaultTest(string secretName, string secretValue, string tenantid, stri this.client = new SecretClient(new Uri(KeyVaultUri), new ClientSecretCredential(tenantid, clientid, clientsecret)); } + + /// + /// Validates the Key Vault SDK + /// + /// true if passes, false if fails public async Task PerformFunctionalities() { Console.WriteLine("\n---------------------------------"); @@ -31,7 +36,6 @@ public async Task PerformFunctionalities() var testPassed = true; - //Create a new Secret Console.Write("Set a secret... "); var result1 = await SetNewSecret(); if (result1 != null) @@ -48,7 +52,6 @@ public async Task PerformFunctionalities() Console.Error.Write("Secret created succesfully.\n"); } - //Retrieve the Secret previously created Console.Write("Get that secret... "); var result2 = await GetSecret(); if (result2 != null) @@ -63,7 +66,6 @@ public async Task PerformFunctionalities() Console.WriteLine("Secret succesfully retreived."); } - //Clean up the resource (Delete the secret that was created) Console.Write("Cleaning up the resource... "); var result3 = await CleanUp(); if (result3 != null) @@ -95,8 +97,6 @@ private async Task SetNewSecret() return null; - //return result.Value.Value; - } private async Task GetSecret() @@ -112,7 +112,7 @@ private async Task GetSecret() return ex; } - //Verify that the secret received is the one that was sent previously + //Verify that the secret received is the one that was set previously if (secret.Value.Value == secretValue) { return null; From 48a656b35166f57ef5198432a67d7345149ef079 Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Tue, 25 Jun 2019 16:52:24 -0700 Subject: [PATCH 08/19] Higher order functions Use of higher order functions to dry up the code. --- samples/SmokeTest/SmokeTest/KeyVaultTest.cs | 92 +++++---------------- samples/SmokeTest/SmokeTest/TestBase.cs | 27 ++++++ 2 files changed, 46 insertions(+), 73 deletions(-) create mode 100644 samples/SmokeTest/SmokeTest/TestBase.cs diff --git a/samples/SmokeTest/SmokeTest/KeyVaultTest.cs b/samples/SmokeTest/SmokeTest/KeyVaultTest.cs index 71d7b5e5c8022..5bf133700ce2e 100644 --- a/samples/SmokeTest/SmokeTest/KeyVaultTest.cs +++ b/samples/SmokeTest/SmokeTest/KeyVaultTest.cs @@ -5,7 +5,7 @@ namespace SmokeTest { - class KeyVaultTest + class KeyVaultTest : TestBase { private string secretName; private string secretValue; @@ -23,7 +23,7 @@ public KeyVaultTest(string secretName, string secretValue, string tenantid, stri /// Validates the Key Vault SDK /// /// true if passes, false if fails - public async Task PerformFunctionalities() + public async Task RunTests() { Console.WriteLine("\n---------------------------------"); Console.WriteLine("KEY VAULT"); @@ -31,111 +31,57 @@ public async Task PerformFunctionalities() Console.WriteLine("Functionalities to test: 3:"); Console.WriteLine("1.- Set a Secret"); Console.WriteLine("2.- Get that Secret"); - Console.WriteLine("3.- Delete that Secret (Clean up)"); - Console.WriteLine(""); + Console.WriteLine("3.- Delete that Secret (Clean up)\n"); var testPassed = true; Console.Write("Set a secret... "); - var result1 = await SetNewSecret(); - if (result1 != null) + var result1 = await ExcecuteTest(SetNewSecret); + if (!result1) { //If this test failes, the other ones are going to fail too. - Console.Error.Write("FAILED.\n"); - Console.Error.WriteLine(result1); - Console.Error.WriteLine("Cannot get a secret and delete it."); - + Console.WriteLine("Cannot get a secret and delete it."); return false; } - else - { - Console.Error.Write("Secret created succesfully.\n"); - } Console.Write("Get that secret... "); - var result2 = await GetSecret(); - if (result2 != null) + var result2 = await ExcecuteTest(GetSecret); + if (!result2) { - Console.Error.Write("FAILED.\n"); - Console.Error.WriteLine(result2); - testPassed = false; } - else - { - Console.WriteLine("Secret succesfully retreived."); - } Console.Write("Cleaning up the resource... "); - var result3 = await CleanUp(); - if (result3 != null) + var result3 = await ExcecuteTest(CleanUp); + if (!result3) { - Console.Error.Write("FAILED.\n"); - Console.Error.WriteLine(result3); - testPassed = false; } - else - { - Console.WriteLine("done."); - } return testPassed; } - private async Task SetNewSecret() + private async Task SetNewSecret() { - try - { - var newSecret = new Secret(secretName, secretValue); - var result = await client.SetAsync(newSecret); - } - catch (Exception ex) - { - return ex; - } - - return null; - + var newSecret = new Secret(secretName, secretValue); + await client.SetAsync(newSecret); } - private async Task GetSecret() + private async Task GetSecret() { Azure.Response secret; - - try - { - secret = await client.GetAsync(secretName); - } - catch (Exception ex) - { - return ex; - } + secret = await client.GetAsync(secretName); //Verify that the secret received is the one that was set previously - if (secret.Value.Value == secretValue) - { - return null; - } - else + if (secret.Value.Value != secretValue) { - return new Exception(String.Format("Secret retreived, but not the one previously created: '" + secret.Value.Value)); + throw new Exception(String.Format("Secret retreived, but not the one previously created: '" + secret.Value.Value)); } - } - private async Task CleanUp() + private async Task CleanUp() { - try - { - await client.DeleteAsync(secretName); - } - catch (Exception ex) - { - return ex; - } - - return null; + await client.DeleteAsync(secretName); } } diff --git a/samples/SmokeTest/SmokeTest/TestBase.cs b/samples/SmokeTest/SmokeTest/TestBase.cs new file mode 100644 index 0000000000000..182a4cc5e6b5c --- /dev/null +++ b/samples/SmokeTest/SmokeTest/TestBase.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace SmokeTest +{ + abstract class TestBase + { + protected async Task ExcecuteTest(Func testAction) + { + try + { + await testAction(); + } + catch (Exception ex) + { + Console.WriteLine("FAILED."); + Console.WriteLine(ex); + return false; + } + + Console.WriteLine("DONE."); + return true; + } + } +} From b871ca1df01c665ce3658a228e204624015523c1 Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Tue, 25 Jun 2019 17:28:52 -0700 Subject: [PATCH 09/19] Test classes refactured Drying up the code by using high order functions. --- .../SmokeTest/SmokeTest/BlobStorageTest.cs | 52 ++---- samples/SmokeTest/SmokeTest/EventHubsTest.cs | 148 +++++++----------- samples/SmokeTest/SmokeTest/Program.cs | 11 +- samples/SmokeTest/SmokeTest/SmokeTest.csproj | 2 + samples/SmokeTest/SmokeTest/TestBase.cs | 7 +- 5 files changed, 86 insertions(+), 134 deletions(-) diff --git a/samples/SmokeTest/SmokeTest/BlobStorageTest.cs b/samples/SmokeTest/SmokeTest/BlobStorageTest.cs index 116a21a5a9c64..c7a369ca11de9 100644 --- a/samples/SmokeTest/SmokeTest/BlobStorageTest.cs +++ b/samples/SmokeTest/SmokeTest/BlobStorageTest.cs @@ -9,7 +9,7 @@ namespace SmokeTest { - class BlobStorageTest + class BlobStorageTest : TestBase { private BlobServiceClient service; @@ -20,11 +20,12 @@ public BlobStorageTest(string connectionString,string containerName, string blob this.service = new BlobServiceClient(connectionString); this.blob = service.GetBlobContainerClient(containerName).GetBlockBlobClient(blobName); } + /// /// Test the Storage Blobs SDK /// /// true if passes, false if fails - public async Task PerformFunctionalities() + public async Task RunTests() { Console.WriteLine("\n---------------------------------"); Console.WriteLine("STORAGE"); @@ -34,66 +35,37 @@ public async Task PerformFunctionalities() Console.WriteLine("2.- Delete that Blob Block" + '\n'); Console.Write("Uploading blob... "); - var result1 = await UploadBlob(); - if(result1 != null) + var result1 = await ExcecuteTest(UploadBlob); + if (!result1) { //If this test failed, then the othe one is going to fail too. - Console.Error.Write("FAILED.\n"); - Console.Error.WriteLine(result1); - Console.Error.WriteLine("Cannot delete the Blob."); + Console.WriteLine("Cannot delete the Blob."); return false; - - } - else - { - Console.Write("Blob uploaded successfully\n"); } Console.Write("Deleting blob... "); - var result2 = await DeleteBlob(); - if (result2 != null) + var result2 = await ExcecuteTest(DeleteBlob); + if (!result2) { - Console.Error.Write("FAILED.\n"); - Console.Error.WriteLine(result2); return false; } - else - { - Console.Write("Blob deleted successfully\n"); - } return true; } - private async Task UploadBlob() + private async Task UploadBlob() { const string path = "./Resources/BlobTestSource.txt"; using (FileStream data = File.OpenRead(path)) { - try - { - await blob.UploadAsync(data); - } - catch (Exception ex) - { - return ex; - } + await blob.UploadAsync(data); } - return null; } - private async Task DeleteBlob() + private async Task DeleteBlob() { - try - { - await blob.DeleteAsync(); - } - catch (Exception ex) - { - return ex; - } - return null; + await blob.DeleteAsync(); } } diff --git a/samples/SmokeTest/SmokeTest/EventHubsTest.cs b/samples/SmokeTest/SmokeTest/EventHubsTest.cs index 8ae66f53cace0..48f99c6764029 100644 --- a/samples/SmokeTest/SmokeTest/EventHubsTest.cs +++ b/samples/SmokeTest/SmokeTest/EventHubsTest.cs @@ -14,7 +14,7 @@ namespace SmokeTest { - class EventHubsTest + class EventHubsTest : TestBase { private EventHubClient client; private EventSender sender; @@ -29,7 +29,7 @@ public EventHubsTest(string connectionString) /// Test the Event Hubs SDK by sending and receiving events /// /// true if pases, false if fails - public async Task PerformFunctionalities() + public async Task RunTests() { Console.WriteLine("\n---------------------------------"); Console.WriteLine("EVENT HUBS"); @@ -39,122 +39,92 @@ public async Task PerformFunctionalities() Console.WriteLine("2.- Recieve those events\n"); Console.Write("Creating the Sender and Receivers... "); - var result1 = await CreateSenderAndReceiver(); - if(result1 != null) + var result1 = await ExcecuteTest(CreateSenderAndReceiver); + if(!result1) { //If this test fail, the next one is going to fail too. - Console.Error.Write("FAILED\n"); - Console.Error.WriteLine(result1); - Console.Error.WriteLine("Cannot send or receive events."); + Console.WriteLine("Cannot send or receive events."); return false; } - else - { - Console.Write("Done\n"); - } - var result2 = await SendAndReceiveEvents(); - if(result2 != null) + var result2 = await ExcecuteTest(SendAndReceiveEvents); + if(!result2) { - Console.Error.Write("FAILED\n"); - Console.Error.WriteLine(result2); return false; } - else - { - Console.WriteLine("Success."); - } return true; } - private async Task CreateSenderAndReceiver() + private async Task CreateSenderAndReceiver() { - try + var partition = (await client.GetPartitionIdsAsync()).First(); + var senderOptions = new EventSenderOptions { - 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); - } - catch (Exception ex) + PartitionId = partition + }; + var receiverOptions = new EventReceiverOptions { - return ex; - } + BeginReceivingAt = EventPosition.NewEventsOnly + }; - return null; + sender = client.CreateSender(senderOptions); + receiver = client.CreateReceiver(partition, receiverOptions); } - private async Task SendAndReceiveEvents() + private async Task SendAndReceiveEvents() { - try - { - 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(); - - //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)) + var eventBatch = new[] { - receivedEvents.AddRange(await receiver.ReceiveAsync(eventBatch.Length + 10, TimeSpan.FromMilliseconds(25))); - } - index = 0; + 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(); + + //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 roder to start validation - if (receivedEvents.Count == 0) - { - throw new Exception(String.Format("Error, No events received.")); - } - Console.Write(receivedEvents.Count() + " events received.\n"); + //Check if at least one event was received in roder 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("Beggining validation..."); + foreach (var receivedEvent in receivedEvents) + { + var receivedEventMessage = Encoding.UTF8.GetString(receivedEvent.Body.ToArray()); + var sentEventMessage = Encoding.UTF8.GetString(eventBatch[index].Body.ToArray()); - Console.WriteLine("Beggining validation..."); - foreach (var receivedEvent in receivedEvents) + if (receivedEventMessage == sentEventMessage) { - 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++; + Console.WriteLine("\tEvent '" + receivedEventMessage + "' correctly validated."); } - - if (index < eventBatch.Count()) + else { - throw new Exception(String.Format("Error, expecting " + eventBatch.Count().ToString() + " events, but only got " + index.ToString() + ".")); + throw new Exception(String.Format("Error, Event: '" + receivedEventMessage + "' was not expected.")); } + index++; } - catch (Exception ex) + + if (index < eventBatch.Count()) { - return ex; + throw new Exception(String.Format("Error, expecting " + eventBatch.Count().ToString() + " events, but only got " + index.ToString() + ".")); } - - return null; } } diff --git a/samples/SmokeTest/SmokeTest/Program.cs b/samples/SmokeTest/SmokeTest/Program.cs index 05a6d493aefd2..4e73240e01579 100644 --- a/samples/SmokeTest/SmokeTest/Program.cs +++ b/samples/SmokeTest/SmokeTest/Program.cs @@ -13,15 +13,18 @@ static async Task Main(string[] args) Console.WriteLine("SMOKE TEST FOR TRACK 2 LIBRARIES"); var keyVault = new KeyVaultTest("SmokeTestSecret", "smokeTestValue", Environment.GetEnvironmentVariable("DIR_TENANT_ID"), Environment.GetEnvironmentVariable("APP_CLIENT_ID"), Environment.GetEnvironmentVariable("CLIENT_SECRET"), Environment.GetEnvironmentVariable("KEY_VAULT_URI")); - var kvResult = await keyVault.PerformFunctionalities(); + var kvResult = await keyVault.RunTests(); var blobStorage = new BlobStorageTest(Environment.GetEnvironmentVariable("BLOB_CONNECTION_STRING"), "mycontainer", "netSmokeTestBlob"); - var bsResult = await blobStorage.PerformFunctionalities(); + var bsResult = await blobStorage.RunTests(); var eventHubs = new EventHubsTest(Environment.GetEnvironmentVariable("EVENT_HUBS_CONNECTION_STRING")); - var ehResult = await eventHubs.PerformFunctionalities(); + var ehResult = await eventHubs.RunTests(); - if (!kvResult || !bsResult || !ehResult) + var cosmosDB = new CosmosDBTest(Environment.GetEnvironmentVariable("COSMOS_URI"), Environment.GetEnvironmentVariable("COSMOS_AUTH_KEY")); + var cdbResult = await cosmosDB.RunTests(); + + if (!kvResult || !bsResult || !ehResult || !cdbResult) { return 1; } diff --git a/samples/SmokeTest/SmokeTest/SmokeTest.csproj b/samples/SmokeTest/SmokeTest/SmokeTest.csproj index 7c093de668190..8ec89ef5bccf1 100644 --- a/samples/SmokeTest/SmokeTest/SmokeTest.csproj +++ b/samples/SmokeTest/SmokeTest/SmokeTest.csproj @@ -10,6 +10,8 @@ + + diff --git a/samples/SmokeTest/SmokeTest/TestBase.cs b/samples/SmokeTest/SmokeTest/TestBase.cs index 182a4cc5e6b5c..3b3451659eb02 100644 --- a/samples/SmokeTest/SmokeTest/TestBase.cs +++ b/samples/SmokeTest/SmokeTest/TestBase.cs @@ -7,6 +7,11 @@ namespace SmokeTest { abstract class TestBase { + /// + /// High order function to handle the exceptions of each test. + /// + /// + /// protected async Task ExcecuteTest(Func testAction) { try @@ -20,7 +25,7 @@ protected async Task ExcecuteTest(Func testAction) return false; } - Console.WriteLine("DONE."); + Console.WriteLine("done"); return true; } } From 7139e22f38bbc6771fc94a8053b8fa48a26190df Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Tue, 25 Jun 2019 17:29:05 -0700 Subject: [PATCH 10/19] Create CosmosDBTest.cs --- samples/SmokeTest/SmokeTest/CosmosDBTest.cs | 146 ++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 samples/SmokeTest/SmokeTest/CosmosDBTest.cs diff --git a/samples/SmokeTest/SmokeTest/CosmosDBTest.cs b/samples/SmokeTest/SmokeTest/CosmosDBTest.cs new file mode 100644 index 0000000000000..ea99cee553038 --- /dev/null +++ b/samples/SmokeTest/SmokeTest/CosmosDBTest.cs @@ -0,0 +1,146 @@ +using Microsoft.Azure.Documents; +using Microsoft.Azure.Documents.Client; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +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 GassyPlanet { 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 DocumentClient client; + + public CosmosDBTest(string endpoint, string authKey) + { + client = new DocumentClient(new Uri(endpoint), authKey); + } + + public async Task RunTests() + { + Console.WriteLine("\n---------------------------------"); + Console.WriteLine("COSMOS DB"); + Console.WriteLine("(Microsoft.Azure.DocumentDB)"); + Console.WriteLine("---------------------------------"); + Console.WriteLine("Functionalities to test: #:"); + Console.WriteLine("1.- Create a Database\n"); + + Console.Write("Creating Database 'SolarSystemDB'... "); + var result1 = await CreateDatabase(); + if(result1 != null) + { + Console.Write("FAILED\n"); + Console.WriteLine(result1); + //If this test failed, the next ones are going to fail too + Console.WriteLine("Cannot create a Collection, Query the collection and neither Delete it."); + return false; + } + else + { + Console.Write("DB created.\n"); + } + + Console.Write("Creating collection 'PlanetsCollection' "); + + + return true; + } + + private async Task CreateDatabase() + { + try + { + await client.CreateDatabaseIfNotExistsAsync(new Database { Id = "SolarSystemDB" }); + } + catch (Exception ex) + { + return ex; + } + return null; + } + + private async Task CreateCollection() + { + try + { + await client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri("SolarSystemDB"), new DocumentCollection { Id = "PlanetsCollection" }); + } + catch (Exception ex) + { + return ex; + } + return null; + } + + private async Task CreateDocuments() + { + Planet planetEarth = new Planet + { + Id = "Earth", + GassyPlanet = false, + Radius = 3959, + Moons = new Moon[] + { + new Moon + { + Name = "Moon" + } + } + }; + + Planet planetMars = new Planet + { + Id = "Mars", + GassyPlanet = false, + Radius = 2106, + Moons = new Moon[] + { + new Moon + { + Name = "Phobos" + }, + new Moon + { + Name = "Deimos" + } + } + }; + + return null; + } + + private async Task ExecuteSimpleQuery(){ + try + { + + } + catch (Exception ex) + { + return ex; + } + return null; + } + } +} From 3fff1190bbb6a47da2b51a06955a8e21289f1557 Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Wed, 26 Jun 2019 16:08:26 -0700 Subject: [PATCH 11/19] Cosmos DB implementation Tests done with Mirosoft.Azure.DocumentDB SDK. --- samples/SmokeTest/SmokeTest.sln | 4 + samples/SmokeTest/SmokeTest/CosmosDBTest.cs | 119 ++++++++++++-------- 2 files changed, 76 insertions(+), 47 deletions(-) diff --git a/samples/SmokeTest/SmokeTest.sln b/samples/SmokeTest/SmokeTest.sln index 462078aaf15f4..2d6ea2d812daf 100644 --- a/samples/SmokeTest/SmokeTest.sln +++ b/samples/SmokeTest/SmokeTest.sln @@ -15,6 +15,10 @@ Global {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 diff --git a/samples/SmokeTest/SmokeTest/CosmosDBTest.cs b/samples/SmokeTest/SmokeTest/CosmosDBTest.cs index ea99cee553038..eb3ca59b32236 100644 --- a/samples/SmokeTest/SmokeTest/CosmosDBTest.cs +++ b/samples/SmokeTest/SmokeTest/CosmosDBTest.cs @@ -3,6 +3,7 @@ using Newtonsoft.Json; using System; using System.Collections.Generic; +using System.Linq; using System.Numerics; using System.Reflection.Metadata; using System.Runtime.ConstrainedExecution; @@ -15,7 +16,7 @@ public class Planet { [JsonProperty(PropertyName = "id")] public string Id { get; set; } - public bool GassyPlanet { get; set; } + public bool HasRings { get; set; } public int Radius { get; set; } public Moon[] Moons { get; set; } public override string ToString() @@ -29,77 +30,92 @@ public class Moon public string Name { get; set; } } - class CosmosDBTest + + class CosmosDBTest : TestBase { private DocumentClient client; - + private Planet planetEarth; + private Planet planetMars; + public CosmosDBTest(string endpoint, string authKey) { client = new DocumentClient(new Uri(endpoint), authKey); } + /// + /// Test the Cosmos DB SDK by creating an example Database called 'SolarSystemDB' and a PlanetsCollection with planets on it. + /// + /// public async Task RunTests() { Console.WriteLine("\n---------------------------------"); Console.WriteLine("COSMOS DB"); - Console.WriteLine("(Microsoft.Azure.DocumentDB)"); Console.WriteLine("---------------------------------"); Console.WriteLine("Functionalities to test: #:"); - Console.WriteLine("1.- Create a Database\n"); + 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 DB"); + Console.WriteLine("5.- Clean up the resource (Delete DB)\n"); + + var testPassed = true; Console.Write("Creating Database 'SolarSystemDB'... "); - var result1 = await CreateDatabase(); - if(result1 != null) + var result1 = await ExcecuteTest(CreateDatabase); + if(!result1) { - Console.Write("FAILED\n"); - Console.WriteLine(result1); //If this test failed, the next ones are going to fail too - Console.WriteLine("Cannot create a Collection, Query the collection and neither Delete it."); + Console.WriteLine("Cannot create a Collection, Documents, Query the collection and neither Delete the DB."); return false; } - else + + Console.Write("Creating collection 'PlanetsCollection' "); + var result2 = await ExcecuteTest(CreateCollection); + if (!result2) { - Console.Write("DB created.\n"); + testPassed = false; } - Console.Write("Creating collection 'PlanetsCollection' "); + Console.Write("Inserting 'Earth' and 'Mars' JSON Documents... "); + var result3 = await ExcecuteTest(CreateDocuments); + if (!result3) + { + testPassed = false; + } + Console.Write("Querying... "); + var result4 = await ExcecuteTest(ExecuteSimpleQuery); + if (!result4) + { + testPassed = false; + } + + Console.Write("Cleaning up the resource... "); + var result5 = await ExcecuteTest(DeleteDatabase); + if (!result5) + { + testPassed = false; + } - return true; + return testPassed; } - private async Task CreateDatabase() + private async Task CreateDatabase() { - try - { - await client.CreateDatabaseIfNotExistsAsync(new Database { Id = "SolarSystemDB" }); - } - catch (Exception ex) - { - return ex; - } - return null; + await client.CreateDatabaseIfNotExistsAsync(new Database { Id = "SolarSystemDB" }); } - private async Task CreateCollection() + private async Task CreateCollection() { - try - { - await client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri("SolarSystemDB"), new DocumentCollection { Id = "PlanetsCollection" }); - } - catch (Exception ex) - { - return ex; - } - return null; + await client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri("SolarSystemDB"), new DocumentCollection { Id = "PlanetsCollection" }); } - private async Task CreateDocuments() + private async Task CreateDocuments() { - Planet planetEarth = new Planet + planetEarth = new Planet { Id = "Earth", - GassyPlanet = false, + HasRings = false, Radius = 3959, Moons = new Moon[] { @@ -110,10 +126,10 @@ private async Task CreateDocuments() } }; - Planet planetMars = new Planet + planetMars = new Planet { Id = "Mars", - GassyPlanet = false, + HasRings = false, Radius = 2106, Moons = new Moon[] { @@ -128,19 +144,28 @@ private async Task CreateDocuments() } }; - return null; + await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri("SolarSystemDB", "PlanetsCollection"), planetEarth); + await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri("SolarSystemDB", "PlanetsCollection"), planetMars); + } - private async Task ExecuteSimpleQuery(){ - try - { + private async Task ExecuteSimpleQuery(){ + IQueryable planetarySqlQuery = client.CreateDocumentQuery(UriFactory.CreateDocumentCollectionUri("SolarSystemDB", "PlanetsCollection"), "SELECT * FROM c"); - } - catch (Exception ex) + foreach (Planet planet in planetarySqlQuery) { - return ex; + //The only 2 planets that were set before were Earth and Mars, if planet does not match any of those, then there is an error. + if (planet.ToString() != planetEarth.ToString() && planet.ToString() != planetMars.ToString()) + { + throw new Exception(String.Format("Error, the values does not match.\n")); + } } - return null; } + + private async Task DeleteDatabase() + { + await client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri("SolarSystemDB")); + } + } } From fd1df4d4247d12530559b2b35555d087411aba5b Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Wed, 26 Jun 2019 16:13:43 -0700 Subject: [PATCH 12/19] Update CosmosDBTest.cs --- samples/SmokeTest/SmokeTest/CosmosDBTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/SmokeTest/SmokeTest/CosmosDBTest.cs b/samples/SmokeTest/SmokeTest/CosmosDBTest.cs index eb3ca59b32236..e0104f1ba573b 100644 --- a/samples/SmokeTest/SmokeTest/CosmosDBTest.cs +++ b/samples/SmokeTest/SmokeTest/CosmosDBTest.cs @@ -51,11 +51,11 @@ public async Task RunTests() Console.WriteLine("\n---------------------------------"); Console.WriteLine("COSMOS DB"); Console.WriteLine("---------------------------------"); - Console.WriteLine("Functionalities to test: #:"); + 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 DB"); + Console.WriteLine("4.- Excecute simple query to the collection"); Console.WriteLine("5.- Clean up the resource (Delete DB)\n"); var testPassed = true; From 57903caa4777291d9a0eed985fc95a686301ba03 Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Wed, 26 Jun 2019 16:40:09 -0700 Subject: [PATCH 13/19] Update CosmosDBTest.cs --- samples/SmokeTest/SmokeTest/CosmosDBTest.cs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/samples/SmokeTest/SmokeTest/CosmosDBTest.cs b/samples/SmokeTest/SmokeTest/CosmosDBTest.cs index e0104f1ba573b..d6887e4c1b772 100644 --- a/samples/SmokeTest/SmokeTest/CosmosDBTest.cs +++ b/samples/SmokeTest/SmokeTest/CosmosDBTest.cs @@ -34,6 +34,9 @@ public class Moon class CosmosDBTest : TestBase { private DocumentClient client; + + private const string DataBaseName = "netSolarSystemDB"; + private const string CollectionName = "netPlanetsCollection"; private Planet planetEarth; private Planet planetMars; @@ -43,7 +46,7 @@ public CosmosDBTest(string endpoint, string authKey) } /// - /// Test the Cosmos DB SDK by creating an example Database called 'SolarSystemDB' and a PlanetsCollection with planets on it. + /// Test the Cosmos DB SDK by creating an example Database called {DataBaseName} and a PlanetsCollection with planets on it. /// /// public async Task RunTests() @@ -60,7 +63,7 @@ public async Task RunTests() var testPassed = true; - Console.Write("Creating Database 'SolarSystemDB'... "); + Console.Write("Creating Database '"+DataBaseName+"'... "); var result1 = await ExcecuteTest(CreateDatabase); if(!result1) { @@ -69,7 +72,7 @@ public async Task RunTests() return false; } - Console.Write("Creating collection 'PlanetsCollection' "); + Console.Write("Creating collection '"+CollectionName+"' "); var result2 = await ExcecuteTest(CreateCollection); if (!result2) { @@ -102,12 +105,12 @@ public async Task RunTests() private async Task CreateDatabase() { - await client.CreateDatabaseIfNotExistsAsync(new Database { Id = "SolarSystemDB" }); + await client.CreateDatabaseIfNotExistsAsync(new Database { Id = DataBaseName }); } private async Task CreateCollection() { - await client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri("SolarSystemDB"), new DocumentCollection { Id = "PlanetsCollection" }); + await client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(DataBaseName), new DocumentCollection { Id = CollectionName }); } private async Task CreateDocuments() @@ -144,13 +147,13 @@ private async Task CreateDocuments() } }; - await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri("SolarSystemDB", "PlanetsCollection"), planetEarth); - await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri("SolarSystemDB", "PlanetsCollection"), planetMars); + await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(DataBaseName, CollectionName), planetEarth); + await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(DataBaseName, CollectionName), planetMars); } private async Task ExecuteSimpleQuery(){ - IQueryable planetarySqlQuery = client.CreateDocumentQuery(UriFactory.CreateDocumentCollectionUri("SolarSystemDB", "PlanetsCollection"), "SELECT * FROM c"); + IQueryable planetarySqlQuery = client.CreateDocumentQuery(UriFactory.CreateDocumentCollectionUri(DataBaseName, CollectionName), "SELECT * FROM c"); foreach (Planet planet in planetarySqlQuery) { @@ -164,7 +167,7 @@ private async Task ExecuteSimpleQuery(){ private async Task DeleteDatabase() { - await client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri("SolarSystemDB")); + await client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(DataBaseName)); } } From ada179917cd5637896fcc76fd0a4f0cb4a5a2f9c Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Tue, 2 Jul 2019 16:26:50 -0700 Subject: [PATCH 14/19] Unnecessary blank lines removed --- samples/SmokeTest/SmokeTest/BlobStorageTest.cs | 6 +----- samples/SmokeTest/SmokeTest/CosmosDBTest.cs | 8 +------- samples/SmokeTest/SmokeTest/EventHubsTest.cs | 4 +--- samples/SmokeTest/SmokeTest/KeyVaultTest.cs | 5 +---- samples/SmokeTest/SmokeTest/Program.cs | 2 -- samples/SmokeTest/SmokeTest/TestBase.cs | 2 +- 6 files changed, 5 insertions(+), 22 deletions(-) diff --git a/samples/SmokeTest/SmokeTest/BlobStorageTest.cs b/samples/SmokeTest/SmokeTest/BlobStorageTest.cs index c7a369ca11de9..f0fd732f2c9db 100644 --- a/samples/SmokeTest/SmokeTest/BlobStorageTest.cs +++ b/samples/SmokeTest/SmokeTest/BlobStorageTest.cs @@ -7,11 +7,8 @@ namespace SmokeTest { - - class BlobStorageTest : TestBase { - private BlobServiceClient service; private BlockBlobClient blob; @@ -67,6 +64,5 @@ private async Task DeleteBlob() { await blob.DeleteAsync(); } - } -} +} \ No newline at end of file diff --git a/samples/SmokeTest/SmokeTest/CosmosDBTest.cs b/samples/SmokeTest/SmokeTest/CosmosDBTest.cs index d6887e4c1b772..b257abf9ee36e 100644 --- a/samples/SmokeTest/SmokeTest/CosmosDBTest.cs +++ b/samples/SmokeTest/SmokeTest/CosmosDBTest.cs @@ -30,11 +30,9 @@ public class Moon public string Name { get; set; } } - class CosmosDBTest : TestBase { private DocumentClient client; - private const string DataBaseName = "netSolarSystemDB"; private const string CollectionName = "netPlanetsCollection"; private Planet planetEarth; @@ -60,7 +58,6 @@ public async Task RunTests() 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"); - var testPassed = true; Console.Write("Creating Database '"+DataBaseName+"'... "); @@ -128,7 +125,6 @@ private async Task CreateDocuments() } } }; - planetMars = new Planet { Id = "Mars", @@ -149,7 +145,6 @@ private async Task CreateDocuments() await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(DataBaseName, CollectionName), planetEarth); await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(DataBaseName, CollectionName), planetMars); - } private async Task ExecuteSimpleQuery(){ @@ -169,6 +164,5 @@ private async Task DeleteDatabase() { await client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(DataBaseName)); } - } -} +} \ No newline at end of file diff --git a/samples/SmokeTest/SmokeTest/EventHubsTest.cs b/samples/SmokeTest/SmokeTest/EventHubsTest.cs index 48f99c6764029..00874ba0194ed 100644 --- a/samples/SmokeTest/SmokeTest/EventHubsTest.cs +++ b/samples/SmokeTest/SmokeTest/EventHubsTest.cs @@ -67,7 +67,6 @@ private async Task CreateSenderAndReceiver() { BeginReceivingAt = EventPosition.NewEventsOnly }; - sender = client.CreateSender(senderOptions); receiver = client.CreateReceiver(partition, receiverOptions); } @@ -126,6 +125,5 @@ private async Task SendAndReceiveEvents() throw new Exception(String.Format("Error, expecting " + eventBatch.Count().ToString() + " events, but only got " + index.ToString() + ".")); } } - } -} +} \ No newline at end of file diff --git a/samples/SmokeTest/SmokeTest/KeyVaultTest.cs b/samples/SmokeTest/SmokeTest/KeyVaultTest.cs index 5bf133700ce2e..1da525b0ba9f9 100644 --- a/samples/SmokeTest/SmokeTest/KeyVaultTest.cs +++ b/samples/SmokeTest/SmokeTest/KeyVaultTest.cs @@ -18,7 +18,6 @@ public KeyVaultTest(string secretName, string secretValue, string tenantid, stri this.client = new SecretClient(new Uri(KeyVaultUri), new ClientSecretCredential(tenantid, clientid, clientsecret)); } - /// /// Validates the Key Vault SDK /// @@ -71,7 +70,6 @@ private async Task GetSecret() { Azure.Response secret; secret = await client.GetAsync(secretName); - //Verify that the secret received is the one that was set previously if (secret.Value.Value != secretValue) { @@ -83,6 +81,5 @@ private async Task CleanUp() { await client.DeleteAsync(secretName); } - } -} +} \ No newline at end of file diff --git a/samples/SmokeTest/SmokeTest/Program.cs b/samples/SmokeTest/SmokeTest/Program.cs index 4e73240e01579..c1ffb4f6b7a7c 100644 --- a/samples/SmokeTest/SmokeTest/Program.cs +++ b/samples/SmokeTest/SmokeTest/Program.cs @@ -5,7 +5,6 @@ namespace SmokeTest { - class Program { static async Task Main(string[] args) @@ -28,7 +27,6 @@ static async Task Main(string[] args) { return 1; } - return 0; } } diff --git a/samples/SmokeTest/SmokeTest/TestBase.cs b/samples/SmokeTest/SmokeTest/TestBase.cs index 3b3451659eb02..eb5a91f3f3974 100644 --- a/samples/SmokeTest/SmokeTest/TestBase.cs +++ b/samples/SmokeTest/SmokeTest/TestBase.cs @@ -29,4 +29,4 @@ protected async Task ExcecuteTest(Func testAction) return true; } } -} +} \ No newline at end of file From d3c294fb7c220240cfe547d1ab013f752fe292d7 Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Tue, 2 Jul 2019 16:31:48 -0700 Subject: [PATCH 15/19] ExecuteTest typo --- samples/SmokeTest/SmokeTest/BlobStorageTest.cs | 4 ++-- samples/SmokeTest/SmokeTest/CosmosDBTest.cs | 10 +++++----- samples/SmokeTest/SmokeTest/EventHubsTest.cs | 4 ++-- samples/SmokeTest/SmokeTest/KeyVaultTest.cs | 6 +++--- samples/SmokeTest/SmokeTest/TestBase.cs | 2 +- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/samples/SmokeTest/SmokeTest/BlobStorageTest.cs b/samples/SmokeTest/SmokeTest/BlobStorageTest.cs index f0fd732f2c9db..fd3aac5f39816 100644 --- a/samples/SmokeTest/SmokeTest/BlobStorageTest.cs +++ b/samples/SmokeTest/SmokeTest/BlobStorageTest.cs @@ -32,7 +32,7 @@ public async Task RunTests() Console.WriteLine("2.- Delete that Blob Block" + '\n'); Console.Write("Uploading blob... "); - var result1 = await ExcecuteTest(UploadBlob); + var result1 = await ExecuteTest(UploadBlob); if (!result1) { //If this test failed, then the othe one is going to fail too. @@ -41,7 +41,7 @@ public async Task RunTests() } Console.Write("Deleting blob... "); - var result2 = await ExcecuteTest(DeleteBlob); + var result2 = await ExecuteTest(DeleteBlob); if (!result2) { return false; diff --git a/samples/SmokeTest/SmokeTest/CosmosDBTest.cs b/samples/SmokeTest/SmokeTest/CosmosDBTest.cs index b257abf9ee36e..c49c84a2c4ea3 100644 --- a/samples/SmokeTest/SmokeTest/CosmosDBTest.cs +++ b/samples/SmokeTest/SmokeTest/CosmosDBTest.cs @@ -61,7 +61,7 @@ public async Task RunTests() var testPassed = true; Console.Write("Creating Database '"+DataBaseName+"'... "); - var result1 = await ExcecuteTest(CreateDatabase); + var result1 = await ExecuteTest(CreateDatabase); if(!result1) { //If this test failed, the next ones are going to fail too @@ -70,28 +70,28 @@ public async Task RunTests() } Console.Write("Creating collection '"+CollectionName+"' "); - var result2 = await ExcecuteTest(CreateCollection); + var result2 = await ExecuteTest(CreateCollection); if (!result2) { testPassed = false; } Console.Write("Inserting 'Earth' and 'Mars' JSON Documents... "); - var result3 = await ExcecuteTest(CreateDocuments); + var result3 = await ExecuteTest(CreateDocuments); if (!result3) { testPassed = false; } Console.Write("Querying... "); - var result4 = await ExcecuteTest(ExecuteSimpleQuery); + var result4 = await ExecuteTest(ExecuteSimpleQuery); if (!result4) { testPassed = false; } Console.Write("Cleaning up the resource... "); - var result5 = await ExcecuteTest(DeleteDatabase); + var result5 = await ExecuteTest(DeleteDatabase); if (!result5) { testPassed = false; diff --git a/samples/SmokeTest/SmokeTest/EventHubsTest.cs b/samples/SmokeTest/SmokeTest/EventHubsTest.cs index 00874ba0194ed..ff39ac88de5cf 100644 --- a/samples/SmokeTest/SmokeTest/EventHubsTest.cs +++ b/samples/SmokeTest/SmokeTest/EventHubsTest.cs @@ -39,7 +39,7 @@ public async Task RunTests() Console.WriteLine("2.- Recieve those events\n"); Console.Write("Creating the Sender and Receivers... "); - var result1 = await ExcecuteTest(CreateSenderAndReceiver); + var result1 = await ExecuteTest(CreateSenderAndReceiver); if(!result1) { //If this test fail, the next one is going to fail too. @@ -47,7 +47,7 @@ public async Task RunTests() return false; } - var result2 = await ExcecuteTest(SendAndReceiveEvents); + var result2 = await ExecuteTest(SendAndReceiveEvents); if(!result2) { return false; diff --git a/samples/SmokeTest/SmokeTest/KeyVaultTest.cs b/samples/SmokeTest/SmokeTest/KeyVaultTest.cs index 1da525b0ba9f9..b2902a49a5bfe 100644 --- a/samples/SmokeTest/SmokeTest/KeyVaultTest.cs +++ b/samples/SmokeTest/SmokeTest/KeyVaultTest.cs @@ -35,7 +35,7 @@ public async Task RunTests() var testPassed = true; Console.Write("Set a secret... "); - var result1 = await ExcecuteTest(SetNewSecret); + var result1 = await ExecuteTest(SetNewSecret); if (!result1) { //If this test failes, the other ones are going to fail too. @@ -44,14 +44,14 @@ public async Task RunTests() } Console.Write("Get that secret... "); - var result2 = await ExcecuteTest(GetSecret); + var result2 = await ExecuteTest(GetSecret); if (!result2) { testPassed = false; } Console.Write("Cleaning up the resource... "); - var result3 = await ExcecuteTest(CleanUp); + var result3 = await ExecuteTest(CleanUp); if (!result3) { testPassed = false; diff --git a/samples/SmokeTest/SmokeTest/TestBase.cs b/samples/SmokeTest/SmokeTest/TestBase.cs index eb5a91f3f3974..b519f2d81385d 100644 --- a/samples/SmokeTest/SmokeTest/TestBase.cs +++ b/samples/SmokeTest/SmokeTest/TestBase.cs @@ -12,7 +12,7 @@ abstract class TestBase /// /// /// - protected async Task ExcecuteTest(Func testAction) + protected async Task ExecuteTest(Func testAction) { try { From 620a7dcb20ef610a6e0cbc2fb2f121224e35f240 Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Mon, 8 Jul 2019 17:21:50 -0700 Subject: [PATCH 16/19] Base class deleted The samples not longer need the "TestBase" class to run. --- .../SmokeTest/SmokeTest/BlobStorageTest.cs | 19 +++----- samples/SmokeTest/SmokeTest/CosmosDBTest.cs | 43 ++++++------------- samples/SmokeTest/SmokeTest/EventHubsTest.cs | 18 +++----- samples/SmokeTest/SmokeTest/KeyVaultTest.cs | 29 ++++--------- samples/SmokeTest/SmokeTest/TestBase.cs | 32 -------------- 5 files changed, 33 insertions(+), 108 deletions(-) delete mode 100644 samples/SmokeTest/SmokeTest/TestBase.cs diff --git a/samples/SmokeTest/SmokeTest/BlobStorageTest.cs b/samples/SmokeTest/SmokeTest/BlobStorageTest.cs index fd3aac5f39816..76446fe216599 100644 --- a/samples/SmokeTest/SmokeTest/BlobStorageTest.cs +++ b/samples/SmokeTest/SmokeTest/BlobStorageTest.cs @@ -7,7 +7,7 @@ namespace SmokeTest { - class BlobStorageTest : TestBase + class BlobStorageTest { private BlobServiceClient service; private BlockBlobClient blob; @@ -32,26 +32,19 @@ public async Task RunTests() Console.WriteLine("2.- Delete that Blob Block" + '\n'); Console.Write("Uploading blob... "); - var result1 = await ExecuteTest(UploadBlob); - if (!result1) - { - //If this test failed, then the othe one is going to fail too. - Console.WriteLine("Cannot delete the Blob."); - return false; - } + await UploadBlob(); + Console.WriteLine("done"); Console.Write("Deleting blob... "); - var result2 = await ExecuteTest(DeleteBlob); - if (!result2) - { - return false; - } + await DeleteBlob(); + Console.WriteLine("done"); return true; } private async Task UploadBlob() { + //This will upload an existing file into the container previously created const string path = "./Resources/BlobTestSource.txt"; using (FileStream data = File.OpenRead(path)) diff --git a/samples/SmokeTest/SmokeTest/CosmosDBTest.cs b/samples/SmokeTest/SmokeTest/CosmosDBTest.cs index c49c84a2c4ea3..902dd7a7649a4 100644 --- a/samples/SmokeTest/SmokeTest/CosmosDBTest.cs +++ b/samples/SmokeTest/SmokeTest/CosmosDBTest.cs @@ -30,11 +30,11 @@ public class Moon public string Name { get; set; } } - class CosmosDBTest : TestBase + class CosmosDBTest { private DocumentClient client; private const string DataBaseName = "netSolarSystemDB"; - private const string CollectionName = "netPlanetsCollection"; + private const string CollectionName = "PlanetsCollection"; private Planet planetEarth; private Planet planetMars; @@ -61,41 +61,24 @@ public async Task RunTests() var testPassed = true; Console.Write("Creating Database '"+DataBaseName+"'... "); - var result1 = await ExecuteTest(CreateDatabase); - if(!result1) - { - //If this test failed, the next ones are going to fail too - Console.WriteLine("Cannot create a Collection, Documents, Query the collection and neither Delete the DB."); - return false; - } + await CreateDatabase(); + Console.WriteLine("done"); Console.Write("Creating collection '"+CollectionName+"' "); - var result2 = await ExecuteTest(CreateCollection); - if (!result2) - { - testPassed = false; - } + await CreateCollection(); + Console.WriteLine("done"); Console.Write("Inserting 'Earth' and 'Mars' JSON Documents... "); - var result3 = await ExecuteTest(CreateDocuments); - if (!result3) - { - testPassed = false; - } + await CreateDocuments(); + Console.WriteLine("done"); Console.Write("Querying... "); - var result4 = await ExecuteTest(ExecuteSimpleQuery); - if (!result4) - { - testPassed = false; - } - + await ExecuteSimpleQuery(); + Console.WriteLine("done"); + Console.Write("Cleaning up the resource... "); - var result5 = await ExecuteTest(DeleteDatabase); - if (!result5) - { - testPassed = false; - } + await DeleteDatabase(); + Console.WriteLine("done"); return testPassed; } diff --git a/samples/SmokeTest/SmokeTest/EventHubsTest.cs b/samples/SmokeTest/SmokeTest/EventHubsTest.cs index ff39ac88de5cf..75fca0ae5df7d 100644 --- a/samples/SmokeTest/SmokeTest/EventHubsTest.cs +++ b/samples/SmokeTest/SmokeTest/EventHubsTest.cs @@ -14,7 +14,7 @@ namespace SmokeTest { - class EventHubsTest : TestBase + class EventHubsTest { private EventHubClient client; private EventSender sender; @@ -39,19 +39,11 @@ public async Task RunTests() Console.WriteLine("2.- Recieve those events\n"); Console.Write("Creating the Sender and Receivers... "); - var result1 = await ExecuteTest(CreateSenderAndReceiver); - if(!result1) - { - //If this test fail, the next one is going to fail too. - Console.WriteLine("Cannot send or receive events."); - return false; - } + await CreateSenderAndReceiver(); + Console.WriteLine("done"); - var result2 = await ExecuteTest(SendAndReceiveEvents); - if(!result2) - { - return false; - } + await SendAndReceiveEvents(); + Console.WriteLine("done"); return true; } diff --git a/samples/SmokeTest/SmokeTest/KeyVaultTest.cs b/samples/SmokeTest/SmokeTest/KeyVaultTest.cs index b2902a49a5bfe..7d66d399b7f25 100644 --- a/samples/SmokeTest/SmokeTest/KeyVaultTest.cs +++ b/samples/SmokeTest/SmokeTest/KeyVaultTest.cs @@ -5,7 +5,7 @@ namespace SmokeTest { - class KeyVaultTest : TestBase + class KeyVaultTest { private string secretName; private string secretValue; @@ -34,28 +34,17 @@ public async Task RunTests() var testPassed = true; - Console.Write("Set a secret... "); - var result1 = await ExecuteTest(SetNewSecret); - if (!result1) - { - //If this test failes, the other ones are going to fail too. - Console.WriteLine("Cannot get a secret and delete it."); - return false; - } + Console.Write("Setting a secret... "); + await SetNewSecret(); + Console.WriteLine("done"); - Console.Write("Get that secret... "); - var result2 = await ExecuteTest(GetSecret); - if (!result2) - { - testPassed = false; - } + Console.Write("Getting that secret... "); + await GetSecret(); + Console.WriteLine("done"); Console.Write("Cleaning up the resource... "); - var result3 = await ExecuteTest(CleanUp); - if (!result3) - { - testPassed = false; - } + await CleanUp(); + Console.WriteLine("done"); return testPassed; } diff --git a/samples/SmokeTest/SmokeTest/TestBase.cs b/samples/SmokeTest/SmokeTest/TestBase.cs deleted file mode 100644 index b519f2d81385d..0000000000000 --- a/samples/SmokeTest/SmokeTest/TestBase.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; - -namespace SmokeTest -{ - abstract class TestBase - { - /// - /// High order function to handle the exceptions of each test. - /// - /// - /// - protected async Task ExecuteTest(Func testAction) - { - try - { - await testAction(); - } - catch (Exception ex) - { - Console.WriteLine("FAILED."); - Console.WriteLine(ex); - return false; - } - - Console.WriteLine("done"); - return true; - } - } -} \ No newline at end of file From 30e472c7c43daa11e095a6a3a157b2355eea22f9 Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Tue, 9 Jul 2019 18:55:22 -0700 Subject: [PATCH 17/19] BlobTestSource.tst deleted This text file is not going to be needed any more since the test Blob is being created within the code. --- .../SmokeTest/SmokeTest/Properties/Resources.Designer.cs | 9 --------- samples/SmokeTest/SmokeTest/Properties/Resources.resx | 3 --- samples/SmokeTest/SmokeTest/Resources/BlobTestSource.txt | 1 - samples/SmokeTest/SmokeTest/SmokeTest.csproj | 6 ------ 4 files changed, 19 deletions(-) delete mode 100644 samples/SmokeTest/SmokeTest/Resources/BlobTestSource.txt diff --git a/samples/SmokeTest/SmokeTest/Properties/Resources.Designer.cs b/samples/SmokeTest/SmokeTest/Properties/Resources.Designer.cs index 65e2c63599f5d..ebf480105cfde 100644 --- a/samples/SmokeTest/SmokeTest/Properties/Resources.Designer.cs +++ b/samples/SmokeTest/SmokeTest/Properties/Resources.Designer.cs @@ -59,14 +59,5 @@ internal Resources() { resourceCulture = value; } } - - /// - /// Looks up a localized string similar to This Blob was uploaded via the SDK.. - /// - internal static string BlobTestSource { - get { - return ResourceManager.GetString("BlobTestSource", resourceCulture); - } - } } } diff --git a/samples/SmokeTest/SmokeTest/Properties/Resources.resx b/samples/SmokeTest/SmokeTest/Properties/Resources.resx index 5054a09fbe69d..2f96abecd49c6 100644 --- a/samples/SmokeTest/SmokeTest/Properties/Resources.resx +++ b/samples/SmokeTest/SmokeTest/Properties/Resources.resx @@ -118,7 +118,4 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - ..\Resources\BlobTestSource.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 - \ No newline at end of file diff --git a/samples/SmokeTest/SmokeTest/Resources/BlobTestSource.txt b/samples/SmokeTest/SmokeTest/Resources/BlobTestSource.txt deleted file mode 100644 index 9b8be627a8a2d..0000000000000 --- a/samples/SmokeTest/SmokeTest/Resources/BlobTestSource.txt +++ /dev/null @@ -1 +0,0 @@ -This Blob was uploaded via the SDK. \ No newline at end of file diff --git a/samples/SmokeTest/SmokeTest/SmokeTest.csproj b/samples/SmokeTest/SmokeTest/SmokeTest.csproj index 8ec89ef5bccf1..257f3424ce7ee 100644 --- a/samples/SmokeTest/SmokeTest/SmokeTest.csproj +++ b/samples/SmokeTest/SmokeTest/SmokeTest.csproj @@ -29,10 +29,4 @@ - - - Always - - - From 6d90610ecb34e70d5bbc296423aa3afd0f8e74b8 Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Tue, 9 Jul 2019 18:56:13 -0700 Subject: [PATCH 18/19] Static Classes All classes are now static. --- .../SmokeTest/SmokeTest/BlobStorageTest.cs | 52 ++++++----- samples/SmokeTest/SmokeTest/CosmosDBTest.cs | 92 ++++++++++--------- samples/SmokeTest/SmokeTest/EventHubsTest.cs | 30 +++--- samples/SmokeTest/SmokeTest/KeyVaultTest.cs | 53 +++++------ samples/SmokeTest/SmokeTest/Program.cs | 19 +--- 5 files changed, 115 insertions(+), 131 deletions(-) diff --git a/samples/SmokeTest/SmokeTest/BlobStorageTest.cs b/samples/SmokeTest/SmokeTest/BlobStorageTest.cs index 76446fe216599..5dee8b1792a96 100644 --- a/samples/SmokeTest/SmokeTest/BlobStorageTest.cs +++ b/samples/SmokeTest/SmokeTest/BlobStorageTest.cs @@ -3,26 +3,21 @@ using System.IO; using System.Runtime.CompilerServices; using System.Runtime.InteropServices.ComTypes; +using System.Text; using System.Threading.Tasks; namespace SmokeTest { class BlobStorageTest { - private BlobServiceClient service; - private BlockBlobClient blob; - - public BlobStorageTest(string connectionString,string containerName, string blobName) - { - this.service = new BlobServiceClient(connectionString); - this.blob = service.GetBlobContainerClient(containerName).GetBlockBlobClient(blobName); - } + private static BlobServiceClient serviceClient; + private static BlockBlobClient blobClient; /// /// Test the Storage Blobs SDK /// /// true if passes, false if fails - public async Task RunTests() + public static async Task RunTests() { Console.WriteLine("\n---------------------------------"); Console.WriteLine("STORAGE"); @@ -31,31 +26,38 @@ public async Task RunTests() Console.WriteLine("1.- Upload Blob Block"); Console.WriteLine("2.- Delete that Blob Block" + '\n'); - Console.Write("Uploading blob... "); - await UploadBlob(); - Console.WriteLine("done"); + 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); - Console.Write("Deleting blob... "); + await UploadBlob(); await DeleteBlob(); - Console.WriteLine("done"); - - return true; } - private async Task UploadBlob() + private static async Task UploadBlob() { - //This will upload an existing file into the container previously created - const string path = "./Resources/BlobTestSource.txt"; + Console.Write("Uploading blob... "); - using (FileStream data = File.OpenRead(path)) - { - await blob.UploadAsync(data); - } + //This is going to create stream to upload, for uploading an existing file use: + // using (FileStream data = File.OpenRead()) + // { + // await blobClient.UploadAsync(data); + // } + 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 async Task DeleteBlob() + private static async Task DeleteBlob() { - await blob.DeleteAsync(); + Console.Write("Deleting blob..."); + await blobClient.DeleteAsync(); + Console.WriteLine("\tdone"); } } } \ No newline at end of file diff --git a/samples/SmokeTest/SmokeTest/CosmosDBTest.cs b/samples/SmokeTest/SmokeTest/CosmosDBTest.cs index 902dd7a7649a4..a8e60a03f3b7b 100644 --- a/samples/SmokeTest/SmokeTest/CosmosDBTest.cs +++ b/samples/SmokeTest/SmokeTest/CosmosDBTest.cs @@ -1,4 +1,5 @@ -using Microsoft.Azure.Documents; +using Azure.Storage.Blobs.Models; +using Microsoft.Azure.Documents; using Microsoft.Azure.Documents.Client; using Newtonsoft.Json; using System; @@ -32,22 +33,16 @@ public class Moon class CosmosDBTest { - private DocumentClient client; + private static DocumentClient client; private const string DataBaseName = "netSolarSystemDB"; private const string CollectionName = "PlanetsCollection"; - private Planet planetEarth; - private Planet planetMars; - - public CosmosDBTest(string endpoint, string authKey) - { - client = new DocumentClient(new Uri(endpoint), authKey); - } + private static List planets = new List(); /// /// Test the Cosmos DB SDK by creating an example Database called {DataBaseName} and a PlanetsCollection with planets on it. /// /// - public async Task RunTests() + public static async Task RunTests() { Console.WriteLine("\n---------------------------------"); Console.WriteLine("COSMOS DB"); @@ -58,57 +53,48 @@ public async Task RunTests() 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"); - var testPassed = true; - Console.Write("Creating Database '"+DataBaseName+"'... "); - await CreateDatabase(); - Console.WriteLine("done"); + string endpoint = Environment.GetEnvironmentVariable("COSMOS_URI"); + string authKey = Environment.GetEnvironmentVariable("COSMOS_AUTH_KEY"); + client = new DocumentClient(new Uri(endpoint), authKey); - Console.Write("Creating collection '"+CollectionName+"' "); + await CreateDatabase(); await CreateCollection(); - Console.WriteLine("done"); - - Console.Write("Inserting 'Earth' and 'Mars' JSON Documents... "); await CreateDocuments(); - Console.WriteLine("done"); - - Console.Write("Querying... "); await ExecuteSimpleQuery(); - Console.WriteLine("done"); - - Console.Write("Cleaning up the resource... "); await DeleteDatabase(); - Console.WriteLine("done"); - - return testPassed; } - private async Task CreateDatabase() + private static async Task CreateDatabase() { + Console.Write("Creating Database '" + DataBaseName + "'..."); await client.CreateDatabaseIfNotExistsAsync(new Database { Id = DataBaseName }); + Console.WriteLine("\tdone"); } - private async Task CreateCollection() + private static async Task CreateCollection() { + Console.Write("Creating collection '" + CollectionName + "'..."); await client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(DataBaseName), new DocumentCollection { Id = CollectionName }); + Console.WriteLine("\tdone"); } - private async Task CreateDocuments() + private static async Task CreateDocuments() { - planetEarth = new Planet + planets.Add(new Planet { Id = "Earth", HasRings = false, Radius = 3959, Moons = new Moon[] - { + { new Moon { Name = "Moon" } - } - }; - planetMars = new Planet + } + }); + planets.Add(new Planet { Id = "Mars", HasRings = false, @@ -124,28 +110,46 @@ private async Task CreateDocuments() Name = "Deimos" } } - }; - - await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(DataBaseName, CollectionName), planetEarth); - await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(DataBaseName, CollectionName), planetMars); + }); + + //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"); + } } - private async Task ExecuteSimpleQuery(){ + private static async Task ExecuteSimpleQuery(){ + Console.Write("Querying... "); IQueryable planetarySqlQuery = client.CreateDocumentQuery(UriFactory.CreateDocumentCollectionUri(DataBaseName, CollectionName), "SELECT * FROM c"); + int i = 0; foreach (Planet planet in planetarySqlQuery) { - //The only 2 planets that were set before were Earth and Mars, if planet does not match any of those, then there is an error. - if (planet.ToString() != planetEarth.ToString() && planet.ToString() != planetMars.ToString()) + foreach (Planet planetInArray in planets) { - throw new Exception(String.Format("Error, the values does not match.\n")); + if (planet.ToString() == planetInArray.ToString()) + { + i++; + } } } + + //The query should retrieve all the planets, this is going to verify that all planets were retreived + if(i != planets.Count) + { + throw new Exception(String.Format("Error, the values does not match.")); + } + Console.WriteLine("\tdone"); } - private async Task DeleteDatabase() + private static async Task DeleteDatabase() { + Console.Write("Cleaning up the resource..."); await client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(DataBaseName)); + Console.WriteLine("\tdone"); } } } \ No newline at end of file diff --git a/samples/SmokeTest/SmokeTest/EventHubsTest.cs b/samples/SmokeTest/SmokeTest/EventHubsTest.cs index 75fca0ae5df7d..55822d574f14f 100644 --- a/samples/SmokeTest/SmokeTest/EventHubsTest.cs +++ b/samples/SmokeTest/SmokeTest/EventHubsTest.cs @@ -16,20 +16,15 @@ namespace SmokeTest { class EventHubsTest { - private EventHubClient client; - private EventSender sender; - private EventReceiver receiver; - - public EventHubsTest(string connectionString) - { - this.client = new EventHubClient(connectionString); - } + private static EventHubClient client; + private static EventSender sender; + private static EventReceiver receiver; /// /// Test the Event Hubs SDK by sending and receiving events /// /// true if pases, false if fails - public async Task RunTests() + public static async Task RunTests() { Console.WriteLine("\n---------------------------------"); Console.WriteLine("EVENT HUBS"); @@ -38,18 +33,16 @@ public async Task RunTests() Console.WriteLine("1.- Send an Event batch"); Console.WriteLine("2.- Recieve those events\n"); - Console.Write("Creating the Sender and Receivers... "); - await CreateSenderAndReceiver(); - Console.WriteLine("done"); + var connectionString = Environment.GetEnvironmentVariable("EVENT_HUBS_CONNECTION_STRING"); + client = new EventHubClient(connectionString); + await CreateSenderAndReceiver(); await SendAndReceiveEvents(); - Console.WriteLine("done"); - - return true; } - private async Task CreateSenderAndReceiver() + private static async Task CreateSenderAndReceiver() { + Console.Write("Creating the Sender and Receivers... "); var partition = (await client.GetPartitionIdsAsync()).First(); var senderOptions = new EventSenderOptions { @@ -61,9 +54,10 @@ private async Task CreateSenderAndReceiver() }; sender = client.CreateSender(senderOptions); receiver = client.CreateReceiver(partition, receiverOptions); + Console.WriteLine("\tdone"); } - private async Task SendAndReceiveEvents() + private static async Task SendAndReceiveEvents() { var eventBatch = new[] { @@ -116,6 +110,8 @@ private async Task SendAndReceiveEvents() { throw new Exception(String.Format("Error, expecting " + eventBatch.Count().ToString() + " events, but only got " + index.ToString() + ".")); } + + Console.WriteLine("done"); } } } \ No newline at end of file diff --git a/samples/SmokeTest/SmokeTest/KeyVaultTest.cs b/samples/SmokeTest/SmokeTest/KeyVaultTest.cs index 7d66d399b7f25..9f8a46368b184 100644 --- a/samples/SmokeTest/SmokeTest/KeyVaultTest.cs +++ b/samples/SmokeTest/SmokeTest/KeyVaultTest.cs @@ -7,22 +7,15 @@ namespace SmokeTest { class KeyVaultTest { - private string secretName; - private string secretValue; - private SecretClient client; - - public KeyVaultTest(string secretName, string secretValue, string tenantid, string clientid, string clientsecret, string KeyVaultUri) - { - this.secretName = secretName; - this.secretValue = secretValue; - this.client = new SecretClient(new Uri(KeyVaultUri), new ClientSecretCredential(tenantid, clientid, clientsecret)); - } + private const string SecretName = "SmokeTestSecret"; + private const string SecretValue = "smokeTestValue"; + private static SecretClient client; /// /// Validates the Key Vault SDK /// /// true if passes, false if fails - public async Task RunTests() + public static async Task RunTests() { Console.WriteLine("\n---------------------------------"); Console.WriteLine("KEY VAULT"); @@ -31,44 +24,44 @@ public async Task RunTests() Console.WriteLine("1.- Set a Secret"); Console.WriteLine("2.- Get that Secret"); Console.WriteLine("3.- Delete that Secret (Clean up)\n"); - - var testPassed = true; - Console.Write("Setting a secret... "); - await SetNewSecret(); - Console.WriteLine("done"); + string tenantID = Environment.GetEnvironmentVariable("DIR_TENANT_ID"); + string clientID = Environment.GetEnvironmentVariable("APP_CLIENT_ID"); + string clientSecret = Environment.GetEnvironmentVariable("CLIENT_SECRET"); + string keyVaultUri = Environment.GetEnvironmentVariable("KEY_VAULT_URI"); + client = new SecretClient(new Uri(keyVaultUri), new ClientSecretCredential(tenantID, clientID, clientSecret)); - Console.Write("Getting that secret... "); + await SetNewSecret(); await GetSecret(); - Console.WriteLine("done"); - - Console.Write("Cleaning up the resource... "); await CleanUp(); - Console.WriteLine("done"); - - return testPassed; } - private async Task SetNewSecret() + private static async Task SetNewSecret() { - var newSecret = new Secret(secretName, secretValue); + Console.Write("Setting a secret..."); + var newSecret = new Secret(SecretName, SecretValue); await client.SetAsync(newSecret); + Console.WriteLine("\tdone"); } - private async Task GetSecret() + private static async Task GetSecret() { + Console.Write("Getting that secret..."); Azure.Response secret; - secret = await client.GetAsync(secretName); + secret = await client.GetAsync(SecretName); //Verify that the secret received is the one that was set previously - if (secret.Value.Value != secretValue) + if (secret.Value.Value != SecretValue) { throw new Exception(String.Format("Secret retreived, but not the one previously created: '" + secret.Value.Value)); } + Console.WriteLine("\tdone"); } - private async Task CleanUp() + private static async Task CleanUp() { - await client.DeleteAsync(secretName); + Console.Write("Cleaning up the resource..."); + await client.DeleteAsync(SecretName); + Console.WriteLine("\tdone"); } } } \ No newline at end of file diff --git a/samples/SmokeTest/SmokeTest/Program.cs b/samples/SmokeTest/SmokeTest/Program.cs index c1ffb4f6b7a7c..9663c6fd9058e 100644 --- a/samples/SmokeTest/SmokeTest/Program.cs +++ b/samples/SmokeTest/SmokeTest/Program.cs @@ -11,22 +11,11 @@ static async Task Main(string[] args) { Console.WriteLine("SMOKE TEST FOR TRACK 2 LIBRARIES"); - var keyVault = new KeyVaultTest("SmokeTestSecret", "smokeTestValue", Environment.GetEnvironmentVariable("DIR_TENANT_ID"), Environment.GetEnvironmentVariable("APP_CLIENT_ID"), Environment.GetEnvironmentVariable("CLIENT_SECRET"), Environment.GetEnvironmentVariable("KEY_VAULT_URI")); - var kvResult = await keyVault.RunTests(); + await KeyVaultTest.RunTests(); + await BlobStorageTest.RunTests(); + await EventHubsTest.RunTests(); + await CosmosDBTest.RunTests(); - var blobStorage = new BlobStorageTest(Environment.GetEnvironmentVariable("BLOB_CONNECTION_STRING"), "mycontainer", "netSmokeTestBlob"); - var bsResult = await blobStorage.RunTests(); - - var eventHubs = new EventHubsTest(Environment.GetEnvironmentVariable("EVENT_HUBS_CONNECTION_STRING")); - var ehResult = await eventHubs.RunTests(); - - var cosmosDB = new CosmosDBTest(Environment.GetEnvironmentVariable("COSMOS_URI"), Environment.GetEnvironmentVariable("COSMOS_AUTH_KEY")); - var cdbResult = await cosmosDB.RunTests(); - - if (!kvResult || !bsResult || !ehResult || !cdbResult) - { - return 1; - } return 0; } } From c0440008f2996ef06d8eefdf389fe8245b94bc0f Mon Sep 17 00:00:00 2001 From: Jonathan Cardenas Date: Fri, 12 Jul 2019 14:12:34 -0700 Subject: [PATCH 19/19] Pair programming comments --- .../SmokeTest/SmokeTest/BlobStorageTest.cs | 6 ---- samples/SmokeTest/SmokeTest/CosmosDBTest.cs | 29 ++++++++++++++----- samples/SmokeTest/SmokeTest/EventHubsTest.cs | 5 ++-- samples/SmokeTest/SmokeTest/KeyVaultTest.cs | 1 - 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/samples/SmokeTest/SmokeTest/BlobStorageTest.cs b/samples/SmokeTest/SmokeTest/BlobStorageTest.cs index 5dee8b1792a96..e9bb585226733 100644 --- a/samples/SmokeTest/SmokeTest/BlobStorageTest.cs +++ b/samples/SmokeTest/SmokeTest/BlobStorageTest.cs @@ -16,7 +16,6 @@ class BlobStorageTest /// /// Test the Storage Blobs SDK /// - /// true if passes, false if fails public static async Task RunTests() { Console.WriteLine("\n---------------------------------"); @@ -40,11 +39,6 @@ private static async Task UploadBlob() { Console.Write("Uploading blob... "); - //This is going to create stream to upload, for uploading an existing file use: - // using (FileStream data = File.OpenRead()) - // { - // await blobClient.UploadAsync(data); - // } const string content = "This is the content for the sample blob"; byte[] byteArray = Encoding.UTF8.GetBytes(content); MemoryStream stream = new MemoryStream(byteArray); diff --git a/samples/SmokeTest/SmokeTest/CosmosDBTest.cs b/samples/SmokeTest/SmokeTest/CosmosDBTest.cs index a8e60a03f3b7b..890b63c376be4 100644 --- a/samples/SmokeTest/SmokeTest/CosmosDBTest.cs +++ b/samples/SmokeTest/SmokeTest/CosmosDBTest.cs @@ -41,7 +41,6 @@ class CosmosDBTest /// /// Test the Cosmos DB SDK by creating an example Database called {DataBaseName} and a PlanetsCollection with planets on it. /// - /// public static async Task RunTests() { Console.WriteLine("\n---------------------------------"); @@ -58,6 +57,14 @@ public static async Task RunTests() 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(); @@ -121,26 +128,32 @@ private static async Task CreateDocuments() } } + /// + /// The query retrieve all planets, this is going to verify that planets match + /// private static async Task ExecuteSimpleQuery(){ Console.Write("Querying... "); IQueryable planetarySqlQuery = client.CreateDocumentQuery(UriFactory.CreateDocumentCollectionUri(DataBaseName, CollectionName), "SELECT * FROM c"); + var planetsSet = new HashSet(planets.Count); + foreach(Planet planet in planets) + { + planetsSet.Add(planet.ToString()); + } + int i = 0; foreach (Planet planet in planetarySqlQuery) { - foreach (Planet planetInArray in planets) + var serializedPlanet = planet.ToString(); + if (planetsSet.Contains(serializedPlanet)) { - if (planet.ToString() == planetInArray.ToString()) - { - i++; - } + i++; } } - //The query should retrieve all the planets, this is going to verify that all planets were retreived if(i != planets.Count) { - throw new Exception(String.Format("Error, the values does not match.")); + throw new Exception("Error, values do not match."); } Console.WriteLine("\tdone"); } diff --git a/samples/SmokeTest/SmokeTest/EventHubsTest.cs b/samples/SmokeTest/SmokeTest/EventHubsTest.cs index 55822d574f14f..115398337a702 100644 --- a/samples/SmokeTest/SmokeTest/EventHubsTest.cs +++ b/samples/SmokeTest/SmokeTest/EventHubsTest.cs @@ -23,7 +23,6 @@ class EventHubsTest /// /// Test the Event Hubs SDK by sending and receiving events /// - /// true if pases, false if fails public static async Task RunTests() { Console.WriteLine("\n---------------------------------"); @@ -82,14 +81,14 @@ private static async Task SendAndReceiveEvents() } index = 0; - //Check if at least one event was received in roder to start validation + //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("Beggining validation..."); + Console.WriteLine("Beginning validation..."); foreach (var receivedEvent in receivedEvents) { var receivedEventMessage = Encoding.UTF8.GetString(receivedEvent.Body.ToArray()); diff --git a/samples/SmokeTest/SmokeTest/KeyVaultTest.cs b/samples/SmokeTest/SmokeTest/KeyVaultTest.cs index 9f8a46368b184..824e85423d004 100644 --- a/samples/SmokeTest/SmokeTest/KeyVaultTest.cs +++ b/samples/SmokeTest/SmokeTest/KeyVaultTest.cs @@ -14,7 +14,6 @@ class KeyVaultTest /// /// Validates the Key Vault SDK /// - /// true if passes, false if fails public static async Task RunTests() { Console.WriteLine("\n---------------------------------");