Exemple #1
0
        public void DeleteDirectory_DeletesDirectoriesRecursively()
        {
            using (TestHostContext hc = new TestHostContext(this))
            {
                Tracing trace = hc.GetTrace();

                // Arrange: Create a directory with a grandchild directory.
                string directory = Path.Combine(hc.GetDirectory(WellKnownDirectory.Bin), Path.GetRandomFileName());
                try
                {
                    Directory.CreateDirectory(Path.Combine(directory, "some child directory", "some grandchild directory"));

                    // Act.
                    IOUtil.DeleteDirectory(directory, CancellationToken.None);

                    // Assert.
                    Assert.False(Directory.Exists(directory));
                }
                finally
                {
                    // Cleanup.
                    if (Directory.Exists(directory))
                    {
                        Directory.Delete(directory, recursive: true);
                    }
                }
            }
        }
Exemple #2
0
        public void GetSourceGitClonePR()
        {
            using (TestHostContext tc = new TestHostContext(this))
            {
                var trace = tc.GetTrace();
                // Arrange.
                string dumySourceFolder = Path.Combine(tc.GetDirectory(WellKnownDirectory.Bin), "SourceProviderL0");
                var    executionContext = GetTestExecutionContext(tc, dumySourceFolder, "refs/pull/12345", "a596e13f5db8869f44574be0392fb8fe1e790ce4", false);
                var    endpoint         = GetTestSourceEndpoint("https://github.com/Microsoft/vsts-agent", false, false);

                var _gitCommandManager = GetDefaultGitCommandMock();
                tc.SetSingleton <IGitCommandManager>(_gitCommandManager.Object);
                tc.SetSingleton <IWhichUtil>(new WhichUtil());
                tc.SetSingleton <IVstsAgentWebProxy>(new VstsAgentWebProxy());

                GitSourceProvider gitSourceProvider = new ExternalGitSourceProvider();
                gitSourceProvider.Initialize(tc);
                gitSourceProvider.SetVariablesInEndpoint(executionContext.Object, endpoint);

                // Act.
                gitSourceProvider.GetSourceAsync(executionContext.Object, endpoint, default(CancellationToken)).GetAwaiter().GetResult();

                // Assert.
                _gitCommandManager.Verify(x => x.GitInit(executionContext.Object, dumySourceFolder));
                _gitCommandManager.Verify(x => x.GitRemoteAdd(executionContext.Object, dumySourceFolder, "origin", "https://github.com/Microsoft/vsts-agent"));
                _gitCommandManager.Verify(x => x.GitFetch(executionContext.Object, dumySourceFolder, "origin", It.IsAny <int>(), new List <string>()
                {
                    "+refs/heads/*:refs/remotes/origin/*", "+refs/pull/12345:refs/remotes/pull/12345"
                }, It.IsAny <string>(), It.IsAny <CancellationToken>()));
                _gitCommandManager.Verify(x => x.GitRemoteSetUrl(executionContext.Object, dumySourceFolder, "origin", "https://github.com/Microsoft/vsts-agent"));
                _gitCommandManager.Verify(x => x.GitRemoteSetPushUrl(executionContext.Object, dumySourceFolder, "origin", "https://github.com/Microsoft/vsts-agent"));
                _gitCommandManager.Verify(x => x.GitCheckout(executionContext.Object, dumySourceFolder, It.Is <string>(s => s.Equals("refs/remotes/pull/12345")), It.IsAny <CancellationToken>()));
            }
        }
Exemple #3
0
        public void DeleteFile_IgnoresDirectory()
        {
            using (TestHostContext hc = new TestHostContext(this))
            {
                Tracing trace = hc.GetTrace();

                // Arrange: Create a directory.
                string directory = Path.Combine(hc.GetDirectory(WellKnownDirectory.Bin), Path.GetRandomFileName());
                try
                {
                    Directory.CreateDirectory(directory);

                    // Act: Call "DeleteFile" against a directory. The method should not blow up and
                    // should simply ignore the directory since it is not a file.
                    IOUtil.DeleteFile(directory);

                    // Assert.
                    Assert.True(Directory.Exists(directory));
                }
                finally
                {
                    // Cleanup.
                    if (Directory.Exists(directory))
                    {
                        Directory.Delete(directory, recursive: true);
                    }
                }
            }
        }
        public async void TestGetCapabilitiesWithDotCapabilities()
        {
            using (var hc = new TestHostContext(this))
                using (var tokenSource = new CancellationTokenSource())
                {
                    var capFile = Path.Combine(hc.GetDirectory(WellKnownDirectory.Root), ".capabilities");
                    try {
                        File.WriteAllText(capFile, "User.Capability=My Value");
                        Mock <IConfigurationManager> configurationManager = new Mock <IConfigurationManager>();
                        hc.SetSingleton <IConfigurationManager>(configurationManager.Object);

                        // Arrange
                        var provider = new UserCapabilitiesProvider();
                        provider.Initialize(hc);
                        var settings = new AgentSettings();

                        // Act
                        List <Capability> capabilities = await provider.GetCapabilitiesAsync(settings, tokenSource.Token);

                        // Assert
                        Assert.NotNull(capabilities);
                        Capability myCapability = capabilities.SingleOrDefault(x => string.Equals(x.Name, "User.Capability", StringComparison.Ordinal));
                        Assert.NotNull(myCapability);
                        Assert.Equal("My Value", myCapability.Value);
                        Assert.Equal(1, capabilities.Count);
                    } finally {
                        File.Delete(capFile);
                    }
                }
        }
