Exemple #1
0
        public async Task CanPurgeQueue()
        {
            var queueName = TestConfig.GetName("send");
            await _managementClient.CreateQueueIfNotExistsAsync(queueName);

            var retryPolicy = new RetryExponential(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5), 10);
            var queueClient = new QueueClient(ConnectionString, queueName, receiveMode: ReceiveMode.PeekLock, retryPolicy: retryPolicy);

            await queueClient.SendAsync(new Message(Encoding.UTF8.GetBytes("Hej med dig min ven!")));

            await queueClient.SendAsync(new Message(Encoding.UTF8.GetBytes("Hej med dig min ven!")));

            await queueClient.SendAsync(new Message(Encoding.UTF8.GetBytes("Hej med dig min ven!")));

            await queueClient.SendAsync(new Message(Encoding.UTF8.GetBytes("Hej med dig min ven!")));

            await ManagementExtensions.PurgeQueue(ConnectionString, queueName);

            var client = new QueueClient(ConnectionString, queueName);
            var somethingWasReceived = new ManualResetEvent(false);

            client.RegisterMessageHandler(async(_, __) => somethingWasReceived.Set(), async _ => somethingWasReceived.Set());

            Assert.That(somethingWasReceived.WaitOne(TimeSpan.FromSeconds(1)), Is.False, $"Looks like a message was received from the queue '{queueName}' even though it was purged :o");
        }
Exemple #2
0
        public VirtualMachine CreateVirtualMachine(string name)
        {
            var scope      = ManagementExtensions.GetScope(this.Host, this.ConnectionOptions);
            var vmIdentity = scope.CreateVirtualMachine(name);

            return(new VirtualMachine(vmIdentity)
            {
                Name = name, State = VirtualMachineStates.Disabled
            });
        }
Exemple #3
0
 void PurgeQueue(string queueName)
 {
     try
     {
         AsyncHelpers.RunSync(async() =>
                              await ManagementExtensions.PurgeQueue(_connectionString, queueName, _cancellationToken).ConfigureAwait(false));
     }
     catch (Exception exception)
     {
         throw new ArgumentException($"Could not purge queue '{queueName}'", exception);
     }
 }
Exemple #4
0
        public IEnumerable <VirtualMachine> GetVirtualMachines()
        {
            var scope = ManagementExtensions.GetScope(this.Host, this.ConnectionOptions);

            return(scope.GetVirtualMachines()
                   .Select(
                       vm =>
                       new VirtualMachine(vm["name"].ToString())
            {
                Name = vm["ElementName"].ToString(),
                State = (VirtualMachineStates)(UInt16)vm["EnabledState"]
            })
                   .ToList());
        }
Exemple #5
0
        public async Task CanSendAndReceiveMessage()
        {
            var queueName = TestConfig.GetName("send-receive");
            await _managementClient.CreateQueueIfNotExistsAsync(queueName);

            var retryPolicy = new RetryExponential(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5), 10);
            var queueClient = new QueueClient(ConnectionString, queueName, receiveMode: ReceiveMode.PeekLock, retryPolicy: retryPolicy);

            await ManagementExtensions.PurgeQueue(ConnectionString, queueName);

            await queueClient.SendAsync(new Message(Encoding.UTF8.GetBytes("Hej med dig min ven! Det spiller!")));

            var messageReceiver = new MessageReceiver(ConnectionString, queueName, receiveMode: ReceiveMode.PeekLock);

            var message = await messageReceiver.ReceiveAsync(TimeSpan.FromSeconds(2));

            Assert.That(message, Is.Not.Null);

            var text = Encoding.UTF8.GetString(message.Body);

            Assert.That(text, Is.EqualTo("Hej med dig min ven! Det spiller!"));
        }
Exemple #6
0
        //https://msdn.microsoft.com/en-us/library/windows/desktop/aa394372%28v=vs.85%29.aspx
        private static ProcessInfo ManagementObjectToProcessInfo(ManagementObject queryObj, Process process)
        {
            var result = new ProcessInfo
            {
                Name          = queryObj.TryGetProperty <string>("Name"),
                PrivateBytes  = process?.PrivateMemorySize64 ?? 0,
                WorkingSet    = (long)queryObj.TryGetProperty <ulong>("WorkingSetSize"),
                Id            = (int)queryObj.TryGetProperty <uint>("ProcessId"),
                ParentProcess = (int)queryObj.TryGetProperty <uint>("ParentProcessId"),
                StartTime     =
                    (ManagementExtensions.ToDateTimeSafe(queryObj.TryGetProperty <string>("CreationDate")) ??
                     ExceptionUtilities.EatExceptions(() => process?.StartTime) ?? DateTime.MinValue)
                    .ToUniversalTime(),
                Filename         = queryObj.TryGetProperty <string>("ExecutablePath"),
                CommandLine      = queryObj.TryGetProperty <string>("CommandLine"),
                MainWindowHandle = (long)(process?.MainWindowHandle ?? IntPtr.Zero)
            };

            ApplyProcessInformation(process, result, queryObj.TryGetProperty <string>("ExecutablePath"));

            return(result);
        }
Exemple #7
0
        public IEnumerable <string> GetVirtualSwitches()
        {
            var scope = ManagementExtensions.GetScope(this.Host, this.ConnectionOptions);

            return(scope.GetVirtualSwitchNames());
        }
