Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

.NET smoke Test Sample #6652

Merged
merged 19 commits into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NuGet.Config
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<add key="Local" value="tools/LocalNugetFeed" />
<add key="roslyn" value="https://dotnet.myget.org/F/roslyn-tools/api/v3/index.json" />
<add key="azure-sdk-tools" value="https://azuresdkartifacts.blob.core.windows.net/azure-sdk-tools/index.json" />
<add key="azure-sdk-for-net" value="https://azuresdkartifacts.blob.core.windows.net/azure-sdk-for-net/index.json" />
</packageSources>
<config>
<add key="globalPackagesFolder" value="restoredPackages" />
Expand Down
1 change: 1 addition & 0 deletions samples/SmokeTest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
launchSettings.json
25 changes: 25 additions & 0 deletions samples/SmokeTest/SmokeTest.sln
Original file line number Diff line number Diff line change
@@ -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
56 changes: 56 additions & 0 deletions samples/SmokeTest/SmokeTest/BlobStorage.cs
Original file line number Diff line number Diff line change
@@ -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.
JonathanCrd marked this conversation as resolved.
Show resolved Hide resolved
* 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_CONNECTION_STRING"));
private static BlockBlobClient blob = service.GetBlobContainerClient("mycontainer").GetBlockBlobClient("SmokeTestBlob");
JonathanCrd marked this conversation as resolved.
Show resolved Hide resolved

public static async Task performFunctionalities()
JonathanCrd marked this conversation as resolved.
Show resolved Hide resolved
{
Console.WriteLine("\n---------------------------------");
danieljurek marked this conversation as resolved.
Show resolved Hide resolved
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<string> UploadBlob()
JonathanCrd marked this conversation as resolved.
Show resolved Hide resolved
{
const string path = "./Resources/BlobTestSource.txt";

using (FileStream data = File.OpenRead(path))
{
await blob.UploadAsync(data);
}
return "Blob created successfully";
}

private static async Task<string> DeleteBlob()
{
await blob.DeleteAsync();
return "Blob deleted successfully";
}

}
}
121 changes: 121 additions & 0 deletions samples/SmokeTest/SmokeTest/EventHubs.cs
Original file line number Diff line number Diff line change
@@ -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("EVENT_HUBS_CONNECTION_STRING"));

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<Tuple<EventSender,EventReceiver>> 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<EventSender, EventReceiver>(sender, receiver);
}

private static async Task<string> 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<EventData>();
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";
}

}
}
72 changes: 72 additions & 0 deletions samples/SmokeTest/SmokeTest/KeyVaultTest.cs
Original file line number Diff line number Diff line change
@@ -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("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";

//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<string> 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<string> 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<string> CleanUp(string secretName, SecretClient client)
{
await client.DeleteAsync(secretName);
return "done";
}

}
}
19 changes: 19 additions & 0 deletions samples/SmokeTest/SmokeTest/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Azure.Messaging.EventHubs;
using System;
using System.Threading.Tasks;

namespace SmokeTest
{

class Program
{
static async Task Main(string[] args)
JonathanCrd marked this conversation as resolved.
Show resolved Hide resolved
{
Console.WriteLine("SMOKE TEST FOR TRACK 2 LIBRARIES");
await KeyVaultTest.performFunctionalities();
await BlobStorage.performFunctionalities();
JonathanCrd marked this conversation as resolved.
Show resolved Hide resolved
await EventHubs.performFunctionalities();

}
}
}
72 changes: 72 additions & 0 deletions samples/SmokeTest/SmokeTest/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading