private DeployAgentResult DeployInternetZoneScript(PowerShellExecutionPolicyBehaviour executionPolicyBehaviour)
        {
            DeployAgentResult result;

            using (var scriptFile = new TemporaryFile(".ps1", PowerShellScripts.GetExecutionPolicyScript))
            {
                ApplyInternetZoneIdentifier(scriptFile.FileInfo.FullName);

                var testDeployData = new DeployAgentData
                {
                    NewQuality             = "Released",
                    OriginalQuality        = null,
                    DeployScriptFile       = scriptFile.FileInfo.FullName,
                    DeployScriptRoot       = scriptFile.FileInfo.DirectoryName,
                    DeployScriptParameters = new List <DeployScriptParameter>(),
                    Tfs2008BuildDetail     = new StubBuildDetail()
                };

                var psAgent = new LocalPowerShellDeployAgent();
                psAgent.ExecutionPolicyBehaviour = executionPolicyBehaviour;

                result = psAgent.Deploy(testDeployData);
            }
            return(result);
        }
Ejemplo n.º 2
0
            public DeployAgentResult Deploy(DeployAgentData deployAgentData)
            {
                lock (_lock)
                {
                    if (_executing)
                    {
                        _wasParallel = true;
                    }
                    _executing = true;
                }

                var expire = DateTime.UtcNow.AddSeconds(3);

                while (!WasParallel && DateTime.UtcNow < expire)
                {
                    Thread.Sleep(500);
                }

                lock (_lock)
                {
                    _executing = false;
                }

                _hasExecuted = true;
                return(new DeployAgentResult());
            }
Ejemplo n.º 3
0
        public void LocalPowerShellDeployAgent_should_finish_cleanly_when_scripts_leave_background_jobs_running()
        {
            // Arrange
            var deploymentEventRecorder = MockRepository.GenerateStub <IDeploymentEventRecorder>();

            DeployAgentResult result;

            using (var scriptFile = new TemporaryFile(".ps1", @"Start-Job { Get-Date; Start-Sleep -Seconds 1} ; 'ExpectedOutput'"))
            {
                var testDeployData = new DeployAgentData
                {
                    NewQuality             = "Released",
                    OriginalQuality        = null,
                    DeployScriptFile       = scriptFile.FileInfo.Name,
                    DeployScriptRoot       = scriptFile.FileInfo.DirectoryName,
                    DeployScriptParameters = new List <DeployScriptParameter>(),
                    TfsBuildDetail         = new BuildDetail()
                };

                var agent = new LocalPowerShellDeployAgent(deploymentEventRecorder);

                // Act
                result = agent.Deploy(testDeployData);
            }

            // Assert
            Assert.IsFalse(result.HasErrors, "HasErrors: {0}", result.Output);
            StringAssert.Contains(result.Output, "ExpectedOutput", "Output");
        }
Ejemplo n.º 4
0
        public void LocalPowerShellDeployAgent_should_return_output_prior_to_a_script_error()
        {
            // Arrange
            var deploymentEventRecorder = MockRepository.GenerateStub <IDeploymentEventRecorder>();

            DeployAgentResult result;

            using (var scriptFile = new TemporaryFile(".ps1", "'Output this first'\nthrow 'fail'"))
            {
                var testDeployData = new DeployAgentData
                {
                    NewQuality             = "Released",
                    OriginalQuality        = null,
                    DeployScriptFile       = scriptFile.FileInfo.Name,
                    DeployScriptRoot       = scriptFile.FileInfo.DirectoryName,
                    DeployScriptParameters = new List <DeployScriptParameter>(),
                    TfsBuildDetail         = new BuildDetail()
                };

                var agent = new LocalPowerShellDeployAgent(deploymentEventRecorder);

                // Act
                result = agent.Deploy(testDeployData);
            }

            // Assert
            Assert.IsTrue(result.HasErrors, "HasErrors");
            StringAssert.Contains(result.Output, "Output this first");
        }
