From afabb584c879683f73c7a048899e011475389707 Mon Sep 17 00:00:00 2001 From: Robert Autenrieth <31539813+rautenrieth-da@users.noreply.github.com> Date: Tue, 24 Sep 2019 15:17:18 +0200 Subject: [PATCH] Move PackageServiceIT to the ledger api test tool --- .../ledger/api/PackageServiceIT.scala | 126 ------------------ .../infrastructure/LedgerServices.scala | 3 + .../infrastructure/LedgerTestSuite.scala | 6 +- .../participant/ParticipantTestContext.scala | 18 +++ .../ledger/api/testtool/tests/Packages.scala | 81 +++++++++++ .../ledger/api/testtool/tests/package.scala | 1 + 6 files changed, 107 insertions(+), 128 deletions(-) delete mode 100644 ledger/ledger-api-integration-tests/src/test/itsuite/scala/com/digitalasset/platform/tests/integration/ledger/api/PackageServiceIT.scala create mode 100644 ledger/ledger-api-test-tool/src/main/scala/com/daml/ledger/api/testtool/tests/Packages.scala diff --git a/ledger/ledger-api-integration-tests/src/test/itsuite/scala/com/digitalasset/platform/tests/integration/ledger/api/PackageServiceIT.scala b/ledger/ledger-api-integration-tests/src/test/itsuite/scala/com/digitalasset/platform/tests/integration/ledger/api/PackageServiceIT.scala deleted file mode 100644 index eed49869546..00000000000 --- a/ledger/ledger-api-integration-tests/src/test/itsuite/scala/com/digitalasset/platform/tests/integration/ledger/api/PackageServiceIT.scala +++ /dev/null @@ -1,126 +0,0 @@ -// Copyright (c) 2019 The DAML Authors. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.digitalasset.platform.tests.integration.ledger.api - -import java.util.UUID - -import com.digitalasset.ledger.api.domain -import com.digitalasset.ledger.api.testing.utils.{ - AkkaBeforeAndAfterAll, - IsStatusException, - SuiteResourceManagementAroundAll -} -import com.digitalasset.ledger.api.v1.package_service.PackageStatus -import com.digitalasset.ledger.client.services.pkg.PackageClient -import com.digitalasset.platform.apitesting.{LedgerContext, MultiLedgerFixture} -import io.grpc.Status -import org.scalatest.concurrent.AsyncTimeLimitedTests -import org.scalatest.time.Span -import org.scalatest.time.SpanSugar._ -import org.scalatest.{AsyncWordSpec, Matchers, OptionValues} - -import scalaz.syntax.tag._ - -@SuppressWarnings(Array("org.wartremover.warts.Any")) -class PackageServiceIT - extends AsyncWordSpec - with AkkaBeforeAndAfterAll - with MultiLedgerFixture - with SuiteResourceManagementAroundAll - with AsyncTimeLimitedTests - with Matchers - with OptionValues { - - override def timeLimit: Span = scaled(5.seconds) - - private def client(ctx: LedgerContext): PackageClient = { - new PackageClient(ctx.ledgerId, ctx.packageService) - } - - private def getARegisteredPackageId(ctx: LedgerContext) = - client(ctx).listPackages().map(_.packageIds.headOption.value) - - "Package Service" when { - - "asked for the list of registered packages" should { - - "return it" in allFixtures { context => - client(context).listPackages() map { - _.packageIds.size shouldEqual 3 // package, stdlib, daml-prim - } - } - - "fail with the expected status on a ledger Id mismatch" in allFixtures { context => - new PackageClient( - domain.LedgerId(s"not-${context.ledgerId.unwrap}"), - context.packageService) - .listPackages() - .failed map { - IsStatusException(Status.NOT_FOUND)(_) - } - - } - } - - "asked to get a package" should { - - "return it if it's registered" in allFixtures { context => - getARegisteredPackageId(context) - .flatMap(client(context).getPackage(_)) map { - _.archivePayload.size() should be > 0 - } - - } - - "return a NOT_FOUND error if it's not registered" in allFixtures { context => - client(context).getPackage(UUID.randomUUID().toString).failed map { - IsStatusException(Status.NOT_FOUND)(_) - } - } - - "fail with the expected status on a ledger Id mismatch" in allFixtures { context => - getARegisteredPackageId(context) - .flatMap( - new PackageClient( - domain.LedgerId(s"not-${context.ledgerId.unwrap}"), - context.packageService) - .getPackage(_) - .failed) map { - IsStatusException(Status.NOT_FOUND)(_) - } - } - } - - "asked to check a package's status" should { - - "return true if it's registered" in allFixtures { context => - getARegisteredPackageId(context) - .flatMap(client(context).getPackageStatus(_)) map { - _.packageStatus shouldEqual PackageStatus.REGISTERED - } - } - - "return false if it's not registered" in allFixtures { context => - client(context).getPackageStatus(UUID.randomUUID().toString) map { - _.packageStatus shouldEqual PackageStatus.UNKNOWN - } - - } - - "fail with the expected status on a ledger Id mismatch" in allFixtures { context => - getARegisteredPackageId(context) - .flatMap( - new PackageClient( - domain.LedgerId(s"not-${context.ledgerId.unwrap}"), - context.packageService) - .getPackageStatus(_) - .failed) map { - IsStatusException(Status.NOT_FOUND)(_) - } - } - } - } - - override protected def config: Config = Config.default -} diff --git a/ledger/ledger-api-test-tool/src/main/scala/com/daml/ledger/api/testtool/infrastructure/LedgerServices.scala b/ledger/ledger-api-test-tool/src/main/scala/com/daml/ledger/api/testtool/infrastructure/LedgerServices.scala index 8476e82a92f..583ed969ef9 100644 --- a/ledger/ledger-api-test-tool/src/main/scala/com/daml/ledger/api/testtool/infrastructure/LedgerServices.scala +++ b/ledger/ledger-api-test-tool/src/main/scala/com/daml/ledger/api/testtool/infrastructure/LedgerServices.scala @@ -13,6 +13,8 @@ import com.digitalasset.ledger.api.v1.command_service.CommandServiceGrpc import com.digitalasset.ledger.api.v1.command_service.CommandServiceGrpc.CommandService import com.digitalasset.ledger.api.v1.ledger_identity_service.LedgerIdentityServiceGrpc import com.digitalasset.ledger.api.v1.ledger_identity_service.LedgerIdentityServiceGrpc.LedgerIdentityService +import com.digitalasset.ledger.api.v1.package_service.PackageServiceGrpc +import com.digitalasset.ledger.api.v1.package_service.PackageServiceGrpc.PackageService import com.digitalasset.ledger.api.v1.testing.time_service.TimeServiceGrpc import com.digitalasset.ledger.api.v1.testing.time_service.TimeServiceGrpc.TimeService import com.digitalasset.ledger.api.v1.transaction_service.TransactionServiceGrpc @@ -25,6 +27,7 @@ private[infrastructure] final class LedgerServices(channel: Channel) { val identity: LedgerIdentityService = LedgerIdentityServiceGrpc.stub(channel) val partyManagement: PartyManagementService = PartyManagementServiceGrpc.stub(channel) val packageManagement: PackageManagementService = PackageManagementServiceGrpc.stub(channel) + val packages: PackageService = PackageServiceGrpc.stub(channel) val transaction: TransactionService = TransactionServiceGrpc.stub(channel) val time: TimeService = TimeServiceGrpc.stub(channel) } diff --git a/ledger/ledger-api-test-tool/src/main/scala/com/daml/ledger/api/testtool/infrastructure/LedgerTestSuite.scala b/ledger/ledger-api-test-tool/src/main/scala/com/daml/ledger/api/testtool/infrastructure/LedgerTestSuite.scala index 8d74e0457e1..d7cda922554 100644 --- a/ledger/ledger-api-test-tool/src/main/scala/com/daml/ledger/api/testtool/infrastructure/LedgerTestSuite.scala +++ b/ledger/ledger-api-test-tool/src/main/scala/com/daml/ledger/api/testtool/infrastructure/LedgerTestSuite.scala @@ -90,9 +90,11 @@ private[testtool] abstract class LedgerTestSuite(val session: LedgerSession) { "Exception is neither a StatusRuntimeException nor a StatusException") } assert(actualCode == expectedCode, s"Expected code [$expectedCode], but got [$actualCode].") + // Note: Status.getDescription() is nullable, map missing descriptions to an empty string + val nonNullMessage = Option(message).getOrElse("") assert( - message.contains(pattern), - s"Error message did not contain [$pattern], but was [$message].") + nonNullMessage.contains(pattern), + s"Error message did not contain [$pattern], but was [$nonNullMessage].") } /** diff --git a/ledger/ledger-api-test-tool/src/main/scala/com/daml/ledger/api/testtool/infrastructure/participant/ParticipantTestContext.scala b/ledger/ledger-api-test-tool/src/main/scala/com/daml/ledger/api/testtool/infrastructure/participant/ParticipantTestContext.scala index 545ed94450b..fdeff0849e3 100644 --- a/ledger/ledger-api-test-tool/src/main/scala/com/daml/ledger/api/testtool/infrastructure/participant/ParticipantTestContext.scala +++ b/ledger/ledger-api-test-tool/src/main/scala/com/daml/ledger/api/testtool/infrastructure/participant/ParticipantTestContext.scala @@ -30,6 +30,13 @@ import com.digitalasset.ledger.api.v1.commands.{Command, Commands} import com.digitalasset.ledger.api.v1.event.Event.Event.Created import com.digitalasset.ledger.api.v1.event.{CreatedEvent, Event} import com.digitalasset.ledger.api.v1.ledger_offset.LedgerOffset +import com.digitalasset.ledger.api.v1.package_service.{ + GetPackageRequest, + GetPackageResponse, + GetPackageStatusRequest, + ListPackagesRequest, + PackageStatus +} import com.digitalasset.ledger.api.v1.testing.time_service.{ GetTimeRequest, GetTimeResponse, @@ -156,6 +163,17 @@ private[testtool] final class ParticipantTestContext private[participant] ( def participantId(): Future[String] = services.partyManagement.getParticipantId(new GetParticipantIdRequest).map(_.participantId) + def listPackages(): Future[Seq[String]] = + services.packages.listPackages(new ListPackagesRequest(ledgerId)).map(_.packageIds) + + def getPackage(packageId: String): Future[GetPackageResponse] = + services.packages.getPackage(new GetPackageRequest(ledgerId, packageId)) + + def getPackageStatus(packageId: String): Future[PackageStatus] = + services.packages + .getPackageStatus(new GetPackageStatusRequest(ledgerId, packageId)) + .map(_.packageStatus) + /** * Managed version of party allocation, should be used anywhere a party has * to be allocated unless the party management service itself is under test diff --git a/ledger/ledger-api-test-tool/src/main/scala/com/daml/ledger/api/testtool/tests/Packages.scala b/ledger/ledger-api-test-tool/src/main/scala/com/daml/ledger/api/testtool/tests/Packages.scala new file mode 100644 index 00000000000..3b34757f7d8 --- /dev/null +++ b/ledger/ledger-api-test-tool/src/main/scala/com/daml/ledger/api/testtool/tests/Packages.scala @@ -0,0 +1,81 @@ +// Copyright (c) 2019 The DAML Authors. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.daml.ledger.api.testtool.tests + +import com.daml.ledger.api.testtool.infrastructure.{LedgerSession, LedgerTest, LedgerTestSuite} +import io.grpc.Status + +final class Packages(session: LedgerSession) extends LedgerTestSuite(session) { + + /** A package ID that is guaranteed to not be uploaded */ + private[this] val unknownPackageId = " " + + private[this] val listPackages = + LedgerTest("PackagesList", "Listing packages should return a result") { context => + for { + ledger <- context.participant() + knownPackages <- ledger.listPackages() + } yield + assert( + knownPackages.size >= 3, + s"List of packages was expected to contain at least 3 packages, got ${knownPackages.size} instead.") + } + + private[this] val getPackage = + LedgerTest("PackagesGet", "Getting package content should return a valid result") { context => + for { + ledger <- context.participant() + somePackageId <- ledger.listPackages().map(_.headOption.getOrElse(fail("No package found"))) + somePackage <- ledger.getPackage(somePackageId) + } yield { + assert(somePackage.hash.length > 0, s"Package $somePackageId has an empty hash.") + assert( + somePackage.hash == somePackageId, + s"Package $somePackageId has hash ${somePackage.hash}, expected hash to be equal to the package ID.") + assert(somePackage.archivePayload.size() >= 0, s"Package $somePackageId has zero size.") + } + } + + private[this] val getUnknownPackage = + LedgerTest("PackagesGetUnknown", "Getting package content for an unknown package should fail") { + context => + for { + ledger <- context.participant() + failure <- ledger.getPackage(unknownPackageId).failed + } yield { + assertGrpcError(failure, Status.Code.NOT_FOUND, "") + } + } + + private[this] val getPackageStatus = + LedgerTest("PackagesStatus", "Getting package status should return a valid result") { context => + for { + ledger <- context.participant() + somePackageId <- ledger.listPackages().map(_.headOption.getOrElse(fail("No package found"))) + status <- ledger.getPackageStatus(somePackageId) + } yield { + assert(status.isRegistered, s"Package $somePackageId is not registered.") + } + } + + private[this] val getUnknownPackageStatus = + LedgerTest("PackagesStatusUnknown", "Getting package status for an unknown package should fail") { + context => + for { + ledger <- context.participant() + status <- ledger.getPackageStatus(unknownPackageId) + } yield { + assert(status.isUnknown, s"Package $unknownPackageId is not unknown.") + } + } + + override val tests: Vector[LedgerTest] = Vector( + listPackages, + getPackage, + getUnknownPackage, + getPackageStatus, + getUnknownPackageStatus + ) + +} diff --git a/ledger/ledger-api-test-tool/src/main/scala/com/daml/ledger/api/testtool/tests/package.scala b/ledger/ledger-api-test-tool/src/main/scala/com/daml/ledger/api/testtool/tests/package.scala index de0e81b23d2..f983d768ada 100644 --- a/ledger/ledger-api-test-tool/src/main/scala/com/daml/ledger/api/testtool/tests/package.scala +++ b/ledger/ledger-api-test-tool/src/main/scala/com/daml/ledger/api/testtool/tests/package.scala @@ -26,6 +26,7 @@ package object tests { "ContractKeysSubmitterIsMaintainerIT" -> (new ContractKeysSubmitterIsMaintainer(_)), "DivulgenceIT" -> (new Divulgence(_)), "IdentityIT" -> (new Identity(_)), + "PackageServiceIT" -> (new Packages(_)), "PackageManagementServiceIT" -> (new PackageManagement(_)), "PartyManagementServiceIT" -> (new PartyManagement(_)), "TimeIT" -> (new Time(_)),