Exemple #5
0
        private void Setup([CallerMemberName] string name = "")
        {
            // Mocks.
            _jobServer  = new Mock <IJobServer>();
            _taskServer = new Mock <ITaskServer>();
            _ec         = new Mock <IExecutionContext>();
            _ec.Setup(x => x.CancellationToken).Returns(_ecTokenSource.Token);

            // Test host context.
            _hc = new TestHostContext(this, name);

            // Random work folder.
            _workFolder = _hc.GetDirectory(WellKnownDirectory.Work);

            _hc.SetSingleton <IJobServer>(_jobServer.Object);
            _hc.SetSingleton <ITaskServer>(_taskServer.Object);

            _configurationStore = new Mock <IConfigurationStore>();
            _configurationStore
            .Setup(x => x.GetSettings())
            .Returns(
                new AgentSettings
            {
                WorkFolder = _workFolder
            });
            _hc.SetSingleton <IConfigurationStore>(_configurationStore.Object);

            // Instance to test.
            _taskManager = new TaskManager();
            _taskManager.Initialize(_hc);
        }
        public void DefaultContainerInfoMappings()
        {
            var dockerContainer = new Pipelines.ContainerResource()
            {
                Alias = "vsts_container_preview",
                Image = "foo"
            };

            using (TestHostContext hc = CreateTestContext())
            {
                ContainerInfo info = hc.CreateContainerInfo(dockerContainer, isJobContainer: false);
                Assert.True(info.TranslateToContainerPath(hc.GetDirectory(WellKnownDirectory.Tools)).EndsWith($"{Path.DirectorySeparatorChar}__t"), "Tools directory maps");
                Assert.True(info.TranslateToContainerPath(hc.GetDirectory(WellKnownDirectory.Work)).EndsWith($"{Path.DirectorySeparatorChar}__w"), "Work directory maps");
                Assert.True(info.TranslateToContainerPath(hc.GetDirectory(WellKnownDirectory.Root)).EndsWith($"{Path.DirectorySeparatorChar}__a"), "Root directory maps");
            }
        }
        public void GetSourceGitClone()
        {
            using (TestHostContext tc = new TestHostContext(this))
            {
                // Arrange.
                string dumySourceFolder = Path.Combine(tc.GetDirectory(WellKnownDirectory.Bin), "SourceProviderL0");
                var    executionContext = GetTestExecutionContext(tc, dumySourceFolder, "master", "a596e13f5db8869f44574be0392fb8fe1e790ce4", false);
                var    endpoint         = GetTestSourceEndpoint("https://github.com/microsoft/azure-pipelines-agent", false, false);

                var _gitCommandManager = GetDefaultGitCommandMock();
                tc.SetSingleton <IGitCommandManager>(_gitCommandManager.Object);
                tc.SetSingleton <IVstsAgentWebProxy>(new VstsAgentWebProxy());
                var _configStore = new Mock <IConfigurationStore>();
                _configStore.Setup(x => x.GetSettings()).Returns(() => new AgentSettings()
                {
                    ServerUrl = "http://localhost:8080/tfs"
                });
                tc.SetSingleton <IConfigurationStore>(_configStore.Object);
                tc.SetSingleton <IAgentCertificateManager>(new AgentCertificateManager());

                GitSourceProvider gitSourceProvider = new ExternalGitSourceProvider();
                gitSourceProvider.Initialize(tc);
                gitSourceProvider.SetVariablesInEndpoint(executionContext.Object, endpoint);

                // Act.
                gitSourceProvider.GetSourceAsync(executionContext.Object, endpoint, default(CancellationToken)).GetAwaiter().GetResult();

                // Assert.
                _gitCommandManager.Verify(x => x.GitInit(executionContext.Object, dumySourceFolder));
                _gitCommandManager.Verify(x => x.GitRemoteAdd(executionContext.Object, dumySourceFolder, "origin", "https://github.com/microsoft/azure-pipelines-agent"));
                _gitCommandManager.Verify(x => x.GitRemoteSetUrl(executionContext.Object, dumySourceFolder, "origin", "https://github.com/microsoft/azure-pipelines-agent"));
                _gitCommandManager.Verify(x => x.GitRemoteSetPushUrl(executionContext.Object, dumySourceFolder, "origin", "https://github.com/microsoft/azure-pipelines-agent"));
                _gitCommandManager.Verify(x => x.GitCheckout(executionContext.Object, dumySourceFolder, "a596e13f5db8869f44574be0392fb8fe1e790ce4", It.IsAny <CancellationToken>()));
            }
        }
Exemple #8
0
        public void ValidateExecutePermission_DoesNotExceedFailsafe()
        {
            using (TestHostContext hc = new TestHostContext(this))
            {
                Tracing trace = hc.GetTrace();

                // Arrange: Create a directory.
                string directory = Path.Combine(hc.GetDirectory(WellKnownDirectory.Bin), Path.GetRandomFileName());
                try
                {
                    Directory.CreateDirectory(directory);

                    // Act/Assert: Call "ValidateExecutePermission". The method should not blow up.
                    IOUtil.ValidateExecutePermission(directory);
                }
                finally
                {
                    // Cleanup.
                    if (Directory.Exists(directory))
                    {
                        Directory.Delete(directory, recursive: true);
                    }
                }
            }
        }
