testcontainers-elasticsearch 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 24ec3386c4c39ac5fc14d069dcefddd9f4db288adf88847d0c80b305c0db3834
4
+ data.tar.gz: 6d8f827ae56255277b4f2dbcab815ddb478b2990fe7482d78680d63ef534eb93
5
+ SHA512:
6
+ metadata.gz: 6e6a2fa3094100a9a6266f697a8fa03e2e482822373c62e73ef1531a40446847bdc0e6de074b028066fedf1edca0a1b2ddfcac270fc574ecc7f1233fbd3df032
7
+ data.tar.gz: 601aff44f9a21c03b92e67ac8394598f852e619786309600e8e23d60176daf8ae7105cdc5516f2e686c06322d1979be7e664b9bf3bf52adebed7a10ee89e7805
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [0.1.0] - 2023-05-13
2
+
3
+ ### Added
4
+
5
+ - Initial release of the ElasticSearch module
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in testcontainers-elasticsearch.gemspec
6
+ gemspec
7
+
8
+ # Use the latest version of testcontainers-core from the local path
9
+ gem "testcontainers-core", path: "../core"
data/Gemfile.lock ADDED
@@ -0,0 +1,90 @@
1
+ PATH
2
+ remote: ../core
3
+ specs:
4
+ testcontainers-core (0.1.3)
5
+ docker-api (~> 2.2)
6
+
7
+ PATH
8
+ remote: .
9
+ specs:
10
+ testcontainers-elasticsearch (0.1.0)
11
+ testcontainers-core (~> 0.1.3)
12
+
13
+ GEM
14
+ remote: https://rubygems.org/
15
+ specs:
16
+ ast (2.4.2)
17
+ docker-api (2.2.0)
18
+ excon (>= 0.47.0)
19
+ multi_json
20
+ elastic-transport (8.2.1)
21
+ faraday (< 3)
22
+ multi_json
23
+ elasticsearch (8.7.1)
24
+ elastic-transport (~> 8)
25
+ elasticsearch-api (= 8.7.1)
26
+ elasticsearch-api (8.7.1)
27
+ multi_json
28
+ excon (0.99.0)
29
+ faraday (2.7.4)
30
+ faraday-net_http (>= 2.0, < 3.1)
31
+ ruby2_keywords (>= 0.0.4)
32
+ faraday-net_http (3.0.2)
33
+ json (2.6.3)
34
+ language_server-protocol (3.17.0.3)
35
+ lint_roller (1.0.0)
36
+ minitest (5.18.0)
37
+ minitest-hooks (1.5.0)
38
+ minitest (> 5.3)
39
+ multi_json (1.15.0)
40
+ parallel (1.23.0)
41
+ parser (3.2.2.1)
42
+ ast (~> 2.4.1)
43
+ rainbow (3.1.1)
44
+ rake (13.0.6)
45
+ regexp_parser (2.8.0)
46
+ rexml (3.2.5)
47
+ rubocop (1.50.2)
48
+ json (~> 2.3)
49
+ parallel (~> 1.10)
50
+ parser (>= 3.2.0.0)
51
+ rainbow (>= 2.2.2, < 4.0)
52
+ regexp_parser (>= 1.8, < 3.0)
53
+ rexml (>= 3.2.5, < 4.0)
54
+ rubocop-ast (>= 1.28.0, < 2.0)
55
+ ruby-progressbar (~> 1.7)
56
+ unicode-display_width (>= 2.4.0, < 3.0)
57
+ rubocop-ast (1.28.1)
58
+ parser (>= 3.2.1.0)
59
+ rubocop-performance (1.16.0)
60
+ rubocop (>= 1.7.0, < 2.0)
61
+ rubocop-ast (>= 0.4.0)
62
+ ruby-progressbar (1.13.0)
63
+ ruby2_keywords (0.0.5)
64
+ standard (1.28.2)
65
+ language_server-protocol (~> 3.17.0.2)
66
+ lint_roller (~> 1.0)
67
+ rubocop (~> 1.50.2)
68
+ standard-custom (~> 1.0.0)
69
+ standard-performance (~> 1.0.1)
70
+ standard-custom (1.0.0)
71
+ lint_roller (~> 1.0)
72
+ standard-performance (1.0.1)
73
+ lint_roller (~> 1.0)
74
+ rubocop-performance (~> 1.16.0)
75
+ unicode-display_width (2.4.2)
76
+
77
+ PLATFORMS
78
+ arm64-darwin-21
79
+
80
+ DEPENDENCIES
81
+ elasticsearch (~> 8.7)
82
+ minitest (~> 5.0)
83
+ minitest-hooks (~> 1.5)
84
+ rake (~> 13.0)
85
+ standard (~> 1.3)
86
+ testcontainers-core!
87
+ testcontainers-elasticsearch!
88
+
89
+ BUNDLED WITH
90
+ 2.4.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Guillermo Iguaran
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,133 @@
1
+ # Testcontainers module for Elasticsearch
2
+
3
+ testcontainers-elasticsearch simplifies the creation and management of ElasticSearch containers for testing purposes using the Testcontainers library.
4
+
5
+ ## Installation
6
+
7
+ Add the library to the test section in your application's Gemfile:
8
+
9
+ ```ruby
10
+ group :test do
11
+ gem 'testcontainers-elasticsearch'
12
+ end
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```bash
18
+ $ bundle install
19
+ ```
20
+
21
+ Or install it yourself as:
22
+
23
+ ```bash
24
+ $ gem install testcontainers-elasticsearch
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ To use the library, you first need to require it:
30
+
31
+ ```ruby
32
+ require "testcontainers/elasticsearch"
33
+ ```
34
+
35
+ ### Creating an Elasticsearch container
36
+
37
+ Create a new instance of the `Testcontainers::ElasticsearchContainer` class:
38
+
39
+ ```ruby
40
+ container = Testcontainers::ElasticsearchContainer.new
41
+ ```
42
+
43
+ This creates a new container with the default Elasticsearch image. You can customize the image by passing an argument to the constructor:
44
+
45
+ ```ruby
46
+ container = Testcontainers::ElasticsearchContainer.new("elasticsearch:7.17.10")
47
+ ```
48
+
49
+ ### Starting and stopping the container
50
+
51
+ Start the container:
52
+
53
+ ```ruby
54
+ container.start
55
+ ```
56
+
57
+ Stop the container when you're done:
58
+
59
+ ```ruby
60
+ container.stop
61
+ ```
62
+
63
+ ### Connecting to the Elasticsearch container
64
+
65
+ Once the container is running, you can obtain the connection details using the following methods:
66
+
67
+ ```ruby
68
+ host = container.host
69
+ port = container.first_mapped_port
70
+ ```
71
+
72
+ Or, you can generate the full Elasticsearch URL:
73
+
74
+ ```ruby
75
+ elasticsearch_url = container.elasticsearch_url
76
+ ```
77
+
78
+ Use this URL to connect to the Elasticsearch container using your preferred Elasticsearch client library.
79
+
80
+ ### Example
81
+
82
+ Here's a complete example of how to use testcontainers-elasticsearch to create a container, connect to it, and perform a simple health check:
83
+
84
+ ```ruby
85
+ require "testcontainers/elasticsearch"
86
+ require "elasticsearch"
87
+
88
+ container = Testcontainers::ElasticsearchContainer.new
89
+ container.start
90
+
91
+ client = Elasticsearch::Client.new(url: ccontainer.elasticsearch_url)
92
+ client.ping #=> true
93
+
94
+ container.stop
95
+ ```
96
+
97
+ This example creates an Elasticsearch container, connects to it using the `elasticsearch` library, performs a simple ping, and then stops the container.
98
+
99
+
100
+ ### Using with RSpec
101
+
102
+ You can manage the container in the `before(:suite)` / `after(:suite)` blocks in your `spec_helper.rb`:
103
+
104
+ ```ruby
105
+ RSpec.configure do |config|
106
+ # This helps us to have access to the `RSpec.configuration.elasticsearch_container` without using global variables.
107
+ config.add_setting :elasticsearch_container, default: nil
108
+
109
+ config.before(:suite) do
110
+ config.elasticsearch_container = Testcontainers::ElasticsearchContainer.new.start
111
+ ENV["ELASTICSEARCH_URL"] = config.elasticsearch_container.elasticsearch_url
112
+ end
113
+
114
+ config.after(:suite) do
115
+ config.elasticsearch_container&.stop
116
+ config.elasticsearch_container&.remove
117
+ end
118
+ end
119
+ ```
120
+
121
+ This code starts an Elasticsearch container before the test suite runs and stops it after the suite finishes. The Elasticsearch URL is stored in an environment variable, so it's accessible to the tests.
122
+
123
+ ## Contributing
124
+
125
+ Bug reports and pull requests are welcome on GitHub at https://github.com/testcontainers/testcontainers-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/testcontainers/testcontainers-ruby/blob/main/CODE_OF_CONDUCT.md).
126
+
127
+ ## License
128
+
129
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
130
+
131
+ ## Code of Conduct
132
+
133
+ Everyone interacting in the Testcontainers project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/testcontainers/testcontainers-ruby/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "standard/rake"
13
+
14
+ task default: %i[test standard]
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Testcontainers
4
+ module Elasticsearch
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,89 @@
1
+ require_relative "elasticsearch/version"
2
+ require "testcontainers"
3
+
4
+ module Testcontainers
5
+ # ElasticsearchContainer class is used to manage Docker containers running Elasticsearch.
6
+ # It extends the generic DockerContainer class and provides Elasticsearch-specific functionality.
7
+ #
8
+ # @attr_reader username [String] the Elasticsearch username
9
+ # @attr_reader password [String] the Elasticsearch password
10
+ class ElasticsearchContainer < ::Testcontainers::DockerContainer
11
+ ELASTICSEARCH_DEFAULT_HTTP_PORT = 9200
12
+ ELASTICSEARCH_DEFAULT_TCP_PORT = 9300
13
+ ELASTICSEARCH_DEFAULT_IMAGE = "docker.elastic.co/elasticsearch/elasticsearch:8.7.1"
14
+ ELASTICSEARCH_DEFAULT_USERNAME = "elastic"
15
+ ELASTICSEARCH_DEFAULT_PASSWORD = "elastic"
16
+
17
+ attr_reader :username, :password
18
+
19
+ # Initializes a new ElasticsearchContainer instance.
20
+ #
21
+ # @param image [String] the Elasticsearch Docker image to use
22
+ # @param http_port [Integer] the port to expose for HTTP traffic, defaults to 9200
23
+ # @param tcp_port [Integer] the port to expose for TCP traffic, defaults to 9300
24
+ # @param username [String] the Elasticsearch username, defaults to 'elastic'
25
+ # @param password [String] the Elasticsearch password, defaults to 'elastic'
26
+ # @param kwargs [Hash] the options to pass to the container. See {DockerContainer#initialize}
27
+ # @return [ElasticsearchContainer] a new ElasticsearchContainer instance
28
+ def initialize(image = ELASTICSEARCH_DEFAULT_IMAGE, http_port: nil, tcp_port: nil, username: nil, password: nil, **kwargs)
29
+ super(image, **kwargs)
30
+ @username = username || ELASTICSEARCH_DEFAULT_USERNAME
31
+ @password = password || ELASTICSEARCH_DEFAULT_PASSWORD
32
+ @healthcheck ||= add_healthcheck(_default_healthcheck_options)
33
+ @wait_for ||= add_wait_for(:healthcheck)
34
+ end
35
+
36
+ # Starts the container
37
+ #
38
+ # @return [ElasticsearchContainer] self
39
+ def start
40
+ with_exposed_ports(http_port, tcp_port)
41
+ _configure
42
+ super
43
+ end
44
+
45
+ # Returns the HTTP port used by the container
46
+ #
47
+ # @return [Integer] the port used by the container
48
+ def http_port
49
+ ELASTICSEARCH_DEFAULT_HTTP_PORT
50
+ end
51
+
52
+ # Returns the TCP (transport) port used by the container
53
+ #
54
+ # @return [Integer] the port used by the container
55
+ def tcp_port
56
+ ELASTICSEARCH_DEFAULT_TCP_PORT
57
+ end
58
+
59
+ # Returns the URL to access Elasticsearch
60
+ #
61
+ # @param protocol [String] the protocol (http or https), defaults to 'http'
62
+ # @param port [Integer] the port to use, defaults to the HTTP port
63
+ # @param username [String] the username to use, defaults to the container username
64
+ # @param password [String] the password to use, defaults to the container password
65
+ # @return [String] the URL to access Elasticsearch
66
+ def elasticsearch_url(protocol: nil, port: nil, username: nil, password: nil)
67
+ if protocol.nil?
68
+ protocol = (get_env("xpack.security.enabled") == "true") ? "https" : "http"
69
+ end
70
+ username ||= @username
71
+ password ||= @password
72
+ port ||= http_port
73
+
74
+ "#{protocol}://#{username}:#{password}@#{host}:#{mapped_port(port)}"
75
+ end
76
+
77
+ private
78
+
79
+ def _configure
80
+ add_env("ELASTIC_PASSWORD", @password.to_s)
81
+ add_env("discovery.type", "single-node") unless get_env("discovery.type")
82
+ add_env("xpack.security.enabled", "false") unless get_env("xpack.security.enabled")
83
+ end
84
+
85
+ def _default_healthcheck_options
86
+ {test: ["curl", "--silent", "--fail", "localhost:#{http_port}/_cluster/health"], interval: 1, timeout: 5, retries: 5}
87
+ end
88
+ end
89
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: testcontainers-elasticsearch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Guillermo Iguaran
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-06-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: testcontainers-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-hooks
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: standard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: elasticsearch
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '8.7'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '8.7'
97
+ description: Testcontainers makes it easy to create and clean up container-based dependencies
98
+ for automated tests.
99
+ email:
100
+ - guilleiguaran@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - CHANGELOG.md
106
+ - Gemfile
107
+ - Gemfile.lock
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - lib/testcontainers/elasticsearch.rb
112
+ - lib/testcontainers/elasticsearch/version.rb
113
+ homepage: https://github.com/testcontainers/testcontainers-ruby
114
+ licenses:
115
+ - MIT
116
+ metadata:
117
+ homepage_uri: https://github.com/testcontainers/testcontainers-ruby/blob/main/elasticsearch
118
+ source_code_uri: https://github.com/testcontainers/testcontainers-ruby/blob/main/elasticsearch
119
+ changelog_uri: https://github.com/testcontainers/testcontainers-ruby/blob/main/elasticsearch/CHANGELOG.md
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: 2.6.0
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubygems_version: 3.4.1
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: 'Testcontainers for Ruby: Elasticsearch module'
139
+ test_files: []