private static VirtualMachine InitVirtualMachineOperation(MockRequestChannel mockChannel, out VirtualMachineOperations vmOperations) { //Cloud for return value of first request (client gets cloud to get stampId) var testCloud = new Cloud {ID = Guid.NewGuid(), StampId = Guid.NewGuid()}; mockChannel.AddReturnObject(testCloud); //VM for return value of second request (client updates VM with operation) var testVM = new VirtualMachine {ID = Guid.NewGuid(), StampId = testCloud.StampId}; mockChannel.AddReturnObject(testVM, new WebHeaderCollection {"x-ms-request-id:" + Guid.NewGuid()}); var factory = new WebClientFactory(new Subscription(), mockChannel); vmOperations = new VirtualMachineOperations(factory); return testVM; }
public void DeleteVM() { var sub = new Subscription(); var channel = new MockRequestChannel(); //Response to client getting /Clouds (client needs stampId, gets it from clouds) var testCloud = new Cloud { ID = Guid.NewGuid(), StampId = Guid.NewGuid() }; channel.AddReturnObject(testCloud); //Response to the DELETE channel.AddReturnObject(null, new WebHeaderCollection {"x-ms-request-id:" + Guid.NewGuid()}); var vmOps = new VirtualMachineOperations(new WebClientFactory(sub, channel)); Guid toDelete = Guid.NewGuid(); Guid? jobOut; vmOps.Delete(toDelete, out jobOut); //Check the requests the client generated Assert.AreEqual(channel.ClientRequests.Count, 2); Assert.AreEqual(channel.ClientRequests[1].Item1.Method, HttpMethod.Delete.ToString()); }
public void VmCreateShouldThrowIfNoVhdAndNoTemplateSupplied() { var channel = new MockRequestChannel(); var testCloud = new Cloud { ID = Guid.NewGuid(), StampId = Guid.NewGuid() }; channel.AddReturnObject(testCloud); var sub = new Subscription(); var vmOps = new VirtualMachineOperations(new WebClientFactory(sub, channel)); var vmToCreate = new VirtualMachine {Name = "Test"}; Guid? jobOut; vmOps.Create(vmToCreate, out jobOut); }
public void VmCreateShouldThrowWhenNoObjectReturned() { var mockChannel = new MockRequestChannel(); var testCloud = new Cloud { ID = Guid.NewGuid(), StampId = Guid.NewGuid() }; mockChannel.AddReturnObject(testCloud); var vmOps = new VirtualMachineOperations(new WebClientFactory(new Subscription(), mockChannel)); var vmToCreate = new VirtualMachine { VirtualHardDiskId = Guid.NewGuid(), Name = "Test" }; Guid? jobOut; vmOps.Create(vmToCreate, out jobOut); }
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); }