/// <summary> kicks off batch specified. </summary>
        /// <param name="batch"> A Test batch objectame of batch file to load and run. </param>
        /// <param name="chassisManagerEndPoint"> Chassis Manager endpoint uri. </param>
        /// <param name="globalParameters"> Global Parameters. </param>
        public void RunFrameworkBatch(TestBatch batch, string chassisManagerEndPoint, Parameters globalParameters, string userName, string userPassword)
        {
            var batchResults = new ResultOfTestBatch(batch.Name, chassisManagerEndPoint);

            try
            {
                batch.Run(globalParameters, batchResults, userName, userPassword);
            }
            catch (OperationCanceledException)
            {
                Console.WriteLine("\n********** Batch has Ended.\n");
            }
            finally
            {
                batchResults.Save();
            }
        }
        /// <summary> Creates a sample Batch File. </summary>
        /// <param name="batchRunResultFile"> Name of the file where sample batch run is to be saved. </param>        
        public static void CreateSampleBatchRunResultFile(string batchRunResultFile)
        {
            var batchRun = new ResultOfTestBatch(batchRunResultFile, "http://MyChassis:8000")
            {
                BatchStartTime = DateTime.UtcNow,
                BatchEndTime = DateTime.UtcNow,
                BatchState = TestRunState.RanSuccessfully
            };

            batchRun.TestResults.Add(new ResultOfTest()
            {
                Name = "MyAPi1",
                IterationsExecutedSuccessfully = 1,
                RestUri = "http://MyChassis:8000/api1",
                SequenceInstance = 1,
                SequenceName = "MyTestSequence",
                StartTime = DateTime.UtcNow,
                State = TestRunState.RanSuccessfully,
                TotalExecutionTime = TimeSpan.FromTicks(1)
            });

            var start = DateTime.UtcNow;

            batchRun.TestResults.Add(new ResultOfTest()
            {
                Name = "MyAPi2",
                IterationsExecutedSuccessfully = 1,
                RestUri = "http://MyChassis:8000/api2",
                SequenceInstance = 1,
                SequenceName = "MyTestSequence",
                StartTime = start,
                State = TestRunState.RanSuccessfully,
                TotalExecutionTime = DateTime.UtcNow.Subtract(start),
            });

            Helper.SaveToFile(batchRun, batchRunResultFile);
        }
        /// <summary> Runs all tests. </summary>
        /// <param name="globalParameters"> Global Parameters. </param>
        /// <param name="batchResults"> Results of batch. </param>
        /// <returns> Result of Test Batch. </returns>
        public void Run(Parameters globalParameters, ResultOfTestBatch batchResults, string userName, string userPassword)
        {
            if (this.GlobalParameters == null)
            {
                this.GlobalParameters = globalParameters;
            }

            var cts = new CancellationTokenSource();
            var parallelOptions = new ParallelOptions()
            {
                MaxDegreeOfParallelism = (int)this.MaximumParallelThreads,
                CancellationToken = cts.Token
            };

            var includedApis = this.TestSequences.SelectMany(ts => ts.Tests).Select(test => test.Name).Distinct();
            var implementedApis = Helper.GetChassisManagerApiList();
            batchResults.ExcludedApis = implementedApis.Where(api => !includedApis.Contains(api));
            batchResults.NonExistingApis = includedApis.Where(api => !implementedApis.Contains(api));
            batchResults.BatchStartTime = DateTime.UtcNow;
            batchResults.BatchState = TestRunState.Running;
            batchResults.Save();

            var keepLooping = false;
            Timer loopingtimer;
            if (this.Duration.HasValue)
            {
                keepLooping = true;
                loopingtimer = new Timer(this.Duration.Value.TotalMilliseconds);
                loopingtimer.Elapsed += delegate(object sender, ElapsedEventArgs args)
                {
                    keepLooping = false;
                    Console.WriteLine("Batch time expired, Ending Batch.");
                    cts.Cancel();
                };

                loopingtimer.Start();
            }

            Timer saveTimer = null;
            if (this.SaveResultFrequency.HasValue)
            {
                saveTimer = new Timer(this.SaveResultFrequency.Value.TotalMilliseconds);
                saveTimer.Elapsed += delegate(object sender, ElapsedEventArgs args)
                {
                    batchResults.Save();
                };

                saveTimer.Start();
            }

            //// Set credentials to run under for each sequence.
            //Parallel.ForEach(this.TestSequences,
            //        ts =>
            //            {
            //                ts.RunAsUserCredentials = new List<UserCredential>();
            //                if (this.UserCredentials != null && !string.IsNullOrWhiteSpace(ts.RunAsRoles))
            //                {
            //                    var runAsUsers = ts.RunAsRoles.Split(',');
            //                    if (runAsUsers.Contains("*"))
            //                    {
            //                        ts.RunAsUserCredentials.AddRange(this.UserCredentials);
            //                    }
            //                    else
            //                    {
            //                        ts.RunAsUserCredentials.AddRange(this.UserCredentials.Where(
            //                            crd => runAsUsers.Contains(crd.Role, StringComparer.InvariantCultureIgnoreCase)));
            //                    }
            //                }

            //                if (!ts.RunAsUserCredentials.Any())
            //                {
            //                    ts.RunAsUserCredentials.Add(null); // Force use of default credentials if none specified;
            //                }
            //            });

            do
            {
                var sequencesToRun = this.TestSequences.ToList();
                if (this.ShuffleSequences.HasValue && this.ShuffleSequences.Value)
                {
                    sequencesToRun.Shuffle();
                }

                var loopResult = Parallel.ForEach(
                    sequencesToRun,
                    parallelOptions,
                    ts =>
                    {
                        var tsResults = ts.Run(
                            batchResults.ChassisManagerEndPoint,
                            this.GlobalParameters,
                            this.ApiSla,
                            userName,
                            userPassword);
                        lock (batchResults.TestResults)
                        {
                            batchResults.TestResults.AddRange(tsResults);
                        }

                        batchResults.Save();
                    });

                batchResults.BatchState = loopResult.IsCompleted &&
                                          batchResults.TestResults.All(t => t.State == TestRunState.RanSuccessfully)
                                          ? TestRunState.RanSuccessfully
                                          : TestRunState.RunFailed;
            }
            while (keepLooping);

            batchResults.BatchEndTime = DateTime.UtcNow;
        }
