Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,20 @@ jobs:
run: SBT_OPTS="-Xss4M -Xms1g -Xmx4g -Dfile.encoding=UTF-8" sbt + compile
- name: log4j closure guard
run: SBT_OPTS="-Xss4M -Xms1g -Xmx4g -Dfile.encoding=UTF-8" sbt checkLog4j
- name: Runner info
run: |
lscpu
nproc
free -m
docker info
- name: Run tests
run: SBT_OPTS="-Xss4M -Xms1g -Xmx4g -Dfile.encoding=UTF-8" sbt compile test
- name: Upload test reports
if: always()
uses: actions/upload-artifact@v4
with:
name: test-reports
path: '**/target/test-reports/*.xml'

lint:
runs-on: ubuntu-latest
Expand Down
12 changes: 12 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,20 @@ jobs:
run: sbt scalafmtSbtCheck scalafmtCheck test:scalafmtCheck
- name: Set vm.max_map_count
run: sudo sysctl -w vm.max_map_count=262144
- name: Runner info
run: |
lscpu
nproc
free -m
docker info
- name: Run tests & Coverage Report
run: SBT_OPTS="-Xss4M -Xms1g -Xmx4g -Dfile.encoding=UTF-8" sbt compile coverage test coverageReport coverageAggregate
- name: Upload test reports
if: always()
uses: actions/upload-artifact@v4
with:
name: test-reports
path: '**/target/test-reports/*.xml'
- name: log4j closure guard
run: SBT_OPTS="-Xss4M -Xms1g -Xmx4g -Dfile.encoding=UTF-8" sbt checkLog4j
- name: Upload coverage to Codecov
Expand Down
62 changes: 62 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,68 @@ class ElasticClientIntegrationSpec
}
```

#### **One Elasticsearch container per test JVM**

The five client modules (`es6rest`, `es6jest`, `es7rest`, `es8java`, `es9java`) set
`-Dsoftclient4es.testkit.shared-container=true` in their `Test / javaOptions`. With it,
`ElasticDockerTestKit` starts **one container per forked test JVM and per configuration** (image
tag + rendered `elasticsearch.yml`), instead of one per suite:

- before each suite, the container is **reset**: every non-dot index, legacy / index / component
template, ingest pipeline, stored script and enrich policy created since the container booted is
deleted (plus users, roles and API keys on a security-enabled configuration). Objects
Elasticsearch owns (names starting with `.` or holding `@`, `_meta.managed: true`, reserved users
and roles, anything present at boot) are never touched;
- a change to watchers, transforms, cluster settings or the licence is **refused**: the reset
deletes nothing and throws, naming the suite after which the change was first seen. Restore such a
change in the suite that makes it (in a `finally`);
- after each suite, a leak report lists what the suite left.

Without the property (the default, e.g. in a downstream repository that has not opted in), every
suite starts and stops a container of its own, as before.

⚠️ **Suites must run serially in the test JVM** (sbt's default for a forked test group; never enable
`testForkedParallel`): a reset deletes the indices another suite would be using.

A suite that asserts on the **whole cluster** keeps a container of its own by overriding
`sharedContainer` from `ElasticContainerSharing`:

```scala
trait MyClusterWideSpec extends AnyFlatSpecLike with ElasticContainerSharing {
self: ElasticTestKit =>
override def sharedContainer: Boolean = false
}
```

`GatewayApiIntegrationSpec` and `ReplGatewayIntegrationSpec` do so. An override of
`elasticContainer` (e.g. a basic-licence container) also keeps the per-suite lifecycle. To reset an
additional kind of object, override `resetSteps` (default `ResetStep.defaults`) with an extra
`ResetStep`.

Log lines (stdout), one per event:

| Line | When | Main fields |
|------------------|----------------------------------------------|-----------------------------------------------------------------|
| `[es-container]` | every container start, shared or not | `boot`, `licence`, `key`, `shared`, `module`, `suite`, `id`, `live` |
| `[es-reset]` | before each suite on a shared container | `suite`, `key`, `took`, `deleted`, `previous` |
| `[es-leaks]` | after each suite on a shared container | `left`, `open_contexts`, `scroll_current`, `script_compilations`, `compilation_limit_triggered` |

Run a module as a downstream repository does (property absent). The `*ResetSpec` control suites
fail by design on an unshared container, so filter them out:

```bash
sbt 'set es8java / Test / javaOptions -= "-Dsoftclient4es.testkit.shared-container=true"' \
'set es8java / Test / testOptions += Tests.Filter(n => !n.endsWith("ResetSpec"))' \
"es8java/test"
```

Run a module's suites in reversed order (a suite that passes only after another one shows up):

```bash
sbt 'set es8java / Test / testGrouping := (es8java / Test / testGrouping).value.map(g => g.withTests(g.tests.reverse))' \
"es8java/test"
```

### **Test Coverage**

✅ **DO**: Maintain high test coverage
Expand Down
7 changes: 7 additions & 0 deletions es6/jest/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,12 @@ val testJavaOptions = {

Test / javaOptions ++= testJavaOptions

// One Elasticsearch container per forked test JVM and per configuration, reset before each suite
// (ElasticDockerTestKit / SharedElasticContainers). Without this property every suite boots its own.
Test / javaOptions += "-Dsoftclient4es.testkit.shared-container=true"

// Per-test durations in the log
Test / testOptions += Tests.Argument(TestFrameworks.ScalaTest, "-oD")

// Required by the Test container framework
Test / fork := true
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2025 SOFTNETWORK
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.softnetwork.elastic.client

class JestSharedContainerResetSpec extends SharedContainerResetSpec
7 changes: 7 additions & 0 deletions es6/rest/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,12 @@ val testJavaOptions = {

Test / javaOptions ++= testJavaOptions

// One Elasticsearch container per forked test JVM and per configuration, reset before each suite
// (ElasticDockerTestKit / SharedElasticContainers). Without this property every suite boots its own.
Test / javaOptions += "-Dsoftclient4es.testkit.shared-container=true"

// Per-test durations in the log
Test / testOptions += Tests.Argument(TestFrameworks.ScalaTest, "-oD")

// Required by the Test container framework
Test / fork := true
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2025 SOFTNETWORK
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.softnetwork.elastic.client

class RestHighLevelClientSharedContainerResetSpec extends SharedContainerResetSpec
7 changes: 7 additions & 0 deletions es7/rest/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,12 @@ val testJavaOptions = {

Test / javaOptions ++= testJavaOptions

// One Elasticsearch container per forked test JVM and per configuration, reset before each suite
// (ElasticDockerTestKit / SharedElasticContainers). Without this property every suite boots its own.
Test / javaOptions += "-Dsoftclient4es.testkit.shared-container=true"

// Per-test durations in the log
Test / testOptions += Tests.Argument(TestFrameworks.ScalaTest, "-oD")

// Required by the Test container framework
Test / fork := true
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2025 SOFTNETWORK
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.softnetwork.elastic.client

class RestHighLevelClientSharedContainerResetSpec extends SharedContainerResetSpec
7 changes: 7 additions & 0 deletions es8/java/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,12 @@ val testJavaOptions = {

Test / javaOptions ++= testJavaOptions

// One Elasticsearch container per forked test JVM and per configuration, reset before each suite
// (ElasticDockerTestKit / SharedElasticContainers). Without this property every suite boots its own.
Test / javaOptions += "-Dsoftclient4es.testkit.shared-container=true"

// Per-test durations in the log
Test / testOptions += Tests.Argument(TestFrameworks.ScalaTest, "-oD")

// Required by the Test container framework
Test / fork := true
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2025 SOFTNETWORK
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.softnetwork.elastic.client

class JavaClientSharedContainerResetSpec extends SharedContainerResetSpec
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* Copyright 2025 SOFTNETWORK
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.softnetwork.elastic.client

import app.softnetwork.elastic.scalatest.{ElasticDockerTestKit, ResetStep, SharedElasticContainers}
import org.apache.http.entity.ContentType
import org.apache.http.message.BasicHeader
import org.apache.http.nio.entity.NStringEntity
import org.apache.http.util.EntityUtils
import org.apache.http.{Header, HttpHost}
import org.elasticsearch.client.{Request, ResponseException, RestClient}
import org.json4s.jackson.JsonMethods.parse
import org.json4s.{JArray, JBool, JString}
import org.scalatest.flatspec.AnyFlatSpecLike
import org.scalatest.matchers.should.Matchers
import org.slf4j.{Logger, LoggerFactory}

import _root_.java.nio.charset.StandardCharsets
import _root_.java.util.Base64

/** The control of the shared-container reset on the SECURE es8 key (the one
* `JavaClientReadOnlyUserSpec` and `JavaClientFactoryIdentitySpec` share): a user, a role and an
* API key created after the snapshot are deleted or invalidated by `reset`, while the reserved
* `elastic` user and the built-in `superuser` role remain.
*
* It FAILS (never cancels) when its container is not shared: run without the
* `softclient4es.testkit.shared-container` property, it is filtered out (`*ResetSpec`).
*/
class JavaClientSharedSecureResetSpec
extends AnyFlatSpecLike
with ElasticDockerTestKit
with Matchers {

lazy val log: Logger = LoggerFactory.getLogger(getClass.getName)

override lazy val xpackSecurityEnabled: Boolean = true

// the password Testcontainers sets for ES >= 8 (ElasticsearchContainer.ELASTICSEARCH_DEFAULT_PASSWORD)
private def basic(user: String, password: String): String =
"Basic " + Base64.getEncoder.encodeToString(s"$user:$password".getBytes(StandardCharsets.UTF_8))

override lazy val restClient: RestClient =
RestClient
.builder(HttpHost.create(elasticURL))
.setDefaultHeaders(
Array[Header](new BasicHeader("Authorization", basic("elastic", "changeme")))
)
.build()

private def http(method: String, path: String, body: Option[String] = None): (Int, String) = {
val request = new Request(method, path)
body.foreach(b => request.setEntity(new NStringEntity(b, ContentType.APPLICATION_JSON)))
try {
val response = restClient.performRequest(request)
(
response.getStatusLine.getStatusCode,
Option(response.getEntity).map(EntityUtils.toString).getOrElse("")
)
} catch {
case e: ResponseException =>
(
e.getResponse.getStatusLine.getStatusCode,
Option(e.getResponse.getEntity).map(EntityUtils.toString).getOrElse("")
)
}
}

private def ok(call: (Int, String)): Unit =
withClue(s"set-up call answered ${call._1}: ${call._2.take(300)}: ")(call._1 should be < 300)

private val Role = "ci1_reset_role"
private val User = "ci1_reset_user"

"The secure shared-container reset" should "run on a shared container" in {
SharedElasticContainers.isShared(elasticContainer) shouldBe true
}

it should "delete the users and roles, and invalidate the API keys, created after the snapshot" in {
val t = SharedElasticContainers
.targetOf(elasticContainer)
.getOrElse(fail("this suite's container is not shared"))
t.security shouldBe true
ok(
http(
"PUT",
s"/_security/role/$Role",
Some("""{"indices":[{"names":["ci1_*"],"privileges":["read"]}]}""")
)
)
ok(
http(
"PUT",
s"/_security/user/$User",
Some(s"""{"password":"ci1-reset-pass","roles":["$Role"]}""")
)
)
val (status, created) = http("POST", "/_security/api_key", Some("""{"name":"ci1-reset-key"}"""))
ok((status, created))
val keyId = parse(created) \ "id" match {
case JString(id) => id
case other => fail(s"no API key id in $other")
}

def listed(step: ResetStep): Set[String] =
step.listed(SharedElasticContainers.list(restClient, step, t), t)

// the steps SEE every planted object (a parse that missed one would pass vacuously below)
listed(ResetStep.Users) should contain(User)
listed(ResetStep.Roles) should contain(Role)
listed(ResetStep.ApiKeys) should contain(keyId)

try {
SharedElasticContainers.reset(elasticContainer, suiteName, resetSteps)

http("GET", s"/_security/user/$User")._1 shouldBe 404
http("GET", s"/_security/role/$Role")._1 shouldBe 404
val (keyStatus, key) = http("GET", s"/_security/api_key?id=$keyId")
keyStatus shouldBe 200
parse(key) \ "api_keys" match {
case JArray(List(k)) => (k \ "invalidated") shouldBe JBool(true)
case other => fail(s"API key $keyId not listed once: $other")
}
listed(ResetStep.ApiKeys) should not contain keyId

// reserved objects are never touched
http("GET", "/_security/user/elastic")._1 shouldBe 200
http("GET", "/_security/role/superuser")._1 shouldBe 200
} finally {
// an in-test reset deletes `all_templates`, which `ElasticTestKit.beforeAll` created
ok(
http(
"PUT",
"/_template/all_templates",
Some(
"""{"index_patterns":["*"],"settings":{"number_of_shards":1,"number_of_replicas":0}}"""
)
)
)
}
}
}
Loading
Loading