/// <summary>
 ///     This method kicks off the process of the server sending out the tests
 /// </summary>
 /// <param name="testSuite"></param>
 /// <returns></returns>
 public async Task BeginTestRun(TestSuite testSuite)
 {
     //first thing we do is make sure all test runs have ips, if they dont we set them
     //up in AWS, this is very tighly coupled at the moment. we get the instance ids back so we 
     //can close them later
     Log4NetLogger.LogEntry(GetType(), "BeginTestRun", "starting test run", LoggerLevel.Info);
     var ec2Client = new AmazonEC2Client(RegionEndpoint.EUWest1);
     Log4NetLogger.LogEntry(GetType(), "BeginTestRun", "created EC2 client, now assiging ips", LoggerLevel.Info);
     List<string> instanceIds = AssignAgentIps(ec2Client, testSuite);
     Log4NetLogger.LogEntry(GetType(), "BeginTestRun",
         "assigned ips if there were any required, about to run tests", LoggerLevel.Info);
     //now we need to send our dlls to the agents
     UploadDllsToAgents(testSuite);
     //once we are all good we run the tests. 
     await Run(testSuite);
     Log4NetLogger.LogEntry(GetType(), "BeginTestRun", "tests have finished running, now terminate any agents",
         LoggerLevel.Info);
     //we return null from AssignAgentIps if we dont need any IPs, bad programming but it will do 
     //for now.
     if (instanceIds != null)
     {
         Log4NetLogger.LogEntry(GetType(), "BeginTestRun", "terminating agents", LoggerLevel.Info);
         //terminate the agents LIKE A BAUSSSSSSSSSSS
         TerminateAgents(ec2Client, instanceIds);
         Log4NetLogger.LogEntry(GetType(), "BeginTestRun", "terminated agents", LoggerLevel.Info);
     }
 }
 private async Task Run(TestSuite testSuite)
 {
     Log4NetLogger.LogEntry(GetType(), "Run", "starting to send tests to agents", LoggerLevel.Info);
     var tasks = new Task[testSuite.Tests.Count];
     for (int i = 0; i < testSuite.Tests.Count; i++)
     {
         int copy = i;
         Task task = Task.Run(() => PostToAgent(testSuite.Tests[copy].Agent, testSuite.Tests[copy].TestRun));
         tasks[i] = task;
     }
     Log4NetLogger.LogEntry(GetType(), "Run", "finished sending tests to agents", LoggerLevel.Info);
     //wait for all the tasks to finish
     await Task.WhenAll(tasks);
     Log4NetLogger.LogEntry(GetType(), "Run", "all tasks finished returning", LoggerLevel.Info);
 }
        public void UploadDllsToAgents(TestSuite testSuite)
        {
            if (testSuite.DllsThatNeedUploadingToAgent.Count > 0)
            {
                IEnumerable<string> agentIps = testSuite.Tests.Select(x => x.Agent);
                foreach (string agentIp in agentIps)
                {
                    foreach (string dll in testSuite.DllsThatNeedUploadingToAgent.Distinct())
                    {
                        string fileName = GetFileName(dll);

                        UploadFile(fileName, string.Format("http://{0}:9999", agentIp), dll);
                    }
                }
            }
        }
 public void SetUpTestRunIdentifier(TestSuite testSuite, Guid guid)
 {
     foreach (Test test in testSuite.Tests)
     {
         test.TestRun.TestRunIdentifier = guid;
     }
 }
        public List<string> AssignAgentIps(AmazonEC2Client ec2Client, TestSuite testSuite)
        {
            int requiredInstances = testSuite.Tests.Count(testToGiveIp => string.IsNullOrWhiteSpace(testToGiveIp.Agent));

            if (requiredInstances > 0)
            {
                var runInstancesRequest = new RunInstancesRequest
                {
                    ImageId = "ami-df844ba8",
                    InstanceType = "t1.micro",
                    MinCount = requiredInstances,
                    MaxCount = requiredInstances,
                    KeyName = "Fourth"
                };
                runInstancesRequest.SecurityGroups.Add("Controller");
                RunInstancesResponse runResponse = ec2Client.RunInstances(runInstancesRequest);
                List<Instance> instances = runResponse.Reservation.Instances;
                List<string> instanceIDs = instances.Select(item => item.InstanceId).ToList();
                WaitForInstancesToBeRunning(ec2Client, instanceIDs);
                var instancesRequest = new DescribeInstancesRequest {InstanceIds = instanceIDs};
                DescribeInstancesResponse statusResponse = ec2Client.DescribeInstances(instancesRequest);
                List<string> ipAddresses =
                    statusResponse.Reservations[0].Instances.Select(x => x.PublicIpAddress).ToList();
                //we now have our running instances and we need to assign the ips to our tests
                foreach (Test test in testSuite.Tests.Where(test => string.IsNullOrWhiteSpace(test.Agent)))
                {
                    //assign the first free Id
                    test.Agent = ipAddresses.First();
                    //then remove it from the list
                    ipAddresses.RemoveAt(0);
                }

                //now we need to make sure all instances are ready
                MakeSureAgentsCanBeUsed(ec2Client, instanceIDs);
                return instanceIDs;
            }
            return null;
        }