コード例 #4
0
        /// <summary> Runs all tests. </summary>
        /// <param name="globalParameters"> Global Parameters. </param>
        /// <param name="batchResults"> Results of batch. </param>
        /// <returns> Result of Test Batch. </returns>
        public void Run(Parameters globalParameters, ResultOfTestBatch batchResults, string userName, string userPassword)
        {
            if (this.GlobalParameters == null)
            {
                this.GlobalParameters = globalParameters;
            }

            var cts             = new CancellationTokenSource();
            var parallelOptions = new ParallelOptions()
            {
                MaxDegreeOfParallelism = (int)this.MaximumParallelThreads,
                CancellationToken      = cts.Token
            };

            var includedApis    = this.TestSequences.SelectMany(ts => ts.Tests).Select(test => test.Name).Distinct();
            var implementedApis = Helper.GetChassisManagerApiList();

            batchResults.ExcludedApis    = implementedApis.Where(api => !includedApis.Contains(api));
            batchResults.NonExistingApis = includedApis.Where(api => !implementedApis.Contains(api));
            batchResults.BatchStartTime  = DateTime.UtcNow;
            batchResults.BatchState      = TestRunState.Running;
            batchResults.Save();

            var   keepLooping = false;
            Timer loopingtimer;

            if (this.Duration.HasValue)
            {
                keepLooping           = true;
                loopingtimer          = new Timer(this.Duration.Value.TotalMilliseconds);
                loopingtimer.Elapsed += delegate(object sender, ElapsedEventArgs args)
                {
                    keepLooping = false;
                    Console.WriteLine("Batch time expired, Ending Batch.");
                    cts.Cancel();
                };

                loopingtimer.Start();
            }

            Timer saveTimer = null;

            if (this.SaveResultFrequency.HasValue)
            {
                saveTimer          = new Timer(this.SaveResultFrequency.Value.TotalMilliseconds);
                saveTimer.Elapsed += delegate(object sender, ElapsedEventArgs args)
                {
                    batchResults.Save();
                };

                saveTimer.Start();
            }

            //// Set credentials to run under for each sequence.
            //Parallel.ForEach(this.TestSequences,
            //        ts =>
            //            {
            //                ts.RunAsUserCredentials = new List<UserCredential>();
            //                if (this.UserCredentials != null && !string.IsNullOrWhiteSpace(ts.RunAsRoles))
            //                {
            //                    var runAsUsers = ts.RunAsRoles.Split(',');
            //                    if (runAsUsers.Contains("*"))
            //                    {
            //                        ts.RunAsUserCredentials.AddRange(this.UserCredentials);
            //                    }
            //                    else
            //                    {
            //                        ts.RunAsUserCredentials.AddRange(this.UserCredentials.Where(
            //                            crd => runAsUsers.Contains(crd.Role, StringComparer.InvariantCultureIgnoreCase)));
            //                    }
            //                }

            //                if (!ts.RunAsUserCredentials.Any())
            //                {
            //                    ts.RunAsUserCredentials.Add(null); // Force use of default credentials if none specified;
            //                }
            //            });

            do
            {
                var sequencesToRun = this.TestSequences.ToList();
                if (this.ShuffleSequences.HasValue && this.ShuffleSequences.Value)
                {
                    sequencesToRun.Shuffle();
                }

                var loopResult = Parallel.ForEach(
                    sequencesToRun,
                    parallelOptions,
                    ts =>
                {
                    var tsResults = ts.Run(
                        batchResults.ChassisManagerEndPoint,
                        this.GlobalParameters,
                        this.ApiSla,
                        userName,
                        userPassword);
                    lock (batchResults.TestResults)
                    {
                        batchResults.TestResults.AddRange(tsResults);
                    }

                    batchResults.Save();
                });

                batchResults.BatchState = loopResult.IsCompleted &&
                                          batchResults.TestResults.All(t => t.State == TestRunState.RanSuccessfully)
                                          ? TestRunState.RanSuccessfully
                                          : TestRunState.RunFailed;
            }while (keepLooping);

            batchResults.BatchEndTime = DateTime.UtcNow;
        }