Пример #1
0
        public void AddListGetDeleteManagementCertificate()
        {
            TestContext.WriteLine("Beginning AddListGetDeleteManagementCertificate test.");
            CancellationToken token = TokenSource.Token;

            X509Certificate2 cert = Utilities.CreateCert(false);

            TestContext.WriteLine("Adding ManagementCertificate with thumbprint {0}.", cert.Thumbprint);
            var addTask = TestClient.AddManagementCertificateAsync(cert);

            addTask.Wait();

            TestContext.WriteLine("Certificate with thumbprint {0} added.", cert.Thumbprint);

            TestContext.WriteLine("To make sure the certificate works, the rest of the API calls in this test will use the new certificate.");
            TestContext.WriteLine("Instantiating new AzureHttpClient with cert with thumbprint: {0}", cert.Thumbprint);
            //make sure the cert works, call the rest of the APIs using this cert!
            AzureHttpClient newClient = new AzureHttpClient(new Guid(Utilities.SubscriptionId), cert);

            TestContext.WriteLine("Calling ListManagmentCertificate with new certificate.");
            var task = newClient.ListManagementCertificatesAsync(token);

            TestContext.WriteLine(task.Result.ToString());

            TestContext.WriteLine("Verifying the new cert is in the list.");

            var listCert = (from c in task.Result
                            where string.CompareOrdinal(c.Thumbprint, cert.Thumbprint) == 0
                            select c).FirstOrDefault();

            Assert.IsNotNull(listCert);

            TestContext.WriteLine("Certificate with thumbprint {0} is in list", listCert.Thumbprint);

            TestContext.WriteLine("Getting certificate with thumbprint {0}.", cert.Thumbprint);

            var getCert = newClient.GetManagementCertificateAsync(cert.Thumbprint, token);

            TestContext.WriteLine(getCert.Result.ToString());

            Assert.AreEqual(getCert.Result.Thumbprint, cert.Thumbprint);

            TestContext.WriteLine("Certificate gotten.");

            TestContext.WriteLine("Deleting certificate with Thumbprint {0}.", cert.Thumbprint);

            var deleteCert = newClient.DeleteManagementCertificateAsync(cert.Thumbprint);

            deleteCert.Wait();

            //dispose new client, won't work anymore...
            newClient.Dispose();

            TestContext.WriteLine("Confirming cert is not in list...");

            task = TestClient.ListManagementCertificatesAsync(token);

            TestContext.WriteLine(task.Result.ToString());

            TestContext.WriteLine("Verifying the new cert is not in the list.");

            listCert = (from c in task.Result
                            where string.CompareOrdinal(c.Thumbprint, cert.Thumbprint) == 0
                            select c).FirstOrDefault();

            Assert.IsNull(listCert);

            TestContext.WriteLine("Certificate deleted.");

            TestContext.WriteLine("Ending AddListGetDeleteManagementCertificate test.");
        }
Пример #2
0
        //This *always* calls the GetOperationStatus API
        internal static Action<Task<string>> PollUntilComplete(AzureHttpClient client, string operationName, TestContext context, CancellationToken token)
        {
            return (resultTask) =>
                {
                    string requestId = resultTask.Result;
                    context.WriteLine("Polling Operation: \"{0}.\" Request id is: {1}", operationName, requestId);
                    int curWaitTime = 1, lastWaitTime = 1;
                    Task<OperationStatusInfo> opStatus;
                    token.ThrowIfCancellationRequested();
                    do
                    {
                        context.WriteLine("\tOperation with id {0} is not complete, waiting {1} seconds.", requestId, curWaitTime);
                        Thread.Sleep(curWaitTime * 1000);
                        token.ThrowIfCancellationRequested();
                        int cur = curWaitTime;
                        curWaitTime += lastWaitTime;
                        lastWaitTime = cur;
                        context.WriteLine("\tCalling GetOperationStatus");
                        opStatus = client.GetOperationStatusAsync(requestId, token);
                        context.WriteLine(opStatus.Result.ToString());
                    } while (opStatus.Result.Status == OperationStatus.InProgress);

                    opStatus.Result.EnsureSuccessStatus();
                };
        }
