1616import org .elasticsearch .cluster .DiskUsage ;
1717import org .elasticsearch .cluster .metadata .Metadata ;
1818import org .elasticsearch .cluster .node .DiscoveryNode ;
19+ import org .elasticsearch .cluster .routing .allocation .DiskThresholdSettings ;
1920import org .elasticsearch .cluster .service .ClusterService ;
2021import org .elasticsearch .common .settings .ClusterSettings ;
2122import org .elasticsearch .common .settings .Settings ;
23+ import org .elasticsearch .common .util .CollectionUtils ;
2224import org .elasticsearch .license .XPackLicenseState ;
2325import org .elasticsearch .plugins .PluginsService ;
2426import org .elasticsearch .test .ESTestCase ;
2527import org .elasticsearch .threadpool .TestThreadPool ;
2628import org .elasticsearch .threadpool .ThreadPool ;
2729import org .elasticsearch .transport .TransportService ;
2830import org .elasticsearch .xpack .core .deprecation .DeprecationIssue ;
31+ import org .hamcrest .BaseMatcher ;
32+ import org .hamcrest .Description ;
33+ import org .hamcrest .Matcher ;
2934import org .junit .After ;
3035import org .junit .Assert ;
3136import org .junit .Before ;
3237import org .mockito .Mockito ;
3338
39+ import java .util .Collections ;
3440import java .util .List ;
3541import java .util .Map ;
3642import java .util .Set ;
3743import java .util .concurrent .TimeUnit ;
3844import java .util .concurrent .atomic .AtomicReference ;
3945
46+ import static org .elasticsearch .common .settings .ClusterSettings .BUILT_IN_CLUSTER_SETTINGS ;
47+ import static org .hamcrest .Matchers .equalTo ;
48+ import static org .hamcrest .Matchers .hasItem ;
49+ import static org .hamcrest .Matchers .hasSize ;
4050import static org .mockito .Mockito .when ;
4151
4252public class TransportNodeDeprecationCheckActionTests extends ESTestCase {
@@ -98,7 +108,6 @@ public void testNodeOperation() {
98108 actionFilters ,
99109 clusterInfoService
100110 );
101- NodesDeprecationCheckAction .NodeRequest nodeRequest = null ;
102111 AtomicReference <Settings > visibleNodeSettings = new AtomicReference <>();
103112 AtomicReference <Settings > visibleClusterStateMetadataSettings = new AtomicReference <>();
104113 NodeDeprecationChecks .NodeDeprecationCheck <
@@ -118,7 +127,7 @@ public void testNodeOperation() {
118127 ClusterState ,
119128 XPackLicenseState ,
120129 DeprecationIssue >> nodeSettingsChecks = List .of (nodeSettingCheck );
121- transportNodeDeprecationCheckAction .nodeOperation (nodeRequest , nodeSettingsChecks );
130+ transportNodeDeprecationCheckAction .nodeOperation (nodeSettingsChecks );
122131 settingsBuilder = Settings .builder ();
123132 settingsBuilder .put ("some.undeprecated.property" , "someValue3" );
124133 settingsBuilder .putList ("some.undeprecated.list.property" , List .of ("someValue4" , "someValue5" ));
@@ -137,7 +146,7 @@ public void testNodeOperation() {
137146 .putList (TransportDeprecationInfoAction .SKIP_DEPRECATIONS_SETTING .getKey (), List .of ("some.undeprecated.property" ))
138147 .build ();
139148 clusterSettings .applySettings (newSettings );
140- transportNodeDeprecationCheckAction .nodeOperation (nodeRequest , nodeSettingsChecks );
149+ transportNodeDeprecationCheckAction .nodeOperation (nodeSettingsChecks );
141150 settingsBuilder = Settings .builder ();
142151 settingsBuilder .put ("some.deprecated.property" , "someValue1" );
143152 settingsBuilder .put ("some.other.bad.deprecated.property" , "someValue2" );
@@ -163,7 +172,7 @@ public void testCheckDiskLowWatermark() {
163172 settingsBuilder .put ("cluster.routing.allocation.disk.watermark.low" , "10%" );
164173 Settings settingsWithLowWatermark = settingsBuilder .build ();
165174 Settings dynamicSettings = settingsWithLowWatermark ;
166- ClusterSettings clusterSettings = new ClusterSettings (nodeSettings , ClusterSettings . BUILT_IN_CLUSTER_SETTINGS );
175+ ClusterSettings clusterSettings = new ClusterSettings (nodeSettings , BUILT_IN_CLUSTER_SETTINGS );
167176 String nodeId = "123" ;
168177 long totalBytesOnMachine = 100 ;
169178 long totalBytesFree = 70 ;
@@ -203,4 +212,93 @@ public void testCheckDiskLowWatermark() {
203212 assertNotNull (issue );
204213 assertEquals ("Disk usage exceeds low watermark" , issue .getMessage ());
205214 }
215+
216+ public void testDiskLowWatermarkIsIncludedInDeprecationWarnings () {
217+ Settings .Builder settingsBuilder = Settings .builder ();
218+ String deprecatedSettingKey = "some.deprecated.property" ;
219+ settingsBuilder .put (deprecatedSettingKey , "someValue1" );
220+ settingsBuilder .put (DiskThresholdSettings .CLUSTER_ROUTING_ALLOCATION_LOW_DISK_WATERMARK_SETTING .getKey (), "10%" );
221+ Settings nodeSettings = settingsBuilder .build ();
222+ final XPackLicenseState licenseState = null ;
223+ ClusterState clusterState = ClusterState .builder (ClusterName .DEFAULT ).metadata (Metadata .builder ().build ()).build ();
224+ ClusterService clusterService = Mockito .mock (ClusterService .class );
225+ when (clusterService .state ()).thenReturn (clusterState );
226+ ClusterSettings clusterSettings = new ClusterSettings (
227+ nodeSettings ,
228+ Set .copyOf (CollectionUtils .appendToCopy (BUILT_IN_CLUSTER_SETTINGS , TransportDeprecationInfoAction .SKIP_DEPRECATIONS_SETTING ))
229+ );
230+ when ((clusterService .getClusterSettings ())).thenReturn (clusterSettings );
231+ DiscoveryNode node = Mockito .mock (DiscoveryNode .class );
232+ String nodeId = "123" ;
233+ when (node .getId ()).thenReturn (nodeId );
234+ TransportService transportService = Mockito .mock (TransportService .class );
235+ when (transportService .getThreadPool ()).thenReturn (threadPool );
236+ when (transportService .getLocalNode ()).thenReturn (node );
237+ PluginsService pluginsService = Mockito .mock (PluginsService .class );
238+ ActionFilters actionFilters = Mockito .mock (ActionFilters .class );
239+ ClusterInfoService clusterInfoService = Mockito .mock (ClusterInfoService .class );
240+ long totalBytesOnMachine = 100 ;
241+ long totalBytesFree = 70 ;
242+ ClusterInfo clusterInfo = ClusterInfo .builder ()
243+ .mostAvailableSpaceUsage (Map .of (nodeId , new DiskUsage (nodeId , "" , "" , totalBytesOnMachine , totalBytesFree )))
244+ .build ();
245+ when (clusterInfoService .getClusterInfo ()).thenReturn (clusterInfo );
246+ TransportNodeDeprecationCheckAction transportNodeDeprecationCheckAction = new TransportNodeDeprecationCheckAction (
247+ nodeSettings ,
248+ threadPool ,
249+ licenseState ,
250+ clusterService ,
251+ transportService ,
252+ pluginsService ,
253+ actionFilters ,
254+ clusterInfoService
255+ );
256+
257+ NodeDeprecationChecks .NodeDeprecationCheck <
258+ Settings ,
259+ PluginsAndModules ,
260+ ClusterState ,
261+ XPackLicenseState ,
262+ DeprecationIssue > deprecationCheck = (first , second , third , fourth ) -> {
263+ if (first .keySet ().contains (deprecatedSettingKey )) {
264+ return new DeprecationIssue (DeprecationIssue .Level .WARNING , "Deprecated setting" , null , null , false , null );
265+ }
266+ return null ;
267+ };
268+ NodesDeprecationCheckAction .NodeResponse nodeResponse = transportNodeDeprecationCheckAction .nodeOperation (
269+ Collections .singletonList (deprecationCheck )
270+ );
271+ List <DeprecationIssue > deprecationIssues = nodeResponse .getDeprecationIssues ();
272+ assertThat (deprecationIssues , hasSize (2 ));
273+ assertThat (deprecationIssues , hasItem (new DeprecationIssueMatcher (DeprecationIssue .Level .WARNING , equalTo ("Deprecated setting" ))));
274+ assertThat (
275+ deprecationIssues ,
276+ hasItem (new DeprecationIssueMatcher (DeprecationIssue .Level .CRITICAL , equalTo ("Disk usage exceeds low watermark" )))
277+ );
278+ }
279+
280+ private static class DeprecationIssueMatcher extends BaseMatcher <DeprecationIssue > {
281+ private final DeprecationIssue .Level level ;
282+ private final Matcher <String > messageMatcher ;
283+
284+ private DeprecationIssueMatcher (DeprecationIssue .Level level , Matcher <String > messageMatcher ) {
285+ this .level = level ;
286+ this .messageMatcher = messageMatcher ;
287+ }
288+
289+ @ Override
290+ public boolean matches (Object actual ) {
291+ if (actual instanceof DeprecationIssue == false ) {
292+ return false ;
293+ }
294+ DeprecationIssue actualDeprecationIssue = (DeprecationIssue ) actual ;
295+ return level .equals (actualDeprecationIssue .getLevel ()) && messageMatcher .matches (actualDeprecationIssue .getMessage ());
296+ }
297+
298+ @ Override
299+ public void describeTo (Description description ) {
300+ description .appendText ("deprecation issue with level: " ).appendValue (level ).appendText (" and message: " );
301+ messageMatcher .describeTo (description );
302+ }
303+ }
206304}
0 commit comments