forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3fs_test.cc
More file actions
1259 lines (1070 loc) · 47.5 KB
/
Copy paths3fs_test.cc
File metadata and controls
1259 lines (1070 loc) · 47.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
#include <algorithm>
#include <exception>
#include <memory>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <gmock/gmock-matchers.h>
#include <gtest/gtest.h>
#ifdef _WIN32
// Undefine preprocessor macros that interfere with AWS function / method names
#ifdef GetMessage
#undef GetMessage
#endif
#ifdef GetObject
#undef GetObject
#endif
#endif
#include <aws/core/Aws.h>
#include <aws/core/Version.h>
#include <aws/core/auth/AWSCredentials.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/core/client/CoreErrors.h>
#include <aws/core/client/RetryStrategy.h>
#include <aws/core/utils/logging/ConsoleLogSystem.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/CreateBucketRequest.h>
#include <aws/s3/model/GetObjectRequest.h>
#include <aws/s3/model/PutObjectRequest.h>
#include <aws/sts/STSClient.h>
#include "arrow/filesystem/filesystem.h"
#include "arrow/filesystem/s3_internal.h"
#include "arrow/filesystem/s3_test_util.h"
#include "arrow/filesystem/s3fs.h"
#include "arrow/filesystem/test_util.h"
#include "arrow/result.h"
#include "arrow/status.h"
#include "arrow/testing/future_util.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/testing/util.h"
#include "arrow/util/async_generator.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/future.h"
#include "arrow/util/io_util.h"
#include "arrow/util/key_value_metadata.h"
#include "arrow/util/logging.h"
#include "arrow/util/macros.h"
namespace arrow {
namespace fs {
using ::arrow::internal::checked_pointer_cast;
using ::arrow::internal::PlatformFilename;
using ::arrow::internal::UriEscape;
using ::arrow::fs::internal::ConnectRetryStrategy;
using ::arrow::fs::internal::ErrorToStatus;
using ::arrow::fs::internal::OutcomeToStatus;
using ::arrow::fs::internal::ToAwsString;
// Use "short" retry parameters to make tests faster
static constexpr int32_t kRetryInterval = 50; /* milliseconds */
static constexpr int32_t kMaxRetryDuration = 6000; /* milliseconds */
::testing::Environment* s3_env = ::testing::AddGlobalTestEnvironment(new S3Environment);
::testing::Environment* minio_env =
::testing::AddGlobalTestEnvironment(new MinioTestEnvironment);
MinioTestEnvironment* GetMinioEnv() {
return ::arrow::internal::checked_cast<MinioTestEnvironment*>(minio_env);
}
class ShortRetryStrategy : public S3RetryStrategy {
public:
bool ShouldRetry(const AWSErrorDetail& error, int64_t attempted_retries) override {
if (error.message.find(kFileExistsMessage) != error.message.npos) {
// Minio returns "file exists" errors (when calling mkdir) as internal errors,
// which would trigger spurious retries.
return false;
}
return IsRetryable(error) && (attempted_retries * kRetryInterval < kMaxRetryDuration);
}
int64_t CalculateDelayBeforeNextRetry(const AWSErrorDetail& error,
int64_t attempted_retries) override {
return kRetryInterval;
}
bool IsRetryable(const AWSErrorDetail& error) const {
return error.should_retry || error.exception_name == "XMinioServerNotInitialized";
}
#ifdef _WIN32
static constexpr const char* kFileExistsMessage = "file already exists";
#else
static constexpr const char* kFileExistsMessage = "file exists";
#endif
};
// NOTE: Connecting in Python:
// >>> fs = s3fs.S3FileSystem(key='minio', secret='miniopass',
// client_kwargs=dict(endpoint_url='http://127.0.0.1:9000'))
// >>> fs.ls('')
// ['bucket']
// or:
// >>> from fs_s3fs import S3FS
// >>> fs = S3FS('bucket', endpoint_url='http://127.0.0.1:9000',
// aws_access_key_id='minio', aws_secret_access_key='miniopass')
#define ARROW_AWS_ASSIGN_OR_FAIL_IMPL(outcome_name, lhs, rexpr) \
auto outcome_name = (rexpr); \
if (!outcome_name.IsSuccess()) { \
FAIL() << "'" ARROW_STRINGIFY(rexpr) "' failed with " \
<< outcome_name.GetError().GetMessage(); \
} \
lhs = std::move(outcome_name).GetResultWithOwnership();
#define ARROW_AWS_ASSIGN_OR_FAIL_NAME(x, y) ARROW_CONCAT(x, y)
#define ARROW_AWS_ASSIGN_OR_FAIL(lhs, rexpr) \
ARROW_AWS_ASSIGN_OR_FAIL_IMPL( \
ARROW_AWS_ASSIGN_OR_FAIL_NAME(_aws_error_or_value, __COUNTER__), lhs, rexpr);
class AwsTestMixin : public ::testing::Test {
public:
// We set this environment variable to speed up tests by ensuring
// DefaultAWSCredentialsProviderChain does not query (inaccessible)
// EC2 metadata endpoint
AwsTestMixin() : ec2_metadata_disabled_guard_("AWS_EC2_METADATA_DISABLED", "true") {}
void SetUp() override {
#ifdef AWS_CPP_SDK_S3_NOT_SHARED
auto aws_log_level = Aws::Utils::Logging::LogLevel::Fatal;
aws_options_.loggingOptions.logLevel = aws_log_level;
aws_options_.loggingOptions.logger_create_fn = [&aws_log_level] {
return std::make_shared<Aws::Utils::Logging::ConsoleLogSystem>(aws_log_level);
};
Aws::InitAPI(aws_options_);
#endif
}
void TearDown() override {
#ifdef AWS_CPP_SDK_S3_NOT_SHARED
Aws::ShutdownAPI(aws_options_);
#endif
}
private:
EnvVarGuard ec2_metadata_disabled_guard_;
#ifdef AWS_CPP_SDK_S3_NOT_SHARED
Aws::SDKOptions aws_options_;
#endif
};
class S3TestMixin : public AwsTestMixin {
public:
void SetUp() override {
AwsTestMixin::SetUp();
// Starting the server may fail, for example if the generated port number
// was "stolen" by another process. Run a dummy S3 operation to make sure it
// is running, otherwise retry a number of times.
Status connect_status;
int retries = kNumServerRetries;
do {
InitServerAndClient();
connect_status = OutcomeToStatus("ListBuckets", client_->ListBuckets());
} while (!connect_status.ok() && --retries > 0);
ASSERT_OK(connect_status);
}
void TearDown() override { AwsTestMixin::TearDown(); }
protected:
void InitServerAndClient() {
ASSERT_OK_AND_ASSIGN(minio_, GetMinioEnv()->GetOneServer());
client_config_.reset(new Aws::Client::ClientConfiguration());
client_config_->endpointOverride = ToAwsString(minio_->connect_string());
client_config_->scheme = Aws::Http::Scheme::HTTP;
client_config_->retryStrategy =
std::make_shared<ConnectRetryStrategy>(kRetryInterval, kMaxRetryDuration);
credentials_ = {ToAwsString(minio_->access_key()), ToAwsString(minio_->secret_key())};
bool use_virtual_addressing = false;
client_.reset(
new Aws::S3::S3Client(credentials_, *client_config_,
Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never,
use_virtual_addressing));
}
// How many times to try launching a server in a row before decreeing failure
static constexpr int kNumServerRetries = 3;
std::shared_ptr<MinioTestServer> minio_;
std::unique_ptr<Aws::Client::ClientConfiguration> client_config_;
Aws::Auth::AWSCredentials credentials_;
std::unique_ptr<Aws::S3::S3Client> client_;
};
void AssertGetObject(Aws::S3::Model::GetObjectResult& result,
const std::string& expected) {
auto length = static_cast<int64_t>(expected.length());
ASSERT_EQ(result.GetContentLength(), length);
auto& stream = result.GetBody();
std::string actual;
actual.resize(length + 1);
stream.read(&actual[0], length + 1);
ASSERT_EQ(stream.gcount(), length); // EOF was reached before length + 1
actual.resize(length);
ASSERT_EQ(actual.size(), expected.size());
ASSERT_TRUE(actual == expected); // Avoid ASSERT_EQ on large data
}
void AssertObjectContents(Aws::S3::S3Client* client, const std::string& bucket,
const std::string& key, const std::string& expected) {
Aws::S3::Model::GetObjectRequest req;
req.SetBucket(ToAwsString(bucket));
req.SetKey(ToAwsString(key));
ARROW_AWS_ASSIGN_OR_FAIL(auto result, client->GetObject(req));
AssertGetObject(result, expected);
}
////////////////////////////////////////////////////////////////////////////
// S3Options tests
class S3OptionsTest : public AwsTestMixin {};
TEST_F(S3OptionsTest, FromUri) {
std::string path;
S3Options options;
ASSERT_OK_AND_ASSIGN(options, S3Options::FromUri("s3://", &path));
ASSERT_EQ(options.region, "");
ASSERT_EQ(options.scheme, "https");
ASSERT_EQ(options.endpoint_override, "");
ASSERT_EQ(path, "");
ASSERT_OK_AND_ASSIGN(options, S3Options::FromUri("s3:", &path));
ASSERT_EQ(path, "");
ASSERT_OK_AND_ASSIGN(options, S3Options::FromUri("s3://access:secret@mybucket", &path));
ASSERT_EQ(path, "mybucket");
const auto creds = options.credentials_provider->GetAWSCredentials();
ASSERT_EQ(creds.GetAWSAccessKeyId(), "access");
ASSERT_EQ(creds.GetAWSSecretKey(), "secret");
ASSERT_OK_AND_ASSIGN(options, S3Options::FromUri("s3://mybucket/", &path));
ASSERT_NE(options.region, ""); // Some region was chosen
ASSERT_EQ(options.scheme, "https");
ASSERT_EQ(options.endpoint_override, "");
ASSERT_EQ(path, "mybucket");
ASSERT_OK_AND_ASSIGN(options, S3Options::FromUri("s3://mybucket/foo/bar/", &path));
ASSERT_NE(options.region, "");
ASSERT_EQ(options.scheme, "https");
ASSERT_EQ(options.endpoint_override, "");
ASSERT_EQ(path, "mybucket/foo/bar");
// Region resolution with a well-known bucket
ASSERT_OK_AND_ASSIGN(
options, S3Options::FromUri("s3://aws-earth-mo-atmospheric-ukv-prd/", &path));
ASSERT_EQ(options.region, "eu-west-2");
// Explicit region override
ASSERT_OK_AND_ASSIGN(
options,
S3Options::FromUri(
"s3://mybucket/foo/bar/?region=utopia&endpoint_override=localhost&scheme=http",
&path));
ASSERT_EQ(options.region, "utopia");
ASSERT_EQ(options.scheme, "http");
ASSERT_EQ(options.endpoint_override, "localhost");
ASSERT_EQ(path, "mybucket/foo/bar");
// Missing bucket name
ASSERT_RAISES(Invalid, S3Options::FromUri("s3:///foo/bar/", &path));
// Invalid option
ASSERT_RAISES(Invalid, S3Options::FromUri("s3://mybucket/?xxx=zzz", &path));
}
TEST_F(S3OptionsTest, FromAccessKey) {
S3Options options;
// session token is optional and should default to empty string
options = S3Options::FromAccessKey("access", "secret");
ASSERT_EQ(options.GetAccessKey(), "access");
ASSERT_EQ(options.GetSecretKey(), "secret");
ASSERT_EQ(options.GetSessionToken(), "");
options = S3Options::FromAccessKey("access", "secret", "token");
ASSERT_EQ(options.GetAccessKey(), "access");
ASSERT_EQ(options.GetSecretKey(), "secret");
ASSERT_EQ(options.GetSessionToken(), "token");
}
TEST_F(S3OptionsTest, FromAssumeRole) {
S3Options options;
// arn should be only required argument
options = S3Options::FromAssumeRole("my_role_arn");
options = S3Options::FromAssumeRole("my_role_arn", "session");
options = S3Options::FromAssumeRole("my_role_arn", "session", "id");
options = S3Options::FromAssumeRole("my_role_arn", "session", "id", 42);
// test w/ custom STSClient (will not use DefaultAWSCredentialsProviderChain)
Aws::Auth::AWSCredentials test_creds = Aws::Auth::AWSCredentials("access", "secret");
std::shared_ptr<Aws::STS::STSClient> sts_client =
std::make_shared<Aws::STS::STSClient>(Aws::STS::STSClient(test_creds));
options = S3Options::FromAssumeRole("my_role_arn", "session", "id", 42, sts_client);
}
////////////////////////////////////////////////////////////////////////////
// Region resolution test
class S3RegionResolutionTest : public AwsTestMixin {};
TEST_F(S3RegionResolutionTest, PublicBucket) {
ASSERT_OK_AND_EQ("us-east-2", ResolveS3BucketRegion("voltrondata-labs-datasets"));
// Taken from a registry of open S3-hosted datasets
// at https://github.com/awslabs/open-data-registry
ASSERT_OK_AND_EQ("eu-west-2",
ResolveS3BucketRegion("aws-earth-mo-atmospheric-ukv-prd"));
// Same again, cached
ASSERT_OK_AND_EQ("eu-west-2",
ResolveS3BucketRegion("aws-earth-mo-atmospheric-ukv-prd"));
}
TEST_F(S3RegionResolutionTest, RestrictedBucket) {
ASSERT_OK_AND_EQ("us-west-2", ResolveS3BucketRegion("ursa-labs-r-test"));
// Same again, cached
ASSERT_OK_AND_EQ("us-west-2", ResolveS3BucketRegion("ursa-labs-r-test"));
}
TEST_F(S3RegionResolutionTest, NonExistentBucket) {
auto maybe_region = ResolveS3BucketRegion("ursa-labs-non-existent-bucket");
ASSERT_RAISES(IOError, maybe_region);
ASSERT_THAT(maybe_region.status().message(),
::testing::HasSubstr("Bucket 'ursa-labs-non-existent-bucket' not found"));
}
TEST_F(S3RegionResolutionTest, InvalidBucketName) {
ASSERT_RAISES(Invalid, ResolveS3BucketRegion("s3:bucket"));
ASSERT_RAISES(Invalid, ResolveS3BucketRegion("foo/bar"));
}
////////////////////////////////////////////////////////////////////////////
// S3FileSystem region test
class S3FileSystemRegionTest : public AwsTestMixin {};
TEST_F(S3FileSystemRegionTest, Default) {
ASSERT_OK_AND_ASSIGN(auto fs, FileSystemFromUri("s3://"));
auto s3fs = checked_pointer_cast<S3FileSystem>(fs);
ASSERT_EQ(s3fs->region(), "us-east-1");
}
// Skipped on Windows, as the AWS SDK ignores runtime environment changes:
// https://github.com/aws/aws-sdk-cpp/issues/1476
#ifndef _WIN32
TEST_F(S3FileSystemRegionTest, EnvironmentVariable) {
// Region override with environment variable (AWS SDK >= 1.8)
EnvVarGuard region_guard("AWS_DEFAULT_REGION", "eu-north-1");
ASSERT_OK_AND_ASSIGN(auto fs, FileSystemFromUri("s3://"));
auto s3fs = checked_pointer_cast<S3FileSystem>(fs);
if (Aws::Version::GetVersionMajor() > 1 || Aws::Version::GetVersionMinor() >= 8) {
ASSERT_EQ(s3fs->region(), "eu-north-1");
} else {
ASSERT_EQ(s3fs->region(), "us-east-1");
}
}
#endif
////////////////////////////////////////////////////////////////////////////
// Basic test for the Minio test server.
class TestMinioServer : public S3TestMixin {
public:
void SetUp() override { S3TestMixin::SetUp(); }
protected:
};
TEST_F(TestMinioServer, Connect) {
// Just a dummy connection test. Check that we can list buckets,
// and that there are none (the server is launched in an empty temp dir).
ARROW_AWS_ASSIGN_OR_FAIL(auto bucket_list, client_->ListBuckets());
ASSERT_EQ(bucket_list.GetBuckets().size(), 0);
}
////////////////////////////////////////////////////////////////////////////
// Concrete S3 tests
class TestS3FS : public S3TestMixin {
public:
void SetUp() override {
S3TestMixin::SetUp();
// Most tests will create buckets
options_.allow_bucket_creation = true;
options_.allow_bucket_deletion = true;
MakeFileSystem();
// Set up test bucket
{
Aws::S3::Model::CreateBucketRequest req;
req.SetBucket(ToAwsString("bucket"));
ASSERT_OK(OutcomeToStatus("CreateBucket", client_->CreateBucket(req)));
req.SetBucket(ToAwsString("empty-bucket"));
ASSERT_OK(OutcomeToStatus("CreateBucket", client_->CreateBucket(req)));
}
{
Aws::S3::Model::PutObjectRequest req;
req.SetBucket(ToAwsString("bucket"));
req.SetKey(ToAwsString("emptydir/"));
req.SetBody(std::make_shared<std::stringstream>(""));
ASSERT_OK(OutcomeToStatus("PutObject", client_->PutObject(req)));
// NOTE: no need to create intermediate "directories" somedir/ and
// somedir/subdir/
req.SetKey(ToAwsString("somedir/subdir/subfile"));
req.SetBody(std::make_shared<std::stringstream>("sub data"));
ASSERT_OK(OutcomeToStatus("PutObject", client_->PutObject(req)));
req.SetKey(ToAwsString("somefile"));
req.SetBody(std::make_shared<std::stringstream>("some data"));
req.SetContentType("x-arrow/test");
ASSERT_OK(OutcomeToStatus("PutObject", client_->PutObject(req)));
req.SetKey(ToAwsString("otherdir/1/2/3/otherfile"));
req.SetBody(std::make_shared<std::stringstream>("other data"));
ASSERT_OK(OutcomeToStatus("PutObject", client_->PutObject(req)));
}
}
void MakeFileSystem() {
options_.ConfigureAccessKey(minio_->access_key(), minio_->secret_key());
options_.scheme = "http";
options_.endpoint_override = minio_->connect_string();
if (!options_.retry_strategy) {
options_.retry_strategy = std::make_shared<ShortRetryStrategy>();
}
ASSERT_OK_AND_ASSIGN(fs_, S3FileSystem::Make(options_));
}
template <typename Matcher>
void AssertMetadataRoundtrip(const std::string& path,
const std::shared_ptr<const KeyValueMetadata>& metadata,
Matcher&& matcher) {
ASSERT_OK_AND_ASSIGN(auto output, fs_->OpenOutputStream(path, metadata));
ASSERT_OK(output->Close());
ASSERT_OK_AND_ASSIGN(auto input, fs_->OpenInputStream(path));
ASSERT_OK_AND_ASSIGN(auto got_metadata, input->ReadMetadata());
ASSERT_NE(got_metadata, nullptr);
ASSERT_THAT(got_metadata->sorted_pairs(), matcher);
}
void AssertInfoAllBucketsRecursive(const std::vector<FileInfo>& infos) {
AssertFileInfo(infos[0], "bucket", FileType::Directory);
AssertFileInfo(infos[1], "bucket/emptydir", FileType::Directory);
AssertFileInfo(infos[2], "bucket/otherdir", FileType::Directory);
AssertFileInfo(infos[3], "bucket/otherdir/1", FileType::Directory);
AssertFileInfo(infos[4], "bucket/otherdir/1/2", FileType::Directory);
AssertFileInfo(infos[5], "bucket/otherdir/1/2/3", FileType::Directory);
AssertFileInfo(infos[6], "bucket/otherdir/1/2/3/otherfile", FileType::File, 10);
AssertFileInfo(infos[7], "bucket/somedir", FileType::Directory);
AssertFileInfo(infos[8], "bucket/somedir/subdir", FileType::Directory);
AssertFileInfo(infos[9], "bucket/somedir/subdir/subfile", FileType::File, 8);
AssertFileInfo(infos[10], "bucket/somefile", FileType::File, 9);
AssertFileInfo(infos[11], "empty-bucket", FileType::Directory);
}
void TestOpenOutputStream() {
std::shared_ptr<io::OutputStream> stream;
// Nonexistent
ASSERT_RAISES(IOError, fs_->OpenOutputStream("nonexistent-bucket/somefile"));
// URI
ASSERT_RAISES(Invalid, fs_->OpenOutputStream("s3:bucket/newfile1"));
// Create new empty file
ASSERT_OK_AND_ASSIGN(stream, fs_->OpenOutputStream("bucket/newfile1"));
ASSERT_OK(stream->Close());
AssertObjectContents(client_.get(), "bucket", "newfile1", "");
// Create new file with 1 small write
ASSERT_OK_AND_ASSIGN(stream, fs_->OpenOutputStream("bucket/newfile2"));
ASSERT_OK(stream->Write("some data"));
ASSERT_OK(stream->Close());
AssertObjectContents(client_.get(), "bucket", "newfile2", "some data");
// Create new file with 3 small writes
ASSERT_OK_AND_ASSIGN(stream, fs_->OpenOutputStream("bucket/newfile3"));
ASSERT_OK(stream->Write("some "));
ASSERT_OK(stream->Write(""));
ASSERT_OK(stream->Write("new data"));
ASSERT_OK(stream->Close());
AssertObjectContents(client_.get(), "bucket", "newfile3", "some new data");
// Create new file with some large writes
std::string s1, s2, s3, s4, s5, expected;
s1 = random_string(6000000, /*seed =*/42); // More than the 5 MB minimum part upload
s2 = "xxx";
s3 = random_string(6000000, 43);
s4 = "zzz";
s5 = random_string(600000, 44);
expected = s1 + s2 + s3 + s4 + s5;
ASSERT_OK_AND_ASSIGN(stream, fs_->OpenOutputStream("bucket/newfile4"));
for (auto input : {s1, s2, s3, s4, s5}) {
ASSERT_OK(stream->Write(input));
// Clobber source contents. This shouldn't reflect in the data written.
input.front() = 'x';
input.back() = 'x';
}
ASSERT_OK(stream->Close());
AssertObjectContents(client_.get(), "bucket", "newfile4", expected);
// Overwrite
ASSERT_OK_AND_ASSIGN(stream, fs_->OpenOutputStream("bucket/newfile1"));
ASSERT_OK(stream->Write("overwritten data"));
ASSERT_OK(stream->Close());
AssertObjectContents(client_.get(), "bucket", "newfile1", "overwritten data");
// Overwrite and make empty
ASSERT_OK_AND_ASSIGN(stream, fs_->OpenOutputStream("bucket/newfile1"));
ASSERT_OK(stream->Close());
AssertObjectContents(client_.get(), "bucket", "newfile1", "");
// Open file and then lose filesystem reference
ASSERT_EQ(fs_.use_count(), 1); // needed for test to work
std::weak_ptr<S3FileSystem> weak_fs(fs_);
ASSERT_OK_AND_ASSIGN(stream, fs_->OpenOutputStream("bucket/newfile99"));
fs_.reset();
ASSERT_OK(stream->Write("some other data"));
ASSERT_OK(stream->Close());
ASSERT_TRUE(weak_fs.expired());
AssertObjectContents(client_.get(), "bucket", "newfile99", "some other data");
}
void TestOpenOutputStreamAbort() {
std::shared_ptr<io::OutputStream> stream;
ASSERT_OK_AND_ASSIGN(stream, fs_->OpenOutputStream("bucket/somefile"));
ASSERT_OK(stream->Write("new data"));
// Abort() cancels the multipart upload.
ASSERT_OK(stream->Abort());
ASSERT_EQ(stream->closed(), true);
AssertObjectContents(client_.get(), "bucket", "somefile", "some data");
}
void TestOpenOutputStreamDestructor() {
std::shared_ptr<io::OutputStream> stream;
ASSERT_OK_AND_ASSIGN(stream, fs_->OpenOutputStream("bucket/somefile"));
ASSERT_OK(stream->Write("new data"));
// Destructor implicitly closes stream and completes the multipart upload.
stream.reset();
AssertObjectContents(client_.get(), "bucket", "somefile", "new data");
}
protected:
S3Options options_;
std::shared_ptr<S3FileSystem> fs_;
};
TEST_F(TestS3FS, GetFileInfoRoot) { AssertFileInfo(fs_.get(), "", FileType::Directory); }
TEST_F(TestS3FS, GetFileInfoBucket) {
AssertFileInfo(fs_.get(), "bucket", FileType::Directory);
AssertFileInfo(fs_.get(), "empty-bucket", FileType::Directory);
AssertFileInfo(fs_.get(), "nonexistent-bucket", FileType::NotFound);
// Trailing slashes
AssertFileInfo(fs_.get(), "bucket/", FileType::Directory);
AssertFileInfo(fs_.get(), "empty-bucket/", FileType::Directory);
AssertFileInfo(fs_.get(), "nonexistent-bucket/", FileType::NotFound);
// URIs
ASSERT_RAISES(Invalid, fs_->GetFileInfo("s3:bucket"));
ASSERT_RAISES(Invalid, fs_->GetFileInfo("s3:empty-bucket"));
ASSERT_RAISES(Invalid, fs_->GetFileInfo("s3:nonexistent-bucket"));
}
TEST_F(TestS3FS, GetFileInfoObject) {
// "Directories"
AssertFileInfo(fs_.get(), "bucket/emptydir", FileType::Directory, kNoSize);
AssertFileInfo(fs_.get(), "bucket/otherdir", FileType::Directory, kNoSize);
AssertFileInfo(fs_.get(), "bucket/otherdir/1", FileType::Directory, kNoSize);
AssertFileInfo(fs_.get(), "bucket/otherdir/1/2", FileType::Directory, kNoSize);
AssertFileInfo(fs_.get(), "bucket/otherdir/1/2/3", FileType::Directory, kNoSize);
AssertFileInfo(fs_.get(), "bucket/somedir", FileType::Directory, kNoSize);
AssertFileInfo(fs_.get(), "bucket/somedir/subdir", FileType::Directory, kNoSize);
// "Files"
AssertFileInfo(fs_.get(), "bucket/otherdir/1/2/3/otherfile", FileType::File, 10);
AssertFileInfo(fs_.get(), "bucket/somefile", FileType::File, 9);
AssertFileInfo(fs_.get(), "bucket/somedir/subdir/subfile", FileType::File, 8);
// Nonexistent
AssertFileInfo(fs_.get(), "bucket/emptyd", FileType::NotFound);
AssertFileInfo(fs_.get(), "bucket/somed", FileType::NotFound);
AssertFileInfo(fs_.get(), "non-existent-bucket/somed", FileType::NotFound);
// Trailing slashes
AssertFileInfo(fs_.get(), "bucket/emptydir/", FileType::Directory, kNoSize);
AssertFileInfo(fs_.get(), "bucket/somefile/", FileType::File, 9);
AssertFileInfo(fs_.get(), "bucket/emptyd/", FileType::NotFound);
AssertFileInfo(fs_.get(), "non-existent-bucket/somed/", FileType::NotFound);
// URIs
ASSERT_RAISES(Invalid, fs_->GetFileInfo("s3:bucket/emptydir"));
ASSERT_RAISES(Invalid, fs_->GetFileInfo("s3:bucket/otherdir"));
ASSERT_RAISES(Invalid, fs_->GetFileInfo("s3:bucket/somefile"));
}
TEST_F(TestS3FS, GetFileInfoSelector) {
FileSelector select;
std::vector<FileInfo> infos;
// Root dir
select.base_dir = "";
ASSERT_OK_AND_ASSIGN(infos, fs_->GetFileInfo(select));
ASSERT_EQ(infos.size(), 2);
SortInfos(&infos);
AssertFileInfo(infos[0], "bucket", FileType::Directory);
AssertFileInfo(infos[1], "empty-bucket", FileType::Directory);
// Empty bucket
select.base_dir = "empty-bucket";
ASSERT_OK_AND_ASSIGN(infos, fs_->GetFileInfo(select));
ASSERT_EQ(infos.size(), 0);
// Nonexistent bucket
select.base_dir = "nonexistent-bucket";
ASSERT_RAISES(IOError, fs_->GetFileInfo(select));
select.allow_not_found = true;
ASSERT_OK_AND_ASSIGN(infos, fs_->GetFileInfo(select));
ASSERT_EQ(infos.size(), 0);
select.allow_not_found = false;
// Non-empty bucket
select.base_dir = "bucket";
ASSERT_OK_AND_ASSIGN(infos, fs_->GetFileInfo(select));
SortInfos(&infos);
ASSERT_EQ(infos.size(), 4);
AssertFileInfo(infos[0], "bucket/emptydir", FileType::Directory);
AssertFileInfo(infos[1], "bucket/otherdir", FileType::Directory);
AssertFileInfo(infos[2], "bucket/somedir", FileType::Directory);
AssertFileInfo(infos[3], "bucket/somefile", FileType::File, 9);
// Empty "directory"
select.base_dir = "bucket/emptydir";
ASSERT_OK_AND_ASSIGN(infos, fs_->GetFileInfo(select));
ASSERT_EQ(infos.size(), 0);
// Non-empty "directories"
select.base_dir = "bucket/somedir";
ASSERT_OK_AND_ASSIGN(infos, fs_->GetFileInfo(select));
ASSERT_EQ(infos.size(), 1);
AssertFileInfo(infos[0], "bucket/somedir/subdir", FileType::Directory);
select.base_dir = "bucket/somedir/subdir";
ASSERT_OK_AND_ASSIGN(infos, fs_->GetFileInfo(select));
ASSERT_EQ(infos.size(), 1);
AssertFileInfo(infos[0], "bucket/somedir/subdir/subfile", FileType::File, 8);
// Nonexistent
select.base_dir = "bucket/nonexistent";
ASSERT_RAISES(IOError, fs_->GetFileInfo(select));
select.allow_not_found = true;
ASSERT_OK_AND_ASSIGN(infos, fs_->GetFileInfo(select));
ASSERT_EQ(infos.size(), 0);
select.allow_not_found = false;
// Trailing slashes
select.base_dir = "empty-bucket/";
ASSERT_OK_AND_ASSIGN(infos, fs_->GetFileInfo(select));
ASSERT_EQ(infos.size(), 0);
select.base_dir = "nonexistent-bucket/";
ASSERT_RAISES(IOError, fs_->GetFileInfo(select));
select.base_dir = "bucket/";
ASSERT_OK_AND_ASSIGN(infos, fs_->GetFileInfo(select));
SortInfos(&infos);
ASSERT_EQ(infos.size(), 4);
// URIs
select.base_dir = "s3:bucket";
ASSERT_RAISES(Invalid, fs_->GetFileInfo(select));
select.base_dir = "s3:bucket/somedir";
ASSERT_RAISES(Invalid, fs_->GetFileInfo(select));
}
TEST_F(TestS3FS, GetFileInfoSelectorRecursive) {
FileSelector select;
std::vector<FileInfo> infos;
select.recursive = true;
// Root dir
select.base_dir = "";
ASSERT_OK_AND_ASSIGN(infos, fs_->GetFileInfo(select));
ASSERT_EQ(infos.size(), 12);
SortInfos(&infos);
AssertInfoAllBucketsRecursive(infos);
// Empty bucket
select.base_dir = "empty-bucket";
ASSERT_OK_AND_ASSIGN(infos, fs_->GetFileInfo(select));
ASSERT_EQ(infos.size(), 0);
// Non-empty bucket
select.base_dir = "bucket";
ASSERT_OK_AND_ASSIGN(infos, fs_->GetFileInfo(select));
SortInfos(&infos);
ASSERT_EQ(infos.size(), 10);
AssertFileInfo(infos[0], "bucket/emptydir", FileType::Directory);
AssertFileInfo(infos[1], "bucket/otherdir", FileType::Directory);
AssertFileInfo(infos[2], "bucket/otherdir/1", FileType::Directory);
AssertFileInfo(infos[3], "bucket/otherdir/1/2", FileType::Directory);
AssertFileInfo(infos[4], "bucket/otherdir/1/2/3", FileType::Directory);
AssertFileInfo(infos[5], "bucket/otherdir/1/2/3/otherfile", FileType::File, 10);
AssertFileInfo(infos[6], "bucket/somedir", FileType::Directory);
AssertFileInfo(infos[7], "bucket/somedir/subdir", FileType::Directory);
AssertFileInfo(infos[8], "bucket/somedir/subdir/subfile", FileType::File, 8);
AssertFileInfo(infos[9], "bucket/somefile", FileType::File, 9);
// Empty "directory"
select.base_dir = "bucket/emptydir";
ASSERT_OK_AND_ASSIGN(infos, fs_->GetFileInfo(select));
ASSERT_EQ(infos.size(), 0);
// Non-empty "directories"
select.base_dir = "bucket/somedir";
ASSERT_OK_AND_ASSIGN(infos, fs_->GetFileInfo(select));
SortInfos(&infos);
ASSERT_EQ(infos.size(), 2);
AssertFileInfo(infos[0], "bucket/somedir/subdir", FileType::Directory);
AssertFileInfo(infos[1], "bucket/somedir/subdir/subfile", FileType::File, 8);
select.base_dir = "bucket/otherdir";
ASSERT_OK_AND_ASSIGN(infos, fs_->GetFileInfo(select));
SortInfos(&infos);
ASSERT_EQ(infos.size(), 4);
AssertFileInfo(infos[0], "bucket/otherdir/1", FileType::Directory);
AssertFileInfo(infos[1], "bucket/otherdir/1/2", FileType::Directory);
AssertFileInfo(infos[2], "bucket/otherdir/1/2/3", FileType::Directory);
AssertFileInfo(infos[3], "bucket/otherdir/1/2/3/otherfile", FileType::File, 10);
}
TEST_F(TestS3FS, GetFileInfoGenerator) {
FileSelector select;
FileInfoVector infos;
// Root dir
select.base_dir = "";
CollectFileInfoGenerator(fs_->GetFileInfoGenerator(select), &infos);
ASSERT_EQ(infos.size(), 2);
SortInfos(&infos);
AssertFileInfo(infos[0], "bucket", FileType::Directory);
AssertFileInfo(infos[1], "empty-bucket", FileType::Directory);
// Root dir, recursive
select.recursive = true;
CollectFileInfoGenerator(fs_->GetFileInfoGenerator(select), &infos);
ASSERT_EQ(infos.size(), 12);
SortInfos(&infos);
AssertInfoAllBucketsRecursive(infos);
// Non-root dir case is tested by generic tests
}
TEST_F(TestS3FS, CreateDir) {
FileInfo st;
// Existing bucket
ASSERT_OK(fs_->CreateDir("bucket"));
AssertFileInfo(fs_.get(), "bucket", FileType::Directory);
// New bucket
AssertFileInfo(fs_.get(), "new-bucket", FileType::NotFound);
ASSERT_OK(fs_->CreateDir("new-bucket"));
AssertFileInfo(fs_.get(), "new-bucket", FileType::Directory);
// Existing "directory"
AssertFileInfo(fs_.get(), "bucket/somedir", FileType::Directory);
ASSERT_OK(fs_->CreateDir("bucket/somedir"));
AssertFileInfo(fs_.get(), "bucket/somedir", FileType::Directory);
AssertFileInfo(fs_.get(), "bucket/emptydir", FileType::Directory);
ASSERT_OK(fs_->CreateDir("bucket/emptydir"));
AssertFileInfo(fs_.get(), "bucket/emptydir", FileType::Directory);
// New "directory"
AssertFileInfo(fs_.get(), "bucket/newdir", FileType::NotFound);
ASSERT_OK(fs_->CreateDir("bucket/newdir"));
AssertFileInfo(fs_.get(), "bucket/newdir", FileType::Directory);
// New "directory", recursive
ASSERT_OK(fs_->CreateDir("bucket/newdir/newsub/newsubsub", /*recursive=*/true));
AssertFileInfo(fs_.get(), "bucket/newdir/newsub", FileType::Directory);
AssertFileInfo(fs_.get(), "bucket/newdir/newsub/newsubsub", FileType::Directory);
// Existing "file", should fail
ASSERT_RAISES(IOError, fs_->CreateDir("bucket/somefile"));
// URI
ASSERT_RAISES(Invalid, fs_->CreateDir("s3:bucket/newdir2"));
}
TEST_F(TestS3FS, DeleteFile) {
// Bucket
ASSERT_RAISES(IOError, fs_->DeleteFile("bucket"));
ASSERT_RAISES(IOError, fs_->DeleteFile("empty-bucket"));
ASSERT_RAISES(IOError, fs_->DeleteFile("nonexistent-bucket"));
// "File"
ASSERT_OK(fs_->DeleteFile("bucket/somefile"));
AssertFileInfo(fs_.get(), "bucket/somefile", FileType::NotFound);
ASSERT_RAISES(IOError, fs_->DeleteFile("bucket/somefile"));
ASSERT_RAISES(IOError, fs_->DeleteFile("bucket/nonexistent"));
// "Directory"
ASSERT_RAISES(IOError, fs_->DeleteFile("bucket/somedir"));
AssertFileInfo(fs_.get(), "bucket/somedir", FileType::Directory);
// URI
ASSERT_RAISES(Invalid, fs_->DeleteFile("s3:bucket/somefile"));
}
TEST_F(TestS3FS, DeleteDir) {
FileSelector select;
select.base_dir = "bucket";
std::vector<FileInfo> infos;
// Empty "directory"
ASSERT_OK(fs_->DeleteDir("bucket/emptydir"));
ASSERT_OK_AND_ASSIGN(infos, fs_->GetFileInfo(select));
ASSERT_EQ(infos.size(), 3);
SortInfos(&infos);
AssertFileInfo(infos[0], "bucket/otherdir", FileType::Directory);
AssertFileInfo(infos[1], "bucket/somedir", FileType::Directory);
AssertFileInfo(infos[2], "bucket/somefile", FileType::File);
// Non-empty "directory"
ASSERT_OK(fs_->DeleteDir("bucket/somedir"));
ASSERT_OK_AND_ASSIGN(infos, fs_->GetFileInfo(select));
ASSERT_EQ(infos.size(), 2);
AssertFileInfo(infos[0], "bucket/otherdir", FileType::Directory);
AssertFileInfo(infos[1], "bucket/somefile", FileType::File);
// Leaving parent "directory" empty
ASSERT_OK(fs_->CreateDir("bucket/newdir/newsub/newsubsub"));
ASSERT_OK(fs_->DeleteDir("bucket/newdir/newsub"));
ASSERT_OK_AND_ASSIGN(infos, fs_->GetFileInfo(select));
ASSERT_EQ(infos.size(), 3);
SortInfos(&infos);
AssertFileInfo(infos[0], "bucket/newdir", FileType::Directory); // still exists
AssertFileInfo(infos[1], "bucket/otherdir", FileType::Directory);
AssertFileInfo(infos[2], "bucket/somefile", FileType::File);
// Bucket
ASSERT_OK(fs_->DeleteDir("bucket"));
AssertFileInfo(fs_.get(), "bucket", FileType::NotFound);
// URI
ASSERT_RAISES(Invalid, fs_->DeleteDir("s3:empty-bucket"));
}
TEST_F(TestS3FS, DeleteDirContents) {
FileSelector select;
select.base_dir = "bucket";
std::vector<FileInfo> infos;
ASSERT_OK(fs_->DeleteDirContents("bucket/doesnotexist", /*missing_dir_ok=*/true));
ASSERT_OK(fs_->DeleteDirContents("bucket/emptydir"));
ASSERT_OK(fs_->DeleteDirContents("bucket/somedir"));
ASSERT_RAISES(IOError, fs_->DeleteDirContents("bucket/somefile"));
ASSERT_RAISES(IOError, fs_->DeleteDirContents("bucket/doesnotexist"));
ASSERT_OK_AND_ASSIGN(infos, fs_->GetFileInfo(select));
ASSERT_EQ(infos.size(), 4);
SortInfos(&infos);
AssertFileInfo(infos[0], "bucket/emptydir", FileType::Directory);
AssertFileInfo(infos[1], "bucket/otherdir", FileType::Directory);
AssertFileInfo(infos[2], "bucket/somedir", FileType::Directory);
AssertFileInfo(infos[3], "bucket/somefile", FileType::File);
}
TEST_F(TestS3FS, DeleteDirContentsAsync) {
FileSelector select;
select.base_dir = "bucket";
std::vector<FileInfo> infos;
ASSERT_FINISHES_OK(fs_->DeleteDirContentsAsync("bucket/emptydir"));
ASSERT_FINISHES_OK(fs_->DeleteDirContentsAsync("bucket/somedir"));
ASSERT_FINISHES_AND_RAISES(IOError, fs_->DeleteDirContentsAsync("bucket/somefile"));
ASSERT_OK_AND_ASSIGN(infos, fs_->GetFileInfo(select));
ASSERT_EQ(infos.size(), 4);
SortInfos(&infos);
AssertFileInfo(infos[0], "bucket/emptydir", FileType::Directory);
AssertFileInfo(infos[1], "bucket/otherdir", FileType::Directory);
AssertFileInfo(infos[2], "bucket/somedir", FileType::Directory);
AssertFileInfo(infos[3], "bucket/somefile", FileType::File);
}
TEST_F(TestS3FS, CopyFile) {
// "File"
ASSERT_OK(fs_->CopyFile("bucket/somefile", "bucket/newfile"));
AssertFileInfo(fs_.get(), "bucket/newfile", FileType::File, 9);
AssertObjectContents(client_.get(), "bucket", "newfile", "some data");
AssertFileInfo(fs_.get(), "bucket/somefile", FileType::File, 9); // still exists
// Overwrite
ASSERT_OK(fs_->CopyFile("bucket/somedir/subdir/subfile", "bucket/newfile"));
AssertFileInfo(fs_.get(), "bucket/newfile", FileType::File, 8);
AssertObjectContents(client_.get(), "bucket", "newfile", "sub data");
// ARROW-13048: URL-encoded paths
ASSERT_OK(fs_->CopyFile("bucket/somefile", "bucket/a=2/newfile"));
ASSERT_OK(fs_->CopyFile("bucket/a=2/newfile", "bucket/a=3/newfile"));
// Nonexistent
ASSERT_RAISES(IOError, fs_->CopyFile("bucket/nonexistent", "bucket/newfile2"));
ASSERT_RAISES(IOError, fs_->CopyFile("nonexistent-bucket/somefile", "bucket/newfile2"));
ASSERT_RAISES(IOError, fs_->CopyFile("bucket/somefile", "nonexistent-bucket/newfile2"));
AssertFileInfo(fs_.get(), "bucket/newfile2", FileType::NotFound);
}
TEST_F(TestS3FS, Move) {
// "File"
ASSERT_OK(fs_->Move("bucket/somefile", "bucket/newfile"));
AssertFileInfo(fs_.get(), "bucket/newfile", FileType::File, 9);
AssertObjectContents(client_.get(), "bucket", "newfile", "some data");
// Source was deleted
AssertFileInfo(fs_.get(), "bucket/somefile", FileType::NotFound);
// Overwrite
ASSERT_OK(fs_->Move("bucket/somedir/subdir/subfile", "bucket/newfile"));
AssertFileInfo(fs_.get(), "bucket/newfile", FileType::File, 8);
AssertObjectContents(client_.get(), "bucket", "newfile", "sub data");
// Source was deleted
AssertFileInfo(fs_.get(), "bucket/somedir/subdir/subfile", FileType::NotFound);
// ARROW-13048: URL-encoded paths
ASSERT_OK(fs_->Move("bucket/newfile", "bucket/a=2/newfile"));
ASSERT_OK(fs_->Move("bucket/a=2/newfile", "bucket/a=3/newfile"));
// Nonexistent
ASSERT_RAISES(IOError, fs_->Move("bucket/non-existent", "bucket/newfile2"));
ASSERT_RAISES(IOError, fs_->Move("nonexistent-bucket/somefile", "bucket/newfile2"));
ASSERT_RAISES(IOError, fs_->Move("bucket/somefile", "nonexistent-bucket/newfile2"));
AssertFileInfo(fs_.get(), "bucket/newfile2", FileType::NotFound);
}
TEST_F(TestS3FS, OpenInputStream) {
std::shared_ptr<io::InputStream> stream;
std::shared_ptr<Buffer> buf;
// Nonexistent
ASSERT_RAISES(IOError, fs_->OpenInputStream("nonexistent-bucket/somefile"));
ASSERT_RAISES(IOError, fs_->OpenInputStream("bucket/zzzt"));
// URI
ASSERT_RAISES(Invalid, fs_->OpenInputStream("s3:bucket/somefile"));
// "Files"
ASSERT_OK_AND_ASSIGN(stream, fs_->OpenInputStream("bucket/somefile"));
ASSERT_OK_AND_ASSIGN(buf, stream->Read(2));
AssertBufferEqual(*buf, "so");
ASSERT_OK_AND_ASSIGN(buf, stream->Read(5));
AssertBufferEqual(*buf, "me da");
ASSERT_OK_AND_ASSIGN(buf, stream->Read(5));
AssertBufferEqual(*buf, "ta");
ASSERT_OK_AND_ASSIGN(buf, stream->Read(5));
AssertBufferEqual(*buf, "");
ASSERT_OK_AND_ASSIGN(stream, fs_->OpenInputStream("bucket/somedir/subdir/subfile"));
ASSERT_OK_AND_ASSIGN(buf, stream->Read(100));
AssertBufferEqual(*buf, "sub data");
ASSERT_OK_AND_ASSIGN(buf, stream->Read(100));
AssertBufferEqual(*buf, "");
ASSERT_OK(stream->Close());
// "Directories"
ASSERT_RAISES(IOError, fs_->OpenInputStream("bucket/emptydir"));
ASSERT_RAISES(IOError, fs_->OpenInputStream("bucket/somedir"));
ASSERT_RAISES(IOError, fs_->OpenInputStream("bucket"));
// Open file and then lose filesystem reference
ASSERT_EQ(fs_.use_count(), 1); // needed for test to work
std::weak_ptr<S3FileSystem> weak_fs(fs_);
ASSERT_OK_AND_ASSIGN(stream, fs_->OpenInputStream("bucket/somefile"));