Exemple #9
0
        public async Task DeleteDirectory_DeletesDirectoryReparsePointChain()
        {
            using (TestHostContext hc = new TestHostContext(this))
            {
                Tracing trace = hc.GetTrace();

                // Arrange: Create the following structure:
                //   randomDir
                //   randomDir/<guid 1> -> <guid 2>
                //   randomDir/<guid 2> -> <guid 3>
                //   randomDir/<guid 3> -> <guid 4>
                //   randomDir/<guid 4> -> <guid 5>
                //   randomDir/<guid 5> -> targetDir
                //   randomDir/targetDir
                //   randomDir/targetDir/file.txt
                //
                // The purpose of this test is to verify that DirectoryNotFoundException is gracefully handled when
                // deleting a chain of reparse point directories. Since the reparse points are named in a random order,
                // the DirectoryNotFoundException case is likely to be encountered.
                string randomDir = Path.Combine(hc.GetDirectory(WellKnownDirectory.Bin), Path.GetRandomFileName());
                try
                {
                    string targetDir = Directory.CreateDirectory(Path.Combine(randomDir, "targetDir")).FullName;
                    string file      = Path.Combine(targetDir, "file.txt");
                    File.WriteAllText(path: file, contents: "some contents");
                    string linkDir1 = Path.Combine(randomDir, $"{Guid.NewGuid()}_linkDir1");
                    string linkDir2 = Path.Combine(randomDir, $"{Guid.NewGuid()}_linkDir2");
                    string linkDir3 = Path.Combine(randomDir, $"{Guid.NewGuid()}_linkDir3");
                    string linkDir4 = Path.Combine(randomDir, $"{Guid.NewGuid()}_linkDir4");
                    string linkDir5 = Path.Combine(randomDir, $"{Guid.NewGuid()}_linkDir5");
                    await CreateDirectoryReparsePoint(context : hc, link : linkDir1, target : linkDir2);
                    await CreateDirectoryReparsePoint(context : hc, link : linkDir2, target : linkDir3);
                    await CreateDirectoryReparsePoint(context : hc, link : linkDir3, target : linkDir4);
                    await CreateDirectoryReparsePoint(context : hc, link : linkDir4, target : linkDir5);
                    await CreateDirectoryReparsePoint(context : hc, link : linkDir5, target : targetDir);

                    // Sanity check to verify the link was created properly:
                    Assert.True(Directory.Exists(linkDir1));
                    Assert.True(new DirectoryInfo(linkDir1).Attributes.HasFlag(FileAttributes.ReparsePoint));
                    Assert.True(File.Exists(Path.Combine(linkDir1, "file.txt")));

                    // Act.
                    IOUtil.DeleteDirectory(randomDir, CancellationToken.None);

                    // Assert.
                    Assert.False(Directory.Exists(linkDir1));
                    Assert.False(Directory.Exists(targetDir));
                    Assert.False(File.Exists(file));
                    Assert.False(Directory.Exists(randomDir));
                }
                finally
                {
                    // Cleanup.
                    if (Directory.Exists(randomDir))
                    {
                        Directory.Delete(randomDir, recursive: true);
                    }
                }
            }
        }
Exemple #10
0
        public TestHostContext Setup([CallerMemberName] string name = "")
        {
            // Setup the host context.
            TestHostContext hc = new TestHostContext(this, name);

            // Create a random work path.
            _workFolder = hc.GetDirectory(WellKnownDirectory.Work);

            // Setup the execution context.
            _ec = new Mock <IExecutionContext>();
            List <string> warnings;

            _variables = new Variables(hc, new Dictionary <string, VariableValue>(), out warnings);
            _variables.Set(Constants.Variables.System.CollectionId, CollectionId);
            _variables.Set(WellKnownDistributedTaskVariables.TFCollectionUrl, CollectionUrl);
            _variables.Set(Constants.Variables.System.DefinitionId, DefinitionId);
            _variables.Set(Constants.Variables.Build.DefinitionName, DefinitionName);
            _ec.Setup(x => x.Variables).Returns(_variables);

            // Setup the endpoint.
            _repository = new Pipelines.RepositoryResource()
            {
                Url = new Uri(RepositoryUrl)
            };

            // Setup the tracking manager.
            _trackingManager = new TrackingManager();
            _trackingManager.Initialize(hc);

            return(hc);
        }
Exemple #11
0
        public async Task RepositoryPlugin_PathInputMoveFolder()
        {
            using (TestHostContext tc = new TestHostContext(this))
            {
                Setup(tc);
                var repository  = _executionContext.Repositories.Single();
                var currentPath = repository.Properties.Get <string>(Pipelines.RepositoryPropertyNames.Path);
                Directory.CreateDirectory(currentPath);

                _executionContext.Inputs["Path"] = "test";

                await _checkoutTask.RunAsync(_executionContext, CancellationToken.None);

                var actualPath = repository.Properties.Get <string>(Pipelines.RepositoryPropertyNames.Path);

                Assert.NotEqual(actualPath, currentPath);
                Assert.Equal(actualPath, Path.Combine(tc.GetDirectory(WellKnownDirectory.Work), "1", "test"));
                Assert.True(Directory.Exists(actualPath));
                Assert.False(Directory.Exists(currentPath));

                var temp = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
                File.Copy(tc.TraceFileName, temp);
                Assert.True(File.ReadAllText(temp).Contains($"##vso[plugininternal.updaterepositorypath alias=myRepo;]{actualPath}"));
            }
        }
Exemple #12
0
        public void DeleteDirectory_DeletesReadOnlyRootDirectory()
        {
            using (TestHostContext hc = new TestHostContext(this))
            {
                Tracing trace = hc.GetTrace();

                // Arrange: Create a read-only directory.
                string directory = Path.Combine(hc.GetDirectory(WellKnownDirectory.Bin), Path.GetRandomFileName());
                try
                {
                    var directoryInfo = new DirectoryInfo(directory);
                    directoryInfo.Create();
                    directoryInfo.Attributes = directoryInfo.Attributes | FileAttributes.ReadOnly;

                    // Act.
                    IOUtil.DeleteDirectory(directory, CancellationToken.None);

                    // Assert.
                    Assert.False(Directory.Exists(directory));
                }
                finally
                {
                    // Cleanup.
                    var directoryInfo = new DirectoryInfo(directory);
                    if (directoryInfo.Exists)
                    {
                        directoryInfo.Attributes = directoryInfo.Attributes & ~FileAttributes.ReadOnly;
                        directoryInfo.Delete();
                    }
                }
            }
        }
