Skip to content

Commit

Permalink
Merge pull request #2 from ba-st/docker
Browse files Browse the repository at this point in the history
Test Consul integration against a real agent running on docker
  • Loading branch information
gcotelli authored Feb 5, 2020
2 parents 151f7ca + a1b7a73 commit 3632504
Show file tree
Hide file tree
Showing 19 changed files with 483 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
language: smalltalk
sudo: false
services:
- docker
os:
- linux
smalltalk:
- Pharo64-8.0
- Pharo64-7.0
- Pharo32-7.0
script:
- smalltalkci
- export TRAVIS_BRANCH
- export TRAVIS_PULL_REQUEST_BRANCH
- ./compose-test.sh
35 changes: 35 additions & 0 deletions api-tests/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Stage 1: Load the project
FROM basmalltalk/pharo:7.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:7.0

USER root

RUN apt-get update \
&& apt-get --assume-yes --no-install-recommends install curl \
&& apt-get clean \
&& rm --recursive --force /var/lib/apt/lists/* /tmp/* /var/tmp/*

WORKDIR /opt/Stargate-Consul-Example-API
COPY start.sh ./
COPY health-check.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 \
&& chmod a+x health-check.sh \
&& chown --recursive pharo:pharo /opt/Stargate-Consul-Example-API

USER pharo
EXPOSE 8080

HEALTHCHECK CMD /opt/Stargate-Consul-Example-API/health-check.sh

CMD ["./start.sh"]
25 changes: 25 additions & 0 deletions api-tests/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: "3.7"
services:
api:
build:
context: ./
args:
- BRANCH_NAME
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.6
ports:
- "8500:8500"
- "8600:8600"
- "8300:8300"
environment:
CONSUL_BIND_INTERFACE: eth0
6 changes: 6 additions & 0 deletions api-tests/health-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

curl --fail --request POST \
--header "Authorization: Bearer $HEALTH_CHECK_TOKEN" \
--header "Accept: application/vnd.stargate.health-check.summary+json;version=1.0.0" \
http://localhost:"${PORT:-8080}"/operations/health-check || exit 1
9 changes: 9 additions & 0 deletions api-tests/load-project.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
| branchName |

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: 'StargateConsul';
repository: ('github://ba-st/Stargate-Consul:<1s>/source' expandMacrosWith: branchName);
load: 'Examples'.
29 changes: 29 additions & 0 deletions api-tests/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

# SIGTERM-handler
termination_handler() {
echo 'SIGTERM was received, stopping the API'
curl --silent --fail --request POST \
--header "Authorization: Bearer $APPLICATION_CONTROL_TOKEN" \
--header "Content-Type:application/json" \
--header "Accept: application/json" \
--data '{"jsonrpc": "2.0" ,"method": "shutdown"}' \
http://localhost:"${PORT:-8080}"/operations/application-control
exit 143; # 128 + 15 -- SIGTERM
}

trap 'kill ${!}; termination_handler' SIGTERM

/opt/pharo/pharo \
/opt/Stargate-Consul-Example-API/Pharo.image \
stargate-consul-example \
"${PORT:+--port=$PORT}" \
"${PUBLIC_URL:+--public-URL=$PUBLIC_URL}" \
"${OPERATIONS_SECRET:+--operations-secret=$OPERATIONS_SECRET}" \
"${CONSUL_AGENT_LOCATION:+--consul-agent-location=$CONSUL_AGENT_LOCATION}" &

# wait forever
while true
do
tail -f /dev/null & wait ${!}
done
15 changes: 15 additions & 0 deletions compose-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

set -e

if [[ -z "${TRAVIS_PULL_REQUEST_BRANCH}" ]]; then
echo "BRANCH_NAME=${TRAVIS_BRANCH}" > .env
else
echo "BRANCH_NAME=${TRAVIS_PULL_REQUEST_BRANCH}" > .env
fi
docker-compose -f api-tests/docker-compose.yml up -d
sleep 10
curl --fail http://localhost:8080/echo/hello
curl --fail http://localhost:8500/v1/agent/services
curl --fail http://localhost:8500/v1/health/checks/echo
docker-compose -f api-tests/docker-compose.yml down
25 changes: 21 additions & 4 deletions source/BaselineOfStargateConsul/BaselineOfStargateConsul.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ BaselineOfStargateConsul >> baseline: spec [
spec
group: 'CI' with: 'Tests';
group: 'Tools' with: #('Stargate-Tools');
group: 'Development' with: #('Tests' 'Tools')
group: 'Development' with: #('Tests' 'Tools' 'Examples')
]
]

Expand All @@ -32,18 +32,35 @@ BaselineOfStargateConsul >> setUpDependencies: spec [
spec
baseline: 'Stargate' with: [ spec repository: 'github://ba-st/Stargate:v4/source' ];
project: 'Stargate-Core' copyFrom: 'Stargate' with: [ spec loads: 'Core' ];
project: 'Stargate-HealthCheck-Deployment'
copyFrom: 'Stargate'
with: [ spec loads: 'HealthCheck' ];
project: 'Stargate-SUnit' copyFrom: 'Stargate' with: [ spec loads: 'Dependent-SUnit-Extensions' ];
project: 'Stargate-Tools' copyFrom: 'Stargate' with: [ spec loads: 'Tools' ]
project: 'Stargate-Tools' copyFrom: 'Stargate' with: [ spec loads: 'Tools' ].

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

{ #category : #baselines }
BaselineOfStargateConsul >> setUpPackages: spec [

spec
package: 'Stargate-Consul' with: [ spec requires: #('Stargate-Core') ];
package: 'Stargate-Consul' with: [ spec requires: 'Stargate-Core' ];
group: 'Deployment' with: 'Stargate-Consul'.

spec
package: 'Stargate-Consul-Tests' with: [ spec requires: #('Stargate-Consul' 'Stargate-SUnit') ];
group: 'Tests' with: 'Stargate-Consul-Tests'
group: 'Tests' with: 'Stargate-Consul-Tests'.

spec
package: 'Stargate-Consul-Examples'
with: [ spec requires: #('Stargate-Consul' 'Launchpad-Deployment' 'Stargate-HealthCheck-Deployment') ];
group: 'Examples' with: 'Stargate-Consul-Examples'.

spec
package: 'Stargate-Consul-Examples-Tests'
with: [ spec requires: #('Stargate-Consul-Examples' 'Stargate-SUnit') ];
group: 'Tests' with: 'Stargate-Consul-Examples-Tests'
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Class {
#name : #StargateConsulEchoRESTfulControllerTest,
#superclass : #SingleResourceRESTfulControllerTest,
#category : #'Stargate-Consul-Examples-Tests'
}

{ #category : #'private - support' }
StargateConsulEchoRESTfulControllerTest >> baseUrl [

^ 'http://api.example.com' asUrl
]

{ #category : #running }
StargateConsulEchoRESTfulControllerTest >> setUpResourceController [

resourceController := StargateConsulEchoRESTfulController new
]

{ #category : #tests }
StargateConsulEchoRESTfulControllerTest >> testMessageText [

| response |

response := resourceController
messageTextBasedOn: ( self requestToGETResourceIdentifiedBy: 'Hello' accepting: ZnMimeType textPlain )
within: self newHttpRequestContext.

self
assert: response isSuccess;
assert: response contents equals: 'HELLO'
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Class {
#name : #StargateConsulExampleLauncherTest,
#superclass : #TestCase,
#category : #'Stargate-Consul-Examples-Tests'
}

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

| launcher |

[ self
shouldnt: [ launcher := StargateConsulExampleLauncher new.
launcher
commandLine:
( CommandLineArguments
withArguments: #('--public-URL=http://localhost' '--consul-agent-location=http://consul:8500' '--debug-mode') );
disableConsulServiceDiscoveryPlugin;
activate
]
raise: Exit
]
ensure: [ launcher stop ]
]

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

self assert: StargateConsulExampleLauncher description equals: 'I provide a RESTful API over HTTP'
]
1 change: 1 addition & 0 deletions source/Stargate-Consul-Examples-Tests/package.st
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Package { #name : #'Stargate-Consul-Examples-Tests' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Class {
#name : #StargateConsulEchoRESTfulController,
#superclass : #SingleResourceRESTfulController,
#instVars : [
'requestHandler'
],
#category : #'Stargate-Consul-Examples'
}

{ #category : #routes }
StargateConsulEchoRESTfulController >> declareGetMessageRoute [

^ RouteSpecification
handling: #GET
at: self identifierTemplate
evaluating: [ :httpRequest :requestContext | self messageTextBasedOn: httpRequest within: requestContext ]
]

{ #category : #initialization }
StargateConsulEchoRESTfulController >> initialize [

super initialize.
requestHandler := RESTfulRequestHandlerBuilder new
handling: 'echo' extractingIdentifierWith: [ :httpRequest | self identifierIn: httpRequest ];
whenResponding: ZnMimeType textPlain encodeApplying: [ :resource | resource ];
createEntityTagHashingEncodedResource;
build
]

{ #category : #API }
StargateConsulEchoRESTfulController >> messageTextBasedOn: httpRequest within: requestContext [

^ requestHandler
from: httpRequest
within: requestContext
get: [ :message | message asString asUppercase ]
]

{ #category : #private }
StargateConsulEchoRESTfulController >> requestHandler [

^ requestHandler
]

{ #category : #private }
StargateConsulEchoRESTfulController >> typeIdConstraint [

^ IsObject
]
Loading

0 comments on commit 3632504

Please sign in to comment.