Skip to content

Commit

Permalink
Merge pull request #15 from ba-st/issue_14
Browse files Browse the repository at this point in the history
Add a basic service discovery test to the build using docker compose
  • Loading branch information
gcotelli committed Nov 3, 2020
2 parents ce1f6f2 + ab802ae commit 3c1f211
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
timeout-minutes: 15
- name: Run tests using Docker
run: ./compose-test.sh
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
Expand Down
21 changes: 3 additions & 18 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
# changes file
*.changes

# system image
*.image

# Pharo Smalltalk Debug log file
PharoDebug.log

# Squeak Smalltalk Debug log file
SqueakDebug.log

# Monticello package cache
/package-cache

# Metacello-github cache
/github-cache
github-*.zip
api-tests/.env
api-tests/logs
.env
25 changes: 25 additions & 0 deletions api-tests/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Stage 1: Load the project
FROM basmalltalk/pharo:8.0-image AS loader
ARG BRANCH_NAME=release-candidate
COPY load-project.st ./
RUN pharo Pharo.image load-project.st --save --quit

# Stage 2: Copy the resulting Pharo.image with our project loaded
# into a new docker image with just the vm
FROM basmalltalk/pharo:8.0

USER root

WORKDIR /opt/Superluminal-Service-Discovery-Example
COPY start.sh ./
COPY --from=loader /opt/pharo/Pharo.image ./
COPY --from=loader /opt/pharo/Pharo.changes ./
COPY --from=loader /opt/pharo/Pharo*.sources ./

RUN mkdir logs \
&& chmod a+x start.sh \
&& chown --recursive pharo:users /opt/Superluminal-Service-Discovery-Example

USER pharo

CMD ["./start.sh"]
33 changes: 33 additions & 0 deletions api-tests/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
version: '3'
services:
api:
image: ghcr.io/ba-st/stargate-consul-example:release-candidate
ports:
- "8080:8080"
environment:
PUBLIC_URL: http://api:8080
OPERATIONS_SECRET: API-tests
HEALTH_CHECK_TOKEN: eyJhbGciOiJIUzI1NiJ9.eyJzY29wZSI6ImV4ZWN1dGU6aGVhbHRoLWNoZWNrIn0.FQgIMpaHZX_7yPWRNC5HAsmlYWkNYu0pdNrSil3ECRc
APPLICATION_CONTROL_TOKEN: eyJhbGciOiJIUzI1NiJ9.eyJzY29wZSI6ImV4ZWN1dGU6YXBwbGljYXRpb24tY29udHJvbCJ9.CCMnq-6mlX-ZoSC2OfwdBOVn5DILmahP58hfXowjqFk
CONSUL_AGENT_LOCATION: http://consul-agent:8500
depends_on:
- consul-agent
consul-agent:
image: consul:1.8
ports:
- "8500:8500"
- "8600:8600"
- "8300:8300"
environment:
CONSUL_BIND_INTERFACE: eth0
api-client:
build:
context: ./
args:
- BRANCH_NAME
environment:
MESSAGE: 'Hello'
CONSUL_AGENT_LOCATION: http://consul-agent:8500
depends_on:
- consul-agent
- api
11 changes: 11 additions & 0 deletions api-tests/load-project.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
| branchName |

EpMonitor current disable.

branchName := Smalltalk os environment at: 'BRANCH_NAME' ifAbsent: [Error signal: 'BRANCH_NAME environment variable not set'].
branchName ifEmpty: [ Error signal: 'BRANCH_NAME environment variable value is empty'].

Metacello new
baseline: 'Superluminal';
repository: ('github://ba-st/Superluminal:<1s>' expandMacrosWith: branchName);
load: 'Examples'.
8 changes: 8 additions & 0 deletions api-tests/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

exec /opt/pharo/pharo \
/opt/Superluminal-Service-Discovery-Example/Pharo.image \
super-luminal-service-discovery \
--message="${MESSAGE}" \
--consul-agent-location="${CONSUL_AGENT_LOCATION}" \
--retry-delay-in-ms=10000
11 changes: 11 additions & 0 deletions compose-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

set -e

if [[ -z "${GITHUB_HEAD_REF##*/}" ]]; then
echo "BRANCH_NAME=${GITHUB_REF##*/}" > .env
else
echo "BRANCH_NAME=${GITHUB_HEAD_REF##*/}" > .env
fi
set +e # disable exit on error to be able to catch the exit code from compose
docker-compose -f api-tests/docker-compose.yml up --exit-code-from api-client
29 changes: 23 additions & 6 deletions source/BaselineOfSuperluminal/BaselineOfSuperluminal.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,25 @@ BaselineOfSuperluminal >> setUpDependencies: spec [
spec
baseline: 'Hyperspace' with: [ spec repository: 'github://ba-st/Hyperspace:v2' ];
project: 'Hyperspace-Deployment' copyFrom: 'Hyperspace' with: [ spec loads: 'Deployment' ];
project: 'Hyperspace-SUnit' copyFrom: 'Hyperspace' with: [ spec loads: 'Dependent-SUnit-Extensions' ].
project: 'Hyperspace-SUnit'
copyFrom: 'Hyperspace'
with: [ spec loads: 'Dependent-SUnit-Extensions' ].

spec
baseline: 'Launchpad' with: [ spec repository: 'github://ba-st/Launchpad:v3' ];
project: 'Launchpad-Deployment' copyFrom: 'Launchpad' with: [ spec loads: 'Deployment' ].

spec
baseline: 'NeoJSON' with: [ spec repository: 'github://svenvc/NeoJSON:master/repository' ];
project: 'NeoJSON-Core' copyFrom: 'NeoJSON' with: [ spec loads: 'core' ].

spec
baseline: 'ObjectPool' with: [ spec repository: 'github://pharo-ide/ObjectPool:v1.0.1' ];
project: 'ObjectPool-Core' copyFrom: 'ObjectPool' with: [ spec loads: 'Core' ].

spec
baseline: 'Teapot' with: [ spec repository: 'github://zeroflag/Teapot:v2.6.0/source' ];
project: 'Teapot-Deployment' copyFrom: 'Teapot' with: [ spec loads: 'Deployment' ].
project: 'Teapot-Deployment' copyFrom: 'Teapot' with: [ spec loads: 'Deployment' ]
]