Пример #3
0
        //this *always* calls GetStorageAccountProperties and GetStorageAccountKeys
        internal static Uri UploadToBlob(AzureHttpClient client, TestContext context, string storageAccountName, string containerName, string fileToUpload, CancellationToken token)
        {
            //container names must be all lowercase...
            //this is the same container vs uses...
            string blobUploadContainer = "vsdeploy";
            if (!string.IsNullOrEmpty(containerName))
            {
                blobUploadContainer = containerName.ToLowerInvariant();
            }

            const int MB = 1048576;
            const int kb = 1024;
            const int MaxMBs = 600; //package can't be larger than 600 MB
            const long MaxFileSize = MB * MaxMBs;
            const int MinsPerMB = 3;

            Uri blob, queue, table;

            context.WriteLine("Preparing to upload file {0} to storage account {1}.", fileToUpload, storageAccountName);
            string fileName = Path.GetFileName(fileToUpload).ToLowerInvariant();

            context.WriteLine("Calling GetStorageAccountProperties and GetStorageAccountKeys for storage account {0}", storageAccountName);
            var storagePropsTask = client.GetStorageAccountPropertiesAsync(storageAccountName, token);
            var storageKeysTask = client.GetStorageAccountKeysAsync(storageAccountName, token);

            Task.WaitAll(storagePropsTask, storageKeysTask);

            context.WriteLine(storagePropsTask.Result.ToString());
            context.WriteLine(storageKeysTask.Result.ToString());

            FixupEndpoints(storagePropsTask.Result, out blob, out queue, out table);

            using (FileStream stream = File.Open(fileToUpload, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                long fileSize = stream.Length;
                double MBs = ((double)fileSize) / MB;

                context.WriteLine("Ready to upload file {0}, file size {1}", fileName, MBs.ToString("F6", CultureInfo.CurrentCulture));

                if (MBs < 1) MBs = 1; // no shorter than 3 minute timeout

                if (fileSize > MaxFileSize)
                {
                    throw new ArgumentException(string.Format("File {0} is too large.", fileName), "FileToUpload");
                }

                context.WriteLine("Instantiating Cloud Storage Account object.");

                CloudStorageAccount account = new CloudStorageAccount(
                                                new StorageCredentialsAccountAndKey(
                                                    storagePropsTask.Result.Name,
                                                    storageKeysTask.Result.Primary),
                                                    blob, queue, table);

                context.WriteLine("Cloud Storage Account object intantiated.");

                //set the timeout based on the size of the file: 3 mins per MB
                TimeSpan timeout = TimeSpan.FromMinutes((double)(MBs * MinsPerMB));
                context.WriteLine("Setting upload timeout to {0} minutes", timeout.Minutes);

                CloudBlobClient blobClient = account.CreateCloudBlobClient();

                blobClient.RetryPolicy = RetryPolicies.Retry(4, TimeSpan.Zero);
                blobClient.Timeout = timeout;

                CloudBlobContainer container = blobClient.GetContainerReference(blobUploadContainer);

                container.CreateIfNotExist();

                //turn off public access to the container
                BlobContainerPermissions perms = new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Off };
                container.SetPermissions(perms);

                CloudBlob blobRef = container.GetBlobReference(fileName);

                DateTime startTime = DateTime.Now;
                context.WriteLine("Beginning upload of file {0} to blob {1} at time: {2}", fileName, blobRef.Uri, startTime.ToString("T", CultureInfo.CurrentCulture));

                IAsyncResult result = blobRef.BeginUploadFromStream(stream, null, null);

                context.WriteLine("Upload Started, waiting...");

                blobRef.EndUploadFromStream(result);

                DateTime endTime = DateTime.Now;
                TimeSpan uploadTime = endTime - startTime;

                double KBytePerSecond = (stream.Length / uploadTime.TotalSeconds) / kb;

                context.WriteLine("Upload of file {0} to blob {1} completed at {2}.", fileName, blobRef.Uri, endTime.ToString("T", CultureInfo.CurrentCulture));
                context.WriteLine("Upload took {0} seconds at an average rate of {1}kb per second.", uploadTime.Seconds.ToString(CultureInfo.CurrentCulture), KBytePerSecond.ToString("F4", CultureInfo.CurrentCulture));

                return blobRef.Uri;

            }
        }
Пример #4
0
        //this method chooses a location at random, makes sure it supports the required services, and then
        //randomly decides whether to create an affinity group or not.
        internal static void GetLocationOrAffinityGroup(TestContext testContext, AzureHttpClient testClient, out string locationName, out string affinityGroup, AvailableServices requiredServices)
        {
            locationName = null;
            affinityGroup = null;

            CancellationTokenSource source = new CancellationTokenSource();
            CancellationToken token = source.Token;

            testContext.WriteLine("Listing available locations:");
            var listTask = testClient.ListLocationsAsync(token);
            testContext.WriteLine(listTask.Result.ToString());

            string locationToUse = null;
            bool appropriateLocationFound = false;
            Random r = new Random();

            while (!appropriateLocationFound)
            {
                testContext.WriteLine("Choosing a location at random");
                int idx = r.Next(listTask.Result.Count);
                var location = listTask.Result[idx];

                testContext.WriteLine("Chose Location {0} randomly", location.Name);

                testContext.WriteLine("Checking to see if it supports required services.");
                testContext.WriteLine("Asking for {0}, locations supports {1}", requiredServices.ToString(), location.AvailableServices.ToString());

                if ((requiredServices & location.AvailableServices) == requiredServices)
                {
                    testContext.WriteLine("Found appropriate location {0}", location.Name);
                    locationToUse = location.Name;
                    appropriateLocationFound = true;
                }
                else
                {
                    testContext.WriteLine("Location {0} is not appropriate, it does not support {1} service.", location.Name, (requiredServices ^ location.AvailableServices).ToString());
                }
            }

            bool doAffinityGroup = r.Next(2) == 1;

            if (doAffinityGroup)
            {
                //create an affinity group with a unique name
                string affinityGroupName;
                string affinityGroupLabel;

                CreateUniqueNameAndLabel(out affinityGroupName, out affinityGroupLabel);

                testContext.WriteLine("Randomly chose to create an affinity group, with name {0} and label {1}.", affinityGroupName, affinityGroupLabel);

                var affinityGroupTask = testClient.CreateAffinityGroupAsync(affinityGroupName, affinityGroupLabel, null, locationToUse);
                affinityGroupTask.Wait();
                testContext.WriteLine("Affinity Group {0} created.", affinityGroupName);

                testContext.WriteLine("Getting properties for affinity group {0}", affinityGroupName);
                var getAffinityGroupTask = testClient.GetAffinityGroupAsync(affinityGroupName, token);

                testContext.WriteLine(getAffinityGroupTask.Result.ToString());

                affinityGroup = getAffinityGroupTask.Result.Name;
            }
            else
            {
                locationName = locationToUse;
            }
        }
