|
| 1 | +package com.microsoft.azure.functions.worker; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import java.lang.reflect.Constructor; |
| 6 | +import java.lang.reflect.Method; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 12 | + |
| 13 | +public class ApplicationTest { |
| 14 | + @Test |
| 15 | + public void legacyImplementationsDefaultFunctionsUriToNull() { |
| 16 | + IApplication application = new LegacyApplication(); |
| 17 | + |
| 18 | + assertNull(application.getFunctionsUri()); |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + public void prefixedStartupArgumentsTakePrecedenceOverLegacyValues() throws Exception { |
| 23 | + Application application = createApplication( |
| 24 | + "--functions-uri", "https://functions.example:8443", |
| 25 | + "--host", "legacy.example", |
| 26 | + "--port", "7071", |
| 27 | + "--functions-worker-id", "prefixed-worker", |
| 28 | + "--workerId", "legacy-worker", |
| 29 | + "--functions-request-id", "prefixed-request", |
| 30 | + "--requestId", "legacy-request", |
| 31 | + "--functions-grpc-max-message-length", "2048", |
| 32 | + "--grpcMaxMessageLength", "1024"); |
| 33 | + |
| 34 | + assertTrue(isCommandLineValid(application)); |
| 35 | + assertEquals("https://functions.example:8443", application.getFunctionsUri()); |
| 36 | + assertEquals("functions.example", application.getHost()); |
| 37 | + assertEquals(8443, application.getPort()); |
| 38 | + assertEquals(Integer.valueOf(2048), application.getMaxMessageSize()); |
| 39 | + assertEquals("prefixed-worker", invokePrivateStringAccessor(application, "getWorkerId")); |
| 40 | + assertEquals("prefixed-request", invokePrivateStringAccessor(application, "getRequestId")); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void legacyHostAndPortRemainAvailableWhenFunctionsUriIsAbsent() throws Exception { |
| 45 | + Application application = createApplication( |
| 46 | + "--host", "localhost", |
| 47 | + "--port", "7001", |
| 48 | + "--workerId", "legacy-worker", |
| 49 | + "--requestId", "legacy-request", |
| 50 | + "--grpcMaxMessageLength", "4096"); |
| 51 | + |
| 52 | + assertTrue(isCommandLineValid(application)); |
| 53 | + assertNull(application.getFunctionsUri()); |
| 54 | + assertEquals("localhost", application.getHost()); |
| 55 | + assertEquals(7001, application.getPort()); |
| 56 | + assertEquals(Integer.valueOf(4096), application.getMaxMessageSize()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void httpsFunctionsUriDefaultsToPort443WhenPortIsMissing() throws Exception { |
| 61 | + Application application = createApplication( |
| 62 | + "--functions-uri", "https://functions.example/path", |
| 63 | + "--functions-worker-id", "prefixed-worker", |
| 64 | + "--functions-request-id", "prefixed-request", |
| 65 | + "--functions-grpc-max-message-length", "2048"); |
| 66 | + |
| 67 | + assertTrue(isCommandLineValid(application)); |
| 68 | + assertEquals("functions.example", application.getHost()); |
| 69 | + assertEquals(443, application.getPort()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void httpFunctionsUriDefaultsToPort80WhenPortIsMissing() throws Exception { |
| 74 | + Application application = createApplication( |
| 75 | + "--functions-uri", "http://functions.example/path", |
| 76 | + "--functions-worker-id", "prefixed-worker", |
| 77 | + "--functions-request-id", "prefixed-request", |
| 78 | + "--functions-grpc-max-message-length", "2048"); |
| 79 | + |
| 80 | + assertTrue(isCommandLineValid(application)); |
| 81 | + assertEquals("functions.example", application.getHost()); |
| 82 | + assertEquals(80, application.getPort()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void unsupportedFunctionsUriSchemeFailsCommandLineValidation() throws Exception { |
| 87 | + Application application = createApplication( |
| 88 | + "--functions-uri", "unix:///tmp/functions.sock", |
| 89 | + "--functions-worker-id", "prefixed-worker", |
| 90 | + "--functions-request-id", "prefixed-request", |
| 91 | + "--functions-grpc-max-message-length", "2048"); |
| 92 | + |
| 93 | + assertFalse(isCommandLineValid(application)); |
| 94 | + } |
| 95 | + |
| 96 | + private static Application createApplication(String... args) throws Exception { |
| 97 | + Constructor<Application> constructor = Application.class.getDeclaredConstructor(String[].class); |
| 98 | + constructor.setAccessible(true); |
| 99 | + return constructor.newInstance((Object) args); |
| 100 | + } |
| 101 | + |
| 102 | + private static boolean isCommandLineValid(Application application) throws Exception { |
| 103 | + Method method = Application.class.getDeclaredMethod("isCommandlineValid"); |
| 104 | + method.setAccessible(true); |
| 105 | + return (boolean) method.invoke(application); |
| 106 | + } |
| 107 | + |
| 108 | + private static String invokePrivateStringAccessor(Application application, String methodName) throws Exception { |
| 109 | + Method method = Application.class.getDeclaredMethod(methodName); |
| 110 | + method.setAccessible(true); |
| 111 | + return (String) method.invoke(application); |
| 112 | + } |
| 113 | + |
| 114 | + private static final class LegacyApplication implements IApplication { |
| 115 | + @Override |
| 116 | + public boolean logToConsole() { |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public String getHost() { |
| 122 | + return "localhost"; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public int getPort() { |
| 127 | + return 7071; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public Integer getMaxMessageSize() { |
| 132 | + return null; |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments