Skip to content

Commit

Permalink
Fix #259: Use Json format to store light stemcell information.
Browse files Browse the repository at this point in the history
  • Loading branch information
AbelHu committed Mar 31, 2017
1 parent d80129a commit 30407da
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 147 deletions.
5 changes: 1 addition & 4 deletions src/bosh_azure_cpi/lib/cloud/azure/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,7 @@ def initialize(uri, metadata)
@name = @metadata['name']
@version = @metadata['version']
@disk_size = @metadata['disk'].nil? ? 3072 : @metadata['disk'].to_i

if @metadata.has_key?('image')
@image = @metadata['image'].kind_of?(Hash) ? @metadata['image'] : eval(@metadata['image'])
end
@image = @metadata['image']
end

def is_light_stemcell?
Expand Down
10 changes: 8 additions & 2 deletions src/bosh_azure_cpi/lib/cloud/azure/light_stemcell_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def create_stemcell(stemcell_properties)
cloud_error("Cannot find the light stemcell (#{stemcell_properties['image']}) in the location `#{@default_location}'") unless platform_image_exists?(@default_location, stemcell_properties)

stemcell_name = "#{LIGHT_STEMCELL_PREFIX}-#{SecureRandom.uuid}"
@blob_manager.create_empty_page_blob(@default_storage_account_name, STEMCELL_CONTAINER, "#{stemcell_name}.vhd", 1, stemcell_properties)
metadata = stemcell_properties.dup
metadata['image'] = JSON.dump(metadata['image'])
@blob_manager.create_empty_page_blob(@default_storage_account_name, STEMCELL_CONTAINER, "#{stemcell_name}.vhd", 1, metadata)
stemcell_name
end

Expand All @@ -47,7 +49,11 @@ def get_stemcell_info(name)
private

def get_metadata(name)
@blob_manager.get_blob_metadata(@default_storage_account_name, STEMCELL_CONTAINER, "#{name}.vhd")
metadata = @blob_manager.get_blob_metadata(@default_storage_account_name, STEMCELL_CONTAINER, "#{name}.vhd")
return nil if metadata.nil?

metadata['image'] = JSON.parse(metadata['image'])
metadata
end

def platform_image_exists?(location, stemcell_properties)
Expand Down
89 changes: 27 additions & 62 deletions src/bosh_azure_cpi/spec/unit/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -617,70 +617,35 @@ def set_logger(logger)
end

context "when metadata contains image" do
context "and image is a hash" do
let(:uri) { "fake-uri" }
let(:metadata) {
{
"name" => "fake-name",
"version" => "fake-version",
"infrastructure" => "azure",
"hypervisor" => "hyperv",
"disk" => "3072",
"disk_format" => "vhd",
"container_format" => "bare",
"os_type" => "linux",
"os_distro" => "ubuntu",
"architecture" => "x86_64",
"image" => "{\"publisher\"=>\"bosh\", \"offer\"=>\"UbuntuServer\", \"sku\"=>\"trusty\", \"version\"=>\"fake-version\"}"
}
}

it "should return correct values" do
stemcell_info = Bosh::AzureCloud::Helpers::StemcellInfo.new(uri, metadata)
expect(stemcell_info.uri).to eq("fake-uri")
expect(stemcell_info.os_type).to eq("linux")
expect(stemcell_info.name).to eq("fake-name")
expect(stemcell_info.version).to eq("fake-version")
expect(stemcell_info.disk_size).to eq(3072)
expect(stemcell_info.is_light_stemcell?).to be(true)
expect(stemcell_info.image_reference['publisher']).to eq('bosh')
expect(stemcell_info.image_reference['offer']).to eq('UbuntuServer')
expect(stemcell_info.image_reference['sku']).to eq('trusty')
expect(stemcell_info.image_reference['version']).to eq('fake-version')
end
end

