Skip to content

Commit f34124d

Browse files
committed
Add test for exception propagation from plugins
1 parent 3cd6897 commit f34124d

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package io.trino.aws.proxy.server.plugin.exception;
15+
16+
import com.google.inject.Inject;
17+
import com.google.inject.Scopes;
18+
import io.trino.aws.proxy.server.testing.TestingTrinoAwsProxyServer.Builder;
19+
import io.trino.aws.proxy.server.testing.harness.BuilderFilter;
20+
import io.trino.aws.proxy.server.testing.harness.TrinoAwsProxyTest;
21+
import io.trino.aws.proxy.spi.credentials.Credentials;
22+
import io.trino.aws.proxy.spi.credentials.CredentialsProvider;
23+
import org.junit.jupiter.api.Test;
24+
import software.amazon.awssdk.services.s3.S3Client;
25+
import software.amazon.awssdk.services.s3.model.S3Exception;
26+
27+
import java.util.Optional;
28+
29+
import static io.trino.aws.proxy.spi.plugin.TrinoAwsProxyServerBinding.credentialsProviderModule;
30+
import static java.util.Objects.requireNonNull;
31+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
32+
import static org.assertj.core.api.InstanceOfAssertFactories.type;
33+
34+
@TrinoAwsProxyTest(filters = TestCredentialsProviderExceptionPropagation.Filter.class)
35+
public class TestCredentialsProviderExceptionPropagation
36+
{
37+
private final DelegatingCredentialsProvider delegatingCredentialsProvider;
38+
private final S3Client internalClient;
39+
40+
public static class DelegatingCredentialsProvider
41+
implements CredentialsProvider
42+
{
43+
private CredentialsProvider delegate;
44+
45+
public void setDelegate(CredentialsProvider delegate)
46+
{
47+
this.delegate = delegate;
48+
}
49+
50+
@Inject
51+
public DelegatingCredentialsProvider() {}
52+
53+
@Override
54+
public Optional<Credentials> credentials(String emulatedAccessKey, Optional<String> session)
55+
{
56+
return requireNonNull(delegate, "delegate is null").credentials(emulatedAccessKey, session);
57+
}
58+
}
59+
60+
public static class Filter
61+
implements BuilderFilter
62+
{
63+
@Override
64+
public Builder filter(Builder builder)
65+
{
66+
return builder.withoutTestingCredentialsRoleProviders()
67+
.addModule(credentialsProviderModule("testing", DelegatingCredentialsProvider.class, binder -> binder.bind(DelegatingCredentialsProvider.class).in(Scopes.SINGLETON)))
68+
.withProperty("credentials-provider.type", "testing");
69+
}
70+
}
71+
72+
@Inject
73+
public TestCredentialsProviderExceptionPropagation(DelegatingCredentialsProvider delegatingCredentialsProvider, S3Client internalClient)
74+
{
75+
this.delegatingCredentialsProvider = requireNonNull(delegatingCredentialsProvider, "delegatingCredentialsProvider is null");
76+
this.internalClient = requireNonNull(internalClient, "internalClient is null");
77+
}
78+
79+
@Test
80+
public void testExceptions()
81+
{
82+
delegatingCredentialsProvider.setDelegate((_, _) -> { throw new RuntimeException("Testing exception"); });
83+
assertThatThrownBy(internalClient::listBuckets).asInstanceOf(type(S3Exception.class)).extracting(S3Exception::statusCode).isEqualTo(500);
84+
}
85+
}

0 commit comments

Comments
 (0)