19
19
20
20
package org .elasticsearch .script ;
21
21
22
+ import org .elasticsearch .ExceptionsHelper ;
23
+ import org .elasticsearch .action .index .IndexRequestBuilder ;
24
+ import org .elasticsearch .action .search .SearchPhaseExecutionException ;
22
25
import org .elasticsearch .action .search .SearchResponse ;
23
26
import org .elasticsearch .test .ElasticsearchIntegrationTest ;
24
27
import org .junit .Test ;
25
28
29
+ import java .util .List ;
30
+
31
+ import static com .google .common .collect .Lists .newArrayList ;
32
+ import static org .elasticsearch .index .query .FilterBuilders .scriptFilter ;
33
+ import static org .elasticsearch .index .query .QueryBuilders .constantScoreQuery ;
26
34
import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertNoFailures ;
35
+ import static org .hamcrest .Matchers .equalTo ;
27
36
28
37
/**
29
38
* Various tests for Groovy scripting
@@ -47,4 +56,50 @@ public void assertScript(String script) {
47
56
"; 1\" , \" type\" : \" number\" , \" lang\" : \" groovy\" }}}" ).get ();
48
57
assertNoFailures (resp );
49
58
}
59
+
60
+ @ Test
61
+ public void testGroovyExceptionSerialization () throws Exception {
62
+ List <IndexRequestBuilder > reqs = newArrayList ();
63
+ for (int i = 0 ; i < randomIntBetween (50 , 500 ); i ++) {
64
+ reqs .add (client ().prepareIndex ("test" , "doc" , "" + i ).setSource ("foo" , "bar" ));
65
+ }
66
+ indexRandom (true , false , reqs );
67
+ try {
68
+ client ().prepareSearch ("test" ).setQuery (constantScoreQuery (scriptFilter ("1 == not_found" ).lang ("groovy" ))).get ();
69
+ fail ("should have thrown an exception" );
70
+ } catch (SearchPhaseExecutionException e ) {
71
+ assertThat (ExceptionsHelper .detailedMessage (e ) + "should not contained NotSerializableTransportException" ,
72
+ ExceptionsHelper .detailedMessage (e ).contains ("NotSerializableTransportException" ), equalTo (false ));
73
+ assertThat (ExceptionsHelper .detailedMessage (e ) + "should have contained GroovyScriptExecutionException" ,
74
+ ExceptionsHelper .detailedMessage (e ).contains ("GroovyScriptExecutionException" ), equalTo (true ));
75
+ assertThat (ExceptionsHelper .detailedMessage (e ) + "should have contained not_found" ,
76
+ ExceptionsHelper .detailedMessage (e ).contains ("No such property: not_found" ), equalTo (true ));
77
+ }
78
+
79
+ try {
80
+ client ().prepareSearch ("test" ).setQuery (constantScoreQuery (
81
+ scriptFilter ("pr = Runtime.getRuntime().exec(\" touch /tmp/gotcha\" ); pr.waitFor()" ).lang ("groovy" ))).get ();
82
+ fail ("should have thrown an exception" );
83
+ } catch (SearchPhaseExecutionException e ) {
84
+ assertThat (ExceptionsHelper .detailedMessage (e ) + "should not contained NotSerializableTransportException" ,
85
+ ExceptionsHelper .detailedMessage (e ).contains ("NotSerializableTransportException" ), equalTo (false ));
86
+ assertThat (ExceptionsHelper .detailedMessage (e ) + "should have contained GroovyScriptCompilationException" ,
87
+ ExceptionsHelper .detailedMessage (e ).contains ("GroovyScriptCompilationException" ), equalTo (true ));
88
+ assertThat (ExceptionsHelper .detailedMessage (e ) + "should have contained Method calls not allowed on [java.lang.Runtime]" ,
89
+ ExceptionsHelper .detailedMessage (e ).contains ("Method calls not allowed on [java.lang.Runtime]" ), equalTo (true ));
90
+ }
91
+
92
+ try {
93
+ client ().prepareSearch ("test" ).setQuery (constantScoreQuery (
94
+ scriptFilter ("assert false" ).lang ("groovy" ))).get ();
95
+ fail ("should have thrown an exception" );
96
+ } catch (SearchPhaseExecutionException e ) {
97
+ assertThat (ExceptionsHelper .detailedMessage (e ) + "should not contained NotSerializableTransportException" ,
98
+ ExceptionsHelper .detailedMessage (e ).contains ("NotSerializableTransportException" ), equalTo (false ));
99
+ assertThat (ExceptionsHelper .detailedMessage (e ) + "should have contained GroovyScriptExecutionException" ,
100
+ ExceptionsHelper .detailedMessage (e ).contains ("GroovyScriptExecutionException" ), equalTo (true ));
101
+ assertThat (ExceptionsHelper .detailedMessage (e ) + "should have contained an assert error" ,
102
+ ExceptionsHelper .detailedMessage (e ).contains ("PowerAssertionError[assert false" ), equalTo (true ));
103
+ }
104
+ }
50
105
}
0 commit comments