Skip to content

Commit ff7d5a7

Browse files
Improves testing w/Sinatra app
- Scaffolds a simple Sinatra app in order to better test the testing functionality of the workflow. - Adds an $ARTIFACTS_DIR environment variable pointing to a directory whose contents will be saved as a build artifact. - Adds the parsed/merged compose file, service logs, and docker event logs to artifacts automatically.
1 parent e856db5 commit ff7d5a7

File tree

10 files changed

+150
-7
lines changed

10 files changed

+150
-7
lines changed

.dockerignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.dockerignore
2+
.DS_Store
3+
.git*
4+
.gitignore
5+
docker-compose*
6+
Dockerfile

.github/workflows/build.yml

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ jobs:
4949
runs-on: ubuntu-latest
5050
needs:
5151
- build
52+
env:
53+
COMPOSE_FILE: docker-compose.yml:docker-compose.ci.yml
54+
DOCKER_APP_IMAGE: ${{ needs.build.outputs.build-image }}
5255
steps:
5356
- name: Checkout code
5457
uses: actions/checkout@v4
@@ -66,11 +69,37 @@ jobs:
6669
username: ${{ github.actor }}
6770
password: ${{ secrets.GITHUB_TOKEN }}
6871

72+
- name: Set ARTIFACTS_DIR
73+
run: echo "ARTIFACTS_DIR=${RUNNER_TEMP}/artifacts" >> $GITHUB_ENV
74+
75+
- name: Create the artifacts directory
76+
run: mkdir -p "$ARTIFACTS_DIR"
77+
78+
- name: Record start time
79+
run: TEST_START=`date +%s` >> $GITHUB_ENV
80+
6981
- name: Run the tests
7082
run: bin/test
71-
env:
72-
COMPOSE_FILE: docker-compose.yml:docker-compose.ci.yml
73-
DOCKER_APP_IMAGE: ${{ needs.build.outputs.build-image }}
83+
84+
- name: Retain the compose file
85+
if: ${{ always() }}
86+
run: docker compose config | tee "${ARTIFACTS_DIR}/docker-compose.yml"
87+
88+
- name: Retain compose logs
89+
if: ${{ always() }}
90+
run: docker compose logs | tee "${ARTIFACTS_DIR}/docker-services.log"
91+
92+
- name: Retain Docker events
93+
if: ${{ always() }}
94+
run: docker events --json --since $TEST_START --until `date +%s` | tee "${ARTIFACTS_DIR}/docker-events.json"
95+
96+
- name: Upload test report
97+
if: ${{ always() }}
98+
uses: actions/upload-artifact@v4
99+
with:
100+
name: Test Report (${{ github.sha }}-${{ github.run_id }}-${{ github.run_attempt }})
101+
path: ${{ env.ARTIFACTS_DIR }}
102+
if-no-files-found: error
74103

75104
push:
76105
runs-on: ubuntu-latest

Dockerfile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
FROM hello-world
1+
FROM ruby:3
2+
3+
WORKDIR /opt/app
4+
COPY Gemfile* .
5+
RUN bundle install
6+
COPY . .
7+
CMD ["bundle", "exec", "ruby", "app.rb", "-o", "0.0.0.0"]
8+
EXPOSE 4567

Gemfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
4+
5+
gem "puma"
6+
gem "rack-test"
7+
gem "rackup"
8+
gem "rspec"
9+
gem "sinatra"

Gemfile.lock

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
base64 (0.3.0)
5+
diff-lcs (1.6.2)
6+
logger (1.7.0)
7+
mustermann (3.0.4)
8+
ruby2_keywords (~> 0.0.1)
9+
nio4r (2.7.4)
10+
puma (6.6.1)
11+
nio4r (~> 2.0)
12+
rack (3.2.0)
13+
rack-protection (4.1.1)
14+
base64 (>= 0.1.0)
15+
logger (>= 1.6.0)
16+
rack (>= 3.0.0, < 4)
17+
rack-session (2.1.1)
18+
base64 (>= 0.1.0)
19+
rack (>= 3.0.0)
20+
rack-test (2.2.0)
21+
rack (>= 1.3)
22+
rackup (2.2.1)
23+
rack (>= 3)
24+
rspec (3.13.1)
25+
rspec-core (~> 3.13.0)
26+
rspec-expectations (~> 3.13.0)
27+
rspec-mocks (~> 3.13.0)
28+
rspec-core (3.13.5)
29+
rspec-support (~> 3.13.0)
30+
rspec-expectations (3.13.5)
31+
diff-lcs (>= 1.2.0, < 2.0)
32+
rspec-support (~> 3.13.0)
33+
rspec-mocks (3.13.5)
34+
diff-lcs (>= 1.2.0, < 2.0)
35+
rspec-support (~> 3.13.0)
36+
rspec-support (3.13.5)
37+
ruby2_keywords (0.0.5)
38+
sinatra (4.1.1)
39+
logger (>= 1.6.0)
40+
mustermann (~> 3.0)
41+
rack (>= 3.0.0, < 4)
42+
rack-protection (= 4.1.1)
43+
rack-session (>= 2.0.0, < 3)
44+
tilt (~> 2.0)
45+
tilt (2.6.1)
46+
47+
PLATFORMS
48+
aarch64-linux
49+
ruby
50+
51+
DEPENDENCIES
52+
puma
53+
rack-test
54+
rackup
55+
rspec
56+
sinatra
57+
58+
BUNDLED WITH
59+
2.6.9

app.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require 'sinatra'
2+
3+
get '/' do
4+
"Hello, world!"
5+
end

bin/test

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#!/bin/sh -e
1+
#!/bin/bash -e
22

3-
docker compose up --exit-code-from app --no-build
4-
docker compose run --rm app | grep -q 'Hello from Docker!'
3+
docker compose up --detach --no-build
4+
docker compose exec app rspec \
5+
--format html --out "${RUNNER_TEMP}/test-results.html" \
6+
--format documentation

docker-compose.ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22

33
services:
44
app:
5+
build: !reset
56
image: ${DOCKER_APP_IMAGE}
7+
ports: !reset
8+
volumes: !reset

docker-compose.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
services:
44
app:
55
build: .
6+
ports:
7+
- 4567:4567
8+
volumes:
9+
- ./:/opt/app

spec/home_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
ENV['APP_ENV'] = 'test'
2+
3+
require_relative '../app.rb'
4+
require 'rspec'
5+
require 'rack/test'
6+
7+
RSpec.describe 'The Test App' do
8+
include Rack::Test::Methods
9+
10+
def app
11+
Sinatra::Application
12+
end
13+
14+
it "says hello" do
15+
get '/'
16+
expect(last_response).to be_ok
17+
expect(last_response.body).to eq('Hello, world!')
18+
end
19+
end

0 commit comments

Comments
 (0)