Exemplo n.º 1
0
        public void CreateVMFromTemplate()
        {
            var mockChannel = new MockRequestChannel();

            var testCloud = new Cloud {
                ID = Guid.NewGuid(), StampId = Guid.NewGuid()
            };

            mockChannel.AddReturnObject(testCloud);

            var vmToCreate = new VirtualMachine {
                VMTemplateId = Guid.NewGuid(), Name = "Test"
            };
            var vmToReturn = new VirtualMachine
            {
                ID      = Guid.NewGuid(),
                Name    = vmToCreate.Name,
                CloudId = testCloud.ID,
                StampId = testCloud.StampId
            };

            mockChannel.AddReturnObject(vmToReturn, new WebHeaderCollection {
                "x-ms-request-id:" + Guid.NewGuid()
            });

            var vmOps = new VirtualMachineOperations(new WebClientFactory(new Subscription(), mockChannel));

            Guid?jobOut;
            var  resultVM = vmOps.Create(vmToCreate, out jobOut);

            //Check the results that client returns
            Assert.NotNull(resultVM);
            Assert.True(resultVM is VirtualMachine);
            Assert.Equal(resultVM.ID, vmToReturn.ID);
            Assert.Equal(resultVM.Name, vmToReturn.Name);
            Assert.Equal(resultVM.CloudId, vmToReturn.CloudId);
            Assert.Equal(resultVM.StampId, vmToReturn.StampId);

            //Check the requests that the client made
            var requestList = mockChannel.ClientRequests;

            Assert.Equal(requestList.Count, 2);
            Assert.Equal(requestList[1].Item1.Method, HttpMethod.Post.ToString());
            Assert.True(requestList[1].Item1.RequestUri.ToString().TrimEnd(new[] { '/' }).EndsWith("/VirtualMachines"));

            var sentVM = mockChannel.DeserializeClientPayload <VirtualMachine>(requestList[1].Item2);

            Assert.NotNull(sentVM);
            Assert.True(sentVM.Count == 1);
            Assert.Equal(sentVM[0].CloudId, testCloud.ID);
            Assert.Equal(sentVM[0].StampId, testCloud.StampId);
            Assert.Equal(sentVM[0].Name, vmToCreate.Name);
            Assert.Equal(sentVM[0].VMTemplateId, vmToCreate.VMTemplateId);
        }
Exemplo n.º 2
0
        private static void CheckVirtualMachineOperationResult(string operation, MockRequestChannel mockChannel, VirtualMachine testVM)
        {
            var requests = mockChannel.ClientRequests;

            Assert.Equal(requests.Count, 2);
            Assert.Equal(requests[1].Item1.Method, HttpMethod.Put.ToString());

            var clientSentVM = mockChannel.DeserializeClientPayload <VirtualMachine>(requests[1].Item2);

            Assert.NotNull(clientSentVM);
            Assert.True(clientSentVM.Count == 1);
            Assert.Equal(testVM.ID, clientSentVM[0].ID);
            Assert.Equal(testVM.StampId, clientSentVM[0].StampId);
            Assert.Equal(clientSentVM[0].Operation, operation);
        }
        private static void CheckVirtualMachineOperationResult(string operation, MockRequestChannel mockChannel, VirtualMachine testVM)
        {
            var requests = mockChannel.ClientRequests;
            Assert.AreEqual(requests.Count, 2);
            Assert.AreEqual(requests[1].Item1.Method, HttpMethod.Put.ToString());

            var clientSentVM = mockChannel.DeserializeClientPayload<VirtualMachine>(requests[1].Item2);
            Assert.IsNotNull(clientSentVM);
            Assert.IsTrue(clientSentVM.Count == 1);
            Assert.AreEqual(testVM.ID, clientSentVM[0].ID);
            Assert.AreEqual(testVM.StampId, clientSentVM[0].StampId);
            Assert.AreEqual(clientSentVM[0].Operation, operation);
        }
        public void CreateVMFromTemplate()
        {
            var mockChannel = new MockRequestChannel();

            var testCloud = new Cloud { ID = Guid.NewGuid(), StampId = Guid.NewGuid() };
            mockChannel.AddReturnObject(testCloud);

            var vmToCreate = new VirtualMachine { VMTemplateId = Guid.NewGuid(), Name = "Test" };
            var vmToReturn = new VirtualMachine
            {
                ID = Guid.NewGuid(),
                Name = vmToCreate.Name,
                CloudId = testCloud.ID,
                StampId = testCloud.StampId
            };
            mockChannel.AddReturnObject(vmToReturn, new WebHeaderCollection { "x-ms-request-id:" + Guid.NewGuid() });

            var vmOps = new VirtualMachineOperations(new WebClientFactory(new Subscription(), mockChannel));

            Guid? jobOut;
            var resultVM = vmOps.Create(vmToCreate, out jobOut);

            //Check the results that client returns
            Assert.IsNotNull(resultVM);
            Assert.IsInstanceOfType(resultVM, typeof(VirtualMachine));
            Assert.AreEqual(resultVM.ID, vmToReturn.ID);
            Assert.AreEqual(resultVM.Name, vmToReturn.Name);
            Assert.AreEqual(resultVM.CloudId, vmToReturn.CloudId);
            Assert.AreEqual(resultVM.StampId, vmToReturn.StampId);

            //Check the requests that the client made
            var requestList = mockChannel.ClientRequests;
            Assert.AreEqual(requestList.Count, 2);
            Assert.AreEqual(requestList[1].Item1.Method, HttpMethod.Post.ToString());
            Assert.IsTrue(requestList[1].Item1.RequestUri.ToString().TrimEnd(new[] { '/' }).EndsWith("/VirtualMachines"));

            var sentVM = mockChannel.DeserializeClientPayload<VirtualMachine>(requestList[1].Item2);
            Assert.IsNotNull(sentVM);
            Assert.IsTrue(sentVM.Count == 1);
            Assert.AreEqual(sentVM[0].CloudId, testCloud.ID);
            Assert.AreEqual(sentVM[0].StampId, testCloud.StampId);
            Assert.AreEqual(sentVM[0].Name, vmToCreate.Name);
            Assert.AreEqual(sentVM[0].VMTemplateId, vmToCreate.VMTemplateId);
        }