Ejemplo n.º 5
0
        public void LocalPowerShellDeployAgent_should_support_backticks_in_script_path()
        {
            // Arrange
            var deploymentEventRecorder = MockRepository.GenerateStub <IDeploymentEventRecorder>();

            DeployAgentResult result;
            const string      subDirectory = "back`tick";

            using (var scriptFile = new TemporaryFile(".ps1", @"'Script path is ' + $MyInvocation.MyCommand.Path", subDirectory))
            {
                var testDeployData = new DeployAgentData
                {
                    NewQuality             = "Released",
                    OriginalQuality        = null,
                    DeployScriptFile       = scriptFile.FileInfo.Name,
                    DeployScriptRoot       = scriptFile.FileInfo.DirectoryName,
                    DeployScriptParameters = new List <DeployScriptParameter>(),
                    TfsBuildDetail         = new BuildDetail()
                };

                var agent = new LocalPowerShellDeployAgent(deploymentEventRecorder);

                // Act
                result = agent.Deploy(testDeployData);
            }

            // Assert
            Assert.IsFalse(result.HasErrors, "HasErrors: {0}", result.Output);
            StringAssert.Contains(result.Output, "Script path is", "Output prefix");
            StringAssert.Contains(result.Output, subDirectory, "Output subdirectory");
        }
Ejemplo n.º 6
0
        public void LocalPowerShellDeployAgent_should_unload_assemblies_loaded_by_scripts()
        {
            // Arrange
            var deploymentEventRecorder = MockRepository.GenerateStub <IDeploymentEventRecorder>();

            DeployAgentResult result;

            using (var scriptFile = new TemporaryFile(".ps1", Resource.AsString("LoadSystemWebAssemblyScript.ps1")))
            {
                var testDeployData = new DeployAgentData
                {
                    NewQuality             = "Released",
                    OriginalQuality        = null,
                    DeployScriptFile       = scriptFile.FileInfo.Name,
                    DeployScriptRoot       = scriptFile.FileInfo.DirectoryName,
                    DeployScriptParameters = new List <DeployScriptParameter>(),
                    TfsBuildDetail         = new BuildDetail()
                };

                var agent = new LocalPowerShellDeployAgent(deploymentEventRecorder);

                // Act
                result = agent.Deploy(testDeployData);
            }

            // Assert
            Assert.IsFalse(result.HasErrors, "Test script failed.");

            var systemWebAssembly = AppDomain.CurrentDomain.GetAssemblies().Where(a => a.GetName().Name == "System.Web").SingleOrDefault();

            Assert.IsNull(systemWebAssembly, "Assembly was not unloaded.");
        }
Ejemplo n.º 7
0
        public void LocalPowerShellDeployAgent_should_expose_build_process_server_path_to_scripts()
        {
            // Arrange
            var deploymentEventRecorder = MockRepository.GenerateStub <IDeploymentEventRecorder>();

            DeployAgentResult result;

            using (var scriptFile = new TemporaryFile(".ps1", @"$TfsDeployerBuildDetail.BuildDefinition.Process.ServerPath"))
            {
                var testDeployData = new DeployAgentData
                {
                    NewQuality             = "Released",
                    OriginalQuality        = null,
                    DeployScriptFile       = scriptFile.FileInfo.Name,
                    DeployScriptRoot       = scriptFile.FileInfo.DirectoryName,
                    DeployScriptParameters = new List <DeployScriptParameter>(),
                    TfsBuildDetail         = new BuildDetail {
                        BuildDefinition = { Process = { ServerPath = "$/foo.xaml" } }
                    }
                };

                var agent = new LocalPowerShellDeployAgent(deploymentEventRecorder);

                // Act
                result = agent.Deploy(testDeployData);
            }

            // Assert
            Assert.IsFalse(result.HasErrors, "HasErrors");
            StringAssert.Contains(result.Output, "$/foo.xaml");
        }
        public void AgentShouldKillScriptsThatExceedTimeoutConstraints()
        {
            // Arrange
            var          agent = new BatchFileDeployAgent();
            const string deployScriptFilename  = "SlowDeployment.bat";
            var          deployScriptDirectory = Directory.GetCurrentDirectory();

            File.WriteAllText(deployScriptFilename, @"ping -n 10 127.0.0.1");    // script that just takes about nn seconds to execute

            // required because the deploy agent builds its own parameter list
            var tfsBuildDetail = new BuildDetail
            {
                DropLocation = string.Empty,
                BuildNumber  = string.Empty,
            };

            var deployAgentData = new DeployAgentData
            {
                DeployScriptRoot       = deployScriptDirectory,
                DeployScriptFile       = deployScriptFilename,
                Timeout                = TimeSpan.FromSeconds(2),
                TfsBuildDetail         = tfsBuildDetail,
                DeployScriptParameters = Enumerable.Empty <DeployScriptParameter>().ToList(),
            };

            // Act
            var deployAgentResult = agent.Deploy(deployAgentData);

            // Assert
            Assert.IsTrue(deployAgentResult.Output.Contains(@"Pinging 127.0.0.1"));
            Assert.IsFalse(deployAgentResult.Output.Contains(@"Ping statistics"));

            // Clean up
            File.Delete(deployScriptFilename);
        }
