Esempio n. 1
0
        public void CleanupTestImages()
        {
            IComputeProvider provider = Bootstrapper.CreateComputeProvider();
            IEnumerable <SimpleServerImage> images = ListAllImages(provider).ToArray();

            foreach (SimpleServerImage image in images)
            {
                if (string.IsNullOrEmpty(image.Name))
                {
                    continue;
                }

                if (!image.Name.StartsWith(UnitTestImagePrefix))
                {
                    continue;
                }

                Console.WriteLine("Deleting unit test image... {0} ({1})", image.Name, image.Id);
                bool deleted = provider.DeleteImage(image.Id);
                Assert.IsTrue(deleted);
                provider.WaitForImageDeleted(image.Id);
            }
        }
        public void TestCreateImage()
        {
            IComputeProvider provider = Bootstrapper.CreateComputeProvider();

            /* Create the image
             */
            string imageName = UserComputeTests.UnitTestImagePrefix + Path.GetRandomFileName();
            bool   imaged    = provider.CreateImage(_server.Id, imageName);

            Assert.IsTrue(imaged);
            ServerImage[] images = provider.ListImagesWithDetails(server: _server.Id, imageName: imageName).ToArray();
            Assert.IsNotNull(images);
            Assert.AreEqual(1, images.Length);

            ServerImage image = images[0];

            Assert.AreEqual(imageName, image.Name);
            Assert.IsFalse(string.IsNullOrEmpty(image.Id));

            Assert.AreEqual(ImageState.Active, provider.WaitForImageActive(image.Id).Status);

            /* Test metadata operations on the image
             */
            Assert.IsTrue(provider.SetImageMetadataItem(image.Id, "Item 1", "Value"));
            Assert.AreEqual("Value", provider.GetImageMetadataItem(image.Id, "Item 1"));
            Assert.IsTrue(provider.SetImageMetadataItem(image.Id, "Item 2", "Value ²"));
            Assert.AreEqual("Value ²", provider.GetImageMetadataItem(image.Id, "Item 2"));

            // setting the same key overwrites the previous value
            Assert.IsTrue(provider.SetImageMetadataItem(image.Id, "Item 1", "Value 1"));
            Assert.AreEqual("Value 1", provider.GetImageMetadataItem(image.Id, "Item 1"));

            Assert.IsTrue(provider.DeleteImageMetadataItem(image.Id, "Item 1"));
            Assert.IsFalse(provider.ListImageMetadata(image.Id).ContainsKey("Item 1"));

            Metadata metadata = new Metadata()
            {
                { "Different", "Variables" },
            };

            Assert.IsTrue(provider.UpdateImageMetadata(image.Id, metadata));
            Metadata actual = provider.ListImageMetadata(image.Id);

            Assert.IsNotNull(actual);
            Assert.AreEqual("Value ²", actual["Item 2"]);
            Assert.AreEqual("Variables", actual["Different"]);

            // a slight tweak
            metadata["Different"] = "Values";
            Assert.IsTrue(provider.SetImageMetadata(image.Id, metadata));
            actual = provider.ListImageMetadata(image.Id);
            Assert.IsNotNull(actual);
            Assert.AreEqual(1, actual.Count);
            Assert.AreEqual("Values", actual["Different"]);

            Assert.IsTrue(provider.SetImageMetadata(image.Id, new Metadata()));
            Assert.AreEqual(0, provider.ListImageMetadata(image.Id).Count);

            /* Cleanup
             */
            bool deleted = provider.DeleteImage(images[0].Id);

            Assert.IsTrue(deleted);
            provider.WaitForImageDeleted(images[0].Id);
        }