baktainer/app/spec/spec_helper.rb
James Paterni 8db5004eea
Some checks are pending
Test and Build Docker Image / test (push) Waiting to run
Test and Build Docker Image / build (push) Blocked by required conditions
Add comprehensive RSpec testing infrastructure and enhance CI/CD pipeline
- Implement complete test suite with 63 examples (49 unit + 14 integration tests)
- Add RSpec, FactoryBot, WebMock, and SimpleCov testing dependencies
- Create mocked integration tests eliminating need for real Docker containers
- Fix SQLite method signature to accept login/password parameters
- Enhance container discovery to handle nil labels gracefully
- Add test coverage reporting and JUnit XML output for CI
- Update GitHub Actions workflow to run tests before Docker builds
- Add Ruby 3.3 setup with gem caching for faster CI execution
- Create CI test script and comprehensive testing documentation
- Ensure Docker builds only proceed when all tests pass

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-13 23:12:59 -04:00

128 lines
No EOL
3.4 KiB
Ruby

# frozen_string_literal: true
# Load coverage if enabled
require_relative 'support/coverage' if ENV['COVERAGE']
require 'rspec'
require 'docker-api'
require 'webmock/rspec'
require 'factory_bot'
# Add lib directory to load path
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
# Require the main application files
require 'baktainer'
require 'baktainer/logger'
require 'baktainer/container'
require 'baktainer/backup_command'
# Configure RSpec
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.shared_context_metadata_behavior = :apply_to_host_groups
config.filter_run_when_matching :focus
config.example_status_persistence_file_path = 'spec/examples.txt'
config.disable_monkey_patching!
config.warnings = true
# Configure FactoryBot
config.include FactoryBot::Syntax::Methods
config.before(:suite) do
FactoryBot.definition_file_paths = [File.expand_path('fixtures', __dir__)]
FactoryBot.find_definitions
end
# Configure WebMock based on test type
config.before(:each) do |example|
if example.metadata[:integration]
# Allow localhost connections for integration tests
WebMock.disable_net_connect!(allow_localhost: true, allow: ['127.0.0.1', 'localhost'])
else
# Completely disable network connections for unit tests
WebMock.disable_net_connect!(allow_localhost: false)
end
end
# Clean up test environment
config.before(:each) do
# Reset environment variables
ENV.delete('BT_DOCKER_URL')
ENV.delete('BT_SSL')
ENV.delete('BT_CRON')
ENV.delete('BT_THREADS')
ENV.delete('BT_LOG_LEVEL')
ENV.delete('BT_BACKUP_DIR')
# Clear Docker configuration and set to localhost for tests
Docker.reset_connection!
Docker.url = 'unix:///var/run/docker.sock'
end
config.after(:each) do
# Clean up any test files
FileUtils.rm_rf(Dir.glob('/tmp/baktainer_test_*'))
end
end
# Test helper methods
module BaktainerTestHelpers
def mock_docker_container(labels = {})
container_info = {
'Id' => '1234567890abcdef',
'Names' => ['/test-container'],
'State' => { 'Status' => 'running' },
'Labels' => {
'baktainer.backup' => 'true',
'baktainer.db.engine' => 'postgres',
'baktainer.db.name' => 'testdb',
'baktainer.db.user' => 'testuser',
'baktainer.db.password' => 'testpass'
}.merge(labels || {})
}
container = double('Docker::Container')
allow(container).to receive(:info).and_return(container_info)
allow(container).to receive(:id).and_return(container_info['Id'])
allow(container).to receive(:exec) do |cmd, env: nil, &block|
block.call(:stdout, 'test backup data') if block
end
container
end
def create_test_backup_dir
test_dir = "/tmp/baktainer_test_#{Time.now.to_i}"
FileUtils.mkdir_p(test_dir)
test_dir
end
def with_env(env_vars)
original_env = {}
env_vars.each do |key, value|
original_env[key] = ENV[key]
ENV[key] = value
end
yield
ensure
original_env.each do |key, value|
if value.nil?
ENV.delete(key)
else
ENV[key] = value
end
end
end
end
RSpec.configure do |config|
config.include BaktainerTestHelpers
end