Ejemplo n.º 9
0
        private static DeployAgentData CreateDeployAgentData(string directory, Mapping mapping, BuildInformation buildInfo)
        {
            var data = new DeployAgentData
            {
                NewQuality             = mapping.NewQuality,
                OriginalQuality        = mapping.OriginalQuality,
                DeployServer           = mapping.Computer,
                DeployScriptFile       = mapping.Script,
                DeployScriptRoot       = directory,
                DeployScriptParameters = CreateParameters(mapping.ScriptParameters),
                Tfs2005BuildData       = buildInfo.Data,
                Tfs2008BuildDetail     = buildInfo.Detail
            };

            return(data);
        }
Ejemplo n.º 10
0
        public void MappingExecutor_should_pass_deployment_id_to_deploy_agent_via_deploy_agent_data()
        {
            // Arrange
            const int deploymentId = 23;

            DeployAgentData deployData  = null;
            var             deployAgent = MockRepository.GenerateStub <IDeployAgent>();

            deployAgent.Stub(o => o.Deploy(null))
            .IgnoreArguments()
            .Return(new DeployAgentResult())
            .WhenCalled(o => deployData = (DeployAgentData)o.Arguments[0]);

            var deployAgentProvider = MockRepository.GenerateStub <IDeployAgentProvider>();

            deployAgentProvider.Stub(o => o.GetDeployAgent(null))
            .IgnoreArguments()
            .Return(deployAgent);

            var deploymentFolderSource = MockRepository.GenerateStub <IDeploymentFolderSource>();

            var mapping = new Mapping();

            var deploymentEventRecorder = MockRepository.GenerateStub <IDeploymentEventRecorder>();

            deploymentEventRecorder.Stub(o => o.RecordQueued(0, null, null))
            .IgnoreArguments()
            .Return(deploymentId);

            var namedLockSet = new NamedLockSet();

            var mappingExecutor  = new MappingExecutor(deploymentEventRecorder, deployAgentProvider, deploymentFolderSource, namedLockSet);
            var postDeployAction = MockRepository.GenerateStub <IPostDeployAction>();

            var buildDetail = new BuildDetail();

            var statusChanged = new BuildStatusChangeEvent {
                StatusChange = new Change()
            };

            // Act
            mappingExecutor.Execute(statusChanged, buildDetail, mapping, postDeployAction, deploymentId);

            // Assert
            Assert.AreEqual(deploymentId, deployData.DeploymentId);
        }
        private static DeployAgentResult DeployFailingPowerShellScript()
        {
            using (var scriptFile = new TemporaryFile(".ps1", PowerShellScripts.FailingPowerShellScript))
            {
                var testDeployData = new DeployAgentData
                {
                    NewQuality             = "Released",
                    OriginalQuality        = null,
                    DeployScriptFile       = scriptFile.FileInfo.Name,
                    DeployScriptRoot       = scriptFile.FileInfo.DirectoryName,
                    DeployScriptParameters = new List <DeployScriptParameter>(),
                    TfsBuildDetail         = new BuildDetail()
                };

                var psAgent = new LocalPowerShellDeployAgent();
                return(psAgent.Deploy(testDeployData));
            }
        }
