Search in sources :

Example 1 with ArtifactDataInstance

use of jetbrains.buildServer.artifacts.ArtifactDataInstance in project teamcity-s3-artifact-storage-plugin by JetBrains.

the class S3ArtifactsPublisherTest method testMultipleArtifactPublishesIncludedInArtifactList.

@Test
public void testMultipleArtifactPublishesIncludedInArtifactList() throws IOException {
    HashMap<File, String> artifacts1 = new HashMap<>();
    File file1 = new File("artifact1");
    artifacts1.put(file1, "");
    List<FileUploadInfo> uploadInfos1 = Collections.singletonList(new FileUploadInfo(file1.getName(), file1.getAbsolutePath(), file1.length(), null));
    HashMap<File, String> artifacts2 = new HashMap<>();
    File file2 = new File("artifact2");
    artifacts2.put(file2, "");
    List<FileUploadInfo> uploadInfos2 = Collections.singletonList(new FileUploadInfo(file2.getName(), file2.getAbsolutePath(), file2.length(), null));
    AgentArtifactHelper helper = Mockito.mock(AgentArtifactHelper.class);
    when(helper.isEnabled(any(), any())).thenReturn(true);
    AgentRunningBuild build = Mockito.mock(AgentRunningBuild.class);
    HashMap<String, String> storageSettings = new HashMap<>();
    storageSettings.put("aws.region.name", "test");
    storageSettings.put("secure:aws.secret.access.key", "test");
    storageSettings.put("aws.access.key.id", "test");
    storageSettings.put("aws.credentials.type", "aws.access.keys");
    storageSettings.put("storage.s3.bucket.name", "BUCKET_NAME");
    storageSettings.put("aws.environment", "custom");
    storageSettings.put("aws.service.endpoint", "http://localhost");
    when(build.getSharedConfigParameters()).thenReturn(Collections.emptyMap());
    when(build.getArtifactStorageSettings()).thenReturn(storageSettings);
    when(build.getBuildLogger()).thenReturn(Mockito.mock(BuildProgressLogger.class, RETURNS_DEEP_STUBS));
    CurrentBuildTracker tracker = Mockito.mock(CurrentBuildTracker.class);
    when(tracker.getCurrentBuild()).thenReturn(build);
    BuildAgentConfiguration config = Mockito.mock(BuildAgentConfiguration.class);
    when(config.getAgentHomeDirectory()).thenReturn(new File(""));
    PresignedUrlsProviderClientFactory clientFactory = Mockito.mock(PresignedUrlsProviderClientFactory.class);
    ExtensionHolder holder = Mockito.mock(ExtensionHolder.class);
    EventDispatcher<AgentLifeCycleListener> dispatcher = EventDispatcher.create(AgentLifeCycleListener.class);
    S3FileUploaderFactory uploaderFactory = Mockito.mock(S3FileUploaderFactory.class);
    final S3Configuration s3Configuration = Mockito.mock(S3Configuration.class);
    final S3UploadLogger s3UploadLogger = Mockito.mock(S3UploadLogger.class);
    S3FileUploader uploader = new S3FileUploader(s3Configuration, s3UploadLogger) {

        @Override
        public Collection<UploadStatistics> upload(@NotNull Map<File, String> filesToUpload, @NotNull Supplier<String> interrupter, Consumer<FileUploadInfo> uploadInfoConsumer) throws InvalidSettingsException {
            uploadInfos1.forEach(i -> uploadInfoConsumer.accept(i));
            return null;
        }
    };
    when(uploaderFactory.create(any(), any(), any())).thenReturn(uploader);
    S3ArtifactsPublisher publisher = new S3ArtifactsPublisher(helper, dispatcher, tracker, config, clientFactory, uploaderFactory, holder);
    publisher.publishFiles(artifacts1);
    ArgumentCaptor<List<ArtifactDataInstance>> argumentCaptor = ArgumentCaptor.forClass(List.class);
    Mockito.verify(helper, times(1)).publishArtifactList(argumentCaptor.capture(), any());
    List<ArtifactDataInstance> value1 = new ArrayList<>(argumentCaptor.getValue());
    assertEquals("First publishing run should have 1 artifact in the list", value1.size(), 1);
    S3FileUploader uploader2 = new S3FileUploader(s3Configuration, s3UploadLogger) {

        @Override
        public Collection<UploadStatistics> upload(@NotNull Map<File, String> filesToUpload, @NotNull Supplier<String> interrupter, Consumer<FileUploadInfo> uploadInfoConsumer) throws InvalidSettingsException {
            uploadInfos2.forEach(i -> uploadInfoConsumer.accept(i));
            return null;
        }
    };
    when(uploaderFactory.create(any(), any(), any())).thenReturn(uploader2);
    publisher.publishFiles(artifacts2);
    Mockito.verify(helper, times(2)).publishArtifactList(argumentCaptor.capture(), any());
    List<ArtifactDataInstance> value2 = argumentCaptor.getValue();
    assertEquals("First publishing run should have 2 artifacts in the list", value2.size(), 2);
}
Also used : NotNull(org.jetbrains.annotations.NotNull) ArtifactDataInstance(jetbrains.buildServer.artifacts.ArtifactDataInstance) Consumer(java.util.function.Consumer) FileUploadInfo(jetbrains.buildServer.artifacts.s3.FileUploadInfo) AgentArtifactHelper(jetbrains.buildServer.agent.artifacts.AgentArtifactHelper) Supplier(java.util.function.Supplier) S3UploadLogger(jetbrains.buildServer.artifacts.s3.publish.logger.S3UploadLogger) PresignedUrlsProviderClientFactory(jetbrains.buildServer.artifacts.s3.publish.presigned.upload.PresignedUrlsProviderClientFactory) S3Configuration(jetbrains.buildServer.artifacts.s3.S3Configuration) ExtensionHolder(jetbrains.buildServer.ExtensionHolder) File(java.io.File) Test(org.testng.annotations.Test)