context "and image is a string" do
let(:uri) { "fake-uri" }
let(:metadata) {
{
"name" => "fake-name",
"version" => "fake-version",
"infrastructure" => "azure",
"hypervisor" => "hyperv",
"disk" => "3072",
"disk_format" => "vhd",
"container_format" => "bare",
"os_type" => "linux",
"os_distro" => "ubuntu",
"architecture" => "x86_64",
"image" => {"publisher"=>"bosh", "offer"=>"UbuntuServer", "sku"=>"trusty", "version"=>"fake-version"}
}
let(:uri) { "fake-uri" }
let(:metadata) {
{
"name" => "fake-name",
"version" => "fake-version",
"infrastructure" => "azure",
"hypervisor" => "hyperv",
"disk" => "3072",
"disk_format" => "vhd",
"container_format" => "bare",
"os_type" => "linux",
"os_distro" => "ubuntu",
"architecture" => "x86_64",
"image" => {"publisher"=>"bosh", "offer"=>"UbuntuServer", "sku"=>"trusty", "version"=>"fake-version"}
}
}

it "should return correct values" do
stemcell_info = Bosh::AzureCloud::Helpers::StemcellInfo.new(uri, metadata)
expect(stemcell_info.uri).to eq("fake-uri")
expect(stemcell_info.os_type).to eq("linux")
expect(stemcell_info.name).to eq("fake-name")
expect(stemcell_info.version).to eq("fake-version")
expect(stemcell_info.disk_size).to eq(3072)
expect(stemcell_info.is_light_stemcell?).to be(true)
expect(stemcell_info.image_reference['publisher']).to eq('bosh')
expect(stemcell_info.image_reference['offer']).to eq('UbuntuServer')
expect(stemcell_info.image_reference['sku']).to eq('trusty')
expect(stemcell_info.image_reference['version']).to eq('fake-version')
end
it "should return correct values" do
stemcell_info = Bosh::AzureCloud::Helpers::StemcellInfo.new(uri, metadata)
expect(stemcell_info.uri).to eq("fake-uri")
expect(stemcell_info.os_type).to eq("linux")
expect(stemcell_info.name).to eq("fake-name")
expect(stemcell_info.version).to eq("fake-version")
expect(stemcell_info.disk_size).to eq(3072)
expect(stemcell_info.is_light_stemcell?).to be(true)
expect(stemcell_info.image_reference['publisher']).to eq('bosh')
expect(stemcell_info.image_reference['offer']).to eq('UbuntuServer')
expect(stemcell_info.image_reference['sku']).to eq('trusty')
expect(stemcell_info.image_reference['version']).to eq('fake-version')
end
end
end
Expand Down
144 changes: 65 additions & 79 deletions src/bosh_azure_cpi/spec/unit/light_stemcell_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
let(:light_stemcell_manager) { Bosh::AzureCloud::LightStemcellManager.new(blob_manager, storage_account_manager, azure_client2) }

let(:stemcell_container) { 'stemcell' }
let(:stemcell_name) { "fake-stemcell-name" }
let(:prefix) { 'bosh-light-stemcell' }
let(:uuid) { 'fac96afe-6953-4e74-b9a8-80ae571dfca7' }
let(:stemcell_name) { "#{prefix}-#{uuid}" }
let(:location) { 'fake-location' }
let(:version) { "fake-version" }
let(:storage_account) {
Expand All @@ -21,6 +23,59 @@
:storage_table_host => "fake-table-endpoint"
}
}
let(:stemcell_properties) {
{
"name" => "fake-name",
"version" => version,
"infrastructure" => "azure",
"hypervisor" => "hyperv",
"disk" => "3072",
"disk_format" => "vhd",
"container_format" => "bare",
"os_type" => "linux",
"os_distro" => "ubuntu",
"architecture" => "x86_64",
"image" => {
"publisher" => "bosh",
"offer" => "UbuntuServer",
"sku" => "trusty",
"version" => version
}
}
}
let(:metadata) {
{
"name" => "fake-name",
"version" => version,
"infrastructure" => "azure",
"hypervisor" => "hyperv",
"disk" => "3072",
"disk_format" => "vhd",
"container_format" => "bare",
"os_type" => "linux",
"os_distro" => "ubuntu",
"architecture" => "x86_64",
"image" => JSON.dump({
"publisher" => "bosh",
"offer" => "UbuntuServer",
"sku" => "trusty",
"version" => version
})
}
}
let(:versions) {
[
{
:id => 'a',
:name => version,
:location => location
}, {
:id => 'c',
:name => 'd',
:location => location
}
]
}

before do
allow(storage_account_manager).to receive(:default_storage_account).
Expand All @@ -29,16 +84,14 @@

