From 05dfac72be15e3beec0f51a3211899e5a8827d7a Mon Sep 17 00:00:00 2001 From: Gabriel Omar Cotelli Date: Mon, 3 Feb 2020 13:29:43 -0300 Subject: [PATCH 01/17] Add examples to be used against a real Consul setup --- .../BaselineOfStargateConsul.class.st | 13 +- ...teConsulEchoRESTfulControllerTest.class.st | 19 +++ ...StargateConsulExampleLauncherTest.class.st | 27 ++++ .../Stargate-Consul-Examples-Tests/package.st | 1 + ...argateConsulEchoRESTfulController.class.st | 49 ++++++ .../StargateConsulExampleLauncher.class.st | 149 ++++++++++++++++++ source/Stargate-Consul-Examples/package.st | 1 + 7 files changed, 257 insertions(+), 2 deletions(-) create mode 100644 source/Stargate-Consul-Examples-Tests/StargateConsulEchoRESTfulControllerTest.class.st create mode 100644 source/Stargate-Consul-Examples-Tests/StargateConsulExampleLauncherTest.class.st create mode 100644 source/Stargate-Consul-Examples-Tests/package.st create mode 100644 source/Stargate-Consul-Examples/StargateConsulEchoRESTfulController.class.st create mode 100644 source/Stargate-Consul-Examples/StargateConsulExampleLauncher.class.st create mode 100644 source/Stargate-Consul-Examples/package.st diff --git a/source/BaselineOfStargateConsul/BaselineOfStargateConsul.class.st b/source/BaselineOfStargateConsul/BaselineOfStargateConsul.class.st index 5f6a090..6a5ca95 100644 --- a/source/BaselineOfStargateConsul/BaselineOfStargateConsul.class.st +++ b/source/BaselineOfStargateConsul/BaselineOfStargateConsul.class.st @@ -40,10 +40,19 @@ BaselineOfStargateConsul >> setUpDependencies: spec [ 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') ]; + 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' ] diff --git a/source/Stargate-Consul-Examples-Tests/StargateConsulEchoRESTfulControllerTest.class.st b/source/Stargate-Consul-Examples-Tests/StargateConsulEchoRESTfulControllerTest.class.st new file mode 100644 index 0000000..d00ae00 --- /dev/null +++ b/source/Stargate-Consul-Examples-Tests/StargateConsulEchoRESTfulControllerTest.class.st @@ -0,0 +1,19 @@ +Class { + #name : #StargateConsulEchoRESTfulControllerTest, + #superclass : #SingleResourceRESTfulControllerTest, + #category : #'Stargate-Consul-Examples-Tests' +} + +{ #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' +] diff --git a/source/Stargate-Consul-Examples-Tests/StargateConsulExampleLauncherTest.class.st b/source/Stargate-Consul-Examples-Tests/StargateConsulExampleLauncherTest.class.st new file mode 100644 index 0000000..d496be8 --- /dev/null +++ b/source/Stargate-Consul-Examples-Tests/StargateConsulExampleLauncherTest.class.st @@ -0,0 +1,27 @@ +Class { + #name : #StargateConsulExampleLauncherTest, + #superclass : #TestCase, + #category : #'Stargate-Consul-Examples-Tests' +} + +{ #category : #tests } +StargateConsulExampleLauncherTest >> testActivate [ + + | launcher | + + self + shouldnt: [ launcher := StargateConsulExampleLauncher new + commandLine: ( CommandLineArguments withArguments: #('--public-URL=http://localhost') ); + disableConsulServiceDiscoveryPlugin; + activate + ] + raise: Exit. + + launcher stop +] + +{ #category : #tests } +StargateConsulExampleLauncherTest >> testDescription [ + + self assert: StargateConsulExampleLauncher description equals: 'I provide a RESTful API over HTTP' +] diff --git a/source/Stargate-Consul-Examples-Tests/package.st b/source/Stargate-Consul-Examples-Tests/package.st new file mode 100644 index 0000000..95c3518 --- /dev/null +++ b/source/Stargate-Consul-Examples-Tests/package.st @@ -0,0 +1 @@ +Package { #name : #'Stargate-Consul-Examples-Tests' } diff --git a/source/Stargate-Consul-Examples/StargateConsulEchoRESTfulController.class.st b/source/Stargate-Consul-Examples/StargateConsulEchoRESTfulController.class.st new file mode 100644 index 0000000..82952ec --- /dev/null +++ b/source/Stargate-Consul-Examples/StargateConsulEchoRESTfulController.class.st @@ -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 +] diff --git a/source/Stargate-Consul-Examples/StargateConsulExampleLauncher.class.st b/source/Stargate-Consul-Examples/StargateConsulExampleLauncher.class.st new file mode 100644 index 0000000..2a439d7 --- /dev/null +++ b/source/Stargate-Consul-Examples/StargateConsulExampleLauncher.class.st @@ -0,0 +1,149 @@ +Class { + #name : #StargateConsulExampleLauncher, + #superclass : #ApplicationStarterCommandLineHandler, + #instVars : [ + 'api', + 'consulServiceDiscoveryIsEnabled' + ], + #category : #'Stargate-Consul-Examples' +} + +{ #category : #accessing } +StargateConsulExampleLauncher class >> commandName [ + + ^ 'stargate-consul-example' +] + +{ #category : #accessing } +StargateConsulExampleLauncher class >> description [ + + ^ 'I provide a RESTful API over HTTP' +] + +{ #category : #'private - accessing' } +StargateConsulExampleLauncher class >> logPrefix [ + + ^ 'stargate-consul-example' +] + +{ #category : #'private - accessing' } +StargateConsulExampleLauncher >> apiConfiguration [ + + ^ Array + with: #serverUrl -> self publicURL + with: #port -> ( self configuration at: 'port' ) + with: #debugMode -> self isDebugModeEnabled + with: #operations -> self operationsConfiguration +] + +{ #category : #'private - accessing' } +StargateConsulExampleLauncher >> authAlgorithm [ + + ^ JWAHMACSHA256 parameterValue +] + +{ #category : #'private - activation' } +StargateConsulExampleLauncher >> basicActivate [ + + api := HTTPBasedRESTfulAPI + configuredBy: self apiConfiguration + installing: {StargateConsulEchoRESTfulController new}. + + CurrentLogger value + logAsInfo: 'Installing API' during: [ api install ]; + logAsInfo: 'Starting API' during: [ api start ] +] + +{ #category : #'private - accessing' } +StargateConsulExampleLauncher >> configurationDefinition [ + + ^ Array + with: ( MandatoryArgument named: 'public-URL' convertingWith: #asUrl ) + with: ( OptionalArgument named: 'port' defaultingTo: 8080 convertingWith: #asNumber ) + with: + ( OptionalArgument named: 'operations-secret' defaultingTo: 'SECRET' convertingWith: #asByteArray ) + asSensitive + with: ( MandatoryArgument named: 'consul-agent-location' convertingWith: #asUrl ) +] + +{ #category : #'private - accessing' } +StargateConsulExampleLauncher >> consulServiceDiscoveryConfiguration [ + + ^ Dictionary new + at: #enabled put: consulServiceDiscoveryIsEnabled; + at: + #definitions + -> + {( ConsulServiceDefinitionBuilder new + addCheck: + ( ConsulAgentHTTPBasedCheck + named: 'health-check' + executing: #POST + against: self publicURL / 'operations' asUrl / HealthCheckPlugin endpoint + withHeaders: + {( #accept -> 'application/vnd.stargate.health-check.summary+json;version=1.0.0' ). + ( #authorization -> ( 'Bearer <1s>' expandMacrosWith: self healthCheckToken ) )} + every: 10 seconds + timeoutAfter: 1 minute ); + buildNamed: 'echo' )}; + at: #consulAgentLocation -> ( self configuration at: 'consul-agent-location' ); + yourself +] + +{ #category : #configuring } +StargateConsulExampleLauncher >> disableConsulServiceDiscoveryPlugin [ + + consulServiceDiscoveryIsEnabled := false +] + +{ #category : #'private - accessing' } +StargateConsulExampleLauncher >> healthCheckToken [ + + | jws | + + jws := JsonWebSignature new. + jws algorithmName: self authAlgorithm. + jws + payload: + ( JWTClaimsSet new + permissions: #('execute:health-check'); + yourself ). + jws key: self operationsSecret. + ^ jws compactSerialized +] + +{ #category : #initialization } +StargateConsulExampleLauncher >> initialize [ + + super initialize. + consulServiceDiscoveryIsEnabled := true +] + +{ #category : #'private - accessing' } +StargateConsulExampleLauncher >> operationsConfiguration [ + + ^ Dictionary new + at: #authSchema put: 'jwt'; + at: #authAlgorithm put: self authAlgorithm; + at: #authSecret put: self operationsSecret; + at: ConsulServiceDiscoveryPlugin endpoint put: self consulServiceDiscoveryConfiguration; + yourself +] + +{ #category : #'private - accessing' } +StargateConsulExampleLauncher >> operationsSecret [ + + ^ self configuration at: 'operations-secret' +] + +{ #category : #'private - accessing' } +StargateConsulExampleLauncher >> publicURL [ + + ^ self configuration at: 'public-URL' +] + +{ #category : #'private - activation' } +StargateConsulExampleLauncher >> stop [ + + api stop +] diff --git a/source/Stargate-Consul-Examples/package.st b/source/Stargate-Consul-Examples/package.st new file mode 100644 index 0000000..a333e82 --- /dev/null +++ b/source/Stargate-Consul-Examples/package.st @@ -0,0 +1 @@ +Package { #name : #'Stargate-Consul-Examples' } From 9c52263c01c59fdc4a0adca4759f83d8e5ea0bdf Mon Sep 17 00:00:00 2001 From: Gabriel Omar Cotelli Date: Mon, 3 Feb 2020 13:33:31 -0300 Subject: [PATCH 02/17] Fix failing test --- ...StargateConsulExampleLauncherTest.class.st | 12 ++++--- .../StargateConsulExampleLauncher.class.st | 31 +++++++++---------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/source/Stargate-Consul-Examples-Tests/StargateConsulExampleLauncherTest.class.st b/source/Stargate-Consul-Examples-Tests/StargateConsulExampleLauncherTest.class.st index d496be8..e6b4f8c 100644 --- a/source/Stargate-Consul-Examples-Tests/StargateConsulExampleLauncherTest.class.st +++ b/source/Stargate-Consul-Examples-Tests/StargateConsulExampleLauncherTest.class.st @@ -9,15 +9,17 @@ StargateConsulExampleLauncherTest >> testActivate [ | launcher | - self + [ self shouldnt: [ launcher := StargateConsulExampleLauncher new - commandLine: ( CommandLineArguments withArguments: #('--public-URL=http://localhost') ); + commandLine: + ( CommandLineArguments + withArguments: #('--public-URL=http://localhost' '--consul-agent-location=http://consul:8500') ); disableConsulServiceDiscoveryPlugin; activate ] - raise: Exit. - - launcher stop + raise: Exit + ] + ensure: [ launcher stop ] ] { #category : #tests } diff --git a/source/Stargate-Consul-Examples/StargateConsulExampleLauncher.class.st b/source/Stargate-Consul-Examples/StargateConsulExampleLauncher.class.st index 2a439d7..0aa1cb3 100644 --- a/source/Stargate-Consul-Examples/StargateConsulExampleLauncher.class.st +++ b/source/Stargate-Consul-Examples/StargateConsulExampleLauncher.class.st @@ -71,22 +71,21 @@ StargateConsulExampleLauncher >> consulServiceDiscoveryConfiguration [ ^ Dictionary new at: #enabled put: consulServiceDiscoveryIsEnabled; - at: - #definitions - -> - {( ConsulServiceDefinitionBuilder new - addCheck: - ( ConsulAgentHTTPBasedCheck - named: 'health-check' - executing: #POST - against: self publicURL / 'operations' asUrl / HealthCheckPlugin endpoint - withHeaders: - {( #accept -> 'application/vnd.stargate.health-check.summary+json;version=1.0.0' ). - ( #authorization -> ( 'Bearer <1s>' expandMacrosWith: self healthCheckToken ) )} - every: 10 seconds - timeoutAfter: 1 minute ); - buildNamed: 'echo' )}; - at: #consulAgentLocation -> ( self configuration at: 'consul-agent-location' ); + at: #definitions + put: + {( ConsulServiceDefinitionBuilder new + addCheck: + ( ConsulAgentHTTPBasedCheck + named: 'health-check' + executing: #POST + against: self publicURL / 'operations' asUrl / HealthCheckPlugin endpoint + withHeaders: + {( #accept -> 'application/vnd.stargate.health-check.summary+json;version=1.0.0' ). + ( #authorization -> ( 'Bearer <1s>' expandMacrosWith: self healthCheckToken ) )} + every: 10 seconds + timeoutAfter: 1 minute ); + buildNamed: 'echo' )}; + at: #consulAgentLocation put: ( self configuration at: 'consul-agent-location' ); yourself ] From 01f33e2c693e102dc837449531e783eb6da7faf5 Mon Sep 17 00:00:00 2001 From: Gabriel Omar Cotelli Date: Mon, 3 Feb 2020 13:34:46 -0300 Subject: [PATCH 03/17] Add debug-mode to the test case --- .../StargateConsulExampleLauncherTest.class.st | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/Stargate-Consul-Examples-Tests/StargateConsulExampleLauncherTest.class.st b/source/Stargate-Consul-Examples-Tests/StargateConsulExampleLauncherTest.class.st index e6b4f8c..64d0b4a 100644 --- a/source/Stargate-Consul-Examples-Tests/StargateConsulExampleLauncherTest.class.st +++ b/source/Stargate-Consul-Examples-Tests/StargateConsulExampleLauncherTest.class.st @@ -13,7 +13,7 @@ StargateConsulExampleLauncherTest >> testActivate [ shouldnt: [ launcher := StargateConsulExampleLauncher new commandLine: ( CommandLineArguments - withArguments: #('--public-URL=http://localhost' '--consul-agent-location=http://consul:8500') ); + withArguments: #('--public-URL=http://localhost' '--consul-agent-location=http://consul:8500' '--debug-mode') ); disableConsulServiceDiscoveryPlugin; activate ] From 02dd703226a7eeca98cd8388ad710b8f1c9fd516 Mon Sep 17 00:00:00 2001 From: Gabriel Omar Cotelli Date: Mon, 3 Feb 2020 13:42:21 -0300 Subject: [PATCH 04/17] Fix tests --- .../StargateConsulEchoRESTfulControllerTest.class.st | 12 ++++++++++++ .../StargateConsulExampleLauncherTest.class.st | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/source/Stargate-Consul-Examples-Tests/StargateConsulEchoRESTfulControllerTest.class.st b/source/Stargate-Consul-Examples-Tests/StargateConsulEchoRESTfulControllerTest.class.st index d00ae00..d3fc316 100644 --- a/source/Stargate-Consul-Examples-Tests/StargateConsulEchoRESTfulControllerTest.class.st +++ b/source/Stargate-Consul-Examples-Tests/StargateConsulEchoRESTfulControllerTest.class.st @@ -4,6 +4,18 @@ Class { #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 [ diff --git a/source/Stargate-Consul-Examples-Tests/StargateConsulExampleLauncherTest.class.st b/source/Stargate-Consul-Examples-Tests/StargateConsulExampleLauncherTest.class.st index 64d0b4a..efc4774 100644 --- a/source/Stargate-Consul-Examples-Tests/StargateConsulExampleLauncherTest.class.st +++ b/source/Stargate-Consul-Examples-Tests/StargateConsulExampleLauncherTest.class.st @@ -10,7 +10,8 @@ StargateConsulExampleLauncherTest >> testActivate [ | launcher | [ self - shouldnt: [ launcher := StargateConsulExampleLauncher new + shouldnt: [ launcher := StargateConsulExampleLauncher new. + launcher commandLine: ( CommandLineArguments withArguments: #('--public-URL=http://localhost' '--consul-agent-location=http://consul:8500' '--debug-mode') ); From 2e11907bf4b55e75bcfa6d5249233af0bce62f81 Mon Sep 17 00:00:00 2001 From: Gabriel Omar Cotelli Date: Mon, 3 Feb 2020 14:08:50 -0300 Subject: [PATCH 05/17] Fixed dependencies --- .../BaselineOfStargateConsul.class.st | 10 +++++++--- .../StargateConsulExampleLauncher.class.st | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/source/BaselineOfStargateConsul/BaselineOfStargateConsul.class.st b/source/BaselineOfStargateConsul/BaselineOfStargateConsul.class.st index 6a5ca95..929f503 100644 --- a/source/BaselineOfStargateConsul/BaselineOfStargateConsul.class.st +++ b/source/BaselineOfStargateConsul/BaselineOfStargateConsul.class.st @@ -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') ] ] @@ -33,7 +33,11 @@ BaselineOfStargateConsul >> setUpDependencies: spec [ baseline: 'Stargate' with: [ spec repository: 'github://ba-st/Stargate:v4/source' ]; project: 'Stargate-Core' copyFrom: 'Stargate' with: [ spec loads: 'Core' ]; 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 } @@ -48,7 +52,7 @@ BaselineOfStargateConsul >> setUpPackages: spec [ group: 'Tests' with: 'Stargate-Consul-Tests'. spec - package: 'Stargate-Consul-Examples' with: [ spec requires: #('Stargate-Consul') ]; + package: 'Stargate-Consul-Examples' with: [ spec requires: #('Stargate-Consul' 'Launchpad-Deployment') ]; group: 'Examples' with: 'Stargate-Consul-Examples'. spec diff --git a/source/Stargate-Consul-Examples/StargateConsulExampleLauncher.class.st b/source/Stargate-Consul-Examples/StargateConsulExampleLauncher.class.st index 0aa1cb3..e09dccb 100644 --- a/source/Stargate-Consul-Examples/StargateConsulExampleLauncher.class.st +++ b/source/Stargate-Consul-Examples/StargateConsulExampleLauncher.class.st @@ -144,5 +144,5 @@ StargateConsulExampleLauncher >> publicURL [ { #category : #'private - activation' } StargateConsulExampleLauncher >> stop [ - api stop + api ifNotNil: [ :theAPI | theAPI stop ] ] From a1b298f0e7f035c73026974659f47ef925eed11f Mon Sep 17 00:00:00 2001 From: Gabriel Omar Cotelli Date: Mon, 3 Feb 2020 14:23:42 -0300 Subject: [PATCH 06/17] Add missing dependency --- .../BaselineOfStargateConsul.class.st | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/source/BaselineOfStargateConsul/BaselineOfStargateConsul.class.st b/source/BaselineOfStargateConsul/BaselineOfStargateConsul.class.st index 929f503..37c80ea 100644 --- a/source/BaselineOfStargateConsul/BaselineOfStargateConsul.class.st +++ b/source/BaselineOfStargateConsul/BaselineOfStargateConsul.class.st @@ -32,6 +32,9 @@ 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' ]. @@ -52,7 +55,8 @@ BaselineOfStargateConsul >> setUpPackages: spec [ group: 'Tests' with: 'Stargate-Consul-Tests'. spec - package: 'Stargate-Consul-Examples' with: [ spec requires: #('Stargate-Consul' 'Launchpad-Deployment') ]; + package: 'Stargate-Consul-Examples' + with: [ spec requires: #('Stargate-Consul' 'Launchpad-Deployment' 'Stargate-HealthCheck-Deployment') ]; group: 'Examples' with: 'Stargate-Consul-Examples'. spec From efa29435c1b7838b769713da505074ae4c26b33d Mon Sep 17 00:00:00 2001 From: Gabriel Omar Cotelli Date: Mon, 3 Feb 2020 15:16:40 -0300 Subject: [PATCH 07/17] First attempt to use docker --- .travis.yml | 5 +++++ api-tests/Dockerfile | 34 ++++++++++++++++++++++++++++++++++ api-tests/docker-compose.yml | 22 ++++++++++++++++++++++ api-tests/health-check.sh | 6 ++++++ api-tests/load-project.st | 9 +++++++++ api-tests/start.sh | 29 +++++++++++++++++++++++++++++ 6 files changed, 105 insertions(+) create mode 100644 api-tests/Dockerfile create mode 100644 api-tests/docker-compose.yml create mode 100755 api-tests/health-check.sh create mode 100644 api-tests/load-project.st create mode 100755 api-tests/start.sh diff --git a/.travis.yml b/.travis.yml index 68009c3..f9ae836 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,13 @@ language: smalltalk sudo: false +services: + - docker os: - linux smalltalk: - Pharo64-8.0 - Pharo64-7.0 - Pharo32-7.0 +script: + - smalltalkci + - docker-compose up --project-directory api-tests diff --git a/api-tests/Dockerfile b/api-tests/Dockerfile new file mode 100644 index 0000000..23f926f --- /dev/null +++ b/api-tests/Dockerfile @@ -0,0 +1,34 @@ +# Stage 1: Load the project +FROM basmalltalk/pharo:7.0-image AS loader +ARG TRAVIS_BRANCH=release-candidate +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"] diff --git a/api-tests/docker-compose.yml b/api-tests/docker-compose.yml new file mode 100644 index 0000000..d213d28 --- /dev/null +++ b/api-tests/docker-compose.yml @@ -0,0 +1,22 @@ +version: "3.7" +services: + api: + build: + args: + - TRAVIS_BRANCH + 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:8500 + depends_on: + - consul + consul: + image: consul:1.6 + ports: + - "8500:8500" + environment: + CONSUL_LOCAL_CONFIG: {"enable_script_checks":true} diff --git a/api-tests/health-check.sh b/api-tests/health-check.sh new file mode 100755 index 0000000..bf54ffa --- /dev/null +++ b/api-tests/health-check.sh @@ -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 diff --git a/api-tests/load-project.st b/api-tests/load-project.st new file mode 100644 index 0000000..57e7d7a --- /dev/null +++ b/api-tests/load-project.st @@ -0,0 +1,9 @@ +| branchName | + +branchName := Smalltalk os environment at: 'TRAVIS_BRANCH' ifAbsent: ['release-candidate']. +branchName ifEmpty: [ branchName := 'release-candidate']. + +Metacello new + baseline: 'StargateConsul'; + repository: 'github://ba-st/Stargate-Consul:release-candidate/source'; + load: 'Examples'. diff --git a/api-tests/start.sh b/api-tests/start.sh new file mode 100755 index 0000000..256e7f6 --- /dev/null +++ b/api-tests/start.sh @@ -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 From 832e6aae91e9ae75ac0831391120587eca7484fd Mon Sep 17 00:00:00 2001 From: Gabriel Omar Cotelli Date: Tue, 4 Feb 2020 14:36:15 -0300 Subject: [PATCH 08/17] Update docker files --- .travis.yml | 2 +- api-tests/Dockerfile | 1 + api-tests/docker-compose.yml | 11 +++++++---- api-tests/load-project.st | 2 +- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index f9ae836..3de527c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,4 +10,4 @@ smalltalk: - Pharo32-7.0 script: - smalltalkci - - docker-compose up --project-directory api-tests + - docker-compose -f api-tests/docker-compose.yml up diff --git a/api-tests/Dockerfile b/api-tests/Dockerfile index 23f926f..ec64607 100644 --- a/api-tests/Dockerfile +++ b/api-tests/Dockerfile @@ -1,6 +1,7 @@ # Stage 1: Load the project FROM basmalltalk/pharo:7.0-image AS loader ARG TRAVIS_BRANCH=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 diff --git a/api-tests/docker-compose.yml b/api-tests/docker-compose.yml index d213d28..af5f6b3 100644 --- a/api-tests/docker-compose.yml +++ b/api-tests/docker-compose.yml @@ -2,6 +2,7 @@ version: "3.7" services: api: build: + context: ./ args: - TRAVIS_BRANCH ports: @@ -11,12 +12,14 @@ services: OPERATIONS_SECRET: API-tests HEALTH_CHECK_TOKEN: eyJhbGciOiJIUzI1NiJ9.eyJzY29wZSI6ImV4ZWN1dGU6aGVhbHRoLWNoZWNrIn0.FQgIMpaHZX_7yPWRNC5HAsmlYWkNYu0pdNrSil3ECRc APPLICATION_CONTROL_TOKEN: eyJhbGciOiJIUzI1NiJ9.eyJzY29wZSI6ImV4ZWN1dGU6YXBwbGljYXRpb24tY29udHJvbCJ9.CCMnq-6mlX-ZoSC2OfwdBOVn5DILmahP58hfXowjqFk - CONSUL_AGENT_LOCATION: http://consul:8500 + CONSUL_AGENT_LOCATION: http://consul-agent:8500 depends_on: - - consul - consul: + - consul-agent + consul-agent: image: consul:1.6 ports: - "8500:8500" + - "8600:8600" + - "8300:8300" environment: - CONSUL_LOCAL_CONFIG: {"enable_script_checks":true} + CONSUL_BIND_INTERFACE: eth0 diff --git a/api-tests/load-project.st b/api-tests/load-project.st index 57e7d7a..465dfed 100644 --- a/api-tests/load-project.st +++ b/api-tests/load-project.st @@ -5,5 +5,5 @@ branchName ifEmpty: [ branchName := 'release-candidate']. Metacello new baseline: 'StargateConsul'; - repository: 'github://ba-st/Stargate-Consul:release-candidate/source'; + repository: ('github://ba-st/Stargate-Consul:<1s>/source' expandMacrosWith: branchName); load: 'Examples'. From f5674248e999fcb19afc1211d4ee153fad75e8cd Mon Sep 17 00:00:00 2001 From: Gabriel Omar Cotelli Date: Tue, 4 Feb 2020 15:03:13 -0300 Subject: [PATCH 09/17] Fixed API url and Headers format on the HTTP based check --- .../StargateConsulExampleLauncher.class.st | 11 +++-- .../ConsulAgentHTTPBasedCheckTest.class.st | 2 +- .../FakeConsulAgentAPI.class.st | 4 +- .../ConsulAgentHTTPBasedCheck.class.st | 19 ++++++- .../ConsulServiceDiscoveryPlugin.class.st | 49 +++++++++++++++---- 5 files changed, 66 insertions(+), 19 deletions(-) diff --git a/source/Stargate-Consul-Examples/StargateConsulExampleLauncher.class.st b/source/Stargate-Consul-Examples/StargateConsulExampleLauncher.class.st index e09dccb..9a4cb4b 100644 --- a/source/Stargate-Consul-Examples/StargateConsulExampleLauncher.class.st +++ b/source/Stargate-Consul-Examples/StargateConsulExampleLauncher.class.st @@ -49,9 +49,10 @@ StargateConsulExampleLauncher >> basicActivate [ configuredBy: self apiConfiguration installing: {StargateConsulEchoRESTfulController new}. - CurrentLogger value - logAsInfo: 'Installing API' during: [ api install ]; - logAsInfo: 'Starting API' during: [ api start ] + CurrentLogger value logAsInfo: 'Installing API' during: [ api install ]. + self isDebugModeEnabled + ifFalse: [ api on: Error addErrorHandler: [ :error | self class dumpStackAndReport: error ] ]. + CurrentLogger value logAsInfo: 'Starting API' during: [ api start ] ] { #category : #'private - accessing' } @@ -80,8 +81,8 @@ StargateConsulExampleLauncher >> consulServiceDiscoveryConfiguration [ executing: #POST against: self publicURL / 'operations' asUrl / HealthCheckPlugin endpoint withHeaders: - {( #accept -> 'application/vnd.stargate.health-check.summary+json;version=1.0.0' ). - ( #authorization -> ( 'Bearer <1s>' expandMacrosWith: self healthCheckToken ) )} + { #accept -> 'application/vnd.stargate.health-check.summary+json;version=1.0.0'. + #authorization -> ( 'Bearer <1s>' expandMacrosWith: self healthCheckToken )} every: 10 seconds timeoutAfter: 1 minute ); buildNamed: 'echo' )}; diff --git a/source/Stargate-Consul-Tests/ConsulAgentHTTPBasedCheckTest.class.st b/source/Stargate-Consul-Tests/ConsulAgentHTTPBasedCheckTest.class.st index 6146f00..83727d8 100644 --- a/source/Stargate-Consul-Tests/ConsulAgentHTTPBasedCheckTest.class.st +++ b/source/Stargate-Consul-Tests/ConsulAgentHTTPBasedCheckTest.class.st @@ -49,7 +49,7 @@ ConsulAgentHTTPBasedCheckTest >> testAsJSONWhenHeadersArePresent [ self assert: json Name equals: 'HTTP check'; assert: json Method equals: 'POST'; - assert: json Header accept equals: 'application/json'; + assert: json Header accept equals: #('application/json'); assert: json HTTP equals: 'http://api.example.com/'; assert: json Timeout equals: '1m30s'; assert: json Interval equals: '10s' diff --git a/source/Stargate-Consul-Tests/FakeConsulAgentAPI.class.st b/source/Stargate-Consul-Tests/FakeConsulAgentAPI.class.st index 55404e8..405c4b4 100644 --- a/source/Stargate-Consul-Tests/FakeConsulAgentAPI.class.st +++ b/source/Stargate-Consul-Tests/FakeConsulAgentAPI.class.st @@ -23,9 +23,9 @@ FakeConsulAgentAPI >> configureTeapotServerWith: configuration [ server := Teapot configure: configuration. server - PUT: '/agent/service/register' -> [ :request | self handleServiceRegistration: request ]; + PUT: '/v1/agent/service/register' -> [ :request | self handleServiceRegistration: request ]; PUT: - '/agent/service/deregister/' + '/v1/agent/service/deregister/' -> [ :request | self handleServiceDeregistration: request ] ] diff --git a/source/Stargate-Consul/ConsulAgentHTTPBasedCheck.class.st b/source/Stargate-Consul/ConsulAgentHTTPBasedCheck.class.st index 489c7d4..2b0f661 100644 --- a/source/Stargate-Consul/ConsulAgentHTTPBasedCheck.class.st +++ b/source/Stargate-Consul/ConsulAgentHTTPBasedCheck.class.st @@ -50,13 +50,30 @@ ConsulAgentHTTPBasedCheck >> asDictionary [ yourself ] +{ #category : #initialization } +ConsulAgentHTTPBasedCheck >> initializeHeadersBasedOn: aHeaderCollection [ + + "Consul expects an array of strings as values for the headers. " + + headers := Dictionary new. + aHeaderCollection asDictionary + keysAndValuesDo: [ :headerName :headerValue | + headers + at: headerName + put: + ( headerValue isArray + ifTrue: [ headerValue ] + ifFalse: [ Array with: headerValue ] ) + ] +] + { #category : #initialization } ConsulAgentHTTPBasedCheck >> initializeNamed: aName executing: anHttpMethod against: anUrl withHeaders: aHeaderCollection every: aDuration timeoutAfter: timeoutDuration [ name := aName. method := anHttpMethod. url := anUrl. - headers := aHeaderCollection asDictionary. + self initializeHeadersBasedOn: aHeaderCollection. invokationInterval := aDuration. timeout := timeoutDuration ] diff --git a/source/Stargate-Consul/ConsulServiceDiscoveryPlugin.class.st b/source/Stargate-Consul/ConsulServiceDiscoveryPlugin.class.st index f92b734..8e72a0a 100644 --- a/source/Stargate-Consul/ConsulServiceDiscoveryPlugin.class.st +++ b/source/Stargate-Consul/ConsulServiceDiscoveryPlugin.class.st @@ -53,6 +53,12 @@ ConsulServiceDiscoveryPlugin class >> reportingLifecycleOfAll: aServiceDefinitio ^ self new initializeReportingLifecycleOfAll: aServiceDefinitionCollection toAgentOn: aConsulAPIUrl ] +{ #category : #private } +ConsulServiceDiscoveryPlugin >> baseAPILocation [ + + ^ consulAgentLocation / 'v1/agent/service' +] + { #category : #private } ConsulServiceDiscoveryPlugin >> deregistrationUrlFor: serviceDefinition [ @@ -60,7 +66,7 @@ ConsulServiceDiscoveryPlugin >> deregistrationUrlFor: serviceDefinition [ serviceId := serviceDefinition at: #ID ifAbsent: [ serviceDefinition Name ]. - ^ consulAgentLocation / ( 'agent/service/deregister/<1s>' expandMacrosWith: serviceId ) + ^ self baseAPILocation / ( 'deregister/<1s>' expandMacrosWith: serviceId ) ] { #category : #configuring } @@ -79,11 +85,13 @@ ConsulServiceDiscoveryPlugin >> startOn: teapotServer [ serviceDefinitions do: [ :serviceDefinition | - ZnClient new - beOneShot; - enforceHttpSuccess; - put: consulAgentLocation / 'agent/service/register' - contents: ( NeoJSONWriter toString: serviceDefinition ) + self + try: [ :client | + client + put: self baseAPILocation / 'register' + contents: ( NeoJSONWriter toString: serviceDefinition ) + ] + retryCount: 3 ] ] @@ -92,9 +100,30 @@ ConsulServiceDiscoveryPlugin >> stop [ serviceDefinitions do: [ :serviceDefinition | - ZnClient new - beOneShot; - enforceHttpSuccess; - put: ( self deregistrationUrlFor: serviceDefinition ) contents: '' + self + try: [ :client | client put: ( self deregistrationUrlFor: serviceDefinition ) contents: '' ] + retryCount: 3 + ] +] + +{ #category : #private } +ConsulServiceDiscoveryPlugin >> try: aBlock retryCount: count [ + + | client | + + client := ZnClient new. + client + beOneShot; + enforceHttpSuccess: true. + count = 1 + ifTrue: [ aBlock value: client ] + ifFalse: [ [ aBlock value: client ] + on: ZnHttpUnsuccessful + do: [ :error | + CurrentLogger value + logAsError: + ( 'Consul Agent HTTP request failed: <1s>, retry count: <2p>' expandMacrosWith: error messageText with: count ). + self try: aBlock retryCount: count - 1 + ] ] ] From 1ae996918ef879148ea91727a395c54a4f4500a0 Mon Sep 17 00:00:00 2001 From: Gabriel Omar Cotelli Date: Wed, 5 Feb 2020 09:27:03 -0300 Subject: [PATCH 10/17] Fail when expected environment variable is not correctly set --- api-tests/load-project.st | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api-tests/load-project.st b/api-tests/load-project.st index 465dfed..55c5698 100644 --- a/api-tests/load-project.st +++ b/api-tests/load-project.st @@ -1,7 +1,7 @@ | branchName | -branchName := Smalltalk os environment at: 'TRAVIS_BRANCH' ifAbsent: ['release-candidate']. -branchName ifEmpty: [ branchName := 'release-candidate']. +branchName := Smalltalk os environment at: 'TRAVIS_BRANCH' ifAbsent: [Error signal: 'TRAVIS_BRANCH environment variable not set']. +branchName ifEmpty: [ Error signal: 'TRAVIS_BRANCH environment variable value is empty']. Metacello new baseline: 'StargateConsul'; From 957df10792bb4b1890e7ef25b41d4b701bc0a69c Mon Sep 17 00:00:00 2001 From: Gabriel Omar Cotelli Date: Wed, 5 Feb 2020 09:44:15 -0300 Subject: [PATCH 11/17] Perform minimal checks after compose --- .gitignore | 1 + .travis.yml | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env diff --git a/.travis.yml b/.travis.yml index 3de527c..1298437 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,4 +10,8 @@ smalltalk: - Pharo32-7.0 script: - smalltalkci - - docker-compose -f api-tests/docker-compose.yml up + - docker-compose -f api-tests/docker-compose.yml up -d + - 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 From c04f23d166a991c11c1da553eb5ac9803bb391d2 Mon Sep 17 00:00:00 2001 From: Gabriel Omar Cotelli Date: Wed, 5 Feb 2020 10:06:20 -0300 Subject: [PATCH 12/17] Move tests involving docker-compose to a script --- .travis.yml | 7 ++----- compose-test.sh | 9 +++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) create mode 100755 compose-test.sh diff --git a/.travis.yml b/.travis.yml index 1298437..b04b6ae 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,8 +10,5 @@ smalltalk: - Pharo32-7.0 script: - smalltalkci - - docker-compose -f api-tests/docker-compose.yml up -d - - 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 + - export TRAVIS_BRANCH + - compose-test.sh diff --git a/compose-test.sh b/compose-test.sh new file mode 100755 index 0000000..280be14 --- /dev/null +++ b/compose-test.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +echo "TRAVIS_BRANCH=${TRAVIS_BRANCH}" > .env +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 From b3ad7135113521808b149270108403d6b29a9f00 Mon Sep 17 00:00:00 2001 From: Gabriel Omar Cotelli Date: Wed, 5 Feb 2020 10:08:57 -0300 Subject: [PATCH 13/17] Fixed typo --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index b04b6ae..e0f1cb9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,4 +11,4 @@ smalltalk: script: - smalltalkci - export TRAVIS_BRANCH - - compose-test.sh + - ./compose-test.sh From 524614fcb99676d3f362d628d8e37ae6ad345eda Mon Sep 17 00:00:00 2001 From: Gabriel Omar Cotelli Date: Wed, 5 Feb 2020 10:17:43 -0300 Subject: [PATCH 14/17] Improve script --- compose-test.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/compose-test.sh b/compose-test.sh index 280be14..0ebba9b 100755 --- a/compose-test.sh +++ b/compose-test.sh @@ -1,9 +1,9 @@ #!/usr/bin/env bash echo "TRAVIS_BRANCH=${TRAVIS_BRANCH}" > .env -docker-compose -f api-tests/docker-compose.yml up -d +docker-compose -f api-tests/docker-compose.yml up -d || exit 1 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 +curl --fail http://localhost:8080/echo/hello || exit 1 +curl --fail http://localhost:8500/v1/agent/services || exit 1 +curl --fail http://localhost:8500/v1/health/checks/echo || exit 1 docker-compose -f api-tests/docker-compose.yml down From 33e819d47d479cee2f1a2349608fdde22b9d5981 Mon Sep 17 00:00:00 2001 From: Gabriel Omar Cotelli Date: Wed, 5 Feb 2020 10:37:17 -0300 Subject: [PATCH 15/17] Use the right branch to load in docker-compose --- .travis.yml | 1 + api-tests/Dockerfile | 2 +- api-tests/docker-compose.yml | 2 +- api-tests/load-project.st | 4 ++-- compose-test.sh | 6 +++++- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index e0f1cb9..8c59bcf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,4 +11,5 @@ smalltalk: script: - smalltalkci - export TRAVIS_BRANCH + - export TRAVIS_PULL_REQUEST_BRANCH - ./compose-test.sh diff --git a/api-tests/Dockerfile b/api-tests/Dockerfile index ec64607..f8e3cae 100644 --- a/api-tests/Dockerfile +++ b/api-tests/Dockerfile @@ -1,6 +1,6 @@ # Stage 1: Load the project FROM basmalltalk/pharo:7.0-image AS loader -ARG TRAVIS_BRANCH=release-candidate +ARG BRANCH_NAME=release-candidate COPY load-project.st ./ RUN pharo Pharo.image load-project.st --save --quit diff --git a/api-tests/docker-compose.yml b/api-tests/docker-compose.yml index af5f6b3..9fc0102 100644 --- a/api-tests/docker-compose.yml +++ b/api-tests/docker-compose.yml @@ -4,7 +4,7 @@ services: build: context: ./ args: - - TRAVIS_BRANCH + - BRANCH_NAME ports: - "8080:8080" environment: diff --git a/api-tests/load-project.st b/api-tests/load-project.st index 55c5698..d53cf9d 100644 --- a/api-tests/load-project.st +++ b/api-tests/load-project.st @@ -1,7 +1,7 @@ | branchName | -branchName := Smalltalk os environment at: 'TRAVIS_BRANCH' ifAbsent: [Error signal: 'TRAVIS_BRANCH environment variable not set']. -branchName ifEmpty: [ Error signal: 'TRAVIS_BRANCH environment variable value is empty']. +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'; diff --git a/compose-test.sh b/compose-test.sh index 0ebba9b..ef231d9 100755 --- a/compose-test.sh +++ b/compose-test.sh @@ -1,6 +1,10 @@ #!/usr/bin/env bash -echo "TRAVIS_BRANCH=${TRAVIS_BRANCH}" > .env +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 || exit 1 sleep 10 curl --fail http://localhost:8080/echo/hello || exit 1 From a6ebe6f7d1e96edced10f4457dc8d01e6b9c9f84 Mon Sep 17 00:00:00 2001 From: Gabriel Omar Cotelli Date: Wed, 5 Feb 2020 12:48:16 -0300 Subject: [PATCH 16/17] Improve handling of network errors --- .../ConsulServiceDiscoveryPlugin.class.st | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/source/Stargate-Consul/ConsulServiceDiscoveryPlugin.class.st b/source/Stargate-Consul/ConsulServiceDiscoveryPlugin.class.st index 8e72a0a..6e2e4d0 100644 --- a/source/Stargate-Consul/ConsulServiceDiscoveryPlugin.class.st +++ b/source/Stargate-Consul/ConsulServiceDiscoveryPlugin.class.st @@ -59,6 +59,12 @@ ConsulServiceDiscoveryPlugin >> baseAPILocation [ ^ consulAgentLocation / 'v1/agent/service' ] +{ #category : #private } +ConsulServiceDiscoveryPlugin >> connectivityErrors [ + + ^ ZnHttpUnsuccessful , NetworkError +] + { #category : #private } ConsulServiceDiscoveryPlugin >> deregistrationUrlFor: serviceDefinition [ @@ -98,12 +104,17 @@ ConsulServiceDiscoveryPlugin >> startOn: teapotServer [ { #category : #controlling } ConsulServiceDiscoveryPlugin >> stop [ - serviceDefinitions + [ serviceDefinitions do: [ :serviceDefinition | self try: [ :client | client put: ( self deregistrationUrlFor: serviceDefinition ) contents: '' ] retryCount: 3 ] + ] + on: self connectivityErrors + do: [ :error | "Since we are already stopping the API, just ignore it. + Consul will figure out eventually that this service is dead." + error return ] ] { #category : #private } @@ -118,11 +129,13 @@ ConsulServiceDiscoveryPlugin >> try: aBlock retryCount: count [ count = 1 ifTrue: [ aBlock value: client ] ifFalse: [ [ aBlock value: client ] - on: ZnHttpUnsuccessful + on: self connectivityErrors do: [ :error | CurrentLogger value logAsError: - ( 'Consul Agent HTTP request failed: <1s>, retry count: <2p>' expandMacrosWith: error messageText with: count ). + ( 'Consul Agent HTTP request failed: <1s>, retry count: <2p>' + expandMacrosWith: error messageText + with: count ). self try: aBlock retryCount: count - 1 ] ] From a1b7a73b43ec12e30aebad3eed64e47141288d19 Mon Sep 17 00:00:00 2001 From: Gabriel Omar Cotelli Date: Wed, 5 Feb 2020 12:49:49 -0300 Subject: [PATCH 17/17] Take in consideration code review --- api-tests/Dockerfile | 4 ++-- compose-test.sh | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/api-tests/Dockerfile b/api-tests/Dockerfile index f8e3cae..43599d0 100644 --- a/api-tests/Dockerfile +++ b/api-tests/Dockerfile @@ -16,8 +16,8 @@ RUN apt-get update \ && rm --recursive --force /var/lib/apt/lists/* /tmp/* /var/tmp/* WORKDIR /opt/Stargate-Consul-Example-API -COPY start.sh . -COPY health-check.sh . +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 ./ diff --git a/compose-test.sh b/compose-test.sh index ef231d9..61644ff 100755 --- a/compose-test.sh +++ b/compose-test.sh @@ -1,13 +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 || exit 1 +docker-compose -f api-tests/docker-compose.yml up -d sleep 10 -curl --fail http://localhost:8080/echo/hello || exit 1 -curl --fail http://localhost:8500/v1/agent/services || exit 1 -curl --fail http://localhost:8500/v1/health/checks/echo || exit 1 +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