Skip to content

Commit

Permalink
Refactor environment variables for integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anti-social committed Mar 24, 2023
1 parent 60761bc commit 1f54639
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
package dev.evo.elasticmagic

import dev.evo.elasticmagic.transport.Auth

import io.ktor.client.engine.HttpClientEngine

const val DEFAULT_ELASTIC_URL = "http://localhost:9200"
const val DEFAULT_ELASTIC_USER = "elastic"

expect val elasticUrl: String

expect val httpEngine: HttpClientEngine

expect val elasticAuth: Auth?
24 changes: 0 additions & 24 deletions integ-tests/src/jsTest/kotlin/dev/evo/elasticmagic/engineJs.kt
Original file line number Diff line number Diff line change
@@ -1,30 +1,6 @@
package dev.evo.elasticmagic

import dev.evo.elasticmagic.transport.Auth

import io.ktor.client.engine.HttpClientEngine
import io.ktor.client.engine.js.Js

external val process: Process

external interface Process {
val env: ProcessEnvVariables
}

external object ProcessEnvVariables {
val ELASTIC_URL: String?
val ELASTIC_USER: String?
val ELASTIC_PASSWORD: String?
}

actual val elasticUrl: String = process.env.ELASTIC_URL ?: DEFAULT_ELASTIC_URL

actual val httpEngine: HttpClientEngine = Js.create {}

actual val elasticAuth: Auth? = when (val elasticPassword = process.env.ELASTIC_PASSWORD) {
null -> null
else -> Auth.Basic(
process.env.ELASTIC_USER ?: DEFAULT_ELASTIC_USER,
elasticPassword
)
}
3 changes: 3 additions & 0 deletions integ-tests/src/jsTest/kotlin/dev/evo/elasticmagic/envJs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dev.evo.elasticmagic

actual fun getenv(name: String): String? = js("process.env[name]") as String?
12 changes: 0 additions & 12 deletions integ-tests/src/jvmTest/kotlin/dev/evo/elasticmagic/engineJvm.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package dev.evo.elasticmagic

import dev.evo.elasticmagic.transport.Auth

import io.ktor.client.engine.HttpClientEngine
import io.ktor.client.engine.cio.CIO

import java.security.cert.X509Certificate
import javax.net.ssl.X509TrustManager

actual val elasticUrl: String = System.getenv("ELASTIC_URL") ?: DEFAULT_ELASTIC_URL

actual val httpEngine: HttpClientEngine = CIO.create {
https {
trustManager = object: X509TrustManager {
Expand All @@ -25,11 +21,3 @@ actual val httpEngine: HttpClientEngine = CIO.create {
}
}
}

actual val elasticAuth: Auth? = when (val elasticPassword = System.getenv("ELASTIC_PASSWORD")) {
null -> null
else -> Auth.Basic(
System.getenv("ELASTIC_USER") ?: DEFAULT_ELASTIC_USER,
elasticPassword
)
}
3 changes: 3 additions & 0 deletions integ-tests/src/jvmTest/kotlin/dev/evo/elasticmagic/envJvm.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dev.evo.elasticmagic

actual fun getenv(name: String): String? = System.getenv(name)
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
package dev.evo.elasticmagic

import dev.evo.elasticmagic.transport.Auth

import io.ktor.client.engine.HttpClientEngine
import io.ktor.client.engine.curl.Curl

import kotlinx.cinterop.toKString

import platform.posix.getenv

actual val elasticUrl: String = getenv("ELASTIC_URL")?.toKString() ?: DEFAULT_ELASTIC_URL

actual val httpEngine: HttpClientEngine = Curl.create {
sslVerify = false
}

actual val elasticAuth: Auth? = when (val elasticPassword = getenv("ELASTIC_PASSWORD")?.toKString()) {
null -> null
else -> Auth.Basic(
getenv("ELASTIC_USER")?.toKString() ?: DEFAULT_ELASTIC_USER,
elasticPassword
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.evo.elasticmagic

import kotlinx.cinterop.toKString

actual fun getenv(name: String): String? = platform.posix.getenv(name)?.toKString()

0 comments on commit 1f54639

Please sign in to comment.