Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,7 +70,7 @@ public static List<ApplicationManifest> 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
Expand All @@ -79,22 +80,27 @@ 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<ApplicationManifest> 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);
}
}

/**
* 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
Expand All @@ -104,20 +110,23 @@ 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<ApplicationManifest> applicationManifests) {
try (Writer writer = new OutputStreamWriter(out)) {
try {
Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
YAML.dump(
Collections.singletonMap(
"applications",
applicationManifests.stream()
.map(ApplicationManifestUtils::toYaml)
.collect(Collectors.toList())),
writer);
writer.flush();
} catch (IOException e) {
throw Exceptions.propagate(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,29 +88,36 @@ 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);
}
}

/**
* 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,17 +685,16 @@ public Mono<Void> pushManifest(PushApplicationManifestRequest request) {

@Override
public Mono<Void> 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,
Expand Down Expand Up @@ -738,7 +737,9 @@ public Mono<Void> pushManifestV3(PushManifestV3Request request) {
manifestApp,
packageId,
appId)))))
.then();
.then()
.transform(OperationsLogging.log("Push Manifest V3"))
.checkpoint();
}

@Override
Expand Down Expand Up @@ -1223,6 +1224,16 @@ private static Mono<String> buildAndStage(
.map(build -> build.getDroplet().getId());
}

private static Mono<byte[]> 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("\\.", "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ApplicationManifest> expected =
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down Expand Up @@ -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};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you using an array here?

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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.<String>error(new IllegalStateException("test-space-error"))
.doOnSubscribe(s -> subscribed.set(true)));

Mono<Void> 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(
Expand Down
Loading