Skip to content

Commit

Permalink
Try to fix tests runner
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinovega committed Dec 11, 2023
1 parent f88b424 commit cc56b8e
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 54 deletions.
3 changes: 1 addition & 2 deletions daikoku/test/daikoku/ApiControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import fr.maif.otoroshi.daikoku.domain.UsagePlanVisibility.{Private, Public}
import fr.maif.otoroshi.daikoku.domain._
import fr.maif.otoroshi.daikoku.domain.json.{ApiFormat, SeqApiSubscriptionFormat}
import fr.maif.otoroshi.daikoku.logger.AppLogger
import fr.maif.otoroshi.daikoku.tests.utils.{DaikokuSpecHelper, OneServerPerSuiteWithMyComponents}
import fr.maif.otoroshi.daikoku.tests.utils.DaikokuSpecHelper
import fr.maif.otoroshi.daikoku.utils.IdGenerator
import org.joda.time.DateTime
import org.scalatest.concurrent.IntegrationPatience
Expand All @@ -28,7 +28,6 @@ import scala.util.Random

class ApiControllerSpec()
extends PlaySpec
with OneServerPerSuiteWithMyComponents
with DaikokuSpecHelper
with IntegrationPatience
with BeforeAndAfterEach
Expand Down
3 changes: 1 addition & 2 deletions daikoku/test/daikoku/BasicUsageSpec.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package fr.maif.otoroshi.daikoku.tests

import fr.maif.otoroshi.daikoku.login.AuthProvider
import fr.maif.otoroshi.daikoku.tests.utils.{DaikokuSpecHelper, OneServerPerSuiteWithMyComponents}
import fr.maif.otoroshi.daikoku.tests.utils.DaikokuSpecHelper
import org.scalatest.concurrent.IntegrationPatience
import org.scalatestplus.play.PlaySpec
import play.api.libs.json.Json