Example 2 with ArtifactDataInstance

use of jetbrains.buildServer.artifacts.ArtifactDataInstance in project teamcity-s3-artifact-storage-plugin by JetBrains.

the class S3CleanupExtensionIntegrationTest method deletesArtifactsFromS3WithRetry.

@Test
public void deletesArtifactsFromS3WithRetry() {
    AWSCredentialsProvider credentialsProvider = myLocalStack.getDefaultCredentialsProvider();
    AwsClientBuilder.EndpointConfiguration endpointConfiguration = myLocalStack.getEndpointConfiguration(S3);
    Map<String, String> storageSettings = getStorageSettings(credentialsProvider, endpointConfiguration);
    storageSettings.put("teamcity.internal.storage.s3.upload.retryDelayMs", "500");
    storageSettings.put("teamcity.internal.storage.s3.upload.numberOfRetries", "50");
    String artifactPath = "bar";
    String expectedContents = "baz";
    ArtifactDataInstance artifact = ArtifactDataInstance.create(artifactPath, expectedContents.length());
    S3CleanupExtension cleanupExtension = getCleanupExtension(storageSettings, artifact);
    AmazonS3 s3 = getS3Client(credentialsProvider, endpointConfiguration);
    final AtomicBoolean listenerCalled = new AtomicBoolean(false);
    cleanupExtension.registerListener(new AbstractCleanupListener() {

        @Override
        public void onError(Exception exception, boolean isRecoverable) {
            if (exception instanceof AmazonS3Exception && isRecoverable) {
                listenerCalled.set(true);
                s3.createBucket(BUCKET_NAME);
                s3.putObject(BUCKET_NAME, artifactPath, expectedContents);
            }
        }
    });
    Mock contextMock = getContextMock();
    contextMock.stubs().method("onBuildCleanupError").will(throwException(new RuntimeException("Build cleanup error")));
    BuildCleanupContext context = (BuildCleanupContext) contextMock.proxy();
    cleanupExtension.prepareBuildsData(context);
    cleanupExtension.cleanupBuildsData(context);
    assertFalse(s3.doesObjectExist(BUCKET_NAME, artifactPath));
    assertTrue(listenerCalled.get());
    if (s3.doesBucketExistV2(BUCKET_NAME)) {
        s3.deleteBucket(BUCKET_NAME);
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) BuildCleanupContext(jetbrains.buildServer.serverSide.cleanup.BuildCleanupContext) AwsClientBuilder(com.amazonaws.client.builder.AwsClientBuilder) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) SkipException(org.testng.SkipException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) Mock(org.jmock.Mock) ArtifactDataInstance(jetbrains.buildServer.artifacts.ArtifactDataInstance) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider) Test(org.testng.annotations.Test)

