|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.action; |
| 10 | + |
| 11 | +import org.junit.After; |
| 12 | +import org.opensearch.OpenSearchParseException; |
| 13 | +import org.opensearch.action.support.master.MasterNodeRequest; |
| 14 | +import org.opensearch.client.node.NodeClient; |
| 15 | +import org.opensearch.common.logging.DeprecationLogger; |
| 16 | +import org.opensearch.common.settings.Settings; |
| 17 | +import org.opensearch.rest.BaseRestHandler; |
| 18 | +import org.opensearch.rest.action.cat.RestNodesAction; |
| 19 | +import org.opensearch.test.OpenSearchTestCase; |
| 20 | +import org.opensearch.test.rest.FakeRestRequest; |
| 21 | +import org.opensearch.threadpool.TestThreadPool; |
| 22 | + |
| 23 | +import static org.hamcrest.Matchers.containsString; |
| 24 | + |
| 25 | +/** |
| 26 | + * As of 2.0, the request parameter 'master_timeout' in all applicable REST APIs is deprecated, |
| 27 | + * and alternative parameter 'cluster_manager_timeout' is added. |
| 28 | + * The tests are used to validate the behavior about the renamed request parameter. |
| 29 | + * Remove the test after removing MASTER_ROLE and 'master_timeout'. |
| 30 | + */ |
| 31 | +public class RenamedTimeoutRequestParameterTests extends OpenSearchTestCase { |
| 32 | + private final TestThreadPool threadPool = new TestThreadPool(RenamedTimeoutRequestParameterTests.class.getName()); |
| 33 | + private final NodeClient client = new NodeClient(Settings.EMPTY, threadPool); |
| 34 | + private final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RenamedTimeoutRequestParameterTests.class); |
| 35 | + |
| 36 | + private static final String DUPLICATE_PARAMETER_ERROR_MESSAGE = |
| 37 | + "Please only use one of the request parameters [master_timeout, cluster_manager_timeout]."; |
| 38 | + private static final String MASTER_TIMEOUT_DEPRECATED_MESSAGE = |
| 39 | + "Deprecated parameter [master_timeout] used. To promote inclusive language, please use [cluster_manager_timeout] instead. It will be unsupported in a future major version."; |
| 40 | + |
| 41 | + @After |
| 42 | + public void terminateThreadPool() { |
| 43 | + terminate(threadPool); |
| 44 | + } |
| 45 | + |
| 46 | + public void testNoWarningsForNewParam() { |
| 47 | + BaseRestHandler.parseDeprecatedMasterTimeoutParameter( |
| 48 | + getMasterNodeRequest(), |
| 49 | + getRestRequestWithNewParam(), |
| 50 | + deprecationLogger, |
| 51 | + "test" |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + public void testDeprecationWarningForOldParam() { |
| 56 | + BaseRestHandler.parseDeprecatedMasterTimeoutParameter( |
| 57 | + getMasterNodeRequest(), |
| 58 | + getRestRequestWithDeprecatedParam(), |
| 59 | + deprecationLogger, |
| 60 | + "test" |
| 61 | + ); |
| 62 | + assertWarnings(MASTER_TIMEOUT_DEPRECATED_MESSAGE); |
| 63 | + } |
| 64 | + |
| 65 | + public void testBothParamsNotValid() { |
| 66 | + Exception e = assertThrows( |
| 67 | + OpenSearchParseException.class, |
| 68 | + () -> BaseRestHandler.parseDeprecatedMasterTimeoutParameter( |
| 69 | + getMasterNodeRequest(), |
| 70 | + getRestRequestWithBothParams(), |
| 71 | + deprecationLogger, |
| 72 | + "test" |
| 73 | + ) |
| 74 | + ); |
| 75 | + assertThat(e.getMessage(), containsString(DUPLICATE_PARAMETER_ERROR_MESSAGE)); |
| 76 | + assertWarnings(MASTER_TIMEOUT_DEPRECATED_MESSAGE); |
| 77 | + } |
| 78 | + |
| 79 | + public void testCatAllocation() { |
| 80 | + RestNodesAction action = new RestNodesAction(); |
| 81 | + Exception e = assertThrows(OpenSearchParseException.class, () -> action.doCatRequest(getRestRequestWithBothParams(), client)); |
| 82 | + assertThat(e.getMessage(), containsString(DUPLICATE_PARAMETER_ERROR_MESSAGE)); |
| 83 | + assertWarnings(MASTER_TIMEOUT_DEPRECATED_MESSAGE); |
| 84 | + } |
| 85 | + |
| 86 | + private MasterNodeRequest getMasterNodeRequest() { |
| 87 | + return new MasterNodeRequest() { |
| 88 | + @Override |
| 89 | + public ActionRequestValidationException validate() { |
| 90 | + return null; |
| 91 | + } |
| 92 | + }; |
| 93 | + } |
| 94 | + |
| 95 | + private FakeRestRequest getRestRequestWithBothParams() { |
| 96 | + FakeRestRequest request = new FakeRestRequest(); |
| 97 | + request.params().put("cluster_manager_timeout", "1h"); |
| 98 | + request.params().put("master_timeout", "3s"); |
| 99 | + return request; |
| 100 | + } |
| 101 | + |
| 102 | + private FakeRestRequest getRestRequestWithDeprecatedParam() { |
| 103 | + FakeRestRequest request = new FakeRestRequest(); |
| 104 | + request.params().put("master_timeout", "3s"); |
| 105 | + return request; |
| 106 | + } |
| 107 | + |
| 108 | + private FakeRestRequest getRestRequestWithNewParam() { |
| 109 | + FakeRestRequest request = new FakeRestRequest(); |
| 110 | + request.params().put("cluster_manager_timeout", "2m"); |
| 111 | + return request; |
| 112 | + } |
| 113 | +} |
0 commit comments