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 ;
@@ -209,4 +218,93 @@ public void testCheckDiskLowWatermark() {
209218 assertNotNull (issue );
210219 assertEquals ("Disk usage exceeds low watermark" , issue .getMessage ());
211220 }
221+
222+ public void testDiskLowWatermarkIsIncludedInDeprecationWarnings () {
223+ Settings .Builder settingsBuilder = Settings .builder ();
224+ String deprecatedSettingKey = "some.deprecated.property" ;
225+ settingsBuilder .put (deprecatedSettingKey , "someValue1" );
226+ settingsBuilder .put (DiskThresholdSettings .CLUSTER_ROUTING_ALLOCATION_LOW_DISK_WATERMARK_SETTING .getKey (), "10%" );
227+ Settings nodeSettings = settingsBuilder .build ();
228+ final XPackLicenseState licenseState = null ;
229+ ClusterState clusterState = ClusterState .builder (ClusterName .DEFAULT ).metadata (Metadata .builder ().build ()).build ();
230+ ClusterService clusterService = Mockito .mock (ClusterService .class );
231+ when (clusterService .state ()).thenReturn (clusterState );
232+ ClusterSettings clusterSettings = new ClusterSettings (
233+ nodeSettings ,
234+ Set .copyOf (CollectionUtils .appendToCopy (BUILT_IN_CLUSTER_SETTINGS , TransportDeprecationInfoAction .SKIP_DEPRECATIONS_SETTING ))
235+ );
236+ when ((clusterService .getClusterSettings ())).thenReturn (clusterSettings );
237+ DiscoveryNode node = Mockito .mock (DiscoveryNode .class );
238+ String nodeId = "123" ;
239+ when (node .getId ()).thenReturn (nodeId );
240+ TransportService transportService = Mockito .mock (TransportService .class );
241+ when (transportService .getThreadPool ()).thenReturn (threadPool );
242+ when (transportService .getLocalNode ()).thenReturn (node );
243+ PluginsService pluginsService = Mockito .mock (PluginsService .class );
244+ ActionFilters actionFilters = Mockito .mock (ActionFilters .class );
245+ ClusterInfoService clusterInfoService = Mockito .mock (ClusterInfoService .class );
246+ long totalBytesOnMachine = 100 ;
247+ long totalBytesFree = 70 ;
248+ ClusterInfo clusterInfo = ClusterInfo .builder ()
249+ .mostAvailableSpaceUsage (Map .of (nodeId , new DiskUsage (nodeId , "" , "" , totalBytesOnMachine , totalBytesFree )))
250+ .build ();
251+ when (clusterInfoService .getClusterInfo ()).thenReturn (clusterInfo );
252+ TransportNodeDeprecationCheckAction transportNodeDeprecationCheckAction = new TransportNodeDeprecationCheckAction (
253+ nodeSettings ,
254+ threadPool ,
255+ licenseState ,
256+ clusterService ,
257+ transportService ,
258+ pluginsService ,
259+ actionFilters ,
260+ clusterInfoService
261+ );
262+
263+ NodeDeprecationChecks .NodeDeprecationCheck <
264+ Settings ,
265+ PluginsAndModules ,
266+ ClusterState ,
267+ XPackLicenseState ,
268+ DeprecationIssue > deprecationCheck = (first , second , third , fourth ) -> {
269+ if (first .keySet ().contains (deprecatedSettingKey )) {
270+ return new DeprecationIssue (DeprecationIssue .Level .WARNING , "Deprecated setting" , null , null , false , null );
271+ }
272+ return null ;
273+ };
274+ NodesDeprecationCheckAction .NodeResponse nodeResponse = transportNodeDeprecationCheckAction .nodeOperation (
275+ Collections .singletonList (deprecationCheck )
276+ );
277+ List <DeprecationIssue > deprecationIssues = nodeResponse .getDeprecationIssues ();
278+ assertThat (deprecationIssues , hasSize (2 ));
279+ assertThat (deprecationIssues , hasItem (new DeprecationIssueMatcher (DeprecationIssue .Level .WARNING , equalTo ("Deprecated setting" ))));
280+ assertThat (
281+ deprecationIssues ,
282+ hasItem (new DeprecationIssueMatcher (DeprecationIssue .Level .CRITICAL , equalTo ("Disk usage exceeds low watermark" )))
283+ );
284+ }
285+
286+ private static class DeprecationIssueMatcher extends BaseMatcher <DeprecationIssue > {
287+ private final DeprecationIssue .Level level ;
288+ private final Matcher <String > messageMatcher ;
289+
290+ private DeprecationIssueMatcher (DeprecationIssue .Level level , Matcher <String > messageMatcher ) {
291+ this .level = level ;
292+ this .messageMatcher = messageMatcher ;
293+ }
294+
295+ @ Override
296+ public boolean matches (Object actual ) {
297+ if (actual instanceof DeprecationIssue == false ) {
298+ return false ;
299+ }
300+ DeprecationIssue actualDeprecationIssue = (DeprecationIssue ) actual ;
301+ return level .equals (actualDeprecationIssue .getLevel ()) && messageMatcher .matches (actualDeprecationIssue .getMessage ());
302+ }
303+
304+ @ Override
305+ public void describeTo (Description description ) {
306+ description .appendText ("deprecation issue with level: " ).appendValue (level ).appendText (" and message: " );
307+ messageMatcher .describeTo (description );
308+ }
309+ }
212310}
0 commit comments