|
| 1 | +package latis.util |
| 2 | + |
| 3 | +import scala.concurrent.duration.Duration |
| 4 | + |
| 5 | +import cats.effect.IO |
| 6 | +import cats.effect.Resource |
| 7 | +import com.dimafeng.testcontainers.LocalStackV2Container |
| 8 | +import munit.catseffect.IOFixture |
| 9 | +import org.testcontainers.containers.localstack.LocalStackContainer.Service |
| 10 | +import software.amazon.awssdk.core.async.AsyncRequestBody |
| 11 | +import software.amazon.awssdk.services.s3.S3AsyncClient |
| 12 | +import software.amazon.awssdk.services.s3.model.CreateBucketRequest |
| 13 | +import software.amazon.awssdk.services.s3.model.PutObjectRequest |
| 14 | + |
| 15 | +import latis.util.S3Utils.* |
| 16 | + |
| 17 | +class S3UtilsSuite extends munit.CatsEffectSuite { |
| 18 | + |
| 19 | + //Starting the test container is slow. |
| 20 | + override val munitIOTimeout: Duration = Duration(1, "minute") |
| 21 | + |
| 22 | + private def makeBucket(client: S3AsyncClient, bucket: String) = { |
| 23 | + IO.fromCompletableFuture { |
| 24 | + IO { |
| 25 | + client.createBucket( |
| 26 | + CreateBucketRequest.builder().bucket(bucket).build() |
| 27 | + ) |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + private def putEmpty(client: S3AsyncClient, bucket: String, key: String) = |
| 33 | + IO.fromCompletableFuture { |
| 34 | + IO { |
| 35 | + client.putObject( |
| 36 | + PutObjectRequest.builder().bucket(bucket).key(key).build(), |
| 37 | + AsyncRequestBody.empty() |
| 38 | + ) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + val s3Client: IOFixture[S3AsyncClient] = { |
| 43 | + val localStack: Resource[IO, LocalStackV2Container] = { |
| 44 | + val mkContainer = IO { |
| 45 | + LocalStackV2Container(services = List(Service.S3)) |
| 46 | + }.flatTap(container => IO(container.start())) |
| 47 | + Resource.make(mkContainer)(container => IO(container.stop())) |
| 48 | + } |
| 49 | + |
| 50 | + val client: Resource[IO, S3AsyncClient] = localStack.flatMap { ls => |
| 51 | + val mkClient = IO { |
| 52 | + S3AsyncClient |
| 53 | + .builder() |
| 54 | + .endpointOverride(ls.endpointOverride(Service.S3)) |
| 55 | + .credentialsProvider(ls.staticCredentialsProvider) |
| 56 | + .region(ls.region) |
| 57 | + .build() |
| 58 | + } |
| 59 | + Resource.make(mkClient)(client => IO(client.close())) |
| 60 | + }.evalTap { client => |
| 61 | + makeBucket(client, "foobar") |
| 62 | + >> putEmpty(client, "foobar", "foo") |
| 63 | + >> putEmpty(client, "foobar", "bar") |
| 64 | + >> makeBucket(client, "barfoo") |
| 65 | + >> putEmpty(client, "foobar", "bar") |
| 66 | + >> putEmpty(client, "foobar", "foo") |
| 67 | + } |
| 68 | + |
| 69 | + ResourceSuiteLocalFixture("s3-client", client) |
| 70 | + } |
| 71 | + |
| 72 | + override val munitFixtures = List(s3Client) |
| 73 | + |
| 74 | + test("ordering") { |
| 75 | + IO(s3Client()).flatTap { client => |
| 76 | + getKeysF[IO](client, "foobar", "/").flatMap { stream => |
| 77 | + stream.compile.toList.map { |
| 78 | + case "bar" :: "foo" :: Nil => () |
| 79 | + case _ => fail("Objects not ordered as expected.") |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + //=== Test with NOAA data ===// |
| 86 | + |
| 87 | + val bucket = "noaa-nesdis-swfo-ccor-1-pds" |
| 88 | + val prefix = "SWFO/GOES-19/CCOR-1/ccor1-l3_science/" |
| 89 | + val first = "SWFO/GOES-19/CCOR-1/ccor1-l3_science/2025/02/25/" + |
| 90 | + "sci_ccor1-l3_g19_s20250225T000020Z_e20250225T000048Z_p20250401T234952Z_pub.fits" |
| 91 | + |
| 92 | + test("pure client") { |
| 93 | + val client = makeClient() |
| 94 | + val key = getKeys(client, bucket, prefix).toList.head |
| 95 | + assertEquals(first, key) |
| 96 | + } |
| 97 | + |
| 98 | + test("effectful client") { |
| 99 | + for { |
| 100 | + client <- makeClientF[IO]() |
| 101 | + keys <- getKeysF[IO](client, bucket, prefix) |
| 102 | + last <- keys.head.compile.last |
| 103 | + key <- IO.fromOption(last)(fail("Empty list of keys")) //TODO: does this failure work? |
| 104 | + } yield { |
| 105 | + assertEquals(first, key) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + test("chunk size") { |
| 110 | + for { |
| 111 | + client <- makeClientF[IO]() |
| 112 | + keys <- getKeysF[IO](client, bucket, prefix) |
| 113 | + last <- keys.chunks.head.compile.last |
| 114 | + chunk <- IO.fromOption(last)(fail("Empty list of keys")) //TODO: does this failure work? |
| 115 | + } yield { |
| 116 | + assertEquals(1000, chunk.size) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments