opensearch-transport 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +3 -0
  3. data/.gitignore +17 -0
  4. data/Gemfile +47 -0
  5. data/LICENSE +202 -0
  6. data/README.md +551 -0
  7. data/Rakefile +89 -0
  8. data/lib/opensearch/transport/client.rb +354 -0
  9. data/lib/opensearch/transport/redacted.rb +84 -0
  10. data/lib/opensearch/transport/transport/base.rb +450 -0
  11. data/lib/opensearch/transport/transport/connections/collection.rb +136 -0
  12. data/lib/opensearch/transport/transport/connections/connection.rb +169 -0
  13. data/lib/opensearch/transport/transport/connections/selector.rb +101 -0
  14. data/lib/opensearch/transport/transport/errors.rb +100 -0
  15. data/lib/opensearch/transport/transport/http/curb.rb +140 -0
  16. data/lib/opensearch/transport/transport/http/faraday.rb +101 -0
  17. data/lib/opensearch/transport/transport/http/manticore.rb +188 -0
  18. data/lib/opensearch/transport/transport/loggable.rb +94 -0
  19. data/lib/opensearch/transport/transport/response.rb +46 -0
  20. data/lib/opensearch/transport/transport/serializer/multi_json.rb +62 -0
  21. data/lib/opensearch/transport/transport/sniffer.rb +111 -0
  22. data/lib/opensearch/transport/version.rb +31 -0
  23. data/lib/opensearch/transport.rb +46 -0
  24. data/lib/opensearch-transport.rb +27 -0
  25. data/opensearch-transport.gemspec +92 -0
  26. data/spec/opensearch/connections/collection_spec.rb +275 -0
  27. data/spec/opensearch/connections/selector_spec.rb +183 -0
  28. data/spec/opensearch/transport/base_spec.rb +313 -0
  29. data/spec/opensearch/transport/client_spec.rb +1818 -0
  30. data/spec/opensearch/transport/sniffer_spec.rb +284 -0
  31. data/spec/spec_helper.rb +99 -0
  32. data/test/integration/transport_test.rb +108 -0
  33. data/test/profile/client_benchmark_test.rb +141 -0
  34. data/test/test_helper.rb +97 -0
  35. data/test/unit/connection_test.rb +145 -0
  36. data/test/unit/response_test.rb +41 -0
  37. data/test/unit/serializer_test.rb +42 -0
  38. data/test/unit/transport_base_test.rb +673 -0
  39. data/test/unit/transport_curb_test.rb +143 -0
  40. data/test/unit/transport_faraday_test.rb +237 -0
  41. data/test/unit/transport_manticore_test.rb +191 -0
  42. data.tar.gz.sig +1 -0
  43. metadata +456 -0
  44. metadata.gz.sig +1 -0
