-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[improve][proxy] Add regression tests for package upload with 'Expect: 100-continue' #25211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
41e23e1
[fix][proxy] Handle "Expect: 100-continue" to prevent Early EOF
nodece 0caa561
[improve][proxy] Simplify HttpClient creation
nodece db42b6e
[improve][test] Refactor package upload tests for clarity and resourc…
nodece f9a009a
[improve][proxy] Refactor HttpClient creation to enhance readability …
nodece 80bbf0c
Update pulsar-package-management/core/src/test/java/org/apache/pulsar…
nodece 9e0679d
Update pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/Admi…
nodece abe3874
Update pulsar-package-management/core/src/test/java/org/apache/pulsar…
nodece d14d5eb
Remove unused imports and delete file statements
nodece 67d67d2
Fix code style
nodece File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...e/src/test/java/org/apache/pulsar/packages/management/core/MockedPackagesStorageTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * 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.pulsar.packages.management.core; | ||
|
|
||
| import static org.mockito.Mockito.mock; | ||
| import static org.testng.Assert.assertEquals; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import lombok.Cleanup; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| public class MockedPackagesStorageTest { | ||
|
|
||
| @Test | ||
| public void testWriteAndRead() throws Exception { | ||
| PackagesStorageProvider provider = new MockedPackagesStorageProvider(); | ||
| PackagesStorage storage = provider.getStorage(mock(PackagesStorageConfiguration.class)); | ||
| storage.initialize(); | ||
|
|
||
| // Test data | ||
| byte[] testBytes = new byte[1 * 1024 * 1024]; | ||
|
nodece marked this conversation as resolved.
|
||
|
|
||
| // Write | ||
| storage.writeAsync("test/path", new ByteArrayInputStream(testBytes)).get(); | ||
|
|
||
| // Read | ||
| @Cleanup | ||
| ByteArrayOutputStream readBaos = new ByteArrayOutputStream(); | ||
| storage.readAsync("test/path", readBaos).get(); | ||
|
|
||
| // Verify | ||
| assertEquals(readBaos.toByteArray(), testBytes); | ||
|
|
||
| storage.closeAsync().get(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyPackagesUploadTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| /* | ||
| * 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.pulsar.proxy.server; | ||
|
|
||
| import static com.google.common.net.HttpHeaders.EXPECT; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.Optional; | ||
| import lombok.Cleanup; | ||
| import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest; | ||
| import org.apache.pulsar.broker.authentication.AuthenticationService; | ||
| import org.apache.pulsar.client.admin.PulsarAdmin; | ||
| import org.apache.pulsar.common.configuration.PulsarConfigurationLoader; | ||
| import org.apache.pulsar.common.util.ObjectMapperFactory; | ||
| import org.apache.pulsar.packages.management.core.MockedPackagesStorageProvider; | ||
| import org.apache.pulsar.packages.management.core.common.PackageMetadata; | ||
| import org.asynchttpclient.AsyncHttpClient; | ||
| import org.asynchttpclient.DefaultAsyncHttpClient; | ||
| import org.asynchttpclient.DefaultAsyncHttpClientConfig; | ||
| import org.asynchttpclient.RequestBuilder; | ||
| import org.asynchttpclient.Response; | ||
| import org.asynchttpclient.request.body.multipart.FilePart; | ||
| import org.asynchttpclient.request.body.multipart.StringPart; | ||
| import org.eclipse.jetty.ee8.servlet.ServletHolder; | ||
| import org.testng.annotations.AfterMethod; | ||
| import org.testng.annotations.BeforeMethod; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| @Test(groups = "broker-admin") | ||
| public class ProxyPackagesUploadTest extends MockedPulsarServiceBaseTest { | ||
|
|
||
| private static final int FILE_SIZE = 8 * 1024 * 1024; // 8 MB | ||
| private static final ObjectMapper MAPPER = ObjectMapperFactory.create(); | ||
| private WebServer webServer; | ||
| private PulsarAdmin proxyAdmin; | ||
|
|
||
| @BeforeMethod(alwaysRun = true) | ||
| @Override | ||
| protected void setup() throws Exception { | ||
| conf.setEnablePackagesManagement(true); | ||
| conf.setPackagesManagementStorageProvider(MockedPackagesStorageProvider.class.getName()); | ||
| super.internalSetup(); | ||
|
|
||
| ProxyConfiguration proxyConfig = new ProxyConfiguration(); | ||
| proxyConfig.setServicePort(Optional.of(0)); | ||
| proxyConfig.setWebServicePort(Optional.of(0)); | ||
| proxyConfig.setBrokerWebServiceURL(brokerUrl.toString()); | ||
|
|
||
| webServer = new WebServer(proxyConfig, new AuthenticationService( | ||
| PulsarConfigurationLoader.convertFrom(proxyConfig, true))); | ||
| webServer.addServlet("/", new ServletHolder(new AdminProxyHandler(proxyConfig, null, null))); | ||
| webServer.start(); | ||
|
|
||
| proxyAdmin = PulsarAdmin.builder() | ||
| .serviceHttpUrl("http://localhost:" + webServer.getListenPortHTTP().get()) | ||
| .build(); | ||
|
|
||
| admin.tenants().createTenant("public", createDefaultTenantInfo()); | ||
| admin.namespaces().createNamespace("public/default"); | ||
| } | ||
|
|
||
| @AfterMethod(alwaysRun = true) | ||
| @Override | ||
| protected void cleanup() throws Exception { | ||
| if (proxyAdmin != null) { | ||
| proxyAdmin.close(); | ||
| } | ||
| if (webServer != null) { | ||
| webServer.stop(); | ||
| } | ||
| super.internalCleanup(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUploadPackageThroughProxy() throws Exception { | ||
| Path packageFile = Files.createTempFile("pkg-sdk", ".nar"); | ||
| packageFile.toFile().deleteOnExit(); | ||
|
nodece marked this conversation as resolved.
|
||
| Files.write(packageFile, new byte[FILE_SIZE]); | ||
|
|
||
| String pkgName = "function://public/default/pkg-sdk@v1"; | ||
| PackageMetadata meta = PackageMetadata.builder().description("sdk-test").build(); | ||
|
|
||
| proxyAdmin.packages().upload(meta, pkgName, packageFile.toString()); | ||
|
|
||
| verifyDownload(pkgName, FILE_SIZE); | ||
|
nodece marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @Test | ||
| public void testUploadWithExpect100Continue() throws Exception { | ||
| Path packageFile = Files.createTempFile("pkg-ahc", ".nar"); | ||
| packageFile.toFile().deleteOnExit(); | ||
| Files.write(packageFile, new byte[FILE_SIZE]); | ||
|
|
||
| String pkgName = "function://public/default/expect-test@v1"; | ||
| String uploadUrl = String.format("http://localhost:%d/admin/v3/packages/function/public/default/expect-test/v1", | ||
| webServer.getListenPortHTTP().orElseThrow()); | ||
|
|
||
| @Cleanup | ||
| AsyncHttpClient client = new DefaultAsyncHttpClient(new DefaultAsyncHttpClientConfig.Builder().build()); | ||
|
|
||
| Response response = client.executeRequest(new RequestBuilder("POST") | ||
| .setUrl(uploadUrl) | ||
| .addHeader(EXPECT, "100-continue") | ||
| .addBodyPart(new FilePart("file", packageFile.toFile())) | ||
| .addBodyPart(new StringPart("metadata", MAPPER.writeValueAsString( | ||
| PackageMetadata.builder().description("ahc-test").build()), "application/json")) | ||
| .build()).get(); | ||
|
|
||
| assertThat(response.getStatusCode()).isEqualTo(204); | ||
|
|
||
| verifyDownload(pkgName, FILE_SIZE); | ||
|
nodece marked this conversation as resolved.
nodece marked this conversation as resolved.
|
||
| } | ||
|
nodece marked this conversation as resolved.
|
||
|
|
||
| private void verifyDownload(String packageName, int expectedSize) throws Exception { | ||
| Path fromBroker = Files.createTempFile("from-broker", ".nar"); | ||
| fromBroker.toFile().deleteOnExit(); | ||
| admin.packages().download(packageName, fromBroker.toString()); | ||
| assertThat(Files.size(fromBroker)).isEqualTo(expectedSize); | ||
| Files.deleteIfExists(fromBroker); | ||
|
|
||
| Path fromProxy = Files.createTempFile("from-proxy", ".nar"); | ||
| fromProxy.toFile().deleteOnExit(); | ||
| proxyAdmin.packages().download(packageName, fromProxy.toString()); | ||
| assertThat(Files.size(fromProxy)).isEqualTo(expectedSize); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.