XComp commented on a change in pull request #16286: URL: https://github.com/apache/flink/pull/16286#discussion_r661979428
########## File path: flink-clients/src/test/java/org/apache/flink/client/program/PackagedProgramRetrieverImplTest.java ########## @@ -0,0 +1,573 @@ +/* + * 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 testDeriveEntryClassInformationForPythonBasedOnJobName() 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().isPresent(), is(true)); + assertThat( + informationProvider.getJobClassName().get(), + is(PackagedProgramUtils.getPythonDriverClassName())); + assertThat(informationProvider.getJarFile().isPresent(), is(true)); + assertThat( + informationProvider.getJarFile().get(), + is(PackagedProgramUtils.getPythonJar().getFile())); + } + + @Test + public void testDeriveEntryClassInformationForPythonBasedOnParameter() 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().isPresent(), is(true)); + assertThat( + informationProvider.getJobClassName().get(), + is(PackagedProgramUtils.getPythonDriverClassName())); + assertThat(informationProvider.getJarFile().isPresent(), is(true)); + assertThat( + informationProvider.getJarFile().get(), + is(PackagedProgramUtils.getPythonJar().getFile())); + } + + @Test + public void testDeriveEntryClassInformationForCustomJar() + 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().isPresent(), is(true)); + assertThat(informationProvider.getJobClassName().get(), 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().isPresent(), is(true)); + assertThat(informationProvider.getJobClassName().get(), 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().isPresent(), is(true)); + assertThat( + informationProvider.getJobClassName().get(), + 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().isPresent(), is(true)); + assertThat( + informationProvider.getJobClassName().get(), + 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(), Review comment: There's a slight difference between providing a user classpath through the user directory and not providing one. In the former case, the code checks whether the job class appears in the user classpath. For the latter one, this check is not done. That's the difference between `testDeriveEntryClassInformationFromClasspathWithJobClass` and `testDeriveEntryClassInformationFromSystemClasspathWithNonExistingJobClassName`. -- 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]