Ejemplo n.º 12
0
        public void OutOfProcessPowerShellDeployAgent_should_expose_live_output_to_the_deployment_event_recorder()
        {
            var deploymentEventRecorder = new StubDeploymentEventRecorder();

            using (var scriptFile = new TemporaryFile(".ps1", "'hello there'"))
            {
                var data = new DeployAgentData
                {
                    DeployScriptFile = scriptFile.FileInfo.Name,
                    DeployScriptRoot = scriptFile.FileInfo.DirectoryName
                };

                var agent = new OutOfProcessPowerShellDeployAgent(deploymentEventRecorder, ClrVersion.Version2);
                agent.Deploy(data);
            }

            StringAssert.Contains(deploymentEventRecorder.OutputDelegate(), "hello there");
        }
        public void Should_include_written_text_in_output()
        {
            using (var scriptFile = TemporaryFile.FromResource(".ps1", "Tests.TfsDeployer.PowerShellRunnerTests.WriteHostScript.ps1"))
            {
                var testDeployData = new DeployAgentData
                {
                    NewQuality = "Released",
                    OriginalQuality = null,
                    DeployScriptFile = scriptFile.FileInfo.Name,
                    DeployScriptRoot = scriptFile.FileInfo.DirectoryName,
                    DeployScriptParameters = new List<DeployScriptParameter>(),
                    TfsBuildDetail = new BuildDetail()
                };

                var psAgent = new LocalPowerShellDeployAgent();
                var result = psAgent.Deploy(testDeployData);

                Assert.AreEqual("Hello world\n", result.Output);
            }
        }
Ejemplo n.º 14
0
        public void OutOfProcessPowerShellDeployAgent_should_pass_a_DeployScriptParameter_as_a_PowerShell_script_switch_parameter()
        {
            using (var scriptFile = new TemporaryFile(".ps1", "param([switch]$Foo) \"Foo=$Foo\" "))
            {
                var data = new DeployAgentData
                {
                    DeployScriptFile       = scriptFile.FileInfo.Name,
                    DeployScriptRoot       = scriptFile.FileInfo.DirectoryName,
                    DeployScriptParameters = new[]
                    {
                        new DeployScriptParameter {
                            Name = "Foo", Value = "False"
                        }
                    },
                    TfsBuildDetail = new BuildDetail()
                };

                var agent  = new OutOfProcessPowerShellDeployAgent(null, ClrVersion.Version2);
                var result = agent.Deploy(data);

                StringAssert.Contains(result.Output, "Foo=False");
            }
        }
Ejemplo n.º 15
0
        public void OutOfProcessPowerShellDeployAgent_should_pass_a_DeployScriptParameter_with_special_characters_as_a_PowerShell_script_parameter()
        {
            using (var scriptFile = new TemporaryFile(".ps1", "param($Foo) \"Foo=$Foo\" "))
            {
                var data = new DeployAgentData
                {
                    DeployScriptFile       = scriptFile.FileInfo.Name,
                    DeployScriptRoot       = scriptFile.FileInfo.DirectoryName,
                    DeployScriptParameters = new[]
                    {
                        new DeployScriptParameter {
                            Name = "Foo", Value = "Who's going to pay $15 for a \"good\" beer?"
                        }
                    },
                    TfsBuildDetail = new BuildDetail()
                };

                var agent  = new OutOfProcessPowerShellDeployAgent(null, ClrVersion.Version2);
                var result = agent.Deploy(data);

                StringAssert.Contains(result.Output, "Foo=Who's going to pay $15 for a \"good\" beer?");
            }
        }