Пример #5
0
        internal static void DeleteStorageAccount(TestContext testContext, AzureHttpClient testClient, string storageAccountName)
        {
            testContext.WriteLine("Beginning DeleteStorageAccount operation.");
            testContext.WriteLine("Deleting Storage account {0}.", storageAccountName);
            //create is a polling operation, delete is not...
            var deleteTask = testClient.DeleteStorageAccountAsync(storageAccountName);

            deleteTask.Wait();
            testContext.WriteLine("Storage account {0} deleted.", storageAccountName);
            testContext.WriteLine("End DeleteStorageAccount operation.");
        }
Пример #6
0
        internal static void DeleteBlob(string storageAccountName, Uri blobUri, AzureHttpClient client, TestContext context, CancellationToken token)
        {
            //this *always* calls GetStorageAccountProperties and GetStorageAccountKeys
            Uri blob, queue, table;

            context.WriteLine("Preparing to delete blob: {0}.", blobUri);

            context.WriteLine("Calling GetStorageAccountProperties and GetStorageAccountKeys for storage account {0}", storageAccountName);
            var storagePropsTask = client.GetStorageAccountPropertiesAsync(storageAccountName, token);
            var storageKeysTask = client.GetStorageAccountKeysAsync(storageAccountName, token);

            Task.WaitAll(storagePropsTask, storageKeysTask);

            context.WriteLine(storagePropsTask.Result.ToString());
            context.WriteLine(storageKeysTask.Result.ToString());

            FixupEndpoints(storagePropsTask.Result, out blob, out queue, out table);

            context.WriteLine("Instantiating Cloud Storage Account object.");

            CloudStorageAccount account = new CloudStorageAccount(
                                            new StorageCredentialsAccountAndKey(
                                                storagePropsTask.Result.Name,
                                                storageKeysTask.Result.Primary),
                                                blob, queue, table);

            context.WriteLine("Cloud Storage Account object intantiated.");

            CloudBlobClient blobClient = account.CreateCloudBlobClient();

            CloudBlob blobRef = blobClient.GetBlobReference(blobUri.ToString());

            context.WriteLine("Deleting blob {0}.", blobUri);

            blobRef.DeleteIfExists();

            context.WriteLine("Blob {0} deleted.", blobUri);
        }
Пример #7
0
        //uploads the test package to a newly created storage account
        //and returns the storage account name
        internal static Uri CreateStorageAccountAndUploadTestPackage(TestContext testContext, AzureHttpClient testClient, ref string accountName, string location, string affinityGroup)
        {
            string retAccountName = CreateStorageAccount(testContext, testClient, accountName, location, affinityGroup);

            accountName = retAccountName;

            string fileToUpload = Path.Combine(DataPath, CSPkgFile);

            return UploadToBlob(testClient, testContext, accountName, null, fileToUpload, default(CancellationToken));
        }
Пример #8
0
        internal static string CreateStorageAccount(TestContext testContext, AzureHttpClient testClient, string accountName, string location, string affinityGroup)
        {
            testContext.WriteLine("Beginning CreateStorageAccount operation.");

            string storageAccountName = accountName.ToLower().Substring(0, 24);

            testContext.WriteLine("Creating Storage account with name {0}", storageAccountName);

            var createAndWaitTask = testClient.CreateStorageAccountAsync(storageAccountName, accountName, null, location, affinityGroup, true, null)
                .ContinueWith(Utilities.PollUntilComplete(testClient, "Create Storage Account", testContext, default(CancellationToken)));

            createAndWaitTask.Wait();

            testContext.WriteLine("Storage account {0} created.", storageAccountName);

            testContext.WriteLine("End CreateStorageAccount operation.");

            testContext.WriteLine("Get StorageAccountProperties on new account.");

            var getTask = testClient.GetStorageAccountPropertiesAsync(storageAccountName);

            testContext.WriteLine(getTask.Result.ToString());

            return storageAccountName;
        }