|
| 1 | +package izumi.distage.testkit.docker |
| 2 | + |
| 3 | +import com.github.dockerjava.api.exception.NotFoundException |
| 4 | +import distage.{DefaultModule2, ModuleDef, TagKK} |
| 5 | +import izumi.distage.docker.ContainerDef |
| 6 | +import izumi.distage.docker.healthcheck.ContainerHealthCheck |
| 7 | +import izumi.distage.docker.impl.{ContainerResource, DockerClientWrapper} |
| 8 | +import izumi.distage.docker.model.Docker.DockerReusePolicy |
| 9 | +import izumi.distage.testkit.docker.DockerPullWithPlatformTest.{HelloWorldRiscV64Docker, imageName} |
| 10 | +import izumi.distage.testkit.model.TestConfig |
| 11 | +import izumi.distage.testkit.scalatest.Spec2 |
| 12 | +import izumi.functional.bio.{F, IO2} |
| 13 | +import org.scalatest.Assertion |
| 14 | + |
| 15 | +final class DockerPullWithPlatformTestZIO extends DockerPullWithPlatformTest[zio.IO] |
| 16 | + |
| 17 | +object DockerPullWithPlatformTest { |
| 18 | + val imageName = "library/hello-world:latest" |
| 19 | + |
| 20 | + object HelloWorldRiscV64Docker extends ContainerDef { |
| 21 | + override def config: Config = { |
| 22 | + Config( |
| 23 | + image = imageName, |
| 24 | + ports = Seq.empty, |
| 25 | + autoRemove = false, |
| 26 | + reuse = DockerReusePolicy.ReuseDisabled, |
| 27 | + // succeed: we only care about the image pull, not the container's runtime behavior; |
| 28 | + // the riscv64 binary won't execute on a non-riscv64 host anyway |
| 29 | + healthCheck = ContainerHealthCheck.succeed, |
| 30 | + platform = Some("linux/riscv64"), |
| 31 | + ) |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +abstract class DockerPullWithPlatformTest[F[+_, +_]: DefaultModule2: TagKK: IO2] extends Spec2[F] { |
| 37 | + |
| 38 | + override protected def config: TestConfig = super.config.copy( |
| 39 | + moduleOverrides = new ModuleDef { |
| 40 | + make[ContainerResource[F[Throwable, _], HelloWorldRiscV64Docker.Tag]] |
| 41 | + .from(HelloWorldRiscV64Docker.make[F[Throwable, _]]) |
| 42 | + } |
| 43 | + ) |
| 44 | + |
| 45 | + "Platform-specific image pull" should { |
| 46 | + |
| 47 | + "pull hello-world for linux/riscv64 and verify image architecture" in { |
| 48 | + (client: DockerClientWrapper[F[Throwable, _]], containerResource: ContainerResource[F[Throwable, _], HelloWorldRiscV64Docker.Tag]) => |
| 49 | + def removeImage(): F[Nothing, Unit] = { |
| 50 | + F.syncThrowable(client.rawClient.removeImageCmd(imageName).withForce(true).exec()).void.catchAll(_ => F.unit) |
| 51 | + } |
| 52 | + |
| 53 | + def verifyImageNotPulled: F[Throwable, NotFoundException] = { |
| 54 | + F.syncThrowable { |
| 55 | + intercept[NotFoundException](client.rawClient.inspectImageCmd(imageName).exec()) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + def verifyImagePulled: F[Throwable, Assertion] = { |
| 60 | + F.syncThrowable { |
| 61 | + val inspection = client.rawClient.inspectImageCmd(imageName).exec() |
| 62 | + assert(inspection.getArch == "riscv64") |
| 63 | + assert(inspection.getOs == "linux") |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + (for { |
| 68 | + _ <- removeImage() |
| 69 | + _ <- verifyImageNotPulled |
| 70 | + _ <- containerResource.use(_ => verifyImagePulled) |
| 71 | + } yield ()).guarantee(removeImage()) |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments