mxm commented on a change in pull request #10811: [FLINK-15504] Allow output to 
stdout/stderr during execution of PackagedProgram
URL: https://github.com/apache/flink/pull/10811#discussion_r364825383
 
 

 ##########
 File path: 
flink-runtime-web/src/test/java/org/apache/flink/runtime/webmonitor/handlers/JarHandlerTest.java
 ##########
 @@ -0,0 +1,163 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.runtime.webmonitor.handlers;
+
+import org.apache.flink.api.common.time.Time;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.RestOptions;
+import org.apache.flink.configuration.WebOptions;
+import org.apache.flink.runtime.rest.RestClient;
+import org.apache.flink.runtime.rest.RestClientConfiguration;
+import org.apache.flink.runtime.rest.messages.MessageHeaders;
+import org.apache.flink.runtime.rest.util.RestClientException;
+import org.apache.flink.runtime.testingUtils.TestingUtils;
+import org.apache.flink.runtime.testutils.MiniClusterResource;
+import org.apache.flink.runtime.testutils.MiniClusterResourceConfiguration;
+import org.apache.flink.testutils.junit.category.AlsoRunWithLegacyScheduler;
+import org.apache.flink.util.ExceptionUtils;
+import org.apache.flink.util.TestLogger;
+
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.IOException;
+import java.net.URI;
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.HashMap;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+/**
+ * Tests for the {@link JarRunHandler} and  {@link JarPlanHandler}.
+ */
+@Category(AlsoRunWithLegacyScheduler.class)
+public class JarHandlerTest extends TestLogger {
+
+       @ClassRule
+       public static final TemporaryFolder TMP = new TemporaryFolder();
+
+       enum Type {
+               PLAN,
+               RUN
+       }
+
+       @Test
+       public void testPlanJar() throws Exception {
+               runTest(Type.PLAN, "hello out!", "hello err!");
+       }
+
+       @Test
+       public void testRunJar() throws Exception {
+               runTest(Type.RUN, "(none)", "(none)");
+       }
+
+       private static void runTest(Type type, String expectedCapturedStdOut, 
String expectedCapturedStdErr) throws Exception {
+               Path uploadDir = TMP.newFolder().toPath();
+
+               Path actualUploadDir = uploadDir.resolve("flink-web-upload");
+               Files.createDirectory(actualUploadDir);
+
+               Path emptyJar = actualUploadDir.resolve("empty.jar");
+               createJarFile(emptyJar);
+
+               Configuration config = new Configuration();
+               config.setString(WebOptions.UPLOAD_DIR, uploadDir.toString());
+
+               MiniClusterResource clusterResource = new MiniClusterResource(
+                       new MiniClusterResourceConfiguration.Builder()
+                               .setConfiguration(config)
+                               .setNumberTaskManagers(1)
+                               .setNumberSlotsPerTaskManager(1)
+                               .build());
+               clusterResource.before();
+
+               try {
+                       Configuration clientConfig = 
clusterResource.getClientConfiguration();
+                       RestClient client = new 
RestClient(RestClientConfiguration.fromConfiguration(clientConfig), 
TestingUtils.defaultExecutor());
+
+                       try {
+                               final MessageHeaders headers;
+                               final JarMessageParameters parameters;
+                               if (type == Type.RUN) {
+                                       headers = JarRunHeaders.getInstance();
+                                       parameters = ((JarRunHeaders) 
headers).getUnresolvedMessageParameters();
+                               } else if (type == Type.PLAN) {
+                                       headers = 
JarPlanGetHeaders.getInstance();
+                                       parameters = ((JarPlanGetHeaders) 
headers).getUnresolvedMessageParameters();
+                               } else {
+                                       throw new RuntimeException("Invalid 
type: " + type);
+                               }
+                               
parameters.jarIdPathParameter.resolve(emptyJar.getFileName().toString());
+
+                               String host = 
clientConfig.getString(RestOptions.ADDRESS);
+                               int port = 
clientConfig.getInteger(RestOptions.PORT);
+
+                               try {
+                                       client.sendRequest(host, port, headers, 
parameters, new JarPlanRequestBody())
+                                               .get();
+                               } catch (Exception e) {
+                                       Optional<RestClientException> expected 
= ExceptionUtils.findThrowable(e, RestClientException.class);
+                                       if (expected.isPresent()) {
+                                               // implies the job was actually 
submitted
+                                               
assertThat(expected.get().getMessage(), 
containsString("ProgramInvocationException"));
+                                               // original cause is preserved 
in stack trace
+                                               
assertThat(expected.get().getMessage(), containsString("The program plan could 
not be fetched - the program aborted pre-maturely"));
+                                               // implies the jar was 
registered for the job graph (otherwise the jar name would not occur in the 
exception)
+                                               // implies the jar was uploaded 
(otherwise the file would not be found at all)
+                                               
assertThat(expected.get().getMessage(), containsString("empty.jar"));
+                                               // ensure that no stdout/stderr 
has been captured
+                                               
assertThat(expected.get().getMessage(), containsString("System.out: " + 
expectedCapturedStdOut));
+                                               
assertThat(expected.get().getMessage(), containsString("System.err: " + 
expectedCapturedStdErr));
+                                       } else {
+                                               throw e;
+                                       }
+                               }
+                       } finally {
+                               client.shutdown(Time.milliseconds(10));
+                       }
+               } finally {
+                       clusterResource.after();
+               }
+       }
+
+       private static void createJarFile(Path zipFile) throws IOException {
+               URI uri = URI.create("jar:file:" + zipFile.toString());
+               HashMap<String, Object> env = new HashMap<>();
+               // We need this to ensure the file will be created if it does 
not exist
+               env.put("create", "true");
+               try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
+                       Files.createDirectory(zipfs.getPath("META-INF"));
+                       Path manifest = zipfs.getPath("META-INF/MANIFEST.MF");
+                       Files.write(manifest, "Manifest-Version: 
1.0\nCreated-By: Apache Flink\nMain-Class: HelloWorld\n".getBytes());
+
+                       Path content = zipfs.getPath("HelloWorld.class");
+                       Files.write(content, new byte[] {
+                               // A program that prints 'hello out!' to stdout 
and 'hello err!' to stderr
 
 Review comment:
   That makes sense. I will add this code as a comment:
   
   ```java
   public class HelloWorld {
     public static void main(String[] args) {
       System.out.println("hello out!");
       System.err.println("hello err!");
     }
   }
   
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to