Skip to content

Commit

Permalink
Add fix to Readme file
Browse files Browse the repository at this point in the history
  • Loading branch information
PRKmilo committed Aug 23, 2023
1 parent 97b3774 commit dc0b946
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 78 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/module-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: "Modules: Compose tests"

on:
push:
branches:
- main

pull_request:

jobs:
build:
runs-on: ubuntu-latest
name: Ruby ${{ matrix.ruby }}
strategy:
matrix:
ruby:
- '3.2.0'

steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Install development dependencies
working-directory: ./compose
run: bundle install
- name: Run the tests
working-directory: ./compose
run: bundle exec rake test
- name: Run standard code style checks
working-directory: ./compose
run: bundle exec rake standard
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ gem "testcontainers-mongo", path: "./mongo"
gem "testcontainers-redpanda", path: "./redpanda"
gem "testcontainers-rabbitmq", path: "./rabbitmq"
gem "testcontainers-selenium", path: "./selenium"
gem "testcontainers-compose", path: "./compose"
gem "mysql2", "~> 0.5.3"
gem "pg", "~> 1.5"
gem "redis", "~> 5.0"
Expand Down
8 changes: 7 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ PATH
testcontainers (0.1.3)
testcontainers-core (= 0.1.3)

PATH
remote: compose
specs:
testcontainers-compose (0.1.0)
testcontainers-core (~> 0.1.2)

PATH
remote: core
specs:
Expand Down Expand Up @@ -80,7 +86,6 @@ GEM
amq-protocol (~> 2.3, >= 2.3.1)
sorted_set (~> 1, >= 1.0.2)
childprocess (4.1.0)
coderay (1.1.3)
connection_pool (2.4.0)
docker-api (2.2.0)
excon (>= 0.47.0)
Expand Down Expand Up @@ -186,6 +191,7 @@ DEPENDENCIES
selenium-webdriver (~> 4.1.0)
standard (~> 1.3)
testcontainers!
testcontainers-compose!
testcontainers-core!
testcontainers-elasticsearch!
testcontainers-mariadb!
Expand Down
2 changes: 0 additions & 2 deletions compose/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ gemspec

# Use the latest version of testcontainers-core from the local path
gem "testcontainers-core", path: "../core"

gem "pry-rails"
29 changes: 15 additions & 14 deletions compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Create a new instance of the `Testcontainers::ComposeContainer` class:
``` ruby
compose = Testcontainer::ComposeContainer.new(filepath: Dir.getwd)
```
The instance creates a set of containers defined on the .yml file, the 'container.start' wakes up all containers as service
The instance creates a set of containers defined on the .yml file, the 'compose.start' wakes up all containers as service

Start the services of compose

Expand All @@ -49,46 +49,47 @@ compose.stop
```
### Connecting to services from the compose

Once the service is running , tu can obtain the connecion details using the following methods
Once the service is running , tu can obtain the connecion details using the fol lowing methods

```ruby
compose.process_information(service: "hub", port: 4444)
```
This function gonna show the logs of the process
This function will show the logs of the process

```ruby
compose.logs
```
This function make a request for the url nested for the service from compose

To wait for a service running on a url you should use 'wait_for_request':

```ruby
compose.wait_for_request(url: )
compose.wait_for_request(url: "http://localhost:4444/hub")
```

### Configuration of services
In this example we re gonna make a container with two services that use the yml files in the current path

next example initialize compose with two services described in the YML file located in the current directory

```ruby
services = ["hub","firefox"]
container = Testcontainers::ComposeContainer.new(filepath: Dir.getwd, services: services)
compose = Testcontainers::ComposeContainer.new(filepath: Dir.getwd, services: services)
```

In this example we re gonna make a continer by specific .yml files
In this example we ll make a continer by specific .yml files

```ruby
compose_file_name = ["docker-compose2.yml", "docker-compose2.yml"]
compose = Testcontainer::ComposeContainer.new(filepath: Dir.getwd, compose_file_name: compose_file_name)
compose_filename = ["docker-compose2.yml", "docker-compose3.yml"]
compose = Testcontainer::ComposeContainer.new(filepath: Dir.getwd, compose_filename: compose_filename)
compose.start


