66
77package org .elasticsearch .xpack .enrich .action ;
88
9+ import org .elasticsearch .ResourceNotFoundException ;
910import org .elasticsearch .action .ActionListener ;
1011import org .elasticsearch .action .support .ActionTestUtils ;
1112import org .elasticsearch .action .support .master .AcknowledgedResponse ;
1213import org .elasticsearch .cluster .service .ClusterService ;
1314import org .elasticsearch .common .util .concurrent .EsRejectedExecutionException ;
1415import org .elasticsearch .common .xcontent .XContentType ;
16+ import org .elasticsearch .index .IndexNotFoundException ;
1517import org .elasticsearch .xpack .core .enrich .EnrichPolicy ;
1618import org .elasticsearch .xpack .core .enrich .action .DeleteEnrichPolicyAction ;
17- import org .elasticsearch .xpack .enrich .EnrichPolicyLocks ;
1819import org .elasticsearch .xpack .enrich .AbstractEnrichTestCase ;
20+ import org .elasticsearch .xpack .enrich .EnrichPolicyLocks ;
21+ import org .elasticsearch .xpack .enrich .EnrichStore ;
22+ import org .junit .After ;
1923
2024import java .util .concurrent .CountDownLatch ;
2125import java .util .concurrent .atomic .AtomicReference ;
2529import static org .hamcrest .Matchers .nullValue ;
2630import static org .hamcrest .core .IsInstanceOf .instanceOf ;
2731
28- public class TransportDeleteEnricyPolicyActionTests extends AbstractEnrichTestCase {
32+ public class TransportDeleteEnrichPolicyActionTests extends AbstractEnrichTestCase {
33+
34+ @ After
35+ private void cleanupPolicy () {
36+ ClusterService clusterService = getInstanceFromNode (ClusterService .class );
37+ String name = "my-policy" ;
38+
39+ try {
40+ deleteEnrichPolicy (name , clusterService );
41+ } catch (Exception e ) {
42+ // if the enrich policy does not exist, then just keep going
43+ }
2944
30- public void testDeleteIsNotLocked () throws InterruptedException {
45+ // fail if the state of this is left locked
46+ EnrichPolicyLocks enrichPolicyLocks = getInstanceFromNode (EnrichPolicyLocks .class );
47+ assertFalse (enrichPolicyLocks .captureExecutionState ().isAnyPolicyInFlight ());
48+ }
49+
50+ public void testDeletePolicyDoesNotExistUnlocksPolicy () throws InterruptedException {
51+ String fakeId = "fake-id" ;
52+ createIndex (EnrichPolicy .getBaseName (fakeId ) + "-foo1" );
53+ createIndex (EnrichPolicy .getBaseName (fakeId ) + "-foo2" );
54+
55+ final CountDownLatch latch = new CountDownLatch (1 );
56+ final AtomicReference <Exception > reference = new AtomicReference <>();
57+ final TransportDeleteEnrichPolicyAction transportAction = node ().injector ().getInstance (TransportDeleteEnrichPolicyAction .class );
58+ ActionTestUtils .execute (transportAction , null ,
59+ new DeleteEnrichPolicyAction .Request (fakeId ),
60+ new ActionListener <AcknowledgedResponse >() {
61+ @ Override
62+ public void onResponse (AcknowledgedResponse acknowledgedResponse ) {
63+ fail ();
64+ }
65+
66+ public void onFailure (final Exception e ) {
67+ reference .set (e );
68+ latch .countDown ();
69+ }
70+ });
71+ latch .await ();
72+ assertNotNull (reference .get ());
73+ assertThat (reference .get (), instanceOf (ResourceNotFoundException .class ));
74+ assertThat (reference .get ().getMessage (), equalTo ("policy [fake-id] not found" ));
75+
76+ // fail if the state of this is left locked
77+ EnrichPolicyLocks enrichPolicyLocks = getInstanceFromNode (EnrichPolicyLocks .class );
78+ assertFalse (enrichPolicyLocks .captureExecutionState ().isAnyPolicyInFlight ());
79+ }
80+
81+ public void testDeleteWithoutIndex () throws Exception {
3182 EnrichPolicy policy = randomEnrichPolicy (XContentType .JSON );
3283 ClusterService clusterService = getInstanceFromNode (ClusterService .class );
3384 String name = "my-policy" ;
@@ -54,6 +105,56 @@ public void onFailure(final Exception e) {
54105 latch .await ();
55106 assertNotNull (reference .get ());
56107 assertTrue (reference .get ().isAcknowledged ());
108+
109+ EnrichPolicyLocks enrichPolicyLocks = getInstanceFromNode (EnrichPolicyLocks .class );
110+ assertFalse (enrichPolicyLocks .captureExecutionState ().isAnyPolicyInFlight ());
111+
112+ assertNull (EnrichStore .getPolicy (name , clusterService .state ()));
113+ }
114+
115+ public void testDeleteIsNotLocked () throws Exception {
116+ EnrichPolicy policy = randomEnrichPolicy (XContentType .JSON );
117+ ClusterService clusterService = getInstanceFromNode (ClusterService .class );
118+ String name = "my-policy" ;
119+
120+ AtomicReference <Exception > error = saveEnrichPolicy (name , policy , clusterService );
121+ assertThat (error .get (), nullValue ());
122+
123+ createIndex (EnrichPolicy .getBaseName (name ) + "-foo1" );
124+ createIndex (EnrichPolicy .getBaseName (name ) + "-foo2" );
125+
126+ client ().admin ().indices ().prepareGetIndex ().setIndices (
127+ EnrichPolicy .getBaseName (name ) + "-foo1" ,
128+ EnrichPolicy .getBaseName (name ) + "-foo2" ).get ();
129+
130+ final CountDownLatch latch = new CountDownLatch (1 );
131+ final AtomicReference <AcknowledgedResponse > reference = new AtomicReference <>();
132+ final TransportDeleteEnrichPolicyAction transportAction = node ().injector ().getInstance (TransportDeleteEnrichPolicyAction .class );
133+ ActionTestUtils .execute (transportAction , null ,
134+ new DeleteEnrichPolicyAction .Request (name ),
135+ new ActionListener <AcknowledgedResponse >() {
136+ @ Override
137+ public void onResponse (AcknowledgedResponse acknowledgedResponse ) {
138+ reference .set (acknowledgedResponse );
139+ latch .countDown ();
140+ }
141+
142+ public void onFailure (final Exception e ) {
143+ fail ();
144+ }
145+ });
146+ latch .await ();
147+ assertNotNull (reference .get ());
148+ assertTrue (reference .get ().isAcknowledged ());
149+
150+ expectThrows (IndexNotFoundException .class , () -> client ().admin ().indices ().prepareGetIndex ().setIndices (
151+ EnrichPolicy .getBaseName (name ) + "-foo1" ,
152+ EnrichPolicy .getBaseName (name ) + "-foo2" ).get ());
153+
154+ EnrichPolicyLocks enrichPolicyLocks = getInstanceFromNode (EnrichPolicyLocks .class );
155+ assertFalse (enrichPolicyLocks .captureExecutionState ().isAnyPolicyInFlight ());
156+
157+ assertNull (EnrichStore .getPolicy (name , clusterService .state ()));
57158 }
58159
59160 public void testDeleteLocked () throws InterruptedException {
@@ -64,6 +165,9 @@ public void testDeleteLocked() throws InterruptedException {
64165 AtomicReference <Exception > error = saveEnrichPolicy (name , policy , clusterService );
65166 assertThat (error .get (), nullValue ());
66167
168+ createIndex (EnrichPolicy .getBaseName (name ) + "-foo1" );
169+ createIndex (EnrichPolicy .getBaseName (name ) + "-foo2" );
170+
67171 EnrichPolicyLocks enrichPolicyLocks = getInstanceFromNode (EnrichPolicyLocks .class );
68172 assertFalse (enrichPolicyLocks .captureExecutionState ().isAnyPolicyInFlight ());
69173
@@ -117,6 +221,8 @@ public void onFailure(final Exception e) {
117221 assertTrue (reference .get ().isAcknowledged ());
118222
119223 assertFalse (enrichPolicyLocks .captureExecutionState ().isAnyPolicyInFlight ());
224+
225+ assertNull (EnrichStore .getPolicy (name , clusterService .state ()));
120226 }
121227 }
122228}
0 commit comments