Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samples(storage transfer): add samples and test cases for storage transfer services #2857

Open
wants to merge 52 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
ba3c128
feat(Storage Transfer) : Addition of Sample and Test Case for storage…
mahendra-google Oct 9, 2024
c2982ef
Merge branch 'missing_.net_transfer_service_samples' of https://githu…
mahendra-google Oct 9, 2024
36706a4
feat(Storage Transfer) : Addition of Schedule End Date in Sample for …
mahendra-google Oct 9, 2024
afcba29
samples(storage transfer) : Addition of schedule end date in sample f…
mahendra-google Oct 9, 2024
5d43c18
Merge branch 'missing_.net_transfer_service_samples' of https://githu…
mahendra-google Oct 9, 2024
3efcf76
chore(deps): add dependency xunit.abstractions version 2.0.3 to see o…
mahendra-google Oct 10, 2024
35e18a3
code changes in transfer to nearline related to capture message in st…
mahendra-google Oct 10, 2024
0acb7fd
feat(storage transfer) : addition of sample and test case to check la…
mahendra-google Oct 10, 2024
e3c0619
Merge branch 'GoogleCloudPlatform:main' into missing_.net_transfer_se…
mahendra-google Oct 11, 2024
000b432
feat (storage transfer) : addition of samples and test cases for tran…
mahendra-google Oct 14, 2024
910ad26
Merge branch 'GoogleCloudPlatform:main' into main
mahendra-google Oct 17, 2024
398b3d5
Merge branch 'GoogleCloudPlatform:main' into main
mahendra-google Oct 29, 2024
b5d9b10
storage transfer from POSIX file system to GCS bucket using manifest …
mahendra-google Oct 29, 2024
3c9d243
minor changes
mahendra-google Oct 30, 2024
f4b73e0
samples(storage transfer): transfer from POSIX file system to GCS buc…
mahendra-google Oct 30, 2024
1acec24
Merge branch 'GoogleCloudPlatform:main' into main
mahendra-google Oct 30, 2024
375f1e0
addition of sample and test case for transfer from posix
mahendra-google Oct 31, 2024
81beca4
samples(storage transfer): add samples and test cases for transfer fr…
mahendra-google Oct 31, 2024
ce4a089
Merge branch 'GoogleCloudPlatform:main' into main
mahendra-google Nov 4, 2024
139190a
added sample and test case for download to posix storage transfer ser…
mahendra-google Nov 4, 2024
7357db6
samples(storage transfer): add sample and test case for storage trans…
mahendra-google Nov 4, 2024
bb66a63
code changes for creation of temp folder in posix file system
mahendra-google Nov 5, 2024
526b445
couple of test checks added in download to posix
mahendra-google Nov 6, 2024
8a06504
samples(storage transfer): code changes in logic for creation of tem…
mahendra-google Nov 6, 2024
edc89e4
added sample and test case for posix to posix storage transfer
mahendra-google Nov 6, 2024
7d100d4
one test check added in posix to posix storage transfer
mahendra-google Nov 7, 2024
4b88480
add sample and test case for event driven transfer and few storage fi…
mahendra-google Nov 11, 2024
effd0c2
samples(storage transfer): code changes for printing messages to cons…
mahendra-google Nov 12, 2024
f5d8252
Update storagetransfer/api/StorageTransfer.Samples/TransferUsingManif…
mahendra-google Dec 19, 2024
be9c4bb
Update storagetransfer/api/StorageTransfer.Samples.Tests/TransferUsin…
mahendra-google Dec 19, 2024
be74818
code refactoring changes
mahendra-google Dec 19, 2024
a69aaa1
Merge branch 'event_driven_gcs_transfer_service_samples' of https://g…
mahendra-google Dec 19, 2024
4b5d2e5
code changes
mahendra-google Dec 19, 2024
52eb9de
Merge branch 'main' into event_driven_gcs_transfer_service_samples
mahendra-google Dec 20, 2024
f57ea40
Copyright year reverted for old files
mahendra-google Jan 30, 2025
bf89631
Merge branch 'main' into event_driven_gcs_transfer_service_samples
mahendra-google Jan 30, 2025
a2192fc
Blank line removed
mahendra-google Jan 30, 2025
2d068d2
changes related to blank spaces
mahendra-google Jan 30, 2025
2e7aa62
Blank spaces reverted in QuickStartTest
mahendra-google Jan 31, 2025
02a0e14
resources for event driven transfer are moved from storage fixture to…
mahendra-google Feb 3, 2025
5e70900
movement of temporary folder path from storage fixture to respective …
mahendra-google Feb 4, 2025
d942177
gcssourcepath , manifestobjectname is moved from fixture to test
mahendra-google Feb 5, 2025
a59263c
UploadObjectToManifestBucket , UploadObjectToPosixBucket , CreateTran…
mahendra-google Feb 6, 2025
7e2b4dd
style and structure changes as per canonical sample
mahendra-google Feb 10, 2025
04eeea7
Merge branch 'main' into event_driven_gcs_transfer_service_samples
mahendra-google Feb 11, 2025
332f78d
XML documentation is added to describe the sample methods
mahendra-google Feb 11, 2025
522b48a
samples and test linter changes
mahendra-google Feb 19, 2025
8642fbf
Structural and linter changes in tests
mahendra-google Feb 20, 2025
7287261
linter changes in tests
mahendra-google Feb 21, 2025
904c989
reuse of existing fixture's storage client in transfer to nearline test
mahendra-google Feb 21, 2025
1b0d4fa
linter changes as per .editorconfig file at the root of repository
mahendra-google Feb 26, 2025
dda9b52
exception handling removed from CheckLatestTransferOperation
mahendra-google Mar 12, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2024 Google LLC
//
// Licensed 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
//
// https://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.