Exemple #13
0
        public async void CommitsShouldBeUploadedAsAttachment()
        {
            using (TestHostContext tc = Setup())
            {
                string commitRootDirectory = Path.Combine(tc.GetDirectory(WellKnownDirectory.Work), Guid.NewGuid().ToString("D"));
                Directory.CreateDirectory(commitRootDirectory);

                try
                {
                    JenkinsArtifactDetails details = _artifactDefinition.Details as JenkinsArtifactDetails;
                    details.StartCommitArtifactVersion = "10";
                    details.EndCommitArtifactVersion   = "20";

                    var artifact = new JenkinsArtifact();
                    artifact.Initialize(tc);

                    SetupBuildRangeQuery(details, "{ \"allBuilds\": [{ \"number\": 20 }, { \"number\": 10 }, { \"number\": 2 } ] }");
                    string commitResult = " {\"builds\": [{ \"number\":9, \"result\":\"SUCCESS\", \"changeSet\": { \"items\": [{ \"commitId\" : \"2869c7ccd0b1b649ba6765e89ee5ff36ef6d4805\", \"author\": { \"fullName\" : \"testuser\" }, \"msg\":\"test\" }]}}]}";
                    string commitsUrl   = $"{details.Url}/job/{details.JobName}/api/json?tree=builds[number,result,changeSet[items[commitId,date,msg,author[fullName]]]]{{0,1}}";
                    _httpClient.Setup(x => x.GetStringAsync(It.Is <string>(y => y.StartsWith(commitsUrl)), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>()))
                    .Returns(Task.FromResult(commitResult));

                    string commitFilePath = Path.Combine(commitRootDirectory, $"commits_{details.Alias}_1.json");

                    await artifact.DownloadCommitsAsync(_ec.Object, _artifactDefinition, commitRootDirectory);

                    _ec.Verify(x => x.QueueAttachFile(It.Is <string>(y => y.Equals(CoreAttachmentType.FileAttachment)), It.IsAny <string>(), It.IsAny <string>()), Times.Once);
                }
                finally
                {
                    IOUtil.DeleteDirectory(commitRootDirectory, CancellationToken.None);
                }
            }
        }
Exemple #14
0
        private TestHostContext Setup(
            [CallerMemberName] string name = "")
        {
            // Setup the host context.
            TestHostContext hc = new TestHostContext(this, name);

            // Setup the execution context.
            _ec = new Mock <IExecutionContext>();

            GitHubContext githubContext = new GitHubContext();

            _ec.Setup(x => x.GetGitHubContext("repository")).Returns("actions/runner");

            // Store the expected tracking file path.
            _trackingFile = Path.Combine(
                hc.GetDirectory(WellKnownDirectory.Work),
                Constants.Pipeline.Path.PipelineMappingDirectory,
                "actions/runner",
                Constants.Pipeline.Path.TrackingConfigFile);

            _workspaceOptions = new Pipelines.WorkspaceOptions();

            // Setup the tracking manager.
            _trackingManager = new Mock <ITrackingManager>();
            hc.SetSingleton <ITrackingManager>(_trackingManager.Object);

            // Setup the build directory manager.
            _pipelineDirectoryManager = new PipelineDirectoryManager();
            _pipelineDirectoryManager.Initialize(hc);
            return(hc);
        }
Exemple #15
0
        public void DeleteDirectory_IgnoresFile()
        {
            using (TestHostContext hc = new TestHostContext(this))
            {
                Tracing trace = hc.GetTrace();

                // Arrange: Create a directory with a file.
                string directory = Path.Combine(hc.GetDirectory(WellKnownDirectory.Bin), Path.GetRandomFileName());
                string file      = Path.Combine(directory, "some file");
                try
                {
                    Directory.CreateDirectory(directory);
                    File.WriteAllText(path: file, contents: "some contents");

                    // Act: Call "DeleteDirectory" against the file. The method should not blow up and
                    // should simply ignore the file since it is not a directory.
                    IOUtil.DeleteDirectory(file, CancellationToken.None);

                    // Assert.
                    Assert.True(File.Exists(file));
                }
                finally
                {
                    // Cleanup.
                    if (Directory.Exists(directory))
                    {
                        Directory.Delete(directory, recursive: true);
                    }
                }
            }
        }
Exemple #16
0
        public void DeleteFile_DeletesFile()
        {
            using (TestHostContext hc = new TestHostContext(this))
            {
                Tracing trace = hc.GetTrace();

                // Arrange: Create a directory with a file.
                string directory = Path.Combine(hc.GetDirectory(WellKnownDirectory.Bin), Path.GetRandomFileName());
                string file      = Path.Combine(directory, "some file");
                try
                {
                    Directory.CreateDirectory(directory);
                    File.WriteAllText(path: file, contents: "some contents");

                    // Act.
                    IOUtil.DeleteFile(file);

                    // Assert.
                    Assert.False(File.Exists(file));
                }
                finally
                {
                    // Cleanup.
                    if (Directory.Exists(directory))
                    {
                        Directory.Delete(directory, recursive: true);
                    }
                }
            }
        }
