Example #1
0
        public ExportAnonymizerFactory(IArtifactProvider artifactProvider, ILogger <ExportJobTask> logger)
        {
            EnsureArg.IsNotNull(artifactProvider, nameof(artifactProvider));
            EnsureArg.IsNotNull(logger, nameof(logger));

            _artifactProvider = artifactProvider;
            _logger           = logger;
        }
        public async Task GivenNoAnonymizationConfiguration_WhenCreatingAnonymizer_CorrectExceptionShouldBeThrow()
        {
            IArtifactProvider client = Substitute.For <IArtifactProvider>();

            client.FetchAsync(Arg.Any <string>(), Arg.Any <Stream>(), Arg.Any <CancellationToken>()).Returns <Task>(
                x =>
            {
                throw new FileNotFoundException();
            });

            ILogger <ExportJobTask> logger = Substitute.For <ILogger <ExportJobTask> >();

            ExportAnonymizerFactory factory = new ExportAnonymizerFactory(client, logger);

            _ = await Assert.ThrowsAsync <AnonymizationConfigurationNotFoundException>(() => factory.CreateAnonymizerAsync("http://dummy", CancellationToken.None));
        }
        public async Task GivenAnInvalidAnonymizationConfiguration_WhenCreatingAnonymizer_CorrectExceptionShouldBeThrow()
        {
            IArtifactProvider client = Substitute.For <IArtifactProvider>();

            client.FetchAsync(Arg.Any <string>(), Arg.Any <Stream>(), Arg.Any <CancellationToken>()).Returns <Task>(
                x =>
            {
                Stream target = x.ArgAt <Stream>(1);
                target.Write(Encoding.UTF8.GetBytes(InvalidConfiguration), 0, SampleConfiguration.Length);
                return(Task.CompletedTask);
            });

            ILogger <ExportJobTask> logger = Substitute.For <ILogger <ExportJobTask> >();

            ExportAnonymizerFactory factory = new ExportAnonymizerFactory(client, logger);

            _ = await Assert.ThrowsAsync <FailedToParseAnonymizationConfigurationException>(() => factory.CreateAnonymizerAsync("http://dummy", CancellationToken.None));
        }
Example #4
0
        private async Task <IAnonymizer> CreateAnonymizerFromConfigContent(string configuration)
        {
            IArtifactProvider client = Substitute.For <IArtifactProvider>();

            client.FetchAsync(Arg.Any <string>(), Arg.Any <Stream>(), Arg.Any <CancellationToken>()).Returns <Task>(
                x =>
            {
                Stream target = x.ArgAt <Stream>(1);
                target.Write(Encoding.UTF8.GetBytes(configuration), 0, configuration.Length);
                return(Task.CompletedTask);
            });

            ILogger <ExportJobTask> logger = Substitute.For <ILogger <ExportJobTask> >();

            ExportAnonymizerFactory factory = new ExportAnonymizerFactory(client, logger);

            return(await factory.CreateAnonymizerAsync("http://dummy", CancellationToken.None));
        }
        public async Task GivenAValidAnonymizationConfiguration_WhenCreatingAnonymizer_AnonymizerShouldBeCreated()
        {
            IArtifactProvider client = Substitute.For <IArtifactProvider>();

            client.FetchAsync(Arg.Any <string>(), Arg.Any <Stream>(), Arg.Any <CancellationToken>()).Returns <Task>(
                x =>
            {
                Stream target = x.ArgAt <Stream>(1);
                target.Write(Encoding.UTF8.GetBytes(SampleConfiguration), 0, SampleConfiguration.Length);
                return(Task.CompletedTask);
            });

            ILogger <ExportJobTask> logger = Substitute.For <ILogger <ExportJobTask> >();

            ExportAnonymizerFactory factory    = new ExportAnonymizerFactory(client, logger);
            IAnonymizer             anonymizer = await factory.CreateAnonymizerAsync("http://dummy", CancellationToken.None);

            Assert.NotNull(anonymizer);
        }
