Skip to content

Commit

Permalink
[ANCHOR-535] Clean up test profiles (#1199)
Browse files Browse the repository at this point in the history
### Description

1. Test profiles are stacked on top of the `default` profile. This is to
simplify and avoid env variables duplications in different profiles to
increase the code readability.
2. Simplify the test profiles. 
3. Add `exitStatus` to the `waitForTxnStatus` function to shorten the
test failure time.

### Context

CI improvement.
  • Loading branch information
lijamie98 authored Nov 16, 2023
1 parent 3511ace commit 58c4c32
Show file tree
Hide file tree
Showing 36 changed files with 81 additions and 259 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const val DEPOSIT_FUND_CLIENT_SECRET_2 = "SCW2SJEPTL4K7FFPFOFABFEFZJCG6LHULWVJX6

@TestInstance(PER_CLASS)
@Execution(CONCURRENT)
class Sep24End2EndTests : AbstractIntegrationTests(TestConfig(testProfileName = "default")) {
class Sep24End2EndTests : AbstractIntegrationTests(TestConfig()) {
private val client = HttpClient {
install(HttpTimeout) {
requestTimeoutMillis = 300000
Expand Down Expand Up @@ -95,7 +95,7 @@ class Sep24End2EndTests : AbstractIntegrationTests(TestConfig(testProfileName =
assertEquals(amount, (interactiveJwt.claims["data"] as Map<*, *>)["amount"], amount)

// Wait for the status to change to COMPLETED
waitForTxnStatus(response.id, COMPLETED, token)
waitForTxnStatus(response.id, COMPLETED, ERROR, token)

// Check if the transaction can be listed by stellar transaction id
val fetchedTxn = anchor.interactive().getTransaction(response.id, token) as DepositTransaction
Expand Down Expand Up @@ -202,7 +202,7 @@ class Sep24End2EndTests : AbstractIntegrationTests(TestConfig(testProfileName =
info("accessing ${withdrawTxn.url}...")
assertEquals(200, resp.status.value)
// Wait for the status to change to PENDING_USER_TRANSFER_START
waitForTxnStatus(withdrawTxn.id, PENDING_USER_TRANSFER_START, token)
waitForTxnStatus(withdrawTxn.id, PENDING_USER_TRANSFER_START, ERROR, token)
// Submit transfer transaction
val walletTxn =
(anchor.interactive().getTransaction(withdrawTxn.id, token) as WithdrawalTransaction)
Expand All @@ -218,7 +218,7 @@ class Sep24End2EndTests : AbstractIntegrationTests(TestConfig(testProfileName =
wallet.stellar().submitTransaction(transfer)
}
// Wait for the status to change to PENDING_USER_TRANSFER_END
waitForTxnStatus(withdrawTxn.id, COMPLETED, token)
waitForTxnStatus(withdrawTxn.id, COMPLETED, ERROR, token)

// Check if the transaction can be listed by stellar transaction id
val fetchTxn =
Expand Down Expand Up @@ -278,27 +278,26 @@ class Sep24End2EndTests : AbstractIntegrationTests(TestConfig(testProfileName =
private suspend fun waitForTxnStatus(
id: String,
expectedStatus: TransactionStatus,
exitStatus: TransactionStatus,
token: AuthToken
) {
var status: TransactionStatus? = null

for (i in 0..maxTries) {
// Get transaction info
val transaction = anchor.interactive().getTransaction(id, token)

if (status != transaction.status) {
status = transaction.status

info(
"Transaction(id=${transaction.id}) status changed to $status. Message: ${transaction.message}"
)
}

delay(1.seconds)
if (transaction.status == expectedStatus) return

if (transaction.status == expectedStatus) {
return
}
if (transaction.status == exitStatus) break

delay(1.seconds)
}

fail("Transaction wasn't $expectedStatus in $maxTries tries, last status: $status")
Expand Down Expand Up @@ -334,7 +333,7 @@ class Sep24End2EndTests : AbstractIntegrationTests(TestConfig(testProfileName =
val deposits =
(0..1).map {
val txnId = makeDeposit(asset, amount, token).id
waitForTxnStatus(txnId, COMPLETED, token)
waitForTxnStatus(txnId, COMPLETED, ERROR, token)
txnId
}
val history = anchor.interactive().getTransactionsForAsset(asset, token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import org.stellar.walletsdk.horizon.sign

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Execution(ExecutionMode.CONCURRENT)
class Sep6End2EndTest : AbstractIntegrationTests(TestConfig(testProfileName = "default")) {
class Sep6End2EndTest : AbstractIntegrationTests(TestConfig()) {
private val maxTries = 30
private val anchorReferenceServerClient =
AnchorReferenceServerClient(Url(config.env["reference.server.url"]!!))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import org.stellar.anchor.util.GsonUtils
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Execution(ExecutionMode.SAME_THREAD)
@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
class CallbackApiTests : AbstractIntegrationTests(TestConfig(testProfileName = "default")) {
class CallbackApiTests : AbstractIntegrationTests(TestConfig()) {

companion object {
private const val JWT_EXPIRATION_MILLISECONDS: Long = 10000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import org.stellar.anchor.platform.config.PropertyEventConfig
import org.stellar.anchor.platform.event.DefaultEventService
import org.stellar.anchor.util.GsonUtils

class EventProcessingServerTests :
AbstractIntegrationTests(TestConfig(testProfileName = "default")) {
class EventProcessingServerTests : AbstractIntegrationTests(TestConfig()) {
companion object {
val eventConfig =
GsonUtils.getInstance().fromJson(eventConfigJson, PropertyEventConfig::class.java)!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import org.stellar.anchor.platform.AbstractIntegrationTests
import org.stellar.anchor.platform.TestConfig
import org.stellar.anchor.util.GsonUtils

class PlatformApiTests : AbstractIntegrationTests(TestConfig(testProfileName = "default")) {
class PlatformApiTests : AbstractIntegrationTests(TestConfig()) {
private val gson = GsonUtils.getInstance()

private val platformApiClient =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.stellar.anchor.api.sep.sep10.ValidationRequest
import org.stellar.anchor.client.Sep10Client
import org.stellar.anchor.platform.*

class Sep10Tests : AbstractIntegrationTests(TestConfig(testProfileName = "default")) {
class Sep10Tests : AbstractIntegrationTests(TestConfig()) {
lateinit var sep10Client: Sep10Client
lateinit var sep10ClientMultiSig: Sep10Client

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import org.stellar.sdk.KeyPair
import org.stellar.walletsdk.anchor.auth
import org.stellar.walletsdk.horizon.SigningKeyPair

open class Sep12Tests : AbstractIntegrationTests(TestConfig(testProfileName = "default")) {
open class Sep12Tests : AbstractIntegrationTests(TestConfig()) {
init {
runBlocking {
// We have to override the default CLIENT_WALLET_SECRET because the deletion of the customer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import org.stellar.walletsdk.asset.IssuedAssetId
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Execution(SAME_THREAD)
@TestMethodOrder(OrderAnnotation::class)
class Sep24Tests : AbstractIntegrationTests(TestConfig(testProfileName = "default")) {
class Sep24Tests : AbstractIntegrationTests(TestConfig()) {
private val jwtService: JwtService =
JwtService(
config.env["secret.sep10.jwt_secret"]!!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ lateinit var savedTxn: Sep31GetTransactionResponse
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Execution(SAME_THREAD)
@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
class Sep31Tests : AbstractIntegrationTests(TestConfig(testProfileName = "default")) {
class Sep31Tests : AbstractIntegrationTests(TestConfig()) {
private val sep12Client: Sep12Client = Sep12Client(toml.getString("KYC_SERVER"), this.token.token)
private val sep31Client: Sep31Client =
Sep31Client(toml.getString("DIRECT_PAYMENT_SERVER"), this.token.token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.stellar.anchor.platform.TestConfig
import org.stellar.anchor.platform.printRequest
import org.stellar.anchor.platform.printResponse

class Sep38Tests : AbstractIntegrationTests(TestConfig(testProfileName = "default")) {
class Sep38Tests : AbstractIntegrationTests(TestConfig()) {
private val sep38Client: Sep38Client =
Sep38Client(toml.getString("ANCHOR_QUOTE_SERVER"), this.token.token)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.stellar.anchor.platform.TestConfig
import org.stellar.anchor.platform.gson
import org.stellar.anchor.util.Log

class Sep6Tests : AbstractIntegrationTests(TestConfig(testProfileName = "default")) {
class Sep6Tests : AbstractIntegrationTests(TestConfig()) {
private val sep6Client = Sep6Client(toml.getString("TRANSFER_SERVER"), token.token)
private val sep38Client = Sep38Client(toml.getString("ANCHOR_QUOTE_SERVER"), this.token.token)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.stellar.anchor.auth.AuthHelper
import org.stellar.anchor.platform.TestConfig

class PlatformServerHealthTests {
val config = TestConfig(testProfileName = "default")
val config = TestConfig()
private val platformApiClient =
PlatformApiClient(AuthHelper.forNone(), config.env["sep.server.url"]!!)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.stellar.anchor.platform.AbstractIntegrationTests
import org.stellar.anchor.platform.TestConfig
import org.stellar.anchor.platform.gson

class StellarObserverTests : AbstractIntegrationTests(TestConfig(testProfileName = "default")) {
class StellarObserverTests : AbstractIntegrationTests(TestConfig()) {
companion object {
const val OBSERVER_HEALTH_SERVER_PORT = 8083
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test

val PostgresConfig =
TestConfig("default").also {
TestConfig().also {
it.env[RUN_ALL_SERVERS] = "false"
it.env[RUN_SEP_SERVER] = "true"
it.env["data.flyway_enabled"] = "true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal class PlatformApiKeyAuthIntegrationTest : AbstractAuthIntegrationTest()
println("Running PlatformApiKeyAuthIntegrationTest")
testProfileRunner =
TestProfileExecutor(
TestConfig(testProfileName = "default").also {
TestConfig().also {
it.env[RUN_DOCKER] = "true"
it.env[RUN_ALL_SERVERS] = "false"
it.env[RUN_PLATFORM_SERVER] = "true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal class PlatformJwtAuthIntegrationTest : AbstractAuthIntegrationTest() {
println("Running PlatformJwtAuthIntegrationTest")
testProfileRunner =
TestProfileExecutor(
TestConfig(testProfileName = "default").also {
TestConfig().also {
it.env[RUN_DOCKER] = "true"
it.env[RUN_ALL_SERVERS] = "false"
it.env[RUN_PLATFORM_SERVER] = "true"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
package org.stellar.anchor.platform

data class TestConfig(var testProfileName: String) {
import org.stellar.anchor.util.StringHelper.isNotEmpty

/**
* TestConfig is a class that reads and merges env variables from the following files in the
* following order: The later files override the previous ones.
* - profiles/default/test.env
* - profiles/default/config.env
* - profiles/{testProfileName}/test.env (if testProfileName is not empty)
* - profiles/{testProfileName}/config.env (if testProfileName is not empty)
* - system env variables
*
* It also allows to override env variables with a custom function.
*
* @param testProfileName - name of the test profile to use. If null, the default test profile will
* be used.
* @param customize - a function that allows to override env variables
* @constructor creates a TestConfig instance
*/
class TestConfig {
val env = mutableMapOf<String, String>()
// override test profile name with TEST_PROFILE_NAME system env variable
private val profileName = System.getenv("TEST_PROFILE_NAME") ?: testProfileName
private val envProfileName = System.getenv("TEST_PROFILE_NAME") ?: null

constructor(testProfileName: String? = null, customize: () -> Unit = {}) {
var profileName = testProfileName
if (System.getenv("TEST_PROFILE_NAME") != null) profileName = System.getenv("TEST_PROFILE_NAME")

if (this.envProfileName != null) profileName = this.envProfileName
// starting from the default test.env file
env.putAll(readResourceAsMap("profiles/default/test.env"))
env.putAll(readResourceAsMap("profiles/default/config.env"))
// if test profile name is not "default", read test.env and config.env files
if (isNotEmpty(profileName) && !"default".equals(profileName, ignoreCase = true)) {
// read and merge test.env file
env.putAll(readResourceAsMap("profiles/${profileName}/test.env"))
// read and merge config.env file
env.putAll(readResourceAsMap("profiles/${profileName}/config.env"))
}

// customize env variables
customize()

init {
// read test.env file
val testEnv = readResourceAsMap("profiles/${profileName}/test.env")
// read config.env file
val configEnv = readResourceAsMap("profiles/${profileName}/config.env")
// read system env variables
val configSystem = readSystemEnvAsMap()
// merge test.env, config.env and system env variables
env.putAll(testEnv)
env.putAll(configEnv)
env.putAll(configSystem)
// read and merge system env variables
env.putAll(readSystemEnvAsMap())
}

private fun readSystemEnvAsMap(): Map<String, String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ lateinit var testProfileExecutor: TestProfileExecutor

fun main() = runBlocking {
info("Starting TestPfofileExecutor...")
testProfileExecutor = TestProfileExecutor(TestConfig(testProfileName = "default"))
testProfileExecutor = TestProfileExecutor(TestConfig())

launch {
Runtime.getRuntime()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlinx.coroutines.runBlocking
import org.stellar.anchor.platform.*

fun main() = runBlocking {
testProfileExecutor = TestProfileExecutor(TestConfig(testProfileName = "default"))
testProfileExecutor = TestProfileExecutor(TestConfig())
launch { registerShutdownHook(testProfileExecutor) }
testProfileExecutor.start(true) {
it.env[RUN_DOCKER] = "false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlinx.coroutines.runBlocking
import org.stellar.anchor.platform.*

fun main() = runBlocking {
testProfileExecutor = TestProfileExecutor(TestConfig(testProfileName = "default"))
testProfileExecutor = TestProfileExecutor(TestConfig())
launch { registerShutdownHook(testProfileExecutor) }
testProfileExecutor.start(true) {
it.env[RUN_DOCKER] = "true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import kotlinx.coroutines.runBlocking
import org.stellar.anchor.platform.*

fun main() = runBlocking {
testProfileExecutor = TestProfileExecutor(TestConfig(testProfileName = "default"))
testProfileExecutor = TestProfileExecutor(TestConfig())
launch { registerShutdownHook(testProfileExecutor) }
testProfileExecutor.start(true) {
it.env[RUN_DOCKER] = "true"
Expand Down
Loading

0 comments on commit 58c4c32

Please sign in to comment.