tillrohrmann commented on a change in pull request #10532: [FLINK-15053][runtime] Escape all dynamical property values for taskmanager URL: https://github.com/apache/flink/pull/10532#discussion_r360342044
########## File path: flink-tests/src/test/java/org/apache/flink/test/runtime/TaskManagerLoadingDynamicPropertiesITCase.java ########## @@ -0,0 +1,158 @@ +/* + * 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.test.runtime; + +import org.apache.flink.configuration.Configuration; +import org.apache.flink.runtime.clusterframework.BootstrapTools; +import org.apache.flink.runtime.entrypoint.FlinkParseException; +import org.apache.flink.runtime.taskexecutor.TaskManagerRunner; +import org.apache.flink.runtime.testutils.CommonTestUtils; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeUnit; + +import static org.apache.flink.configuration.GlobalConfiguration.FLINK_CONF_FILENAME; +import static org.apache.flink.runtime.testutils.CommonTestUtils.getCurrentClasspath; +import static org.apache.flink.runtime.testutils.CommonTestUtils.getJavaCommandPath; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Tests for passing and loading dynamical properties of task manager. + * Usually(in Yarn, Kubernetes), the taskmanager java start commands are wrapped within bash. This test will generate a + * launch_container.sh script to validate the dynamical properties passing and loading. + */ +public class TaskManagerLoadingDynamicPropertiesITCase { + + private static final String KEY_A = "key.a"; + private static final String VALUE_A = "value-of-a"; + private static final String KEY_B = "key.b"; + private static final String VALUE_B = "#a,b&c^d*e%f(g!h"; + private static final String KEY_C = "key.c"; + private static final String VALUE_C = "my'value is 1"; + + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void testLoadingDynamicPropertiesInBash() throws Exception { + final Configuration clientConfiguration = new Configuration(); + clientConfiguration.setString("client.key.a", "value"); + final File root = folder.getRoot(); + final File homeDir = new File(root, "home"); + assertTrue(homeDir.mkdir()); + BootstrapTools.writeConfiguration(clientConfiguration, new File(homeDir, FLINK_CONF_FILENAME)); + + final Configuration jmUpdatedConfiguration = new Configuration(); + jmUpdatedConfiguration.setString(KEY_A, VALUE_A); + jmUpdatedConfiguration.setString(KEY_B, VALUE_B); + jmUpdatedConfiguration.setString(KEY_C, VALUE_C); + final String[] dynamicProperties = BootstrapTools.getDynamicProperties(clientConfiguration, jmUpdatedConfiguration); + + UnixShellScriptBuilder shellScriptBuilder = new UnixShellScriptBuilder(); + final List<String> commands = new ArrayList<>(); + commands.add(getJavaCommandPath()); + commands.add(TestingTaskManagerRunner.class.getName().replace("$", "'$'")); + commands.add("--configDir"); + commands.add(homeDir.getAbsolutePath()); + commands.addAll(Arrays.asList(dynamicProperties)); + shellScriptBuilder.env("CLASSPATH", getCurrentClasspath()); + shellScriptBuilder.command(commands); + final File shellScriptFile = new File(homeDir, "launch_container.sh"); + shellScriptBuilder.write(shellScriptFile); + + final StringWriter processOutput = new StringWriter(); + final Process process = new ProcessBuilder("sh", shellScriptFile.getAbsolutePath()).start(); + new CommonTestUtils.PipeForwarder(process.getErrorStream(), processOutput); + + if (!process.waitFor(10, TimeUnit.SECONDS)) { + throw new Exception("TestingTaskManagerRunner did not shutdown in time."); + } + assertEquals(processOutput.toString(), 0, process.exitValue()); + + process.destroy(); + } + + // -------------------------------------------------------------------------------------------- + + /** + * The testing taskmanager runner. The exit code will be 0 if configuration values check passed. + */ + public static class TestingTaskManagerRunner { + + public static void main(String[] args) { + try { + final Configuration flinkConfig = TaskManagerRunner.loadConfiguration(args); + assertEquals(VALUE_A, flinkConfig.toMap().get(KEY_A)); + assertEquals(VALUE_B, flinkConfig.toMap().get(KEY_B)); + assertEquals(VALUE_C, flinkConfig.toMap().get(KEY_C)); + } catch (FlinkParseException e) { + System.err.println("Loading dynamical properties failed. " + e); + System.exit(1); + } + System.exit(0); + } + } + + /** + * UnixShellScriptBuilder for creating shell script to start taskmanager with dynamical properties. + */ + private static final class UnixShellScriptBuilder { + + private static final String LINE_SEPARATOR = System.getProperty("line.separator"); + private final StringBuilder sb = new StringBuilder(); + + private void line(String... command) { + for (String s : command) { + sb.append(s); + } + sb.append(LINE_SEPARATOR); + } + + UnixShellScriptBuilder(){ + line("#!/bin/bash"); + line(); + } + + void command(List<String> command) { + line("exec /bin/bash -c \"", String.join(" ", command), "\""); Review comment: How do we deal with different platforms? E.g. what happens if this test runs on Windows? ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
