diff --git a/README.md b/README.md index ca61ffaa..13cf98cb 100644 --- a/README.md +++ b/README.md @@ -34,3 +34,14 @@ write(p, "some data") read(p, byte_range=1:4) # returns b"some" ``` +## Testing + +Some of the tests involve using a temporary AWS S3 bucket. For these tests to succeed you'll +need to set your current AWS profile to use a role which allows for `s3:*` access to the `arn:aws:s3:::ocaws.jl.test.*` resource. + +If you do not have AWS access or lack the required permission you can use the +`AWSS3_TESTSETS` environmental variable to control which testsets run: + +```bash +AWSS3_TESTSETS=MinIO julia --project -e 'using Pkg; Pkg.test()' +``` \ No newline at end of file diff --git a/test/awss3.jl b/test/awss3.jl index ec309925..cfc1f4d7 100644 --- a/test/awss3.jl +++ b/test/awss3.jl @@ -1,14 +1,23 @@ +_has_failed(testset::Test.DefaultTestSet) = any(r -> !(r isa Test.Pass), testset.results) + function awss3_tests(config) - bucket_name = - "ocaws.jl.test." * lowercase(Dates.format(now(Dates.UTC), "yyyymmddTHHMMSSZ")) + bucket_name = begin + "ocaws.jl.test." * Dates.format(now(Dates.UTC), dateformat"yyyymmdd\tHHMMSS\z") + end - @testset "Create Bucket" begin + t = @testset "Create Bucket" begin s3_create_bucket(config, bucket_name) + @test bucket_name in s3_list_buckets(config) is_aws(config) && s3_enable_versioning(config, bucket_name) sleep(1) end + if _has_failed(t) + @warn "Bucket creation has failed. Skipping remaining tests which rely on bucket: $bucket_name" + return nothing + end + @testset "Bucket Tagging" begin @test isempty(s3_get_tags(config, bucket_name)) tags = Dict("A" => "1", "B" => "2", "C" => "3") diff --git a/test/runtests.jl b/test/runtests.jl index 82a9a5ef..102e67af 100755 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -12,6 +12,8 @@ using FilePathsBase.TestPaths using UUIDs: uuid4 using JSON3 +const AWSS3_TESTSETS = split(get(ENV, "AWSS3_TESTSETS", "MinIO,S3"), ',') + is_aws(config) = config isa AWSConfig # Load the test functions @@ -19,39 +21,48 @@ include("s3path.jl") # creates `awss3_tests(config)` include("awss3.jl") # creates `s3path_tests(config)` @testset "AWSS3.jl" begin - if VERSION >= v"1.5" - using Minio - AWS.aws_account_number(::Minio.MinioConfig) = "123" - - # We run most tests under Minio. This can be done locally by those - # without access to the s3 bucket under which CI is performed. - # We then run all tests with s3 directly. - - port = 9005 - minio_server = Minio.Server([mktempdir()]; address="localhost:$port") - - try - run(minio_server; wait=false) - sleep(0.5) # give the server just a bit of time, though it is amazingly fast to start - config = global_aws_config( - MinioConfig( - "http://localhost:$port"; username="minioadmin", password="minioadmin" - ), + @testset "MinIO" begin + if "MinIO" in AWSS3_TESTSETS && VERSION >= v"1.5" + using Minio + AWS.aws_account_number(::Minio.MinioConfig) = "123" + + # We run most tests under Minio. This can be done locally by those + # without access to the s3 bucket under which CI is performed. + # We then run all tests with s3 directly. + + port = 9005 + minio_server = Minio.Server([mktempdir()]; address="localhost:$port") + + minio_config = MinioConfig( + "http://localhost:$port"; username="minioadmin", password="minioadmin" ) - @testset "Minio" begin - awss3_tests(config) - s3path_tests(config) + + try + run(minio_server; wait=false) + sleep(0.5) # give the server just a bit of time, though it is amazingly fast to start + + awss3_tests(minio_config) + s3path_tests(minio_config) + finally + # Make sure we kill the server even if a test failed. + kill(minio_server) end - finally - # Make sure we kill the server even if a test failed. - kill(minio_server) + elseif VERSION < v"1.5" + @warn "Skipping MinIO tests as they can only be run on Julia ≥ 1.5" + else + @warn "Skipping MinIO tests" end end - # Set `AWSConfig` as the default for the following tests - aws = global_aws_config(AWSConfig()) @testset "S3" begin - awss3_tests(aws) - s3path_tests(aws) + if "S3" in AWSS3_TESTSETS + # Set `AWSConfig` as the default for the following tests + aws_config = AWSConfig() + + awss3_tests(aws_config) + s3path_tests(aws_config) + else + @warn "Skipping S3 tests" + end end end diff --git a/test/s3path.jl b/test/s3path.jl index c3f7fa4c..c878e5bc 100644 --- a/test/s3path.jl +++ b/test/s3path.jl @@ -285,8 +285,9 @@ end # This is the main entrypoint for the S3Path tests function s3path_tests(config) - bucket_name = - "ocaws.jl.test." * lowercase(Dates.format(now(Dates.UTC), "yyyymmddTHHMMSSZ")) + bucket_name = begin + "ocaws.jl.test." * Dates.format(now(Dates.UTC), dateformat"yyyymmdd\tHHMMSS\z") + end s3_create_bucket(config, bucket_name) root = Path("s3://$bucket_name/pathset-root/")