Exemple #8
0
        public void StartVirtualMachine(VirtualMachine virtualMachine)
        {
            var scope = ManagementExtensions.GetScope(this.Host, this.ConnectionOptions);

            scope.StartVirtualMachine(virtualMachine.VmIdentity);
        }
Exemple #9
0
        public void SetVhd(VirtualMachine virtualMachine, string vhdPath)
        {
            var scope = ManagementExtensions.GetScope(this.Host, this.ConnectionOptions);

            scope.SetVhd(virtualMachine.VmIdentity, vhdPath);
        }
Exemple #10
0
        public void SetDynamicRam(VirtualMachine virtualMachine, int ramSize = 4096, int?reservation = 1024, int?limit = 8192)
        {
            var scope = ManagementExtensions.GetScope(this.Host, this.ConnectionOptions);

            scope.SetDynamicRam(virtualMachine.VmIdentity, ramSize, reservation, limit);
        }
Exemple #11
0
        public void AddNetworkAdapter(VirtualMachine virtualMachine, string switchName)
        {
            var scope = ManagementExtensions.GetScope(this.Host, this.ConnectionOptions);

            scope.AddNetworAdapter(virtualMachine.VmIdentity, switchName);
        }
Exemple #12
0
        private List <DeviceInfo> GetAllDevices()
        {
            var list = new List <DeviceInfo>();

            using (
                var searcher = new ManagementObjectSearcher(@"\\" + Environment.MachineName + @"\root\CIMV2",
                                                            "Select * from Win32_PnPEntity"))
                using (var collection = searcher.Get())
                {
                    foreach (var managementObject in collection.Cast <ManagementObject>())
                    {
                        if (managementObject.TryGetProperty <string>("DeviceId") == @"HTREE\ROOT\0")
                        {
                            continue;
                        }

                        var device = new DeviceInfo
                        {
                            Name         = managementObject.TryGetProperty <string>("Caption"),
                            DeviceId     = managementObject.TryGetProperty <string>("DeviceId"),
                            Description  = managementObject.TryGetProperty <string>("Description"),
                            Manufacturer = managementObject.TryGetProperty <string>("Manufacturer"),
                            StatusCode   = managementObject.TryGetProperty <uint>("ConfigManagerErrorCode")
                        };
                        var hardwareIds = managementObject.TryGetProperty <string[]>("HardWareID");
                        if (hardwareIds?.Length > 0)
                        {
                            device.HardwareId = hardwareIds[0];
                        }

                        list.Add(device);
                        var  classGuidString = managementObject.TryGetProperty <string>("ClassGuid");
                        Guid classGuid;
#if NET35
                        try
                        {
                            classGuid = new Guid(classGuidString);
                        }
                        catch (Exception)
                        {
                            classGuid = Guid.Empty;
                        }
#else
                        if (!Guid.TryParse(classGuidString, out classGuid))
                        {
                            classGuid = Guid.Empty;
                        }
#endif
                        device.Category = DeviceCategory.None;


                        foreach (var value in (DeviceCategory[])Enum.GetValues(typeof(DeviceCategory)))
                        {
                            if (value.GetAttributeOfType <DeviceCategoryGuidAttribute>().Guid == classGuid)
                            {
                                device.Category = value;
                                break;
                            }
                        }

                        if (device.Category == DeviceCategory.None)
                        {
                            device.CustomCategory = managementObject.TryGetProperty <string>("PNPClass");
                        }
                    }
                }

            using (
                var searcher = new ManagementObjectSearcher(@"\\" + Environment.MachineName + @"\root\CIMV2",
                                                            "Select * from Win32_PnPSignedDriver"))
                using (var collection = searcher.Get())
                {
                    foreach (var managementObject in collection.Cast <ManagementObject>())
                    {
                        var deviceId = managementObject.TryGetProperty <string>("DeviceID");
                        var device   = list.FirstOrDefault(x => x.DeviceId == deviceId);
                        if (device != null)
                        {
                            device.DriverFriendlyName = managementObject.TryGetProperty <string>("FriendlyName");
                            var buildDate = managementObject.TryGetProperty <string>("DriverDate");
                            if (buildDate != null)
                            {
                                device.DriverBuildDate = (ManagementExtensions.ToDateTimeSafe(buildDate) ?? DateTime.MinValue).ToUniversalTime();
                            }
                            device.DriverDescription = managementObject.TryGetProperty <string>("Description");
                            var installDate = managementObject.TryGetProperty <string>("InstallDate");
                            if (installDate != null)
                            {
                                device.DriverInstallDate =
                                    (ManagementExtensions.ToDateTimeSafe(installDate) ?? DateTime.MinValue).ToUniversalTime();
                            }
                            device.DriverName         = managementObject.TryGetProperty <string>("DriverName");
                            device.DriverProviderName = managementObject.TryGetProperty <string>("DriverProviderName");
                            device.DriverSigner       = managementObject.TryGetProperty <string>("Signer");
                            device.DriverVersion      = managementObject.TryGetProperty <string>("DriverVersion");
                            device.DriverInfName      = managementObject.TryGetProperty <string>("InfName");
                        }
                    }
                }

            return(list);
        }
Exemple #13
0
 internal static VirtualDiskSetInformation Parse(string embeddedInstance) => ManagementExtensions.Parse <VirtualDiskSetInformation>(embeddedInstance);