class BasicUsageSpec()
extends PlaySpec
with OneServerPerSuiteWithMyComponents
with DaikokuSpecHelper
with IntegrationPatience {

Expand Down
6 changes: 1 addition & 5 deletions daikoku/test/daikoku/ConsumptionControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import com.github.tomakehurst.wiremock.client.WireMock._
import com.github.tomakehurst.wiremock.core.WireMockConfiguration._
import com.typesafe.config.ConfigFactory
import fr.maif.otoroshi.daikoku.domain._
import fr.maif.otoroshi.daikoku.tests.utils.{
DaikokuSpecHelper,
OneServerPerSuiteWithMyComponents
}
import fr.maif.otoroshi.daikoku.tests.utils.DaikokuSpecHelper
import org.joda.time.DateTime
import org.scalatest.BeforeAndAfterEach
import org.scalatest.concurrent.IntegrationPatience
Expand All @@ -21,7 +18,6 @@ import scala.util.Random

class ConsumptionControllerSpec()
extends PlaySpec
with OneServerPerSuiteWithMyComponents
with DaikokuSpecHelper
with IntegrationPatience
with BeforeAndAfterEach {
Expand Down
6 changes: 1 addition & 5 deletions daikoku/test/daikoku/EnvironmentDisplayMode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@ import fr.maif.otoroshi.daikoku.domain.{
UsagePlan,
UsagePlanId
}
import fr.maif.otoroshi.daikoku.tests.utils.{
DaikokuSpecHelper,
OneServerPerSuiteWithMyComponents
}
import fr.maif.otoroshi.daikoku.tests.utils.DaikokuSpecHelper
import fr.maif.otoroshi.daikoku.utils.IdGenerator
import org.scalatest.concurrent.IntegrationPatience
import org.scalatestplus.play.PlaySpec
import play.api.libs.json.{JsArray, Json}

class EnvironmentDisplayMode()
extends PlaySpec
with OneServerPerSuiteWithMyComponents
with DaikokuSpecHelper
with IntegrationPatience {

Expand Down
6 changes: 1 addition & 5 deletions daikoku/test/daikoku/GuestModeSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package fr.maif.otoroshi.daikoku.tests
import com.typesafe.config.ConfigFactory
import fr.maif.otoroshi.daikoku.domain._
import fr.maif.otoroshi.daikoku.logger.AppLogger
import fr.maif.otoroshi.daikoku.tests.utils.{
DaikokuSpecHelper,
OneServerPerSuiteWithMyComponents
}
import fr.maif.otoroshi.daikoku.tests.utils.DaikokuSpecHelper
import org.scalatest.BeforeAndAfterEach
import org.scalatest.concurrent.IntegrationPatience
import org.scalatestplus.play.PlaySpec
Expand All @@ -15,7 +12,6 @@ import play.api.libs.json.{JsArray, JsObject, Json}

class GuestModeSpec()
extends PlaySpec
with OneServerPerSuiteWithMyComponents
with DaikokuSpecHelper
with IntegrationPatience
with BeforeAndAfterEach {
Expand Down
6 changes: 1 addition & 5 deletions daikoku/test/daikoku/MessagesControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import fr.maif.otoroshi.daikoku.domain.{
json
}
import fr.maif.otoroshi.daikoku.logger.AppLogger
import fr.maif.otoroshi.daikoku.tests.utils.{
DaikokuSpecHelper,
OneServerPerSuiteWithMyComponents
}
import fr.maif.otoroshi.daikoku.tests.utils.DaikokuSpecHelper
import fr.maif.otoroshi.daikoku.utils.IdGenerator
import org.joda.time.DateTime
import org.scalatest.concurrent.IntegrationPatience
Expand All @@ -20,7 +17,6 @@ import play.api.libs.json.{JsArray, Json}

class MessagesControllerSpec()
extends PlaySpec
with OneServerPerSuiteWithMyComponents
with DaikokuSpecHelper
with IntegrationPatience {

Expand Down
4 changes: 2 additions & 2 deletions daikoku/test/daikoku/NotificationControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import fr.maif.otoroshi.daikoku.domain.TeamPermission.Administrator
import fr.maif.otoroshi.daikoku.domain.UsagePlan.QuotasWithLimits
import fr.maif.otoroshi.daikoku.domain._
import fr.maif.otoroshi.daikoku.domain.json._
import fr.maif.otoroshi.daikoku.tests.utils.{DaikokuSpecHelper, OneServerPerSuiteWithMyComponents}
import fr.maif.otoroshi.daikoku.tests.utils.DaikokuSpecHelper
import fr.maif.otoroshi.daikoku.utils.IdGenerator
import org.joda.time.DateTime
import org.scalatest.BeforeAndAfterEach
Expand All @@ -21,7 +21,7 @@ import scala.util.Try

class NotificationControllerSpec()
extends PlaySpec
with OneServerPerSuiteWithMyComponents
// with OneServerPerSuiteWithMyComponents
with DaikokuSpecHelper
with IntegrationPatience
with BeforeAndAfterEach {
Expand Down
7 changes: 2 additions & 5 deletions daikoku/test/daikoku/TeamControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ import fr.maif.otoroshi.daikoku.domain.TeamPermission.{
TeamUser
}
import fr.maif.otoroshi.daikoku.domain._
import fr.maif.otoroshi.daikoku.tests.utils.{
DaikokuSpecHelper,
OneServerPerSuiteWithMyComponents
}
import fr.maif.otoroshi.daikoku.tests.utils.DaikokuSpecHelper
import org.joda.time.DateTime
import org.scalatest.concurrent.IntegrationPatience
import org.scalatestplus.play.PlaySpec
Expand All @@ -26,7 +23,7 @@ import scala.util.Random

class TeamControllerSpec()
extends PlaySpec
with OneServerPerSuiteWithMyComponents
// with OneServerPerSuiteWithMyComponents
with DaikokuSpecHelper
with IntegrationPatience {

Expand Down
4 changes: 2 additions & 2 deletions daikoku/test/daikoku/TenantControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package fr.maif.otoroshi.daikoku.tests
import fr.maif.otoroshi.daikoku.domain.json.SeqCmsHistoryFormat
import fr.maif.otoroshi.daikoku.domain._
import fr.maif.otoroshi.daikoku.login.AuthProvider
import fr.maif.otoroshi.daikoku.tests.utils.{DaikokuSpecHelper, OneServerPerSuiteWithMyComponents}
import fr.maif.otoroshi.daikoku.tests.utils.DaikokuSpecHelper
import fr.maif.otoroshi.daikoku.utils.IdGenerator
import org.scalatest.concurrent.IntegrationPatience
import org.scalatestplus.play.PlaySpec
Expand All @@ -14,7 +14,7 @@ import scala.concurrent.duration.DurationInt

class TenantControllerSpec()
extends PlaySpec
with OneServerPerSuiteWithMyComponents
// with OneServerPerSuiteWithMyComponents
with DaikokuSpecHelper
with IntegrationPatience {

Expand Down
6 changes: 1 addition & 5 deletions daikoku/test/daikoku/UserControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package fr.maif.otoroshi.daikoku.tests

import fr.maif.otoroshi.daikoku.domain._
import fr.maif.otoroshi.daikoku.login.AuthProvider
import fr.maif.otoroshi.daikoku.tests.utils.{
DaikokuSpecHelper,
OneServerPerSuiteWithMyComponents
}
import fr.maif.otoroshi.daikoku.tests.utils.DaikokuSpecHelper
import org.scalatest.concurrent.IntegrationPatience
import org.scalatestplus.play.PlaySpec
import play.api.libs.json.{JsArray, Json}
Expand All @@ -14,7 +11,6 @@ import scala.util.Random

class UserControllerSpec()
extends PlaySpec
with OneServerPerSuiteWithMyComponents
with DaikokuSpecHelper
with IntegrationPatience {

Expand Down
65 changes: 49 additions & 16 deletions daikoku/test/daikoku/suites.scala
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package fr.maif.otoroshi.daikoku.tests

import org.apache.pekko.http.scaladsl.util.FastFuture
import org.apache.pekko.stream.scaladsl.{Keep, Sink, Source}
import cats.implicits.catsSyntaxOptionId
import com.auth0.jwt.algorithms.Algorithm
import com.themillhousegroup.scoup.Scoup
import fr.maif.otoroshi.daikoku.domain.TeamPermission._
import fr.maif.otoroshi.daikoku.domain.UsagePlan._
import fr.maif.otoroshi.daikoku.domain._
import fr.maif.otoroshi.daikoku.logger.AppLogger
import fr.maif.otoroshi.daikoku.login.AuthProvider
import fr.maif.otoroshi.daikoku.modules.DaikokuComponentsInstances
import fr.maif.otoroshi.daikoku.utils.IdGenerator
import org.apache.pekko.actor.ActorSystem
import org.apache.pekko.http.scaladsl.util.FastFuture
import org.apache.pekko.stream.Materializer
import org.apache.pekko.stream.scaladsl.{Keep, Sink, Source}
import org.joda.time.DateTime
import org.jsoup.nodes.Document
import org.mindrot.jbcrypt.BCrypt
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{BeforeAndAfterAll, Suite, TestSuite}
import org.scalatest.{Args, BeforeAndAfterAll, FailedStatus, Status, Suite, TestSuite, TestSuiteMixin}
import org.scalatestplus.play.components.OneServerPerSuiteWithComponents
import play.api.libs.json.{JsObject, JsValue, Json}
import play.api.libs.ws.{DefaultWSCookie, WSResponse}
Expand All @@ -28,24 +29,34 @@ import java.nio.charset.StandardCharsets
import java.nio.file.{Files, StandardCopyOption}
import java.util.concurrent.TimeUnit
import scala.concurrent.duration._
import scala.concurrent.impl.Promise
import scala.concurrent.{Await, ExecutionContext, Future, Promise}
import scala.sys.process.ProcessLogger
import scala.util.{Failure, Success, Try}

class DaikokuSuites extends Suite with BeforeAndAfterAll { thisSuite =>

override protected def beforeAll(): Unit = {}

override protected def afterAll(): Unit = {}
}

case class ApiWithPlans(api: Api, plans: Seq[UsagePlan])

object utils {
trait OneServerPerSuiteWithMyComponents
extends OneServerPerSuiteWithComponents
with ScalaFutures {
this: TestSuite =>
// trait OneServerPerSuiteWithMyComponents
// extends OneServerPerSuiteWithComponents
// with ScalaFutures {
// this: TestSuite =>
//
// lazy val daikokuComponents = {
// val components =
// new DaikokuComponentsInstances(context)
// println(s"Using env ${components.env}") // WARNING: important to keep, needed to switch env between suites
// components
// }
//
// override def components: BuiltInComponents = daikokuComponents
//
// override def fakeApplication(): Application = {
// daikokuComponents.application
// }
// }

trait DaikokuSpecHelper extends TestSuiteMixin with OneServerPerSuiteWithComponents with ScalaFutures { suite: TestSuite =>

lazy val daikokuComponents = {
val components =
Expand All @@ -59,9 +70,31 @@ object utils {
override def fakeApplication(): Application = {
daikokuComponents.application
}
}

trait DaikokuSpecHelper { suite: OneServerPerSuiteWithMyComponents =>
abstract override def run(testName: Option[String], args: Args): Status = {
lazy val run = super.run(testName, args)

def runIfDatabaseAvailable(timeout: FiniteDuration): Status = {

val triedLong = Try(Await.result(daikokuComponents.env.dataStore.tenantRepo.count(Json.obj()), 1.second))

logger.info(s"database is avalaible ? ${triedLong.isSuccess}")

if (triedLong.isSuccess) {
run
} else if (timeout < 1.minute) {
logger.info(s"database is no longer avalaible, wainting $timeout before retry")
await(timeout)
val newDuration = timeout * 2
runIfDatabaseAvailable(newDuration)
} else {
FailedStatus
}
}

runIfDatabaseAvailable(1.second)
}


implicit val ec: ExecutionContext = daikokuComponents.env.defaultExecutionContext
implicit val as: ActorSystem = daikokuComponents.env.defaultActorSystem
Expand Down

0 comments on commit cc56b8e

Please sign in to comment.