-
Notifications
You must be signed in to change notification settings - Fork 368
Add storage-cli Support for AliCloud #4672
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
Draft
kathap
wants to merge
4
commits into
main
Choose a base branch
from
add-alioss-storage-cli-functionality
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
lib/cloud_controller/blobstore/storage_cli/alioss_storage_cli_client.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| module CloudController | ||
| module Blobstore | ||
| class AliStorageCliClient < StorageCliClient | ||
| def cli_path | ||
| ENV['ALI_STORAGE_CLI_PATH'] || '/var/vcap/packages/ali-storage-cli/bin/ali-storage-cli' | ||
| end | ||
|
|
||
| CloudController::Blobstore::StorageCliClient.register('aliyun', AliStorageCliClient) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
215 changes: 215 additions & 0 deletions
215
spec/unit/lib/cloud_controller/blobstore/storage_cli/alioss_storage_cli_client_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| require 'spec_helper' | ||
| require 'tempfile' | ||
| require 'json' | ||
| require_relative '../client_shared' | ||
| require 'cloud_controller/blobstore/storage_cli/azure_storage_cli_client' | ||
| require 'cloud_controller/blobstore/storage_cli/storage_cli_blob' | ||
|
|
||
| module CloudController | ||
| module Blobstore | ||
| RSpec.describe AzureStorageCliClient do | ||
| let!(:tmp_cfg) do | ||
| f = Tempfile.new(['storage_cli_config', '.json']) | ||
| f.write({ provider: 'AzureRM', | ||
| account_name: 'some-account-name', | ||
| account_key: 'some-access-key', | ||
| container_name: directory_key, | ||
| environment: 'AzureCloud' }.to_json) | ||
| f.flush | ||
| f | ||
| end | ||
|
|
||
| before do | ||
| cc_cfg = instance_double(VCAP::CloudController::Config) | ||
| allow(VCAP::CloudController::Config).to receive(:config).and_return(cc_cfg) | ||
|
|
||
| allow(cc_cfg).to receive(:get) do |key, *_| | ||
| case key | ||
| when :storage_cli_config_file_droplets, | ||
| :storage_cli_config_file_buildpacks, | ||
| :storage_cli_config_file_packages, | ||
| :storage_cli_config_file_resource_pool | ||
| tmp_cfg.path | ||
| end | ||
| end | ||
| allow(Steno).to receive(:logger).and_return(double(info: nil, error: nil)) | ||
| end | ||
|
|
||
| after { tmp_cfg.close! } | ||
|
|
||
| subject(:client) { AzureStorageCliClient.new(provider: 'AzureRM', directory_key: directory_key, resource_type: resource_type, root_dir: 'bommel', config_path: 'path') } | ||
| let(:directory_key) { 'my-bucket' } | ||
| let(:resource_type) { 'resource_pool' } | ||
| let(:downloaded_file) do | ||
| Tempfile.open('') do |tmpfile| | ||
| tmpfile.write('downloaded file content') | ||
| tmpfile | ||
| end | ||
| end | ||
|
|
||
| let(:deletable_blob) { StorageCliBlob.new('deletable-blob') } | ||
| let(:dest_path) { File.join(Dir.mktmpdir, SecureRandom.uuid) } | ||
|
|
||
| describe 'conforms to the blobstore client interface' do | ||
| before do | ||
| allow(client).to receive(:run_cli).with('exists', anything, allow_exit_code_three: true).and_return([nil, instance_double(Process::Status, exitstatus: 0)]) | ||
| allow(client).to receive(:run_cli).with('get', anything, anything).and_wrap_original do |_original_method, _cmd, _source, dest_path| | ||
| File.write(dest_path, 'downloaded content') | ||
| [nil, instance_double(Process::Status, exitstatus: 0)] | ||
| end | ||
| allow(client).to receive(:run_cli).with('put', anything, anything).and_return([nil, instance_double(Process::Status, exitstatus: 0)]) | ||
| allow(client).to receive(:run_cli).with('copy', anything, anything).and_return([nil, instance_double(Process::Status, exitstatus: 0)]) | ||
| allow(client).to receive(:run_cli).with('delete', anything).and_return([nil, instance_double(Process::Status, exitstatus: 0)]) | ||
| allow(client).to receive(:run_cli).with('delete-recursive', anything).and_return([nil, instance_double(Process::Status, exitstatus: 0)]) | ||
| allow(client).to receive(:run_cli).with('list', anything).and_return(["aa/bb/blob1\ncc/dd/blob2\n", instance_double(Process::Status, exitstatus: 0)]) | ||
| allow(client).to receive(:run_cli).with('ensure-bucket-exists').and_return([nil, instance_double(Process::Status, exitstatus: 0)]) | ||
| allow(client).to receive(:run_cli).with('properties', anything).and_return(['{"dummy": "json"}', instance_double(Process::Status, exitstatus: 0)]) | ||
| allow(client).to receive(:run_cli).with('sign', anything, 'get', '3600s').and_return(['some-url', instance_double(Process::Status, exitstatus: 0)]) | ||
| end | ||
|
|
||
| it_behaves_like 'a blobstore client' | ||
| end | ||
|
|
||
| describe '#local?' do | ||
| it 'returns false' do | ||
| expect(client.local?).to be false | ||
| end | ||
| end | ||
|
|
||
| describe '#cli_path' do | ||
| it 'returns the default CLI path' do | ||
| expect(client.cli_path).to eq('/var/vcap/packages/azure-storage-cli/bin/azure-storage-cli') | ||
| end | ||
|
|
||
| it 'can be overridden by an environment variable' do | ||
| allow(ENV).to receive(:[]).with('AZURE_STORAGE_CLI_PATH').and_return('/custom/path/to/AzureRM-storage-cli') | ||
| expect(client.cli_path).to eq('/custom/path/to/AzureRM-storage-cli') | ||
| end | ||
| end | ||
|
|
||
| describe '#exists?' do | ||
| context 'when the blob exists' do | ||
| before { allow(client).to receive(:run_cli).with('exists', any_args).and_return([nil, instance_double(Process::Status, exitstatus: 0)]) } | ||
|
|
||
| it('returns true') { expect(client.exists?('some-blob-key')).to be true } | ||
| end | ||
|
|
||
| context 'when the blob does not exist' do | ||
| before { allow(client).to receive(:run_cli).with('exists', any_args).and_return([nil, instance_double(Process::Status, exitstatus: 3)]) } | ||
|
|
||
| it('returns false') { expect(client.exists?('some-blob-key')).to be false } | ||
| end | ||
| end | ||
|
|
||
| describe '#files_for' do | ||
| context 'when CLI returns multiple files' do | ||
| let(:cli_output) { "aa/bb/blob1\ncc/dd/blob2\n" } | ||
|
|
||
| before do | ||
| allow(client).to receive(:run_cli). | ||
| with('list', 'some-prefix'). | ||
| and_return([cli_output, instance_double(Process::Status, success?: true)]) | ||
| end | ||
|
|
||
| it 'returns StorageCliBlob instances for each file' do | ||
| blobs = client.files_for('some-prefix') | ||
| expect(blobs.map(&:key)).to eq(['aa/bb/blob1', 'cc/dd/blob2']) | ||
| expect(blobs).to all(be_a(StorageCliBlob)) | ||
| end | ||
| end | ||
|
|
||
| context 'when CLI returns empty output' do | ||
| before do | ||
| allow(client).to receive(:run_cli). | ||
| with('list', 'some-prefix'). | ||
| and_return(["\n", instance_double(Process::Status, success?: true)]) | ||
| end | ||
|
|
||
| it 'returns an empty array' do | ||
| expect(client.files_for('some-prefix')).to eq([]) | ||
| end | ||
| end | ||
|
|
||
| context 'when CLI output has extra whitespace' do | ||
| let(:cli_output) { "aa/bb/blob1 \n \ncc/dd/blob2\n" } | ||
|
|
||
| before do | ||
| allow(client).to receive(:run_cli). | ||
| with('list', 'some-prefix'). | ||
| and_return([cli_output, instance_double(Process::Status, success?: true)]) | ||
| end | ||
|
|
||
| it 'strips and rejects empty lines' do | ||
| blobs = client.files_for('some-prefix') | ||
| expect(blobs.map(&:key)).to eq(['aa/bb/blob1', 'cc/dd/blob2']) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe '#blob' do | ||
| let(:properties_json) { '{"etag": "test-etag", "last_modified": "2024-10-01T00:00:00Z", "content_length": 1024}' } | ||
|
|
||
| it 'returns a list of StorageCliBlob instances for a given key' do | ||
| allow(client).to receive(:run_cli).with('properties', 'bommel/va/li/valid-blob').and_return([properties_json, instance_double(Process::Status, exitstatus: 0)]) | ||
| allow(client).to receive(:run_cli).with('sign', 'bommel/va/li/valid-blob', 'get', '3600s').and_return(['some-url', instance_double(Process::Status, exitstatus: 0)]) | ||
|
|
||
| blob = client.blob('valid-blob') | ||
| expect(blob).to be_a(StorageCliBlob) | ||
| expect(blob.key).to eq('valid-blob') | ||
| expect(blob.attributes(:etag, :last_modified, :content_length)).to eq({ | ||
| etag: 'test-etag', | ||
| last_modified: '2024-10-01T00:00:00Z', | ||
| content_length: 1024 | ||
| }) | ||
| expect(blob.internal_download_url).to eq('some-url') | ||
| expect(blob.public_download_url).to eq('some-url') | ||
| end | ||
|
|
||
| it 'raises an error if the cli output is empty' do | ||
| allow(client).to receive(:run_cli).with('properties', 'bommel/no/ne/nonexistent-blob').and_return([nil, instance_double(Process::Status, exitstatus: 0)]) | ||
| expect { client.blob('nonexistent-blob') }.to raise_error(BlobstoreError, /Properties command returned empty output/) | ||
| end | ||
|
|
||
| it 'raises an error if the cli output is not valid JSON' do | ||
| allow(client).to receive(:run_cli).with('properties', 'bommel/in/va/invalid-json').and_return(['not a json', instance_double(Process::Status, exitstatus: 0)]) | ||
| expect { client.blob('invalid-json') }.to raise_error(BlobstoreError, /Failed to parse json properties/) | ||
| end | ||
| end | ||
|
|
||
| describe '#run_cli' do | ||
| it 'returns output and status on success' do | ||
| status = instance_double(Process::Status, success?: true, exitstatus: 0) | ||
| allow(Open3).to receive(:capture3).with(anything, '-c', anything, 'list', 'arg1').and_return(['ok', '', status]) | ||
|
|
||
| output, returned_status = client.send(:run_cli, 'list', 'arg1') | ||
| expect(output).to eq('ok') | ||
| expect(returned_status).to eq(status) | ||
| end | ||
|
|
||
| it 'raises an error on failure' do | ||
| status = instance_double(Process::Status, success?: false, exitstatus: 1) | ||
| allow(Open3).to receive(:capture3).with(anything, '-c', anything, 'list', 'arg1').and_return(['', 'error message', status]) | ||
|
|
||
| expect do | ||
| client.send(:run_cli, 'list', 'arg1') | ||
| end.to raise_error(RuntimeError, /storage-cli list failed with exit code 1/) | ||
| end | ||
|
|
||
| it 'allows exit code 3 if specified' do | ||
| status = instance_double(Process::Status, success?: false, exitstatus: 3) | ||
| allow(Open3).to receive(:capture3).with(anything, '-c', anything, 'list', 'arg1').and_return(['', 'error message', status]) | ||
|
|
||
| output, returned_status = client.send(:run_cli, 'list', 'arg1', allow_exit_code_three: true) | ||
| expect(output).to eq('') | ||
| expect(returned_status).to eq(status) | ||
| end | ||
|
|
||
| it 'raises BlobstoreError on Open3 failure' do | ||
| allow(Open3).to receive(:capture3).and_raise(StandardError.new('Open3 error')) | ||
|
|
||
| expect { client.send(:run_cli, 'list', 'arg1') }.to raise_error(BlobstoreError, /Open3 error/) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.