diff --git a/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/ApplicationManifestUtils.java b/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/ApplicationManifestUtils.java index a82bd80608..42aed2a63f 100644 --- a/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/ApplicationManifestUtils.java +++ b/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/ApplicationManifestUtils.java @@ -23,6 +23,7 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; @@ -69,7 +70,7 @@ public static List read(Path path, Path variablesPath) { } /** - * Write {@link ApplicationManifest}s to a {@link Path} + * Write {@link ApplicationManifest}s to a {@link Path} as UTF-8 encoded YAML, replacing any existing content * * @param path the path to write to * @param applicationManifests the manifests to write @@ -79,14 +80,18 @@ public static void write(Path path, ApplicationManifest... applicationManifests) } /** - * Write {@link ApplicationManifest}s to a {@link Path} + * Write {@link ApplicationManifest}s to a {@link Path} as UTF-8 encoded YAML, replacing any existing content * * @param path the path to write to * @param applicationManifests the manifests to write */ public static void write(Path path, List applicationManifests) { try (OutputStream out = - Files.newOutputStream(path, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) { + Files.newOutputStream( + path, + StandardOpenOption.CREATE, + StandardOpenOption.TRUNCATE_EXISTING, + StandardOpenOption.WRITE)) { write(out, applicationManifests); } catch (IOException e) { throw Exceptions.propagate(e); @@ -94,7 +99,8 @@ public static void write(Path path, List applicationManifes } /** - * Write {@link ApplicationManifest}s to an {@link OutputStream} + * Write {@link ApplicationManifest}s to an {@link OutputStream} as UTF-8 encoded YAML. The stream is flushed but not + * closed. * * @param out the {@link OutputStream} to write to * @param applicationManifests the manifests to write @@ -104,13 +110,15 @@ public static void write(OutputStream out, ApplicationManifest... applicationMan } /** - * Write {@link ApplicationManifest}s to an {@link OutputStream} + * Write {@link ApplicationManifest}s to an {@link OutputStream} as UTF-8 encoded YAML. The stream is flushed but not + * closed. * * @param out the {@link OutputStream} to write to * @param applicationManifests the manifests to write */ public static void write(OutputStream out, List applicationManifests) { - try (Writer writer = new OutputStreamWriter(out)) { + try { + Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8); YAML.dump( Collections.singletonMap( "applications", @@ -118,6 +126,7 @@ public static void write(OutputStream out, List application .map(ApplicationManifestUtils::toYaml) .collect(Collectors.toList())), writer); + writer.flush(); } catch (IOException e) { throw Exceptions.propagate(e); } diff --git a/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/ApplicationManifestUtilsV3.java b/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/ApplicationManifestUtilsV3.java index 5b585b810c..f2b418f38d 100644 --- a/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/ApplicationManifestUtilsV3.java +++ b/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/ApplicationManifestUtilsV3.java @@ -23,6 +23,7 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; @@ -87,14 +88,18 @@ public static ManifestV3 read(Path path, Path variablesPath) { } /** - * Write a {@link ManifestV3} to a {@link Path} + * Write a {@link ManifestV3} to a {@link Path} as UTF-8 encoded YAML, replacing any existing content * * @param path the path to write to * @param manifest the manifest to write */ public static void write(Path path, ManifestV3 manifest) { try (OutputStream out = - Files.newOutputStream(path, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) { + Files.newOutputStream( + path, + StandardOpenOption.CREATE, + StandardOpenOption.TRUNCATE_EXISTING, + StandardOpenOption.WRITE)) { write(out, manifest); } catch (IOException e) { throw Exceptions.propagate(e); @@ -102,14 +107,17 @@ public static void write(Path path, ManifestV3 manifest) { } /** - * Write a {@link ManifestV3} to an {@link OutputStream} + * Write a {@link ManifestV3} to an {@link OutputStream} as UTF-8 encoded YAML. The stream is flushed but not + * closed. * * @param out the {@link OutputStream} to write to * @param manifest the manifest to write */ public static void write(OutputStream out, ManifestV3 manifest) { - try (Writer writer = new OutputStreamWriter(out)) { + try { + Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8); YAML.dump(toYaml(manifest), writer); + writer.flush(); } catch (IOException e) { throw Exceptions.propagate(e); } diff --git a/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/DefaultApplications.java b/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/DefaultApplications.java index ec4e7a0d6d..b57a1f76bd 100644 --- a/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/DefaultApplications.java +++ b/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/DefaultApplications.java @@ -685,17 +685,16 @@ public Mono pushManifest(PushApplicationManifestRequest request) { @Override public Mono pushManifestV3(PushManifestV3Request request) { - byte[] manifestSerialized; - try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { - ApplicationManifestUtilsV3.write(baos, request.getManifest()); - manifestSerialized = baos.toByteArray(); - } catch (IOException e) { - throw new RuntimeException("Could not serialize manifest", e); - } - return Mono.zip(this.cloudFoundryClient, this.spaceId) + return serializeManifest(request.getManifest()) + .flatMap( + manifestSerialized -> + Mono.zip( + this.cloudFoundryClient, + this.spaceId, + Mono.just(manifestSerialized))) .flatMap( function( - (cloudFoundryClient, spaceId) -> + (cloudFoundryClient, spaceId, manifestSerialized) -> applyManifestAndWaitForCompletion( cloudFoundryClient, spaceId, @@ -738,7 +737,9 @@ public Mono pushManifestV3(PushManifestV3Request request) { manifestApp, packageId, appId))))) - .then(); + .then() + .transform(OperationsLogging.log("Push Manifest V3")) + .checkpoint(); } @Override @@ -1223,6 +1224,16 @@ private static Mono buildAndStage( .map(build -> build.getDroplet().getId()); } + private static Mono serializeManifest(ManifestV3 manifest) { + return Mono.fromCallable( + () -> { + try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + ApplicationManifestUtilsV3.write(baos, manifest); + return baos.toByteArray(); + } + }); + } + private static String cleanName(ApplicationManifest manifest) { return manifest.getName().replaceAll("\\.", ""); } diff --git a/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/ApplicationManifestUtilsTest.java b/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/ApplicationManifestUtilsTest.java index 7ed8fbb9ce..e6cbba1932 100644 --- a/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/ApplicationManifestUtilsTest.java +++ b/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/ApplicationManifestUtilsTest.java @@ -23,7 +23,9 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assumptions.assumeTrue; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -33,10 +35,13 @@ import java.util.NoSuchElementException; import org.apache.commons.lang3.SystemUtils; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import org.springframework.core.io.ClassPathResource; final class ApplicationManifestUtilsTest { + @TempDir Path tempDir; + @Test void anchorsAndReferences() throws IOException { List expected = @@ -628,7 +633,7 @@ private void read(String path, String expectedManifest) throws IOException { } private void write(String path, String expectedManifest) throws IOException { - Path out = Files.createTempFile("test-manifest-", ".yml"); + Path out = this.tempDir.resolve("test-manifest.yml"); ApplicationManifestUtils.write( out, @@ -684,4 +689,32 @@ private void write(String path, String expectedManifest) throws IOException { assertThat(actual).isEqualTo(expected); } + + @Test + void writeUsesUtf8() { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ApplicationManifestUtils.write( + out, + ApplicationManifest.builder() + .name("test-app") + .environmentVariable("CITY", "M\u00fcnchen") + .build()); + + assertThat(new String(out.toByteArray(), StandardCharsets.UTF_8)).contains("M\u00fcnchen"); + } + + @Test + void writeTruncatesExistingFile() throws IOException { + Path file = this.tempDir.resolve("test-manifest.yml"); + Files.write(file, new byte[4096]); + + ApplicationManifest manifest = + ApplicationManifest.builder() + .name("b") + .path(Paths.get("/test-path").toAbsolutePath()) + .build(); + ApplicationManifestUtils.write(file, manifest); + + assertThat(ApplicationManifestUtils.read(file)).containsExactly(manifest); + } } diff --git a/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/ApplicationManifestUtilsV3Test.java b/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/ApplicationManifestUtilsV3Test.java index 3e05995f57..ee70424d4e 100644 --- a/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/ApplicationManifestUtilsV3Test.java +++ b/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/ApplicationManifestUtilsV3Test.java @@ -1,16 +1,24 @@ package org.cloudfoundry.operations.applications; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.ByteArrayOutputStream; +import java.io.FilterOutputStream; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.Map; import org.cloudfoundry.client.v3.Metadata; import org.cloudfoundry.client.v3.processes.ReadinessHealthCheckType; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; class ApplicationManifestUtilsV3Test { + @TempDir Path tempDir; + @Test void testWithDockerApp() throws IOException { ManifestV3 manifest = @@ -148,8 +156,62 @@ void testWithMetadataOnlyAnnotation() throws IOException { assertSerializeDeserialize(manifest); } + @Test + void testWriteUsesUtf8() throws IOException { + ManifestV3 manifest = + ManifestV3.builder() + .application( + ManifestV3Application.builder() + .name("test-app") + .environmentVariable("CITY", "M\u00fcnchen") + .build()) + .build(); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ApplicationManifestUtilsV3.write(out, manifest); + + assertTrue(new String(out.toByteArray(), StandardCharsets.UTF_8).contains("M\u00fcnchen")); + } + + @Test + void testWriteTruncatesExistingFile() throws IOException { + Path file = this.tempDir.resolve("test-manifest.yml"); + Files.write(file, new byte[4096]); + + ManifestV3 manifest = + ManifestV3.builder() + .application(ManifestV3Application.builder().name("b").build()) + .build(); + ApplicationManifestUtilsV3.write(file, manifest); + + assertEquals(manifest, ApplicationManifestUtilsV3.read(file)); + } + + @Test + void testWriteDoesNotCloseStream() { + boolean[] closed = {false}; + ByteArrayOutputStream delegate = new ByteArrayOutputStream(); + FilterOutputStream out = + new FilterOutputStream(delegate) { + @Override + public void close() throws IOException { + closed[0] = true; + super.close(); + } + }; + + ApplicationManifestUtilsV3.write( + out, + ManifestV3.builder() + .application(ManifestV3Application.builder().name("test-app").build()) + .build()); + + assertFalse(closed[0]); + assertTrue(new String(delegate.toByteArray(), StandardCharsets.UTF_8).contains("test-app")); + } + private void assertSerializeDeserialize(ManifestV3 manifest) throws IOException { - Path file = Files.createTempFile("test-manifest-", ".yml"); + Path file = this.tempDir.resolve("test-manifest.yml"); ApplicationManifestUtilsV3.write(file, manifest); ManifestV3 read = ApplicationManifestUtilsV3.read(file); diff --git a/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/DefaultApplicationsTest.java b/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/DefaultApplicationsTest.java index 3fbffef118..34e283ec9b 100644 --- a/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/DefaultApplicationsTest.java +++ b/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/DefaultApplicationsTest.java @@ -177,6 +177,42 @@ final class DefaultApplicationsTest extends AbstractOperationsTest { this.randomWords, Mono.just(TEST_SPACE_ID)); + @Test + void pushManifestV3IsLazyAndReportsErrorsThroughMono() { + java.util.concurrent.atomic.AtomicBoolean subscribed = + new java.util.concurrent.atomic.AtomicBoolean(); + DefaultApplications applications = + new DefaultApplications( + Mono.just(this.cloudFoundryClient), + Mono.just(this.dopplerClient), + Mono.just(this.logCacheClient), + this.randomWords, + Mono.error(new IllegalStateException("test-space-error")) + .doOnSubscribe(s -> subscribed.set(true))); + + Mono result = + applications.pushManifestV3( + PushManifestV3Request.builder() + .manifest( + ManifestV3.builder() + .application( + ManifestV3Application.builder() + .name("test-app") + .build()) + .build()) + .build()); + + assertThat(subscribed).isFalse(); + + StepVerifier.create(result) + .consumeErrorWith( + t -> + assertThat(t) + .isInstanceOf(IllegalStateException.class) + .hasMessage("test-space-error")) + .verify(Duration.ofSeconds(5)); + } + @Test void copySourceNoRestartOrgSpace() { requestApplications(