/// <summary>
        /// Functional Cases : for New-AzureStorageTable
        /// 1. Create a list of new Tables (Positive 2)
        /// 2. Create a list of Tables that already exist (Negative 4)
        /// 3. Create a list of Tables that some of them already exist (Negative 5)
        /// 
        /// Functional Cases : for Get-AzureStorageTable
        /// 4.	Get a list of Tables by using wildcards in the name (Positive 4)
        /// 5.	Get a list of tables by using Prefix parameter (Positive 2)
        /// 
        /// Functional Cases : for Remove-AzureStorageTable
        /// 6.	Remove a list of existing Tables by using pipeline (Positive 4)
        /// </summary>
        internal void TableListOperations(Agent agent)
        {
            string PREFIX = Utility.GenNameString("uniqueprefix");
            string[] TABLE_NAMES = new string[] { Utility.GenNameString(PREFIX), Utility.GenNameString(PREFIX), Utility.GenNameString(PREFIX) };

            // PART_EXISTING_NAMES differs only the last element with Table_NAMES
            string[] PARTLY_EXISTING_NAMES = new string[TABLE_NAMES.Length];
            Array.Copy(TABLE_NAMES, PARTLY_EXISTING_NAMES, TABLE_NAMES.Length - 1);
            PARTLY_EXISTING_NAMES[TABLE_NAMES.Length - 1] = Utility.GenNameString(PREFIX);

            string[] MERGED_NAMES = TABLE_NAMES.Union(PARTLY_EXISTING_NAMES).ToArray();
            Array.Sort(MERGED_NAMES);

            // Generate the comparison data
            Collection<Dictionary<string, object>> comp = new Collection<Dictionary<string, object>>();
            foreach (string name in MERGED_NAMES)
            {
                comp.Add(Utility.GenComparisonData(StorageObjectType.Table, name));
            }

            CloudTableClient tableClient = _StorageAccount.CreateCloudTableClient();

            // Check if all the above Tables have been removed
            foreach (string name in MERGED_NAMES)
            {
                CloudTable Table = tableClient.GetTableReference(name);
                Table.DeleteIfExists();
            }

            //--------------1. New operation--------------
            Test.Assert(agent.NewAzureStorageTable(TABLE_NAMES), Utility.GenComparisonData("NewAzureStorageTable", true));
            // Verification for returned values
            Test.Assert(agent.Output.Count == TABLE_NAMES.Count(), "{0} row returned : {1}", TABLE_NAMES.Count(), agent.Output.Count);

            // Check if all the above tables have been created
            foreach (string name in TABLE_NAMES)
            {
                CloudTable table = tableClient.GetTableReference(name);
                Test.Assert(table.Exists(), "table {0} should exist", name);
            }

            try
            {
                //--------------2. New operation--------------
                Test.Assert(!agent.NewAzureStorageTable(TABLE_NAMES), Utility.GenComparisonData("NewAzureStorageTable", false));
                // Verification for returned values
                Test.Assert(agent.Output.Count == 0, "0 row returned : {0}", agent.Output.Count);
                int i = 0;
                foreach (string name in TABLE_NAMES)
                {
                    Test.Assert(agent.ErrorMessages[i].Equals(String.Format("Table '{0}' already exists.", name)), agent.ErrorMessages[i]);
                    ++i;
                }

                //--------------3. New operation--------------
                Test.Assert(!agent.NewAzureStorageTable(PARTLY_EXISTING_NAMES), Utility.GenComparisonData("NewAzureStorageTable", false));
                // Verification for returned values
                Test.Assert(agent.Output.Count == 1, "1 row returned : {0}", agent.Output.Count);

                // Check if all the above tables have been created
                foreach (string name in TABLE_NAMES)
                {
                    CloudTable table = tableClient.GetTableReference(name);
                    Test.Assert(table.Exists(), "table {0} should exist", name);
                }

                //--------------4. Get operation--------------
                Test.Assert(agent.GetAzureStorageTable("*" + PREFIX + "*"), Utility.GenComparisonData("GetAzureStorageTable", true));
                // Verification for returned values
                agent.OutputValidation(_StorageAccount.CreateCloudTableClient().ListTables(PREFIX));

                // use Prefix parameter
                Test.Assert(agent.GetAzureStorageTableByPrefix(PREFIX), Utility.GenComparisonData("GetAzureStorageTableByPrefix", true));
                // Verification for returned values
                agent.OutputValidation(_StorageAccount.CreateCloudTableClient().ListTables(PREFIX));
            }
            finally { 
                //--------------5. Remove operation--------------
                Test.Assert(agent.RemoveAzureStorageTable(MERGED_NAMES), Utility.GenComparisonData("RemoveAzureStorageTable", true));
                // Check if all the above tables have been removed
                foreach (string name in TABLE_NAMES)
                {
                    CloudTable table = tableClient.GetTableReference(name);
                    Test.Assert(!table.Exists(), "table {0} should not exist", name);
                }
            }
        }
        /// <summary>
        /// Negative Functional Cases : for New-AzureStorageTable 
        /// 1. Create a Table that already exists (Negative 3)
        /// </summary>
        internal void CreateExistingTable(Agent agent)
        {
            string TABLE_NAME = Utility.GenNameString("existing");

            // create table if not exists
            CloudTable table = _StorageAccount.CreateCloudTableClient().GetTableReference(TABLE_NAME);
            table.CreateIfNotExists();

            try
            {
                //--------------New operation--------------
                Test.Assert(!agent.NewAzureStorageTable(TABLE_NAME), Utility.GenComparisonData("NewAzureStorageTable", false));
                // Verification for returned values
                Test.Assert(agent.Output.Count == 0, "Only 0 row returned : {0}", agent.Output.Count);
                Test.Assert(agent.ErrorMessages[0].Equals(String.Format("Table '{0}' already exists.", TABLE_NAME)), agent.ErrorMessages[0]);
            }
            finally
            {
                // Recover the environment
                table.DeleteIfExists();
            }
        }
        /// <summary>
        /// Negative Functional Cases : for Remove-AzureStorageContainer 
        /// 1. Remove a non-existing blob container (Negative 2)
        /// </summary>
        internal void RemoveNonExistingContainer(Agent agent)
        {
            string CONTAINER_NAME = Utility.GenNameString("nonexisting");

            // Delete the container if it exists
            CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference(CONTAINER_NAME);
            container.DeleteIfExists();

            //--------------Remove operation--------------
            Test.Assert(!agent.RemoveAzureStorageContainer(CONTAINER_NAME), Utility.GenComparisonData("RemoveAzureStorageContainer", false));
            // Verification for returned values
            Test.Assert(agent.Output.Count == 0, "Only 0 row returned : {0}", agent.Output.Count);
            Test.Assert(agent.ErrorMessages[0].Equals(String.Format("Can not find the container '{0}'.", CONTAINER_NAME)), agent.ErrorMessages[0]);
        }
        internal void CopyBlobTestGB(Agent agent, StorageBlob.BlobType blobType)
        {
            string uploadFilePath = @".\" + Utility.GenNameString("gbupload");
            string srcContainerName = Utility.GenNameString("gbupload-", 15);
            string destContainerName = Utility.GenNameString("gbupload-", 15);
            string blobName = Path.GetFileName(uploadFilePath);

            // create the container
            CloudBlobContainer srcContainer = blobUtil.CreateContainer(srcContainerName);
            CloudBlobContainer destContainer = blobUtil.CreateContainer(destContainerName);

            // Generate a 512 bytes file which contains GB18030 characters
            File.WriteAllText(uploadFilePath, GB18030String);

            string localMd5 = Helper.GetFileContentMD5(uploadFilePath);

            try
            {
                // create source blob
                ICloudBlob blob = CloudBlobUtil.GetBlob(srcContainer, blobName, blobType);

                // upload file data
                using (var fileStream = System.IO.File.OpenRead(uploadFilePath))
                {
                    blob.UploadFromStream(fileStream);
                    // need to set MD5 as for page blob, it won't set MD5 automatically
                    blob.Properties.ContentMD5 = localMd5;
                    blob.SetProperties();
                }

                //--------------Copy blob operation--------------
                Test.Assert(agent.StartAzureStorageBlobCopy(srcContainerName, blobName, destContainerName, blobName), Utility.GenComparisonData("Start copy blob using blob name", true));

                // Get destination blob
                blob = CloudBlobUtil.GetBlob(destContainer, blobName, blobType);

                // Check MD5               
                blob.FetchAttributes();
                Test.Assert(localMd5 == blob.Properties.ContentMD5,
                    string.Format("blob content md5 should be {0}, and actualy it's {1}", localMd5, blob.Properties.ContentMD5));
            }
            finally
            {
                // cleanup
                srcContainer.DeleteIfExists();
                destContainer.DeleteIfExists();
                File.Delete(uploadFilePath);
            }
        }
        /// <summary>
        /// Functional Cases:
        /// 1. Create the root container  (New-AzureStorageContainer Positive 4)
        /// 2. Get the root container (Get-AzureStorageContainer Positive 4)
        /// 3. Remove the root container  (Remove-AzureStorageContainer Positive 4)
        /// </summary>
        internal void RootContainerOperations(Agent agent)
        {
            const string ROOT_CONTAINER_NAME = "$root";
            Dictionary<string, object> dic = Utility.GenComparisonData(StorageObjectType.Container, ROOT_CONTAINER_NAME);
            Collection<Dictionary<string, object>> comp = new Collection<Dictionary<string, object>> { dic };

            // delete container if it not exists
            CloudBlobContainer container = StorageAccount.CreateCloudBlobClient().GetRootContainerReference();
            bool bExists = container.Exists();
            if (bExists)
            {
                container.Delete();
            }

            try
            {
                //--------------New operation--------------
                bool created = false;
                int retryCount = 0;
                int maxRetryCount = 60; //retry ten minutes

                do
                {
                    if (retryCount > 0)
                    {
                        int sleepInterval = 10 * 1000;
                        Test.Info("Sleep and wait for retry...");
                        Thread.Sleep(sleepInterval);
                        Test.Info(string.Format("{0}th retry to create the $root container", retryCount));
                    }

                    bool successed = agent.NewAzureStorageContainer(ROOT_CONTAINER_NAME);

                    if (successed)
                    {
                        Test.Info("Create $root container successfully");
                        created = true;
                    }
                    else
                    {
                        if (agent.ErrorMessages.Count == 0)
                        {
                            Test.AssertFail("Can not create $root container and can't get any error messages");
                            break;
                        }

                        if (agent.ErrorMessages[0].StartsWith("The remote server returned an error: (409) Conflict."))
                        {
                            retryCount++;
                        }
                        else
                        {
                            Test.AssertFail(string.Format("Can not create $root container. Exception: {0}", agent.ErrorMessages[0]));
                            break;
                        }
                    }
                }
                while (!created && retryCount < maxRetryCount);

                if (!created && retryCount == maxRetryCount)
                { 
                    Test.AssertFail(string.Format("Can not create $root container after {0} times retry", retryCount));
                }

                container.FetchAttributes();
                CloudBlobUtil.PackContainerCompareData(container, dic);
                // Verification for returned values
                agent.OutputValidation(comp);
                Test.Assert(container.Exists(), "container {0} should exist!", ROOT_CONTAINER_NAME);

                //--------------Get operation--------------
                Test.Assert(agent.GetAzureStorageContainer(ROOT_CONTAINER_NAME), Utility.GenComparisonData("GetAzureStorageContainer", true));
                // Verification for returned values
                agent.OutputValidation(comp);

                //--------------Remove operation--------------
                Test.Assert(agent.RemoveAzureStorageContainer(ROOT_CONTAINER_NAME), Utility.GenComparisonData("RemoveAzureStorageContainer", true));
                Test.Assert(!container.Exists(), "container {0} should not exist!", ROOT_CONTAINER_NAME);
            }
            finally
            {
                // Recover the environment
                try
                {
                    if (bExists)
                    {
                        Test.Info("Sleep for 150 seconds to wait for removing the root container");
                        System.Threading.Thread.Sleep(150000);
                        //The following statement often throw an 409 conflict exception
                        container.Create();
                    }
                }
                catch
                { }
            }
        }
        /// <summary>
        /// Negative Functional Cases : for New-AzureStorageContainer 
        /// 1. Create a new blob containter with an invalid blob container name (Negative 1)
        /// </summary>
        internal void CreateInvalidContainer(Agent agent)
        {
            string containerName = Utility.GenNameString("abc_");

            //--------------New operation--------------
            Test.Assert(!agent.NewAzureStorageContainer(containerName), Utility.GenComparisonData("NewAzureStorageContainer", false));
            // Verification for returned values
            Test.Assert(agent.Output.Count == 0, "Only 0 row returned : {0}", agent.Output.Count);
            Test.Assert(agent.ErrorMessages[0].StartsWith(String.Format("Container name '{0}' is invalid.", containerName)), agent.ErrorMessages[0]);
        }
        internal void StorageTableTest(Agent agent)
        {
            string NEW_TABLE_NAME = Utility.GenNameString("Washington");

            //--------------New operation--------------
            Test.Assert(!agent.NewAzureStorageTable(NEW_TABLE_NAME), Utility.GenComparisonData("NewAzureStorageTable", false));
            CheckErrorOutput(agent);

            //--------------Get operation--------------
            Test.Assert(!agent.GetAzureStorageTable(NEW_TABLE_NAME), Utility.GenComparisonData("GetAzureStorageTable", false));
            CheckErrorOutput(agent);

            //--------------Remove operation--------------
            Test.Assert(!agent.RemoveAzureStorageTable(NEW_TABLE_NAME), Utility.GenComparisonData("RemoveAzureStorageTable", false));
            CheckErrorOutput(agent);
        }
        /// <summary>
        /// Negative Functional Cases : for Remove-AzureStorageTable 
        /// 1. Remove the table without by force (Negative 3)
        /// </summary>
        internal void RemoveTableWithoutForce(Agent agent)
        {
            string TABLE_NAME = Utility.GenNameString("withoutforce");

            // create table if not exists
            CloudTable table = _StorageAccount.CreateCloudTableClient().GetTableReference(TABLE_NAME);
            table.CreateIfNotExists();

            try
            {
                //--------------Remove operation--------------
                Test.Assert(!agent.RemoveAzureStorageTable(TABLE_NAME, false), Utility.GenComparisonData("RemoveAzureStorageTable", false));
                // Verification for returned values
                Test.Assert(agent.Output.Count == 0, "Only 0 row returned : {0}", agent.Output.Count);
                Test.Assert(agent.ErrorMessages[0].StartsWith("A command that prompts the user failed because"), agent.ErrorMessages[0]);
            }
            finally
            {
                // Recover the environment
                table.DeleteIfExists();
            }
        }
        internal void StorageBlobTest(Agent agent, string FilePath, StorageBlob.BlobType Type)
        {
            string NEW_CONTAINER_NAME = Utility.GenNameString("upload-");
            string BlobName = Path.GetFileName(FilePath);

            //--------------Upload operation--------------
            Test.Assert(!agent.SetAzureStorageBlobContent(FilePath, NEW_CONTAINER_NAME, Type), Utility.GenComparisonData("SendAzureStorageBlob", false));
            CheckErrorOutput(agent);

            //--------------Get operation--------------
            Test.Assert(!agent.GetAzureStorageBlob(BlobName, NEW_CONTAINER_NAME), Utility.GenComparisonData("GetAzureStorageBlob", false));
            CheckErrorOutput(agent);

            //--------------Remove operation--------------
            Test.Assert(!agent.RemoveAzureStorageBlob(BlobName, NEW_CONTAINER_NAME), Utility.GenComparisonData("RemoveAzureStorageBlob", false));
            CheckErrorOutput(agent);
        }
        internal void StorageQueueTest(Agent agent)
        {
            string NEW_QUEUE_NAME = Utility.GenNameString("redmond-");

            //--------------New operation--------------
            Test.Assert(!agent.NewAzureStorageQueue(NEW_QUEUE_NAME), Utility.GenComparisonData("NewAzureStorageQueue", false));
            CheckErrorOutput(agent);

            //--------------Get operation--------------
            Test.Assert(!agent.GetAzureStorageQueue(NEW_QUEUE_NAME), Utility.GenComparisonData("GetAzureStorageQueue", false));
            CheckErrorOutput(agent);

            //--------------Remove operation--------------
            Test.Assert(!agent.RemoveAzureStorageQueue(NEW_QUEUE_NAME), Utility.GenComparisonData("RemoveAzureStorageQueue", false));
            CheckErrorOutput(agent);
        }
        internal void StorageContainerTest(Agent agent)
        {
            string NEW_CONTAINER_NAME = Utility.GenNameString("astoria-");

            //--------------New operation--------------
            Test.Assert(!agent.NewAzureStorageContainer(NEW_CONTAINER_NAME), Utility.GenComparisonData("NewAzureStorageContainer", false));
            CheckErrorOutput(agent);

            //--------------Get operation--------------
            Test.Assert(!agent.GetAzureStorageContainer(NEW_CONTAINER_NAME), Utility.GenComparisonData("GetAzureStorageContainer", false));
            CheckErrorOutput(agent);

            //--------------Set operation-------------- 
            Test.Assert(!agent.SetAzureStorageContainerACL(NEW_CONTAINER_NAME, BlobContainerPublicAccessType.Blob),
                "SetAzureStorageContainerACL operation should fail");
            CheckErrorOutput(agent);

            Test.Assert(!agent.SetAzureStorageContainerACL(NEW_CONTAINER_NAME, BlobContainerPublicAccessType.Container),
                "SetAzureStorageContainerACL operation should fail");
            CheckErrorOutput(agent);
        }
        internal void StorageContextTest(Agent agent)
        {
            string StorageAccountName = Test.Data.Get("StorageAccountName");
            string StorageAccountKey = Test.Data.Get("StorageAccountKey");
            string StorageEndPoint = Test.Data.Get("StorageEndPoint");

            Collection<Dictionary<string, object>> comp = new Collection<Dictionary<string, object>>();
            bool useHttps = true; //default protocol is https
            string[] endPoints = Utility.GetStorageEndPoints(StorageAccountName, useHttps, StorageEndPoint);
            comp.Add(new Dictionary<string, object>{
                {"StorageAccountName", StorageAccountName},
                {"BlobEndPoint", endPoints[0]},
                {"QueueEndPoint", endPoints[1]},
                {"TableEndPoint", endPoints[2]}
            });

            //--------------New operation--------------
            Test.Assert(agent.NewAzureStorageContext(StorageAccountName, StorageAccountKey, StorageEndPoint), Utility.GenComparisonData("NewAzureStorageContext", true));
            // Verification for returned values
            agent.OutputValidation(comp);
        }
