github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/utils/subprocess_server_test.py (about) 1 # 2 # Licensed to the Apache Software Foundation (ASF) under one or more 3 # contributor license agreements. See the NOTICE file distributed with 4 # this work for additional information regarding copyright ownership. 5 # The ASF licenses this file to You under the Apache License, Version 2.0 6 # (the "License"); you may not use this file except in compliance with 7 # the License. You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 # 17 18 """Unit tests for the processes module.""" 19 20 # pytype: skip-file 21 22 import glob 23 import os 24 import re 25 import shutil 26 import socketserver 27 import subprocess 28 import tempfile 29 import threading 30 import unittest 31 32 from apache_beam.utils import subprocess_server 33 34 35 class JavaJarServerTest(unittest.TestCase): 36 def test_gradle_jar_release(self): 37 self.assertEqual( 38 'https://repo.maven.apache.org/maven2/org/apache/beam/' 39 'beam-sdks-java-fake/VERSION/beam-sdks-java-fake-VERSION.jar', 40 subprocess_server.JavaJarServer.path_to_beam_jar( 41 ':sdks:java:fake:fatJar', version='VERSION')) 42 self.assertEqual( 43 'https://repo.maven.apache.org/maven2/org/apache/beam/' 44 'beam-sdks-java-fake/VERSION/beam-sdks-java-fake-A-VERSION.jar', 45 subprocess_server.JavaJarServer.path_to_beam_jar( 46 ':sdks:java:fake:fatJar', appendix='A', version='VERSION')) 47 self.assertEqual( 48 'https://repo.maven.apache.org/maven2/org/apache/beam/' 49 'beam-sdks-java-fake/VERSION/beam-sdks-java-fake-A-VERSION.jar', 50 subprocess_server.JavaJarServer.path_to_beam_jar( 51 ':gradle:target:doesnt:matter', 52 appendix='A', 53 version='VERSION', 54 artifact_id='beam-sdks-java-fake')) 55 56 def test_gradle_jar_dev(self): 57 with self.assertRaisesRegex( 58 Exception, 59 re.escape(os.path.join('sdks', 60 'java', 61 'fake', 62 'build', 63 'libs', 64 'beam-sdks-java-fake-VERSION-SNAPSHOT.jar')) + 65 ' not found.'): 66 subprocess_server.JavaJarServer.path_to_beam_jar( 67 ':sdks:java:fake:fatJar', version='VERSION.dev') 68 with self.assertRaisesRegex( 69 Exception, 70 re.escape(os.path.join('sdks', 71 'java', 72 'fake', 73 'build', 74 'libs', 75 'beam-sdks-java-fake-A-VERSION-SNAPSHOT.jar')) + 76 ' not found.'): 77 subprocess_server.JavaJarServer.path_to_beam_jar( 78 ':sdks:java:fake:fatJar', appendix='A', version='VERSION.dev') 79 with self.assertRaisesRegex( 80 Exception, 81 re.escape(os.path.join('sdks', 82 'java', 83 'fake', 84 'build', 85 'libs', 86 'fake-artifact-id-A-VERSION-SNAPSHOT.jar')) + 87 ' not found.'): 88 subprocess_server.JavaJarServer.path_to_beam_jar( 89 ':sdks:java:fake:fatJar', 90 appendix='A', 91 version='VERSION.dev', 92 artifact_id='fake-artifact-id') 93 94 def test_beam_services(self): 95 with subprocess_server.JavaJarServer.beam_services({':some:target': 'foo'}): 96 self.assertEqual( 97 'foo', 98 subprocess_server.JavaJarServer.path_to_beam_jar(':some:target')) 99 100 def test_local_jar(self): 101 class Handler(socketserver.BaseRequestHandler): 102 timeout = 1 103 104 def handle(self): 105 self.request.recv(1024) 106 self.request.sendall(b'HTTP/1.1 200 OK\n\ndata') 107 108 port, = subprocess_server.pick_port(None) 109 server = socketserver.TCPServer(('localhost', port), Handler) 110 t = threading.Thread(target=server.handle_request) 111 t.daemon = True 112 t.start() 113 114 with tempfile.TemporaryDirectory() as temp_dir: 115 subprocess_server.JavaJarServer.local_jar( 116 'http://localhost:%s/path/to/file.jar' % port, temp_dir) 117 with open(os.path.join(temp_dir, 'file.jar')) as fin: 118 self.assertEqual(fin.read(), 'data') 119 120 @unittest.skipUnless(shutil.which('javac'), 'missing java jdk') 121 def test_classpath_jar(self): 122 with tempfile.TemporaryDirectory() as temp_dir: 123 try: 124 # Avoid having to prefix everything in our test strings. 125 oldwd = os.getcwd() 126 os.chdir(temp_dir) 127 128 with open('Main.java', 'w') as fout: 129 fout.write( 130 """ 131 public class Main { 132 public static void main(String[] args) { Other.greet(); } 133 } 134 """) 135 136 with open('Other.java', 'w') as fout: 137 fout.write( 138 """ 139 public class Other { 140 public static void greet() { System.out.println("You got me!"); } 141 } 142 """) 143 144 os.mkdir('jars') 145 # Using split just for readability/copyability. 146 subprocess.check_call('javac Main.java Other.java'.split()) 147 subprocess.check_call('jar cfe jars/Main.jar Main Main.class'.split()) 148 subprocess.check_call('jar cf jars/Other.jar Other.class'.split()) 149 # Make sure the java and class files don't get picked up. 150 for path in glob.glob('*.*'): 151 os.unlink(path) 152 153 # These should fail. 154 self.assertNotEqual( 155 subprocess.call('java -jar jars/Main.jar'.split()), 0) 156 self.assertNotEqual( 157 subprocess.call('java -jar jars/Other.jar'.split()), 0) 158 159 os.mkdir('beam_temp') 160 composite_jar = subprocess_server.JavaJarServer.make_classpath_jar( 161 'jars/Main.jar', ['jars/Other.jar'], cache_dir='beam_temp') 162 # This, however, should work. 163 subprocess.check_call(f'java -jar {composite_jar}'.split()) 164 165 finally: 166 os.chdir(oldwd) 167 168 169 if __name__ == '__main__': 170 unittest.main()