Example #6
0
        // Download for version 2. This decision was made because version 1 is sealed and we didn't want to break any existing customers.
        internal async Task DownloadAsyncV2(
            AgentTaskPluginExecutionContext context,
            PipelineArtifactDownloadParameters downloadParameters,
            DownloadOptions downloadOptions,
            CancellationToken cancellationToken)
        {
            VssConnection connection  = context.VssConnection;
            BuildServer   buildHelper = new BuildServer(connection);

            // download all pipeline artifacts if artifact name is missing
            if (downloadOptions == DownloadOptions.MultiDownload)
            {
                List <BuildArtifact> artifacts;
                if (downloadParameters.ProjectRetrievalOptions == BuildArtifactRetrievalOptions.RetrieveByProjectId)
                {
                    artifacts = await buildHelper.GetArtifactsAsync(downloadParameters.ProjectId, downloadParameters.PipelineId, cancellationToken);
                }
                else if (downloadParameters.ProjectRetrievalOptions == BuildArtifactRetrievalOptions.RetrieveByProjectName)
                {
                    if (string.IsNullOrEmpty(downloadParameters.ProjectName))
                    {
                        throw new InvalidOperationException("Project name can't be empty when trying to fetch build artifacts!");
                    }
                    else
                    {
                        artifacts = await buildHelper.GetArtifactsWithProjectNameAsync(downloadParameters.ProjectName, downloadParameters.PipelineId, cancellationToken);
                    }
                }
                else
                {
                    throw new InvalidOperationException($"Invalid {nameof(downloadParameters.ProjectRetrievalOptions)}!");
                }

                IEnumerable <BuildArtifact> buildArtifacts    = artifacts.Where(a => a.Resource.Type == PipelineArtifactConstants.Container);
                IEnumerable <BuildArtifact> pipelineArtifacts = artifacts.Where(a => a.Resource.Type == PipelineArtifactConstants.PipelineArtifact);
                if (buildArtifacts.Any())
                {
                    FileContainerProvider provider = new FileContainerProvider(connection, this.CreateTracer(context));
                    await provider.DownloadMultipleArtifactsAsync(downloadParameters, buildArtifacts, cancellationToken);
                }

                if (pipelineArtifacts.Any())
                {
                    PipelineArtifactProvider provider = new PipelineArtifactProvider(context, connection, this.CreateTracer(context));
                    await provider.DownloadMultipleArtifactsAsync(downloadParameters, pipelineArtifacts, cancellationToken);
                }
            }
            else if (downloadOptions == DownloadOptions.SingleDownload)
            {
                // 1) get manifest id from artifact data
                BuildArtifact buildArtifact;
                if (downloadParameters.ProjectRetrievalOptions == BuildArtifactRetrievalOptions.RetrieveByProjectId)
                {
                    buildArtifact = await buildHelper.GetArtifact(downloadParameters.ProjectId, downloadParameters.PipelineId, downloadParameters.ArtifactName, cancellationToken);
                }
                else if (downloadParameters.ProjectRetrievalOptions == BuildArtifactRetrievalOptions.RetrieveByProjectName)
                {
                    if (string.IsNullOrEmpty(downloadParameters.ProjectName))
                    {
                        throw new InvalidOperationException("Project name can't be empty when trying to fetch build artifacts!");
                    }
                    else
                    {
                        buildArtifact = await buildHelper.GetArtifactWithProjectNameAsync(downloadParameters.ProjectName, downloadParameters.PipelineId, downloadParameters.ArtifactName, cancellationToken);
                    }
                }
                else
                {
                    throw new InvalidOperationException($"Invalid {nameof(downloadParameters.ProjectRetrievalOptions)}!");
                }

                ArtifactProviderFactory factory  = new ArtifactProviderFactory(context, connection, this.CreateTracer(context));
                IArtifactProvider       provider = factory.GetProvider(buildArtifact);
                await provider.DownloadSingleArtifactAsync(downloadParameters, buildArtifact, cancellationToken);
            }
            else
            {
                throw new InvalidOperationException($"Invalid {nameof(downloadOptions)}!");
            }
        }