Ejemplo n.º 16
0
        private static DeployAgentResult DeployFailingPowerShellScript()
        {
            string testScriptFileName = Path.GetRandomFileName() + ".ps1";
            string testDirectory      = Path.GetTempPath();

            var scriptFile = new FileInfo(Path.Combine(testDirectory, testScriptFileName));

            using (var stream = scriptFile.OpenWrite())
                using (var writer = new StreamWriter(stream, Encoding.ASCII))
                {
                    writer.Write(PowerShellScripts.FailingPowerShellScript);
                }

            var testDeployData = new DeployAgentData
            {
                NewQuality             = "Released",
                OriginalQuality        = null,
                DeployScriptFile       = testScriptFileName,
                DeployScriptRoot       = testDirectory,
                DeployScriptParameters = new List <DeployScriptParameter>(),
                Tfs2008BuildDetail     = new StubBuildDetail()
            };

            var psAgent = new LocalPowerShellDeployAgent();
            DeployAgentResult result;

            try
            {
                result = psAgent.Deploy(testDeployData);
            }
            finally
            {
                scriptFile.Delete();
            }

            return(result);
        }
        public void BatchFileDeployAgent__should_not_terminate_a_script_when_the_timeout_is_maximum()
        {
            // Arrange
            const string deployScriptFilename  = "SlowDeployment.bat";
            var          deployScriptDirectory = Path.GetTempPath();

            File.WriteAllText(Path.Combine(deployScriptDirectory, deployScriptFilename), @"ping -n 2 127.0.0.1");    // script that just takes about nn seconds to execute

            // required because the deploy agent builds its own parameter list
            var tfsBuildDetail = new BuildDetail
            {
                DropLocation = string.Empty,
                BuildNumber  = string.Empty,
            };

            var deployAgentData = new DeployAgentData
            {
                DeployScriptRoot       = deployScriptDirectory,
                DeployScriptFile       = deployScriptFilename,
                Timeout                = TimeSpan.MaxValue,
                TfsBuildDetail         = tfsBuildDetail,
                DeployScriptParameters = Enumerable.Empty <DeployScriptParameter>().ToList(),
            };

            var agent = new BatchFileDeployAgent();

            // Act
            var deployAgentResult = agent.Deploy(deployAgentData);

            // Absterge
            File.Delete(deployScriptFilename);

            // Assert
            StringAssert.Contains(deployAgentResult.Output, @"Pinging 127.0.0.1");
            StringAssert.Contains(deployAgentResult.Output, @"Ping statistics");
        }
        public void AgentShouldNotCauseScriptsWithMassiveOutputToStall()
        {
            // Arrange
            const string massiveOutputScript = "@echo off\nfor /L %%i in (1000, -1, 0) do echo %%i green bottles, hanging on the wall";

            var          agent = new BatchFileDeployAgent();
            const string deployScriptFilename  = "MassiveOutputDeployment.bat";
            var          deployScriptDirectory = Directory.GetCurrentDirectory();

            File.WriteAllText(deployScriptFilename, massiveOutputScript);

            // required because the deploy agent builds its own parameter list
            var tfsBuildDetail = new BuildDetail
            {
                DropLocation = string.Empty,
                BuildNumber  = string.Empty,
            };

            var deployAgentData = new DeployAgentData
            {
                DeployScriptRoot       = deployScriptDirectory,
                DeployScriptFile       = deployScriptFilename,
                Timeout                = TimeSpan.FromSeconds(5),
                TfsBuildDetail         = tfsBuildDetail,
                DeployScriptParameters = Enumerable.Empty <DeployScriptParameter>().ToList(),
            };

            // Act
            var deployAgentResult = agent.Deploy(deployAgentData);

            // Assert
            Assert.IsTrue(deployAgentResult.Output.Contains(@"0 green bottles, hanging on the wall"));

            // Clean up
            File.Delete(deployScriptFilename);
        }