diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index cf295a184..8942b13ee 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index f18770425..935524eae 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1f97ee353..ede42ac2e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 diff --git a/es6/jest/build.sbt b/es6/jest/build.sbt index f67387131..834867b9c 100644 --- a/es6/jest/build.sbt +++ b/es6/jest/build.sbt @@ -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 diff --git a/es6/jest/src/test/scala/app/softnetwork/elastic/client/JestSharedContainerResetSpec.scala b/es6/jest/src/test/scala/app/softnetwork/elastic/client/JestSharedContainerResetSpec.scala new file mode 100644 index 000000000..af1581131 --- /dev/null +++ b/es6/jest/src/test/scala/app/softnetwork/elastic/client/JestSharedContainerResetSpec.scala @@ -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 diff --git a/es6/rest/build.sbt b/es6/rest/build.sbt index 0b7ce6ee8..08b625a0f 100644 --- a/es6/rest/build.sbt +++ b/es6/rest/build.sbt @@ -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 diff --git a/es6/rest/src/test/scala/app/softnetwork/elastic/client/RestHighLevelClientSharedContainerResetSpec.scala b/es6/rest/src/test/scala/app/softnetwork/elastic/client/RestHighLevelClientSharedContainerResetSpec.scala new file mode 100644 index 000000000..59c9b9827 --- /dev/null +++ b/es6/rest/src/test/scala/app/softnetwork/elastic/client/RestHighLevelClientSharedContainerResetSpec.scala @@ -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 diff --git a/es7/rest/build.sbt b/es7/rest/build.sbt index 0b7ce6ee8..08b625a0f 100644 --- a/es7/rest/build.sbt +++ b/es7/rest/build.sbt @@ -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 diff --git a/es7/rest/src/test/scala/app/softnetwork/elastic/client/RestHighLevelClientSharedContainerResetSpec.scala b/es7/rest/src/test/scala/app/softnetwork/elastic/client/RestHighLevelClientSharedContainerResetSpec.scala new file mode 100644 index 000000000..59c9b9827 --- /dev/null +++ b/es7/rest/src/test/scala/app/softnetwork/elastic/client/RestHighLevelClientSharedContainerResetSpec.scala @@ -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 diff --git a/es8/java/build.sbt b/es8/java/build.sbt index b8c68406c..35ddce3ac 100644 --- a/es8/java/build.sbt +++ b/es8/java/build.sbt @@ -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 diff --git a/es8/java/src/test/scala/app/softnetwork/elastic/client/JavaClientSharedContainerResetSpec.scala b/es8/java/src/test/scala/app/softnetwork/elastic/client/JavaClientSharedContainerResetSpec.scala new file mode 100644 index 000000000..3ae1e6189 --- /dev/null +++ b/es8/java/src/test/scala/app/softnetwork/elastic/client/JavaClientSharedContainerResetSpec.scala @@ -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 diff --git a/es8/java/src/test/scala/app/softnetwork/elastic/client/JavaClientSharedSecureResetSpec.scala b/es8/java/src/test/scala/app/softnetwork/elastic/client/JavaClientSharedSecureResetSpec.scala new file mode 100644 index 000000000..7c329d7a8 --- /dev/null +++ b/es8/java/src/test/scala/app/softnetwork/elastic/client/JavaClientSharedSecureResetSpec.scala @@ -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}}""" + ) + ) + ) + } + } +} diff --git a/es9/java/build.sbt b/es9/java/build.sbt index 58a970d01..21f3e0230 100644 --- a/es9/java/build.sbt +++ b/es9/java/build.sbt @@ -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 diff --git a/es9/java/src/test/scala/app/softnetwork/elastic/client/JavaClientSharedContainerResetSpec.scala b/es9/java/src/test/scala/app/softnetwork/elastic/client/JavaClientSharedContainerResetSpec.scala new file mode 100644 index 000000000..3ae1e6189 --- /dev/null +++ b/es9/java/src/test/scala/app/softnetwork/elastic/client/JavaClientSharedContainerResetSpec.scala @@ -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 diff --git a/testkit/src/main/scala/app/softnetwork/elastic/client/GatewayApiIntegrationSpec.scala b/testkit/src/main/scala/app/softnetwork/elastic/client/GatewayApiIntegrationSpec.scala index e06d0be15..80ec0c5da 100644 --- a/testkit/src/main/scala/app/softnetwork/elastic/client/GatewayApiIntegrationSpec.scala +++ b/testkit/src/main/scala/app/softnetwork/elastic/client/GatewayApiIntegrationSpec.scala @@ -18,7 +18,7 @@ package app.softnetwork.elastic.client import app.softnetwork.elastic.client.result.{DdlResult, DmlResult, ElasticSuccess} import app.softnetwork.elastic.client.spi.ElasticClientSpi -import app.softnetwork.elastic.scalatest.ElasticTestKit +import app.softnetwork.elastic.scalatest.{ElasticContainerSharing, ElasticTestKit} import app.softnetwork.elastic.sql.{DoubleValue, IdValue} import app.softnetwork.elastic.sql.`type`.SQLTypes import app.softnetwork.elastic.sql.health.HealthStatus @@ -36,9 +36,16 @@ import java.time.{LocalDate, ZoneOffset} // Base test trait — to be mixed with ElasticDockerTestKit // --------------------------------------------------------------------------- -trait GatewayApiIntegrationSpec extends GatewayIntegrationTestKit { +trait GatewayApiIntegrationSpec extends GatewayIntegrationTestKit with ElasticContainerSharing { self: ElasticTestKit => + /** Opted out of the shared container: this suite asserts on the WHOLE cluster (no index starting + * with `.` in `SHOW TABLES LIKE '.%'`, exactly one enrich policy in `SHOW ENRICH POLICIES`) and + * shares about 25 literal object names with `ReplGatewayIntegrationSpec`, so it keeps a + * container of its own. + */ + override def sharedContainer: Boolean = false + // ------------------------------------------------------------------------- // SHOW / DESCRIBE TABLE tests // ------------------------------------------------------------------------- diff --git a/testkit/src/main/scala/app/softnetwork/elastic/client/SharedContainerResetSpec.scala b/testkit/src/main/scala/app/softnetwork/elastic/client/SharedContainerResetSpec.scala new file mode 100644 index 000000000..c4158025d --- /dev/null +++ b/testkit/src/main/scala/app/softnetwork/elastic/client/SharedContainerResetSpec.scala @@ -0,0 +1,280 @@ +/* + * 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.{ + DeleteNew, + ElasticDockerTestKit, + ResetRefusedException, + ResetStep, + ResetTarget, + SharedElasticContainers +} +import org.apache.http.entity.ContentType +import org.apache.http.nio.entity.NStringEntity +import org.apache.http.util.EntityUtils +import org.elasticsearch.client.{Request, ResponseException, RestClient} +import org.scalatest.flatspec.AnyFlatSpecLike +import org.scalatest.matchers.should.Matchers +import org.slf4j.{Logger, LoggerFactory} + +/** The control of the shared-container reset on an OPEN key, one runner per client module. + * + * - one object of each deletable kind the major supports, planted after the snapshot, is gone + * after `reset`, while every object the reset must keep (system objects, the snapshot) stays; + * - a persistent cluster setting makes `reset` refuse, naming the suite after which it was first + * seen, and deleting nothing; once the setting is restored, `reset` passes. + * + * It FAILS (never cancels) when its container is not shared: run without the + * `softclient4es.testkit.shared-container` property, it is filtered out (`*ResetSpec`). + */ +trait SharedContainerResetSpec extends AnyFlatSpecLike with ElasticDockerTestKit with Matchers { + + lazy val log: Logger = LoggerFactory.getLogger(getClass.getName) + + private def http( + client: RestClient, + 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 = client.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 def target: ResetTarget = + SharedElasticContainers + .targetOf(elasticContainer) + .getOrElse(fail("this suite's container is not shared")) + + /** (step, raw names, names the reset may delete) per applicable deletable step. */ + private def lists(t: ResetTarget): Map[String, (Set[String], Set[String])] = + resetSteps + .filter(s => s.action == DeleteNew && s.applies(t)) + .map { s => + val body = SharedElasticContainers.list(restClient, s, t) + s.name -> ((s.listed(body, t), s.parse(body, t))) + } + .toMap + + /** `all_templates`, as `ElasticTestKit.beforeAll` creates it: an in-test reset deletes it. */ + private def restoreAllTemplates(): Unit = + ok( + http( + restClient, + "PUT", + "/_template/all_templates", + Some( + """{"index_patterns":["*"],"settings":{"number_of_shards":1,"number_of_replicas":0}}""" + ) + ) + ) + + private val Index = "ci1_reset_idx" + private val Source = "ci1_reset_src" + private val Legacy = "ci1_reset_legacy" + private val IndexTemplate = "ci1_reset_it" + private val Component = "ci1_reset_ct" + private val Script = "ci1_reset_script" + private val Pipeline = "ci1_reset_pipeline" + private val EnrichPipeline = "ci1_reset_enrich_pipeline" + private val Policy = "ci1_reset_policy" + + /** One object per deletable kind the major supports, as (step, name). */ + private def plant(t: ResetTarget): Seq[(String, String)] = { + val c = restClient + val v = t.version + val composable = ResetStep.IndexTemplates.applies(t) + val enrich = ResetStep.EnrichPolicies.applies(t) + val idMapping = + if (ElasticsearchVersion.isEs6(v)) + """{"_doc":{"properties":{"id":{"type":"keyword"},"name":{"type":"keyword"}}}}""" + else """{"properties":{"id":{"type":"keyword"},"name":{"type":"keyword"}}}""" + ok(http(c, "PUT", s"/$Index")) + ok(http(c, "PUT", s"/$Source", Some(s"""{"mappings":$idMapping}"""))) + ok(http(c, "PUT", s"/$Source/_doc/1?refresh=true", Some("""{"id":"1","name":"one"}"""))) + ok( + http( + c, + "PUT", + s"/_template/$Legacy", + Some("""{"index_patterns":["ci1_reset_legacy_*"],"settings":{"number_of_shards":1}}""") + ) + ) + if (composable) { + ok( + http( + c, + "PUT", + s"/_component_template/$Component", + Some("""{"template":{"settings":{"number_of_replicas":0}}}""") + ) + ) + ok( + http( + c, + "PUT", + s"/_index_template/$IndexTemplate", + Some(s"""{"index_patterns":["ci1_reset_it_*"],"composed_of":["$Component"]}""") + ) + ) + } + ok( + http( + c, + "PUT", + s"/_scripts/$Script", + Some("""{"script":{"lang":"painless","source":"ctx.ci1 = 1"}}""") + ) + ) + // the pipeline names the stored script: pipelines are deleted before stored scripts + ok( + http( + c, + "PUT", + s"/_ingest/pipeline/$Pipeline", + Some(s"""{"processors":[{"script":{"id":"$Script"}}]}""") + ) + ) + if (enrich) { + ok( + http( + c, + "PUT", + s"/_enrich/policy/$Policy", + Some( + s"""{"match":{"indices":"$Source","match_field":"id","enrich_fields":["name"]}}""" + ) + ) + ) + ok(http(c, "POST", s"/_enrich/policy/$Policy/_execute?wait_for_completion=true")) + // a policy a pipeline still uses cannot be deleted: pipelines go before enrich policies + ok( + http( + c, + "PUT", + s"/_ingest/pipeline/$EnrichPipeline", + Some( + s"""{"processors":[{"enrich":{"policy_name":"$Policy","field":"id","target_field":"e"}}]}""" + ) + ) + ) + } + Seq( + "indices" -> Index, + "indices" -> Source, + "legacy_templates" -> Legacy, + "pipelines" -> Pipeline, + "stored_scripts" -> Script + ) ++ + (if (composable) + Seq("index_templates" -> IndexTemplate, "component_templates" -> Component) + else Nil) ++ + (if (enrich) Seq("enrich_policies" -> Policy, "pipelines" -> EnrichPipeline) else Nil) + } + + "The shared-container reset" should "run on a shared container" in { + SharedElasticContainers.isShared(elasticContainer) shouldBe true + } + + it should "delete one object of every deletable kind and keep what it must keep" in { + val t = target + val snapshot = SharedElasticContainers.snapshotOf(elasticContainer).getOrElse(Map.empty) + val planted = plant(t) + val before = lists(t) + // the steps SEE every planted object (a parse that missed one would pass vacuously below) + planted.foreach { case (step, name) => + withClue(s"$step before the reset: ")(before(step)._1 should contain(name)) + } + // what the reset must keep: every listed object it may not delete (system objects, snapshot), + // but for what the planted objects derive (the policy's `.enrich-ci1_reset_policy-*` index) + val kept = before.map { case (step, (raw, deletable)) => + step -> (raw -- (deletable -- snapshot.getOrElse(step, Set.empty[String]))) + .filterNot(_.contains("ci1_reset")) + } + withClue("no system object was listed: the keep check below would be vacuous: ") { + kept.values.flatten should not be empty + } + try { + SharedElasticContainers.reset(elasticContainer, suiteName, resetSteps) + val after = lists(t) + planted.foreach { case (step, name) => + withClue(s"$step after the reset: ")(after(step)._1 should not contain name) + } + kept.foreach { case (step, names) => + withClue(s"$step kept by the reset: ")(names.diff(after(step)._1) shouldBe empty) + } + } finally restoreAllTemplates() + } + + it should "refuse a persistent cluster setting, naming the suite, and pass once it is restored" in { + val setting = "cluster.routing.allocation.node_concurrent_recoveries" + ok( + http( + restClient, + "PUT", + "/_cluster/settings", + Some(s"""{"persistent":{"$setting":"3"}}""") + ) + ) + val leftIndex = "ci1_reset_left" + try { + ok(http(restClient, "PUT", s"/$leftIndex")) + val refused = intercept[ResetRefusedException] { + SharedElasticContainers.reset(elasticContainer, suiteName, resetSteps) + } + refused.refusals.map(r => (r.step, r.leftBy)) shouldBe Seq("cluster_settings" -> suiteName) + refused.refusals.head.added.exists(_.contains(setting)) shouldBe true + refused.getMessage should include(s"left by $suiteName") + // nothing was deleted + http(restClient, "HEAD", s"/$leftIndex")._1 shouldBe 200 + } finally { + ok( + http( + restClient, + "PUT", + "/_cluster/settings", + Some(s"""{"persistent":{"$setting":null}}""") + ) + ) + } + try { + noException should be thrownBy SharedElasticContainers.reset( + elasticContainer, + suiteName, + resetSteps + ) + http(restClient, "HEAD", s"/$leftIndex")._1 shouldBe 404 + } finally restoreAllTemplates() + } +} diff --git a/testkit/src/main/scala/app/softnetwork/elastic/client/repl/ReplGatewayIntegrationSpec.scala b/testkit/src/main/scala/app/softnetwork/elastic/client/repl/ReplGatewayIntegrationSpec.scala index 618fbf862..822789427 100644 --- a/testkit/src/main/scala/app/softnetwork/elastic/client/repl/ReplGatewayIntegrationSpec.scala +++ b/testkit/src/main/scala/app/softnetwork/elastic/client/repl/ReplGatewayIntegrationSpec.scala @@ -27,15 +27,22 @@ import app.softnetwork.elastic.client.result.{ import akka.stream.scaladsl.Sink import scala.collection.immutable.ListMap -import app.softnetwork.elastic.scalatest.ElasticTestKit +import app.softnetwork.elastic.scalatest.{ElasticContainerSharing, ElasticTestKit} import java.time.LocalDate /** Common integration tests for REPL, mirroring GatewayApiIntegrationSpec */ -trait ReplGatewayIntegrationSpec extends ReplIntegrationTestKit { +trait ReplGatewayIntegrationSpec extends ReplIntegrationTestKit with ElasticContainerSharing { self: ElasticTestKit => + /** Opted out of the shared container: this suite asserts on the WHOLE cluster (no index starting + * with `.` in `SHOW TABLES LIKE '.*'`, exactly one enrich policy in `SHOW ENRICH POLICIES`) and + * shares about 25 literal object names with `GatewayApiIntegrationSpec`, so it keeps a container + * of its own. + */ + override def sharedContainer: Boolean = false + // ========================================================================= // 1. SHOW / DESCRIBE TABLE tests // ========================================================================= diff --git a/testkit/src/main/scala/app/softnetwork/elastic/scalatest/ClusterReset.scala b/testkit/src/main/scala/app/softnetwork/elastic/scalatest/ClusterReset.scala new file mode 100644 index 000000000..e041d294f --- /dev/null +++ b/testkit/src/main/scala/app/softnetwork/elastic/scalatest/ClusterReset.scala @@ -0,0 +1,500 @@ +/* + * 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.scalatest + +import app.softnetwork.elastic.client.ElasticsearchVersion +import org.json4s._ +import org.json4s.jackson.JsonMethods.{compact, render} + +import java.nio.charset.StandardCharsets + +/** What a reset does with the objects of one kind that differ from the boot snapshot. */ +sealed trait ResetAction + +/** Objects created after the snapshot are deleted. */ +case object DeleteNew extends ResetAction + +/** Any difference with the snapshot (added or removed) refuses the reset: nothing is deleted. */ +case object RefuseChange extends ResetAction + +/** The cluster a reset inspects: its version, and whether security and Watcher are on. */ +final case class ResetTarget(version: String, security: Boolean, watcher: Boolean) + +/** One kind of cluster object a shared-container reset inspects (and, for [[DeleteNew]], deletes). + * + * `parse` and `deleteRequests` are pure: the I/O lives in [[SharedElasticContainers]]. + */ +trait ResetStep { + + def name: String + + def action: ResetAction + + def applies(t: ResetTarget): Boolean + + /** The GET path that lists the objects of this kind. 200 is parsed, 404 means none. */ + def listPath(t: ResetTarget): String + + /** The objects the list body names, system objects INCLUDED (no filter). */ + def listed(body: JValue, t: ResetTarget): Set[String] = parse(body, t) + + /** The objects the list body names, system objects EXCLUDED: names starting with `.` or holding + * `@`, objects whose `_meta.managed` is `true`, reserved users and roles. + */ + def parse(body: JValue, t: ResetTarget): Set[String] + + /** (method, path, body) of the requests that delete these objects; empty for a refused kind. */ + def deleteRequests(names: Seq[String], t: ResetTarget): Seq[(String, String, Option[String])] +} + +object ResetStep { + + /** A system object by its name alone: a leading `.` or an `@` (ES's own templates, pipelines, + * hidden indices). + */ + def isSystemName(name: String): Boolean = name.startsWith(".") || name.contains("@") + + /** `_meta.managed == true`: an object Elasticsearch installs and re-installs itself. */ + def isManaged(meta: JValue): Boolean = meta \ "managed" match { + case JBool(true) => true + case _ => false + } + + /** The kinds a default reset handles, in deletion order (a policy still used by a pipeline cannot + * be deleted, nor a component template still used by an index template; a pipeline's script + * processor can name a stored script). + */ + lazy val defaults: Seq[ResetStep] = Seq( + Indices, + LegacyTemplates, + IndexTemplates, + ComponentTemplates, + Pipelines, + StoredScripts, + EnrichPolicies, + Users, + Roles, + ApiKeys, + Watchers, + Transforms, + ClusterSettings, + Licence + ) + + // ------------------------------------------------------------------------------------------ + // JSON shape helpers: a 200 body whose shape is not the expected one FAILS, so that a wrong + // per-major endpoint or a changed response can never silently disable a step. A 404 reaches a + // step as `JNothing` and means none. + // ------------------------------------------------------------------------------------------ + + private[scalatest] def shapeError(step: String, expected: String): Nothing = + throw new IllegalStateException(s"[es-reset] unexpected $step list: expected $expected") + + private[scalatest] def objectFields( + step: String, + v: JValue, + what: String + ): List[(String, JValue)] = + v match { + case JObject(fields) => fields + case _ => shapeError(step, s"an object at $what") + } + + private[scalatest] def arrayElements(step: String, v: JValue, what: String): List[JValue] = + v match { + case JArray(elements) => elements + case _ => shapeError(step, s"an array at $what") + } + + private[scalatest] def stringOf(v: JValue): Option[String] = v match { + case JString(s) => Some(s) + case _ => None + } + + private[scalatest] def longOf(v: JValue): Option[Long] = v match { + case JInt(i) => Some(i.toLong) + case JLong(l) => Some(l) + case JDouble(d) => Some(d.toLong) + case JDecimal(d) => Some(d.toLong) + case JString(s) => scala.util.Try(s.trim.toLong).toOption + case _ => None + } + + private[scalatest] def valueText(v: JValue): String = v match { + case JString(s) => s + case JNull => "null" + case JNothing => "" + case other => compact(render(other)) + } + + private[scalatest] def reservedMetadata(obj: JValue): Boolean = + obj \ "metadata" \ "_reserved" match { + case JBool(true) => true + case _ => false + } + + /** A path segment, percent-encoded but for the RFC 3986 unreserved characters. */ + private[scalatest] def segment(name: String): String = { + val sb = new StringBuilder + name.getBytes(StandardCharsets.UTF_8).foreach { b => + val c = (b & 0xff).toChar + if ( + (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || + c == '-' || c == '_' || c == '.' || c == '~' + ) + sb.append(c) + else sb.append("%%%02X".format(b & 0xff)) + } + sb.toString + } + + private def atLeast(t: ResetTarget, major: Int, minor: Int): Boolean = + ElasticsearchVersion.isAtLeast(t.version, major, minor) + + // ------------------------------------------------------------------------------------------ + // Deletable kinds + // ------------------------------------------------------------------------------------------ + + /** A deletable kind whose list body names objects. */ + abstract class ObjectStep(val name: String) extends ResetStep { + + final val action: ResetAction = DeleteNew + + /** (name, object) of every listed object. */ + protected def objects(body: JValue, t: ResetTarget): Seq[(String, JValue)] + + /** Whether an object is a system one beyond its name (managed, reserved). */ + protected def system(obj: JValue): Boolean = false + + override def listed(body: JValue, t: ResetTarget): Set[String] = + if (body == JNothing) Set.empty // 404: none + else objects(body, t).map(_._1).toSet + + override def parse(body: JValue, t: ResetTarget): Set[String] = + if (body == JNothing) Set.empty // 404: none + else objects(body, t).collect { case (n, o) if !isSystemName(n) && !system(o) => n }.toSet + } + + /** Non-dot indices; deleted by name (never a wildcard: `action.destructive_requires_name`). */ + case object Indices extends ObjectStep("indices") { + def applies(t: ResetTarget): Boolean = true + + /** `expand_wildcards=all` from 7.7 only, so a hidden non-dot index cannot escape. 6.8's + * `_cat/indices` reads no such parameter and answers 400 to an unconsumed one; 6.x has no + * hidden indices. + */ + def listPath(t: ResetTarget): String = + if (atLeast(t, 7, 7)) "/_cat/indices?format=json&h=index&expand_wildcards=all" + else "/_cat/indices?format=json&h=index" + + protected def objects(body: JValue, t: ResetTarget): Seq[(String, JValue)] = + arrayElements(name, body, "the root").map { o => + stringOf(o \ "index").getOrElse(shapeError(name, "an index name in each row")) -> o + } + + def deleteRequests(names: Seq[String], t: ResetTarget): Seq[(String, String, Option[String])] = + ClusterReset + .chunks(names.map(segment)) + .map(chunk => ("DELETE", s"/${chunk.mkString(",")}?ignore_unavailable=true", None)) + } + + /** A deletable kind deleted one object per request, at `/`. */ + abstract class NamedStep(name: String, deletePrefix: String) extends ObjectStep(name) { + def deleteRequests(names: Seq[String], t: ResetTarget): Seq[(String, String, Option[String])] = + names.map(n => ("DELETE", s"$deletePrefix/${segment(n)}", None)) + } + + case object LegacyTemplates extends NamedStep("legacy_templates", "/_template") { + def applies(t: ResetTarget): Boolean = true + def listPath(t: ResetTarget): String = "/_template" + protected def objects(body: JValue, t: ResetTarget): Seq[(String, JValue)] = + objectFields(name, body, "the root") + override protected def system(obj: JValue): Boolean = isManaged(obj \ "_meta") + } + + case object IndexTemplates extends NamedStep("index_templates", "/_index_template") { + def applies(t: ResetTarget): Boolean = + ElasticsearchVersion.supportsComposableTemplates(t.version) + def listPath(t: ResetTarget): String = "/_index_template" + protected def objects(body: JValue, t: ResetTarget): Seq[(String, JValue)] = + arrayElements(name, body \ "index_templates", "index_templates").map { o => + stringOf(o \ "name").getOrElse(shapeError(name, "a name in each template")) -> + (o \ "index_template") + } + override protected def system(obj: JValue): Boolean = isManaged(obj \ "_meta") + } + + case object ComponentTemplates extends NamedStep("component_templates", "/_component_template") { + def applies(t: ResetTarget): Boolean = + ElasticsearchVersion.supportsComposableTemplates(t.version) + def listPath(t: ResetTarget): String = "/_component_template" + protected def objects(body: JValue, t: ResetTarget): Seq[(String, JValue)] = + arrayElements(name, body \ "component_templates", "component_templates").map { o => + stringOf(o \ "name").getOrElse(shapeError(name, "a name in each template")) -> + (o \ "component_template") + } + override protected def system(obj: JValue): Boolean = isManaged(obj \ "_meta") + } + + /** Ingest pipelines. ES 8 installs built-in pipelines ASYNCHRONOUSLY, possibly after the + * snapshot: the `.`, `@` and `_meta.managed` filter applies here as for templates. + */ + case object Pipelines extends NamedStep("pipelines", "/_ingest/pipeline") { + def applies(t: ResetTarget): Boolean = true + def listPath(t: ResetTarget): String = "/_ingest/pipeline" + protected def objects(body: JValue, t: ResetTarget): Seq[(String, JValue)] = + objectFields(name, body, "the root") + override protected def system(obj: JValue): Boolean = isManaged(obj \ "_meta") + } + + /** Stored scripts (and search templates): listed from the cluster state, where an absent key + * means none. + */ + case object StoredScripts extends NamedStep("stored_scripts", "/_scripts") { + def applies(t: ResetTarget): Boolean = true + def listPath(t: ResetTarget): String = + "/_cluster/state/metadata?filter_path=metadata.stored_scripts" + protected def objects(body: JValue, t: ResetTarget): Seq[(String, JValue)] = body match { + case JObject(_) => + body \ "metadata" \ "stored_scripts" match { + case JNothing => Nil // no stored script: filter_path drops the key + case scripts => objectFields(name, scripts, "metadata.stored_scripts") + } + case _ => shapeError(name, "an object at the root") + } + } + + case object EnrichPolicies extends NamedStep("enrich_policies", "/_enrich/policy") { + def applies(t: ResetTarget): Boolean = ElasticsearchVersion.supportsEnrich(t.version) + def listPath(t: ResetTarget): String = "/_enrich/policy" + protected def objects(body: JValue, t: ResetTarget): Seq[(String, JValue)] = + arrayElements(name, body \ "policies", "policies").map { p => + // {"config": {"": {"name": …}}} + objectFields(name, p \ "config", "config").headOption + .flatMap { case (_, config) => stringOf(config \ "name") } + .getOrElse(shapeError(name, "config..name in each policy")) -> p + } + } + + /** Users created after the snapshot (secure keys only: the security API refuses every call when + * security is off, so no such user can exist on an open cluster). + */ + case object Users extends NamedStep("users", "/_security/user") { + def applies(t: ResetTarget): Boolean = t.security + def listPath(t: ResetTarget): String = "/_security/user" + protected def objects(body: JValue, t: ResetTarget): Seq[(String, JValue)] = + objectFields(name, body, "the root") + override protected def system(obj: JValue): Boolean = reservedMetadata(obj) + } + + case object Roles extends NamedStep("roles", "/_security/role") { + def applies(t: ResetTarget): Boolean = t.security + def listPath(t: ResetTarget): String = "/_security/role" + protected def objects(body: JValue, t: ResetTarget): Seq[(String, JValue)] = + objectFields(name, body, "the root") + override protected def system(obj: JValue): Boolean = reservedMetadata(obj) + } + + /** API keys, compared by id among the VALID ones: an invalidated key stays listed until its + * retention period ends. "Deleting" one invalidates it. + */ + case object ApiKeys extends ObjectStep("api_keys") { + def applies(t: ResetTarget): Boolean = t.security + def listPath(t: ResetTarget): String = "/_security/api_key" + protected def objects(body: JValue, t: ResetTarget): Seq[(String, JValue)] = + arrayElements(name, body \ "api_keys", "api_keys") + .filterNot(k => (k \ "invalidated") == JBool(true)) + .map(k => stringOf(k \ "id").getOrElse(shapeError(name, "an id in each key")) -> k) + def deleteRequests(names: Seq[String], t: ResetTarget): Seq[(String, String, Option[String])] = + if (names.isEmpty) Nil + else + Seq( + ( + "DELETE", + "/_security/api_key", + Some(compact(render(JObject("ids" -> JArray(names.map(JString(_)).toList))))) + ) + ) + } + + // ------------------------------------------------------------------------------------------ + // Refused kinds: a difference with the snapshot refuses the reset + // ------------------------------------------------------------------------------------------ + + abstract class RefusedStep(val name: String) extends ResetStep { + final val action: ResetAction = RefuseChange + + /** What a 200 body says (a 404 means none). */ + protected def read(body: JValue, t: ResetTarget): Set[String] + + final def parse(body: JValue, t: ResetTarget): Set[String] = + if (body == JNothing) Set.empty else read(body, t) + + def deleteRequests(names: Seq[String], t: ResetTarget): Seq[(String, String, Option[String])] = + Nil + } + + /** Watchers, by their count; inspected only when the key has Watcher on. */ + case object Watchers extends RefusedStep("watchers") { + def applies(t: ResetTarget): Boolean = t.watcher + def listPath(t: ResetTarget): String = + if (atLeast(t, 7, 0)) "/_watcher/stats" else "/_xpack/watcher/stats" + protected def read(body: JValue, t: ResetTarget): Set[String] = { + val count = arrayElements(name, body \ "stats", "stats").map { s => + longOf(s \ "watch_count").getOrElse(shapeError(name, "a watch_count in each node")) + }.sum + Set(s"watch_count=$count") + } + } + + case object Transforms extends RefusedStep("transforms") { + def applies(t: ResetTarget): Boolean = atLeast(t, 7, 5) + def listPath(t: ResetTarget): String = "/_transform" + protected def read(body: JValue, t: ResetTarget): Set[String] = + arrayElements(name, body \ "transforms", "transforms").map { tr => + stringOf(tr \ "id").getOrElse(shapeError(name, "an id in each transform")) + }.toSet + } + + /** Persistent and transient cluster settings, as `persistent:=`. */ + case object ClusterSettings extends RefusedStep("cluster_settings") { + def applies(t: ResetTarget): Boolean = true + def listPath(t: ResetTarget): String = "/_cluster/settings?flat_settings=true" + protected def read(body: JValue, t: ResetTarget): Set[String] = + Seq("persistent", "transient").flatMap { scope => + objectFields(name, body \ scope, scope).map { case (k, v) => s"$scope:$k=${valueText(v)}" } + }.toSet + } + + case object Licence extends RefusedStep("licence") { + def applies(t: ResetTarget): Boolean = true + def listPath(t: ResetTarget): String = "/_license" + protected def read(body: JValue, t: ResetTarget): Set[String] = { + val licence = body \ "license" + Seq("type", "status").map { field => + s"$field=${stringOf(licence \ field).getOrElse(shapeError(name, s"license.$field"))}" + }.toSet + } + } +} + +/** A refused kind: what changed since the snapshot, and the suite after which it was first seen. */ +final case class Refusal(step: String, added: Set[String], removed: Set[String], leftBy: String) + +/** The deletions, in step order, and the refusals of one reset. */ +final case class ResetPlan(deletions: Seq[(String, Seq[String])], refusals: Seq[Refusal]) + +/** The pure part of a shared-container reset: what differs from the snapshot, what to delete and + * what to refuse. No I/O. + */ +object ClusterReset { + + /** The longest comma-joined list of index names one DELETE carries. */ + val MaxChunkChars: Int = 3000 + + /** Per applicable step, the names that differ from the snapshot: created after it for a deletable + * kind (system names never count), the symmetric difference for a refused one. A step absent + * from the snapshot has an empty baseline. + */ + def differences( + steps: Seq[ResetStep], + t: ResetTarget, + snapshot: Map[String, Set[String]], + current: Map[String, Set[String]] + ): Seq[(ResetStep, Set[String], Set[String])] = + steps.filter(_.applies(t)).map { step => + val now = current.getOrElse(step.name, Set.empty[String]) + val atBoot = snapshot.getOrElse(step.name, Set.empty[String]) + step.action match { + case DeleteNew => + (step, (now -- atBoot).filterNot(ResetStep.isSystemName), Set.empty[String]) + case RefuseChange => (step, now -- atBoot, atBoot -- now) + } + } + + /** The suite after which each differing object was first seen: an object already known keeps its + * suite (so a cascade keeps naming the real offender), a new one gets `previous`, an object gone + * is pruned. + */ + def updateFirstSeen( + steps: Seq[ResetStep], + t: ResetTarget, + snapshot: Map[String, Set[String]], + current: Map[String, Set[String]], + firstSeen: Map[(String, String), String], + previous: String + ): Map[(String, String), String] = + differences(steps, t, snapshot, current).flatMap { case (step, added, removed) => + (added ++ removed).toSeq.map { n => + val key = (step.name, n) + key -> firstSeen.getOrElse(key, previous) + } + }.toMap + + def plan( + steps: Seq[ResetStep], + t: ResetTarget, + snapshot: Map[String, Set[String]], + current: Map[String, Set[String]], + firstSeen: Map[(String, String), String], + previous: String + ): ResetPlan = { + val diffs = differences(steps, t, snapshot, current) + val deletions = diffs.collect { + case (step, added, _) if step.action == DeleteNew && added.nonEmpty => + step.name -> added.toSeq.sorted + } + val refusals = diffs.collect { + case (step, added, removed) + if step.action == RefuseChange && (added.nonEmpty || removed.nonEmpty) => + val leftBy = (added ++ removed).toSeq.sorted + .flatMap(n => firstSeen.get((step.name, n))) + .headOption + .getOrElse(previous) + Refusal(step.name, added, removed, leftBy) + } + ResetPlan(deletions, refusals) + } + + /** Greedy chunks whose comma-joined length is at most `maxChars` (a single longer name is a chunk + * of its own). + */ + def chunks(names: Seq[String], maxChars: Int = MaxChunkChars): Seq[Seq[String]] = { + val result = Seq.newBuilder[Seq[String]] + var current = Vector.empty[String] + var length = 0 + names.foreach { n => + val added = if (current.isEmpty) n.length else length + 1 + n.length + if (current.nonEmpty && added > maxChars) { + result += current + current = Vector(n) + length = n.length + } else { + current = current :+ n + length = added + } + } + if (current.nonEmpty) result += current + result.result() + } + + /** A sorted name list cut at `max` characters. */ + def cut(names: Iterable[String], max: Int = 200): String = { + val joined = names.toSeq.sorted.mkString(",") + if (joined.length <= max) joined else joined.take(max) + "…" + } +} diff --git a/testkit/src/main/scala/app/softnetwork/elastic/scalatest/ElasticContainerSharing.scala b/testkit/src/main/scala/app/softnetwork/elastic/scalatest/ElasticContainerSharing.scala new file mode 100644 index 000000000..a4eabfbf6 --- /dev/null +++ b/testkit/src/main/scala/app/softnetwork/elastic/scalatest/ElasticContainerSharing.scala @@ -0,0 +1,37 @@ +/* + * 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.scalatest + +/** Whether a suite may run on its test JVM's shared Elasticsearch container. + * + * When the system property `softclient4es.testkit.shared-container` is `true`, + * [[ElasticDockerTestKit]] runs every suite whose `sharedContainer` is `true` on one container per + * forked test JVM and per configuration, reset before each suite (see + * [[SharedElasticContainers]]). A suite that asserts on the WHOLE cluster (every index, every + * watcher) overrides it to `false` and keeps a container of its own. + * + * 🔴 The member lives HERE and nowhere else. A suite trait whose self-type is `ElasticTestKit` + * cannot override a member of `ElasticTestKit`, but it can extend this trait and override this + * member; `ElasticDockerTestKit` extends it too and only READS it, so by linearization the suite + * trait's override wins in both mixin orders (`X extends Spec with ElasticDockerTestKit` and the + * reverse). A concrete `sharedContainer` added to `ElasticDockerTestKit` would win silently. + */ +trait ElasticContainerSharing { + + def sharedContainer: Boolean = true + +} diff --git a/testkit/src/main/scala/app/softnetwork/elastic/scalatest/ElasticDockerTestKit.scala b/testkit/src/main/scala/app/softnetwork/elastic/scalatest/ElasticDockerTestKit.scala index ec2964322..3db648a8f 100644 --- a/testkit/src/main/scala/app/softnetwork/elastic/scalatest/ElasticDockerTestKit.scala +++ b/testkit/src/main/scala/app/softnetwork/elastic/scalatest/ElasticDockerTestKit.scala @@ -27,10 +27,11 @@ import org.testcontainers.utility.{DockerImageName, MountableFile} import java.nio.file.Files import java.time.Duration +import scala.util.control.NonFatal /** Created by smanciot on 28/06/2018. */ -trait ElasticDockerTestKit extends ElasticTestKit { _: Suite => +trait ElasticDockerTestKit extends ElasticTestKit with ElasticContainerSharing { _: Suite => override lazy val elasticURL: String = s"http://${elasticContainer.getHttpHostAddress}" @@ -57,7 +58,48 @@ trait ElasticDockerTestKit extends ElasticTestKit { _: Suite => */ lazy val xpackLicenseType: String = "trial" - lazy val elasticContainer: ElasticsearchContainer = { + /** The kinds of cluster object a shared-container reset inspects, in deletion order. Override to + * add a kind (a step absent from the boot snapshot has an empty baseline). + */ + def resetSteps: Seq[ResetStep] = ResetStep.defaults + + /** The configuration this suite's container is keyed by: image tag and rendered yml. */ + private[elastic] def containerKey: ContainerKey = + ContainerKey(elasticVersion, renderElasticsearchYml()) + + /** What a reset of this suite's container inspects. Watcher is off under a `basic` licence. */ + private[elastic] def resetTarget: ResetTarget = + ResetTarget( + elasticVersion, + security = xpackSecurityEnabled, + watcher = xpackWatcherEnabled && xpackLicenseType != "basic" + ) + + // A failed `acquire` is not retried by the same suite (its `afterAll` reads `elasticContainer` + // again through `restClient`): it re-throws, as a never-started container did before. + @volatile private[this] var sharedAcquireFailure: Throwable = _ + + /** This suite's container. + * + * With the system property `softclient4es.testkit.shared-container=true` and `sharedContainer`, + * the JVM's shared container of this configuration (started on first use); else a container of + * its own, started by `start()` and stopped by `stop()`. An override keeps the latter lifecycle + * whatever the property: `start()` and `stop()` decide by IDENTITY. + */ + lazy val elasticContainer: ElasticsearchContainer = + if (SharedElasticContainers.enabled && sharedContainer) { + if (sharedAcquireFailure != null) throw sharedAcquireFailure + try SharedElasticContainers.acquire(containerKey, resetTarget, suiteName, resetSteps)(() => + newElasticContainer() + ) + catch { + case NonFatal(e) => + sharedAcquireFailure = e + throw e + } + } else newElasticContainer() + + private def newElasticContainer(): ElasticsearchContainer = { val tmpDir = if (localExecution) { val tmp = Files.createTempDirectory("es-tmp") @@ -69,7 +111,7 @@ trait ElasticDockerTestKit extends ElasticTestKit { _: Suite => Console.println(s"📁 Temp directory: $tmpDir") - val configFile = createElasticsearchYml() + val configFile = writeElasticsearchYml(renderElasticsearchYml()) Console.println(s"⚙️ Config file: $configFile") @@ -91,7 +133,10 @@ trait ElasticDockerTestKit extends ElasticTestKit { _: Suite => .withStartupTimeout(Duration.ofMinutes(2)) } - private def createElasticsearchYml(): String = { + /** The text of `elasticsearch.yml`: the ONE renderer, both for the file and for the container + * key. + */ + private[scalatest] def renderElasticsearchYml(): String = { val enrollmentLine = if (ElasticsearchVersion.isEs8OrHigher(elasticVersion)) { s"xpack.security.enrollment.enabled: $xpackSecurityEnabled\n" @@ -161,6 +206,11 @@ trait ElasticDockerTestKit extends ElasticTestKit { _: Suite => |bootstrap.memory_lock: false |""".stripMargin + config + } + + /** Writes the config to a temp file and returns its path. */ + private def writeElasticsearchYml(config: String): String = { val configFile = Files.createTempFile("elasticsearch-", ".yml") Files.write( configFile, @@ -173,8 +223,24 @@ trait ElasticDockerTestKit extends ElasticTestKit { _: Suite => configFile.toAbsolutePath.toString } - override def start(): Unit = elasticContainer.start() + /** A shared container is reset BEFORE the suite (a refused reset throws: the suite aborts and + * nothing is deleted); an unshared one is started, as it always was. + */ + override def start(): Unit = { + val container = elasticContainer + if (SharedElasticContainers.isShared(container)) + SharedElasticContainers.reset(container, suiteName, resetSteps) + else SharedElasticContainers.startUnshared(container, containerKey, suiteName) + } - override def stop(): Unit = elasticContainer.stop() + /** A shared container reports what the suite left (never throws) and keeps running; an unshared + * one is stopped, as it always was. + */ + override def stop(): Unit = { + val container = elasticContainer + if (SharedElasticContainers.isShared(container)) + SharedElasticContainers.leakReport(container, suiteName, resetSteps) + else SharedElasticContainers.stopUnshared(container) + } } diff --git a/testkit/src/main/scala/app/softnetwork/elastic/scalatest/SharedElasticContainers.scala b/testkit/src/main/scala/app/softnetwork/elastic/scalatest/SharedElasticContainers.scala new file mode 100644 index 000000000..fb0ca8e8a --- /dev/null +++ b/testkit/src/main/scala/app/softnetwork/elastic/scalatest/SharedElasticContainers.scala @@ -0,0 +1,407 @@ +/* + * 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.scalatest + +import app.softnetwork.elastic.client.spi.ElasticClientFactory +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._ +import org.json4s.jackson.JsonMethods.parse +import org.testcontainers.elasticsearch.ElasticsearchContainer + +import java.nio.charset.StandardCharsets +import java.nio.file.Paths +import java.security.MessageDigest +import java.util.{Base64, Locale} +import scala.util.control.NonFatal + +/** The configuration a shared container is keyed by: its image tag and the text of its + * `elasticsearch.yml`, as rendered by the ONE renderer that also writes the file. + */ +final case class ContainerKey(imageTag: String, yml: String) { + + /** `/`. */ + def short: String = s"$imageTag/${ContainerKey.sha256(yml).take(8)}" +} + +object ContainerKey { + private[scalatest] def sha256(text: String): String = + MessageDigest + .getInstance("SHA-256") + .digest(text.getBytes(StandardCharsets.UTF_8)) + .map(b => "%02x".format(b & 0xff)) + .mkString +} + +/** A reset refused because a refused kind (watchers, transforms, cluster settings, licence) differs + * from the snapshot. Nothing was deleted. + */ +final class ResetRefusedException(val refusals: Seq[Refusal], message: String) + extends IllegalStateException(message) + +/** One Elasticsearch container per test JVM per [[ContainerKey]], reset before each suite. + * + * Enabled by the system property `softclient4es.testkit.shared-container=true` (set in the forked + * test JVM's `javaOptions`). Without it nothing here is ever reached: [[ElasticDockerTestKit]] + * starts and stops a container of its own per suite, as it always did. + * + * - [[acquire]] starts the container once, waits for an active licence, takes the snapshot of + * every reset step, then registers it. A failed start is never registered: the next suite + * tries again. + * - [[reset]] (from `ElasticDockerTestKit.start()`, BEFORE each suite) inspects every step, + * refuses (deleting nothing) when a refused kind changed, else deletes what was created after + * the snapshot, then shuts the cached factory clients down (the next suite has the same URL). + * - [[leakReport]] (from `stop()`, after each suite) reports what the suite left. It never + * throws. + * - A JVM shutdown hook stops every registered container; Ryuk is the backstop. + * + * Suites must run SERIALLY in the JVM (sbt's default for a forked test group): a reset deletes the + * indices another suite would be using. + * + * Log lines (stdout): `[es-container]` for every container start, shared or not; `[es-reset]` and + * `[es-leaks]` for a shared container only. No header, body or password is ever printed. + */ +object SharedElasticContainers { + + val Property: String = "softclient4es.testkit.shared-container" + + /** Read on every call (a `lazy val` would latch it for the whole JVM). */ + def enabled: Boolean = sys.props.get(Property).contains("true") + + private final class Entry( + val key: ContainerKey, + val container: ElasticsearchContainer, + val target: ResetTarget, + val admin: RestClient, + val snapshot: Map[String, Set[String]] + ) { + @volatile var firstSeen: Map[(String, String), String] = Map.empty + @volatile var lastSuite: String = "-" + } + + // Immutable, replaced under the lock: the shutdown hook reads it without locking. + @volatile private[this] var registry: List[Entry] = Nil + + // Started unshared containers, by identity: counted in `live=` only (never stopped by the hook). + private[this] val unsharedLive = + java.util.Collections.newSetFromMap( + new java.util.IdentityHashMap[ElasticsearchContainer, java.lang.Boolean]() + ) + + @volatile private[this] var hookInstalled = false + + private[this] lazy val module: String = { + val dir = Paths.get(sys.props.getOrElse("user.dir", ".")).toAbsolutePath.normalize + val n = dir.getNameCount + (if (n >= 2) dir.subpath(n - 2, n) else dir).toString.replace('\\', '/') + } + + private def entryOf(c: ElasticsearchContainer): Option[Entry] = registry.find(_.container eq c) + + /** Whether this container is the shared one of its key (identity, never the property). */ + def isShared(c: ElasticsearchContainer): Boolean = entryOf(c).isDefined + + private[elastic] def targetOf(c: ElasticsearchContainer): Option[ResetTarget] = + entryOf(c).map(_.target) + + private[elastic] def snapshotOf(c: ElasticsearchContainer): Option[Map[String, Set[String]]] = + entryOf(c).map(_.snapshot) + + /** The shared container of `key`, started (once) by `create()` when none is registered. */ + def acquire(key: ContainerKey, target: ResetTarget, suite: String, steps: Seq[ResetStep])( + create: () => ElasticsearchContainer + ): ElasticsearchContainer = synchronized { + registry.find(_.key == key) match { + case Some(entry) => entry.container + case None => + val container = create() + val bootStart = System.nanoTime() + try container.start() + catch { + case NonFatal(e) => + try container.stop() + catch { case NonFatal(_) => () } + throw e + } + val boot = secondsSince(bootStart) + var admin: RestClient = null + try { + admin = adminClient(container, target) + val licenceStart = System.nanoTime() + waitForActiveLicence(admin, target) + val licence = secondsSince(licenceStart) + val snapshot = inspect(admin, target, steps) + val entry = new Entry(key, container, target, admin, snapshot) + registry = registry :+ entry + installShutdownHook() + Console.println( + s"[es-container] boot=${fmt(boot)}s licence=${fmt(licence)}s key=${key.short} " + + s"shared=true module=$module suite=$suite id=${idOf(container)} live=$live" + ) + container + } catch { + case NonFatal(e) => + if (admin != null) { + try admin.close() + catch { case NonFatal(_) => () } + } + try container.stop() + catch { case NonFatal(_) => () } + throw e + } + } + } + + /** Starts an unshared container exactly as before (`start()`), plus the `[es-container]` line. */ + private[scalatest] def startUnshared( + c: ElasticsearchContainer, + key: ContainerKey, + suite: String + ): Unit = { + val bootStart = System.nanoTime() + c.start() + val boot = secondsSince(bootStart) + unsharedLive.synchronized(unsharedLive.add(c)) + Console.println( + s"[es-container] boot=${fmt(boot)}s licence=- key=${key.short} shared=false " + + s"module=$module suite=$suite id=${idOf(c)} live=$live" + ) + } + + /** Stops an unshared container exactly as before (`stop()`). */ + private[scalatest] def stopUnshared(c: ElasticsearchContainer): Unit = + try c.stop() + finally unsharedLive.synchronized(unsharedLive.remove(c)) + + /** Deletes what was created on this shared container since its snapshot, or throws a + * [[ResetRefusedException]] (deleting nothing) when a refused kind changed. + */ + def reset(c: ElasticsearchContainer, suite: String, steps: Seq[ResetStep]): Unit = + synchronized { + val entry = entryOf(c).getOrElse( + throw new IllegalStateException(s"[es-reset] suite=$suite: the container is not shared") + ) + val t = entry.target + val start = System.nanoTime() + val applicable = steps.filter(_.applies(t)) + val current = inspect(entry.admin, t, applicable) + val previous = entry.lastSuite + val firstSeen = + ClusterReset.updateFirstSeen( + applicable, + t, + entry.snapshot, + current, + entry.firstSeen, + previous + ) + val plan = ClusterReset.plan(applicable, t, entry.snapshot, current, firstSeen, previous) + entry.firstSeen = firstSeen + entry.lastSuite = suite + if (plan.refusals.nonEmpty) { + val reasons = plan.refusals.map { r => + s"${r.step} changed (added=${ClusterReset.cut(r.added)} " + + s"removed=${ClusterReset.cut(r.removed)}) left by ${r.leftBy}" + } + throw new ResetRefusedException( + plan.refusals, + s"[es-reset] refused before suite $suite on key ${entry.key.short}: " + + s"${reasons.mkString("; ")}. Nothing was deleted; a refused kind is never reset." + ) + } + val byName = applicable.map(s => s.name -> s).toMap + plan.deletions.foreach { case (stepName, names) => + byName(stepName).deleteRequests(names, t).foreach { case (method, path, body) => + send(entry.admin, method, path, body, t) + } + } + // the next suite reaches the same URL: no client cached by the previous one may serve it + ElasticClientFactory.shutdown() + val deleted = plan.deletions.map(_._2.size).sum + val kinds = plan.deletions.map { case (k, names) => s"$k=${ClusterReset.cut(names)}" } + Console.println( + s"[es-reset] suite=$suite key=${entry.key.short} took=${fmt(secondsSince(start))}s " + + s"deleted=$deleted (${kinds.mkString(" ")}) previous=$previous" + ) + } + + /** Reports what the suite left on this shared container. Never throws. */ + def leakReport(c: ElasticsearchContainer, suite: String, steps: Seq[ResetStep]): Unit = { + val line = + try { + synchronized { + val entry = entryOf(c).getOrElse( + throw new IllegalStateException("the container is not shared") + ) + val t = entry.target + val applicable = steps.filter(_.applies(t)) + val current = inspect(entry.admin, t, applicable) + val left = ClusterReset + .differences(applicable, t, entry.snapshot, current) + .collect { + case (step, added, removed) if added.nonEmpty || removed.nonEmpty => + step.name -> (added ++ removed) + } + val search = get(entry.admin, "/_nodes/stats/indices/search", t) + val script = get(entry.admin, "/_nodes/stats/script", t) + def total(stats: JValue, path: String*): String = { + val values = ResetStep + .objectFields("nodes", stats \ "nodes", "nodes") + .map { case (_, node) => path.foldLeft(node)(_ \ _) } + .flatMap(ResetStep.longOf) + if (values.isEmpty) "-" else values.sum.toString + } + s"[es-leaks] suite=$suite key=${entry.key.short} left=${left.map(_._2.size).sum} " + + s"(${left.map { case (k, names) => s"$k=${ClusterReset.cut(names)}" }.mkString(" ")}) " + + s"open_contexts=${total(search, "indices", "search", "open_contexts")} " + + s"scroll_current=${total(search, "indices", "search", "scroll_current")} " + + s"script_compilations=${total(script, "script", "compilations")} " + + s"compilation_limit_triggered=${total(script, "script", "compilation_limit_triggered")}" + } + } catch { + case NonFatal(e) => + val message = Option(e.getMessage).getOrElse("").take(200) + s"[es-leaks] suite=$suite left=? error=${e.getClass.getSimpleName}: $message" + } + try Console.println(line) + catch { case NonFatal(_) => () } + } + + /** Every applicable step's objects, system objects excluded. */ + private def inspect( + admin: RestClient, + t: ResetTarget, + steps: Seq[ResetStep] + ): Map[String, Set[String]] = + steps.filter(_.applies(t)).map(s => s.name -> s.parse(list(admin, s, t), t)).toMap + + /** The list body of one step: 200 is parsed, 404 means none (`JNothing`), any other status fails + * loud, naming the method, path, status and Elasticsearch version. + */ + private[elastic] def list(client: RestClient, step: ResetStep, t: ResetTarget): JValue = + get(client, step.listPath(t), t) + + private def get(client: RestClient, path: String, t: ResetTarget): JValue = { + val (status, body) = + try { + val response = client.performRequest(new Request("GET", path)) + ( + response.getStatusLine.getStatusCode, + Option(response.getEntity).map(EntityUtils.toString).getOrElse("") + ) + } catch { + case e: ResponseException => (e.getResponse.getStatusLine.getStatusCode, "") + } + status match { + case 200 => parse(body) + case 404 => JNothing + case other => + throw new IllegalStateException( + s"[es-reset] GET $path answered $other on Elasticsearch ${t.version}" + ) + } + } + + private def send( + client: RestClient, + method: String, + path: String, + body: Option[String], + t: ResetTarget + ): Unit = { + val request = new Request(method, path) + body.foreach(b => request.setEntity(new NStringEntity(b, ContentType.APPLICATION_JSON))) + val status = + try client.performRequest(request).getStatusLine.getStatusCode + catch { case e: ResponseException => e.getResponse.getStatusLine.getStatusCode } + // 404: already gone + if (status >= 300 && status != 404) + throw new IllegalStateException( + s"[es-reset] $method $path answered $status on Elasticsearch ${t.version}" + ) + } + + /** The admin client of a key: anonymous on an open cluster; `elastic` with the container's + * `ELASTIC_PASSWORD` (Testcontainers sets one for ES >= 8) on a secure one. + */ + private def adminClient(c: ElasticsearchContainer, t: ResetTarget): RestClient = { + val builder = RestClient.builder(HttpHost.create(s"http://${c.getHttpHostAddress}")) + if (t.security) { + val password = Option(c.getEnvMap.get("ELASTIC_PASSWORD")).getOrElse( + throw new IllegalStateException( + "[es-container] security is on but the container declares no ELASTIC_PASSWORD" + ) + ) + val token = Base64.getEncoder.encodeToString( + s"elastic:$password".getBytes(StandardCharsets.UTF_8) + ) + builder.setDefaultHeaders(Array[Header](new BasicHeader("Authorization", s"Basic $token"))) + } + builder.build() + } + + /** Waits (once per container) until `GET /_license` answers `"status":"active"`. */ + private def waitForActiveLicence(admin: RestClient, t: ResetTarget): Unit = { + val deadline = System.nanoTime() + 120L * 1000000000L + var last = "no answer" + var active = false + while (!active && System.nanoTime() < deadline) { + try { + val response = admin.performRequest(new Request("GET", "/_license")) + val body = EntityUtils.toString(response.getEntity).replaceAll("\\s", "") + active = body.contains("\"status\":\"active\"") + if (!active) last = "licence not active yet" + } catch { + case e: ResponseException => + last = s"status ${e.getResponse.getStatusLine.getStatusCode}" + case NonFatal(e) => last = e.getClass.getSimpleName + } + if (!active) Thread.sleep(500) + } + if (!active) + throw new IllegalStateException( + s"[es-container] no active licence after 120s on Elasticsearch ${t.version} ($last)" + ) + } + + private def installShutdownHook(): Unit = + if (!hookInstalled) { + hookInstalled = true + sys.addShutdownHook { + registry.foreach { entry => + try entry.admin.close() + catch { case NonFatal(_) => () } + try entry.container.stop() + catch { case NonFatal(_) => () } + } + } + () + } + + private def live: Int = registry.size + unsharedLive.synchronized(unsharedLive.size) + + private def idOf(c: ElasticsearchContainer): String = + Option(c.getContainerId).map(_.take(12)).getOrElse("-") + + private def secondsSince(start: Long): Double = (System.nanoTime() - start) / 1e9 + + private def fmt(seconds: Double): String = String.format(Locale.ROOT, "%.2f", Double.box(seconds)) +} diff --git a/testkit/src/test/scala/app/softnetwork/elastic/scalatest/ClusterResetPlanSpec.scala b/testkit/src/test/scala/app/softnetwork/elastic/scalatest/ClusterResetPlanSpec.scala new file mode 100644 index 000000000..2220f6112 --- /dev/null +++ b/testkit/src/test/scala/app/softnetwork/elastic/scalatest/ClusterResetPlanSpec.scala @@ -0,0 +1,347 @@ +/* + * 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.scalatest + +import org.json4s.JsonAST.JNothing +import org.json4s.jackson.JsonMethods.parse +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers + +/** The pure part of the shared-container reset, Docker-free: per kind and per major, what a list + * body names, what the plan deletes (current - snapshot - system objects, in deletion order), what + * it refuses and whom it blames. + */ +class ClusterResetPlanSpec extends AnyFlatSpec with Matchers { + + private val es6 = ResetTarget("6.8.23", security = false, watcher = true) + private val es7 = ResetTarget("7.17.29", security = false, watcher = true) + private val es8 = ResetTarget("8.18.3", security = false, watcher = true) + private val es9 = ResetTarget("9.0.3", security = false, watcher = true) + private val es8Secure = es8.copy(security = true) + private val all = Seq(es6, es7, es8, es9) + + import ResetStep._ + + // ------------------------------------------------------------------------------------------ + // list bodies (inline ES JSON) -> names + // ------------------------------------------------------------------------------------------ + + "indices" should "list every index and keep the non-system ones" in { + val body = parse( + """[{"index":"orders"},{"index":".watches"},{"index":".ds-ilm-history-7-2026.11.13-000001"}, + | {"index":"a@b"},{"index":"all_templates_target"}]""".stripMargin + ) + Indices.listed(body, es8) shouldBe Set( + "orders", + ".watches", + ".ds-ilm-history-7-2026.11.13-000001", + "a@b", + "all_templates_target" + ) + Indices.parse(body, es8) shouldBe Set("orders", "all_templates_target") + Indices.parse(JNothing, es8) shouldBe empty + } + + it should "expand wildcards from 7.7 only (6.8 answers 400 to an unconsumed parameter)" in { + Indices.listPath(es6) shouldBe "/_cat/indices?format=json&h=index" + Seq(es7, es8, es9).foreach { t => + Indices.listPath(t) shouldBe "/_cat/indices?format=json&h=index&expand_wildcards=all" + } + Indices.listPath(ResetTarget("7.6.2", security = false, watcher = true)) shouldBe + "/_cat/indices?format=json&h=index" + } + + it should "delete by name, in chunks of at most 3,000 characters, ignoring unavailable ones" in { + val names = (1 to 400).map(i => f"index_with_a_rather_long_name_$i%04d") + val requests = Indices.deleteRequests(names, es8) + requests.size should be > 1 + requests.foreach { case (method, path, body) => + method shouldBe "DELETE" + body shouldBe None + path should endWith("?ignore_unavailable=true") + path.stripPrefix("/").stripSuffix("?ignore_unavailable=true").length should be <= 3000 + } + requests + .flatMap(_._2.stripPrefix("/").stripSuffix("?ignore_unavailable=true").split(",")) + .toSeq shouldBe names + } + + "chunks" should "keep every chunk within the limit and preserve the order" in { + ClusterReset.chunks(Seq("aa", "bb", "cc"), maxChars = 5) shouldBe Seq( + Seq("aa", "bb"), + Seq("cc") + ) + ClusterReset.chunks(Seq("abcdef", "g"), maxChars = 5) shouldBe Seq(Seq("abcdef"), Seq("g")) + ClusterReset.chunks(Nil) shouldBe empty + } + + "legacy templates" should "drop dot, @ and managed templates" in { + val body = parse( + """{"all_templates":{"index_patterns":["*"]},".watch-history-9":{"index_patterns":[".watcher-history-9*"]}, + | "logs@settings":{},"managed_one":{"_meta":{"managed":true}},"mine":{"_meta":{"managed":false}}}""".stripMargin + ) + LegacyTemplates.listed(body, es6) should have size 5 + LegacyTemplates.parse(body, es6) shouldBe Set("all_templates", "mine") + LegacyTemplates.deleteRequests(Seq("mine"), es6) shouldBe Seq( + ("DELETE", "/_template/mine", None) + ) + } + + "index and component templates" should "apply from 7.8 and drop managed ones" in { + val index = parse( + """{"index_templates":[{"name":"logs","index_template":{"_meta":{"managed":true}}}, + | {"name":"ilm-history-7","index_template":{"_meta":{"description":"x","managed":true}}}, + | {"name":"ci1_it","index_template":{"index_patterns":["ci1_*"]}}]}""".stripMargin + ) + IndexTemplates.listed(index, es8) shouldBe Set("logs", "ilm-history-7", "ci1_it") + IndexTemplates.parse(index, es8) shouldBe Set("ci1_it") + val component = parse( + """{"component_templates":[{"name":"logs@mappings","component_template":{}}, + | {"name":"ci1_ct","component_template":{"template":{}}}]}""".stripMargin + ) + ComponentTemplates.parse(component, es8) shouldBe Set("ci1_ct") + Seq(IndexTemplates, ComponentTemplates).foreach { step => + step.applies(es6) shouldBe false + step.applies(ResetTarget("7.7.1", security = false, watcher = true)) shouldBe false + Seq(es7, es8, es9).foreach(t => step.applies(t) shouldBe true) + } + } + + "pipelines" should "drop dot, @ and managed pipelines (ES 8 installs some after the snapshot)" in { + val body = parse( + """{"logs@json-pipeline":{},"logs-default-pipeline":{"_meta":{"managed":true}}, + | ".fleet_final_pipeline-1":{},"ci1_pipeline":{"processors":[]}}""".stripMargin + ) + Pipelines.parse(body, es8) shouldBe Set("ci1_pipeline") + Pipelines.parse(JNothing, es8) shouldBe empty // 404: no pipeline at all + } + + "stored scripts" should "read the cluster state, an absent key meaning none" in { + StoredScripts.listPath( + es6 + ) shouldBe "/_cluster/state/metadata?filter_path=metadata.stored_scripts" + StoredScripts.parse(parse("{}"), es8) shouldBe empty + StoredScripts.parse( + parse("""{"metadata":{"stored_scripts":{"grouped_tag_document":{"lang":"painless"}}}}"""), + es8 + ) shouldBe Set("grouped_tag_document") + StoredScripts.deleteRequests(Seq("grouped_tag_document"), es6) shouldBe + Seq(("DELETE", "/_scripts/grouped_tag_document", None)) + } + + "enrich policies" should "apply from 7.5 and read the policy name of any type" in { + val body = parse( + """{"policies":[{"config":{"match":{"name":"p1","indices":["s"]}}}, + | {"config":{"range":{"name":"p2"}}}]}""".stripMargin + ) + EnrichPolicies.parse(body, es8) shouldBe Set("p1", "p2") + EnrichPolicies.applies(es6) shouldBe false + Seq(es7, es8, es9).foreach(t => EnrichPolicies.applies(t) shouldBe true) + } + + "users, roles and API keys" should "apply on a secure key only and never name a reserved object" in { + val users = parse( + """{"elastic":{"metadata":{"_reserved":true}},"kibana_system":{"metadata":{"_reserved":true}}, + | "s7_ro":{"metadata":{}}}""".stripMargin + ) + Users.listed(users, es8Secure) shouldBe Set("elastic", "kibana_system", "s7_ro") + Users.parse(users, es8Secure) shouldBe Set("s7_ro") + val roles = parse( + """{"superuser":{"metadata":{"_reserved":true}},"s7_reader":{"metadata":{}}}""" + ) + Roles.parse(roles, es8Secure) shouldBe Set("s7_reader") + val keys = parse( + """{"api_keys":[{"id":"k1","invalidated":false},{"id":"k2","invalidated":true},{"id":"k3"}]}""" + ) + ApiKeys.parse(keys, es8Secure) shouldBe Set("k1", "k3") + ApiKeys.deleteRequests(Seq("k1", "k3"), es8Secure) shouldBe + Seq(("DELETE", "/_security/api_key", Some("""{"ids":["k1","k3"]}"""))) + ApiKeys.deleteRequests(Nil, es8Secure) shouldBe empty + Seq(Users, Roles, ApiKeys).foreach { step => + all.foreach(t => step.applies(t) shouldBe false) + step.applies(es8Secure) shouldBe true + } + Users.deleteRequests(Seq("s7_ro"), es8Secure) shouldBe + Seq(("DELETE", "/_security/user/s7_ro", None)) + } + + "refused kinds" should "read watchers, transforms, cluster settings and the licence per major" in { + Watchers.listPath(es6) shouldBe "/_xpack/watcher/stats" + Seq(es7, es8, es9).foreach(t => Watchers.listPath(t) shouldBe "/_watcher/stats") + Watchers.parse(parse("""{"stats":[{"watch_count":2},{"watch_count":1}]}"""), es8) shouldBe + Set("watch_count=3") + Watchers.applies(es8.copy(watcher = false)) shouldBe false + Transforms.applies(es6) shouldBe false + Transforms.parse(parse("""{"count":1,"transforms":[{"id":"t1"}]}"""), es8) shouldBe Set("t1") + ClusterSettings.listPath(es8) shouldBe "/_cluster/settings?flat_settings=true" + ClusterSettings.parse( + parse("""{"persistent":{"a.b":"3"},"transient":{"c":true}}"""), + es8 + ) shouldBe Set("persistent:a.b=3", "transient:c=true") + Licence.parse( + parse("""{"license":{"type":"trial","status":"active","uid":"x"}}"""), + es6 + ) shouldBe + Set("type=trial", "status=active") + Seq(Watchers, Transforms, ClusterSettings, Licence).foreach { step => + step.deleteRequests(Seq("x"), es8) shouldBe empty + } + } + + "a body of an unexpected shape" should "fail loud, never read as empty" in { + an[IllegalStateException] should be thrownBy Indices.parse(parse("""{"index":"x"}"""), es8) + an[IllegalStateException] should be thrownBy IndexTemplates.parse(parse("""{"x":[]}"""), es8) + an[IllegalStateException] should be thrownBy Watchers.parse(parse("""{"x":1}"""), es8) + an[IllegalStateException] should be thrownBy Licence.parse(parse("""{"license":{}}"""), es8) + } + + // ------------------------------------------------------------------------------------------ + // the plan + // ------------------------------------------------------------------------------------------ + + private val snapshot = Map( + "indices" -> Set.empty[String], + "legacy_templates" -> Set("boot_template"), + "pipelines" -> Set("boot_pipeline"), + "cluster_settings" -> Set.empty[String], + "licence" -> Set("type=trial", "status=active"), + "watchers" -> Set("watch_count=0") + ) + + "plan" should "delete exactly current - snapshot, system names excluded, in deletion order" in { + val current = Map( + "indices" -> Set("orders", "customers", ".tasks"), + "legacy_templates" -> Set("boot_template", "all_templates"), + "pipelines" -> Set("boot_pipeline", "p1"), + "stored_scripts" -> Set("s1"), // absent from the snapshot: empty baseline + "enrich_policies" -> Set("e1"), + "cluster_settings" -> Set.empty[String], + "licence" -> Set("type=trial", "status=active"), + "watchers" -> Set("watch_count=0") + ) + val plan = ClusterReset.plan(ResetStep.defaults, es8, snapshot, current, Map.empty, "Prev") + plan.refusals shouldBe empty + plan.deletions shouldBe Seq( + "indices" -> Seq("customers", "orders"), + "legacy_templates" -> Seq("all_templates"), + "pipelines" -> Seq("p1"), + "stored_scripts" -> Seq("s1"), + "enrich_policies" -> Seq("e1") + ) + // the order is ResetStep.defaults' order + val order = ResetStep.defaults.map(_.name) + val positions = plan.deletions.map { case (step, _) => order.indexOf(step) } + positions shouldBe positions.sorted + } + + it should "skip the steps a major does not support" in { + val current = Map( + "enrich_policies" -> Set("e1"), + "index_templates" -> Set("it1"), + "users" -> Set("u1") + ) + ClusterReset + .plan(ResetStep.defaults, es6, Map.empty, current, Map.empty, "Prev") + .deletions shouldBe + empty + ClusterReset + .plan(ResetStep.defaults, es8Secure, Map.empty, current, Map.empty, "Prev") + .deletions shouldBe + Seq("index_templates" -> Seq("it1"), "enrich_policies" -> Seq("e1"), "users" -> Seq("u1")) + } + + it should "never delete a reserved user or a built-in role (the parse drops them)" in { + val users = Users.parse(parse("""{"elastic":{"metadata":{"_reserved":true}}}"""), es8Secure) + val roles = Roles.parse(parse("""{"superuser":{"metadata":{"_reserved":true}}}"""), es8Secure) + val plan = ClusterReset.plan( + ResetStep.defaults, + es8Secure, + Map.empty, + Map("users" -> users, "roles" -> roles), + Map.empty, + "Prev" + ) + plan.deletions shouldBe empty + } + + it should "refuse a changed refused kind, naming the suite after which it was first seen" in { + val current = snapshot ++ Map( + "cluster_settings" -> Set("persistent:a.b=3"), + "indices" -> Set("orders") + ) + val plan = ClusterReset.plan(ResetStep.defaults, es8, snapshot, current, Map.empty, "SuiteA") + plan.refusals shouldBe Seq( + Refusal("cluster_settings", Set("persistent:a.b=3"), Set.empty, "SuiteA") + ) + // a removed object is a change too + val removed = snapshot ++ Map("licence" -> Set("type=basic", "status=active")) + ClusterReset + .plan(ResetStep.defaults, es8, snapshot, removed, Map.empty, "SuiteB") + .refusals shouldBe + Seq(Refusal("licence", Set("type=basic"), Set("type=trial"), "SuiteB")) + // each refused kind + Seq( + "watchers" -> Set("watch_count=1"), + "transforms" -> Set("t1") + ).foreach { case (step, names) => + ClusterReset + .plan(ResetStep.defaults, es8, snapshot, snapshot + (step -> names), Map.empty, "SuiteC") + .refusals + .map(r => (r.step, r.leftBy)) shouldBe Seq(step -> "SuiteC") + } + } + + "firstSeen" should "keep naming the real offender through a cascade, and forget what is gone" in { + val changed = snapshot + ("cluster_settings" -> Set("persistent:a.b=3")) + // after SuiteA: first seen + val seen1 = + ClusterReset.updateFirstSeen(ResetStep.defaults, es8, snapshot, changed, Map.empty, "SuiteA") + seen1 shouldBe Map(("cluster_settings", "persistent:a.b=3") -> "SuiteA") + // the next reset (after the aborted SuiteB) still blames SuiteA + val seen2 = + ClusterReset.updateFirstSeen(ResetStep.defaults, es8, snapshot, changed, seen1, "SuiteB") + seen2 shouldBe seen1 + ClusterReset + .plan(ResetStep.defaults, es8, snapshot, changed, seen2, "SuiteB") + .refusals + .map(_.leftBy) shouldBe + Seq("SuiteA") + // restored: pruned + ClusterReset.updateFirstSeen( + ResetStep.defaults, + es8, + snapshot, + snapshot, + seen2, + "SuiteC" + ) shouldBe empty + } + + "cut" should "sort and cut a name list at 200 characters" in { + ClusterReset.cut(Seq("b", "a")) shouldBe "a,b" + val long = ClusterReset.cut((1 to 100).map(i => s"name$i")) + long.length shouldBe 201 + long should endWith("…") + } + + "ContainerKey" should "be the image tag and the first 8 hex digits of the yml's sha256" in { + val key = ContainerKey("8.18.3", "cluster.name: docker-cluster\n") + key.short should fullyMatch regex """8\.18\.3/[0-9a-f]{8}""" + key.short shouldBe ContainerKey("8.18.3", "cluster.name: docker-cluster\n").short + key.short should not be ContainerKey("8.18.3", "cluster.name: other\n").short + } +} diff --git a/testkit/src/test/scala/app/softnetwork/elastic/scalatest/ElasticDockerTestKitLifecycleSpec.scala b/testkit/src/test/scala/app/softnetwork/elastic/scalatest/ElasticDockerTestKitLifecycleSpec.scala new file mode 100644 index 000000000..83315bf88 --- /dev/null +++ b/testkit/src/test/scala/app/softnetwork/elastic/scalatest/ElasticDockerTestKitLifecycleSpec.scala @@ -0,0 +1,234 @@ +/* + * 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.scalatest + +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import org.slf4j.{Logger, LoggerFactory} +import org.testcontainers.elasticsearch.ElasticsearchContainer +import org.testcontainers.utility.DockerImageName + +import java.io.{ByteArrayOutputStream, PrintStream} +import java.nio.charset.StandardCharsets + +/** `ElasticDockerTestKit`'s lifecycle without Docker: an overridden `elasticContainer` (the + * downstream basic-licence pattern) keeps today's lifecycle whatever the shared-container + * property, the default container is unshared without the property, the opt-out survives + * linearization in both mixin orders, and the rendered `elasticsearch.yml` is byte-identical to + * the one main wrote for the default knobs. + * + * The suites under test are anonymous (never discovered). No test reads a default + * `elasticContainer` with the property on: that would boot a container. + */ +class ElasticDockerTestKitLifecycleSpec extends AnyFlatSpec with Matchers { + + /** A container whose `start` and `stop` only count (no Docker; `getContainerId` stays null). */ + private final class StubContainer + extends ElasticsearchContainer( + DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch").withTag("8.18.3") + ) { + @volatile var starts = 0 + @volatile var stops = 0 + override def start(): Unit = starts += 1 + override def stop(): Unit = stops += 1 + } + + /** Runs `body` with the property set to `value` (absent for `None`), then restores it. + * + * The lock is the property name itself: a string literal is ONE JVM-wide instance, so the copies + * of this spec that `copyTestkit` puts in every `softclient4es{N}-core-testkit` (run in the same + * sbt JVM, possibly concurrently) never see each other's value. + */ + private def withProperty[T](value: Option[String])(body: => T): T = + SharedElasticContainers.Property.synchronized { + val saved = sys.props.get(SharedElasticContainers.Property) + value match { + case Some(v) => System.setProperty(SharedElasticContainers.Property, v) + case None => System.clearProperty(SharedElasticContainers.Property) + } + try body + finally saved match { + case Some(v) => System.setProperty(SharedElasticContainers.Property, v) + case None => System.clearProperty(SharedElasticContainers.Property) + } + } + + /** `body`'s result and what it printed (kept off the build log: the CI boot counters must count + * real containers only). + */ + private def captured[T](body: => T): (T, String) = { + val buffer = new ByteArrayOutputStream() + val out = new PrintStream(buffer, true, StandardCharsets.UTF_8.name()) + val result = Console.withOut(out)(body) + out.flush() + (result, buffer.toString(StandardCharsets.UTF_8.name())) + } + + private val probeLog: Logger = LoggerFactory.getLogger(classOf[ElasticDockerTestKitLifecycleSpec]) + + private val UnsharedLine = + """\[es-container\] boot=\d+\.\d\ds licence=- key=8\.18\.3/[0-9a-f]{8} shared=false module=.+ suite=LifecycleProbe id=- live=\d+""" + + Seq(None, Some("true")).foreach { property => + "An overridden elasticContainer" should s"be started and stopped once each, never registered (property ${property + .getOrElse("absent")})" in { + withProperty(property) { + val stub = new StubContainer + val suite = new AnyFlatSpec with ElasticDockerTestKit { + lazy val log: Logger = probeLog + override def suiteName: String = "LifecycleProbe" + override def elasticVersion: String = "8.18.3" // the key in the line, whatever the copy + override lazy val elasticContainer: ElasticsearchContainer = stub + } + SharedElasticContainers.enabled shouldBe property.isDefined + val (_, started) = captured(suite.start()) + (stub.starts, stub.stops) shouldBe ((1, 0)) + SharedElasticContainers.isShared(stub) shouldBe false + val (_, stopped) = captured(suite.stop()) + (stub.starts, stub.stops) shouldBe ((1, 1)) + SharedElasticContainers.isShared(stub) shouldBe false + // one line, unshared, no licence wait, no reset, no leak report + started.linesIterator.toList match { + case List(line) => line should fullyMatch regex UnsharedLine + case lines => fail(s"expected one [es-container] line, got: $lines") + } + stopped shouldBe empty + } + } + } + + "The default elasticContainer" should "be a container of the suite's own without the property" in { + withProperty(None) { + val suite = new AnyFlatSpec with ElasticDockerTestKit { + lazy val log: Logger = probeLog + override def elasticVersion: String = "8.18.3" + } + val (container, printed) = captured(suite.elasticContainer) + SharedElasticContainers.isShared(container) shouldBe false + container.getContainerId shouldBe null // never started + printed should include("📁 Temp directory:") + printed should include(LifecycleYml.es8) + } + } + + "The opt-out" should "win in both mixin orders" in { + trait OptOut extends ElasticContainerSharing { self: ElasticTestKit => + override def sharedContainer: Boolean = false + } + val specFirst = new AnyFlatSpec with OptOut with ElasticDockerTestKit { + lazy val log: Logger = probeLog + } + val kitFirst = new AnyFlatSpec with ElasticDockerTestKit with OptOut { + lazy val log: Logger = probeLog + } + val default = new AnyFlatSpec with ElasticDockerTestKit { + lazy val log: Logger = probeLog + } + (specFirst.sharedContainer, kitFirst.sharedContainer, default.sharedContainer) shouldBe + ((false, false, true)) + } + + "The rendered elasticsearch.yml" should "be byte-identical to main's for the default knobs" in { + def rendered(version: String): String = { + val suite = new AnyFlatSpec with ElasticDockerTestKit { + lazy val log: Logger = probeLog + override def elasticVersion: String = version + } + suite.renderElasticsearchYml() + } + rendered("8.18.3") shouldBe LifecycleYml.es8 + rendered("6.8.23") shouldBe LifecycleYml.es6 + // the key is the rendered text: a knob that changes the yml changes the key + val secure = new AnyFlatSpec with ElasticDockerTestKit { + lazy val log: Logger = probeLog + override def elasticVersion: String = "8.18.3" + override lazy val xpackSecurityEnabled: Boolean = true + } + val open = new AnyFlatSpec with ElasticDockerTestKit { + lazy val log: Logger = probeLog + override def elasticVersion: String = "8.18.3" + } + open.containerKey shouldBe ContainerKey("8.18.3", LifecycleYml.es8) + secure.containerKey should not be open.containerKey + secure.resetTarget shouldBe ResetTarget("8.18.3", security = true, watcher = true) + } +} + +/** The `elasticsearch.yml` main (ec9b065a) wrote for the default knobs, as its suites printed it. + */ +private object LifecycleYml { + + private def lines(ls: String*): String = ls.mkString("", "\n", "\n") + + private def body(es8: Boolean): String = lines( + Seq( + "# Cluster settings", + "cluster.name: docker-cluster", + "node.name: test-node", + "", + "# Network", + "network.host: 0.0.0.0", + "http.port: 9200", + "transport.port: 9300", + "", + "# Discovery", + "discovery.type: single-node", + "" + ) ++ + (if (es8) + Seq( + "# Deprecation indexing (the suite must not create the index it asserts is absent)", + "cluster.deprecation_indexing.enabled: false", + "" + ) + else Nil) ++ + Seq( + "# X-Pack License (forced; see xpackLicenseType)", + "xpack.license.self_generated.type: trial", + "", + "# X-Pack Security (disabled for tests)", + "xpack.security.enabled: false" + ) ++ + (if (es8) Seq("xpack.security.enrollment.enabled: false") else Nil) ++ + Seq( + "xpack.security.http.ssl.enabled: false", + "xpack.security.transport.ssl.enabled: false", + "", + "# X-Pack Features", + "xpack.ml.enabled: false", + "xpack.graph.enabled: false", + "", + "# X-Pack Watcher", + "xpack.watcher.enabled: true", + "xpack.watcher.encrypt_sensitive_data: false # for tests", + "# xpack.watcher.encryption_key: \"your-32-char-encryption-key\" # set a fixed key for tests if needed", + "", + "# Set default throttle period to 5s to allow frequent executions during tests", + "xpack.watcher.execution.default_throttle_period: 5s", + "", + "# Script compilation rate limit (default 75/5m can be exhausted during tests)", + "script.max_compilations_rate: 10000/5m", + "", + "# Performance", + "bootstrap.memory_lock: false" + ): _* + ) + + val es8: String = body(es8 = true) + + val es6: String = body(es8 = false) +}