|
30 | 30 | import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksAction;
|
31 | 31 | import org.elasticsearch.client.Request;
|
32 | 32 | import org.elasticsearch.client.RequestOptions;
|
| 33 | +import org.elasticsearch.client.RequestOptions.Builder; |
33 | 34 | import org.elasticsearch.client.Response;
|
34 | 35 | import org.elasticsearch.client.ResponseException;
|
35 | 36 | import org.elasticsearch.client.RestClient;
|
36 | 37 | import org.elasticsearch.client.RestClientBuilder;
|
| 38 | +import org.elasticsearch.client.WarningsHandler; |
37 | 39 | import org.elasticsearch.common.CheckedRunnable;
|
38 | 40 | import org.elasticsearch.common.Strings;
|
39 | 41 | import org.elasticsearch.common.io.PathUtils;
|
|
69 | 71 | import java.security.NoSuchAlgorithmException;
|
70 | 72 | import java.security.cert.CertificateException;
|
71 | 73 | import java.util.ArrayList;
|
| 74 | +import java.util.Arrays; |
72 | 75 | import java.util.HashSet;
|
73 | 76 | import java.util.List;
|
74 | 77 | import java.util.Map;
|
75 | 78 | import java.util.Set;
|
76 | 79 | import java.util.TreeSet;
|
77 | 80 | import java.util.concurrent.TimeUnit;
|
| 81 | +import java.util.function.Consumer; |
78 | 82 | import java.util.function.Predicate;
|
79 | 83 |
|
80 | 84 | import static java.util.Collections.sort;
|
@@ -177,6 +181,69 @@ public void initClient() throws IOException {
|
177 | 181 | assert hasXPack != null;
|
178 | 182 | assert nodeVersions != null;
|
179 | 183 | }
|
| 184 | + |
| 185 | + // Helper class to check warnings in REST responses with sensitivity to versions |
| 186 | + // used in the target cluster. |
| 187 | + public static class VersionSensitiveWarningsHandler implements WarningsHandler { |
| 188 | + Set<String> requiredSameVersionClusterWarnings = new HashSet<>(); |
| 189 | + Set<String> allowedWarnings = new HashSet<>(); |
| 190 | + final Set<Version> testNodeVersions; |
| 191 | + |
| 192 | + public VersionSensitiveWarningsHandler(Set<Version> nodeVersions) { |
| 193 | + this.testNodeVersions = nodeVersions; |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Adds to the set of warnings that are all required in responses if the cluster |
| 198 | + * is formed from nodes all running the exact same version as the client. |
| 199 | + * @param requiredWarnings a set of required warnings |
| 200 | + */ |
| 201 | + public void current(String... requiredWarnings) { |
| 202 | + requiredSameVersionClusterWarnings.addAll(Arrays.asList(requiredWarnings)); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Adds to the set of warnings that are permissible (but not required) when running |
| 207 | + * in mixed-version clusters or those that differ in version from the test client. |
| 208 | + * @param allowedWarnings optional warnings that will be ignored if received |
| 209 | + */ |
| 210 | + public void compatible(String... allowedWarnings) { |
| 211 | + this.allowedWarnings.addAll(Arrays.asList(allowedWarnings)); |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public boolean warningsShouldFailRequest(List<String> warnings) { |
| 216 | + if (isExclusivelyTargetingCurrentVersionCluster()) { |
| 217 | + // absolute equality required in expected and actual. |
| 218 | + Set<String> actual = new HashSet<>(warnings); |
| 219 | + return false == requiredSameVersionClusterWarnings.equals(actual); |
| 220 | + } else { |
| 221 | + // Some known warnings can safely be ignored |
| 222 | + for (String actualWarning : warnings) { |
| 223 | + if (false == allowedWarnings.contains(actualWarning) && |
| 224 | + false == requiredSameVersionClusterWarnings.contains(actualWarning)) { |
| 225 | + return true; |
| 226 | + } |
| 227 | + } |
| 228 | + return false; |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + private boolean isExclusivelyTargetingCurrentVersionCluster() { |
| 233 | + assertFalse("Node versions running in the cluster are missing", testNodeVersions.isEmpty()); |
| 234 | + return testNodeVersions.size() == 1 && |
| 235 | + testNodeVersions.iterator().next().equals(Version.CURRENT); |
| 236 | + } |
| 237 | + |
| 238 | + } |
| 239 | + |
| 240 | + public static RequestOptions expectVersionSpecificWarnings(Consumer<VersionSensitiveWarningsHandler> expectationsSetter) { |
| 241 | + Builder builder = RequestOptions.DEFAULT.toBuilder(); |
| 242 | + VersionSensitiveWarningsHandler warningsHandler = new VersionSensitiveWarningsHandler(nodeVersions); |
| 243 | + expectationsSetter.accept(warningsHandler); |
| 244 | + builder.setWarningsHandler(warningsHandler); |
| 245 | + return builder.build(); |
| 246 | + } |
180 | 247 |
|
181 | 248 | /**
|
182 | 249 | * Construct an HttpHost from the given host and port
|
|
0 commit comments