public static void AmountOfAgentsWithBusinessTimesSet(int jobsCount, int agentsCount, int maxAgentsCount, string testDateTime, int expectedAmount)
        {
            TestInitilizers.InitAppSettingsForBusinessTimesTests();
            Clock.TestApi.Now = () => TestInitilizers.ParseDateTimeForTest(testDateTime);
            var amount = Decisions.HowMuchAgents(jobsCount, agentsCount, maxAgentsCount);

            Assert.AreEqual(expectedAmount, amount);
            Clock.TestApi.Reset();
        }
Esempio n. 2
0
        /// <summary>
        /// Here we will proceed working with VMSS ((de)provision additional agents, keep current agents count)
        /// </summary>
        /// <param name="onlineAgents"></param>
        /// <param name="maxAgentsInPool"></param>
        /// <param name="areWeCheckingToStartVmInVmss">Describes, which functions calls out - provisioning or deprovisioning</param>
        public static void WorkWithVmss(int onlineAgents, int maxAgentsInPool, bool areWeCheckingToStartVmInVmss)
        {
            //working with VMSS
            var vmss            = GetVirtualMachinesScaleSet(Properties.VmScaleSetResourceGroupName, Properties.VmScaleSetName);
            var virtualMachines = vmss.VirtualMachines.List()
                                  //there could be failed VMs during provisioning
                                  .Where(vm => !vm.Inner.ProvisioningState.Equals("Failed", StringComparison.OrdinalIgnoreCase))
                                  .Select(vmssVm => new ScaleSetVirtualMachineStripped
            {
                VmInstanceId    = vmssVm.InstanceId,
                VmName          = vmssVm.ComputerName,
                VmInstanceState = vmssVm.PowerState
            }).ToArray();

            //get jobs again to check, if we could deallocate a VM in VMSS
            //(if it is running a job - it is not wise to deallocate it)
            //since getting VMMS is potentially lengthy operation - we could need this)
            var currentJobs    = Checker.DataRetriever.GetRuningJobs(Properties.AgentsPoolId);
            var amountOfAgents = Decisions.HowMuchAgents(currentJobs.Length, onlineAgents, maxAgentsInPool);
            var addMoreAgents  = amountOfAgents > 0;

            if (amountOfAgents == 0)
            {
                //nevertheless - should we (de)provision agents: we are at boundaries
                Console.WriteLine("Should not add/remove more agents...");
                return;
            }

            //further I need to work with positive numbers only
            amountOfAgents = Math.Abs(amountOfAgents);

            if (addMoreAgents != areWeCheckingToStartVmInVmss)
            {
                //target event is not the same as source one
                return;
            }

            //I wish this record to be processed on it's own; it is just tracking
            RecordDataInTable(addMoreAgents, amountOfAgents);

            if (addMoreAgents)
            {
                AllocateVms(virtualMachines, amountOfAgents, vmss);
            }
            else
            {
                DeallocationWorkWithScaleSet(virtualMachines, currentJobs, vmss, amountOfAgents);
            }
        }
        public static void AmountOfAgents(int jobsCount, int agentsCount, int maxAgentsCount, int expectedAmount)
        {
            var amount = Decisions.HowMuchAgents(jobsCount, agentsCount, maxAgentsCount);

            Assert.AreEqual(expectedAmount, amount);
        }