From 05f61a9216cc9ad19663a257edad6c6ec84e178c Mon Sep 17 00:00:00 2001 From: Christian Bickel <github@cbickel.de> Date: Fri, 10 Feb 2017 23:35:02 +0100 Subject: [PATCH] Change logging to Akka-logging (#1836) * Fix log format of logged responses * Add Akka Logging to OpenWhisk * Code review comments --- tests/src/common/StreamLogging.scala | 35 ++++++ tests/src/services/KafkaConnectorTests.scala | 80 ++++++-------- tests/src/system/basic/WskSequenceTests.scala | 15 +-- tests/src/whisk/common/ConfigTests.scala | 7 +- tests/src/whisk/common/SchedulerTests.scala | 10 +- .../src/whisk/consul/ConsulClientTests.scala | 22 ++-- .../src/whisk/consul/ConsulHealthTests.scala | 13 ++- tests/src/whisk/core/WhiskConfigTests.scala | 7 +- .../test/ApiGwRoutemgmtActionTests.scala | 43 ++++---- .../cli/test/SequenceMigrationTests.scala | 14 ++- .../container/test/ContainerPoolTests.scala | 27 ++--- .../controller/test/ActionsApiTests.scala | 75 +++++-------- .../controller/test/AuthenticateTests.scala | 36 ++---- .../test/ControllerTestCommon.scala | 32 +++--- .../core/controller/test/MetaApiTests.scala | 25 +---- .../controller/test/RateThrottleTests.scala | 7 +- .../controller/test/SequenceApiTests.scala | 103 +++++++----------- .../test/CouchDbRestClientTests.scala | 4 +- .../test/ExtendedCouchDbRestClient.scala | 5 +- ...ultipleReadersSingleWriterCacheTests.scala | 11 +- .../core/database/test/ReplicatorTests.scala | 10 +- .../test/ActivationResponseTests.scala | 6 +- .../dispatcher/test/DispatcherTests.scala | 23 ++-- .../core/dispatcher/test/TestConnector.scala | 10 +- .../core/entity/test/DatastoreTests.scala | 10 +- .../whisk/core/entity/test/ViewTests.scala | 24 ++-- 26 files changed, 319 insertions(+), 335 deletions(-) create mode 100644 tests/src/common/StreamLogging.scala diff --git a/tests/src/common/StreamLogging.scala b/tests/src/common/StreamLogging.scala new file mode 100644 index 00000000..6ca5fbe8 --- /dev/null +++ b/tests/src/common/StreamLogging.scala @@ -0,0 +1,35 @@ +/* + * Copyright 2015-2016 IBM Corporation + * + * 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 common + +import java.io.ByteArrayOutputStream +import java.io.PrintStream + +import whisk.common.Logging +import whisk.common.PrintStreamLogging + +/** + * Logging facility, that can be used by tests. + * + * It contains the implicit Logging-instance, that is needed implicitly for some methods and classes. + * the logger logs to the stream, that can be accessed from your test, to check if a specific message has been written. + */ +trait StreamLogging { + val stream = new ByteArrayOutputStream + val printstream = new PrintStream(stream) + implicit val logging: Logging = new PrintStreamLogging(printstream) +} diff --git a/tests/src/services/KafkaConnectorTests.scala b/tests/src/services/KafkaConnectorTests.scala index 769acc22..abc13b88 100644 --- a/tests/src/services/KafkaConnectorTests.scala +++ b/tests/src/services/KafkaConnectorTests.scala @@ -20,8 +20,10 @@ import java.util.Calendar import scala.concurrent.Await import scala.concurrent.duration.DurationInt +import scala.concurrent.duration.FiniteDuration import scala.language.postfixOps +import org.apache.commons.lang3.StringUtils import org.apache.kafka.clients.consumer.CommitFailedException import org.junit.runner.RunWith import org.scalatest.BeforeAndAfterAll @@ -29,23 +31,24 @@ import org.scalatest.FlatSpec import org.scalatest.Matchers import org.scalatest.junit.JUnitRunner -import akka.event.Logging.ErrorLevel +import common.StreamLogging +import common.WskActorSystem +import whisk.common.Logging import whisk.common.TransactionId import whisk.connector.kafka.KafkaConsumerConnector import whisk.connector.kafka.KafkaProducerConnector import whisk.core.WhiskConfig import whisk.core.connector.Message import whisk.utils.ExecutionContextFactory -import common.WskActorSystem -import scala.concurrent.duration.FiniteDuration import whisk.utils.retry -import java.io.PrintStream -import java.io.ByteArrayOutputStream -import org.apache.commons.lang3.StringUtils -import scala.concurrent.duration.FiniteDuration @RunWith(classOf[JUnitRunner]) -class KafkaConnectorTests extends FlatSpec with Matchers with WskActorSystem with BeforeAndAfterAll { +class KafkaConnectorTests + extends FlatSpec + with Matchers + with WskActorSystem + with BeforeAndAfterAll + with StreamLogging { implicit val transid = TransactionId.testing implicit val ec = ExecutionContextFactory.makeCachedThreadPoolExecutionContext() @@ -58,9 +61,6 @@ class KafkaConnectorTests extends FlatSpec with Matchers with WskActorSystem wit val producer = new KafkaProducerConnector(config.kafkaHost, ec) val consumer = new TestKafkaConsumerConnector(config.kafkaHost, groupid, topic, sessionTimeout = sessionTimeout) - producer.setVerbosity(ErrorLevel) - consumer.setVerbosity(ErrorLevel) - override def afterAll() { producer.close() consumer.close() @@ -116,39 +116,31 @@ class KafkaConnectorTests extends FlatSpec with Matchers with WskActorSystem wit } it should "catch a failing commit" in { - val stream = new ByteArrayOutputStream - val printstream = new PrintStream(stream) - consumer.outputStream = printstream val messageReceived = "message received" - try { - consumer.onMessage((topic, partition, offset, bytes) => { - printstream.println(messageReceived) - }) - val message = new Message { override val serialize = Calendar.getInstance().getTime().toString } - - // Send message while commit throws no exception -> Should be processed - consumer.commitFails = false - Await.result(producer.send(topic, message), 10 seconds) - retry(stream.toString should include(messageReceived), 20, Some(500 millisecond)) - - // Send message while commit throws exception -> Message will not be processed - consumer.commitFails = true - retry(stream.toString should include("failed to commit to kafka: commit failed"), 50, Some(100 millisecond)) - Await.result(producer.send(topic, message), 10 seconds) - retry(stream.toString should include("failed to commit to kafka: commit failed"), 50, Some(100 millisecond)) - - // Send message again -> No commit exception -> Should work again - consumer.commitFails = false - Await.result(producer.send(topic, message), 10 seconds) - retry(StringUtils.countMatches(stream.toString, messageReceived) should be(2), 50, Some(100 milliseconds)) - - // Wait a few seconds and ensure that the message is not processed three times - Thread.sleep(5000) - StringUtils.countMatches(stream.toString, messageReceived) should be(2) - } finally { - printstream.close() - stream.close() - } + consumer.onMessage((topic, partition, offset, bytes) => { + printstream.println(messageReceived) + }) + val message = new Message { override val serialize = Calendar.getInstance().getTime().toString } + + // Send message while commit throws no exception -> Should be processed + consumer.commitFails = false + Await.result(producer.send(topic, message), 10 seconds) + retry(stream.toString should include(messageReceived), 20, Some(500 millisecond)) + + // Send message while commit throws exception -> Message will not be processed + consumer.commitFails = true + retry(stream.toString should include("failed to commit to kafka: commit failed"), 50, Some(100 millisecond)) + Await.result(producer.send(topic, message), 10 seconds) + retry(stream.toString should include("failed to commit to kafka: commit failed"), 50, Some(100 millisecond)) + + // Send message again -> No commit exception -> Should work again + consumer.commitFails = false + Await.result(producer.send(topic, message), 10 seconds) + retry(StringUtils.countMatches(stream.toString, messageReceived) should be(2), 50, Some(100 milliseconds)) + + // Wait a few seconds and ensure that the message is not processed three times + Thread.sleep(5000) + StringUtils.countMatches(stream.toString, messageReceived) should be(2) } } @@ -157,7 +149,7 @@ class TestKafkaConsumerConnector( kafkahost: String, groupid: String, topic: String, - sessionTimeout: FiniteDuration) extends KafkaConsumerConnector(kafkahost, groupid, topic, sessionTimeout = sessionTimeout) { + sessionTimeout: FiniteDuration)(implicit logging: Logging) extends KafkaConsumerConnector(kafkahost, groupid, topic, sessionTimeout = sessionTimeout) { override def commit() = { if (commitFails) { diff --git a/tests/src/system/basic/WskSequenceTests.scala b/tests/src/system/basic/WskSequenceTests.scala index d993a54d..5caa77c5 100644 --- a/tests/src/system/basic/WskSequenceTests.scala +++ b/tests/src/system/basic/WskSequenceTests.scala @@ -21,24 +21,24 @@ import java.util.Date import scala.concurrent.duration.DurationInt import scala.language.postfixOps +import scala.util.matching.Regex import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner +import common.StreamLogging import common.TestHelpers import common.TestUtils import common.TestUtils._ import common.Wsk import common.WskProps import common.WskTestHelpers -import spray.json.DefaultJsonProtocol._ import spray.json._ +import spray.json.DefaultJsonProtocol._ import spray.testkit.ScalatestRouteTest import whisk.core.WhiskConfig import whisk.http.Messages.sequenceIsTooLong -import scala.util.matching.Regex - /** * Tests sequence execution */ @@ -47,7 +47,8 @@ import scala.util.matching.Regex class WskSequenceTests extends TestHelpers with ScalatestRouteTest - with WskTestHelpers { + with WskTestHelpers + with StreamLogging { implicit val wskprops = WskProps() val wsk = new Wsk @@ -414,7 +415,7 @@ class WskSequenceTests * t trigger with payload * rule r: t -> s */ - it should "execute a sequence that is part of a rule and pass the trigger parameters to the sequence" in withAssetCleaner(wskprops){ + it should "execute a sequence that is part of a rule and pass the trigger parameters to the sequence" in withAssetCleaner(wskprops) { (wp, assetHelper) => val seqName = "seqRule" val actionName = "echo" @@ -459,13 +460,13 @@ class WskSequenceTests * @param triggerPayload the payload used for the trigger (that should be reflected in the sequence result) */ private def checkEchoSeqRuleResult(triggerFireRun: RunResult, seqName: String, triggerPayload: JsObject) = { - withActivation(wsk.activation, triggerFireRun) { + withActivation(wsk.activation, triggerFireRun) { triggerActivation => withActivationsFromEntity(wsk.activation, seqName, since = Some(Instant.ofEpochMilli(triggerActivation.start))) { activationList => activationList.head.response.result shouldBe Some(triggerPayload) activationList.head.cause shouldBe None } - } + } } /** diff --git a/tests/src/whisk/common/ConfigTests.scala b/tests/src/whisk/common/ConfigTests.scala index 0341d8cc..f8560a68 100644 --- a/tests/src/whisk/common/ConfigTests.scala +++ b/tests/src/whisk/common/ConfigTests.scala @@ -21,8 +21,13 @@ import org.scalatest.FlatSpec import org.scalatest.Matchers import org.scalatest.junit.JUnitRunner +import common.StreamLogging + @RunWith(classOf[JUnitRunner]) -class ConfigTests extends FlatSpec with Matchers { +class ConfigTests + extends FlatSpec + with Matchers + with StreamLogging { "Config" should "gets default value" in { val config = new Config(Map("a" -> "A"))(Map()) diff --git a/tests/src/whisk/common/SchedulerTests.scala b/tests/src/whisk/common/SchedulerTests.scala index cb4aa004..b3bc6f34 100644 --- a/tests/src/whisk/common/SchedulerTests.scala +++ b/tests/src/whisk/common/SchedulerTests.scala @@ -19,6 +19,7 @@ package whisk.common import java.time.Instant import scala.collection.mutable.Buffer +import scala.concurrent.Await import scala.concurrent.Future import scala.concurrent.duration._ import scala.language.postfixOps @@ -29,12 +30,15 @@ import org.scalatest.Matchers import org.scalatest.junit.JUnitRunner import akka.actor.PoisonPill - +import common.StreamLogging import common.WskActorSystem -import scala.concurrent.Await @RunWith(classOf[JUnitRunner]) -class SchedulerTests extends FlatSpec with Matchers with WskActorSystem { +class SchedulerTests + extends FlatSpec + with Matchers + with WskActorSystem + with StreamLogging { val timeBetweenCalls = 50 milliseconds val callsToProduce = 5 diff --git a/tests/src/whisk/consul/ConsulClientTests.scala b/tests/src/whisk/consul/ConsulClientTests.scala index cfd5ea28..3e1ad166 100644 --- a/tests/src/whisk/consul/ConsulClientTests.scala +++ b/tests/src/whisk/consul/ConsulClientTests.scala @@ -31,6 +31,7 @@ import akka.http.scaladsl.marshalling._ import akka.http.scaladsl.model._ import akka.http.scaladsl.unmarshalling._ import akka.stream.ActorMaterializer +import common.StreamLogging import common.WskActorSystem import spray.json._ import spray.json.DefaultJsonProtocol._ @@ -40,7 +41,12 @@ import whisk.core.WhiskConfig import whisk.core.WhiskConfig.consulServer @RunWith(classOf[JUnitRunner]) -class ConsulClientTests extends FlatSpec with ScalaFutures with Matchers with WskActorSystem { +class ConsulClientTests + extends FlatSpec + with ScalaFutures + with Matchers + with WskActorSystem + with StreamLogging { implicit val testConfig = PatienceConfig(5.seconds) implicit val materializer = ActorMaterializer() @@ -58,12 +64,10 @@ class ConsulClientTests extends FlatSpec with ScalaFutures with Matchers with Ws def registerService(name: String, id: String, checkScript: Option[String] = None) = { val obj = Map( "ID" -> id.toJson, - "Name" -> name.toJson - ) ++ checkScript.map { script => + "Name" -> name.toJson) ++ checkScript.map { script => "Check" -> JsObject( "Script" -> script.toJson, - "Interval" -> s"${checkInterval.toSeconds}s".toJson - ) + "Interval" -> s"${checkInterval.toSeconds}s".toJson) } Marshal(obj.toJson).to[RequestEntity].flatMap { entity => @@ -71,9 +75,7 @@ class ConsulClientTests extends FlatSpec with ScalaFutures with Matchers with Ws HttpRequest( method = HttpMethods.PUT, uri = consulUri.withPath(Uri.Path("/v1/agent/service/register")), - entity = entity - ) - ) + entity = entity)) r.flatMap { response => Unmarshal(response).to[Any] } @@ -87,9 +89,7 @@ class ConsulClientTests extends FlatSpec with ScalaFutures with Matchers with Ws val r = Http().singleRequest( HttpRequest( method = HttpMethods.PUT, - uri = consulUri.withPath(Uri.Path(s"/v1/agent/service/deregister/$id")) - ) - ) + uri = consulUri.withPath(Uri.Path(s"/v1/agent/service/deregister/$id")))) r.flatMap { response => Unmarshal(response).to[Any] } diff --git a/tests/src/whisk/consul/ConsulHealthTests.scala b/tests/src/whisk/consul/ConsulHealthTests.scala index e4114ac9..05fc26e7 100644 --- a/tests/src/whisk/consul/ConsulHealthTests.scala +++ b/tests/src/whisk/consul/ConsulHealthTests.scala @@ -16,6 +16,7 @@ package whisk.consul +import scala.concurrent.Future import scala.concurrent.duration.DurationInt import org.junit.runner.RunWith @@ -25,15 +26,19 @@ import org.scalatest.concurrent.ScalaFutures import org.scalatest.junit.JUnitRunner import org.scalatest.time.Span.convertDurationToSpan +import common.StreamLogging +import common.WskActorSystem import whisk.common.ConsulClient import whisk.core.WhiskConfig import whisk.core.WhiskConfig.consulServer -import common.WskActorSystem -import scala.concurrent.Future - @RunWith(classOf[JUnitRunner]) -class ConsulHealthTests extends FlatSpec with ScalaFutures with Matchers with WskActorSystem { +class ConsulHealthTests + extends FlatSpec + with ScalaFutures + with Matchers + with WskActorSystem + with StreamLogging { implicit val testConfig = PatienceConfig(5.seconds) diff --git a/tests/src/whisk/core/WhiskConfigTests.scala b/tests/src/whisk/core/WhiskConfigTests.scala index 30bf2828..02d9fbb2 100644 --- a/tests/src/whisk/core/WhiskConfigTests.scala +++ b/tests/src/whisk/core/WhiskConfigTests.scala @@ -28,11 +28,16 @@ import org.scalatest.FlatSpec import org.scalatest.Matchers import org.scalatest.junit.JUnitRunner +import common.StreamLogging import common.WskActorSystem import whisk.common.ConsulClient @RunWith(classOf[JUnitRunner]) -class WhiskConfigTests extends FlatSpec with Matchers with WskActorSystem { +class WhiskConfigTests + extends FlatSpec + with Matchers + with WskActorSystem + with StreamLogging { behavior of "WhiskConfig" diff --git a/tests/src/whisk/core/apigw/actions/test/ApiGwRoutemgmtActionTests.scala b/tests/src/whisk/core/apigw/actions/test/ApiGwRoutemgmtActionTests.scala index b892aac5..4b5dab8d 100644 --- a/tests/src/whisk/core/apigw/actions/test/ApiGwRoutemgmtActionTests.scala +++ b/tests/src/whisk/core/apigw/actions/test/ApiGwRoutemgmtActionTests.scala @@ -19,20 +19,21 @@ package whisk.core.apigw.actions.test import org.junit.runner.RunWith import org.scalatest.BeforeAndAfterAll import org.scalatest.junit.JUnitRunner -import spray.json.DefaultJsonProtocol._ -import spray.json._ import common.JsHelpers +import common.StreamLogging import common.TestHelpers import common.TestUtils.ANY_ERROR_EXIT import common.TestUtils.DONTCARE_EXIT -import common.TestUtils.SUCCESS_EXIT import common.TestUtils.RunResult +import common.TestUtils.SUCCESS_EXIT import common.Wsk -import common.WskAdmin import common.WskActorSystem +import common.WskAdmin import common.WskProps import common.WskTestHelpers +import spray.json._ +import spray.json.DefaultJsonProtocol._ import whisk.core.WhiskConfig case class ApiAction( @@ -47,8 +48,7 @@ case class ApiAction( "namespace" -> namespace.toJson, "backendMethod" -> backendMethod.toJson, "backendUrl" -> backendUrl.toJson, - "authkey" -> authkey.toJson - ) + "authkey" -> authkey.toJson) } } @@ -61,7 +61,8 @@ class ApiGwRoutemgmtActionTests with BeforeAndAfterAll with WskActorSystem with WskTestHelpers - with JsHelpers { + with JsHelpers + with StreamLogging { implicit val wskprops = WskProps() val wsk = new Wsk @@ -297,30 +298,29 @@ class ApiGwRoutemgmtActionTests //createApi ("/whisk.system/routemgmt/createApi", ANY_ERROR_EXIT, "apidoc is required", Seq("-p", "__ow_meta_namespace", "_")), ("/whisk.system/routemgmt/createApi", ANY_ERROR_EXIT, "apidoc is missing the namespace field", - Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", "{}")), + Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", "{}")), ("/whisk.system/routemgmt/createApi", ANY_ERROR_EXIT, "apidoc is missing the gatewayBasePath field", - Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", """{"namespace":"_"}""")), + Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", """{"namespace":"_"}""")), ("/whisk.system/routemgmt/createApi", ANY_ERROR_EXIT, "apidoc is missing the gatewayPath field", - Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", """{"namespace":"_","gatewayBasePath":"/ApiGwRoutemgmtActionTests_bp"}""")), + Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", """{"namespace":"_","gatewayBasePath":"/ApiGwRoutemgmtActionTests_bp"}""")), ("/whisk.system/routemgmt/createApi", ANY_ERROR_EXIT, "apidoc is missing the gatewayMethod field", - Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", """{"namespace":"_","gatewayBasePath":"/ApiGwRoutemgmtActionTests_bp","gatewayPath":"ApiGwRoutemgmtActionTests_rp"}""")), + Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", """{"namespace":"_","gatewayBasePath":"/ApiGwRoutemgmtActionTests_bp","gatewayPath":"ApiGwRoutemgmtActionTests_rp"}""")), ("/whisk.system/routemgmt/createApi", ANY_ERROR_EXIT, "apidoc is missing the action field", - Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", """{"namespace":"_","gatewayBasePath":"/ApiGwRoutemgmtActionTests_bp","gatewayPath":"ApiGwRoutemgmtActionTests_rp","gatewayMethod":"get"}""")), + Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", """{"namespace":"_","gatewayBasePath":"/ApiGwRoutemgmtActionTests_bp","gatewayPath":"ApiGwRoutemgmtActionTests_rp","gatewayMethod":"get"}""")), ("/whisk.system/routemgmt/createApi", ANY_ERROR_EXIT, "action is missing the backendMethod field", - Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", """{"namespace":"_","gatewayBasePath":"/ApiGwRoutemgmtActionTests_bp","gatewayPath":"ApiGwRoutemgmtActionTests_rp","gatewayMethod":"get","action":{}}""")), + Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", """{"namespace":"_","gatewayBasePath":"/ApiGwRoutemgmtActionTests_bp","gatewayPath":"ApiGwRoutemgmtActionTests_rp","gatewayMethod":"get","action":{}}""")), ("/whisk.system/routemgmt/createApi", ANY_ERROR_EXIT, "action is missing the backendUrl field", - Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", """{"namespace":"_","gatewayBasePath":"/ApiGwRoutemgmtActionTests_bp","gatewayPath":"ApiGwRoutemgmtActionTests_rp","gatewayMethod":"get","action":{"backendMethod":"post"}}""")), + Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", """{"namespace":"_","gatewayBasePath":"/ApiGwRoutemgmtActionTests_bp","gatewayPath":"ApiGwRoutemgmtActionTests_rp","gatewayMethod":"get","action":{"backendMethod":"post"}}""")), ("/whisk.system/routemgmt/createApi", ANY_ERROR_EXIT, "action is missing the namespace field", - Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", """{"namespace":"_","gatewayBasePath":"/ApiGwRoutemgmtActionTests_bp","gatewayPath":"ApiGwRoutemgmtActionTests_rp","gatewayMethod":"get","action":{"backendMethod":"post","backendUrl":"URL"}}""")), + Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", """{"namespace":"_","gatewayBasePath":"/ApiGwRoutemgmtActionTests_bp","gatewayPath":"ApiGwRoutemgmtActionTests_rp","gatewayMethod":"get","action":{"backendMethod":"post","backendUrl":"URL"}}""")), ("/whisk.system/routemgmt/createApi", ANY_ERROR_EXIT, "action is missing the name field", - Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", """{"namespace":"_","gatewayBasePath":"/ApiGwRoutemgmtActionTests_bp","gatewayPath":"ApiGwRoutemgmtActionTests_rp","gatewayMethod":"get","action":{"backendMethod":"post","backendUrl":"URL","namespace":"_"}}""")), + Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", """{"namespace":"_","gatewayBasePath":"/ApiGwRoutemgmtActionTests_bp","gatewayPath":"ApiGwRoutemgmtActionTests_rp","gatewayMethod":"get","action":{"backendMethod":"post","backendUrl":"URL","namespace":"_"}}""")), ("/whisk.system/routemgmt/createApi", ANY_ERROR_EXIT, "action is missing the authkey field", - Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", """{"namespace":"_","gatewayBasePath":"/ApiGwRoutemgmtActionTests_bp","gatewayPath":"ApiGwRoutemgmtActionTests_rp","gatewayMethod":"get","action":{"backendMethod":"post","backendUrl":"URL","namespace":"_","name":"N"}}""")), + Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", """{"namespace":"_","gatewayBasePath":"/ApiGwRoutemgmtActionTests_bp","gatewayPath":"ApiGwRoutemgmtActionTests_rp","gatewayMethod":"get","action":{"backendMethod":"post","backendUrl":"URL","namespace":"_","name":"N"}}""")), ("/whisk.system/routemgmt/createApi", ANY_ERROR_EXIT, "swagger and gatewayBasePath are mutually exclusive and cannot be specified together", - Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", """{"namespace":"_","gatewayBasePath":"/ApiGwRoutemgmtActionTests_bp","gatewayPath":"ApiGwRoutemgmtActionTests_rp","gatewayMethod":"get","action":{"backendMethod":"post","backendUrl":"URL","namespace":"_","name":"N","authkey":"XXXX"},"swagger":{}}""")), + Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", """{"namespace":"_","gatewayBasePath":"/ApiGwRoutemgmtActionTests_bp","gatewayPath":"ApiGwRoutemgmtActionTests_rp","gatewayMethod":"get","action":{"backendMethod":"post","backendUrl":"URL","namespace":"_","name":"N","authkey":"XXXX"},"swagger":{}}""")), ("/whisk.system/routemgmt/createApi", ANY_ERROR_EXIT, "apidoc field cannot be parsed. Ensure it is valid JSON", - Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", "{1:[}}}")) - ) + Seq("-p", "__ow_meta_namespace", "_", "-p", "apidoc", "{1:[}}}"))) invalidArgs foreach { case (action: String, exitcode: Int, errmsg: String, params: Seq[String]) => @@ -329,8 +329,7 @@ class ApiGwRoutemgmtActionTests action, "-i", "-b", "-r", "--apihost", wskprops.apihost, - "--auth", systemKey - ) ++ params + "--auth", systemKey) ++ params val rr = wsk.cli(cmd, expectedExitCode = exitcode) rr.stderr should include regex (errmsg) } diff --git a/tests/src/whisk/core/cli/test/SequenceMigrationTests.scala b/tests/src/whisk/core/cli/test/SequenceMigrationTests.scala index c28f876e..cba30400 100644 --- a/tests/src/whisk/core/cli/test/SequenceMigrationTests.scala +++ b/tests/src/whisk/core/cli/test/SequenceMigrationTests.scala @@ -16,9 +16,15 @@ package whisk.core.cli.test import java.util.Date + import scala.concurrent.duration.DurationInt +import scala.language.postfixOps + import org.junit.runner.RunWith +import org.scalatest.BeforeAndAfter import org.scalatest.junit.JUnitRunner + +import common.StreamLogging import common.TestHelpers import common.TestUtils import common.Wsk @@ -29,11 +35,8 @@ import spray.json._ import spray.json.DefaultJsonProtocol.StringJsonFormat import spray.testkit.ScalatestRouteTest import whisk.core.WhiskConfig -import whisk.core.entity._ import whisk.core.database.test.DbUtils -import org.scalatest.BeforeAndAfter - -import scala.language.postfixOps +import whisk.core.entity._ /** * Tests that "old-style" sequences can be invoked @@ -45,7 +48,8 @@ class SequenceMigrationTests with BeforeAndAfter with DbUtils with ScalatestRouteTest - with WskTestHelpers { + with WskTestHelpers + with StreamLogging { implicit val actorSystem = system diff --git a/tests/src/whisk/core/container/test/ContainerPoolTests.scala b/tests/src/whisk/core/container/test/ContainerPoolTests.scala index f924db36..1e9c115b 100644 --- a/tests/src/whisk/core/container/test/ContainerPoolTests.scala +++ b/tests/src/whisk/core/container/test/ContainerPoolTests.scala @@ -18,6 +18,7 @@ package whisk.core.container.test import scala.concurrent.Future import scala.concurrent.duration._ +import scala.language.postfixOps import org.junit.runner.RunWith import org.scalatest.BeforeAndAfter @@ -26,25 +27,24 @@ import org.scalatest.FlatSpec import org.scalatest.junit.JUnitRunner import akka.event.Logging.ErrorLevel +import common.StreamLogging +import common.WskActorSystem import whisk.common.TransactionId import whisk.core.WhiskConfig import whisk.core.WhiskConfig.dockerEndpoint import whisk.core.WhiskConfig.edgeHostName -import whisk.core.WhiskConfig.selfDockerEndpoint import whisk.core.WhiskConfig.invokerSerializeDockerOp import whisk.core.WhiskConfig.invokerSerializeDockerPull +import whisk.core.WhiskConfig.selfDockerEndpoint import whisk.core.container.Container import whisk.core.container.ContainerPool import whisk.core.entity.AuthKey import whisk.core.entity.EntityName -import whisk.core.entity.Exec import whisk.core.entity.EntityPath +import whisk.core.entity.Exec import whisk.core.entity.WhiskAction import whisk.core.entity.WhiskAuthStore import whisk.core.entity.WhiskEntityStore -import scala.language.postfixOps - -import common.WskActorSystem /** * Unit tests for ContainerPool and, by association, Container and WhiskContainer. @@ -53,19 +53,20 @@ import common.WskActorSystem class ContainerPoolTests extends FlatSpec with BeforeAndAfter with BeforeAndAfterAll - with WskActorSystem { + with WskActorSystem + with StreamLogging { implicit val transid = TransactionId.testing val config = new WhiskConfig( WhiskEntityStore.requiredProperties ++ - WhiskAuthStore.requiredProperties ++ - ContainerPool.requiredProperties ++ - Map(selfDockerEndpoint -> "localhost", - dockerEndpoint -> null, - edgeHostName -> "localhost", - invokerSerializeDockerOp -> "true", - invokerSerializeDockerPull -> "true")) + WhiskAuthStore.requiredProperties ++ + ContainerPool.requiredProperties ++ + Map(selfDockerEndpoint -> "localhost", + dockerEndpoint -> null, + edgeHostName -> "localhost", + invokerSerializeDockerOp -> "true", + invokerSerializeDockerPull -> "true")) assert(config.isValid) diff --git a/tests/src/whisk/core/controller/test/ActionsApiTests.scala b/tests/src/whisk/core/controller/test/ActionsApiTests.scala index fbb2208a..4cdbb99e 100644 --- a/tests/src/whisk/core/controller/test/ActionsApiTests.scala +++ b/tests/src/whisk/core/controller/test/ActionsApiTests.scala @@ -16,8 +16,6 @@ package whisk.core.controller.test -import java.io.ByteArrayOutputStream -import java.io.PrintStream import java.time.Instant import scala.concurrent.duration.DurationInt @@ -26,7 +24,6 @@ import scala.language.postfixOps import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner -import akka.event.Logging.InfoLevel import spray.http.StatusCodes._ import spray.httpx.SprayJsonSupport.sprayJsonMarshaller import spray.httpx.SprayJsonSupport.sprayJsonUnmarshaller @@ -60,7 +57,6 @@ class ActionsApiTests extends ControllerTestCommon with WhiskActionsApi { val namespace = EntityPath(creds.subject.asString) val collectionPath = s"/${EntityPath.DEFAULT}/${collection.path}" def aname = MakeName.next("action_tests") - setVerbosity(InfoLevel) val actionLimit = Exec.sizeLimit val parametersLimit = Parameters.sizeLimit @@ -461,52 +457,39 @@ class ActionsApiTests extends ControllerTestCommon with WhiskActionsApi { val content = WhiskActionPut(Some(action.exec), Some(action.parameters), Some(ActionLimitsOption(Some(action.limits.timeout), Some(action.limits.memory), Some(action.limits.logs)))) val name = action.name - val stream = new ByteArrayOutputStream - val printstream = new PrintStream(stream) - val savedstream = authStore.outputStream - val savedVerbosity = entityStore.getVerbosity() - entityStore.outputStream = printstream - entityStore.setVerbosity(akka.event.Logging.InfoLevel) - try { - // first request invalidates any previous entries and caches new result - Put(s"$collectionPath/$name", content) ~> sealRoute(routes(creds)(transid())) ~> check { - status should be(OK) - val response = responseAs[WhiskAction] - response should be(WhiskAction(action.namespace, action.name, action.exec, - action.parameters, action.limits, action.version, - action.publish, action.annotations ++ Parameters(WhiskAction.execFieldName, Exec.NODEJS))) - } - stream.toString should include regex (s"caching*.*${action.docid.asDocInfo}") - stream.reset() + // first request invalidates any previous entries and caches new result + Put(s"$collectionPath/$name", content) ~> sealRoute(routes(creds)(transid())) ~> check { + status should be(OK) + val response = responseAs[WhiskAction] + response should be(WhiskAction(action.namespace, action.name, action.exec, + action.parameters, action.limits, action.version, + action.publish, action.annotations ++ Parameters(WhiskAction.execFieldName, Exec.NODEJS))) + } + stream.toString should include regex (s"caching*.*${action.docid.asDocInfo}") + stream.reset() - // second request should fetch from cache - Get(s"$collectionPath/$name") ~> sealRoute(routes(creds)(transid())) ~> check { - status should be(OK) - val response = responseAs[WhiskAction] - response should be(WhiskAction(action.namespace, action.name, action.exec, - action.parameters, action.limits, action.version, - action.publish, action.annotations ++ Parameters(WhiskAction.execFieldName, Exec.NODEJS))) - } + // second request should fetch from cache + Get(s"$collectionPath/$name") ~> sealRoute(routes(creds)(transid())) ~> check { + status should be(OK) + val response = responseAs[WhiskAction] + response should be(WhiskAction(action.namespace, action.name, action.exec, + action.parameters, action.limits, action.version, + action.publish, action.annotations ++ Parameters(WhiskAction.execFieldName, Exec.NODEJS))) + } - stream.toString should include regex (s"serving from cache:*.*${action.docid.asDocInfo}") - stream.reset() + stream.toString should include regex (s"serving from cache:*.*${action.docid.asDocInfo}") + stream.reset() - // delete should invalidate cache - Delete(s"$collectionPath/$name") ~> sealRoute(routes(creds)(transid())) ~> check { - status should be(OK) - val response = responseAs[WhiskAction] - response should be(WhiskAction(action.namespace, action.name, action.exec, - action.parameters, action.limits, action.version, - action.publish, action.annotations ++ Parameters(WhiskAction.execFieldName, Exec.NODEJS))) - } - stream.toString should include regex (s"invalidating*.*${action.docid.asDocInfo}") - stream.reset() - } finally { - entityStore.outputStream = savedstream - entityStore.setVerbosity(savedVerbosity) - stream.close() - printstream.close() + // delete should invalidate cache + Delete(s"$collectionPath/$name") ~> sealRoute(routes(creds)(transid())) ~> check { + status should be(OK) + val response = responseAs[WhiskAction] + response should be(WhiskAction(action.namespace, action.name, action.exec, + action.parameters, action.limits, action.version, + action.publish, action.annotations ++ Parameters(WhiskAction.execFieldName, Exec.NODEJS))) } + stream.toString should include regex (s"invalidating*.*${action.docid.asDocInfo}") + stream.reset() } it should "reject put with conflict for pre-existing action" in { diff --git a/tests/src/whisk/core/controller/test/AuthenticateTests.scala b/tests/src/whisk/core/controller/test/AuthenticateTests.scala index 7f8aadb3..f4a79361 100644 --- a/tests/src/whisk/core/controller/test/AuthenticateTests.scala +++ b/tests/src/whisk/core/controller/test/AuthenticateTests.scala @@ -16,9 +16,6 @@ package whisk.core.controller.test -import java.io.ByteArrayOutputStream -import java.io.PrintStream - import scala.concurrent.Await import org.junit.runner.RunWith @@ -63,29 +60,16 @@ class AuthenticateTests extends ControllerTestCommon with Authenticate { val pass = UserPass(creds.authkey.uuid.asString, creds.authkey.key.asString) // first query will be served from datastore - val stream = new ByteArrayOutputStream - val printstream = new PrintStream(stream) - val savedstream = authStore.outputStream - val savedVerbosity = authStore.getVerbosity() - authStore.outputStream = printstream - authStore.setVerbosity(akka.event.Logging.InfoLevel) - try { - val user = Await.result(validateCredentials(Some(pass))(transid()), dbOpTimeout) - user.get should be(creds) - stream.toString should include regex (s"serving from datastore: ${creds.authkey.uuid.asString}") - stream.reset() - - // repeat query, should be served from cache - val cachedUser = Await.result(validateCredentials(Some(pass))(transid()), dbOpTimeout) - cachedUser.get should be(creds) - stream.toString should include regex (s"serving from cache: ${creds.authkey.uuid.asString}") - stream.reset() - } finally { - authStore.outputStream = savedstream - authStore.setVerbosity(savedVerbosity) - stream.close() - printstream.close() - } + val user = Await.result(validateCredentials(Some(pass))(transid()), dbOpTimeout) + user.get should be(creds) + stream.toString should include regex (s"serving from datastore: ${creds.authkey.uuid.asString}") + stream.reset() + + // repeat query, should be served from cache + val cachedUser = Await.result(validateCredentials(Some(pass))(transid()), dbOpTimeout) + cachedUser.get should be(creds) + stream.toString should include regex (s"serving from cache: ${creds.authkey.uuid.asString}") + stream.reset() } it should "not authorize a known user with an invalid key" in { diff --git a/tests/src/whisk/core/controller/test/ControllerTestCommon.scala b/tests/src/whisk/core/controller/test/ControllerTestCommon.scala index 99f80ab1..1f07330f 100644 --- a/tests/src/whisk/core/controller/test/ControllerTestCommon.scala +++ b/tests/src/whisk/core/controller/test/ControllerTestCommon.scala @@ -17,8 +17,8 @@ package whisk.core.controller.test import scala.concurrent.{ Await, Future } -import scala.concurrent.duration.{ DurationInt, FiniteDuration } import scala.concurrent.ExecutionContext +import scala.concurrent.duration.{ DurationInt, FiniteDuration } import scala.language.postfixOps import org.scalatest.BeforeAndAfter @@ -26,13 +26,14 @@ import org.scalatest.BeforeAndAfterAll import org.scalatest.FlatSpec import org.scalatest.Matchers -import akka.event.Logging.{ ErrorLevel, InfoLevel, LogLevel } +import common.StreamLogging import spray.http.BasicHttpCredentials import spray.json.DefaultJsonProtocol import spray.json.JsString import spray.routing.HttpService import spray.testkit.ScalatestRouteTest -import whisk.common.{ Logging, TransactionCounter, TransactionId } +import whisk.common.TransactionCounter +import whisk.common.TransactionId import whisk.core.WhiskConfig import whisk.core.connector.ActivationMessage import whisk.core.controller.WhiskActionsApi @@ -54,7 +55,7 @@ protected trait ControllerTestCommon with DbUtils with WhiskServices with HttpService - with Logging { + with StreamLogging { override val actorRefFactory = null implicit val routeTestTimeout = RouteTestTimeout(90 seconds) @@ -65,7 +66,7 @@ protected trait ControllerTestCommon override val whiskConfig = new WhiskConfig(WhiskAuthStore.requiredProperties ++ WhiskActionsApi.requiredProperties) assert(whiskConfig.isValid) - override val loadBalancer = new DegenerateLoadBalancerService(whiskConfig, InfoLevel) + override val loadBalancer = new DegenerateLoadBalancerService(whiskConfig) override val iam = new NamespaceProvider(whiskConfig, forceLocal = true) override val entitlementProvider: EntitlementProvider = new LocalEntitlementProvider(whiskConfig, loadBalancer, iam) @@ -94,42 +95,42 @@ protected trait ControllerTestCommon def deleteAction(doc: DocId)(implicit transid: TransactionId) = { Await.result(WhiskAction.get(entityStore, doc) flatMap { doc => - info(this, s"deleting ${doc.docinfo}") + logging.info(this, s"deleting ${doc.docinfo}") WhiskAction.del(entityStore, doc.docinfo) }, dbOpTimeout) } def deleteActivation(doc: DocId)(implicit transid: TransactionId) = { Await.result(WhiskActivation.get(entityStore, doc) flatMap { doc => - info(this, s"deleting ${doc.docinfo}") + logging.info(this, s"deleting ${doc.docinfo}") WhiskActivation.del(entityStore, doc.docinfo) }, dbOpTimeout) } def deleteTrigger(doc: DocId)(implicit transid: TransactionId) = { Await.result(WhiskTrigger.get(entityStore, doc) flatMap { doc => - info(this, s"deleting ${doc.docinfo}") + logging.info(this, s"deleting ${doc.docinfo}") WhiskAction.del(entityStore, doc.docinfo) }, dbOpTimeout) } def deleteRule(doc: DocId)(implicit transid: TransactionId) = { Await.result(WhiskRule.get(entityStore, doc) flatMap { doc => - info(this, s"deleting ${doc.docinfo}") + logging.info(this, s"deleting ${doc.docinfo}") WhiskRule.del(entityStore, doc.docinfo) }, dbOpTimeout) } def deleteAuth(doc: DocId)(implicit transid: TransactionId) = { Await.result(WhiskAuth.get(authStore, doc) flatMap { doc => - info(this, s"deleting ${doc.docinfo}") + logging.info(this, s"deleting ${doc.docinfo}") WhiskAuth.del(authStore, doc.docinfo) }, dbOpTimeout) } def deletePackage(doc: DocId)(implicit transid: TransactionId) = { Await.result(WhiskPackage.get(entityStore, doc) flatMap { doc => - info(this, s"deleting ${doc.docinfo}") + logging.info(this, s"deleting ${doc.docinfo}") WhiskPackage.del(entityStore, doc.docinfo) }, dbOpTimeout) } @@ -144,12 +145,7 @@ protected trait ControllerTestCommon } } - setVerbosity(ErrorLevel) - Collection.initialize(entityStore, ErrorLevel) - entityStore.setVerbosity(ErrorLevel) - activationStore.setVerbosity(ErrorLevel) - authStore.setVerbosity(ErrorLevel) - entitlementProvider.setVerbosity(ErrorLevel) + Collection.initialize(entityStore) val ACTIONS = Collection(Collection.ACTIONS) val TRIGGERS = Collection(Collection.TRIGGERS) @@ -188,7 +184,7 @@ protected trait ControllerTestCommon } } -class DegenerateLoadBalancerService(config: WhiskConfig, verbosity: LogLevel)(implicit ec: ExecutionContext) +class DegenerateLoadBalancerService(config: WhiskConfig)(implicit ec: ExecutionContext) extends LoadBalancer { import scala.concurrent.blocking diff --git a/tests/src/whisk/core/controller/test/MetaApiTests.scala b/tests/src/whisk/core/controller/test/MetaApiTests.scala index 57a5794f..c967f214 100644 --- a/tests/src/whisk/core/controller/test/MetaApiTests.scala +++ b/tests/src/whisk/core/controller/test/MetaApiTests.scala @@ -16,8 +16,6 @@ package whisk.core.controller.test -import java.io.ByteArrayOutputStream -import java.io.PrintStream import java.time.Instant import java.util.Base64 @@ -29,7 +27,6 @@ import org.junit.runner.RunWith import org.scalatest.BeforeAndAfterEach import org.scalatest.junit.JUnitRunner -import akka.event.Logging.InfoLevel import spray.http.FormData import spray.http.HttpMethods import spray.http.StatusCodes._ @@ -76,7 +73,6 @@ class MetaApiTests extends ControllerTestCommon with WhiskMetaApi with BeforeAnd val creds = WhiskAuth(Subject(), AuthKey()).toIdentity val namespace = EntityPath(creds.subject.asString) - setVerbosity(InfoLevel) val packages = Seq( WhiskPackage( @@ -162,7 +158,7 @@ class MetaApiTests extends ControllerTestCommon with WhiskMetaApi with BeforeAnd if (namespace.asString == systemId.asString) { systemIdentity } else { - info(this, s"namespace has no identity") + logging.info(this, s"namespace has no identity") Future.failed(RejectRequest(BadRequest)) } } @@ -460,20 +456,11 @@ class MetaApiTests extends ControllerTestCommon with WhiskMetaApi with BeforeAnd it should "warn if meta package is public" in { implicit val tid = transid() - val stream = new ByteArrayOutputStream - val printstream = new PrintStream(stream) - val savedstream = this.outputStream - this.outputStream = printstream - - try { - Get(s"/$routePath/publicmeta") ~> sealRoute(routes(creds)) ~> check { - status should be(OK) - stream.toString should include regex (s"""[WARN] *.*publicmeta@0.0.1' is public""") - stream.reset() - } - } finally { - stream.close() - printstream.close() + + Get(s"/$routePath/publicmeta") ~> sealRoute(routes(creds)) ~> check { + status should be(OK) + stream.toString should include regex (s"""[WARN] *.*publicmeta@0.0.1' is public""") + stream.reset() } } diff --git a/tests/src/whisk/core/controller/test/RateThrottleTests.scala b/tests/src/whisk/core/controller/test/RateThrottleTests.scala index a7b547b0..e1faa1ac 100644 --- a/tests/src/whisk/core/controller/test/RateThrottleTests.scala +++ b/tests/src/whisk/core/controller/test/RateThrottleTests.scala @@ -23,6 +23,7 @@ import org.scalatest.FlatSpec import org.scalatest.Matchers import org.scalatest.junit.JUnitRunner +import common.StreamLogging import whisk.common.TransactionId import whisk.core.entitlement._ import whisk.core.entity.Subject @@ -34,7 +35,11 @@ import whisk.core.entity.Subject * "using Specification DSL to write unit tests, as in should, must, not, be" */ @RunWith(classOf[JUnitRunner]) -class RateThrottleTests extends FlatSpec with Matchers { +class RateThrottleTests + extends FlatSpec + with Matchers + with StreamLogging { + implicit val transid = TransactionId.testing val subject = Subject() diff --git a/tests/src/whisk/core/controller/test/SequenceApiTests.scala b/tests/src/whisk/core/controller/test/SequenceApiTests.scala index 4b3b228b..9e517315 100644 --- a/tests/src/whisk/core/controller/test/SequenceApiTests.scala +++ b/tests/src/whisk/core/controller/test/SequenceApiTests.scala @@ -16,15 +16,12 @@ package whisk.core.controller.test -import java.io.PrintStream - import scala.concurrent.duration.DurationInt import scala.language.postfixOps import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner -import akka.event.Logging.DebugLevel import spray.http.StatusCodes._ import spray.httpx.SprayJsonSupport._ import spray.json._ @@ -52,9 +49,6 @@ class SequenceApiTests def aname() = MakeName.next("sequence_tests") val allowedActionDuration = 120 seconds - // set logging level to debug - setVerbosity(DebugLevel) - it should "reject creation of sequence with more actions than allowed limit" in { implicit val tid = transid() val seqName = EntityName(s"${aname()}_toomanyactions") @@ -300,19 +294,11 @@ class SequenceApiTests val sSeq = makeSimpleSequence(sSeqName, namespace, Vector(xSeqName, xSeqName), false) // x is installed in the db already val content = WhiskActionPut(Some(sSeq.exec)) - implicit val stream = new java.io.ByteArrayOutputStream - this.outputStream = new PrintStream(stream) - try { - stream.reset() - Console.withOut(stream) { - Put(s"$collectionPath/$sSeqName", content) ~> sealRoute(routes(creds)) ~> check { - status should be(OK) - logContains(s"atomic action count ${2 * actionCnt}") - } + Console.withOut(stream) { + Put(s"$collectionPath/$sSeqName", content) ~> sealRoute(routes(creds)) ~> check { + status should be(OK) + logContains(s"atomic action count ${2 * actionCnt}")(stream) } - } finally { - stream.close() - this.outputStream.close() } } @@ -348,56 +334,49 @@ class SequenceApiTests // create an action sequence s val content = WhiskActionPut(Some(sSeq.exec)) - implicit val stream = new java.io.ByteArrayOutputStream - this.outputStream = new PrintStream(stream) - try { - stream.reset() - Console.withOut(stream) { - Put(s"$collectionPath/$sAct", content) ~> sealRoute(routes(creds)) ~> check { - status should be(OK) - } - logContains("atomic action count 4") + stream.reset() + Console.withOut(stream) { + Put(s"$collectionPath/$sAct", content) ~> sealRoute(routes(creds)) ~> check { + status should be(OK) } + logContains("atomic action count 4")(stream) + } - // update action z to point to s --- should be rejected - val zUpdate = makeSimpleSequence(zAct, namespace, Vector(s"$sAct"), false) // s in the db already - val zUpdateContent = WhiskActionPut(Some(zUpdate.exec)) - Put(s"$collectionPath/$zAct?overwrite=true", zUpdateContent) ~> sealRoute(routes(creds)) ~> check { - status should be(BadRequest) - responseAs[ErrorResponse].error shouldBe Messages.sequenceIsCyclic - } + // update action z to point to s --- should be rejected + val zUpdate = makeSimpleSequence(zAct, namespace, Vector(s"$sAct"), false) // s in the db already + val zUpdateContent = WhiskActionPut(Some(zUpdate.exec)) + Put(s"$collectionPath/$zAct?overwrite=true", zUpdateContent) ~> sealRoute(routes(creds)) ~> check { + status should be(BadRequest) + responseAs[ErrorResponse].error shouldBe Messages.sequenceIsCyclic + } - // update action s to point to a, s, b --- should be rejected - val sUpdate = makeSimpleSequence(sAct, namespace, Vector(s"$aAct", s"$sAct", s"$bAct"), false) // s in the db already - val sUpdateContent = WhiskActionPut(Some(sUpdate.exec)) - Put(s"$collectionPath/$sAct?overwrite=true", sUpdateContent) ~> sealRoute(routes(creds)) ~> check { - status should be(BadRequest) - responseAs[ErrorResponse].error shouldBe Messages.sequenceIsCyclic - } + // update action s to point to a, s, b --- should be rejected + val sUpdate = makeSimpleSequence(sAct, namespace, Vector(s"$aAct", s"$sAct", s"$bAct"), false) // s in the db already + val sUpdateContent = WhiskActionPut(Some(sUpdate.exec)) + Put(s"$collectionPath/$sAct?overwrite=true", sUpdateContent) ~> sealRoute(routes(creds)) ~> check { + status should be(BadRequest) + responseAs[ErrorResponse].error shouldBe Messages.sequenceIsCyclic + } - // update action z to point to y - val zSeq = makeSimpleSequence(zAct, namespace, Vector(s"$yAct"), false) // y in the db already - val updateContent = WhiskActionPut(Some(zSeq.exec)) - stream.reset() - Console.withOut(stream) { - Put(s"$collectionPath/$zAct?overwrite=true", updateContent) ~> sealRoute(routes(creds)) ~> check { - status should be(OK) - } - logContains("atomic action count 1") + // update action z to point to y + val zSeq = makeSimpleSequence(zAct, namespace, Vector(s"$yAct"), false) // y in the db already + val updateContent = WhiskActionPut(Some(zSeq.exec)) + stream.reset() + Console.withOut(stream) { + Put(s"$collectionPath/$zAct?overwrite=true", updateContent) ~> sealRoute(routes(creds)) ~> check { + status should be(OK) } - // update sequence s to s -> a, x, y, a, b - val newS = makeSimpleSequence(sAct, namespace, Vector(s"$aAct", s"$xAct", s"$yAct", s"$aAct", s"$bAct"), false) // a, x, y, b in the db already - val newSContent = WhiskActionPut(Some(newS.exec)) - stream.reset() - Console.withOut(stream) { - Put(s"${collectionPath}/$sAct?overwrite=true", newSContent) ~> sealRoute(routes(creds)) ~> check { - status should be(OK) - } - logContains("atomic action count 6") + logContains("atomic action count 1")(stream) + } + // update sequence s to s -> a, x, y, a, b + val newS = makeSimpleSequence(sAct, namespace, Vector(s"$aAct", s"$xAct", s"$yAct", s"$aAct", s"$bAct"), false) // a, x, y, b in the db already + val newSContent = WhiskActionPut(Some(newS.exec)) + stream.reset() + Console.withOut(stream) { + Put(s"${collectionPath}/$sAct?overwrite=true", newSContent) ~> sealRoute(routes(creds)) ~> check { + status should be(OK) } - } finally { - stream.close() - this.outputStream.close() + logContains("atomic action count 6")(stream) } } diff --git a/tests/src/whisk/core/database/test/CouchDbRestClientTests.scala b/tests/src/whisk/core/database/test/CouchDbRestClientTests.scala index 75ec733a..1e9d4965 100644 --- a/tests/src/whisk/core/database/test/CouchDbRestClientTests.scala +++ b/tests/src/whisk/core/database/test/CouchDbRestClientTests.scala @@ -35,6 +35,7 @@ import akka.actor.Props import akka.http.scaladsl.model._ import akka.stream.scaladsl._ import akka.util.ByteString +import common.StreamLogging import common.WskActorSystem import spray.json._ import spray.json.DefaultJsonProtocol._ @@ -48,7 +49,8 @@ class CouchDbRestClientTests extends FlatSpec with ScalaFutures with BeforeAndAfterAll with WskActorSystem - with DbUtils { + with DbUtils + with StreamLogging { override implicit val patienceConfig = PatienceConfig(timeout = 10.seconds, interval = 0.5.seconds) diff --git a/tests/src/whisk/core/database/test/ExtendedCouchDbRestClient.scala b/tests/src/whisk/core/database/test/ExtendedCouchDbRestClient.scala index 2ac28455..90f4fb09 100644 --- a/tests/src/whisk/core/database/test/ExtendedCouchDbRestClient.scala +++ b/tests/src/whisk/core/database/test/ExtendedCouchDbRestClient.scala @@ -19,16 +19,15 @@ import scala.concurrent.Future import akka.actor.ActorSystem import akka.http.scaladsl.model._ - import spray.json._ import spray.json.DefaultJsonProtocol._ - +import whisk.common.Logging import whisk.core.database.CouchDbRestClient /** * Implementation of additional endpoints that should only be used in testing. */ -class ExtendedCouchDbRestClient(protocol: String, host: String, port: Int, username: String, password: String, db: String)(implicit system: ActorSystem) +class ExtendedCouchDbRestClient(protocol: String, host: String, port: Int, username: String, password: String, db: String)(implicit system: ActorSystem, logging: Logging) extends CouchDbRestClient(protocol, host, port, username, password, db) { // http://docs.couchdb.org/en/1.6.1/api/server/common.html#get-- diff --git a/tests/src/whisk/core/database/test/MultipleReadersSingleWriterCacheTests.scala b/tests/src/whisk/core/database/test/MultipleReadersSingleWriterCacheTests.scala index 402b15ae..bb5dd76a 100644 --- a/tests/src/whisk/core/database/test/MultipleReadersSingleWriterCacheTests.scala +++ b/tests/src/whisk/core/database/test/MultipleReadersSingleWriterCacheTests.scala @@ -32,6 +32,7 @@ import scala.util.Success import org.scalatest.FlatSpec import org.scalatest.Matchers +import common.StreamLogging import common.WskActorSystem import whisk.common.Logging import whisk.common.TransactionId @@ -41,7 +42,7 @@ class MultipleReadersSingleWriterCacheTests(nIters: Int = 3) extends FlatSpec with Matchers with MultipleReadersSingleWriterCache[String, String] with WskActorSystem - with Logging { + with StreamLogging { "the cache" should "support simple CRUD" in { val inhibits = doReadWriteRead("foo").go(0 seconds) @@ -146,14 +147,14 @@ class MultipleReadersSingleWriterCacheTests(nIters: Int = 3) extends FlatSpec nWriteInhibits: AtomicInteger = new AtomicInteger(0)) { def debug(from: AnyRef) = { - logger.debug(from, "InhibitedReads: " + nReadInhibits); - logger.debug(from, "InhibitedWrites: " + nWriteInhibits); + logging.debug(from, "InhibitedReads: " + nReadInhibits) + logging.debug(from, "InhibitedWrites: " + nWriteInhibits) } def hasInhibits: Boolean = { nReadInhibits.get > 0 || nWriteInhibits.get > 0 } } - private case class doReadWriteRead(key: String, inhibits: Inhibits = Inhibits(), readFirst: Boolean = true) { + private case class doReadWriteRead(key: String, inhibits: Inhibits = Inhibits(), readFirst: Boolean = true)(implicit logging: Logging) { def go(implicit delay: FiniteDuration): Inhibits = { val latch = new CountDownLatch(2) @@ -208,6 +209,4 @@ class MultipleReadersSingleWriterCacheTests(nIters: Int = 3) extends FlatSpec /** we are using cache keys, so the update key is just the string itself */ override protected def cacheKeyForUpdate(w: String): String = (w) - - implicit private val logger = this } diff --git a/tests/src/whisk/core/database/test/ReplicatorTests.scala b/tests/src/whisk/core/database/test/ReplicatorTests.scala index d579d7c0..f2523145 100644 --- a/tests/src/whisk/core/database/test/ReplicatorTests.scala +++ b/tests/src/whisk/core/database/test/ReplicatorTests.scala @@ -17,6 +17,7 @@ package whisk.core.database.test import java.io.File +import java.time.Instant import scala.concurrent.duration._ import scala.language.implicitConversions @@ -28,16 +29,16 @@ import org.scalatest.concurrent.IntegrationPatience import org.scalatest.concurrent.ScalaFutures import org.scalatest.junit.JUnitRunner +import akka.http.scaladsl.model.StatusCodes +import common.StreamLogging import common.TestUtils +import common.WaitFor import common.WhiskProperties import common.WskActorSystem import spray.json._ import spray.json.DefaultJsonProtocol._ import whisk.core.WhiskConfig._ import whisk.core.WhiskConfig -import common.WaitFor -import java.time.Instant -import akka.http.scaladsl.model.StatusCodes import whisk.utils.retry @RunWith(classOf[JUnitRunner]) @@ -46,7 +47,8 @@ class ReplicatorTests extends FlatSpec with ScalaFutures with WskActorSystem with IntegrationPatience - with WaitFor { + with WaitFor + with StreamLogging { val config = new WhiskConfig(Map( dbProvider -> null, diff --git a/tests/src/whisk/core/dispatcher/test/ActivationResponseTests.scala b/tests/src/whisk/core/dispatcher/test/ActivationResponseTests.scala index ccf76731..ca15a9f6 100644 --- a/tests/src/whisk/core/dispatcher/test/ActivationResponseTests.scala +++ b/tests/src/whisk/core/dispatcher/test/ActivationResponseTests.scala @@ -25,17 +25,17 @@ import org.scalatest.junit.JUnitRunner import spray.json.pimpAny import spray.json.pimpString -import whisk.common.Logging +import whisk.common.PrintStreamLogging import whisk.core.entity.ActivationResponse._ -import whisk.http.Messages._ import whisk.core.entity.size.SizeInt +import whisk.http.Messages._ @RunWith(classOf[JUnitRunner]) class ActivationResponseTests extends FlatSpec with Matchers { behavior of "ActivationResponse" - val logger = new Logging {} + val logger = new PrintStreamLogging() it should "interpret truncated response" in { val max = 5.B diff --git a/tests/src/whisk/core/dispatcher/test/DispatcherTests.scala b/tests/src/whisk/core/dispatcher/test/DispatcherTests.scala index 1dff4733..34d6f406 100644 --- a/tests/src/whisk/core/dispatcher/test/DispatcherTests.scala +++ b/tests/src/whisk/core/dispatcher/test/DispatcherTests.scala @@ -16,7 +16,6 @@ package whisk.core.dispatcher.test -import java.io.PrintStream import java.util.concurrent.atomic.AtomicInteger import scala.concurrent.Future @@ -29,11 +28,10 @@ import org.scalatest.Matchers import org.scalatest.junit.JUnitRunner import akka.actor.actorRef2Scala -import akka.event.Logging.{ InfoLevel, DebugLevel } +import common.StreamLogging import common.WskActorSystem import spray.json.JsNumber import spray.json.JsObject -import whisk.common.Logging import whisk.common.TransactionId import whisk.core.connector.{ ActivationMessage => Message } import whisk.core.dispatcher.ActivationFeed @@ -43,7 +41,12 @@ import whisk.core.entity._ import whisk.utils.retry @RunWith(classOf[JUnitRunner]) -class DispatcherTests extends FlatSpec with Matchers with WskActorSystem { +class DispatcherTests + extends FlatSpec + with Matchers + with WskActorSystem + with StreamLogging { + implicit val transid = TransactionId.testing behavior of "Dispatcher" @@ -65,10 +68,9 @@ class DispatcherTests extends FlatSpec with Matchers with WskActorSystem { connector.send(msg) } - class TestRule(dosomething: Message => Any) extends MessageHandler("test message handler") with Logging { - setVerbosity(InfoLevel) + class TestRule(dosomething: Message => Any) extends MessageHandler("test message handler") { override def onMessage(msg: Message)(implicit transid: TransactionId): Future[Any] = { - debug(this, s"received: ${msg.content.get.compactPrint}") + logging.debug(this, s"received: ${msg.content.get.compactPrint}") Future.successful { dosomething(msg) } @@ -81,13 +83,10 @@ class DispatcherTests extends FlatSpec with Matchers with WskActorSystem { val connector = new TestConnector("test connector", maxdepth / 2, true) val messagesProcessed = new AtomicInteger() val handler = new TestRule({ msg => messagesProcessed.incrementAndGet() }) - val dispatcher = new Dispatcher(DebugLevel, connector, 100 milliseconds, maxdepth, actorSystem) + val dispatcher = new Dispatcher(connector, 100 milliseconds, maxdepth, actorSystem) dispatcher.addHandler(handler, true) dispatcher.start() - implicit val stream = new java.io.ByteArrayOutputStream - dispatcher.outputStream = new PrintStream(stream) - try { withClue("commit exception must be caught") { connector.throwCommitException = true @@ -163,8 +162,6 @@ class DispatcherTests extends FlatSpec with Matchers with WskActorSystem { } } finally { dispatcher.stop() - stream.close() - dispatcher.outputStream.close() } } } diff --git a/tests/src/whisk/core/dispatcher/test/TestConnector.scala b/tests/src/whisk/core/dispatcher/test/TestConnector.scala index e49f84a7..66226020 100644 --- a/tests/src/whisk/core/dispatcher/test/TestConnector.scala +++ b/tests/src/whisk/core/dispatcher/test/TestConnector.scala @@ -27,8 +27,8 @@ import org.apache.kafka.clients.producer.RecordMetadata import org.apache.kafka.common.TopicPartition import org.apache.kafka.common.record.Record +import common.StreamLogging import whisk.common.Counter -import whisk.common.Logging import whisk.core.connector.Message import whisk.core.connector.MessageConsumer import whisk.core.connector.MessageProducer @@ -38,7 +38,7 @@ class TestConnector( override val maxPeek: Int, allowMoreThanMax: Boolean) extends MessageConsumer - with Logging { + with StreamLogging { override def peek(duration: Duration) = { val msgs = new ArrayList[Message] @@ -62,7 +62,7 @@ class TestConnector( new Thread { override def run() = while (!closed) { val msg = queue.take() - info(this, s"received message for '$topic'") + logging.info(this, s"received message for '$topic'") process(topic, -1, -1, msg.serialize.getBytes) Thread.sleep(100) // let producer get in a send if any } @@ -83,10 +83,10 @@ class TestConnector( private val producer = new MessageProducer { def send(topic: String, msg: Message): Future[RecordMetadata] = { if (queue.offer(msg)) { - info(this, s"put: $msg") + logging.info(this, s"put: $msg") Future.successful(new RecordMetadata(new TopicPartition(topic, 0), 0, queue.size, Record.NO_TIMESTAMP, -1, -1, -1)) } else { - error(this, s"put failed: $msg") + logging.error(this, s"put failed: $msg") Future.failed(new IllegalStateException("failed to write msg")) } } diff --git a/tests/src/whisk/core/entity/test/DatastoreTests.scala b/tests/src/whisk/core/entity/test/DatastoreTests.scala index d5a419d5..7731c83e 100644 --- a/tests/src/whisk/core/entity/test/DatastoreTests.scala +++ b/tests/src/whisk/core/entity/test/DatastoreTests.scala @@ -27,28 +27,26 @@ import org.scalatest.BeforeAndAfterAll import org.scalatest.FlatSpec import org.scalatest.junit.JUnitRunner -import akka.event.Logging.InfoLevel +import common.StreamLogging +import common.WskActorSystem import whisk.core.WhiskConfig import whisk.core.database.DocumentConflictException import whisk.core.database.NoDocumentException import whisk.core.database.test.DbUtils import whisk.core.entity._ -import common.WskActorSystem - @RunWith(classOf[JUnitRunner]) class DatastoreTests extends FlatSpec with BeforeAndAfter with BeforeAndAfterAll with WskActorSystem - with DbUtils { + with DbUtils + with StreamLogging { val namespace = EntityPath("test namespace") val config = new WhiskConfig(WhiskAuthStore.requiredProperties ++ WhiskEntityStore.requiredProperties) val datastore = WhiskEntityStore.datastore(config) val authstore = WhiskAuthStore.datastore(config) - datastore.setVerbosity(InfoLevel) - authstore.setVerbosity(InfoLevel) override def afterAll() { println("Shutting down store connections") diff --git a/tests/src/whisk/core/entity/test/ViewTests.scala b/tests/src/whisk/core/entity/test/ViewTests.scala index 89f0cf63..23476fc4 100644 --- a/tests/src/whisk/core/entity/test/ViewTests.scala +++ b/tests/src/whisk/core/entity/test/ViewTests.scala @@ -18,13 +18,20 @@ package whisk.core.entity.test import java.time.Clock import java.time.Instant + import scala.concurrent.Await +import scala.language.postfixOps import scala.util.Try + import org.junit.runner.RunWith import org.scalatest.BeforeAndAfter +import org.scalatest.BeforeAndAfterAll import org.scalatest.FlatSpec import org.scalatest.Matchers import org.scalatest.junit.JUnitRunner + +import common.StreamLogging +import common.WskActorSystem import spray.json.JsObject import whisk.core.WhiskConfig import whisk.core.database.test.DbUtils @@ -32,28 +39,23 @@ import whisk.core.entity.ActivationId import whisk.core.entity.AuthKey import whisk.core.entity.Binding import whisk.core.entity.EntityName -import whisk.core.entity.Exec import whisk.core.entity.EntityPath +import whisk.core.entity.Exec +import whisk.core.entity.FullyQualifiedEntityName import whisk.core.entity.Subject import whisk.core.entity.WhiskAction import whisk.core.entity.WhiskActivation import whisk.core.entity.WhiskAuth import whisk.core.entity.WhiskEntity import whisk.core.entity.WhiskEntityQueries.listAllInNamespace -import whisk.core.entity.WhiskEntityQueries.listEntitiesInNamespace -import whisk.core.entity.WhiskEntityQueries.listCollectionInAnyNamespace import whisk.core.entity.WhiskEntityQueries.listCollectionByName +import whisk.core.entity.WhiskEntityQueries.listCollectionInAnyNamespace import whisk.core.entity.WhiskEntityQueries.listCollectionInNamespace +import whisk.core.entity.WhiskEntityQueries.listEntitiesInNamespace import whisk.core.entity.WhiskEntityStore import whisk.core.entity.WhiskPackage import whisk.core.entity.WhiskRule import whisk.core.entity.WhiskTrigger -import org.scalatest.BeforeAndAfterAll -import scala.language.postfixOps -import akka.event.Logging.InfoLevel - -import common.WskActorSystem -import whisk.core.entity.FullyQualifiedEntityName @RunWith(classOf[JUnitRunner]) class ViewTests extends FlatSpec @@ -61,7 +63,8 @@ class ViewTests extends FlatSpec with BeforeAndAfterAll with Matchers with DbUtils - with WskActorSystem { + with WskActorSystem + with StreamLogging { def aname = MakeName.next("viewtests") def afullname(namespace: EntityPath) = FullyQualifiedEntityName(namespace, aname) @@ -82,7 +85,6 @@ class ViewTests extends FlatSpec val config = new WhiskConfig(WhiskEntityStore.requiredProperties) val datastore = WhiskEntityStore.datastore(config) - datastore.setVerbosity(InfoLevel) after { cleanup() -- GitLab