Exemple #17
0
        private void Setup(TestHostContext hostContext)
        {
            var repo = new Pipelines.RepositoryResource()
            {
                Alias = "myRepo",
                Type  = Pipelines.RepositoryTypes.Git,
            };

            repo.Properties.Set <string>(Pipelines.RepositoryPropertyNames.Path, Path.Combine(hostContext.GetDirectory(WellKnownDirectory.Work), "1", "s"));

            _executionContext = new AgentTaskPluginExecutionContext(hostContext.GetTrace())
            {
                Endpoints = new List <ServiceEndpoint>(),
                Inputs    = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase)
                {
                    { Pipelines.PipelineConstants.CheckoutTaskInputs.Repository, "myRepo" },
                },
                Repositories = new List <Pipelines.RepositoryResource>
                {
                    repo
                },
                Variables = new Dictionary <string, VariableValue>(StringComparer.OrdinalIgnoreCase)
                {
                    {
                        "agent.builddirectory",
                        Path.Combine(hostContext.GetDirectory(WellKnownDirectory.Work), "1")
                    },
                    {
                        "agent.workfolder",
                        hostContext.GetDirectory(WellKnownDirectory.Work)
                    },
                    {
                        "agent.tempdirectory",
                        hostContext.GetDirectory(WellKnownDirectory.Temp)
                    }
                },
            };

            _sourceProvider = new Mock <ISourceProvider>();

            _sourceProviderFactory = new Mock <ISourceProviderFactory>();
            _sourceProviderFactory
            .Setup(x => x.GetSourceProvider(It.IsAny <String>()))
            .Returns(_sourceProvider.Object);

            _checkoutTask = new CheckoutTask(_sourceProviderFactory.Object);
        }
