|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pulsar.proxy.server; |
| 20 | + |
| 21 | +import static com.google.common.net.HttpHeaders.EXPECT; |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 24 | +import java.nio.file.Files; |
| 25 | +import java.nio.file.Path; |
| 26 | +import java.util.Optional; |
| 27 | +import lombok.Cleanup; |
| 28 | +import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest; |
| 29 | +import org.apache.pulsar.broker.authentication.AuthenticationService; |
| 30 | +import org.apache.pulsar.client.admin.PulsarAdmin; |
| 31 | +import org.apache.pulsar.common.configuration.PulsarConfigurationLoader; |
| 32 | +import org.apache.pulsar.common.util.ObjectMapperFactory; |
| 33 | +import org.apache.pulsar.packages.management.core.MockedPackagesStorageProvider; |
| 34 | +import org.apache.pulsar.packages.management.core.common.PackageMetadata; |
| 35 | +import org.asynchttpclient.AsyncHttpClient; |
| 36 | +import org.asynchttpclient.DefaultAsyncHttpClient; |
| 37 | +import org.asynchttpclient.DefaultAsyncHttpClientConfig; |
| 38 | +import org.asynchttpclient.RequestBuilder; |
| 39 | +import org.asynchttpclient.Response; |
| 40 | +import org.asynchttpclient.request.body.multipart.FilePart; |
| 41 | +import org.asynchttpclient.request.body.multipart.StringPart; |
| 42 | +import org.eclipse.jetty.servlet.ServletHolder; |
| 43 | +import org.testng.annotations.AfterMethod; |
| 44 | +import org.testng.annotations.BeforeMethod; |
| 45 | +import org.testng.annotations.Test; |
| 46 | + |
| 47 | +@Test(groups = "broker-admin") |
| 48 | +public class ProxyPackagesUploadTest extends MockedPulsarServiceBaseTest { |
| 49 | + |
| 50 | + private static final int FILE_SIZE = 8 * 1024 * 1024; // 8 MB |
| 51 | + private static final ObjectMapper MAPPER = ObjectMapperFactory.create(); |
| 52 | + private WebServer webServer; |
| 53 | + private PulsarAdmin proxyAdmin; |
| 54 | + |
| 55 | + @BeforeMethod(alwaysRun = true) |
| 56 | + @Override |
| 57 | + protected void setup() throws Exception { |
| 58 | + conf.setEnablePackagesManagement(true); |
| 59 | + conf.setPackagesManagementStorageProvider(MockedPackagesStorageProvider.class.getName()); |
| 60 | + super.internalSetup(); |
| 61 | + |
| 62 | + ProxyConfiguration proxyConfig = new ProxyConfiguration(); |
| 63 | + proxyConfig.setServicePort(Optional.of(0)); |
| 64 | + proxyConfig.setWebServicePort(Optional.of(0)); |
| 65 | + proxyConfig.setBrokerWebServiceURL(brokerUrl.toString()); |
| 66 | + |
| 67 | + webServer = new WebServer(proxyConfig, new AuthenticationService( |
| 68 | + PulsarConfigurationLoader.convertFrom(proxyConfig, true))); |
| 69 | + webServer.addServlet("/", new ServletHolder(new AdminProxyHandler(proxyConfig, null, null))); |
| 70 | + webServer.start(); |
| 71 | + |
| 72 | + proxyAdmin = PulsarAdmin.builder() |
| 73 | + .serviceHttpUrl("http://localhost:" + webServer.getListenPortHTTP().get()) |
| 74 | + .build(); |
| 75 | + |
| 76 | + admin.tenants().createTenant("public", createDefaultTenantInfo()); |
| 77 | + admin.namespaces().createNamespace("public/default"); |
| 78 | + } |
| 79 | + |
| 80 | + @AfterMethod(alwaysRun = true) |
| 81 | + @Override |
| 82 | + protected void cleanup() throws Exception { |
| 83 | + if (proxyAdmin != null) { |
| 84 | + proxyAdmin.close(); |
| 85 | + } |
| 86 | + if (webServer != null) { |
| 87 | + webServer.stop(); |
| 88 | + } |
| 89 | + super.internalCleanup(); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testUploadPackageThroughProxy() throws Exception { |
| 94 | + Path packageFile = Files.createTempFile("pkg-sdk", ".nar"); |
| 95 | + packageFile.toFile().deleteOnExit(); |
| 96 | + Files.write(packageFile, new byte[FILE_SIZE]); |
| 97 | + |
| 98 | + String pkgName = "function://public/default/pkg-sdk@v1"; |
| 99 | + PackageMetadata meta = PackageMetadata.builder().description("sdk-test").build(); |
| 100 | + |
| 101 | + proxyAdmin.packages().upload(meta, pkgName, packageFile.toString()); |
| 102 | + |
| 103 | + verifyDownload(pkgName, FILE_SIZE); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testUploadWithExpect100Continue() throws Exception { |
| 108 | + Path packageFile = Files.createTempFile("pkg-ahc", ".nar"); |
| 109 | + packageFile.toFile().deleteOnExit(); |
| 110 | + Files.write(packageFile, new byte[FILE_SIZE]); |
| 111 | + |
| 112 | + String pkgName = "function://public/default/expect-test@v1"; |
| 113 | + String uploadUrl = String.format("http://localhost:%d/admin/v3/packages/function/public/default/expect-test/v1", |
| 114 | + webServer.getListenPortHTTP().orElseThrow()); |
| 115 | + |
| 116 | + @Cleanup |
| 117 | + AsyncHttpClient client = new DefaultAsyncHttpClient(new DefaultAsyncHttpClientConfig.Builder().build()); |
| 118 | + |
| 119 | + Response response = client.executeRequest(new RequestBuilder("POST") |
| 120 | + .setUrl(uploadUrl) |
| 121 | + .addHeader(EXPECT, "100-continue") |
| 122 | + .addBodyPart(new FilePart("file", packageFile.toFile())) |
| 123 | + .addBodyPart(new StringPart("metadata", MAPPER.writeValueAsString( |
| 124 | + PackageMetadata.builder().description("ahc-test").build()), "application/json")) |
| 125 | + .build()).get(); |
| 126 | + |
| 127 | + assertThat(response.getStatusCode()).isEqualTo(204); |
| 128 | + |
| 129 | + verifyDownload(pkgName, FILE_SIZE); |
| 130 | + } |
| 131 | + |
| 132 | + private void verifyDownload(String packageName, int expectedSize) throws Exception { |
| 133 | + Path fromBroker = Files.createTempFile("from-broker", ".nar"); |
| 134 | + fromBroker.toFile().deleteOnExit(); |
| 135 | + admin.packages().download(packageName, fromBroker.toString()); |
| 136 | + assertThat(Files.size(fromBroker)).isEqualTo(expectedSize); |
| 137 | + Files.deleteIfExists(fromBroker); |
| 138 | + |
| 139 | + Path fromProxy = Files.createTempFile("from-proxy", ".nar"); |
| 140 | + fromProxy.toFile().deleteOnExit(); |
| 141 | + proxyAdmin.packages().download(packageName, fromProxy.toString()); |
| 142 | + assertThat(Files.size(fromProxy)).isEqualTo(expectedSize); |
| 143 | + } |
| 144 | +} |
0 commit comments