private Server WaitForVMState(string serverId, VirtualMachineState[] expectedVMStates, VirtualMachineState[] errorVMStates, int refreshCount = 600, TimeSpan?refreshDelay = null)
        {
            refreshDelay = refreshDelay ?? TimeSpan.FromMilliseconds(2400);

            var    os            = new OpenStackMember(UserName, Password, TenantName);
            Server serverDetails = os.GetServer(serverId);

            Func <bool> exitCondition = () => serverDetails.TaskState == null && (expectedVMStates.Contains(serverDetails.VMState) || errorVMStates.Contains(serverDetails.VMState));
            int         count         = 0;
            int         exitCount     = exitCondition() ? 1 : 0;

            while (exitCount < 3 && count < refreshCount)
            {
                System.Threading.Thread.Sleep(refreshDelay ?? TimeSpan.FromMilliseconds(2400));
                serverDetails = os.GetServer(serverId);
                count++;
                if (exitCondition())
                {
                    exitCount++;
                }
                else
                {
                    exitCount = 0;
                }
            }

            if (errorVMStates.Contains(serverDetails.VMState))
            {
                throw new ServerEnteredErrorStateException(serverDetails.Status);
            }

            return(serverDetails);
        }
        public void RevertResizeServerTest()
        {
            var          osm = new OpenStackMember(UserName, Password, TenantName, TenantId);
            SimpleServer ss  = osm.ListServers().FirstOrDefault(s => s.GetDetails().Status == ServerState.ShutOff);

            if (ss != null)
            {
                Server s = osm.GetServer(ss.Id);
                //if (s.Status.Name != "SHUTOFF")
                //    os.StopServer(s.Id);

                if (s.Status != ServerState.VerifyResize)
                {
                    IEnumerable <Flavor> flavors = osm.ListFlavors();
                    Flavor flavor1g = flavors.Where <Flavor>(x => x.Name == "g-1gb").First <Flavor>();
                    Flavor flavor2g = flavors.Where <Flavor>(x => x.Name == "g-2gb").First <Flavor>();
                    osm.ResizeServer(s.Id, (s.Flavor.Id == flavor1g.Id ? flavor2g.Id : flavor1g.Id), DiskConfiguration.FromName("AUTO"));
                    Trace.WriteLine(String.Format("server requested a resizing : {0}", DateTime.Now));
                    Trace.WriteLine(String.Format("FlavorId : {0}", s.Flavor.Id));
                    // wait for request
                    s = osm.ServersProvider.WaitForServerState(s.Id, ServerState.VerifyResize, new[] { ServerState.Error, ServerState.Unknown, ServerState.Suspended });
                    Trace.WriteLine(String.Format("server status changed to {0} : {1}", s.Status, DateTime.Now));
                }

                // resize
                bool b = osm.RevertResizeServer(s.Id);
                Assert.IsTrue(b);
                Trace.WriteLine(String.Format("server reverted a resizing : {0}", DateTime.Now));

                // wait for activate
                s = osm.ServersProvider.WaitForServerActive(s.Id);
                Trace.WriteLine(String.Format("server activated : {0}", DateTime.Now));
            }
        }
        public void RebuildServerTest_FlavorId_Invalid()
        {
            var          os = new OpenStackMember(UserName, Password, TenantName, TenantId);
            SimpleServer ss = GetServerByNameOrCreate(TesterName);
            Server       s  = os.GetServer(ss.Id);

            try
            {
                Server rebuiltserver = os.RebuildServer(s.Id, s.Image.Id, "newPassword");
            }
            catch (ServiceConflictException sce)
            {
                if (sce.Message.Equals("Cannot 'rebuild' while instance is in task_state reboot_started"))
                {
                    // no error for this is what expected.
                    Trace.WriteLine(string.Format("s.Status : {0}", s.Status));
                    Trace.WriteLine(string.Format("s.VMState : {0}", s.VMState));
                    return;
                }

                // in other cases,
                throw;
            }

            s = os.ServersProvider.WaitForServerActive(s.Id, 100);
        }
        public void StopServerTest()
        {
            var          osm    = new OpenStackMember(UserName, Password, TenantName, TenantId);
            SimpleServer ss     = osm.ListServers().FirstOrDefault(s => s.GetDetails().Status == ServerState.Active);
            Server       server = osm.GetServer(ss.Id);

            // check server status
            if (server.VMState == VirtualMachineState.Stopped)
            {
                if (server.Status != ServerState.Reboot)
                {
                    osm.StartServer(server.Id);
                }
                // wait for activate
                osm.ServersProvider.WaitForServerActive(server.Id);
                Trace.WriteLine(String.Format("server activated : {0}", DateTime.Now));

                // stop server
                bool b = osm.StopServer(server.Id);
                Assert.IsTrue(b);
                var vmState = server.VMState;
                Trace.WriteLine(string.Format("server stopped : {0}", DateTime.Now));
            }
            else
            {
                Trace.WriteLine(string.Format("server.Status : {0}", server.Status));
                Trace.WriteLine(string.Format("server.VMState : {0}", server.VMState));
            }
        }
        public void StartServerTest()
        {
            var          osm    = new OpenStackMember(UserName, Password, TenantName, TenantId);
            SimpleServer ss     = osm.ListServers().FirstOrDefault(s => s.GetDetails().Status == ServerState.Active);
            Server       server = osm.GetServer(ss.Id);

            // check server status
            if (server.Status == ServerState.Active && server.VMState == VirtualMachineState.Active)
            {
                osm.StopServer(server.Id);
                // wait for stop
                WaitForVMState(server.Id, new[] { VirtualMachineState.Stopped }, new[] { VirtualMachineState.Error });
                Trace.WriteLine(String.Format("server stopped : {0}", DateTime.Now));

                // start server
                bool b = osm.StartServer(server.Id);
                Assert.IsTrue(b);
                Trace.WriteLine(string.Format("server started : {0}", DateTime.Now));
            }
            else
            {
                Trace.WriteLine(string.Format("server.Status : {0}", server.Status));
                Trace.WriteLine(string.Format("server.VMState : {0}", server.VMState));
            }
        }
        public void GetServerTest_VM_Invalid()
        {
            var os = new OpenStackMember(UserName, Password, TenantName);

            // expect ItemNotFoundException
            Server server = os.GetServer(InvalidId);
        }
        public void AddInterfaceAttachmentTest_2Times_Sequencely()
        {
            var          osm = new OpenStackMember(UserName, Password, TenantName, TenantId);
            SimpleServer ss  = osm.ListServers().FirstOrDefault(s => s.GetDetails().Status == ServerState.ShutOff);

            if (ss != null)
            {
                Server server = osm.GetServer(ss.Id);
                Trace.WriteLine(string.Format("s.Status : {0}", server.Status));
                Trace.WriteLine(string.Format("s.VMState : {0}", server.VMState));

                string portId = GetInterfaceAttachmentIdByTesterName();

                // the first time
                InterfaceAttachment i = osm.AddInterfaceAttachment(ss.Id, portId);
                Assert.IsNotNull(i);
                Assert.IsNotNull(i.PortId);
                Assert.IsNotNull(i.NetId);
                Assert.IsNotNull(i.MacAddr);
                Assert.IsNotNull(i.PortState);
                Trace.WriteLine(string.Format("interface attachment added : {0}", DateTime.Now));
                Trace.WriteLine(string.Format("PortId : {0}, NetId : {1}, MacAddress : {2}, PortState : {3}", i.PortId, i.NetId, i.MacAddr, i.PortState));

                // the second time, expected BadServiceRequestException but ItemNotFundException occurs
                AssertCatch <BadServiceRequestException>(() => i = osm.AddInterfaceAttachment(ss.Id, portId));

                // delete interface
                Assert.IsTrue(osm.DeleteInterfaceAttachment(ss.Id, i.PortId));
            }
        }
        public void AddInterfaceAttachmentTest()
        {
            var          osm    = new OpenStackMember(UserName, Password, TenantName, TenantId);
            SimpleServer ss     = osm.ListServers().FirstOrDefault(s => s.GetDetails().Status == ServerState.Active);
            Server       server = osm.GetServer(ss.Id);

            Trace.WriteLine(string.Format("s.Status : {0}", server.Status));
            Trace.WriteLine(string.Format("s.VMState : {0}", server.VMState));

            // detach all port
            foreach (var _i in osm.ListInterfaceAttachments(ss.Id))
            {
                Assert.IsTrue(osm.DeleteInterfaceAttachment(ss.Id, _i.PortId));
            }

            // add interface
            var portId            = GetInterfaceAttachmentIdByTesterName();
            InterfaceAttachment i = osm.AddInterfaceAttachment(ss.Id, portId);

            try
            {
                Trace.WriteLine(string.Format("interface attachment added : {0}", DateTime.Now));
                Trace.WriteLine(string.Format("PortId : {0}, NetId : {1}, MacAddress : {2}, PortState : {3}", i.PortId, i.NetId, i.MacAddr, i.PortState));
                Assert.IsNotNull(i);
                Assert.IsNotNull(i.PortId);
                Assert.IsNotNull(i.NetId);
                Assert.IsNotNull(i.MacAddr);
                Assert.IsNotNull(i.PortState);
            }
            finally
            {
                // delete interface
                Assert.IsTrue(osm.DeleteInterfaceAttachment(ss.Id, i.PortId));
            }
        }
        public void RebuildServerTest_ImageId_Invalid()
        {
            var          os = new OpenStackMember(UserName, Password, TenantName, TenantId);
            SimpleServer ss = GetServerByNameOrCreate(TesterName);
            Server       s  = os.GetServer(ss.Id);

            // expect BadServiceRequestException
            Server rebuiltserver = os.RebuildServer(s.Id, InvalidId, "newPassword");
        }
        public void RebuildServerTest_ServerId_Invalid()
        {
            var          osm    = new OpenStackMember(UserName, Password, TenantName, TenantId);
            SimpleServer ss     = osm.ListServers().FirstOrDefault(s => s.GetDetails().Status == ServerState.Active);
            Server       server = osm.GetServer(ss.Id);

            // expect ItemNotFoundException
            Server rebuiltserver = osm.RebuildServer(InvalidId, server.Image.Id, "newPassword");
        }
        public void GetServerTest()
        {
            var          osm    = new OpenStackMember(UserName, Password, TenantName); // with tenant
            SimpleServer ss     = osm.ListServers().FirstOrDefault(s => s.GetDetails().Status == ServerState.Active);
            Server       server = osm.GetServer(ss.Id);

            Assert.IsTrue(server.Id == ss.Id);
            Assert.IsTrue(osm.DeleteServer(ss.Id));
        }
        public void ListServerVolumesTest()
        {
            var          osm = new OpenStackMember(UserName, Password, TenantName, TenantId);
            SimpleServer ss  = osm.ListServers().FirstOrDefault(s => s.GetDetails().Status == ServerState.Active);
            IEnumerable <ServerVolume> servervolumes = osm.ListServerVolumes(ss.Id);
            Server server = osm.GetServer(ss.Id);

            Trace.WriteLine(string.Format("Server Status : {0}", server.Status.Name));
            //if (s.Status.Name != "SHUTOFF")
            //    os.StopServer(s.Id);

            Assert.IsNotNull(servervolumes);
            Trace.WriteLine(String.Format("servervolumes count : {0}", servervolumes.Count()));
            foreach (var v in servervolumes)
            {
                Assert.IsNotNull(v);
                Trace.WriteLine(String.Format("server volume id : {0}", v.Id));
            }
        }
        public void ConfirmServerResizedTest_Revert_Without_Resize()
        {
            var          osm = new OpenStackMember(UserName, Password, TenantName, TenantId);
            SimpleServer ss  = osm.ListServers().FirstOrDefault(s => s.GetDetails().Status == ServerState.ShutOff);

            if (ss != null)
            {
                Server s = osm.GetServer(ss.Id);
                if (s.Status == ServerState.VerifyResize)
                {
                    osm.RevertResizeServer(s.Id);
                    Trace.WriteLine(String.Format("server reverted a resizing : {0}", DateTime.Now));
                    // wait for activate
                    s = osm.ServersProvider.WaitForServerActive(s.Id);
                    Trace.WriteLine(String.Format("server activated : {0}", DateTime.Now));
                }

                // expect ServiceConflictException
                bool b = osm.RevertResizeServer(s.Id);
            }
        }
        public void ResizeServerTest_same_flavorid()
        {
            var          osm = new OpenStackMember(UserName, Password, TenantName, TenantId);
            SimpleServer ss  = osm.ListServers().FirstOrDefault(s => s.GetDetails().Status == ServerState.ShutOff);

            if (ss != null)
            {
                Server s = osm.GetServer(ss.Id);
                if (s.Status == ServerState.VerifyResize)
                {
                    osm.RevertResizeServer(s.Id);
                    Trace.WriteLine(String.Format("server reverted a resizing : {0}", DateTime.Now));
                    // wait for activate
                    s = osm.ServersProvider.WaitForServerActive(s.Id);
                    Trace.WriteLine(String.Format("server activated : {0}", DateTime.Now));
                }

                // expect BadServiceRequestException
                bool b = osm.ResizeServer(s.Id, s.Flavor.Id, DiskConfiguration.FromName("AUTO"));
                Assert.IsTrue(b);
            }
        }
        public void ResizeServerTest()
        {
            var          osm    = new OpenStackMember(UserName, Password, TenantName, TenantId);
            SimpleServer ss     = osm.ListServers().FirstOrDefault(s => s.GetDetails().Status == ServerState.ShutOff);
            Server       server = osm.GetServer(ss.Id);

            if (server != null)
            {
                if (server.Status == ServerState.VerifyResize)
                {
                    osm.RevertResizeServer(server.Id);
                    Trace.WriteLine(String.Format("server reverted a resizing : {0}", DateTime.Now));
                    // wait for activate
                    server = osm.ServersProvider.WaitForServerActive(server.Id);
                    Trace.WriteLine(String.Format("server activated : {0}", DateTime.Now));
                }
                IEnumerable <Flavor> flavors = osm.ListFlavors();
                Flavor flavor1g = flavors.Where <Flavor>(x => x.Name == "g-1gb").First <Flavor>();
                Flavor flavor2g = flavors.Where <Flavor>(x => x.Name == "g-2gb").First <Flavor>();
                bool   b;
                try
                {
                    b = osm.ResizeServer(server.Id, (server.Flavor.Id == flavor1g.Id ? flavor2g.Id : flavor1g.Id), DiskConfiguration.FromName("AUTO"));
                    Assert.IsTrue(b);

                    Trace.WriteLine(String.Format("server requested a resizing : {0}", DateTime.Now));
                    Trace.WriteLine(String.Format("FlavorId : {0}", server.Flavor.Id));

                    // wait for request
                    server = osm.ServersProvider.WaitForServerState(server.Id, ServerState.VerifyResize, new[] { ServerState.Error, ServerState.Unknown, ServerState.Suspended });
                    Trace.WriteLine(String.Format("server status changed to {0} : {1}", server.Status, DateTime.Now));
                }
                catch (ServiceConflictException sce)
                {
                    // throwing ServiceConflictException when server is locked is designed.
                    Assert.IsTrue(sce.Message.Contains("locked"));
                }
            }
        }
        public void RestartServerTest()
        {
            var          osm    = new OpenStackMember(UserName, Password, TenantName, TenantId);
            SimpleServer ss     = osm.ListServers().FirstOrDefault(s => s.GetDetails().Status == ServerState.ShutOff);
            Server       server = osm.GetServer(ss.Id);

            // check server status
            if (server.Status != ServerState.Active)
            {
                if (server.Status != ServerState.Reboot)
                {
                    osm.StartServer(server.Id);
                }
                // wait for activate
                osm.ServersProvider.WaitForServerActive(server.Id);
                Trace.WriteLine(String.Format("server activated : {0}", DateTime.Now));
            }

            bool b = server.SoftReboot();

            Assert.IsTrue(b);
            Trace.WriteLine(string.Format("server restarted : {0}", DateTime.Now));
        }