Skip to content

Commit

Permalink
Make config.yml path configurable via environment; add ConfigService.…
Browse files Browse the repository at this point in the history
…database_config_from_file; add first draft of GitHub Action workflow artifacts
  • Loading branch information
ssciolla committed Apr 1, 2024
1 parent 6f59d12 commit 2df5c43
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 16 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Run Tests

on: push

jobs:
# Run tests
test:
runs-on: ubuntu-latest
services:
mariadb:
image: mariadb:11
env:
MARIADB_USER: user
MARIADB_PASSWORD: password
MARIADB_DATABASE: test_database
MARIADB_ROOT_PASSWORD: password
ports: ["3306:3306"]
options: --health-cmd="mysqladmin ping" --health-interval=5s --health-timeout=2s --health-retries=3
steps:
- uses: actions/checkout@v3
- name: Wait for MariaDB
run: |
while ! mysqladmin ping -h"127.0.0.1" -P"3306" --silent; do
sleep 1
done
- name: Set up Ruby 3.2
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.2'
bundler-cache: true
- name: Run tests
run: bundle exec rake test
env:
CONFIG_PATH_YAML: "./config/config.tests.yml"
5 changes: 2 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ namespace :db do

Sequel.extension :migration

config = Config::ConfigService.from_file(
File.join(".", "config", "config.yml")
db_config = Config::ConfigService.database_config_from_file(
ENV.fetch("CONFIG_YML_PATH", File.join(".", "config", "config.yml"))
)
db_config = config.database
if !db_config
raise DatabaseError, "Migration failed. A database connection is not configured."
end
Expand Down
8 changes: 8 additions & 0 deletions config/config.tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Configuration for GitHub Action defined in .github/workflows/tests.yaml

Database:
Host: 127.0.0.1
Database: test_database
Port: 3306
User: user
Password: password
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ services:
target: /run/host-services/ssh-auth.sock
environment:
- SSH_AUTH_SOCK=/run/host-services/ssh-auth.sock
- CONFIG_YML_PATH=./config/config.tests.yml
depends_on:
database:
condition: "service_healthy"
Expand Down
24 changes: 17 additions & 7 deletions lib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ def self.create_remote_config(data)
)
end

def self.create_database_config(data)
DatabaseConfig.new(
host: verify_string("Host", data["Host"]),
database: verify_string("Database", data["Database"]),
port: verify_int("Port", data["Port"]),
user: verify_string("User", data["User"]),
password: verify_string("Password", data["Password"])
)
end

def self.create_config(data)
logger.debug(data)
db_data = data.fetch("Database", nil)
Expand All @@ -197,13 +207,7 @@ def self.create_config(data)
name: verify_string("Repository", data["Repository"]),
description: verify_string("RepositoryDescription", data["RepositoryDescription"])
),
database: db_data && DatabaseConfig.new(
host: verify_string("Host", db_data["Host"]),
database: verify_string("Database", db_data["Database"]),
port: verify_int("Port", db_data["Port"]),
user: verify_string("User", db_data["User"]),
password: verify_string("Password", db_data["Password"])
),
database: db_data && create_database_config(db_data),
dark_blue: DarkBlueConfig.new(
archivematicas: (
data["DarkBlue"]["ArchivematicaInstances"].map do |arch_data|
Expand Down Expand Up @@ -234,6 +238,12 @@ def self.create_config(data)
)
end

def self.database_config_from_file(yaml_path)
data = read_data_from_file(yaml_path)
db_data = data.fetch("Database", nil)
db_data && create_database_config(db_data)
end

def self.from_file(yaml_path)
create_config(read_data_from_file(yaml_path))
end
Expand Down
4 changes: 3 additions & 1 deletion run_dark_blue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
require_relative "lib/config"

SemanticLogger.add_appender(io: $stderr, formatter: :color)
config = Config::ConfigService.from_file(File.join(".", "config", "config.yml"))
config = Config::ConfigService.from_file(
ENV.fetch("CONFIG_YML_PATH", File.join(".", "config", "config.yml"))
)
SemanticLogger.default_level = config.settings.log_level

DB = config.database && Sequel.connect(
Expand Down
4 changes: 3 additions & 1 deletion run_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

SemanticLogger.add_appender(io: $stderr, formatter: :color)

config = Config::ConfigService.from_file(File.join(".", "config", "config.yml"))
config = Config::ConfigService.from_file(
ENV.fetch("CONFIG_YML_PATH", File.join(".", "config", "config.yml"))
)

SemanticLogger.default_level = config.settings.log_level
logger = SemanticLogger["run_example"]
Expand Down
6 changes: 3 additions & 3 deletions test/setup_db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

Sequel.extension :migration

config = Config::ConfigService.from_file(
File.join(__dir__, "..", "config", "config.yml")
config_yml_path = ENV.fetch("CONFIG_YML_PATH", File.join(".", "config", "config.yml"))
db_config = Config::ConfigService.database_config_from_file(
File.join(__dir__, "..", config_yml_path)
)

db_config = config.database
if !db_config
message = "A database connection is not configured. This is required for some tests."
raise DatabaseError, message
Expand Down
4 changes: 3 additions & 1 deletion verify_aptrust.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
require_relative "lib/config"

SemanticLogger.add_appender(io: $stderr, formatter: :color)
config = Config::ConfigService.from_file(File.join(".", "config", "config.yml"))
config = Config::ConfigService.from_file(
ENV.fetch("CONFIG_YML_PATH", File.join(".", "config", "config.yml"))
)
SemanticLogger.default_level = config.settings.log_level

DB = config.database && Sequel.connect(
Expand Down

0 comments on commit 2df5c43

Please sign in to comment.