Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Use GET on the container to check if the storage container exists #16

Merged
merged 2 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 14 additions & 1 deletion lib/fluent/plugin/out_azure-storage-append-blob.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,23 @@ def write(chunk)
end
end

def container_exists?(container)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please add a test for this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added tests for this function.

begin
@bs.get_container_properties(container)
rescue Azure::Core::Http::HTTPError => ex
if ex.status_code == 404 # container does not exist
return false
else
raise
end
end
return true
end

private

def ensure_container
unless @bs.list_containers.find { |c| c.name == @azure_container }
unless container_exists? @azure_container
if @auto_create_container
@bs.create_container(@azure_container)
else
Expand Down
51 changes: 51 additions & 0 deletions test/plugin/test_out_azure_storage_append_blob.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'helper'
require 'fluent/plugin/out_azure-storage-append-blob.rb'
require 'azure/core/http/http_response'
require 'azure/core/http/http_error'

include Fluent::Test::Helpers

Expand Down Expand Up @@ -83,4 +85,53 @@ def create_driver(conf = CONFIG)
assert_equal slice, Time.now.utc.strftime('log/%Y/%m/%d')
end
end

# This class is used to create an Azure::Core::Http::HTTPError. HTTPError parses
# a response object when it is created.
class FakeResponse
def initialize(status=404)
@status = status
@body = "body"
@headers = {}
end

attr_reader :status
attr_reader :body
attr_reader :headers
end

# This class is used to test plugin functions which interact with the blob service
class FakeBlobService
def initialize(status)
@response = Azure::Core::Http::HttpResponse.new(FakeResponse.new(status))
end

def get_container_properties(container)
unless @response.status_code == 200
raise Azure::Core::Http::HTTPError.new(@response)
end
end
end

sub_test_case 'test container_exists' do
test 'container 404 returns false' do
d = create_driver
d.instance.instance_variable_set(:@bs, FakeBlobService.new(404))
assert_false d.instance.container_exists? "anything"
end

test 'existing container returns true' do
d = create_driver
d.instance.instance_variable_set(:@bs, FakeBlobService.new(200))
assert_true d.instance.container_exists? "anything"
end

test 'unexpected exception raises' do
d = create_driver
d.instance.instance_variable_set(:@bs, FakeBlobService.new(500))
assert_raise_kind_of Azure::Core::Http::HTTPError do
d.instance.container_exists? "anything"
end
end
end
end