1313import  org .apache .lucene .tests .util .TimeUnits ;
1414import  org .elasticsearch .Version ;
1515import  org .elasticsearch .client .Request ;
16- import  org .elasticsearch .client .ResponseException ;
1716import  org .elasticsearch .client .RestClient ;
1817import  org .elasticsearch .common .xcontent .XContentHelper ;
1918import  org .elasticsearch .features .NodeFeature ;
3635import  org .elasticsearch .xpack .esql .qa .rest .RestEsqlTestCase .RequestObjectBuilder ;
3736import  org .elasticsearch .xpack .esql .telemetry .TookMetrics ;
3837import  org .junit .After ;
39- import  org .junit .AfterClass ;
4038import  org .junit .Before ;
4139import  org .junit .Rule ;
4240
6967import  static  org .elasticsearch .xpack .esql .CsvTestUtils .isEnabled ;
7068import  static  org .elasticsearch .xpack .esql .CsvTestUtils .loadCsvSpecValues ;
7169import  static  org .elasticsearch .xpack .esql .CsvTestsDataLoader .createInferenceEndpoints ;
72- import  static  org .elasticsearch .xpack .esql .CsvTestsDataLoader .deleteInferenceEndpoints ;
7370import  static  org .elasticsearch .xpack .esql .CsvTestsDataLoader .loadDataSetIntoEs ;
7471import  static  org .elasticsearch .xpack .esql .EsqlTestUtils .classpathResources ;
7572import  static  org .elasticsearch .xpack .esql .action .EsqlCapabilities .Cap .COMPLETION ;
@@ -122,9 +119,11 @@ protected EsqlSpecTestCase(
122119    }
123120
124121    private  static  boolean  dataLoaded  = false ;
122+     protected  static  boolean  testClustersOk  = true ;
125123
126124    @ Before 
127125    public  void  setup () throws  IOException  {
126+         assumeTrue ("test clusters were broken" , testClustersOk );
128127        boolean  supportsLookup  = supportsIndexModeLookup ();
129128        boolean  supportsSourceMapping  = supportsSourceFieldMapping ();
130129        boolean  supportsInferenceTestService  = supportsInferenceTestService ();
@@ -138,21 +137,6 @@ public void setup() throws IOException {
138137        }
139138    }
140139
141-     @ AfterClass 
142-     public  static  void  wipeTestData () throws  IOException  {
143-         try  {
144-             dataLoaded  = false ;
145-             adminClient ().performRequest (new  Request ("DELETE" , "/*" ));
146-         } catch  (ResponseException  e ) {
147-             // 404 here just means we had no indexes 
148-             if  (e .getResponse ().getStatusLine ().getStatusCode () != 404 ) {
149-                 throw  e ;
150-             }
151-         }
152- 
153-         deleteInferenceEndpoints (adminClient ());
154-     }
155- 
156140    public  boolean  logResults () {
157141        return  false ;
158142    }
@@ -162,11 +146,25 @@ public final void test() throws Throwable {
162146            shouldSkipTest (testName );
163147            doTest ();
164148        } catch  (Exception  e ) {
149+             ensureTestClustersAreOk (e );
165150            throw  reworkException (e );
166151        }
167152    }
168153
154+     protected  void  ensureTestClustersAreOk (Exception  failure ) {
155+         try  {
156+             ensureHealth (client (), "" , (request ) -> {
157+                 request .addParameter ("wait_for_status" , "yellow" );
158+                 request .addParameter ("level" , "shards" );
159+             });
160+         } catch  (Exception  inner ) {
161+             testClustersOk  = false ;
162+             failure .addSuppressed (inner );
163+         }
164+     }
165+ 
169166    protected  void  shouldSkipTest (String  testName ) throws  IOException  {
167+         assumeTrue ("test clusters were broken" , testClustersOk );
170168        if  (requiresInferenceEndpoint ()) {
171169            assumeTrue ("Inference test service needs to be supported" , supportsInferenceTestService ());
172170        }
@@ -374,7 +372,9 @@ protected boolean preserveClusterUponCompletion() {
374372
375373    @ After 
376374    public  void  assertRequestBreakerEmptyAfterTests () throws  Exception  {
377-         assertRequestBreakerEmpty ();
375+         if  (testClustersOk ) {
376+             assertRequestBreakerEmpty ();
377+         }
378378    }
379379
380380    public  static  void  assertRequestBreakerEmpty () throws  Exception  {
0 commit comments