using Google.Cloud.StorageTransfer.V1;
using System;
using Xunit;

namespace StorageTransfer.Samples.Tests;

[Collection(nameof(StorageFixture))]
public class CheckLatestTransferOperationTest : IDisposable
{
private readonly StorageFixture _fixture;
private string _jobName;
private readonly string _transferJobName;

public CheckLatestTransferOperationTest(StorageFixture fixture)
{
_fixture = fixture;
_fixture.BucketNameSource = _fixture.GenerateBucketName();
_fixture.BucketNameSink = _fixture.GenerateBucketName();
_fixture.CreateBucketAndGrantStsPermissions(_fixture.BucketNameSource);
_fixture.CreateBucketAndGrantStsPermissions(_fixture.BucketNameSink);
_transferJobName = CreateTransferJob();
}

[Fact]
public void CheckLatestTransferOperation()
{
CheckLatestTransferOperationSample checkLatestTransferOperationSample = new CheckLatestTransferOperationSample();
var transferJob = checkLatestTransferOperationSample.CheckLatestTransferOperation(_fixture.ProjectId, _transferJobName);
Assert.Contains("transferJobs/", transferJob.Name);
_jobName = transferJob.Name;
}

private string CreateTransferJob()
{
// Initialize request argument(s)
TransferJob transferJob = new TransferJob
{
ProjectId = _fixture.ProjectId,
TransferSpec = new TransferSpec
{
GcsDataSink = new GcsData { BucketName = _fixture.BucketNameSource },
GcsDataSource = new GcsData { BucketName = _fixture.BucketNameSink }
},
Status = TransferJob.Types.Status.Enabled
};
CreateTransferJobRequest request = new CreateTransferJobRequest
{
TransferJob = transferJob
};
// Make the request
TransferJob response = _fixture.Sts.CreateTransferJob(new CreateTransferJobRequest { TransferJob = transferJob });
return response.Name;
}

public void Dispose()
{
try
{
_fixture.Sts.UpdateTransferJob(new UpdateTransferJobRequest()
{
ProjectId = _fixture.ProjectId,
JobName = _transferJobName,
TransferJob = new TransferJob()
{
Name = _transferJobName,
Status = TransferJob.Types.Status.Deleted
}
});
}
catch (Exception)
{
// Do nothing, we delete on a best effort basis.
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* Copyright 2024 Google Inc.
*
* Licensed 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.
*/

using Google.Cloud.PubSub.V1;
using Google.Cloud.StorageTransfer.V1;
using System;
using Xunit;

namespace StorageTransfer.Samples.Tests;

[Collection(nameof(StorageFixture))]
public class CreateEventDrivenGcsTransferTest : IDisposable
{
private readonly StorageFixture _fixture;
private readonly string _pubSubId;
private string _transferJobName;
private string TopicId { get; } = "DotNetTopic" + Guid.NewGuid().ToString();
private string SubscriptionId { get; } = "DotNetSubscription" + Guid.NewGuid().ToString();
private SubscriberServiceApiClient SubscriberClient { get; } = SubscriberServiceApiClient.Create();
private PublisherServiceApiClient PublisherClient { get; } = PublisherServiceApiClient.Create();

public CreateEventDrivenGcsTransferTest(StorageFixture fixture)
{
_fixture = fixture;
_fixture.BucketNameSource = _fixture.GenerateBucketName();
_fixture.BucketNameSink = _fixture.GenerateBucketName();
_fixture.CreateBucketAndGrantStsPermissions(_fixture.BucketNameSource);
_fixture.CreateBucketAndGrantStsPermissions(_fixture.BucketNameSink);
_pubSubId = $"projects/{_fixture.ProjectId}/subscriptions/{SubscriptionId}";
string email = _fixture.Sts.GetGoogleServiceAccount(new GetGoogleServiceAccountRequest()
{
ProjectId = _fixture.ProjectId
}).AccountEmail;

string memberServiceAccount = "serviceAccount:" + email;
// Create subscription name
SubscriptionName subscriptionName = new SubscriptionName(_fixture.ProjectId, SubscriptionId);
// Create topic name
TopicName topicName = new TopicName(_fixture.ProjectId, TopicId);
// Create topic
PublisherClient.CreateTopic(topicName);
// Create subscription.
SubscriberClient.CreateSubscription(subscriptionName, topicName, pushConfig: null, ackDeadlineSeconds: 500);

var policyIamPolicyTopic = new Google.Cloud.Iam.V1.Policy();

policyIamPolicyTopic.AddRoleMember("roles/pubsub.publisher", memberServiceAccount);

PublisherClient.IAMPolicyClient.SetIamPolicy(new Google.Cloud.Iam.V1.SetIamPolicyRequest
{
ResourceAsResourceName = topicName,
Policy = policyIamPolicyTopic
});

var policyIamPolicySubscriber = new Google.Cloud.Iam.V1.Policy();

policyIamPolicySubscriber.AddRoleMember("roles/pubsub.subscriber", memberServiceAccount);

PublisherClient.IAMPolicyClient.SetIamPolicy(new Google.Cloud.Iam.V1.SetIamPolicyRequest
{
ResourceAsResourceName = subscriptionName,
Policy = policyIamPolicySubscriber
});
}

[Fact]
public void CreateEventDrivenGcsTransfer()
{
CreateEventDrivenGcsTransferSample createEventDrivenGcsTransferSample = new CreateEventDrivenGcsTransferSample();
var transferJob = createEventDrivenGcsTransferSample.CreateEventDrivenGcsTransfer(_fixture.ProjectId, _fixture.BucketNameSource, _fixture.BucketNameSink, _pubSubId);
Assert.Contains("transferJobs/", transferJob.Name);
_transferJobName = transferJob.Name;
}

public void Dispose()
{
try
{
_fixture.Sts.UpdateTransferJob(new UpdateTransferJobRequest()
{
ProjectId = _fixture.ProjectId,
JobName = _transferJobName,
TransferJob = new TransferJob()
{
Name = _transferJobName,
Status = TransferJob.Types.Status.Deleted
}
});

TopicName topicName = TopicName.FromProjectTopic(_fixture.ProjectId, TopicId);
PublisherClient.DeleteTopic(topicName);
SubscriptionName subscriptionName = SubscriptionName.FromProjectSubscription(_fixture.ProjectId, SubscriptionId);
SubscriberClient.DeleteSubscription(subscriptionName);
}
catch (Exception)
{
// Do nothing, we delete on a best effort basis.
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2024 Google LLC
//
// Licensed 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
//
// https://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.

using Google.Cloud.Storage.V1;
using Google.Cloud.StorageTransfer.V1;
using System;
using System.IO;
using Xunit;

namespace StorageTransfer.Samples.Tests;

[Collection(nameof(StorageFixture))]
public class DownloadToPosixTest : IDisposable
{
private readonly StorageFixture _fixture;
private string _transferJobName;
private readonly string _tempDirectory;
private readonly string _gcsSourcePath;
private readonly string _bucketNamePosixSource;

public DownloadToPosixTest(StorageFixture fixture)
{
_fixture = fixture;
_bucketNamePosixSource = _fixture.GenerateBucketName();
_fixture.CreateBucketAndGrantStsPermissions(_bucketNamePosixSource);
_tempDirectory = _fixture.GenerateTempFolderPath();
_gcsSourcePath = $"{Guid.NewGuid()}/{Guid.NewGuid()}/";
UploadObjectToPosixBucket(_bucketNamePosixSource);
}

[Fact]
public void DownloadToPosix()
{
DownloadToPosixSample downloadToPosixSample = new DownloadToPosixSample();
Directory.CreateDirectory(_tempDirectory);
var transferJob = downloadToPosixSample.DownloadToPosix(_fixture.ProjectId, _fixture.SinkAgentPoolName, _bucketNamePosixSource, _gcsSourcePath, _tempDirectory);
Assert.Contains("transferJobs/", transferJob.Name);
Assert.True(Directory.Exists(_tempDirectory));
_transferJobName = transferJob.Name;
}

private void UploadObjectToPosixBucket(string bucketName)
{
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes($@"{Guid.NewGuid()}.jpeg");
MemoryStream stream = new MemoryStream(byteArray);
string fileName = $"{_gcsSourcePath}{DateTime.Now.ToString("yyyyMMddHHmmss")}.txt";
_fixture.Storage.UploadObject(bucketName, fileName, "application/octet-stream", stream);
}

public void Dispose()
{
try
{
_fixture.Sts.UpdateTransferJob(new UpdateTransferJobRequest()
{
ProjectId = _fixture.ProjectId,
JobName = _transferJobName,
TransferJob = new TransferJob()
{
Name = _transferJobName,
Status = TransferJob.Types.Status.Deleted
}
});
Directory.Delete(_tempDirectory, true);
_fixture.Storage.DeleteBucket(_bucketNamePosixSource, new DeleteBucketOptions { DeleteObjects = true });
}
catch (Exception)
{
// Do nothing, we delete on a best effort basis.
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -14,8 +14,8 @@
* limitations under the License.
*/

using System;
using Google.Cloud.StorageTransfer.V1;
using System;
using Xunit;

namespace StorageTransfer.Samples.Tests
Expand All @@ -29,6 +29,10 @@ public class QuickstartTest : IDisposable
public QuickstartTest(StorageFixture fixture)
{
_fixture = fixture;
_fixture.BucketNameSource = _fixture.GenerateBucketName();
_fixture.BucketNameSink = _fixture.GenerateBucketName();
_fixture.CreateBucketAndGrantStsPermissions(_fixture.BucketNameSource);
_fixture.CreateBucketAndGrantStsPermissions(_fixture.BucketNameSink);
}

[Fact]
Expand Down
Loading