describe "#delete_stemcell" do
context "when the stemcell exists" do
let(:metadata) { { "foo" => "bar" } }

before do
allow(blob_manager).to receive(:get_blob_metadata).
with(MOCK_DEFAULT_STORAGE_ACCOUNT_NAME, stemcell_container, "#{stemcell_name}.vhd").
and_return(metadata)
end

it "should delete the stemcell" do
expect(blob_manager).to receive(:delete_blob)
expect(blob_manager).to receive(:delete_blob).
with(MOCK_DEFAULT_STORAGE_ACCOUNT_NAME, stemcell_container, "#{stemcell_name}.vhd")

light_stemcell_manager.delete_stemcell(stemcell_name)
end
Expand All @@ -59,54 +112,19 @@
end

describe "#create_stemcell" do
let(:stemcell_properties) {
{
"name" => "fake-name",
"version" => version,
"infrastructure" => "azure",
"hypervisor" => "hyperv",
"disk" => "3072",
"disk_format" => "vhd",
"container_format" => "bare",
"os_type" => "linux",
"os_distro" => "ubuntu",
"architecture" => "x86_64",
"image" => {
"publisher" => "bosh",
"offer" => "UbuntuServer",
"sku" => "trusty",
"version" => version
}
}
}

context "when the platform image exists" do
let(:versions) {
[
{
:id => 'a',
:name => version,
:location => location
}, {
:id => 'c',
:name => 'd',
:location => location
}
]
}

before do
allow(SecureRandom).to receive(:uuid).and_return(uuid)
allow(azure_client2).to receive(:list_platform_image_versions).
and_return(versions)
allow(blob_manager).to receive(:create_empty_page_blob).
and_return(true)
and_return(versions)
end

it "should create the stemcell" do
expect(azure_client2).to receive(:list_platform_image_versions)
expect(blob_manager).to receive(:create_empty_page_blob)
expect(blob_manager).to receive(:create_empty_page_blob).
with(MOCK_DEFAULT_STORAGE_ACCOUNT_NAME, stemcell_container, "#{stemcell_name}.vhd", 1, metadata).
and_return(true)

expect(light_stemcell_manager.create_stemcell(stemcell_properties)).to start_with('bosh-light-stemcell')
expect(light_stemcell_manager.create_stemcell(stemcell_properties)).to eq(stemcell_name)
end
end

Expand Down Expand Up @@ -158,22 +176,6 @@
end

context "when the blob exists in the default storage account" do
let(:metadata) {
{
"name" => "fake-name",
"version" => version,
"infrastructure" => "azure",
"hypervisor" => "hyperv",
"disk" => "3072",
"disk_format" => "vhd",
"container_format" => "bare",
"os_type" => "linux",
"os_distro" => "ubuntu",
"architecture" => "x86_64",
"image" => "{\"publisher\"=>\"bosh\", \"offer\"=>\"UbuntuServer\", \"sku\"=>\"trusty\", \"version\"=>\"#{version}\"}"
}
}

before do
allow(blob_manager).to receive(:get_blob_metadata).
with(MOCK_DEFAULT_STORAGE_ACCOUNT_NAME, stemcell_container, "#{stemcell_name}.vhd").
Expand All @@ -197,20 +199,6 @@
end

context "and the platform image exists" do
let(:versions) {
[
{
:id => 'a',
:name => version,
:location => location
}, {
:id => 'c',
:name => 'd',
:location => location
}
]
}

before do
allow(azure_client2).to receive(:list_platform_image_versions).
and_return(versions)
Expand Down Expand Up @@ -244,8 +232,6 @@
end

context "when the blob exists in the default storage account" do
let(:metadata) { { "foo" => "bar" } }

before do
allow(blob_manager).to receive(:get_blob_metadata).
with(MOCK_DEFAULT_STORAGE_ACCOUNT_NAME, stemcell_container, "#{stemcell_name}.vhd").
Expand All @@ -255,7 +241,7 @@
it "should return stemcell info" do
stemcell_info = light_stemcell_manager.get_stemcell_info(stemcell_name)
expect(stemcell_info.uri).to eq('')
expect(stemcell_info.metadata).to eq(metadata)
expect(stemcell_info.metadata).to eq(stemcell_properties)
end
end
end
Expand Down

0 comments on commit 30407da

Please sign in to comment.