@@ -163,6 +163,7 @@ public static function cleanUpCluster(Client $client): void
163
163
self ::ensureNoInitializingShards ($ client );
164
164
self ::wipeCluster ($ client );
165
165
self ::waitForClusterStateUpdatesToFinish ($ client );
166
+ self ::checkForUnexpectedlyRecreatedObjects ($ client );
166
167
}
167
168
168
169
/**
@@ -607,7 +608,15 @@ private static function preserveILMPolicyIds(): array
607
608
"watch-history-ilm-policy " ,
608
609
"ml-size-based-ilm-policy " ,
609
610
"logs " ,
610
- "metrics "
611
+ "metrics " ,
612
+ "synthetics " ,
613
+ "7-days-default " ,
614
+ "30-days-default " ,
615
+ "90-days-default " ,
616
+ "180-days-default " ,
617
+ "365-days-default " ,
618
+ ".fleet-actions-results-ilm-policy " ,
619
+ ".deprecation-indexing-ilm-policy "
611
620
];
612
621
}
613
622
@@ -714,4 +723,85 @@ private static function waitForClusterStateUpdatesToFinish(Client $client, int $
714
723
$ stillWaiting = ! empty ($ result ['tasks ' ]);
715
724
} while ($ stillWaiting && time () < ($ start + $ timeout ));
716
725
}
726
+
727
+ /**
728
+ * Returns all the unexpected ilm policies, removing $exclusions from the list
729
+ */
730
+ private static function getAllUnexpectedIlmPolicies (Client $ client , array $ exclusions ): array
731
+ {
732
+ try {
733
+ $ policies = $ client ->ilm ()->getLifecycle ();
734
+ } catch (ElasticsearchException $ e ) {
735
+ return [];
736
+ }
737
+ foreach ($ policies as $ name => $ value ) {
738
+ if (in_array ($ name , $ exclusions )) {
739
+ unset($ policies [$ name ]);
740
+ }
741
+ }
742
+ return $ policies ;
743
+ }
744
+
745
+ /**
746
+ * Returns all the unexpected templates
747
+ */
748
+ private static function getAllUnexpectedTemplates (Client $ client ): array
749
+ {
750
+ if (!self ::$ hasXPack ) {
751
+ return [];
752
+ }
753
+ $ unexpected = [];
754
+ // In case of bwc testing, if all nodes are before 7.7.0 then no need
755
+ // to attempt to delete component and composable index templates,
756
+ // because these were introduced in 7.7.0:
757
+ if (version_compare (self ::getVersion ($ client ), '7.6.99 ' ) > 0 ) {
758
+ $ result = $ client ->indices ()->getIndexTemplate ();
759
+ foreach ($ result ['index_templates ' ] as $ template ) {
760
+ if (!self ::isXPackTemplate ($ template ['name ' ])) {
761
+ $ unexpected [$ template ['name ' ]] = true ;
762
+ }
763
+ }
764
+ $ result = $ client ->cluster ()->getComponentTemplate ();
765
+ foreach ($ result ['component_templates ' ] as $ template ) {
766
+ if (!self ::isXPackTemplate ($ template ['name ' ])) {
767
+ $ unexpected [$ template ['name ' ]] = true ;
768
+ }
769
+ }
770
+ }
771
+ $ result = $ client ->indices ()->getIndexTemplate ();
772
+ foreach ($ result ['index_templates ' ] as $ template ) {
773
+ if (!self ::isXPackTemplate ($ template ['name ' ])) {
774
+ $ unexpected [$ template ['name ' ]] = true ;
775
+ }
776
+ }
777
+ return array_keys ($ unexpected );
778
+ }
779
+
780
+
781
+ /**
782
+ * This method checks whether ILM policies or templates get recreated after
783
+ * they have been deleted. If so, we are probably deleting them unnecessarily,
784
+ * potentially causing test performance problems. This could happen for example
785
+ * if someone adds a new standard ILM policy but forgets to put it in the
786
+ * exclusion list in this test.
787
+ */
788
+ private static function checkForUnexpectedlyRecreatedObjects (Client $ client ): void
789
+ {
790
+ if (self ::$ hasIlm ) {
791
+ $ policies = self ::getAllUnexpectedIlmPolicies ($ client , self ::preserveILMPolicyIds ());
792
+ if (count ($ policies ) > 0 ) {
793
+ throw new Exception (sprintf (
794
+ "Expected no ILM policies after deletions, but found %s " ,
795
+ implode (', ' , array_keys ($ policies ))
796
+ ));
797
+ }
798
+ }
799
+ $ templates = self ::getAllUnexpectedTemplates ($ client );
800
+ if (count ($ templates ) > 0 ) {
801
+ throw new Exception (sprintf (
802
+ "Expected no templates after deletions, but found %s " ,
803
+ implode (', ' , array_keys ($ templates ))
804
+ ));
805
+ }
806
+ }
717
807
}
0 commit comments