diff --git a/modules/search-solr-client/src/test/scala/io/renku/search/solr/client/SearchSolrSpec.scala b/modules/search-solr-client/src/test/scala/io/renku/search/solr/client/SearchSolrSpec.scala index a8b932f7..9d59f23a 100644 --- a/modules/search-solr-client/src/test/scala/io/renku/search/solr/client/SearchSolrSpec.scala +++ b/modules/search-solr-client/src/test/scala/io/renku/search/solr/client/SearchSolrSpec.scala @@ -20,6 +20,7 @@ package io.renku.search.solr.client import cats.effect.{IO, Resource} import io.renku.search.solr.schema.Migrations +import io.renku.solr.client.SolrClient import io.renku.solr.client.migration.SchemaMigrator import io.renku.solr.client.util.SolrSpec @@ -30,15 +31,15 @@ trait SearchSolrSpec extends SolrSpec: new Fixture[Resource[IO, SearchSolrClient[IO]]]("search-solr"): def apply(): Resource[IO, SearchSolrClient[IO]] = - withSolrClient() - .evalTap(SchemaMigrator[IO](_).migrate(Migrations.all)) + SolrClient[IO](solrConfig.copy(core = server.searchCoreName)) + .evalTap(SchemaMigrator[IO](_).migrate(Migrations.all).attempt.void) .map(new SearchSolrClientImpl[IO](_)) override def beforeAll(): Unit = - withSolrClient.beforeAll() + server.start() override def afterAll(): Unit = - withSolrClient.afterAll() + server.stop() override def munitFixtures: Seq[Fixture[_]] = List(withSearchSolrClient) diff --git a/modules/solr-client/src/test/scala/io/renku/solr/client/SolrClientSpec.scala b/modules/solr-client/src/test/scala/io/renku/solr/client/SolrClientSpec.scala index 6477bfcc..dee7bcac 100644 --- a/modules/solr-client/src/test/scala/io/renku/solr/client/SolrClientSpec.scala +++ b/modules/solr-client/src/test/scala/io/renku/solr/client/SolrClientSpec.scala @@ -38,14 +38,6 @@ class SolrClientSpec extends CatsEffectSuite with SolrSpec with SolrTruncate: ) withSolrClient().use { client => for { - _ <- truncateAll(client)( - Seq( - FieldName("roomName"), - FieldName("roomDescription"), - FieldName("roomSeats") - ), - Seq(TypeName("roomText"), TypeName("roomInt")) - ) _ <- client.modifySchema(cmds) _ <- client .insert[Room](Seq(Room("meeting room", "room for meetings", 56))) diff --git a/modules/solr-client/src/test/scala/io/renku/solr/client/migration/SolrMigratorSpec.scala b/modules/solr-client/src/test/scala/io/renku/solr/client/migration/SolrMigratorSpec.scala index 5f0ef1d5..bb6573b7 100644 --- a/modules/solr-client/src/test/scala/io/renku/solr/client/migration/SolrMigratorSpec.scala +++ b/modules/solr-client/src/test/scala/io/renku/solr/client/migration/SolrMigratorSpec.scala @@ -35,7 +35,7 @@ class SolrMigratorSpec extends CatsEffectSuite with SolrSpec with SolrTruncate: SchemaMigration(-1, Add(Field(FieldName("testSeats"), TypeName("testInt")))) ) - def truncate(client: SolrClient[IO]): IO[Unit] = + private def truncate(client: SolrClient[IO]): IO[Unit] = truncateAll(client)( Seq( FieldName("currentSchemaVersion"), @@ -57,7 +57,7 @@ class SolrMigratorSpec extends CatsEffectSuite with SolrSpec with SolrTruncate: } yield () } - test("run only remaining migrations".ignore): + test("run migrations"): withSolrClient().use { client => val migrator = SchemaMigrator(client) val first = migrations.take(2) diff --git a/modules/solr-client/src/test/scala/io/renku/solr/client/util/SolrServer.scala b/modules/solr-client/src/test/scala/io/renku/solr/client/util/SolrServer.scala index f997f7fe..311e809e 100644 --- a/modules/solr-client/src/test/scala/io/renku/solr/client/util/SolrServer.scala +++ b/modules/solr-client/src/test/scala/io/renku/solr/client/util/SolrServer.scala @@ -37,18 +37,22 @@ class SolrServer(module: String, port: Int) { private val containerName = s"$module-test-solr" private val image = "solr:9.4.1-slim" - val coreName = "renku-search-test" + val genericCoreName = "core-test" + val searchCoreName = "search-core-test" + private val cores = Set(genericCoreName, searchCoreName) private val startCmd = s"""|docker run --rm |--name $containerName |-p $port:8983 |-d $image""".stripMargin private val isRunningCmd = s"docker container ls --filter 'name=$containerName'" private val stopCmd = s"docker stop -t5 $containerName" - private val readyCmd = - s"curl http://localhost:8983/solr/$coreName/select?q=*:* --no-progress-meter --fail 1> /dev/null" - private val isReadyCmd = s"docker exec $containerName sh -c '$readyCmd'" - private val createCore = s"precreate-core $coreName" - private val createCoreCmd = s"docker exec $containerName sh -c '$createCore'" + private def readyCmd(core: String) = + s"curl http://localhost:8983/solr/$core/select?q=*:* --no-progress-meter --fail 1> /dev/null" + private def isReadyCmd(core: String) = + s"docker exec $containerName sh -c '${readyCmd(core)}'" + private def createCore(core: String) = s"precreate-core $core" + private def createCoreCmd(core: String) = + s"docker exec $containerName sh -c '${createCore(core)}'" private val wasRunning = new AtomicBoolean(false) def start(): Unit = synchronized { @@ -57,19 +61,26 @@ class SolrServer(module: String, port: Int) { else { println(s"Starting Solr container for '$module' from '$image' image") startContainer() - var rc = 1 - while (rc != 0) { - Thread.sleep(500) - rc = isReadyCmd.! - if (rc == 0) println(s"Solr container for '$module' started on port $port") - } + waitForCoresToBeReady() } } + private def waitForCoresToBeReady(): Unit = + var rc = 1 + while (rc != 0) { + Thread.sleep(500) + rc = checkCoresReady + if (rc == 0) println(s"Solr container for '$module' ready on port $port") + } + + private def checkCoresReady = + cores.foldLeft(0)((rc, core) => if (rc == 0) isReadyCmd(core).! else rc) + private def checkRunning: Boolean = { val out = isRunningCmd.lazyLines.toList val isRunning = out.exists(_ contains containerName) wasRunning.set(isRunning) + if (isRunning) waitForCoresToBeReady() isRunning } @@ -80,8 +91,10 @@ class SolrServer(module: String, port: Int) { case ex => throw ex } Try(startCmd.!!).fold(retryOnContainerFailedToRun, _ => ()) - val rc = createCoreCmd.! - println(s"Created solr core $coreName ($rc)") + val rcs = cores.map(c => c -> createCoreCmd(c).!) + println( + s"Created solr cores: ${rcs.map { case (core, rc) => s"'$core' ($rc)" }.mkString(", ")}" + ) } def stop(): Unit = diff --git a/modules/solr-client/src/test/scala/io/renku/solr/client/util/SolrSpec.scala b/modules/solr-client/src/test/scala/io/renku/solr/client/util/SolrSpec.scala index e7d014c1..7ce5abb0 100644 --- a/modules/solr-client/src/test/scala/io/renku/solr/client/util/SolrSpec.scala +++ b/modules/solr-client/src/test/scala/io/renku/solr/client/util/SolrSpec.scala @@ -26,15 +26,19 @@ import scala.concurrent.duration.Duration trait SolrSpec: self: munit.Suite => - private lazy val server: SolrServer = SolrServer + protected lazy val server: SolrServer = SolrServer + protected lazy val solrConfig: SolrConfig = SolrConfig( + server.url / "solr", + server.genericCoreName, + commitWithin = Some(Duration.Zero), + logMessageBodies = true + ) val withSolrClient: Fixture[Resource[IO, SolrClient[IO]]] = new Fixture[Resource[IO, SolrClient[IO]]]("solr"): def apply(): Resource[IO, SolrClient[IO]] = - SolrClient[IO]( - SolrConfig(server.url / "solr", server.coreName, Some(Duration.Zero), true) - ) + SolrClient[IO](solrConfig) override def beforeAll(): Unit = server.start()