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 57 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
57 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
f1b05b3
Comments added in tests and sample description modified
mahendra-google Mar 27, 2025
b05a6bc
Revert "Comments added in tests and sample description modified"
mahendra-google Mar 27, 2025
526ddad
Comments are added and sample description modified
mahendra-google Mar 27, 2025
c6950d8
Quickstart test and sample changes
mahendra-google Mar 28, 2025
d043b9e
minor changes
mahendra-google Mar 28, 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,37 @@
// 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 Xunit;

namespace StorageTransfer.Samples.Tests;

[Collection(nameof(StorageFixture))]
public class CheckLatestTransferOperationTest
{

private readonly StorageFixture _fixture;
private string _jobName;
public CheckLatestTransferOperationTest(StorageFixture fixture)
{
_fixture = fixture;
}

[Fact]
public void CheckLatestTransferOperation()
{
CheckLatestTransferOperationSample checkLatestTransferOperationSample = new CheckLatestTransferOperationSample();
var transferJob = checkLatestTransferOperationSample.CheckLatestTransferOperation(_fixture.ProjectId, _fixture.JobName);
Assert.Contains("transferJobs/", transferJob.Name);
_jobName = transferJob.Name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* 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.StorageTransfer.V1;
using System;
using Xunit;

namespace StorageTransfer.Samples.Tests
{
[Collection(nameof(StorageFixture))]
public class CreateEventDrivenGcsTransferTest : IDisposable
{
private readonly StorageFixture _fixture;
private string _transferJobName;
public CreateEventDrivenGcsTransferTest(StorageFixture fixture)
{
_fixture = fixture;
}

[Fact]
public void CreateEventDrivenGcsTransfer()
{
CreateEventDrivenGcsTransferSample createEventDrivenGcsTransferSample = new CreateEventDrivenGcsTransferSample();
var transferJob = createEventDrivenGcsTransferSample.CreateEventDrivenGcsTransfer(_fixture.ProjectId, _fixture.BucketNameSource, _fixture.BucketNameSink, _fixture.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
}
});
}
catch (Exception)
{
// Do nothing, we delete on a best effort basis.
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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 System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace StorageTransfer.Samples.Tests;
[Collection(nameof(StorageFixture))]
public class DownloadToPosixTest : IDisposable
{
private readonly StorageFixture _fixture;
private string _transferJobName;
public DownloadToPosixTest(StorageFixture fixture)
{
_fixture = fixture;
}

[Fact]
public void DownloadToPosix()
{
DownloadToPosixSample downloadToPosixSample = new DownloadToPosixSample();
Directory.CreateDirectory(_fixture.TempDirectory);
var storage = StorageClient.Create();
byte[] byteArray = Encoding.UTF8.GetBytes("flower.jpeg");
MemoryStream stream = new MemoryStream(byteArray);
string fileName = $"{_fixture.GcsSourcePath}{DateTime.Now.ToString("yyyyMMddHHmmss")}.txt";
storage.UploadObject(_fixture.BucketNameSource, fileName, "application/octet-stream", stream);
var transferJob = downloadToPosixSample.DownloadToPosix(_fixture.ProjectId, _fixture.SinkAgentPoolName, _fixture.BucketNameSource, _fixture.GcsSourcePath, _fixture.TempDirectory);
Assert.Contains("transferJobs/", transferJob.Name);
Assert.True(Directory.Exists(_fixture.TempDirectory));
_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
}
});
Directory.Delete(_fixture.TempDirectory, true);
}
catch (Exception)
{
// Do nothing, we delete on a best effort basis.
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2021 Google Inc.
/**
* 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.
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 @@ -25,7 +25,6 @@ public class QuickstartTest : IDisposable
{
private readonly StorageFixture _fixture;
private string _transferJobName;

public QuickstartTest(StorageFixture fixture)
{
_fixture = fixture;
Expand Down
114 changes: 108 additions & 6 deletions storagetransfer/api/StorageTransfer.Samples.Tests/StorageFixture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2021 Google Inc.
/**
* 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.
Expand All @@ -14,12 +14,16 @@
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using Google.Apis.Storage.v1.Data;
using Google.Cloud.PubSub.V1;
using Google.Cloud.Storage.V1;
using Google.Cloud.StorageTransfer.V1;
using Grpc.Core;
using System;
using System.Collections.Generic;
using System.IO;
using Xunit;

namespace StorageTransfer.Samples.Tests
{
[CollectionDefinition(nameof(StorageFixture))]
Expand All @@ -28,19 +32,84 @@ public class StorageFixture : IDisposable, ICollectionFixture<StorageFixture>
public string ProjectId { get; }
public string BucketNameSource { get; } = Guid.NewGuid().ToString();
public string BucketNameSink { get; } = Guid.NewGuid().ToString();
public string JobName { get; }
public string SourceAgentPoolName { get; }
public string SinkAgentPoolName { get; }
public string GcsSourcePath { get; }
public string RootDirectory { get; } = System.IO.Path.GetTempPath();
public string DestinationDirectory { get; } = System.IO.Path.GetTempPath();
public string TempDirectory { get; } = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
public string TempDestinationDirectory { get; } = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
public StorageClient Storage { get; } = StorageClient.Create();
public string ManifestObjectName { get; } = "manifest.csv";
public string TopicId { get; } = "DotNetTopic" + Guid.NewGuid().ToString();
public string SubscriptionId { get; } = "DotNetSubscription" + Guid.NewGuid().ToString();
public string PubSubId { get; }
public StorageTransferServiceClient Sts { get; } = StorageTransferServiceClient.Create();

public SubscriberServiceApiClient SubscriberClient { get; } = SubscriberServiceApiClient.Create();

public PublisherServiceApiClient PublisherClient { get; } = PublisherServiceApiClient.Create();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How many of these are being used by more than one test? In the fixture, we only should add resources that are used by more than one test. Otherwise, the test itself should create and clean up the resources.

Copy link
Contributor Author

@mahendra-google mahendra-google Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JobName , SourceAgentPoolName , SinkAgentPoolName , RootDirectory are being used by more than one test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resources for event driven transfer are moved from storage fixture to test itself

Copy link
Contributor Author

@mahendra-google mahendra-google Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amanda-tarafa Do I need to move directory paths (GcsSourcePath, DestinationDirectory, TempDirectory, TempDestinationDirectory) and ManifestObjectName property to respective tests from storage fixture ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these are being used for a single test, and that inmediate expectation is that that remains the case, then yes, it's clearer to have them on their respective tests only. In the fixture you can have a method that helps with creating random IDs, file names, temporary paths etc. and you use those on the tests when initializing these values.

Copy link
Contributor Author

@mahendra-google mahendra-google Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amanda-tarafa just wanted to make sure once if there is any linter available which I by mistakenly missed out in documentation provided (apart from dot net format command in IDE) for .NET style and structure changes and more specific to blank spaces which I am manually doing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use the Format command in Visual Studio you should be fine. The linter has been deactivated for some time because of reasons. We have #1080 .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amanda-tarafa I have created GenerateBucketName method in storage fixture similar to .NET client library and called the same in constructor of each individual tests from constructor injected fixture instance and also I made CreateBucketAndGrantStsPermissions internal from private and called same in constructor of each individual tests as it was being used by all tests and to avoid repeatative call of the same method in the fixture and cleaned up resources in individual test if created by test itself else cleaned up in storage fixture

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks, that sounds good.

public StorageFixture()
{
ProjectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");
SourceAgentPoolName = "projects/" + ProjectId + "/agentPools/transfer_service_default";
SinkAgentPoolName = "projects/" + ProjectId + "/agentPools/transfer_service_default";
PubSubId = "projects/" + ProjectId + "/subscriptions/" + SubscriptionId + "";
GcsSourcePath = "foo/bar/";
if (string.IsNullOrWhiteSpace(ProjectId))
{
throw new Exception("You need to set the Environment variable 'GOOGLE_PROJECT_ID' with your Google Cloud Project's project id.");
}

CreateBucketAndGrantStsPermissions(BucketNameSink);
CreateBucketAndGrantStsPermissions(BucketNameSource);
// Initialize request argument(s)
TransferJob transferJob = new TransferJob
{
ProjectId = ProjectId,
TransferSpec = new TransferSpec
{
GcsDataSink = new GcsData { BucketName = BucketNameSource },
GcsDataSource = new GcsData { BucketName = BucketNameSink }
},
Status = TransferJob.Types.Status.Enabled
};
CreateTransferJobRequest request = new CreateTransferJobRequest
{
TransferJob = transferJob
};
// Make the request
TransferJob response = Sts.CreateTransferJob(new CreateTransferJobRequest { TransferJob = transferJob });
JobName = response.Name;
string email = Sts.GetGoogleServiceAccount(new GetGoogleServiceAccountRequest()
{
ProjectId = ProjectId
}).AccountEmail;
string memberServiceAccount = "serviceAccount:" + email;
// Create subscription name
SubscriptionName subscriptionName = new SubscriptionName(ProjectId, SubscriptionId);
// Create topic name
TopicName topicName = new TopicName(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
});
}

private void CreateBucketAndGrantStsPermissions(string bucketName)
Expand Down Expand Up @@ -82,11 +151,13 @@ private void CreateBucketAndGrantStsPermissions(string bucketName)
Members = new List<string> { member }
};


policy.Bindings.Add(objectViewerBinding);
policy.Bindings.Add(bucketReaderBinding);
policy.Bindings.Add(bucketWriterBinding);

Storage.SetBucketIamPolicy(bucketName, policy);

}

public void Dispose()
Expand All @@ -97,16 +168,47 @@ public void Dispose()
}
catch (Exception)
{
// Do nothing, we delete on a best effort basis.
// If bucket is not empty, we delete on a best effort basis.
foreach (var storageObject in Storage.ListObjects(BucketNameSink, ""))
{
Storage.DeleteObject(BucketNameSink, storageObject.Name);
}
Storage.DeleteBucket(BucketNameSink);
}
try
{
Storage.DeleteBucket(BucketNameSource);
}
catch (Exception)
{
// Do nothing, we delete on a best effort basis.
// If bucket is not empty, we delete on a best effort basis.
foreach (var storageObject in Storage.ListObjects(BucketNameSource, ""))
{
Storage.DeleteObject(BucketNameSource, storageObject.Name);
}
Storage.DeleteBucket(BucketNameSource);
}

try
{
TopicName topicName = TopicName.FromProjectTopic(ProjectId, TopicId);
PublisherClient.DeleteTopic(topicName);
}
catch (RpcException ex)
{
throw new Exception($"Exception occur while deleting Topic {TopicId} Exception: {ex}");
}

try
{
SubscriptionName subscriptionName = SubscriptionName.FromProjectSubscription(ProjectId, SubscriptionId);
SubscriberClient.DeleteSubscription(subscriptionName);
}
catch (RpcException ex)
{
throw new Exception($"Exception occur while deleting subscription {SubscriptionId} Exception: {ex}");
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageReference Include="Google.Cloud.PubSub.V1" Version="3.18.0" />
</ItemGroup>

</Project>
Loading