Example #13
0
        internal void UploadBlobTestGB(Agent agent, StorageBlob.BlobType blobType)
        {
            string uploadFilePath = @".\" + Utility.GenNameString("gbupload");
            string containerName = Utility.GenNameString("gbupload-");
            string blobName = Path.GetFileName(uploadFilePath);

            // create the container
            CloudBlobContainer container = blobUtil.CreateContainer(containerName);

            // Generate a 512 bytes file which contains GB18030 characters
            File.WriteAllText(uploadFilePath, GB18030String);

            try
            {
                //--------------Upload operation--------------
                Test.Assert(agent.SetAzureStorageBlobContent(uploadFilePath, containerName, blobType), 
                    Utility.GenComparisonData("SendAzureStorageBlob", true));

                ICloudBlob blob = CloudBlobUtil.GetBlob(container, blobName, blobType);
                Test.Assert(blob != null && blob.Exists(), "blob " + blobName + " should exist!");

                // Check MD5
                string localMd5 = Helper.GetFileContentMD5(uploadFilePath);
                blob.FetchAttributes();
                Test.Assert(localMd5 == blob.Properties.ContentMD5, 
                    string.Format("blob content md5 should be {0}, and actualy it's {1}", localMd5, blob.Properties.ContentMD5));
            }
            finally
            {
                // cleanup
                container.DeleteIfExists();
                File.Delete(uploadFilePath);
            }
        }
        /// <summary>
        /// Functional Cases : for Get-AzureStorageTable
        /// 1. Validate that all the tables can be enumerated (Positive 5)
        /// </summary>
        internal void EnumerateAllTables(Agent agent)
        {
            //--------------Get operation--------------
            Test.Assert(agent.GetAzureStorageTable(""), Utility.GenComparisonData("EnumerateAllTables", false));

            // Verification for returned values
            agent.OutputValidation(_StorageAccount.CreateCloudTableClient().ListTables());
        }
        internal void CheckErrorOutput(Agent agent)
        {
            Test.Assert(agent.Output.Count == 0, "Only 0 row returned : {0}", agent.Output.Count);

            //the same error may output different error messages in different environments
            bool expectedError = agent.ErrorMessages[0].StartsWith("The remote server returned an error: (502) Bad Gateway") ||
                agent.ErrorMessages[0].StartsWith("The remote name could not be resolved") ||
                agent.ErrorMessages[0].StartsWith("The operation has timed out");
            Test.Assert(expectedError, "use invalid storage account should return 502 or time out, actually it's {0}", agent.ErrorMessages[0]);
        }
        /// <summary>
        /// Negative Functional Cases : for Remove-AzureStorageTable 
        /// 1. Remove a non-existing table (Negative 2)
        /// </summary>
        internal void RemoveNonExistingTable(Agent agent)
        {
            string TABLE_NAME = Utility.GenNameString("nonexisting");

            // Delete the table if it exists
            CloudTable table = _StorageAccount.CreateCloudTableClient().GetTableReference(TABLE_NAME);
            table.DeleteIfExists();

            //--------------Remove operation--------------
            Test.Assert(!agent.RemoveAzureStorageTable(TABLE_NAME), Utility.GenComparisonData("RemoveAzureStorageTable", false));
            // Verification for returned values
            Test.Assert(agent.Output.Count == 0, "Only 0 row returned : {0}", agent.Output.Count);
            Test.Assert(agent.ErrorMessages[0].Equals(String.Format("Can not find table '{0}'.", TABLE_NAME)), agent.ErrorMessages[0]);
        }
        /// <summary>
        /// Negative Functional Cases : for Get-AzureStorageQueue 
        /// 1. Get a non-existing queue (Negative 1)
        /// </summary>
        internal void GetNonExistingQueue(Agent agent)
        {
            string QUEUE_NAME = Utility.GenNameString("nonexisting");

            // Delete the queue if it exists
            CloudQueue queue = _StorageAccount.CreateCloudQueueClient().GetQueueReference(QUEUE_NAME);
            queue.DeleteIfExists();

            //--------------Get operation--------------
            Test.Assert(!agent.GetAzureStorageQueue(QUEUE_NAME), Utility.GenComparisonData("GetAzureStorageQueue", false));
            // Verification for returned values
            Test.Assert(agent.Output.Count == 0, "Only 0 row returned : {0}", agent.Output.Count);
            Test.Assert(agent.ErrorMessages[0].Equals(String.Format("Can not find queue '{0}'.", QUEUE_NAME)), agent.ErrorMessages[0]);
        }
        /// <summary>
        /// Functional Cases : for New-AzureStorageContainer
        /// 1. Create a list of new blob containers (Positive 2)
        /// 2. Create a list of containers that some of them already exist (Negative 4)
        /// 
        /// Functional Cases : for Get-AzureStorageContainer
        /// 3.	Get a list of blob containers by using wildcards in the name (Positive 2)
        /// 
        /// Functional Cases : for Remove-AzureStorageContainer
        /// 4.	Remove a list of existing blob containers by using pipeline (Positive 6)
        /// </summary>
        internal void ContainerListOperations(Agent agent)
        {
            string PREFIX = Utility.GenNameString("uniqueprefix-") + "-";
            string[] CONTAINER_NAMES = new string[] { Utility.GenNameString(PREFIX), Utility.GenNameString(PREFIX), Utility.GenNameString(PREFIX) };

            // PART_EXISTING_NAMES differs only the last element with CONTAINER_NAMES
            string[] PARTLY_EXISTING_NAMES = new string[CONTAINER_NAMES.Length];
            Array.Copy(CONTAINER_NAMES, PARTLY_EXISTING_NAMES, CONTAINER_NAMES.Length - 1);
            PARTLY_EXISTING_NAMES[CONTAINER_NAMES.Length - 1] = Utility.GenNameString(PREFIX);

            string[] MERGED_NAMES = CONTAINER_NAMES.Union(PARTLY_EXISTING_NAMES).ToArray();
            Array.Sort(MERGED_NAMES);

            // Generate the comparison data
            Collection<Dictionary<string, object>> comp = new Collection<Dictionary<string, object>>();
            foreach (string name in MERGED_NAMES)
                comp.Add(Utility.GenComparisonData(StorageObjectType.Container, name));

            CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient();

            // Check if all the above containers have been removed
            foreach (string name in MERGED_NAMES)
            {
                CloudBlobContainer container = blobClient.GetContainerReference(name);
                container.DeleteIfExists();
            }

            //--------------1. New operation--------------
            Test.Assert(agent.NewAzureStorageContainer(CONTAINER_NAMES), Utility.GenComparisonData("NewAzureStorageContainer", true));
            // Verification for returned values
            Test.Assert(agent.Output.Count == CONTAINER_NAMES.Count(), "3 row returned : {0}", agent.Output.Count);

            // Check if all the above containers have been created
            foreach (string name in CONTAINER_NAMES)
            {
                CloudBlobContainer container = blobClient.GetContainerReference(name);
                Test.Assert(container.Exists(), "container {0} should exist", name);
            }

            try
            {
                //--------------2. New operation--------------
                Test.Assert(!agent.NewAzureStorageContainer(CONTAINER_NAMES), Utility.GenComparisonData("NewAzureStorageContainer", false));
                // Verification for returned values
                Test.Assert(agent.Output.Count == 0, "0 row returned : {0}", agent.Output.Count);
                int i = 0;
                foreach (string name in CONTAINER_NAMES)
                {
                    Test.Assert(agent.ErrorMessages[i].Equals(String.Format("Container '{0}' already exists.", name)), agent.ErrorMessages[i]);
                    ++i;
                }

                //--------------3. New operation--------------
                Test.Assert(!agent.NewAzureStorageContainer(PARTLY_EXISTING_NAMES), Utility.GenComparisonData("NewAzureStorageContainer", false));
                // Verification for returned values
                Test.Assert(agent.Output.Count == 1, "1 row returned : {0}", agent.Output.Count);

                // Check if all the above containers have been created
                foreach (string name in CONTAINER_NAMES)
                {
                    CloudBlobContainer container = blobClient.GetContainerReference(name);
                    Test.Assert(container.Exists(), "container {0} should exist", name);
                }


                //--------------4. Get operation--------------
                // use wildcards
                Test.Assert(agent.GetAzureStorageContainer("*" + PREFIX + "*"), Utility.GenComparisonData("GetAzureStorageContainer", true));
                // Verification for returned values
                agent.OutputValidation(StorageAccount.CreateCloudBlobClient().ListContainers(PREFIX, ContainerListingDetails.All));

                // use Prefix parameter
                Test.Assert(agent.GetAzureStorageContainerByPrefix(PREFIX), Utility.GenComparisonData("GetAzureStorageContainerByPrefix", true));
                // Verification for returned values
                agent.OutputValidation(StorageAccount.CreateCloudBlobClient().ListContainers(PREFIX, ContainerListingDetails.All));
            }
            finally { }

            //--------------5. Remove operation--------------
            Test.Assert(agent.RemoveAzureStorageContainer(MERGED_NAMES), Utility.GenComparisonData("RemoveAzureStorageContainer", true));
            // Check if all the above containers have been removed
            foreach (string name in CONTAINER_NAMES)
            {
                CloudBlobContainer container = blobClient.GetContainerReference(name);
                Test.Assert(!container.Exists(), "container {0} should not exist", name);
            }
        }
        /// <summary>
        /// Positive Functional Cases : for Get-AzureStorageQueue 
        /// 1. Get the ApproximateMessageCount of the queue (Positive 5)
        /// </summary>
        internal void GetMessageCount(Agent agent)
        {
            const int MAX_SIZE = 32;
            string QUEUE_NAME = Utility.GenNameString("messagecount-");

            // create queue if not exists
            CloudQueue queue = _StorageAccount.CreateCloudQueueClient().GetQueueReference(QUEUE_NAME);
            queue.CreateIfNotExists();
            // insert random count queues
            Random random = new Random();
            int count = random.Next(MAX_SIZE) + 1;  // count >= 1
            for (int i = 1; i <= count; ++i)
                queue.AddMessage(new CloudQueueMessage("message " + i));

            // generate comparsion data
            Collection<Dictionary<string, object>> comp = new Collection<Dictionary<string, object>>();
            var dic = Utility.GenComparisonData(StorageObjectType.Queue, QUEUE_NAME);
            dic["ApproximateMessageCount"] = count;
            comp.Add(dic);            

            try
            {
                //--------------Get operation--------------
                Test.Assert(agent.GetAzureStorageQueue(QUEUE_NAME), Utility.GenComparisonData("GetAzureStorageQueue", true));
                // Verification for returned values
                agent.OutputValidation(comp);            
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
        /// <summary>
        /// Negative Functional Cases : for New-AzureStorageContainer 
        /// 1. Create a blob container that already exists (Negative 3)
        /// </summary>
        internal void CreateExistingContainer(Agent agent)
        {
            string CONTAINER_NAME = Utility.GenNameString("existing");

            // create container if not exists
            CloudBlobContainer container = StorageAccount.CreateCloudBlobClient().GetContainerReference(CONTAINER_NAME);
            container.CreateIfNotExists();

            try
            {
                //--------------New operation--------------
                Test.Assert(!agent.NewAzureStorageContainer(CONTAINER_NAME), Utility.GenComparisonData("NewAzureStorageContainer", false));
                // Verification for returned values
                Test.Assert(agent.Output.Count == 0, "Only 0 row returned : {0}", agent.Output.Count);
                Test.Assert(agent.ErrorMessages[0].Equals(String.Format("Container '{0}' already exists.", CONTAINER_NAME)), agent.ErrorMessages[0]);
            }
            finally
            {
                // Recover the environment
                container.DeleteIfExists();
            }
        }
        /// <summary>
        /// Functional Cases:
        /// 1. Upload a new blob file in the root container     (Set-AzureStorageBlobContent Positive 2)
        /// 2. Get an existing blob in the root container       (Get-AzureStorageBlob Positive 2)
        /// 3. Download an existing blob in the root container  (Get-AzureStorageBlobContent Positive 2)
        /// 4. Remove an existing blob in the root container    (Remove-AzureStorageBlob Positive 2)
        /// </summary>
        internal void RootBlobOperations(Agent agent, string UploadFilePath, string DownloadDirPath, Microsoft.WindowsAzure.Storage.Blob.BlobType Type)
        {
            const string ROOT_CONTAINER_NAME = "$root";
            string blobName = Path.GetFileName(UploadFilePath);
            string downloadFilePath = Path.Combine(DownloadDirPath, blobName);

            Collection<Dictionary<string, object>> comp = new Collection<Dictionary<string, object>>();
            Dictionary<string, object> dic = Utility.GenComparisonData(StorageObjectType.Blob, blobName);

            dic["BlobType"] = Type;
            comp.Add(dic);

            // create the container
            CloudBlobContainer container = StorageAccount.CreateCloudBlobClient().GetRootContainerReference();
            container.CreateIfNotExists();

            //--------------Upload operation--------------
            Test.Assert(agent.SetAzureStorageBlobContent(UploadFilePath, ROOT_CONTAINER_NAME, Type), Utility.GenComparisonData("SendAzureStorageBlob", true));
            ICloudBlob blob = BlobHelper.QueryBlob(ROOT_CONTAINER_NAME, blobName);
            blob.FetchAttributes();
            // Verification for returned values
            CloudBlobUtil.PackBlobCompareData(blob, dic);
            agent.OutputValidation(comp);

            Test.Assert(blob.Exists(), "blob " + blobName + " should exist!");

            // validate the ContentType value for GetAzureStorageBlob operation
            if (Type == Microsoft.WindowsAzure.Storage.Blob.BlobType.BlockBlob)
            {
                dic["ContentType"] = "application/octet-stream";
            }

            //--------------Get operation--------------
            Test.Assert(agent.GetAzureStorageBlob(blobName, ROOT_CONTAINER_NAME), Utility.GenComparisonData("GetAzureStorageBlob", true));
            // Verification for returned values
            agent.OutputValidation(comp);

            //--------------Download operation--------------
            downloadFilePath = Path.Combine(DownloadDirPath, blobName);    
            Test.Assert(agent.GetAzureStorageBlobContent(blobName, downloadFilePath, ROOT_CONTAINER_NAME),
                Utility.GenComparisonData("GetAzureStorageBlobContent", true));
            // Verification for returned values
            agent.OutputValidation(comp);

            Test.Assert(Helper.CompareTwoFiles(downloadFilePath, UploadFilePath),
                String.Format("File '{0}' should be bit-wise identicial to '{1}'", downloadFilePath, UploadFilePath));

            //--------------Remove operation--------------
            Test.Assert(agent.RemoveAzureStorageBlob(blobName, ROOT_CONTAINER_NAME), Utility.GenComparisonData("RemoveAzureStorageBlob", true));
            blob = BlobHelper.QueryBlob(ROOT_CONTAINER_NAME, blobName);
            Test.Assert(blob == null, "blob {0} should not exist!", blobName);
        }
        /// <summary>
        /// Functional Cases : for Get-AzureStorageContainer 
        /// 1. Validate that all the containers can be enumerated (Positive 5)
        /// </summary>
        internal void EnumerateAllContainers(Agent agent)
        {
            //--------------Get operation--------------
            Test.Assert(agent.GetAzureStorageContainer(""), Utility.GenComparisonData("GetAzureStorageContainer", false));

            // Verification for returned values
            agent.OutputValidation(StorageAccount.CreateCloudBlobClient().ListContainers());
        }
        /// <summary>
        /// Negative Functional Cases : for Remove-AzureStorageBlob 
        /// 1. Remove a non-existing blob (Negative 2)
        /// </summary>
        internal void RemoveNonExistingBlob(Agent agent)
        {
            string CONTAINER_NAME = Utility.GenNameString("upload-");
            string BLOB_NAME = Utility.GenNameString("nonexisting");

            // create the container
            CloudBlobContainer container = StorageAccount.CreateCloudBlobClient().GetContainerReference(CONTAINER_NAME);
            container.CreateIfNotExists();

            try
            {
                // Delete the blob if it exists
                ICloudBlob blob = BlobHelper.QueryBlob(CONTAINER_NAME, BLOB_NAME);
                if (blob != null)
                    blob.DeleteIfExists();

                //--------------Remove operation--------------
                Test.Assert(!agent.RemoveAzureStorageBlob(BLOB_NAME, CONTAINER_NAME), Utility.GenComparisonData("RemoveAzureStorageBlob", false));
                // Verification for returned values
                Test.Assert(agent.Output.Count == 0, "Only 0 row returned : {0}", agent.Output.Count);
                Test.Assert(agent.ErrorMessages[0].Equals(String.Format("Can not find blob '{0}' in container '{1}'.", BLOB_NAME, CONTAINER_NAME)), agent.ErrorMessages[0]);
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
        /// <summary>
        /// Negative Functional Cases : for Remove-AzureStorageContainer 
        /// 1. Remove the blob container with blobs in it without by force (Negative 3)
        /// </summary>
        internal void RemoveContainerWithoutForce(Agent agent)
        {
            string CONTAINER_NAME = Utility.GenNameString("withoutforce-");

            // create container if not exists
            CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference(CONTAINER_NAME);
            container.CreateIfNotExists();

            try
            {
                //--------------Remove operation--------------
                Test.Assert(!agent.RemoveAzureStorageContainer(CONTAINER_NAME, false), Utility.GenComparisonData("RemoveAzureStorageContainer", false));
                // Verification for returned values
                Test.Assert(agent.Output.Count == 0, "Only 0 row returned : {0}", agent.Output.Count);
                Test.Assert(agent.ErrorMessages[0].StartsWith("A command that prompts the user failed because"), agent.ErrorMessages[0]);
            }
            finally
            {
                // Recover the environment
                container.DeleteIfExists();
            }
        }
Example #25
0
        internal void DownloadBlobTestGB(Agent agent, StorageBlob.BlobType blobType)
        {
            string uploadFilePath = @".\" + Utility.GenNameString("gbupload");
            string downloadFilePath = @".\" + Utility.GenNameString("gbdownload");
            string containerName = Utility.GenNameString("gbupload-");
            string blobName = Path.GetFileName(uploadFilePath);

            // create the container
            CloudBlobContainer container = blobUtil.CreateContainer(containerName);

            // Generate a 512 bytes file which contains GB18030 characters
            File.WriteAllText(uploadFilePath, GB18030String);

            try
            {
                // create source blob
                ICloudBlob blob = CloudBlobUtil.GetBlob(container, blobName, blobType);

                // upload file data
                using (var fileStream = System.IO.File.OpenRead(uploadFilePath))
                {
                    blob.UploadFromStream(fileStream);
                }

                //--------------Download operation--------------
                Test.Assert(agent.GetAzureStorageBlobContent(blobName, downloadFilePath, containerName, true), "download blob should be successful");

                // Check MD5
                string localMd5 = Helper.GetFileContentMD5(downloadFilePath);
                string uploadMd5 = Helper.GetFileContentMD5(uploadFilePath);
                blob.FetchAttributes();
                Test.Assert(localMd5 == uploadMd5, string.Format("blob content md5 should be {0}, and actualy it's {1}", localMd5, uploadMd5));
            }
            finally
            {
                // cleanup
                container.DeleteIfExists();
                File.Delete(uploadFilePath);
                File.Delete(downloadFilePath);
            }
        }