Exemple #18
0
        public void TranslatePathForStepTarget_should_convert_path_only_for_containers(bool isCheckout, string jobTarget, string stepTarget)
        {
            // Note: the primary repository is defined as the first repository that is checked out in the job
            using (TestHostContext hc = CreateTestContext())
                using (var ec = new Agent.Worker.ExecutionContext())
                {
                    ec.Initialize(hc);

                    // Arrange: Create a container.
                    var pipeContainer = new Pipelines.ContainerResource
                    {
                        Alias = "container"
                    };
                    pipeContainer.Properties.Set <string>("image", "someimage");

                    // Arrange: Create a job request message.
                    TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference();
                    TimelineReference timeline          = new TimelineReference();
                    JobEnvironment    environment       = new JobEnvironment();
                    environment.SystemConnection = new ServiceEndpoint();
                    List <Pipelines.JobStep> steps = new List <Pipelines.JobStep>();
                    steps.Add(new Pipelines.TaskStep
                    {
                        Target = new Pipelines.StepTarget
                        {
                            Target = stepTarget
                        },
                        Reference = new Pipelines.TaskStepDefinitionReference()
                    });
                    var resources = new Pipelines.JobResources();
                    resources.Containers.Add(pipeContainer);
                    Guid   JobId      = Guid.NewGuid();
                    string jobName    = "some job name";
                    var    jobRequest = new Pipelines.AgentJobRequestMessage(plan, timeline, JobId, jobName, jobName, jobTarget, new Dictionary <string, string>(),
                                                                             new Dictionary <string, VariableValue>(), new List <MaskHint>(), resources, new Pipelines.WorkspaceOptions(), steps);

                    // Arrange
                    var pagingLogger = new Mock <IPagingLogger>();
                    hc.EnqueueInstance(pagingLogger.Object);

                    // Act.
                    ec.InitializeJob(jobRequest, CancellationToken.None);
                    ec.SetStepTarget(steps[0].Target);
                    ec.Variables.Set(Constants.Variables.Task.SkipTranslatorForCheckout, isCheckout.ToString());

                    string stringBeforeTranslation = hc.GetDirectory(WellKnownDirectory.Work);
                    string stringAfterTranslation  = ec.TranslatePathForStepTarget(stringBeforeTranslation);

                    // Assert.
                    if ((stepTarget == "container") || (isCheckout is false && jobTarget == "container" && stepTarget == null))
                    {
                        string stringContainer = "C:\\__w";
                        if (ec.StepTarget().ExecutionOS != PlatformUtil.OS.Windows)
                        {
                            stringContainer = "/__w";
                        }
                        Assert.Equal(stringContainer, stringAfterTranslation);
                    }
Exemple #19
0
        public void GetSourceGitFetchPR()
        {
            using (TestHostContext tc = new TestHostContext(this))
            {
                var trace = tc.GetTrace();
                // Arrange.
                string dumySourceFolder = Path.Combine(tc.GetDirectory(WellKnownDirectory.Bin), "SourceProviderL0");
                try
                {
                    Directory.CreateDirectory(dumySourceFolder);
                    string dumyGitFolder = Path.Combine(dumySourceFolder, ".git");
                    Directory.CreateDirectory(dumyGitFolder);
                    string dumyGitConfig = Path.Combine(dumyGitFolder, "config");
                    File.WriteAllText(dumyGitConfig, "test git confg file");

                    var executionContext = GetTestExecutionContext(tc, dumySourceFolder, "refs/pull/12345/merge", "a596e13f5db8869f44574be0392fb8fe1e790ce4", false);
                    var endpoint         = GetTestSourceEndpoint("https://github.com/Microsoft/vsts-agent", false, false);

                    var _gitCommandManager = GetDefaultGitCommandMock();
                    tc.SetSingleton <IGitCommandManager>(_gitCommandManager.Object);
                    tc.SetSingleton <IWhichUtil>(new WhichUtil());
                    tc.SetSingleton <IVstsAgentWebProxy>(new VstsAgentWebProxy());
                    var _configStore = new Mock <IConfigurationStore>();
                    _configStore.Setup(x => x.GetSettings()).Returns(() => new AgentSettings()
                    {
                        ServerUrl = "http://*****:*****@github.com/Microsoft/vsts-agent"));
                    _gitCommandManager.Verify(x => x.GitRemoteSetPushUrl(executionContext.Object, dumySourceFolder, "origin", "https://*****:*****@github.com/Microsoft/vsts-agent"));
                    _gitCommandManager.Verify(x => x.GitFetch(executionContext.Object, dumySourceFolder, "origin", It.IsAny <int>(), new List <string>()
                    {
                        "+refs/heads/*:refs/remotes/origin/*", "+refs/pull/12345/merge:refs/remotes/pull/12345/merge"
                    }, It.IsAny <string>(), It.IsAny <CancellationToken>()));
                    _gitCommandManager.Verify(x => x.GitRemoteSetUrl(executionContext.Object, dumySourceFolder, "origin", "https://github.com/Microsoft/vsts-agent"));
                    _gitCommandManager.Verify(x => x.GitRemoteSetPushUrl(executionContext.Object, dumySourceFolder, "origin", "https://github.com/Microsoft/vsts-agent"));
                    _gitCommandManager.Verify(x => x.GitCheckout(executionContext.Object, dumySourceFolder, "refs/remotes/pull/12345/merge", It.IsAny <CancellationToken>()));
                }
                finally
                {
                    IOUtil.DeleteDirectory(dumySourceFolder, CancellationToken.None);
                }
            }
        }
        private void Setup(TestHostContext hostContext, List <Pipelines.RepositoryResource> repos)
        {
            _executionContext = new AgentTaskPluginExecutionContext(hostContext.GetTrace())
            {
                Endpoints = new List <ServiceEndpoint>(),
                Inputs    = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase)
                {
                    { Pipelines.PipelineConstants.CheckoutTaskInputs.Repository, "myRepo" },
                },
                Repositories = repos,
                Variables    = new Dictionary <string, VariableValue>(StringComparer.OrdinalIgnoreCase)
                {
                    {
                        "agent.builddirectory",
                        Path.Combine(hostContext.GetDirectory(WellKnownDirectory.Work), "1")
                    },
                    {
                        "agent.workfolder",
                        hostContext.GetDirectory(WellKnownDirectory.Work)
                    },
                    {
                        "agent.tempdirectory",
                        hostContext.GetDirectory(WellKnownDirectory.Temp)
                    }
                },
                JobSettings = new Dictionary <string, string>()
                {
                    // Set HasMultipleCheckouts to true if the number of repos is greater than 1
                    { WellKnownJobSettings.HasMultipleCheckouts, (repos.Count > 1).ToString() }
                },
            };

            _sourceProvider = new Mock <ISourceProvider>();

            _sourceProviderFactory = new Mock <ISourceProviderFactory>();
            _sourceProviderFactory
            .Setup(x => x.GetSourceProvider(It.IsAny <String>()))
            .Returns(_sourceProvider.Object);

            _checkoutTask = new CheckoutTask(_sourceProviderFactory.Object);
        }
Exemple #21
0
 private void CleanLogFolder()
 {
     using (TestHostContext hc = new TestHostContext(this))
     {
         //clean test data if any old test forgot
         string pagesFolder = Path.Combine(hc.GetDirectory(WellKnownDirectory.Diag), PagingLogger.PagingFolder);
         if (Directory.Exists(pagesFolder))
         {
             Directory.Delete(pagesFolder, true);
         }
     }
 }
Exemple #22
0
        public void NormalizesLocalPath()
        {
            using (TestHostContext tc = new TestHostContext(this))
            {
                // Arrange.
                Tracing trace   = tc.GetTrace();
                var     mapping = new TfsVCSourceProvider.DefinitionWorkspaceMapping
                {
                    MappingType = TfsVCSourceProvider.DefinitionMappingType.Map,
                    ServerPath  = "$/myProj",
                    LocalPath   = @"myProj/myDir\mySubDir",
                };

                // Act.
                string actual = mapping.GetRootedLocalPath(tc.GetDirectory(WellKnownDirectory.Bin));

                // Assert.
                string expected = Path.Combine(tc.GetDirectory(WellKnownDirectory.Bin), "myProj", "myDir", "mySubDir");
                Assert.Equal(expected, actual);
            }
        }
Exemple #23
0
        public void GetSourceReCloneOnUrlNotMatch()
        {
            using (TestHostContext tc = new TestHostContext(this))
            {
                var trace = tc.GetTrace();
                // Arrange.
                string dumySourceFolder = Path.Combine(tc.GetDirectory(WellKnownDirectory.Bin), "SourceProviderL0");
                try
                {
                    Directory.CreateDirectory(dumySourceFolder);
                    string dumyGitFolder = Path.Combine(dumySourceFolder, ".git");
                    Directory.CreateDirectory(dumyGitFolder);
                    string dumyGitConfig = Path.Combine(dumyGitFolder, "config");
                    File.WriteAllText(dumyGitConfig, "test git confg file");

                    var executionContext = GetTestExecutionContext(tc, dumySourceFolder, "refs/heads/users/user1", "", true);
                    var endpoint         = GetTestSourceEndpoint("https://github.com/Microsoft/vsts-agent", false, false);

                    var _gitCommandManager = GetDefaultGitCommandMock();
                    _gitCommandManager
                    .Setup(x => x.GitGetFetchUrl(It.IsAny <IExecutionContext>(), It.IsAny <string>()))
                    .Returns(Task.FromResult <Uri>(new Uri("https://github.com/Microsoft/vsts-another-agent")));

                    tc.SetSingleton <IGitCommandManager>(_gitCommandManager.Object);
                    tc.SetSingleton <IWhichUtil>(new WhichUtil());
                    tc.SetSingleton <IVstsAgentWebProxy>(new VstsAgentWebProxy());
                    var _configStore = new Mock <IConfigurationStore>();
                    _configStore.Setup(x => x.GetSettings()).Returns(() => new AgentSettings()
                    {
                        ServerUrl = "http://localhost:8080/tfs"
                    });
                    tc.SetSingleton <IConfigurationStore>(_configStore.Object);
                    tc.SetSingleton <IAgentCertificateManager>(new AgentCertificateManager());

                    GitSourceProvider gitSourceProvider = new ExternalGitSourceProvider();
                    gitSourceProvider.Initialize(tc);
                    gitSourceProvider.SetVariablesInEndpoint(executionContext.Object, endpoint);

                    // Act.
                    gitSourceProvider.GetSourceAsync(executionContext.Object, endpoint, default(CancellationToken)).GetAwaiter().GetResult();

                    // Assert.
                    _gitCommandManager.Verify(x => x.GitInit(executionContext.Object, dumySourceFolder));
                    _gitCommandManager.Verify(x => x.GitRemoteAdd(executionContext.Object, dumySourceFolder, "origin", "https://github.com/Microsoft/vsts-agent"));
                    _gitCommandManager.Verify(x => x.GitCheckout(executionContext.Object, dumySourceFolder, "refs/remotes/origin/users/user1", It.IsAny <CancellationToken>()));
                }
                finally
                {
                    IOUtil.DeleteDirectory(dumySourceFolder, CancellationToken.None);
                }
            }
        }
        private Pipelines.RepositoryResource GetRepository(TestHostContext hostContext, String alias, String relativePath)
        {
            var workFolder = hostContext.GetDirectory(WellKnownDirectory.Work);
            var repo       = new Pipelines.RepositoryResource()
            {
                Alias = alias,
                Type  = Pipelines.RepositoryTypes.Git,
            };

            repo.Properties.Set <string>(Pipelines.RepositoryPropertyNames.Path, Path.Combine(workFolder, "1", relativePath));

            return(repo);
        }
Exemple #25
0
        public void GetSourcePreferFeatureVariables()
        {
            using (TestHostContext tc = new TestHostContext(this))
            {
                var trace = tc.GetTrace();
                // Arrange.
                string dumySourceFolder = Path.Combine(tc.GetDirectory(WellKnownDirectory.Bin), "SourceProviderL0");
                try
                {
                    Directory.CreateDirectory(dumySourceFolder);
                    var executionContext = GetTestExecutionContext(tc, dumySourceFolder, "refs/remotes/origin/master", "", false);
                    executionContext.Object.Variables.Set("agent.source.git.lfs", "true");
                    executionContext.Object.Variables.Set("agent.source.git.shallowFetchDepth", "10");

                    var endpoint = GetTestSourceEndpoint("https://github.com/Microsoft/vsts-agent", false, false, false, 0);

                    var _gitCommandManager = GetDefaultGitCommandMock();
                    tc.SetSingleton <IGitCommandManager>(_gitCommandManager.Object);
                    tc.SetSingleton <IWhichUtil>(new WhichUtil());
                    tc.SetSingleton <IVstsAgentWebProxy>(new VstsAgentWebProxy());
                    var _configStore = new Mock <IConfigurationStore>();
                    _configStore.Setup(x => x.GetSettings()).Returns(() => new AgentSettings()
                    {
                        ServerUrl = "http://*****:*****@github.com/Microsoft/vsts-agent.git/info/lfs"));
                    _gitCommandManager.Verify(x => x.GitConfig(executionContext.Object, dumySourceFolder, "remote.origin.lfspushurl", "https://*****:*****@github.com/Microsoft/vsts-agent.git/info/lfs"));
                    _gitCommandManager.Verify(x => x.GitLFSFetch(executionContext.Object, dumySourceFolder, "origin", It.IsAny <string>(), It.IsAny <string>(), It.IsAny <CancellationToken>()));
                    _gitCommandManager.Verify(x => x.GitFetch(executionContext.Object, dumySourceFolder, "origin", 10, It.IsAny <List <string> >(), It.IsAny <string>(), It.IsAny <CancellationToken>()));
                    _gitCommandManager.Verify(x => x.GitCheckout(executionContext.Object, dumySourceFolder, "refs/remotes/origin/master", It.IsAny <CancellationToken>()));
                }
                finally
                {
                    IOUtil.DeleteDirectory(dumySourceFolder, CancellationToken.None);
                }
            }
        }
Exemple #26
0
        public async void TestSelfUpdateAsync()
        {
            using (var hc = new TestHostContext(this))
            {
                //Arrange
                var updater = new Runner.Listener.SelfUpdater();
                hc.SetSingleton <ITerminal>(_term.Object);
                hc.SetSingleton <IRunnerServer>(_runnerServer.Object);
                hc.SetSingleton <IConfigurationStore>(_configStore.Object);
                hc.SetSingleton <IHttpClientHandlerFactory>(new HttpClientHandlerFactory());

                var p = new ProcessInvokerWrapper();
                p.Initialize(hc);
                hc.EnqueueInstance <IProcessInvoker>(p);
                updater.Initialize(hc);

                _runnerServer.Setup(x => x.UpdateAgentUpdateStateAsync(1, 1, It.IsAny <string>(), It.IsAny <string>()))
                .Callback((int p, int a, string s, string t) =>
                {
                    hc.GetTrace().Info(t);
                })
                .Returns(Task.FromResult(new TaskAgent()));

                try
                {
                    var result = await updater.SelfUpdate(_refreshMessage, _jobDispatcher.Object, true, hc.RunnerShutdownToken);

                    Assert.True(result);
                    Assert.True(Directory.Exists(Path.Combine(hc.GetDirectory(WellKnownDirectory.Root), "bin.2.299.0")));
                    Assert.True(Directory.Exists(Path.Combine(hc.GetDirectory(WellKnownDirectory.Root), "externals.2.299.0")));
                }
                finally
                {
                    IOUtil.DeleteDirectory(Path.Combine(hc.GetDirectory(WellKnownDirectory.Root), "bin.2.299.0"), CancellationToken.None);
                    IOUtil.DeleteDirectory(Path.Combine(hc.GetDirectory(WellKnownDirectory.Root), "externals.2.299.0"), CancellationToken.None);
                }
            }
        }
Exemple #27
0
        public void RecreatesPipelinesDirectoryWhenCleanIsAll()
        {
            // Arrange.
            using (TestHostContext hc = Setup())
            {
                _existingConfig = new TrackingConfig(_ec.Object);
                _trackingManager.Setup(x => x.LoadIfExists(_ec.Object, _trackingFile)).Returns(_existingConfig);

                _workspaceOptions.Clean = Pipelines.PipelineConstants.WorkspaceCleanOptions.All;

                string pipelinesDirectory = Path.Combine(hc.GetDirectory(WellKnownDirectory.Work), _existingConfig.PipelineDirectory);
                string looseFile          = Path.Combine(pipelinesDirectory, "some loose directory", "some loose file");
                Directory.CreateDirectory(Path.GetDirectoryName(looseFile));
                File.WriteAllText(path: looseFile, contents: "some loose file contents");

                // Act.
                _pipelineDirectoryManager.PrepareDirectory(_ec.Object, _workspaceOptions);

                // Assert.
                Assert.Equal(1, Directory.GetFileSystemEntries(pipelinesDirectory, "*", SearchOption.AllDirectories).Length);
                Assert.True(Directory.Exists(Path.Combine(hc.GetDirectory(WellKnownDirectory.Work), _existingConfig.WorkspaceDirectory)));
            }
        }
Exemple #28
0
        public void GeneratePluginExecutionContextHostInfoTest()
        {
            using (TestHostContext tc = CreateTestContext())
            {
                Tracing trace = tc.GetTrace();
                var     agentPluginManager = new AgentPluginManager();
                agentPluginManager.Initialize(tc);

                var inputs = new Dictionary <string, string>()
                {
                    { "input1", "foo" },
                    { "input2", tc.GetDirectory(WellKnownDirectory.Work) },
                };

                var variables = new Dictionary <string, VariableValue>()
                {
                    { "variable1", "foo" },
                    { "variable2", tc.GetDirectory(WellKnownDirectory.Work) },
                };

                var taskVariables = new Dictionary <string, VariableValue>()
                {
                    { "taskVariable1", "foo" },
                    { "taskVariable2", tc.GetDirectory(WellKnownDirectory.Work) },
                };
                var executionContext = CreateTestExecutionContext(tc, variables: variables, taskVariables: taskVariables);

                var pluginContext = agentPluginManager.GeneratePluginExecutionContext(executionContext, inputs, executionContext.Variables);
                Assert.True(pluginContext != null, "PluginContext for Host Step Target is not null");
                // inputs should match exactly for Host Step Targets
                Assert.True(inputs.All(e => pluginContext.Inputs.Contains(e)));
                // variables should match exactly for Host Step Targets
                Assert.True(variables.All(e => pluginContext.Variables.Contains(e)));
                // task variables should match exactly for Host Step Targets
                Assert.True(taskVariables.All(e => pluginContext.TaskVariables.Contains(e)));
            }
        }
        private void Prepare(TestHostContext hostContext)
        {
            _trace = hostContext.GetTrace();

            // Prepare the sources directory. The workspace helper will not return any
            // matches if the sources directory does not exist with something in it.
            _sourcesDirectory = Path.Combine(hostContext.GetDirectory(WellKnownDirectory.Bin), Path.GetRandomFileName());
            _sourceFile       = Path.Combine(_sourcesDirectory, "some file");
            Directory.CreateDirectory(_sourcesDirectory);
            File.WriteAllText(path: _sourceFile, contents: "some contents");

            // Prepare a basic definition workspace.
            _workspaceName      = "ws_1_1";
            _definitionMappings = new[]
            {
                new TfsVCSourceProvider.DefinitionWorkspaceMapping
                {
                    LocalPath   = "",
                    MappingType = TfsVCSourceProvider.DefinitionMappingType.Map,
                    ServerPath  = "$/*",
                },
                new TfsVCSourceProvider.DefinitionWorkspaceMapping
                {
                    LocalPath   = "myProj",
                    MappingType = TfsVCSourceProvider.DefinitionMappingType.Map,
                    ServerPath  = "$/myProj",
                },
                new TfsVCSourceProvider.DefinitionWorkspaceMapping
                {
                    LocalPath   = "myProj/Drops",
                    MappingType = TfsVCSourceProvider.DefinitionMappingType.Cloak,
                    ServerPath  = "$/myProj/Drops",
                },
                new TfsVCSourceProvider.DefinitionWorkspaceMapping
                {
                    LocalPath   = "otherProj/mydir",
                    MappingType = TfsVCSourceProvider.DefinitionMappingType.Map,
                    ServerPath  = "$/otherProj/mydir/*",
                },
            };

            _executionContext = new Mock <IExecutionContext>();
            _executionContext
            .Setup(x => x.WriteDebug)
            .Returns(true);
            _executionContext
            .Setup(x => x.Write(It.IsAny <string>(), It.IsAny <string>()))
            .Callback((string tag, string message) => _trace.Info($"[ExecutionContext]{tag} {message}"));
        }
Exemple #30
0
        public async void MissingStartVersionShouldDownloadCommitsFromSingleBuild()
        {
            using (TestHostContext tc = Setup())
            {
                JenkinsArtifactDetails details = _artifactDefinition.Details as JenkinsArtifactDetails;
                details.EndCommitArtifactVersion = "10";

                var artifact = new JenkinsArtifact();
                artifact.Initialize(tc);
                string expectedUrl = $"{details.Url}/job/{details.JobName}/{details.EndCommitArtifactVersion}/api/json?tree=number,result,changeSet[items[commitId,date,msg,author[fullName]]]";
                await artifact.DownloadCommitsAsync(_ec.Object, _artifactDefinition, tc.GetDirectory(WellKnownDirectory.Root));

                _httpClient.Verify(x => x.GetStringAsync(It.Is <string>(y => y.StartsWith(expectedUrl)), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>()), Times.Once);
            }
        }