Skip to content

Commit

Permalink
ConsulServiceLocatorFT added
Browse files Browse the repository at this point in the history
  • Loading branch information
akolod committed Dec 23, 2020
1 parent 6b5f55c commit 4eede9f
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.hltech.judged.agent

import com.github.tomakehurst.wiremock.junit.WireMockRule
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import spock.lang.Specification

import java.util.concurrent.TimeUnit

import static com.github.tomakehurst.wiremock.client.WireMock.equalTo
import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson
import static com.github.tomakehurst.wiremock.client.WireMock.get
import static com.github.tomakehurst.wiremock.client.WireMock.ok
import static com.github.tomakehurst.wiremock.client.WireMock.okJson
import static com.github.tomakehurst.wiremock.client.WireMock.put
import static com.github.tomakehurst.wiremock.client.WireMock.putRequestedFor
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching
import static org.awaitility.Awaitility.await

@SpringBootTest
@ActiveProfiles(["test", "consul"])
class ConsulServiceLocatorFT extends Specification {

@Value('${hltech.contracts.judge-d.updateServices.initialDelay}')
Long updateServicesInitialDelay

static WireMockRule wireMockRuleConsulServer
static WireMockRule wireMockRuleJudgeDServer

def setupSpec() {
wireMockRuleConsulServer = new WireMockRule(8500)
wireMockRuleConsulServer.start()
wireMockRuleJudgeDServer = new WireMockRule(8080)
wireMockRuleJudgeDServer.start()
}

def cleanupSpec() {
wireMockRuleConsulServer.stop()
wireMockRuleJudgeDServer.stop()
}

def "should send update services message with expected names and versions"() {
given:
wireMockRuleConsulServer.stubFor(
get(urlPathMatching("/v1/catalog/services"))
.willReturn(okJson(
"""
{
"test_app_1": ["version=78d588f9cc"],
"test_app_2": ["version=54d576f9dh"],
"test_app_3": ["version=98d500g9df"]
}
"""
))
)

and:
stubServiceHealthResponse(1)
stubServiceHealthResponse(2)
stubServiceHealthResponse(3)

and: "expected message to publish"
def expectedRequestBody = """
[
{
"name": "test_app_1",
"version": "78d588f9cc"
},
{
"name": "test_app_2",
"version": "54d576f9dh"
},
{
"name": "test_app_3",
"version": "98d500g9df"
}
]
"""
wireMockRuleJudgeDServer.stubFor(
put(urlPathMatching("/environments/test"))
.withRequestBody(equalToJson(expectedRequestBody, true, false))
.willReturn(ok())
)

expect: "update services message is sent with expected services names and versions"
await().atMost(updateServicesInitialDelay + 5000, TimeUnit.MILLISECONDS).until({ wireMockRuleJudgeDServer.getServeEvents().requests.size() == 1 })
wireMockRuleJudgeDServer.verify(1, putRequestedFor(urlPathMatching("/environments/test")).withRequestBody(equalToJson(expectedRequestBody, true, false)))
}

def stubServiceHealthResponse(int serviceTestId) {
wireMockRuleConsulServer.stubFor(
get(urlPathMatching("/v1/health/service/test_app_$serviceTestId"))
.withQueryParam("passing", equalTo("true"))
.willReturn(okJson(
"""
[
{
"Node": {
"ID": "test_app_$serviceTestId",
"Node": "test_app_$serviceTestId"
}
}
]
"""
))
)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class K8sServiceLocatorFT extends Specification {
wireMockRule.start()
}

def cleanupSpec() {
wireMockRule.stop()
}

def "should send update services message with expected names and versions"() {
given: "expected namespaces"
k8sServer.expect().get().withPath("/api/v1/namespaces")
Expand Down
2 changes: 2 additions & 0 deletions judge-d-agent/src/test/resources/application-test.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ hltech.contracts.judge-d.included-namespaces=

hltech.contracts.judge-d.updateServices.fixedDelay=2000
hltech.contracts.judge-d.updateServices.initialDelay=4000

hltech.contracts.judge-d.consul-host=http://localhost

0 comments on commit 4eede9f

Please sign in to comment.