Example 3 with ArtifactDataInstance

use of jetbrains.buildServer.artifacts.ArtifactDataInstance in project teamcity-s3-artifact-storage-plugin by JetBrains.

the class S3CleanupExtensionIntegrationTest method failsBecauseRetryDoesntHaveEnoughTime.

@Test
public void failsBecauseRetryDoesntHaveEnoughTime() {
    if (myTestLogger != null) {
        myTestLogger.doNotFailOnErrorMessages();
    }
    AWSCredentialsProvider credentialsProvider = myLocalStack.getDefaultCredentialsProvider();
    AwsClientBuilder.EndpointConfiguration endpointConfiguration = myLocalStack.getEndpointConfiguration(S3);
    Map<String, String> storageSettings = getStorageSettings(credentialsProvider, endpointConfiguration);
    storageSettings.put("teamcity.internal.storage.s3.upload.retryDelayMs", "200");
    storageSettings.put("teamcity.internal.storage.s3.upload.numberOfRetries", "5");
    String artifactPath = "bar";
    String expectedContents = "baz";
    ArtifactDataInstance artifact = ArtifactDataInstance.create(artifactPath, expectedContents.length());
    S3CleanupExtension cleanupExtension = getCleanupExtension(storageSettings, artifact);
    AtomicInteger tryCount = new AtomicInteger(0);
    cleanupExtension.registerListener(new AbstractCleanupListener() {

        @Override
        public void onError(Exception exception, boolean isRecoverable) {
            if (exception instanceof AmazonS3Exception && isRecoverable) {
                tryCount.incrementAndGet();
            }
        }
    });
    Mock contextMock = getContextMock();
    contextMock.stubs().method("onBuildCleanupError");
    BuildCleanupContext context = (BuildCleanupContext) contextMock.proxy();
    cleanupExtension.prepareBuildsData(context);
    cleanupExtension.cleanupBuildsData(context);
    assertEquals("Should try deleting object for 6 times", 6, tryCount.get());
}
Also used : BuildCleanupContext(jetbrains.buildServer.serverSide.cleanup.BuildCleanupContext) AwsClientBuilder(com.amazonaws.client.builder.AwsClientBuilder) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) SkipException(org.testng.SkipException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) Mock(org.jmock.Mock) ArtifactDataInstance(jetbrains.buildServer.artifacts.ArtifactDataInstance) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider) Test(org.testng.annotations.Test)

Aggregations

ArtifactDataInstance (jetbrains.buildServer.artifacts.ArtifactDataInstance)3 Test (org.testng.annotations.Test)3 AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)2 AwsClientBuilder (com.amazonaws.client.builder.AwsClientBuilder)2 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)2 BuildCleanupContext (jetbrains.buildServer.serverSide.cleanup.BuildCleanupContext)2 Mock (org.jmock.Mock)2 SkipException (org.testng.SkipException)2 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1 File (java.io.File)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Consumer (java.util.function.Consumer)1 Supplier (java.util.function.Supplier)1 ExtensionHolder (jetbrains.buildServer.ExtensionHolder)1 AgentArtifactHelper (jetbrains.buildServer.agent.artifacts.AgentArtifactHelper)1 FileUploadInfo (jetbrains.buildServer.artifacts.s3.FileUploadInfo)1 S3Configuration (jetbrains.buildServer.artifacts.s3.S3Configuration)1 S3UploadLogger (jetbrains.buildServer.artifacts.s3.publish.logger.S3UploadLogger)1 PresignedUrlsProviderClientFactory (jetbrains.buildServer.artifacts.s3.publish.presigned.upload.PresignedUrlsProviderClientFactory)1