|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using Google.Cloud.StorageBatchOperations.V1; |
| 16 | +using System; |
| 17 | +using System.IO; |
| 18 | +using System.Text; |
| 19 | +using Xunit; |
| 20 | + |
| 21 | +[Collection(nameof(StorageFixture))] |
| 22 | +public class CreateBatchJobTest |
| 23 | +{ |
| 24 | + private readonly StorageFixture _fixture; |
| 25 | + private readonly BucketList.Types.Bucket _bucket = new(); |
| 26 | + private readonly BucketList _bucketList = new(); |
| 27 | + private readonly PrefixList _prefixListObject = new(); |
| 28 | + private string _kmsKey; |
| 29 | + private string _keyRingId; |
| 30 | + private string _cryptoKeyId; |
| 31 | + private CryptoKeyName _cryptoKeyName; |
| 32 | + |
| 33 | + public CreateBatchJobTest(StorageFixture fixture) |
| 34 | + { |
| 35 | + _fixture = fixture; |
| 36 | + var bucketName = _fixture.GenerateBucketName(); |
| 37 | + _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: false, registerForDeletion: true); |
| 38 | + |
| 39 | + var manifestBucketName = _fixture.GenerateBucketName(); |
| 40 | + _fixture.CreateBucket(manifestBucketName, multiVersion: false, softDelete: false, registerForDeletion: true); |
| 41 | + |
| 42 | + var objectName = _fixture.GenerateGuid(); |
| 43 | + var manifestObjectName = _fixture.GenerateGuid(); |
| 44 | + var objectContent = _fixture.GenerateGuid(); |
| 45 | + var manifestObjectContent = $"bucket,name,generation{Environment.NewLine}{bucketName},{objectName}"; |
| 46 | + |
| 47 | + byte[] byteObjectContent = Encoding.UTF8.GetBytes(objectContent); |
| 48 | + MemoryStream streamObjectContent = new MemoryStream(byteObjectContent); |
| 49 | + // Uploading an object to the bucket |
| 50 | + _fixture.Client.UploadObject(bucketName, objectName, "application/text", streamObjectContent); |
| 51 | + |
| 52 | + byte[] byteManifestObjectContent = Encoding.UTF8.GetBytes(manifestObjectContent); |
| 53 | + // Uploading a manifest object to the manifest bucket |
| 54 | + MemoryStream streamManifestObjectContent = new MemoryStream(byteManifestObjectContent); |
| 55 | + _fixture.Client.UploadObject(manifestBucketName, $"{manifestObjectName}.csv", "text/csv", streamManifestObjectContent); |
| 56 | + _bucket = new BucketList.Types.Bucket |
| 57 | + { |
| 58 | + Bucket_ = bucketName, |
| 59 | + // The prefix list is used to specify the objects to be transformed. To match all objects, use an empty list. |
| 60 | + PrefixList = _prefixListObject, |
| 61 | + // Manifest location contains csv file having list of objects to be transformed" |
| 62 | + Manifest = new Manifest { ManifestLocation = $"gs://{manifestBucketName}/{manifestObjectName}.csv" } |
| 63 | + }; |
| 64 | + // Adding the bucket to the bucket list. |
| 65 | + _bucketList.Buckets.Insert(0, _bucket); |
| 66 | + } |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public void TestCreateBatchJob() |
| 70 | + { |
| 71 | + CreateBatchJobSample createJob = new CreateBatchJobSample(); |
| 72 | + var jobId = _fixture.GenerateGuid(); |
| 73 | + var jobTransformationCase = "DeleteObject"; |
| 74 | + var holdState = "EventBasedHoldSet"; |
| 75 | + var jobTransformationObject = new object(); |
| 76 | + string jobType; |
| 77 | + |
| 78 | + // If the job transformation case is PutObjectHold, we can set the hold state to EventBasedHoldSet or EventBasedHoldUnSet or TemporaryHoldSet or TemporaryHoldUnSet. |
| 79 | + if (jobTransformationCase == "PutObjectHold") |
| 80 | + { |
| 81 | + jobType = $"{jobTransformationCase}{holdState}"; |
| 82 | + } |
| 83 | + // If the job transformation case is other than PutObjectHold, we dont set the hold state. |
| 84 | + else |
| 85 | + { |
| 86 | + jobType = jobTransformationCase; |
| 87 | + } |
| 88 | + // If the job transformation case is RewriteObject, we can set the KmsKey and KmsKeyAsCryptoKeyName. |
| 89 | + if (jobTransformationCase == "RewriteObject") |
| 90 | + { |
| 91 | + _keyRingId = GetEnvironmentVariable("STORAGE_KMS_KEYRING_ID", "This is the Key Ring ID"); |
| 92 | + _cryptoKeyId = GetEnvironmentVariable("STORAGE_KMS_CRYPTOKEY_ID", "This is the Crypto Key ID."); |
| 93 | + _kmsKey = $"projects/{_fixture.ProjectId}/locations/{_fixture.LocationId}/keyRings/{_keyRingId}/cryptoKeys/{_cryptoKeyId}"; |
| 94 | + _cryptoKeyName = CryptoKeyName.FromProjectLocationKeyRingCryptoKey(_fixture.ProjectId, _fixture.LocationId, _keyRingId, _cryptoKeyId); |
| 95 | + RewriteObject rewriteObject = new RewriteObject { KmsKey = _kmsKey, KmsKeyAsCryptoKeyName = _cryptoKeyName }; |
| 96 | + jobTransformationObject = rewriteObject; |
| 97 | + |
| 98 | + } |
| 99 | + // If the job transformation case is PutMetadata, we can set the CacheControl, ContentDisposition, ContentEncoding, ContentLanguage, ContentType and CustomTime. |
| 100 | + else if (jobTransformationCase == "PutMetadata") |
| 101 | + { |
| 102 | + PutMetadata putMetadata = new PutMetadata |
| 103 | + { |
| 104 | + CacheControl = "no-cache", |
| 105 | + ContentDisposition = "inline", |
| 106 | + ContentEncoding = "gzip", |
| 107 | + ContentLanguage = "en-US", |
| 108 | + ContentType = "text/plain", |
| 109 | + CustomTime = DateTime.UtcNow.ToString("o") |
| 110 | + }; |
| 111 | + jobTransformationObject = putMetadata; |
| 112 | + } |
| 113 | + // Create a batch job with the specified transformation case and bucket list |
| 114 | + var createdBatchJob = createJob.CreateBatchJob(_fixture.LocationName, _bucketList, jobId, jobType, jobTransformationObject); |
| 115 | + Assert.Equal(createdBatchJob.BucketList, _bucketList); |
| 116 | + Assert.Equal(createdBatchJob.TransformationCase.ToString(), jobTransformationCase); |
| 117 | + Assert.Equal(createdBatchJob.SourceCase.ToString(), _bucketList.GetType().Name); |
| 118 | + Assert.NotNull(createdBatchJob.Name); |
| 119 | + Assert.NotNull(createdBatchJob.JobName); |
| 120 | + Assert.NotNull(createdBatchJob.CreateTime); |
| 121 | + Assert.NotNull(createdBatchJob.CompleteTime); |
| 122 | + _fixture.DeleteBatchJob(createdBatchJob.Name); |
| 123 | + } |
| 124 | + |
| 125 | + private static string GetEnvironmentVariable(string envVarName, string message = "") |
| 126 | + { |
| 127 | + string varValue = Environment.GetEnvironmentVariable(envVarName); |
| 128 | + if (string.IsNullOrEmpty(varValue)) |
| 129 | + { |
| 130 | + throw new InvalidOperationException( |
| 131 | + $"Please set the {envVarName} environment variable. {message}"); |
| 132 | + } |
| 133 | + return varValue; |
| 134 | + } |
| 135 | +} |
0 commit comments