diff --git a/README.md b/README.md index 0acc02c..1d5221b 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,11 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-storage-tra | Aws-request | [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/aws-request.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/aws-request.js,samples/README.md) | | Check-latest-transfer-operation | [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/check-latest-transfer-operation.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/check-latest-transfer-operation.js,samples/README.md) | | Get-transfer-job-with-retries | [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/get-transfer-job-with-retries.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/get-transfer-job-with-retries.js,samples/README.md) | +| Manifest-request | [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/manifest-request.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/manifest-request.js,samples/README.md) | | Nearline-request | [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/nearline-request.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/nearline-request.js,samples/README.md) | +| Posix-download | [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/posix-download.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/posix-download.js,samples/README.md) | +| Posix-request | [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/posix-request.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/posix-request.js,samples/README.md) | +| Posix-to-posix-request | [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/posix-to-posix-request.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/posix-to-posix-request.js,samples/README.md) | | Quickstart | [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/quickstart.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/quickstart.js,samples/README.md) | | Transfer-check | [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/transfer-check.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/transfer-check.js,samples/README.md) | diff --git a/samples/README.md b/samples/README.md index a8d35f2..732d750 100644 --- a/samples/README.md +++ b/samples/README.md @@ -15,7 +15,11 @@ * [Aws-request](#aws-request) * [Check-latest-transfer-operation](#check-latest-transfer-operation) * [Get-transfer-job-with-retries](#get-transfer-job-with-retries) + * [Manifest-request](#manifest-request) * [Nearline-request](#nearline-request) + * [Posix-download](#posix-download) + * [Posix-request](#posix-request) + * [Posix-to-posix-request](#posix-to-posix-request) * [Quickstart](#quickstart) * [Transfer-check](#transfer-check) @@ -85,6 +89,23 @@ __Usage:__ +### Manifest-request + +View the [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/manifest-request.js). + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/manifest-request.js,samples/README.md) + +__Usage:__ + + +`node samples/manifest-request.js` + + +----- + + + + ### Nearline-request View the [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/nearline-request.js). @@ -102,6 +123,57 @@ __Usage:__ +### Posix-download + +View the [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/posix-download.js). + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/posix-download.js,samples/README.md) + +__Usage:__ + + +`node samples/posix-download.js` + + +----- + + + + +### Posix-request + +View the [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/posix-request.js). + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/posix-request.js,samples/README.md) + +__Usage:__ + + +`node samples/posix-request.js` + + +----- + + + + +### Posix-to-posix-request + +View the [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/posix-to-posix-request.js). + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/posix-to-posix-request.js,samples/README.md) + +__Usage:__ + + +`node samples/posix-to-posix-request.js` + + +----- + + + + ### Quickstart View the [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/quickstart.js). diff --git a/samples/manifest-request.js b/samples/manifest-request.js new file mode 100644 index 0000000..3817bfd --- /dev/null +++ b/samples/manifest-request.js @@ -0,0 +1,99 @@ +/** + * Copyright 2022 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 + * + * 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. + */ + +'use strict'; + +async function main( + projectId = 'my-project', + sourceAgentPoolName = '', + rootDirectory = '', + gcsSinkBucket = '', + manifestLocation = '' +) { + // [START storagetransfer_manifest_request] + + // Imports the Google Cloud client library + const { + StorageTransferServiceClient, + } = require('@google-cloud/storage-transfer'); + + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // Your project id + // const projectId = 'my-project' + + // The agent pool associated with the POSIX data source. Defaults to the default agent + // const sourceAgentPoolName = 'projects/my-project/agentPools/transfer_service_default' + + // The root directory path on the source filesystem + // const rootDirectory = '/directory/to/transfer/source', + + // The ID of the GCS bucket to transfer data to + // const gcsSinkBucket = 'my-sink-bucket' + + // Transfer manifest location. Must be a + // const manifestLocation = 'gs://my-bucket/sample_manifest.csv' + + // Creates a client + const client = new StorageTransferServiceClient(); + + /** + * Creates a request to transfer from the local file system to the sink bucket + */ + async function transferDirectory() { + const createRequest = { + transferJob: { + projectId, + transferSpec: { + sourceAgentPoolName, + posixDataSource: { + rootDirectory, + }, + gcsDataSink: {bucketName: gcsSinkBucket}, + transferManifest: { + location: manifestLocation, + }, + }, + status: 'ENABLED', + }, + }; + + // Runs the request and creates the job + const [transferJob] = await client.createTransferJob(createRequest); + + const runRequest = { + jobName: transferJob.name, + projectId: projectId, + }; + + await client.runTransferJob(runRequest); + + console.log( + `Created and ran a transfer job from '${rootDirectory}' to '${gcsSinkBucket}' using manifest \`${manifestLocation}\` with name ${transferJob.name}` + ); + } + + transferDirectory(); + // [END storagetransfer_manifest_request] +} + +main(...process.argv.slice(2)); + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/samples/posix-download.js b/samples/posix-download.js new file mode 100644 index 0000000..818de46 --- /dev/null +++ b/samples/posix-download.js @@ -0,0 +1,99 @@ +/** + * Copyright 2022 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 + * + * 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. + */ + +'use strict'; + +async function main( + projectId = 'my-project', + sinkAgentPoolName = '', + gcsSourceBucket = '', + gcsSourcePath = '', + rootDirectory = '' +) { + // [START storagetransfer_download_to_posix] + + // Imports the Google Cloud client library + const { + StorageTransferServiceClient, + } = require('@google-cloud/storage-transfer'); + + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // Your project id + // const projectId = 'my-project' + + // The agent pool associated with the POSIX data sink. Defaults to the default agent + // const sinkAgentPoolName = 'projects/my-project/agentPools/transfer_service_default' + + // Google Cloud Storage source bucket name + // const gcsSourceBucket = 'my-gcs-source-bucket' + + // An optional path on the Google Cloud Storage bucket to download from + // const gcsSourcePath = 'foo/bar/' + + // The root directory path on the source filesystem + // const rootDirectory = '/directory/to/transfer/source', + + // Creates a client + const client = new StorageTransferServiceClient(); + + /** + * Creates a request to transfer from the local file system to the sink bucket + */ + async function transferDirectory() { + const createRequest = { + transferJob: { + projectId, + transferSpec: { + sinkAgentPoolName, + gcsDataSource: { + bucketName: gcsSourceBucket, + path: gcsSourcePath, + }, + posixDataSink: { + rootDirectory, + }, + }, + status: 'ENABLED', + }, + }; + + // Runs the request and creates the job + const [transferJob] = await client.createTransferJob(createRequest); + + const runRequest = { + jobName: transferJob.name, + projectId: projectId, + }; + + await client.runTransferJob(runRequest); + + console.log( + `Downloading from '${gcsSourceBucket}' (path: \`${gcsSourcePath}\`) to '${rootDirectory}' with name ${transferJob.name}` + ); + } + + transferDirectory(); + // [END storagetransfer_download_to_posix] +} + +main(...process.argv.slice(2)); + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/samples/posix-request.js b/samples/posix-request.js new file mode 100644 index 0000000..8ff8ee6 --- /dev/null +++ b/samples/posix-request.js @@ -0,0 +1,92 @@ +/** + * Copyright 2022 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 + * + * 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. + */ + +'use strict'; + +async function main( + projectId = 'my-project', + sourceAgentPoolName = '', + rootDirectory = '', + gcsSinkBucket = '' +) { + // [START storagetransfer_transfer_from_posix] + + // Imports the Google Cloud client library + const { + StorageTransferServiceClient, + } = require('@google-cloud/storage-transfer'); + + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // Your project id + // const projectId = 'my-project' + + // The agent pool associated with the POSIX data source. Defaults to the default agent + // const sourceAgentPoolName = 'projects/my-project/agentPools/transfer_service_default' + + // The root directory path on the source filesystem + // const rootDirectory = '/directory/to/transfer/source', + + // The ID of the GCS bucket to transfer data to + // const gcsSinkBucket = 'my-sink-bucket' + + // Creates a client + const client = new StorageTransferServiceClient(); + + /** + * Creates a request to transfer from the local file system to the sink bucket + */ + async function transferDirectory() { + const createRequest = { + transferJob: { + projectId, + transferSpec: { + sourceAgentPoolName, + posixDataSource: { + rootDirectory, + }, + gcsDataSink: {bucketName: gcsSinkBucket}, + }, + status: 'ENABLED', + }, + }; + + // Runs the request and creates the job + const [transferJob] = await client.createTransferJob(createRequest); + + const runRequest = { + jobName: transferJob.name, + projectId: projectId, + }; + + await client.runTransferJob(runRequest); + + console.log( + `Created and ran a transfer job from '${rootDirectory}' to '${gcsSinkBucket}' with name ${transferJob.name}` + ); + } + + transferDirectory(); + // [END storagetransfer_transfer_from_posix] +} + +main(...process.argv.slice(2)); + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/samples/posix-to-posix-request.js b/samples/posix-to-posix-request.js new file mode 100644 index 0000000..50f3097 --- /dev/null +++ b/samples/posix-to-posix-request.js @@ -0,0 +1,106 @@ +/** + * Copyright 2022 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 + * + * 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. + */ + +'use strict'; + +async function main( + projectId = 'my-project', + sourceAgentPoolName = '', + sinkAgentPoolName = '', + rootDirectory = '', + destinationDirectory = '', + bucketName = '' +) { + // [START storagetransfer_transfer_posix_to_posix] + + // Imports the Google Cloud client library + const { + StorageTransferServiceClient, + } = require('@google-cloud/storage-transfer'); + + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // Your project id + // const projectId = 'my-project' + + // The agent pool associated with the POSIX data source. Defaults to the default agent + // const sourceAgentPoolName = 'projects/my-project/agentPools/transfer_service_default' + + // The agent pool associated with the POSIX data sink. Defaults to the default agent + // const sinkAgentPoolName = 'projects/my-project/agentPools/transfer_service_default' + + // The root directory path on the source filesystem + // const rootDirectory = '/directory/to/transfer/source', + + // The root directory path on the sink filesystem + // const destinationDirectory = '/directory/to/transfer/sink' + + // The ID of the GCS bucket for intermediate storage + // const bucketName = 'my-intermediate-bucket' + + // Creates a client + const client = new StorageTransferServiceClient(); + + /** + * Creates a request to transfer from the local file system to the sink bucket + */ + async function transferDirectory() { + const createRequest = { + transferJob: { + projectId, + transferSpec: { + sourceAgentPoolName, + sinkAgentPoolName, + posixDataSource: { + rootDirectory, + }, + posixDataSink: { + rootDirectory: destinationDirectory, + }, + gcsIntermediateDataLocation: { + bucketName, + }, + }, + status: 'ENABLED', + }, + }; + + // Runs the request and creates the job + const [transferJob] = await client.createTransferJob(createRequest); + + const runRequest = { + jobName: transferJob.name, + projectId: projectId, + }; + + await client.runTransferJob(runRequest); + + console.log( + `Created and ran a transfer job from '${rootDirectory}' to '${destinationDirectory}' with name ${transferJob.name}` + ); + } + + transferDirectory(); + // [END storagetransfer_transfer_posix_to_posix] +} + +main(...process.argv.slice(2)); + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/samples/test/manifest-request.test.js b/samples/test/manifest-request.test.js new file mode 100644 index 0000000..b0db4a4 --- /dev/null +++ b/samples/test/manifest-request.test.js @@ -0,0 +1,93 @@ +/** + * Copyright 2022 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 + * + * 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. + */ + +'use strict'; + +const fs = require('fs').promises; +const os = require('os'); +const path = require('path'); + +const {assert} = require('chai'); +const {after, before, describe, it} = require('mocha'); + +const {BucketManager, TransferJobManager, runSample} = require('./utils'); + +describe('manifest-request', () => { + const testBucketManager = new BucketManager(); + const testTransferJobManager = new TransferJobManager(); + + let projectId; + let sourceAgentPoolName; + let rootDirectory; + let gcsSinkBucket; + let manifestLocation; + + let tempManifestObject; + let tempFile; + + before(async () => { + projectId = await testTransferJobManager.client.getProjectId(); + + // Use default pool + sourceAgentPoolName = ''; + + rootDirectory = await fs.mkdtemp( + path.join(os.tmpdir(), 'sts-manifest-request-test-src-') + ); + + const bucket = await testBucketManager.generateGCSBucket(); + + gcsSinkBucket = bucket.name; + + tempFile = path.join(rootDirectory, 'text.txt'); + await fs.writeFile(tempFile, 'test data'); + + // Double-quote to escape double-quotes in CSV text + const csvContent = `"${tempFile.replace(/"/g, '""')}"`; + + tempManifestObject = bucket.file('manifest.csv'); + await tempManifestObject.save(csvContent); + + manifestLocation = `gs://${bucket.name}/${tempManifestObject.name}`; + }); + + after(async () => { + await tempManifestObject.delete(); + await testBucketManager.deleteBuckets(); + await testTransferJobManager.cleanUp(); + await fs.unlink(tempFile); + await fs.rmdir(rootDirectory); + }); + + it('should create a transfer job with a manifest from POSIX to GCS', async () => { + const output = await runSample('manifest-request', [ + projectId, + sourceAgentPoolName, + rootDirectory, + gcsSinkBucket, + manifestLocation, + ]); + + // If it ran successfully and a job was created, delete it to clean up + const [jobName] = output.match(/transferJobs.*/); + if (jobName) { + testTransferJobManager.transferJobToCleanUp(jobName); + } + + // Find at least 1 transfer operation from the transfer job in the output + assert.include(output, 'Created and ran a transfer job'); + }); +}); diff --git a/samples/test/posix-download.test.js b/samples/test/posix-download.test.js new file mode 100644 index 0000000..bfe904f --- /dev/null +++ b/samples/test/posix-download.test.js @@ -0,0 +1,89 @@ +/** + * Copyright 2022 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 + * + * 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. + */ + +'use strict'; + +const fs = require('fs').promises; +const os = require('os'); +const path = require('path'); + +const {assert} = require('chai'); +const {after, before, describe, it} = require('mocha'); + +const {BucketManager, TransferJobManager, runSample} = require('./utils'); + +describe('posix-download', () => { + const testBucketManager = new BucketManager(); + const testTransferJobManager = new TransferJobManager(); + + let projectId; + let sinkAgentPoolName; + let gcsSourceBucket; + let gcsSourcePath; + let rootDirectory; + + let tempObject; + + before(async () => { + projectId = await testTransferJobManager.client.getProjectId(); + + // Use default pool + sinkAgentPoolName = ''; + + const bucket = await testBucketManager.generateGCSBucket(); + gcsSourceBucket = bucket.name; + + rootDirectory = await fs.mkdtemp( + path.join(os.tmpdir(), 'sts-posix-download-test-sink-') + ); + + // API requires path to end with '/' + gcsSourcePath = rootDirectory + path.posix.sep; + + tempObject = bucket.file(path.join(rootDirectory, 'text.txt')); + + await tempObject.save('test data'); + }); + + after(async () => { + await tempObject.delete(); + await testBucketManager.deleteBuckets(); + await testTransferJobManager.cleanUp(); + await fs.rmdir(rootDirectory); + }); + + it('should create a transfer job from GCS to POSIX', async () => { + const output = await runSample('posix-download', [ + projectId, + sinkAgentPoolName, + gcsSourceBucket, + gcsSourcePath, + rootDirectory, + ]); + + // If it ran successfully and a job was created, delete it to clean up + const [jobName] = output.match(/transferJobs.*/); + if (jobName) { + testTransferJobManager.transferJobToCleanUp(jobName); + } + + // Find at least 1 transfer operation from the transfer job in the output + assert.include( + output, + `Downloading from '${gcsSourceBucket}' (path: \`${gcsSourcePath}\`) to '${rootDirectory}'` + ); + }); +}); diff --git a/samples/test/posix-request.test.js b/samples/test/posix-request.test.js new file mode 100644 index 0000000..d7e0d11 --- /dev/null +++ b/samples/test/posix-request.test.js @@ -0,0 +1,79 @@ +/** + * Copyright 2022 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 + * + * 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. + */ + +'use strict'; + +const fs = require('fs').promises; +const os = require('os'); +const path = require('path'); + +const {assert} = require('chai'); +const {after, before, describe, it} = require('mocha'); + +const {BucketManager, TransferJobManager, runSample} = require('./utils'); + +describe('posix-request', () => { + const testBucketManager = new BucketManager(); + const testTransferJobManager = new TransferJobManager(); + + let projectId; + let sourceAgentPoolName; + let rootDirectory; + let gcsSinkBucket; + + let tempFile; + + before(async () => { + projectId = await testTransferJobManager.client.getProjectId(); + + // Use default pool + sourceAgentPoolName = ''; + + rootDirectory = await fs.mkdtemp( + path.join(os.tmpdir(), 'sts-posix-request-test-src-') + ); + + gcsSinkBucket = (await testBucketManager.generateGCSBucket()).name; + + tempFile = path.join(rootDirectory, 'text.txt'); + await fs.writeFile(tempFile, 'test data'); + }); + + after(async () => { + await testBucketManager.deleteBuckets(); + await testTransferJobManager.cleanUp(); + await fs.unlink(tempFile); + await fs.rmdir(rootDirectory); + }); + + it('should create a transfer job from POSIX to GCS', async () => { + const output = await runSample('posix-request', [ + projectId, + sourceAgentPoolName, + rootDirectory, + gcsSinkBucket, + ]); + + // If it ran successfully and a job was created, delete it to clean up + const [jobName] = output.match(/transferJobs.*/); + if (jobName) { + testTransferJobManager.transferJobToCleanUp(jobName); + } + + // Find at least 1 transfer operation from the transfer job in the output + assert.include(output, 'Created and ran a transfer job'); + }); +}); diff --git a/samples/test/posix-to-posix-request.test.js b/samples/test/posix-to-posix-request.test.js new file mode 100644 index 0000000..da7fd9a --- /dev/null +++ b/samples/test/posix-to-posix-request.test.js @@ -0,0 +1,89 @@ +/** + * Copyright 2022 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 + * + * 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. + */ + +'use strict'; + +const fs = require('fs').promises; +const os = require('os'); +const path = require('path'); + +const {assert} = require('chai'); +const {after, before, describe, it} = require('mocha'); + +const {BucketManager, TransferJobManager, runSample} = require('./utils'); + +describe('posix-to-posix-request', () => { + const testBucketManager = new BucketManager(); + const testTransferJobManager = new TransferJobManager(); + + let projectId; + let sourceAgentPoolName; + let sinkAgentPoolName; + let rootDirectory; + let destinationDirectory; + let bucketName; + + let tempFile; + + before(async () => { + projectId = await testTransferJobManager.client.getProjectId(); + + // Use default pool + sourceAgentPoolName = ''; + sinkAgentPoolName = ''; + + rootDirectory = await fs.mkdtemp( + path.join(os.tmpdir(), 'sts-posix-to-posix-request-test-src-') + ); + + destinationDirectory = await fs.mkdtemp( + path.join(os.tmpdir(), 'sts-posix-to-posix-request-test-sink-') + ); + + bucketName = (await testBucketManager.generateGCSBucket()).name; + + tempFile = path.join(rootDirectory, 'text.txt'); + await fs.writeFile(tempFile, 'test data'); + }); + + after(async () => { + await testBucketManager.deleteBuckets(); + await testTransferJobManager.cleanUp(); + + await fs.unlink(tempFile); + await fs.rmdir(rootDirectory); + }); + + it('should create a transfer job from POSIX to POSIX', async () => { + const output = await runSample('posix-to-posix-request', [ + projectId, + sourceAgentPoolName, + sinkAgentPoolName, + rootDirectory, + destinationDirectory, + bucketName, + ]); + + // If it ran successfully and a job was created, delete it to clean up + const [jobName] = output.match(/transferJobs.*/); + if (jobName) { + testTransferJobManager.transferJobToCleanUp(jobName); + } + + // Find at least 1 transfer operation from the transfer job in the output + assert.include(output, 'Created and ran a transfer job'); + }); +});