{ #category : #baselines }
Expand All @@ -64,6 +70,11 @@ BaselineOfSuperluminal >> setUpPackages: spec [
package: 'Superluminal-Service-Discovery';
group: 'Deployment' with: 'Superluminal-Service-Discovery'.

spec
package: 'Superluminal-Service-Discovery-Examples'
with: [ spec requires: #( 'Deployment' 'Launchpad-Deployment' ) ];
group: 'Examples' with: 'Superluminal-Service-Discovery-Examples'.

spec
package: 'Superluminal-Model-Tests'
with: [ spec requires: #( 'Superluminal-Model' 'Hyperspace-SUnit' ) ];
Expand All @@ -76,6 +87,12 @@ BaselineOfSuperluminal >> setUpPackages: spec [

spec
package: 'Superluminal-Service-Discovery-Tests'
with: [ spec requires: #( 'Superluminal-Service-Discovery' 'Hyperspace-SUnit' 'Teapot-Deployment') ];
group: 'Tests' with: 'Superluminal-Service-Discovery-Tests'
with: [
spec requires: #( 'Superluminal-Service-Discovery' 'Hyperspace-SUnit' 'Teapot-Deployment' ) ];
group: 'Tests' with: 'Superluminal-Service-Discovery-Tests'.

spec
package: 'Superluminal-Service-Discovery-Examples-Tests'
with: [ spec requires: 'Superluminal-Service-Discovery-Examples' ];
group: 'Tests' with: 'Superluminal-Service-Discovery-Examples-Tests'
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"
A SuperluminalServiceDiscoveryExampleLauncherTest is a test class for testing the behavior of SuperluminalServiceDiscoveryExampleLauncher
"
Class {
#name : #SuperluminalServiceDiscoveryExampleLauncherTest,
#superclass : #TestCase,
#category : #'Superluminal-Service-Discovery-Examples-Tests'
}

{ #category : #tests }
SuperluminalServiceDiscoveryExampleLauncherTest >> testActivate [

self
should: [
| launcher |

launcher := SuperluminalServiceDiscoveryExampleLauncher new.
launcher
commandLine: ( CommandLineArguments withArguments:
#( '--consul-agent-location=http://consul:8500'
'--message=Hello' '--retry-delay-in-ms=1' '--debug-mode' ) );
activate
]
raise: NameLookupFailure
]

{ #category : #tests }
SuperluminalServiceDiscoveryExampleLauncherTest >> testArguments [

| launcher |

launcher := SuperluminalServiceDiscoveryExampleLauncher new.
launcher commandLine: ( CommandLineArguments withArguments:
#( '--consul-agent-location=http://consul:8500' '--message=Hello' '--retry-delay-in-ms=1'
'--debug-mode' ) ).

self
assert: launcher message equals: 'Hello';
assertUrl: launcher consulAgentLocation equals: 'http://consul:8500'
]

{ #category : #tests }
SuperluminalServiceDiscoveryExampleLauncherTest >> testCommandName [

self assert: SuperluminalServiceDiscoveryExampleLauncher commandName
equals: 'super-luminal-service-discovery'
]

{ #category : #tests }
SuperluminalServiceDiscoveryExampleLauncherTest >> testDescription [

self assert: SuperluminalServiceDiscoveryExampleLauncher description
equals: 'I''m a command line example using as dependency an echo API'
]

{ #category : #tests }
SuperluminalServiceDiscoveryExampleLauncherTest >> testLogPrefix [

self assert: SuperluminalServiceDiscoveryExampleLauncher logPrefix
equals: 'Superluminal-Service-Discovery'
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Package { #name : #'Superluminal-Service-Discovery-Examples-Tests' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
Class {
#name : #SuperluminalServiceDiscoveryExampleLauncher,
#superclass : #LaunchpadCommandLineHandler,
#category : #'Superluminal-Service-Discovery-Examples'
}

{ #category : #accessing }
SuperluminalServiceDiscoveryExampleLauncher class >> commandName [

^'super-luminal-service-discovery'
]

{ #category : #accessing }
SuperluminalServiceDiscoveryExampleLauncher class >> description [

^ 'I''m a command line example using as dependency an echo API'
]

{ #category : #'private - accessing' }
SuperluminalServiceDiscoveryExampleLauncher class >> logPrefix [

^ 'Superluminal-Service-Discovery'
]

{ #category : #'private - activation' }
SuperluminalServiceDiscoveryExampleLauncher >> basicActivate [

| consulAgentLocation echoServiceLocation |

consulAgentLocation := self consulAgentLocation.
CurrentLogger value logAsInfo: 'Discovering dependencies' during: [
echoServiceLocation := Retry
value: [
( ConsulAgentHttpAPIBasedDiscoveryClient queryingAgentOn:
consulAgentLocation ) withLocationOfService: #echo
do: [ :location | location ]
ifUnable: [ Error signal: 'Cannot discover #echo service' ]
]
configuredBy: [ :retry |
retry
upTo: 3 timesEvery: ( self configuration at: 'retry-delay-in-ms' );
on: Error evaluating: [ :attemptNumber :exception |
CurrentLogger value logAsWarning:
( 'Attempt #<1p> failed with error: <2s>' expandMacrosWith:
attemptNumber
with: exception messageText )
]
]
].

RESTfulAPIClient default
get: ( echoServiceLocation
scheme: #http;
addPathSegment: 'echo';
addPathSegment: self message;
yourself )
withSuccessfulResponseDo: [ :response |
response = self message asUppercase ifFalse: [ Error signal: 'Invalid response received' ] ].

self exitSuccess
]

{ #category : #'private - accessing' }
SuperluminalServiceDiscoveryExampleLauncher >> configurationDefinition [

^ Array
with: ( MandatoryArgument named: 'message' )
with: ( MandatoryArgument named: 'consul-agent-location' convertingWith: #asUrl )
with: ( OptionalArgument named: 'retry-delay-in-ms' defaultingTo: 200 convertingWith: [:parameter | parameter asNumber milliSeconds ])
]

{ #category : #'private - accessing' }
SuperluminalServiceDiscoveryExampleLauncher >> consulAgentLocation [

^ self configuration at: 'consul-agent-location'
]

{ #category : #'private - accessing' }
SuperluminalServiceDiscoveryExampleLauncher >> message [

^ self configuration at: 'message'
]
1 change: 1 addition & 0 deletions source/Superluminal-Service-Discovery-Examples/package.st
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Package { #name : #'Superluminal-Service-Discovery-Examples' }

0 comments on commit 3c1f211

Please sign in to comment.