Skip to content

Commit

Permalink
add github workflow to run kotlin tests against docker
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderwiederin committed Nov 8, 2023
1 parent 476446f commit 52c9694
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 17 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/kotlin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Continuous Integration Checks - Kotlin

on: [push, pull_request]

jobs:
check-kotlin:
runs-on: ubuntu-latest

env:
LDK_NODE_JVM_DIR: bindings/kotlin/ldk-node-jvm
LDK_NODE_ANDROID_DIR: bindings/kotlin/ldk-node-android

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up JDK
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 11

- name: Run ktlintCheck on ldk-node-jvm
run: |
cd $LDK_NODE_JVM_DIR
./gradlew ktlintCheck
- name: Run ktlintCheck on ldk-node-android
run: |
cd $LDK_NODE_ANDROID_DIR
./gradlew ktlintCheck
- name: Generate Kotlin JVM
run: ./scripts/uniffi_bindgen_generate_kotlin.sh

- name: Start bitcoind and electrs
run: docker compose up -d

- name: Run ldk-node-jvm tests
run: |
cd $LDK_NODE_JVM_DIR
./gradlew test -Penv=ci
- name: Run ldk-node-android tests
run: |
cd $LDK_NODE_ANDROID_DIR
./gradlew test
17 changes: 17 additions & 0 deletions bindings/kotlin/ldk-node-jvm/lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ tasks.named<Test>("test") {
}
}

tasks.test {
doFirst {
if (project.hasProperty("env") && project.property("env") == "ci") {
environment("BITCOIN_CLI_BIN", "docker exec ldk-node-bitcoin-1 bitcoin-cli")
environment("BITCOIND_RPC_USER", "user")
environment("BITCOIND_RPC_PASSWORD", "pass")
environment("ESPLORA_ENDPOINT", "http://127.0.0.1:3002")
} else {
// Adapt these to your local environment
environment("BITCOIN_CLI_BIN", "bitcoin-cli")
environment("BITCOIND_RPC_USER", "")
environment("BITCOIND_RPC_PASSWORD", "")
environment("ESPLORA_ENDPOINT", "http://127.0.0.1:3002")
}
}
}

afterEvaluate {
publishing {
publications {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.lightningdevkit.ldknode

import kotlin.UInt
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.io.path.createTempDirectory
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
Expand All @@ -23,9 +22,26 @@ fun runCommandAndWait(vararg cmd: String): String {
return stdout + stderr
}

fun bitcoinCli(vararg cmd: String): String {
val bitcoinCliBin = System.getenv("BITCOIN_CLI_BIN")?.split(" ") ?: listOf("bitcoin-cli")
val bitcoinDRpcUser = System.getenv("BITCOIND_RPC_USER") ?: ""
val bitcoinDRpcPassword = System.getenv("BITCOIND_RPC_PASSWORD") ?: ""

val baseCommand = bitcoinCliBin + "-regtest"

val rpcAuth = if (bitcoinDRpcUser.isNotBlank() && bitcoinDRpcPassword.isNotBlank()) {
listOf("-rpcuser=$bitcoinDRpcUser", "-rpcpassword=$bitcoinDRpcPassword")
} else {
emptyList()
}

val fullCommand = baseCommand + rpcAuth + cmd.toList()
return runCommandAndWait(*fullCommand.toTypedArray())
}

fun mine(blocks: UInt): String {
val address = runCommandAndWait("bitcoin-cli", "-regtest", "getnewaddress")
val output = runCommandAndWait("bitcoin-cli", "-regtest", "generatetoaddress", blocks.toString(), address)
val address = bitcoinCli("getnewaddress")
val output = bitcoinCli("generatetoaddress", blocks.toString(), address)
println("Mining output: $output")
val re = Regex("\n.+\n\\]$")
val lastBlock = re.find(output)!!.value.replace("]", "").replace("\"", "").replace("\n", "").trim()
Expand All @@ -40,17 +56,10 @@ fun mineAndWait(esploraEndpoint: String, blocks: UInt) {

fun sendToAddress(address: String, amountSats: UInt): String {
val amountBtc = amountSats.toDouble() / 100000000.0
val output = runCommandAndWait("bitcoin-cli", "-regtest", "sendtoaddress", address, amountBtc.toString())
val output = bitcoinCli("sendtoaddress", address, amountBtc.toString())
return output
}

fun setup() {
runCommandAndWait("bitcoin-cli", "-regtest", "createwallet", "ldk_node_test")
runCommandAndWait("bitcoin-cli", "-regtest", "loadwallet", "ldk_node_test", "true")
mine(101u)
Thread.sleep(5_000)
}

fun waitForTx(esploraEndpoint: String, txid: String) {
var esploraPickedUpTx = false
val re = Regex("\"txid\":\"$txid\"")
Expand Down Expand Up @@ -83,11 +92,20 @@ fun waitForBlock(esploraEndpoint: String, blockHash: String) {
}
}

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class LibraryTest {
@Test fun fullCycle() {
val esploraEndpoint = "http://127.0.0.1:3002"
setup()

val esploraEndpoint = System.getenv("ESPLORA_ENDPOINT")

@BeforeAll
fun setup() {
bitcoinCli("createwallet", "ldk_node_test")
bitcoinCli("loadwallet", "ldk_node_test", "true")
mine(101u)
Thread.sleep(5_000)
}

@Test fun fullCycle() {
val tmpDir1 = createTempDirectory("ldk_node").toString()
println("Random dir 1: $tmpDir1")
val tmpDir2 = createTempDirectory("ldk_node").toString()
Expand Down
53 changes: 53 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
version: '3'

services:
bitcoin:
image: blockstream/bitcoind:24.1
platform: linux/amd64
command:
[
"bitcoind",
"-printtoconsole",
"-regtest=1",
"-rpcallowip=0.0.0.0/0",
"-rpcbind=0.0.0.0",
"-rpcuser=user",
"-rpcpassword=pass",
"-fallbackfee=0.00001"
]
ports:
- "18443:18443" # Regtest RPC port
- "18444:18444" # Regtest P2P port
networks:
- bitcoin-electrs
healthcheck:
test: ["CMD", "bitcoin-cli", "-regtest", "-rpcuser=user", "-rpcpassword=pass", "getblockchaininfo"]
interval: 5s
timeout: 10s
retries: 5

electrs:
image: blockstream/esplora:electrs-cd9f90c115751eb9d2bca9a4da89d10d048ae931
platform: linux/amd64
depends_on:
bitcoin:
condition: service_healthy
command:
[
"/app/electrs_bitcoin/bin/electrs",
"-vvvv",
"--timestamp",
"--jsonrpc-import",
"--cookie=user:pass",
"--network=regtest",
"--daemon-rpc-addr=bitcoin:18443",
"--http-addr=0.0.0.0:3002"
]
ports:
- "3002:3002"
networks:
- bitcoin-electrs

networks:
bitcoin-electrs:
driver: bridge

0 comments on commit 52c9694

Please sign in to comment.