```
In this example we re gonna make a container with a enviorement variables configuration file

You can overwrite an enviorement file as next:

```ruby
compose_file_name = ["docker-compose3.yml"]
compose = Testcontainers::ComposeContainer.new(filepath: TEST_PATH, compose_file_name: compose_file_name, env_file: ".env.test")
TEST_PATH = "#{Dir.getwd}/test"
compose_filename = ["docker-compose3.yml"]
compose = Testcontainers::ComposeContainer.new(filepath: TEST_PATH, compose_filename: compose_filename, env_file: ".env.test")
```

### Send commands for the compose service
Expand Down
44 changes: 30 additions & 14 deletions compose/lib/testcontainers/compose.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
require_relative "compose/version"
require "testcontainers"
require "open3"
require "pry"
require "net/http"
require "json"
require "uri"
module Testcontainers
# ComposeContainer class is used to manage a large number of containers in a synchronous environment
#
# @attr_accesor [String] filepath used by the container
# @attr_accesor [String,List] compose_file_name used by the container
# @attr_accesor [String,List] compose_filename used by the container
# @attr_accesor [Boolean] pull used by the container
# @attr_accesor [Boolean] build used by the container
# @attr_accesor [List] services used by the container
class ComposeContainer
# Default image used by the container

attr_accessor :filepath, :compose_file_name, :pull, :build, :env_file, :services
attr_accessor :filepath, :compose_filename, :pull, :build, :env_file, :services

# Initializes a new instance of ComposeContainer
#
# @param image [String] the image to use
# @param filepath [String] the filepath of the configuration files for the configuration of docker compose
# @param compose_file_name [String, List] the names of the files with yml extencion for custom configuration
# @param compose_filename [String, List] the names of the files with yml extencion for custom configuration
# @param pull [Boolean] is the option for decide if there should be a pull request to generate the image for the containers
# @param build [Boolean] is the option for decide if there have to use a build command for the images used for the containers
# @param env_file [String] is the name of the envieroment configuration
# @param services [List] are the names of the services that gonna use in the images of the containers
def initialize(filepath: ".", compose_file_name: ["docker-compose.yml"], pull: false, build: false, env_file: nil, services: nil, **kwargs)
def initialize(filepath: ".", compose_filename: ["docker-compose.yml"], pull: false, build: false, env_file: nil, services: nil, **kwargs)
@filepath = filepath
@compose_file_names = compose_file_name
@compose_filenames = compose_filename
@pull = pull
@build = build
@services = services
Expand All @@ -40,7 +39,7 @@ def initialize(filepath: ".", compose_file_name: ["docker-compose.yml"], pull: f
# @return [docker_compose_cmd]
def with_command
docker_compose_cmd = ["docker compose"]
@compose_file_names.each do |file|
@compose_filenames.each do |file|
docker_compose_cmd += ["-f #{file}"]
end
if env_file.nil? == false
Expand Down Expand Up @@ -116,13 +115,30 @@ def process_information(service: nil, port: 0)
# Return the response of generate a request to a url to wich is located in our Docker s service and make a sleep for wait the server complete the build correctly
# @params url [String]
# @return response [Http]
def wait_for_request(url: nil)
sleep 3
url = URI.parse(url)
http = Net::HTTP.new(url.host, url.port)
http.read_timeout = 5
request = Net::HTTP::Get.new(url)
http.request(request)
def wait_for_request(url: nil, attemps_url: 2, timeout: 60, read_timeout: 3)
max_attemps_url = attemps_url
send = 0
Timeout.timeout(timeout) do
loop do
url = URI.parse(url)
http = Net::HTTP.new(url.host, url.port)
http.read_timeout = read_timeout
request = Net::HTTP::Get.new(url)
response = http.request(request)
return true if response.code == "200"
end
rescue Errno::ECONNREFUSED
sleep 1
retry
rescue URI::InvalidURIError
send += 1
sleep 1
if send <= max_attemps_url
retry
end
end
rescue Timeout::Error
raise TimeoutError, "Timed out waiting fot logs "
end
end
end
Loading

0 comments on commit dc0b946

Please sign in to comment.