-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ba-st/docker
Test Consul integration against a real agent running on docker
- Loading branch information
Showing
19 changed files
with
483 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
source/Stargate-Consul-Examples-Tests/StargateConsulEchoRESTfulControllerTest.class.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
] |
30 changes: 30 additions & 0 deletions
30
source/Stargate-Consul-Examples-Tests/StargateConsulExampleLauncherTest.class.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Package { #name : #'Stargate-Consul-Examples-Tests' } |
49 changes: 49 additions & 0 deletions
49
source/Stargate-Consul-Examples/StargateConsulEchoRESTfulController.class.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] |
Oops, something went wrong.