-
Notifications
You must be signed in to change notification settings - Fork 139
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
Add an API compatibility check to Nessie clients #6818
Merged
Merged
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
fea6656
Add an API compatibility check to Nessie clients
adutra 52b474c
Minor nit
adutra ee1c26e
spotlessApply
adutra 71c7c7f
Fix compatibility tests
adutra 1bc9959
Minor nit
adutra e0dab4b
Revert NessieOptions
adutra 24058ae
Fix TestNessieApiHolder
adutra f6cbeaa
Doing things differently
adutra 9d12b31
wip
adutra 28bbaf1
New field in NessieConfiguration
adutra a7fbcc6
Revert unnecessary change
adutra 6e18959
Move if up
adutra 435d5b2
Revert unnecessary change
adutra 5ae7cef
Revert move if up
adutra d60d2a0
Improving compat tests
adutra df8d35e
Fix failing tests
adutra 692b748
Soft assertions
adutra 52b90ec
Review comments
adutra 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 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 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 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
48 changes: 48 additions & 0 deletions
48
api/client/src/main/java/org/projectnessie/client/http/NessieApiCompatibility.java
This file contains 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,48 @@ | ||
/* | ||
* Copyright (C) 2023 Dremio | ||
* | ||
* Licensed 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.projectnessie.client.http; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
public class NessieApiCompatibility { | ||
|
||
private static final String MIN_API_VERSION = "minSupportedApiVersion"; | ||
private static final String MAX_API_VERSION = "maxSupportedApiVersion"; | ||
private static final String ACTUAL_API_VERSION = "actualApiVersion"; | ||
|
||
/** | ||
* Checks if the API version of the client is compatible with the server's. | ||
* | ||
* @param clientApiVersion the API version of the client | ||
* @param httpClient the underlying HTTP client. | ||
* @throws NessieApiCompatibilityException if the API version is not compatible. | ||
*/ | ||
public static void check(int clientApiVersion, HttpClient httpClient) | ||
throws NessieApiCompatibilityException { | ||
JsonNode config = httpClient.newRequest().path("config").get().readEntity(JsonNode.class); | ||
int minServerApiVersion = | ||
config.hasNonNull(MIN_API_VERSION) ? config.get(MIN_API_VERSION).asInt() : 1; | ||
int maxServerApiVersion = config.get(MAX_API_VERSION).asInt(); | ||
int actualServerApiVersion = | ||
config.hasNonNull(ACTUAL_API_VERSION) ? config.get(ACTUAL_API_VERSION).asInt() : 0; | ||
if (clientApiVersion < minServerApiVersion | ||
|| clientApiVersion > maxServerApiVersion | ||
|| (actualServerApiVersion > 0 && clientApiVersion != actualServerApiVersion)) { | ||
throw new NessieApiCompatibilityException( | ||
clientApiVersion, minServerApiVersion, maxServerApiVersion, actualServerApiVersion); | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
api/client/src/main/java/org/projectnessie/client/http/NessieApiCompatibilityException.java
This file contains 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,81 @@ | ||
/* | ||
* Copyright (C) 2023 Dremio | ||
* | ||
* Licensed 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.projectnessie.client.http; | ||
|
||
public class NessieApiCompatibilityException extends RuntimeException { | ||
|
||
private final int clientApiVersion; | ||
private final int minServerApiVersion; | ||
private final int maxServerApiVersion; | ||
private final int actualServerApiVersion; | ||
|
||
public NessieApiCompatibilityException( | ||
int clientApiVersion, | ||
int minServerApiVersion, | ||
int maxServerApiVersion, | ||
int actualServerApiVersion) { | ||
super( | ||
formatMessage( | ||
clientApiVersion, minServerApiVersion, maxServerApiVersion, actualServerApiVersion)); | ||
this.clientApiVersion = clientApiVersion; | ||
this.minServerApiVersion = minServerApiVersion; | ||
this.maxServerApiVersion = maxServerApiVersion; | ||
this.actualServerApiVersion = actualServerApiVersion; | ||
} | ||
|
||
private static String formatMessage( | ||
int clientApiVersion, | ||
int minServerApiVersion, | ||
int maxServerApiVersion, | ||
int actualServerApiVersion) { | ||
if (clientApiVersion < minServerApiVersion) { | ||
return String.format( | ||
"API version %d is too old for server (minimum supported version is %d)", | ||
clientApiVersion, minServerApiVersion); | ||
} | ||
if (clientApiVersion > maxServerApiVersion) { | ||
return String.format( | ||
"API version %d is too new for server (maximum supported version is %d)", | ||
clientApiVersion, maxServerApiVersion); | ||
} | ||
return String.format( | ||
"API version mismatch, check URI prefix (expected: %d, actual: %d)", | ||
clientApiVersion, actualServerApiVersion); | ||
} | ||
|
||
/** The client's API version. */ | ||
public int getClientApiVersion() { | ||
return clientApiVersion; | ||
} | ||
|
||
/** The minimum API version supported by the server. */ | ||
public int getMinServerApiVersion() { | ||
return minServerApiVersion; | ||
} | ||
|
||
/** The maximum API version supported by the server. */ | ||
public int getMaxServerApiVersion() { | ||
return maxServerApiVersion; | ||
} | ||
|
||
/** | ||
* The actual API version used by the server, or zero if the server does not report its actual API | ||
* version. | ||
*/ | ||
public int getActualServerApiVersion() { | ||
return actualServerApiVersion; | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
api/client/src/test/java/org/projectnessie/client/http/TestNessieApiCompatibility.java
This file contains 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,113 @@ | ||
/* | ||
* Copyright (C) 2023 Dremio | ||
* | ||
* Licensed 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.projectnessie.client.http; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.get; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; | ||
import static java.net.HttpURLConnection.HTTP_OK; | ||
import static org.assertj.core.api.Assertions.assertThatCode; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.assertj.core.api.InstanceOfAssertFactories.type; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.JsonNodeFactory; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; | ||
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; | ||
import com.github.tomakehurst.wiremock.junit5.WireMockTest; | ||
import java.net.URI; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.projectnessie.client.rest.NessieHttpResponseFilter; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
@WireMockTest | ||
class TestNessieApiCompatibility { | ||
|
||
enum Expectation { | ||
OK, | ||
TOO_OLD, | ||
TOO_NEW, | ||
MISMATCH | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource( | ||
value = { | ||
"1, 1, 1, 0, OK", | ||
"1, 1, 2, 1, OK", | ||
"1, 1, 2, 2, MISMATCH", // v2 endpoint mistakenly called with v1 client | ||
"1, 2, 2, 2, TOO_OLD", | ||
"2, 1, 1, 1, TOO_NEW", | ||
"2, 1, 2, 1, MISMATCH", // v1 endpoint mistakenly called with v2 client | ||
"2, 1, 2, 2, OK", | ||
"2, 2, 2, 2, OK", | ||
}) | ||
void checkApiCompatibility( | ||
int client, | ||
int serverMin, | ||
int serverMax, | ||
int serverActual, | ||
Expectation expectation, | ||
WireMockRuntimeInfo wireMock) { | ||
|
||
ObjectNode config = JsonNodeFactory.instance.objectNode(); | ||
config.set("minSupportedApiVersion", JsonNodeFactory.instance.numberNode(serverMin)); | ||
config.set("maxSupportedApiVersion", JsonNodeFactory.instance.numberNode(serverMax)); | ||
if (serverActual > 0) { | ||
config.set("actualApiVersion", JsonNodeFactory.instance.numberNode(serverActual)); | ||
} | ||
|
||
stubFor( | ||
get("/config") | ||
.willReturn( | ||
ResponseDefinitionBuilder.responseDefinition() | ||
.withStatus(HTTP_OK) | ||
.withBody(config.toString()) | ||
.withHeader("Content-Type", "application/json"))); | ||
|
||
try (HttpClient httpClient = | ||
HttpClient.builder() | ||
.setBaseUri(URI.create(wireMock.getHttpBaseUrl())) | ||
.setObjectMapper(new ObjectMapper()) | ||
.addResponseFilter(new NessieHttpResponseFilter()) | ||
.build()) { | ||
|
||
if (expectation == Expectation.OK) { | ||
|
||
assertThatCode(() -> NessieApiCompatibility.check(client, httpClient)) | ||
.doesNotThrowAnyException(); | ||
|
||
} else { | ||
|
||
assertThatThrownBy(() -> NessieApiCompatibility.check(client, httpClient)) | ||
.hasMessageContaining( | ||
expectation == Expectation.MISMATCH | ||
? "mismatch" | ||
: expectation == Expectation.TOO_OLD ? "too old" : "too new") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: put |
||
.asInstanceOf(type(NessieApiCompatibilityException.class)) | ||
.extracting( | ||
NessieApiCompatibilityException::getClientApiVersion, | ||
NessieApiCompatibilityException::getMinServerApiVersion, | ||
NessieApiCompatibilityException::getMaxServerApiVersion, | ||
NessieApiCompatibilityException::getActualServerApiVersion) | ||
.containsExactly(client, serverMin, serverMax, serverActual); | ||
} | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...ient/src/test/java/org/projectnessie/client/http/TestNessieApiCompatibilityException.java
This file contains 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,45 @@ | ||
/* | ||
* Copyright (C) 2023 Dremio | ||
* | ||
* Licensed 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.projectnessie.client.http; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class TestNessieApiCompatibilityException { | ||
|
||
@Test | ||
void testMessages() { | ||
NessieApiCompatibilityException e = new NessieApiCompatibilityException(1, 2, 3, 3); | ||
assertThat(e.getMessage()) | ||
.isEqualTo("API version 1 is too old for server (minimum supported version is 2)"); | ||
e = new NessieApiCompatibilityException(5, 3, 4, 4); | ||
assertThat(e.getMessage()) | ||
.isEqualTo("API version 5 is too new for server (maximum supported version is 4)"); | ||
e = new NessieApiCompatibilityException(3, 2, 4, 2); | ||
assertThat(e.getMessage()) | ||
.isEqualTo("API version mismatch, check URI prefix (expected: 3, actual: 2)"); | ||
} | ||
|
||
@Test | ||
void testGetters() { | ||
NessieApiCompatibilityException e = new NessieApiCompatibilityException(1, 2, 4, 3); | ||
assertThat(e.getClientApiVersion()).isEqualTo(1); | ||
assertThat(e.getMinServerApiVersion()).isEqualTo(2); | ||
assertThat(e.getMaxServerApiVersion()).isEqualTo(4); | ||
assertThat(e.getActualServerApiVersion()).isEqualTo(3); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this
if
up (minor nit)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually this would break things:
HttpApiV2
is a subclass ofNessieApiV1
.