@@ -0,0 +1,97 @@
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ #
3
+ # The OpenSearch Contributors require contributions made to
4
+ # this file be licensed under the Apache-2.0 license or a
5
+ # compatible open source license.
6
+ #
7
+ # Modifications Copyright OpenSearch Contributors. See
8
+ # GitHub history for details.
9
+ #
10
+ # Licensed to Elasticsearch B.V. under one or more contributor
11
+ # license agreements. See the NOTICE file distributed with
12
+ # this work for additional information regarding copyright
13
+ # ownership. Elasticsearch B.V. licenses this file to you under
14
+ # the Apache License, Version 2.0 (the "License"); you may
15
+ # not use this file except in compliance with the License.
16
+ # You may obtain a copy of the License at
17
+ #
18
+ # http://www.apache.org/licenses/LICENSE-2.0
19
+ #
20
+ # Unless required by applicable law or agreed to in writing,
21
+ # software distributed under the License is distributed on an
22
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23
+ # KIND, either express or implied. See the License for the
24
+ # specific language governing permissions and limitations
25
+ # under the License.
26
+
27
+
28
+ OPENSEARCH_HOSTS = if hosts = ENV['TEST_OPENSEARCH_SERVER'] || ENV['OPENSEARCH_HOSTS']
29
+ hosts.split(',').map do |host|
30
+ /(http\:\/\/)?(\S+)/.match(host)[2]
31
+ end
32
+ else
33
+ ['localhost:9200']
34
+ end.freeze
35
+
36
+ TEST_HOST, TEST_PORT = OPENSEARCH_HOSTS.first.split(':') if OPENSEARCH_HOSTS
37
+
38
+ JRUBY = defined?(JRUBY_VERSION)
39
+
40
+ if ENV['COVERAGE']
41
+ require 'simplecov'
42
+ SimpleCov.start { add_filter %r{^/test/} }
43
+ end
44
+
45
+ require 'minitest/autorun'
46
+ require 'minitest/reporters'
47
+ require 'shoulda/context'
48
+ require 'mocha/minitest'
49
+ require 'ansi/code'
50
+
51
+ require 'require-prof' if ENV["REQUIRE_PROF"]
52
+ require 'opensearch-transport'
53
+ require 'logger'
54
+
55
+ require 'hashie'
56
+
57
+ RequireProf.print_timing_infos if ENV["REQUIRE_PROF"]
58
+
59
+ class FixedMinitestSpecReporter < Minitest::Reporters::SpecReporter
60
+ def before_test(test)
61
+ last_test = tests.last
62
+
63
+ before_suite(test.class) unless last_test
64
+
65
+ if last_test && last_test.klass.to_s != test.class.to_s
66
+ after_suite(last_test.class) if last_test
67
+ before_suite(test.class)
68
+ end
69
+ end
70
+ end
71
+
72
+ module Minitest
73
+ class Test
74
+ def assert_nothing_raised(*args)
75
+ begin
76
+ line = __LINE__
77
+ yield
78
+ rescue RuntimeError => e
79
+ raise MiniTest::Assertion, "Exception raised:\n<#{e.class}>", e.backtrace
80
+ end
81
+ true
82
+ end
83
+
84
+ def assert_not_nil(object, msg=nil)
85
+ msg = message(msg) { "<#{object.inspect}> expected to not be nil" }
86
+ assert !object.nil?, msg
87
+ end
88
+
89
+ def assert_block(*msgs)
90
+ assert yield, *msgs
91
+ end
92
+
93
+ alias :assert_raise :assert_raises
94
+ end
95
+ end
96
+
97
+ Minitest::Reporters.use! FixedMinitestSpecReporter.new
@@ -0,0 +1,145 @@
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ #
3
+ # The OpenSearch Contributors require contributions made to
4
+ # this file be licensed under the Apache-2.0 license or a
5
+ # compatible open source license.
6
+ #
7
+ # Modifications Copyright OpenSearch Contributors. See
8
+ # GitHub history for details.
9
+ #
10
+ # Licensed to Elasticsearch B.V. under one or more contributor
11
+ # license agreements. See the NOTICE file distributed with
12
+ # this work for additional information regarding copyright
13
+ # ownership. Elasticsearch B.V. licenses this file to you under
14
+ # the Apache License, Version 2.0 (the "License"); you may
15
+ # not use this file except in compliance with the License.
16
+ # You may obtain a copy of the License at
17
+ #
18
+ # http://www.apache.org/licenses/LICENSE-2.0
19
+ #
20
+ # Unless required by applicable law or agreed to in writing,
21
+ # software distributed under the License is distributed on an
22
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23
+ # KIND, either express or implied. See the License for the
24
+ # specific language governing permissions and limitations
25
+ # under the License.
26
+
27
+ require 'test_helper'
28
+
29
+ class OpenSearch::Transport::Transport::Connections::ConnectionTest < Minitest::Test
30
+ include OpenSearch::Transport::Transport::Connections
31
+
32
+ context "Connection" do
33
+
34
+ should "be initialized with :host, :connection, and :options" do
35
+ c = Connection.new :host => 'x', :connection => 'y', :options => {}
36
+ assert_equal 'x', c.host
37
+ assert_equal 'y', c.connection
38
+ assert_instance_of Hash, c.options
39
+ end
40
+
41
+ should "return full path" do
42
+ c = Connection.new
43
+ assert_equal '_search', c.full_path('_search')
44
+ assert_equal '_search', c.full_path('_search', {})
45
+ assert_equal '_search?foo=bar', c.full_path('_search', {:foo => 'bar'})
46
+ assert_equal '_search?foo=bar+bam', c.full_path('_search', {:foo => 'bar bam'})
47
+ end
48
+
49
+ should "return full url" do
50
+ c = Connection.new :host => { :protocol => 'http', :host => 'localhost', :port => '9200' }
51
+ assert_equal 'http://localhost:9200/_search?foo=bar', c.full_url('_search', {:foo => 'bar'})
52
+ end
53
+
54
+ should "return full url with credentials" do
55
+ c = Connection.new :host => { :protocol => 'http', :user => 'U', :password => 'P', :host => 'localhost', :port => '9200' }
56
+ assert_equal 'http://U:P@localhost:9200/_search?foo=bar', c.full_url('_search', {:foo => 'bar'})
57
+ end
58
+
59
+ should "return full url with escaped credentials" do
60
+ c = Connection.new :host => { :protocol => 'http', :user => 'U$$$', :password => 'P^^^', :host => 'localhost', :port => '9200' }
61
+ assert_equal 'http://U%24%24%24:P%5E%5E%5E@localhost:9200/_search?foo=bar', c.full_url('_search', {:foo => 'bar'})
62
+ end
63
+
64
+ should "return full url with path" do
65
+ c = Connection.new :host => { :protocol => 'http', :host => 'localhost', :port => '9200', :path => '/foo' }
66
+ assert_equal 'http://localhost:9200/foo/_search?foo=bar', c.full_url('_search', {:foo => 'bar'})
67
+ end
68
+
69
+ should "return right full url with path when path starts with /" do
70
+ c = Connection.new :host => { :protocol => 'http', :host => 'localhost', :port => '9200', :path => '/foo' }
71
+ assert_equal 'http://localhost:9200/foo/_search?foo=bar', c.full_url('/_search', {:foo => 'bar'})
72
+ end
73
+
74
+ should "have a string representation" do
75
+ c = Connection.new :host => 'x'
76
+ assert_match(/host: x/, c.to_s)
77
+ assert_match(/alive/, c.to_s)
78
+ end
79
+
80
+ should "not be dead by default" do
81
+ c = Connection.new
82
+ assert ! c.dead?
83
+ end
84
+
85
+ should "be dead when marked" do
86
+ c = Connection.new.dead!
87
+ assert c.dead?
88
+ assert_equal 1, c.failures
89
+ assert_in_delta c.dead_since, Time.now, 1
90
+ end
91
+
92
+ should "be alive when marked" do
93
+ c = Connection.new.dead!
94
+ assert c.dead?
95
+ assert_equal 1, c.failures
96
+ assert_in_delta c.dead_since, Time.now, 1
97
+
98
+ c.alive!
99
+ assert ! c.dead?
100
+ assert_equal 1, c.failures
101
+ end
102
+
103
+ should "be healthy when marked" do
104
+ c = Connection.new.dead!
105
+ assert c.dead?
106
+ assert_equal 1, c.failures
107
+ assert_in_delta c.dead_since, Time.now, 1
108
+
109
+ c.healthy!
110
+ assert ! c.dead?
111
+ assert_equal 0, c.failures
112
+ end
113
+
114
+ should "be resurrected if timeout passed" do
115
+ c = Connection.new.dead!
116
+
117
+ now = Time.now + 60
118
+ Time.stubs(:now).returns(now)
119
+
120
+ assert c.resurrect!, c.inspect
121
+ assert ! c.dead?, c.inspect
122
+ end
123
+
124
+ should "be resurrected if timeout passed for multiple failures" do
125
+ c = Connection.new.dead!.dead!
126
+
127
+ now = Time.now + 60*2
128
+ Time.stubs(:now).returns(now)
129
+
130
+ assert c.resurrect!, c.inspect
131
+ assert ! c.dead?, c.inspect
132
+ end
133
+
134
+ should "implement the equality operator" do
135
+ c1 = Connection.new(:host => { :protocol => 'http', :host => 'foo', :port => 123 })
136
+ c2 = Connection.new(:host => { :protocol => 'http', :host => 'foo', :port => 123 })
137
+ c3 = Connection.new(:host => { :protocol => 'http', :host => 'foo', :port => 456 })
138
+
139
+ assert c1 == c2, "Connection #{c1} should be equal to #{c2}"
140
+ assert c2 != c3, "Connection #{c2} should NOT be equal to #{c3}"
141
+ end
142
+
143
+ end
144
+
145
+ end
@@ -0,0 +1,41 @@
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ #
3
+ # The OpenSearch Contributors require contributions made to
4
+ # this file be licensed under the Apache-2.0 license or a
5
+ # compatible open source license.
6
+ #
7
+ # Modifications Copyright OpenSearch Contributors. See
8
+ # GitHub history for details.
9
+ #
10
+ # Licensed to Elasticsearch B.V. under one or more contributor
11
+ # license agreements. See the NOTICE file distributed with
12
+ # this work for additional information regarding copyright
13
+ # ownership. Elasticsearch B.V. licenses this file to you under
14
+ # the Apache License, Version 2.0 (the "License"); you may
15
+ # not use this file except in compliance with the License.
16
+ # You may obtain a copy of the License at
17
+ #
18
+ # http://www.apache.org/licenses/LICENSE-2.0
19
+ #
20
+ # Unless required by applicable law or agreed to in writing,
21
+ # software distributed under the License is distributed on an
22
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23
+ # KIND, either express or implied. See the License for the
24
+ # specific language governing permissions and limitations
25
+ # under the License.
26
+
27
+ require 'test_helper'
28
+
29
+ class OpenSearch::Transport::Transport::ResponseTest < Minitest::Test
30
+ context "Response" do
31
+
32
+ should "force-encode the body into UTF" do
33
+ body = "Hello Encoding!".encode(Encoding::ISO_8859_1)
34
+ assert_equal 'ISO-8859-1', body.encoding.name
35
+
36
+ response = OpenSearch::Transport::Transport::Response.new 200, body
37
+ assert_equal 'UTF-8', response.body.encoding.name
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,42 @@
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ #
3
+ # The OpenSearch Contributors require contributions made to
4
+ # this file be licensed under the Apache-2.0 license or a
5
+ # compatible open source license.
6
+ #
7
+ # Modifications Copyright OpenSearch Contributors. See
8
+ # GitHub history for details.
9
+ #
10
+ # Licensed to Elasticsearch B.V. under one or more contributor
11
+ # license agreements. See the NOTICE file distributed with
12
+ # this work for additional information regarding copyright
13
+ # ownership. Elasticsearch B.V. licenses this file to you under
14
+ # the Apache License, Version 2.0 (the "License"); you may
15
+ # not use this file except in compliance with the License.
16
+ # You may obtain a copy of the License at
17
+ #
18
+ # http://www.apache.org/licenses/LICENSE-2.0
19
+ #
20
+ # Unless required by applicable law or agreed to in writing,
21
+ # software distributed under the License is distributed on an
22
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23
+ # KIND, either express or implied. See the License for the
24
+ # specific language governing permissions and limitations
25
+ # under the License.
26
+
27
+ require 'test_helper'
28
+
29
+ class OpenSearch::Transport::Transport::SerializerTest < Minitest::Test
30
+
31
+ context "Serializer" do
32
+
33
+ should "use MultiJson by default" do
34
+ ::MultiJson.expects(:load)
35
+ ::MultiJson.expects(:dump)
36
+ OpenSearch::Transport::Transport::Serializer::MultiJson.new.load('{}')
37
+ OpenSearch::Transport::Transport::Serializer::MultiJson.new.dump({})
38
+ end
39
+
40
+ end
41
+
42
+ end