Skip to content

Commit

Permalink
Integrate Minitest reports
Browse files Browse the repository at this point in the history
  • Loading branch information
buehlmann committed Nov 13, 2024
1 parent 7525311 commit e8f2310
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 53 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/dagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ jobs:
module: .
# Arguments to pass to CLI
args: ci-integration --dir=./
# - name: Test Reporter
# uses: dorny/[email protected]
# with:
# name: JEST Tests # Name of the check run which will be created
# path: reports/jest-*.xml # Path to test results
# reporter: jest-junit # Format of test results
- name: Test Reporter
uses: dorny/[email protected]
with:
name: Minitests
path: test/reports/*.xml
reporter: java-junit
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,7 @@ group :test do
gem 'selenium-webdriver'
gem 'webdrivers'
gem 'webmock'
gem 'simplecov'
gem 'simplecov-rcov'
gem 'minitest'
end
79 changes: 45 additions & 34 deletions ci/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type Results struct {
VulnerabilityScan *dagger.File
// the built image
Image *dagger.Container
// the test reports
TestReports *dagger.Directory
}

// Returns a Container built from the Dockerfile in the provided Directory
Expand Down Expand Up @@ -87,38 +89,6 @@ func (m *Ci) Memcached(
AsService()
}

// Executes the test suite for the Rails application in the provided Directory
func (m *Ci) Test(ctx context.Context, dir *dagger.Directory) *dagger.Container {

return dag.Container().
From("registry.puzzle.ch/docker.io/ruby:3.2").
WithExec([]string{"useradd", "-m", "-u", "1001", "ruby"}).
WithServiceBinding("postgresql", m.Postgres(ctx, "11")).
WithServiceBinding("memcached", m.Memcached(ctx, "latest")).
WithMountedDirectory("/mnt", dir, dagger.ContainerWithMountedDirectoryOpts{Owner: "ruby"}).
WithWorkdir("/mnt").
WithEnvVariable("RAILS_DB_HOST", "postgresql"). // This is the service name of the postgres container called by rails
WithEnvVariable("RAILS_TEST_DB_HOST", "postgresql").
WithEnvVariable("RAILS_TEST_DB_NAME", "postgres").
WithEnvVariable("RAILS_TEST_DB_USERNAME", "postgres").
WithEnvVariable("RAILS_TEST_DB_PASSWORD", "postgres").
WithEnvVariable("RAILS_ENV", "test").
WithEnvVariable("CI", "true").
WithEnvVariable("PGDATESTYLE", "German").
WithEnvVariable("TZ", "Europe/Zurich").
WithEnvVariable("DEBIAN_FRONTEND", "noninteractive").
WithExec([]string{"apt-get", "update"}).
WithExec([]string{"apt-get", "-yqq", "install", "libpq-dev", "libvips-dev", "chromium", "graphviz", "imagemagick"}).
WithUser("ruby").
WithExec([]string{"gem", "update", "--system"}). // update bundler version
WithExec([]string{"gem", "install", "bundler", "--version", `~>2`}).
WithExec([]string{"bundle", "install", "--jobs", "4", "--retry", "3"}).
WithExec([]string{"bundle", "exec", "rails", "db:create"}).
WithExec([]string{"bundle", "exec", "rails", "db:migrate"}).
WithExec([]string{"bundle", "exec", "rails", "assets:precompile"}).
WithExec([]string{"bundle", "exec", "rails", "test", "test/controllers", "test/domain", "test/fabricators", "test/fixtures", "test/helpers", "test/mailers", "test/models", "test/presenters", "test/support", "test/tarantula"})
}

// Creates an SBOM for the container
func (m *Ci) Sbom(container *dagger.Container) *dagger.File {
trivy_container := dag.Container().
Expand All @@ -137,6 +107,18 @@ func (m *Ci) Sbom(container *dagger.Container) *dagger.File {
return sbom
}

// Executes the test suite for the Rails application in the provided Directory
func (m *Ci) Test(ctx context.Context, dir *dagger.Directory) *dagger.Directory {
return m.BaseTestContainer(ctx, dir).
WithServiceBinding("postgresql", m.Postgres(ctx, "11")).
WithServiceBinding("memcached", m.Memcached(ctx, "latest")).
WithExec([]string{"bundle", "exec", "rails", "db:create"}).
WithExec([]string{"bundle", "exec", "rails", "db:migrate"}).
WithExec([]string{"bundle", "exec", "rails", "assets:precompile"}).
WithExec([]string{"sh", "-c", "bundle exec rails test -v test/controllers test/domain test/fabricators test/fixtures test/helpers test/mailers test/models test/presenters test/support test/tarantula"}).
Directory("/mnt/test/reports")
}

// Builds the container and creates an SBOM for it
func (m *Ci) SbomBuild(ctx context.Context, dir *dagger.Directory) *dagger.File {
container := m.Build(ctx, dir)
Expand All @@ -158,15 +140,43 @@ func (m *Ci) Vulnscan(sbom *dagger.File) *dagger.File {
return trivy.Sbom(sbom).Report("json")
}

// Returns a Container with the base setup for running the rails test suite
func (m *Ci) BaseTestContainer(_ context.Context, dir *dagger.Directory) *dagger.Container {
return dag.Container().
From("registry.puzzle.ch/docker.io/ruby:3.2").
WithExec([]string{"useradd", "-m", "-u", "1001", "ruby"}).
WithMountedDirectory("/mnt", dir, dagger.ContainerWithMountedDirectoryOpts{Owner: "ruby"}).
WithWorkdir("/mnt").
WithEnvVariable("RAILS_DB_HOST", "postgresql"). // This is the service name of the postgres container called by rails
WithEnvVariable("RAILS_TEST_DB_HOST", "postgresql").
WithEnvVariable("RAILS_TEST_DB_NAME", "postgres").
WithEnvVariable("RAILS_TEST_DB_USERNAME", "postgres").
WithEnvVariable("RAILS_TEST_DB_PASSWORD", "postgres").
WithEnvVariable("RAILS_ENV", "test").
WithEnvVariable("CI", "true").
WithEnvVariable("PGDATESTYLE", "German").
WithEnvVariable("TZ", "Europe/Zurich").
WithEnvVariable("DEBIAN_FRONTEND", "noninteractive").
WithEnvVariable("TEST_REPORTS", "true").
WithExec([]string{"apt-get", "update"}).
WithExec([]string{"apt-get", "-yqq", "install", "libpq-dev", "libvips-dev", "chromium", "graphviz", "imagemagick"}).
WithUser("ruby").
//WithExec([]string{"gem", "update", "--system"}).
WithExec([]string{"gem", "install", "bundler", "--version", `~>2`}).
WithExec([]string{"bundle", "install", "--jobs", "4", "--retry", "3"})
}

// Executes all the steps and returns a Results object
func (m *Ci) Ci(ctx context.Context, dir *dagger.Directory) *Results {
lintOutput := m.Lint(dir)
securityScan := m.Sast(dir)
image := m.Build(ctx, dir)
sbom := m.Sbom(image)
vulnerabilityScan := m.Vulnscan(sbom)
testReports := m.Test(ctx, dir)

return &Results{
TestReports: testReports,
LintOutput: lintOutput,
SecurityScan: securityScan,
VulnerabilityScan: vulnerabilityScan,
Expand Down Expand Up @@ -199,15 +209,16 @@ func (m *Ci) CiIntegration(ctx context.Context, dir *dagger.Directory) *Results
return m.Build(ctx, dir)
}()

go func() {
var testReports = func() *dagger.Directory {
defer wg.Done()
m.Test(ctx, dir)
return m.Test(ctx, dir)
}()

// This Blocks the execution until its counter become 0
wg.Wait()

return &Results{
TestReports: testReports,
LintOutput: lintOutput,
SecurityScan: securityScan,
VulnerabilityScan: vulnerabilityScan,
Expand Down
26 changes: 13 additions & 13 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

ENV['RAILS_ENV'] = 'test'

# if ENV['TEST_REPORTS']
# require 'simplecov'
# require 'simplecov-rcov'
# SimpleCov.coverage_dir 'test/coverage'
# # use this formatter for jenkins compatibility
# SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
# SimpleCov.command_name 'Unit Tests'
# SimpleCov.start 'rails'

# require 'minitest/reporters'
# MiniTest::Reporters.use! [MiniTest::Reporters::DefaultReporter.new,
# MiniTest::Reporters::JUnitReporter.new]
# end
if ENV['TEST_REPORTS']
require 'simplecov'
require 'simplecov-rcov'
SimpleCov.coverage_dir 'test/coverage'
# use this formatter for jenkins compatibility
SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
SimpleCov.command_name 'Unit Tests'
SimpleCov.start 'rails'

require 'minitest/reporters'
Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new,
Minitest::Reporters::JUnitReporter.new]
end

require File.expand_path('../config/environment', __dir__)
Rails.env = 'test'
Expand Down

0 comments on commit e8f2310

Please sign in to comment.