wangyang0918 commented on a change in pull request #16286: URL: https://github.com/apache/flink/pull/16286#discussion_r661131964
########## File path: flink-clients/src/test/java/org/apache/flink/client/testjar/ClasspathProvider.java ########## @@ -0,0 +1,226 @@ +/* + * 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.client.testjar; + +import org.apache.flink.client.deployment.application.JarManifestParser; +import org.apache.flink.client.program.PackagedProgram; + +import org.junit.rules.ExternalResource; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +/** {@code ClasspathProvider} offers methods to generate classpaths based on actual jars. */ +public class ClasspathProvider extends ExternalResource { + + private static final String CLASSPATH_PROPERTY_NAME = "java.class.path"; + + private static final Path TEST_JOB_JAR_PATH = Paths.get("target", "maven-test-jar.jar"); + + private static final Path JOB_JAR_PATH = + Paths.get("target", "maven-test-user-classloader-job-jar.jar"); + private static final Path JOB_LIB_JAR_PATH = + Paths.get("target", "maven-test-user-classloader-job-lib-jar.jar"); + + private final TemporaryFolder temporaryFolder = new TemporaryFolder(); + + private File userDirHasTwoEntryClasses; + + private File userDirHasEntryClass; + + private File userDirHasNotEntryClass; + + // this was added to support the legacy tests of ClassPathPackagedProgramRetrieverTest in the + // refactored PackagedProgramRetrieverImplTest + private File userDirWithTestJob; + + private File userDirWithNoJarButTextFile; + + private String originalSystemClasspath; + + @Override + public void before() throws IOException { + temporaryFolder.create(); + + userDirHasTwoEntryClasses = + temporaryFolder.newFolder("_test_user_dir_has_two_entry_classes"); + copyJar(JOB_JAR_PATH, userDirHasTwoEntryClasses); + copyJar(TEST_JOB_JAR_PATH, userDirHasTwoEntryClasses); + copyJar(JOB_LIB_JAR_PATH, userDirHasTwoEntryClasses); + createTestFile(userDirHasTwoEntryClasses); + + userDirHasEntryClass = temporaryFolder.newFolder("_test_user_dir_has_entry_class"); + copyJar(JOB_JAR_PATH, userDirHasEntryClass); + copyJar(JOB_LIB_JAR_PATH, userDirHasEntryClass); + createTestFile(userDirHasEntryClass); + + userDirHasNotEntryClass = temporaryFolder.newFolder("_test_user_dir_has_not_entry_class"); + copyJar(JOB_LIB_JAR_PATH, userDirHasNotEntryClass); + createTestFile(userDirHasNotEntryClass); + + userDirWithTestJob = temporaryFolder.newFolder("_with_test_job"); + copyJar(TEST_JOB_JAR_PATH, userDirWithTestJob); + + userDirWithNoJarButTextFile = temporaryFolder.newFolder("_with_no_jar"); + createTestFile(userDirWithNoJarButTextFile); + } + + private static void copyJar(Path sourcePath, File targetDir) throws IOException { + Files.copy(sourcePath, targetDir.toPath().resolve(sourcePath.toFile().getName())); + } + + private static void createTestFile(File targetDir) throws IOException { + Files.createFile(targetDir.toPath().resolve("test.txt")); + } + + @Override + protected void after() { + temporaryFolder.delete(); + resetSystemClasspath(); + } + + public String getJobClassName() { + return extractEntryClassNameFromJar(JOB_JAR_PATH.toFile()); + } + + public String getTestJobClassName() { + return extractEntryClassNameFromJar(TEST_JOB_JAR_PATH.toFile()); + } + + private static String extractEntryClassNameFromJar(File f) { + try { + return JarManifestParser.findFirstManifestAttribute( + f, PackagedProgram.MANIFEST_ATTRIBUTE_MAIN_CLASS) + .orElseThrow( + () -> + new IllegalArgumentException( + "The passed file does not contain a main class: " + + f.getAbsolutePath())); + } catch (Throwable t) { + throw new AssertionError( + "Something went wrong with retrieving the main class from " + + f.getAbsolutePath(), + t); + } + } + + public File getUserDirectoryWithNoEntryClass() { + return userDirHasNotEntryClass; + } + + public File getUserDirectoryWithOneEntryClass() { + return userDirHasEntryClass; + } + + public File getUserDirectoryWithTwoEntryClasses() { + return userDirHasTwoEntryClasses; + } + + public File getUserDirectoryWithTestJob() { + return userDirWithTestJob; + } + + public String[] getTestJobArgs(String expectedSuffix) { + return new String[] {"--arg", expectedSuffix}; + } + + public Iterable<URL> getURLUserClasspathWithTwoEntryClasses() throws MalformedURLException { + return getURLUserClasspath(userDirHasTwoEntryClasses); + } + + public Iterable<URL> getURLUserClasspathWithEntryClass() throws MalformedURLException { + return getURLUserClasspath(userDirHasEntryClass); + } + + public Iterable<URL> getURLUserClasspathWithoutEntryClass() throws MalformedURLException { + return getURLUserClasspath(userDirHasNotEntryClass); + } + + public Iterable<URL> getURLUserClasspathWithTestJob() throws MalformedURLException { + return getURLUserClasspath(userDirWithTestJob); + } + + public Iterable<URL> getURLUserClasspathWithOnlyTextFile() throws MalformedURLException { + return getURLUserClasspath(userDirWithNoJarButTextFile); + } + + public void setSystemClasspathWithTwoEntryClasses() throws MalformedURLException { + setSystemClasspath(getURLUserClasspathWithTwoEntryClasses()); + } + + public void setSystemClasspathWithEntryClass() throws MalformedURLException { + setSystemClasspath(getURLUserClasspathWithEntryClass()); + } + + public void setSystemClasspathWithoutEntryClass() throws MalformedURLException { + setSystemClasspath(getURLUserClasspathWithoutEntryClass()); + } + + public void setSystemClasspathWithTestJob() throws MalformedURLException { Review comment: Never used. Right? ########## File path: flink-clients/src/main/java/org/apache/flink/client/deployment/application/FromJarEntryClassInformationProvider.java ########## @@ -0,0 +1,76 @@ +/* + * 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.client.deployment.application; + +import org.apache.flink.client.program.PackagedProgramUtils; +import org.apache.flink.util.Preconditions; + +import java.io.File; +import java.util.Optional; + +/** + * {@code FromJarEntryClassInformationProvider} returns a is used for cases where the Jar archive is + * explicitly specified. + */ +public class FromJarEntryClassInformationProvider implements EntryClassInformationProvider { + + private final File jarFile; + private final String jobClassName; Review comment: IIUC, we do not need to inheritance from `FromClasspathEntryClassInformationProvider`. ########## File path: flink-clients/src/main/java/org/apache/flink/client/deployment/application/FromJarEntryClassInformationProvider.java ########## @@ -0,0 +1,76 @@ +/* + * 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.client.deployment.application; + +import org.apache.flink.client.program.PackagedProgramUtils; +import org.apache.flink.util.Preconditions; + +import java.io.File; +import java.util.Optional; + +/** + * {@code FromJarEntryClassInformationProvider} returns a is used for cases where the Jar archive is + * explicitly specified. + */ +public class FromJarEntryClassInformationProvider implements EntryClassInformationProvider { + + private final File jarFile; + private final String jobClassName; + + /** + * Creates a {@code FromJarEntryClassInformationProvider} for a custom Jar archive. + * + * @param jarFile The Jar archive. + * @param jobClassName The name of the job class. + * @return The {@code FromJarEntryClassInformationProvider} referring to the passed information. + */ + public static FromJarEntryClassInformationProvider createFromCustomJar( + File jarFile, String jobClassName) { Review comment: Maybe add `Nullable` here is better. ########## File path: flink-clients/src/test/java/org/apache/flink/client/program/PackagedProgramRetrieverImplTest.java ########## @@ -372,6 +372,35 @@ public void testRetrieveCorrectUserClasspathsWithoutSpecifiedEntryClass() IsIterableContainingInAnyOrder.containsInAnyOrder(expectedClasspath.toArray())); } + @Test + public void testRetrieveCorrectUserClasspathsWithSpecifiedEntryClass() + throws IOException, FlinkException, ProgramInvocationException { + final PackagedProgramRetriever retrieverUnderTest = + PackagedProgramRetrieverImpl.create( + classpathProvider.getUserDirectoryWithOneEntryClass(), + classpathProvider.getJobClassName(), + classpathProvider.getTestJobArgs("suffix")); + final JobGraph jobGraph = retrieveJobGraph(retrieverUnderTest, new Configuration()); + final List<String> actualClasspath = + jobGraph.getClasspaths().stream().map(URL::toString).collect(Collectors.toList()); + + final Path workingDirectory = FileUtils.getCurrentWorkingDirectory(); + final List<String> expectedClasspath = new ArrayList<>(); + for (File file : classpathProvider.getUserDirectoryWithOneEntryClass().listFiles()) { + if (!file.getName().endsWith("jar")) { + // only jars are expected + continue; + } + + Path relativePath = FileUtils.relativizePath(workingDirectory, file.toPath()); + expectedClasspath.add(FileUtils.toURL(relativePath).toString()); + } Review comment: Could be deduplicated by introducing a new method. ########## File path: flink-clients/src/test/java/org/apache/flink/client/testjar/ClasspathProvider.java ########## @@ -0,0 +1,226 @@ +/* + * 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.client.testjar; + +import org.apache.flink.client.deployment.application.JarManifestParser; +import org.apache.flink.client.program.PackagedProgram; + +import org.junit.rules.ExternalResource; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +/** {@code ClasspathProvider} offers methods to generate classpaths based on actual jars. */ +public class ClasspathProvider extends ExternalResource { + + private static final String CLASSPATH_PROPERTY_NAME = "java.class.path"; + + private static final Path TEST_JOB_JAR_PATH = Paths.get("target", "maven-test-jar.jar"); + + private static final Path JOB_JAR_PATH = + Paths.get("target", "maven-test-user-classloader-job-jar.jar"); + private static final Path JOB_LIB_JAR_PATH = + Paths.get("target", "maven-test-user-classloader-job-lib-jar.jar"); + + private final TemporaryFolder temporaryFolder = new TemporaryFolder(); + + private File userDirHasTwoEntryClasses; + + private File userDirHasEntryClass; + + private File userDirHasNotEntryClass; + + // this was added to support the legacy tests of ClassPathPackagedProgramRetrieverTest in the Review comment: Actually, this comment confused me. Do you mean the related tests from legacy `ClassPathPackagedProgramRetrieverTest` will be removed or is outdated? ########## File path: flink-clients/src/test/java/org/apache/flink/client/deployment/application/FromJarEntryClassInformationProviderTest.java ########## @@ -72,9 +73,13 @@ public void testEitherJobClassNameOrJarHasToBeSet() { FromJarEntryClassInformationProvider.createFromCustomJar(null, null); } - @Ignore // FLINK_OPT_DIR has to be set @Test public void testPythonJarFile() { + // TODO: see FLINK-23154 + Assume.assumeTrue( + "This tests only succeeds if " + ConfigConstants.ENV_FLINK_OPT_DIR + " is set.", + System.getProperty(ConfigConstants.ENV_FLINK_OPT_DIR) != null); Review comment: Maybe we need to use `System.getenv`. Refer to `PackagedProgramUtils#getPythonJar`. ########## File path: flink-clients/src/test/java/org/apache/flink/client/program/PackagedProgramRetrieverImplTest.java ########## @@ -285,6 +287,26 @@ public void testSavepointRestoreSettings() assertThat(jobGraph.getJobID(), is(jobId)); } + @Test + public void testFailIfJobDirDoesNotHaveEntryClass() throws IOException { + try { + PackagedProgramRetrieverImpl.create( + classpathProvider.getUserDirectoryWithNoEntryClass(), + classpathProvider.getTestJobClassName(), + classpathProvider.getTestJobArgs("suffix")); + fail("This case should throw exception !"); + } catch (FlinkException e) { + assertThat( + ExceptionUtils.findThrowableWithMessage( Review comment: I think we could use `assertThat(e, FlinkMatchers.containsMessage(msg))` here. ########## File path: flink-clients/src/main/java/org/apache/flink/client/deployment/application/FromJarEntryClassInformationProvider.java ########## @@ -0,0 +1,76 @@ +/* + * 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.client.deployment.application; + +import org.apache.flink.client.program.PackagedProgramUtils; +import org.apache.flink.util.Preconditions; + +import java.io.File; +import java.util.Optional; + +/** + * {@code FromJarEntryClassInformationProvider} returns a is used for cases where the Jar archive is Review comment: I guess you mean `{@code FromJarEntryClassInformationProvider}` returns a `{@link EntryClassInformationProvider}`. ########## File path: flink-clients/src/test/java/org/apache/flink/client/program/PackagedProgramRetrieverImplTest.java ########## @@ -0,0 +1,557 @@ +/* + * 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.client.program; + +import org.apache.flink.api.common.JobID; +import org.apache.flink.api.dag.Pipeline; +import org.apache.flink.client.deployment.application.EntryClassInformationProvider; +import org.apache.flink.client.deployment.executors.PipelineExecutorUtils; +import org.apache.flink.client.testjar.ClasspathProvider; +import org.apache.flink.configuration.ConfigConstants; +import org.apache.flink.configuration.ConfigUtils; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.configuration.CoreOptions; +import org.apache.flink.configuration.PipelineOptions; +import org.apache.flink.configuration.PipelineOptionsInternal; +import org.apache.flink.runtime.execution.librarycache.FlinkUserCodeClassLoaders; +import org.apache.flink.runtime.jobgraph.JobGraph; +import org.apache.flink.runtime.jobgraph.SavepointRestoreSettings; +import org.apache.flink.util.ChildFirstClassLoader; +import org.apache.flink.util.ExceptionUtils; +import org.apache.flink.util.FileUtils; +import org.apache.flink.util.FlinkException; +import org.apache.flink.util.TestLogger; + +import org.hamcrest.collection.IsIterableContainingInAnyOrder; +import org.hamcrest.core.IsInstanceOf; +import org.junit.Assume; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNot.not; +import static org.junit.Assert.fail; + +/** {@code PackagedProgramRetrieverImplTest} tests {@link PackagedProgramRetrieverImpl}. */ +public class PackagedProgramRetrieverImplTest extends TestLogger { + + @Rule public ClasspathProvider classpathProvider = new ClasspathProvider(); + + @Test + public void deriveEntryClassInformationForPythonBasedOnJobName() throws FlinkException { + // TODO: see FLINK-23154 + Assume.assumeTrue( + "This tests only succeeds if " + ConfigConstants.ENV_FLINK_OPT_DIR + " is set.", + System.getProperty(ConfigConstants.ENV_FLINK_OPT_DIR) != null); + final EntryClassInformationProvider informationProvider = + PackagedProgramRetrieverImpl.createEntryClassInformationProvider( + null, + Collections.emptyList(), + null, + PackagedProgramUtils.getPythonDriverClassName(), + new String[0]); + assertThat( + informationProvider.getJobClassName(), + is(PackagedProgramUtils.getPythonDriverClassName())); + assertThat(informationProvider.getJarFile().isPresent(), is(true)); + assertThat( + informationProvider.getJarFile().get(), + is(PackagedProgramUtils.getPythonJar().getFile())); + } + + @Test + public void deriveEntryClassInformationForPythonBasedOnParameter() throws FlinkException { + // TODO: see FLINK-23154 + Assume.assumeTrue( + "This tests only succeeds if " + ConfigConstants.ENV_FLINK_OPT_DIR + " is set.", + System.getProperty(ConfigConstants.ENV_FLINK_OPT_DIR) != null); + + final EntryClassInformationProvider informationProvider = + PackagedProgramRetrieverImpl.createEntryClassInformationProvider( + null, Collections.emptyList(), null, null, new String[] {"--python"}); + + assertThat( + informationProvider.getJobClassName(), + is(PackagedProgramUtils.getPythonDriverClassName())); + assertThat(informationProvider.getJarFile().isPresent(), is(true)); + assertThat( + informationProvider.getJarFile().get(), + is(PackagedProgramUtils.getPythonJar().getFile())); + } + + @Test + public void deriveEntryClassInformationForCustomJar() + throws FlinkException, MalformedURLException { + // make loading from system classpath fail to make sure that it's not triggered + classpathProvider.setSystemClasspathWithTwoEntryClasses(); + + final String jobClassName = "SomeJobClassName"; + final File jarFile = new File("some/jar/file.jar"); + final EntryClassInformationProvider informationProvider = + PackagedProgramRetrieverImpl.createEntryClassInformationProvider( + null, null, jarFile, jobClassName, new String[0]); + assertThat(informationProvider.getJobClassName(), is(jobClassName)); + assertThat(informationProvider.getJarFile().isPresent(), is(true)); + assertThat(informationProvider.getJarFile().get(), is(jarFile)); + } + + @Test + // TODO: we might want to change this behavior triggering a failure here + public void testDeriveEntryClassInformationFromSystemClasspathWithNonExistingJobClassName() + throws IOException, FlinkException { + classpathProvider.setSystemClasspathWithEntryClass(); + + final String jobClassName = "SomeJobClassNotBeingOnTheSystemClasspath"; + final EntryClassInformationProvider informationProvider = + PackagedProgramRetrieverImpl.createEntryClassInformationProvider( + null, null, null, jobClassName, new String[0]); + assertThat(informationProvider.getJobClassName(), is(jobClassName)); + assertThat(informationProvider.getJarFile().isPresent(), is(false)); + } + + @Test + public void testDeriveEntryClassInformationFromSystemClasspathWithExistingJobClassName() + throws IOException, FlinkException { + classpathProvider.setSystemClasspathWithEntryClass(); + + final EntryClassInformationProvider informationProvider = + PackagedProgramRetrieverImpl.createEntryClassInformationProvider( + null, null, null, classpathProvider.getJobClassName(), new String[0]); + assertThat(informationProvider.getJobClassName(), is(classpathProvider.getJobClassName())); + assertThat(informationProvider.getJarFile().isPresent(), is(false)); + } + + @Test + public void testDeriveEntryClassInformationFromSystemClasspathExtractingTheJobClassFromThere() + throws IOException, FlinkException { + classpathProvider.setSystemClasspathWithEntryClass(); + + final EntryClassInformationProvider informationProvider = + PackagedProgramRetrieverImpl.createEntryClassInformationProvider( + null, null, null, null, new String[0]); + assertThat(informationProvider.getJobClassName(), is(classpathProvider.getJobClassName())); + assertThat(informationProvider.getJarFile().isPresent(), is(false)); + } + + @Test + public void testDeriveEntryClassInformationFromClasspathWithJobClass() + throws IOException, FlinkException { + final EntryClassInformationProvider informationProvider = + PackagedProgramRetrieverImpl.createEntryClassInformationProvider( + // the user directory must be specified + classpathProvider.getUserDirectoryWithTwoEntryClasses(), + // the user classpath is derived from the user directory outside of the + // method + classpathProvider.getURLUserClasspathWithTwoEntryClasses(), + null, + // we have to specify the job class - otherwise the call would fail due to + // two main method being present + classpathProvider.getJobClassName(), + new String[0]); + assertThat(informationProvider.getJobClassName(), is(classpathProvider.getJobClassName())); + assertThat(informationProvider.getJarFile().isPresent(), is(false)); + } + + @Test + public void testDeriveEntryClassInformationFromClasspathWithNoJobClass() + throws IOException, FlinkException { + final EntryClassInformationProvider informationProvider = + PackagedProgramRetrieverImpl.createEntryClassInformationProvider( + // the user directory must be specified + classpathProvider.getUserDirectoryWithOneEntryClass(), + // the user classpath is derived from the user directory outside of the + // method + classpathProvider.getURLUserClasspathWithEntryClass(), + null, + // no job class name is specified which enables looking for the entry class + // on the user classpath + null, + new String[0]); + assertThat(informationProvider.getJobClassName(), is(classpathProvider.getJobClassName())); + assertThat(informationProvider.getJarFile().isPresent(), is(false)); + } + + @Test + public void testCreateWithUserLibDir() throws FlinkException { + final PackagedProgramRetriever retriever = + PackagedProgramRetrieverImpl.create( + classpathProvider.getUserDirectoryWithOneEntryClass(), + null, + classpathProvider.getJobClassName(), + new String[0], + new Configuration()); + + // the right information is picked up without any error + assertThat( + retriever.getPackagedProgram().getMainClassName(), + is(classpathProvider.getJobClassName())); + } + + @Test + public void testJobGraphRetrieval() + throws IOException, FlinkException, ProgramInvocationException { + final int parallelism = 42; + final JobID jobId = new JobID(); + + final Configuration configuration = new Configuration(); + configuration.setInteger(CoreOptions.DEFAULT_PARALLELISM, parallelism); + configuration.set(PipelineOptionsInternal.PIPELINE_FIXED_JOB_ID, jobId.toHexString()); + + final String expectedSuffix = "suffix"; + final PackagedProgramRetriever retriever = + PackagedProgramRetrieverImpl.create( + null, + classpathProvider.getTestJobClassName(), + classpathProvider.getTestJobArgs(expectedSuffix), + new Configuration()); + + final JobGraph jobGraph = retrieveJobGraph(retriever, configuration); + + assertThat( + jobGraph.getName(), + is(classpathProvider.getTestJobClassName() + "-" + expectedSuffix)); + assertThat(jobGraph.getSavepointRestoreSettings(), is(SavepointRestoreSettings.none())); + assertThat(jobGraph.getMaximumParallelism(), is(parallelism)); + assertThat(jobGraph.getJobID(), is(jobId)); + } + + @Test + public void testJobGraphRetrievalFromJar() + throws IOException, FlinkException, ProgramInvocationException { + final String expectedSuffix = "suffix"; + final PackagedProgramRetriever retrieverUnderTest = + PackagedProgramRetrieverImpl.create( + classpathProvider.getUserDirectoryWithTestJob(), + null, + null, + classpathProvider.getTestJobArgs(expectedSuffix), + new Configuration()); + + final JobGraph jobGraph = retrieveJobGraph(retrieverUnderTest, new Configuration()); + + assertThat( + jobGraph.getName(), + is(classpathProvider.getTestJobClassName() + "-" + expectedSuffix)); + } + + @Test + public void testJobGraphRetrievalJobClassNameHasPrecedenceOverClasspath() Review comment: Yes. The legacy `ClassPathPackagedProgramRetriever.Builder#setJarsOnClassPath` is never used in production code. And it seems not have some benefits to specify the jar name on classpath instead of entry class. +1 to remove this test. ########## File path: flink-clients/src/test/java/org/apache/flink/client/program/PackagedProgramRetrieverImplTest.java ########## @@ -328,18 +329,15 @@ public void testFailIfJobDirDoesNotHaveEntryClass() { } } - @Test - public void testEntryClassNotFoundOnSystemClasspath() throws FlinkException, IOException { - classpathProvider.setSystemClasspathWithoutEntryClass(); - final PackagedProgramRetrieverImpl retriever = - PackagedProgramRetrieverImpl.create( - null, - classpathProvider.getTestJobClassName(), - new String[0], - new Configuration()); - assertThat( - retriever.getPackagedProgram().getMainClassName(), - is(classpathProvider.getTestJobClassName())); + // TODO: this test checks the same code path as + // testDeriveEntryClassInformationFromSystemClasspathWithNonExistingJobClassName + // We should make it fail early if the class is not present on the system classpath + // Right now, the test is failing because no error is thrown + @Ignore + @Test(expected = FlinkException.class) + public void testEntryClassNotFoundOnSystemClasspath() throws FlinkException { Review comment: IIUC, this test could be subsumed by `testDeriveEntryClassInformationFromSystemClasspathWithNonExistingJobClassName`. ########## File path: flink-clients/src/test/java/org/apache/flink/client/program/PackagedProgramRetrieverImplTest.java ########## @@ -215,20 +215,6 @@ public void testCreateWithUserLibDir() throws FlinkException { is(classpathProvider.getJobClassName())); } - @Test - public void testCreateWithoutUserLibDir() throws IOException, FlinkException { Review comment: I could not fully understand why we need to remove the `testCreateWithoutUserLibDir` here. IIUC, it is used to verify the system classpath pickup. ########## File path: flink-clients/src/test/java/org/apache/flink/client/program/PackagedProgramRetrieverImplTest.java ########## @@ -0,0 +1,590 @@ +/* + * 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.client.program; + +import org.apache.flink.api.common.JobID; +import org.apache.flink.api.dag.Pipeline; +import org.apache.flink.client.deployment.application.EntryClassInformationProvider; +import org.apache.flink.client.deployment.executors.PipelineExecutorUtils; +import org.apache.flink.client.testjar.ClasspathProvider; +import org.apache.flink.configuration.ConfigConstants; +import org.apache.flink.configuration.ConfigUtils; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.configuration.CoreOptions; +import org.apache.flink.configuration.PipelineOptions; +import org.apache.flink.configuration.PipelineOptionsInternal; +import org.apache.flink.runtime.execution.librarycache.FlinkUserCodeClassLoaders; +import org.apache.flink.runtime.jobgraph.JobGraph; +import org.apache.flink.runtime.jobgraph.SavepointRestoreSettings; +import org.apache.flink.util.ChildFirstClassLoader; +import org.apache.flink.util.ExceptionUtils; +import org.apache.flink.util.FileUtils; +import org.apache.flink.util.FlinkException; +import org.apache.flink.util.TestLogger; + +import org.hamcrest.collection.IsIterableContainingInAnyOrder; +import org.hamcrest.core.IsInstanceOf; +import org.junit.Assume; +import org.junit.Rule; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNot.not; +import static org.junit.Assert.fail; + +/** {@code PackagedProgramRetrieverImplTest} tests {@link PackagedProgramRetrieverImpl}. */ +public class PackagedProgramRetrieverImplTest extends TestLogger { + + @Rule public ClasspathProvider classpathProvider = new ClasspathProvider(); + + @Test + public void deriveEntryClassInformationForPythonBasedOnJobName() throws FlinkException { + // TODO: see FLINK-23154 + Assume.assumeTrue( + "This tests only succeeds if " + ConfigConstants.ENV_FLINK_OPT_DIR + " is set.", + System.getProperty(ConfigConstants.ENV_FLINK_OPT_DIR) != null); + final EntryClassInformationProvider informationProvider = + PackagedProgramRetrieverImpl.createEntryClassInformationProvider( + null, + Collections.emptyList(), + null, + PackagedProgramUtils.getPythonDriverClassName(), + new String[0]); + assertThat( + informationProvider.getJobClassName(), + is(PackagedProgramUtils.getPythonDriverClassName())); + assertThat(informationProvider.getJarFile().isPresent(), is(true)); + assertThat( + informationProvider.getJarFile().get(), + is(PackagedProgramUtils.getPythonJar().getFile())); + } + + @Test + public void deriveEntryClassInformationForPythonBasedOnParameter() throws FlinkException { + // TODO: see FLINK-23154 + Assume.assumeTrue( + "This tests only succeeds if " + ConfigConstants.ENV_FLINK_OPT_DIR + " is set.", + System.getProperty(ConfigConstants.ENV_FLINK_OPT_DIR) != null); + + final EntryClassInformationProvider informationProvider = + PackagedProgramRetrieverImpl.createEntryClassInformationProvider( + null, Collections.emptyList(), null, null, new String[] {"--python"}); + + assertThat( + informationProvider.getJobClassName(), + is(PackagedProgramUtils.getPythonDriverClassName())); + assertThat(informationProvider.getJarFile().isPresent(), is(true)); + assertThat( + informationProvider.getJarFile().get(), + is(PackagedProgramUtils.getPythonJar().getFile())); + } + + @Test + public void deriveEntryClassInformationForCustomJar() + throws FlinkException, MalformedURLException { + // make loading from system classpath fail to make sure that it's not triggered + classpathProvider.setSystemClasspathWithTwoEntryClasses(); + + final String jobClassName = "SomeJobClassName"; + final File jarFile = new File("some/jar/file.jar"); + final EntryClassInformationProvider informationProvider = + PackagedProgramRetrieverImpl.createEntryClassInformationProvider( + null, null, jarFile, jobClassName, new String[0]); + assertThat(informationProvider.getJobClassName(), is(jobClassName)); + assertThat(informationProvider.getJarFile().isPresent(), is(true)); + assertThat(informationProvider.getJarFile().get(), is(jarFile)); + } + + @Test + // TODO: we might want to change this behavior triggering a failure here Review comment: I agree with that we also need to check the system classpath contains specified JobClass. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
