testcontainers-mariadb 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: ddda1e688d370cee4be396c175b28c4943ffcc29e9bc9279826c5dafb29f5013
4
+ data.tar.gz: 35acaa90e1cbbdbd0bdbc68b77d0e60e5d0fe09f6c8f266b4a6421b1526e65eb
5
+ SHA512:
6
+ metadata.gz: 84f999615a260a04a663eee1e0f1ecab3ab22d60a65211565fd0c98bd93ff76bbd422d77749b5f972e6567c67a074b13ccedc5f8c89cb6416416ea8b5f74fa71
7
+ data.tar.gz: d890faef3913f657a509442b0a5147a28358ca14265d46c8510ea0d2d011dbfd0806a73e332801bd79fea32eb82b026ab1a135d0795cc0a47369ef1732888917
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [0.1.0] - 2023-05-13
2
+
3
+ ### Added
4
+
5
+ - Initial release of the MariaDB 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-mariadb.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,78 @@
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-mariadb (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
+ excon (0.99.0)
21
+ json (2.6.3)
22
+ language_server-protocol (3.17.0.3)
23
+ lint_roller (1.0.0)
24
+ minitest (5.18.0)
25
+ minitest-hooks (1.5.0)
26
+ minitest (> 5.3)
27
+ multi_json (1.15.0)
28
+ mysql2 (0.5.5)
29
+ parallel (1.23.0)
30
+ parser (3.2.2.1)
31
+ ast (~> 2.4.1)
32
+ rainbow (3.1.1)
33
+ rake (13.0.6)
34
+ regexp_parser (2.8.0)
35
+ rexml (3.2.5)
36
+ rubocop (1.50.2)
37
+ json (~> 2.3)
38
+ parallel (~> 1.10)
39
+ parser (>= 3.2.0.0)
40
+ rainbow (>= 2.2.2, < 4.0)
41
+ regexp_parser (>= 1.8, < 3.0)
42
+ rexml (>= 3.2.5, < 4.0)
43
+ rubocop-ast (>= 1.28.0, < 2.0)
44
+ ruby-progressbar (~> 1.7)
45
+ unicode-display_width (>= 2.4.0, < 3.0)
46
+ rubocop-ast (1.28.1)
47
+ parser (>= 3.2.1.0)
48
+ rubocop-performance (1.16.0)
49
+ rubocop (>= 1.7.0, < 2.0)
50
+ rubocop-ast (>= 0.4.0)
51
+ ruby-progressbar (1.13.0)
52
+ standard (1.28.2)
53
+ language_server-protocol (~> 3.17.0.2)
54
+ lint_roller (~> 1.0)
55
+ rubocop (~> 1.50.2)
56
+ standard-custom (~> 1.0.0)
57
+ standard-performance (~> 1.0.1)
58
+ standard-custom (1.0.0)
59
+ lint_roller (~> 1.0)
60
+ standard-performance (1.0.1)
61
+ lint_roller (~> 1.0)
62
+ rubocop-performance (~> 1.16.0)
63
+ unicode-display_width (2.4.2)
64
+
65
+ PLATFORMS
66
+ arm64-darwin-21
67
+
68
+ DEPENDENCIES
69
+ minitest (~> 5.0)
70
+ minitest-hooks (~> 1.5)
71
+ mysql2 (~> 0.5.0)
72
+ rake (~> 13.0)
73
+ standard (~> 1.3)
74
+ testcontainers-core!
75
+ testcontainers-mariadb!
76
+
77
+ BUNDLED WITH
78
+ 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,147 @@
1
+ # Testcontainers module for MariaDB
2
+
3
+ ## Installation
4
+
5
+ Add the library to the test section in your application's Gemfile:
6
+
7
+ ```ruby
8
+ group :test do
9
+ gem "testcontainers-mariadb"
10
+ end
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ $ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```bash
22
+ $ gem install testcontainers-mariadb
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ To use the library, you first need to require it:
28
+
29
+ ```ruby
30
+ require "testcontainers/mariadb"
31
+ ```
32
+
33
+ ### Creating a MariaDB container
34
+
35
+ Create a new instance of the `Testcontainers::MariadbContainer` class:
36
+
37
+ ```ruby
38
+ container = Testcontainers::MariadbContainer.new
39
+ ```
40
+
41
+
42
+ This creates a new container with the default MariaDB image, user, password, and database. You can customize these by passing arguments to the constructor:
43
+
44
+ ```ruby
45
+ container = Testcontainers::MariadbContainer.new("mariadb:10.10", username: "custom_user", password: "custom_pass", database: "custom_db")
46
+ ```
47
+
48
+
49
+ ### Starting and stopping the container
50
+
51
+ Start the container:
52
+
53
+ ```ruby
54
+ container.start
55
+ ```
56
+
57
+
58
+ Stop the container when you're done:
59
+
60
+ ```ruby
61
+ container.stop
62
+ ```
63
+
64
+ ### Connecting to the MariaDB container
65
+
66
+ Once the container is running, you can obtain the connection details using the following methods:
67
+
68
+ ```ruby
69
+ host = container.host
70
+ port = container.first_mapped_port
71
+ ```
72
+
73
+
74
+ Or, you can generate a full database URL:
75
+
76
+ ```ruby
77
+ database_url = container.database_url
78
+ ```
79
+
80
+ Use this URL to connect to the MariaDB container using your preferred MySQL client library.
81
+
82
+ ### Customizing the container
83
+
84
+ You can also customize the container using the following methods:
85
+
86
+ ```ruby
87
+ container.with_database("custom_db")
88
+ container.with_username("custom_user")
89
+ container.with_password("custom_pass")
90
+ ```
91
+
92
+ ### Example
93
+
94
+ Here's a complete example of how to use testcontainers-mysdql to create a container, connect to it, and run a simple query:
95
+
96
+ ```ruby
97
+ require "testcontainers/mariadb"
98
+ require "mysql2"
99
+
100
+ container = Testcontainers::MariadbContainer.new
101
+ container.start
102
+
103
+ client = Mysql2::Client.new(url: container.database_url)
104
+
105
+ result = client.query("SELECT 1")
106
+ result.each do |row|
107
+ puts row.inspect
108
+ end
109
+
110
+ client.close
111
+ container.stop
112
+ ```
113
+
114
+ This example creates a MariaDB container, connects to it using the `mysql2` gem, runs a simple `SELECT 1` query, and then stops the container.
115
+
116
+ ### Using with RSpec
117
+
118
+ You can manage the container in the `before(:suite)` / `after(:suite)` blocks in your `spec_helper.rb`:
119
+
120
+ ```ruby
121
+ RSpec.configure do |config|
122
+ # This helps us to have access to the `RSpec.configuration.mariadb_container` without using global variables.
123
+ config.add_setting :mariadb_container, default: nil
124
+
125
+ config.before(:suite) do
126
+ config.mariadb_container = Testcontainers::MariadbContainer.new.start
127
+ ENV["DATABASE_URL"] = config.mariadb_container.database_url(protocol: "mysql2") # or you can expose it to a fixed port and use database.yml for configuration
128
+ end
129
+
130
+ config.after(:suite) do
131
+ config.mariadb_container&.stop
132
+ config.mariadb_container&.remove
133
+ end
134
+ end
135
+ ```
136
+
137
+ ## Contributing
138
+
139
+ 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).
140
+
141
+ ## License
142
+
143
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
144
+
145
+ ## Code of Conduct
146
+
147
+ 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 Mariadb
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,132 @@
1
+ require_relative "mariadb/version"
2
+ require "testcontainers"
3
+
4
+ module Testcontainers
5
+ # MariadbContainer class is used to manage containers that runs a MariaDB database
6
+ #
7
+ # @attr_reader [String] username used by the container
8
+ # @attr_reader [String] password used by the container
9
+ # @attr_reader [String] database used by the container
10
+ class MariadbContainer < ::Testcontainers::DockerContainer
11
+ # Default port used by the container
12
+ MARIADB_DEFAULT_PORT = 3306
13
+
14
+ # Default image used by the container
15
+ MARIADB_DEFAULT_IMAGE = "mariadb:latest"
16
+
17
+ MARIADB_DEFAULT_USERNAME = "test"
18
+ MARIADB_DEFAULT_PASSWORD = "test"
19
+ MARIADB_DEFAULT_ROOT_USERNAME = "root"
20
+ MARIADB_DEFAULT_DATABASE = "test"
21
+
22
+ attr_reader :username, :password, :database
23
+
24
+ # Initializes a new instance of MariadbContainer
25
+ #
26
+ # @param image [String] the image to use
27
+ # @param username [String] the username to use
28
+ # @param password [String] the password to use
29
+ # @param database [String] the database to use
30
+ # @param port [String] the port to use
31
+ # @param kwargs [Hash] the options to pass to the container. See {DockerContainer#initialize}
32
+ # @return [MariadbContainer] a new instance of MariadbContainer
33
+ def initialize(image = MARIADB_DEFAULT_IMAGE, username: nil, password: nil, database: nil, port: nil, **kwargs)
34
+ super(image, **kwargs)
35
+ @username = username || ENV.fetch("MARIADB_USER", MARIADB_DEFAULT_USERNAME)
36
+ @password = password || ENV.fetch("MARIADB_PASSWORD", MARIADB_DEFAULT_PASSWORD)
37
+ @database = database || ENV.fetch("MARIADB_DATABASE", MARIADB_DEFAULT_DATABASE)
38
+ @healthcheck ||= add_healthcheck(_default_healthcheck_options)
39
+ @wait_for ||= add_wait_for(:healthcheck)
40
+ end
41
+
42
+ # Starts the container
43
+ #
44
+ # @return [MariadbContainer] self
45
+ def start
46
+ with_exposed_ports(port)
47
+ _configure
48
+ super
49
+ end
50
+
51
+ # Returns the host used to connect to the container
52
+ # If the host is "localhost", it is replaced by "127.0.0.1" since MariaDB fallbacks
53
+ # to a socket connection with "localhost"
54
+ #
55
+ # @return [String] the host used to connect to the container
56
+ def host
57
+ host = super
58
+ (host == "localhost") ? "127.0.0.1" : host
59
+ end
60
+
61
+ # Returns the port used by the container
62
+ #
63
+ # @return [Integer] the port used by the container
64
+ def port
65
+ MARIADB_DEFAULT_PORT
66
+ end
67
+
68
+ # Returns the database url (e.g. mariadb://user:password@host:port/database)
69
+ #
70
+ # @param protocol [String] the protocol to use in the string (default: "mariadb")
71
+ # @param database [String] the database to use in the string (default: @database)
72
+ # @param options [Hash] the options to use in the query string (default: {})
73
+ # @return [String] the database url
74
+ # @raise [ConnectionError] If the connection to the Docker daemon fails.
75
+ # @raise [ContainerNotStartedError] If the container has not been started.
76
+ def database_url(protocol: "mariadb", username: nil, password: nil, database: nil, options: {})
77
+ database ||= @database
78
+ username ||= @username
79
+ password ||= @password
80
+ query_string = options.empty? ? "" : "?#{URI.encode_www_form(options)}"
81
+
82
+ "#{protocol}://#{username}:#{password}@#{host}:#{mapped_port(port)}/#{database}#{query_string}"
83
+ end
84
+
85
+ # Sets the database to use
86
+ #
87
+ # @param database [String] the database to use
88
+ # @return [MariadbContainer] self
89
+ def with_database(database)
90
+ @database = database
91
+ self
92
+ end
93
+
94
+ # Sets the username to use
95
+ #
96
+ # @param username [String] the username to use
97
+ # @return [MariadbContainer] self
98
+ def with_username(username)
99
+ @username = username
100
+ self
101
+ end
102
+
103
+ # Sets the password to use
104
+ #
105
+ # @param password [String] the password to use
106
+ # @return [MariadbContainer] self
107
+ def with_password(password)
108
+ @password = password
109
+ self
110
+ end
111
+
112
+ private
113
+
114
+ def _configure
115
+ add_env("MARIADB_DATABASE", @database)
116
+ add_env("MARIADB_USER", @username) if @username != MARIADB_DEFAULT_ROOT_USERNAME
117
+
118
+ if !@password.nil? && !@password.empty?
119
+ add_env("MARIADB_PASSWORD", @password)
120
+ add_env("MARIADB_ROOT_PASSWORD", @password)
121
+ elsif @username == MARIADB_DEFAULT_ROOT_USERNAME
122
+ add_env("MARIADB_ALLOW_EMPTY_PASSWORD", "yes")
123
+ else
124
+ raise ContainerLaunchException, "Password is required for non-root users"
125
+ end
126
+ end
127
+
128
+ def _default_healthcheck_options
129
+ {test: ["/usr/bin/mysql", "--protocol=TCP", "--port=#{port}", "--user=#{username}", "--password=#{password}", database, "--silent", "--execute=SELECT 1;"], interval: 1, timeout: 5, retries: 5}
130
+ end
131
+ end
132
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: testcontainers-mariadb
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: mysql2
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.5.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.5.0
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/mariadb.rb
112
+ - lib/testcontainers/mariadb/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/mariadb
118
+ source_code_uri: https://github.com/testcontainers/testcontainers-ruby/blob/main/mariadb
119
+ changelog_uri: https://github.com/testcontainers/testcontainers-ruby/blob/main/mariadb/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: Mariadb module'
139
+ test_files: []