From 05e718a167b139f1394d5c419c4eedeb7969b08e Mon Sep 17 00:00:00 2001 From: dheerajd-msys Date: Wed, 10 Aug 2016 17:45:47 +0530 Subject: [PATCH 1/3] Adding support for json format output in flavor list --- lib/chef/knife/ec2_flavor_list.rb | 24 +++++---- spec/unit/ec2_flavor_list_spec.rb | 82 +++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 10 deletions(-) create mode 100644 spec/unit/ec2_flavor_list_spec.rb diff --git a/lib/chef/knife/ec2_flavor_list.rb b/lib/chef/knife/ec2_flavor_list.rb index 65de6b33..9842b894 100644 --- a/lib/chef/knife/ec2_flavor_list.rb +++ b/lib/chef/knife/ec2_flavor_list.rb @@ -15,7 +15,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # - require 'chef/knife/ec2_base' class Chef @@ -38,15 +37,20 @@ def run ui.color('Disk', :bold), ui.color('Cores', :bold) ] - connection.flavors.sort_by(&:id).each do |flavor| - flavor_list << flavor.id.to_s - flavor_list << flavor.name - flavor_list << "#{flavor.bits.to_s}-bit" - flavor_list << "#{flavor.ram.to_s}" - flavor_list << "#{flavor.disk.to_s} GB" - flavor_list << flavor.cores.to_s - end - puts ui.list(flavor_list, :columns_across, 6) + flavors = connection.flavors.sort_by(&:id) + if (config[:format] == 'summary') + flavors.each do |flavor| + flavor_list << flavor.id.to_s + flavor_list << flavor.name + flavor_list << "#{flavor.bits.to_s}-bit" + flavor_list << "#{flavor.ram.to_s}" + flavor_list << "#{flavor.disk.to_s} GB" + flavor_list << flavor.cores.to_s + end + puts ui.list(flavor_list, :columns_across, 6) + else + output(format_for_display(flavors)) + end end end end diff --git a/spec/unit/ec2_flavor_list_spec.rb b/spec/unit/ec2_flavor_list_spec.rb new file mode 100644 index 00000000..e77dc533 --- /dev/null +++ b/spec/unit/ec2_flavor_list_spec.rb @@ -0,0 +1,82 @@ +# License:: Apache License, Version 2.0 +# +# 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. +# + +require File.expand_path('../../spec_helper', __FILE__) +require 'fog/aws' +require 'chef/knife/ec2_flavor_list' + +describe Chef::Knife::Ec2FlavorList do + + describe '#run' do + let(:knife_flavor_list) { Chef::Knife::Ec2FlavorList.new } + let(:ec2_connection) { double(Fog::Compute::AWS) } + before do + allow(knife_flavor_list).to receive(:connection).and_return(ec2_connection) + end + + it 'invokes validate!' do + ec2_flavors = double(:sort_by => []) + + allow(ec2_connection).to receive(:flavors).and_return(ec2_flavors) + allow(knife_flavor_list.ui).to receive(:warn) + expect(knife_flavor_list).to receive(:validate!) + knife_flavor_list.run + end + + before do + @flavor1 = double("flavor1", :name => "Micro Instance", :id => "t1.micro", :bits => "0", :cores => "2", :disk => "0", :ram => "613", :ebs_optimized_available => "false", :instance_store_volumes => "0") + @flavor2 = double("flavor2", :name => "Micro Instance", :id => "t2.micro", :bits => "64", :cores => "1", :disk => "0", :ram => "1024", :ebs_optimized_available => "false", :instance_store_volumes => "0") + @flavor3 = double("flavor3", :name => "Micro Instance", :id => "t2.small", :bits => "64", :cores => "1", :disk => "0", :ram => "2048", :ebs_optimized_available => "false", :instance_store_volumes => "0") + + allow(ec2_connection).to receive(:flavors).and_return([@flavor1, @flavor2, @flavor3]) + end + + + context '--format option' do + context 'when format=summary' do + before do + knife_flavor_list.config[:format] = 'summary' + allow(knife_flavor_list.ui).to receive(:warn) + end + + it 'shows the output in summary format' do + output_column = ["ID", "Name", "Architecture", "RAM", "Disk", "Cores"] + output_column_count = output_column.length + allow(ec2_connection).to receive(:flavors).and_return([]) + allow(knife_flavor_list).to receive(:validate!) + expect(knife_flavor_list.ui).to receive(:list).with(output_column,:columns_across, output_column_count) + knife_flavor_list.run + end + end + + context 'when format=json' do + before do + knife_flavor_list.config[:format] = 'json' + allow(knife_flavor_list.ui).to receive(:warn) + end + + it 'shows the output in json format' do + allow(ec2_connection).to receive(:flavors).and_return([]) + allow(knife_flavor_list).to receive(:validate!) + allow(knife_flavor_list).to receive(:format_for_display) + expect(knife_flavor_list).to receive(:output) + knife_flavor_list.run + end + end + + end + +end +end From f7028f0e699061337a8bb4bb4068c1941a210aa1 Mon Sep 17 00:00:00 2001 From: dheerajd-msys Date: Tue, 16 Aug 2016 12:50:06 +0530 Subject: [PATCH 2/3] Adding sample output for summary format --- spec/unit/ec2_flavor_list_spec.rb | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/spec/unit/ec2_flavor_list_spec.rb b/spec/unit/ec2_flavor_list_spec.rb index e77dc533..4315d024 100644 --- a/spec/unit/ec2_flavor_list_spec.rb +++ b/spec/unit/ec2_flavor_list_spec.rb @@ -24,6 +24,10 @@ let(:ec2_connection) { double(Fog::Compute::AWS) } before do allow(knife_flavor_list).to receive(:connection).and_return(ec2_connection) + @flavor1 = double("flavor1", :name => "High-CPU Medium", :architecture => "32-bit-bit", :id => "c1.medium", :bits => "32-bit", :cores => "5", :disk => "1740.8 GB", :ram => "350 GB", :ebs_optimized_available => "false", :instance_store_volumes => "0") + + allow(ec2_connection).to receive(:flavors).and_return([@flavor1]) + end it 'invokes validate!' do @@ -35,28 +39,18 @@ knife_flavor_list.run end - before do - @flavor1 = double("flavor1", :name => "Micro Instance", :id => "t1.micro", :bits => "0", :cores => "2", :disk => "0", :ram => "613", :ebs_optimized_available => "false", :instance_store_volumes => "0") - @flavor2 = double("flavor2", :name => "Micro Instance", :id => "t2.micro", :bits => "64", :cores => "1", :disk => "0", :ram => "1024", :ebs_optimized_available => "false", :instance_store_volumes => "0") - @flavor3 = double("flavor3", :name => "Micro Instance", :id => "t2.small", :bits => "64", :cores => "1", :disk => "0", :ram => "2048", :ebs_optimized_available => "false", :instance_store_volumes => "0") - - allow(ec2_connection).to receive(:flavors).and_return([@flavor1, @flavor2, @flavor3]) - end - context '--format option' do context 'when format=summary' do before do + @output_s=["ID", "Name", "Architecture", "RAM", "Disk", "Cores", "c1.medium", "High-CPU Medium", "32-bit-bit", "350 GB", "1740.8 GB GB", "5"] knife_flavor_list.config[:format] = 'summary' allow(knife_flavor_list.ui).to receive(:warn) + allow(knife_flavor_list).to receive(:validate!) end it 'shows the output in summary format' do - output_column = ["ID", "Name", "Architecture", "RAM", "Disk", "Cores"] - output_column_count = output_column.length - allow(ec2_connection).to receive(:flavors).and_return([]) - allow(knife_flavor_list).to receive(:validate!) - expect(knife_flavor_list.ui).to receive(:list).with(output_column,:columns_across, output_column_count) + expect(knife_flavor_list.ui).to receive(:list).with(@output_s,:columns_across,6) knife_flavor_list.run end end From 30b27d9caa0ff062bdba5f3d29fdc04e0e4fa1a3 Mon Sep 17 00:00:00 2001 From: dheerajd-msys Date: Tue, 16 Aug 2016 12:50:06 +0530 Subject: [PATCH 3/3] Adding sample output for summary format --- lib/chef/knife/ec2_flavor_list.rb | 1 + spec/unit/ec2_flavor_list_spec.rb | 22 +++++++--------------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/lib/chef/knife/ec2_flavor_list.rb b/lib/chef/knife/ec2_flavor_list.rb index 9842b894..6e7d4f13 100644 --- a/lib/chef/knife/ec2_flavor_list.rb +++ b/lib/chef/knife/ec2_flavor_list.rb @@ -15,6 +15,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # + require 'chef/knife/ec2_base' class Chef diff --git a/spec/unit/ec2_flavor_list_spec.rb b/spec/unit/ec2_flavor_list_spec.rb index e77dc533..23c79519 100644 --- a/spec/unit/ec2_flavor_list_spec.rb +++ b/spec/unit/ec2_flavor_list_spec.rb @@ -24,6 +24,10 @@ let(:ec2_connection) { double(Fog::Compute::AWS) } before do allow(knife_flavor_list).to receive(:connection).and_return(ec2_connection) + @flavor1 = double("flavor1", :name => "High-CPU Medium", :architecture => "32-bit-bit", :id => "c1.medium", :bits => "32-bit", :cores => "5", :disk => "1740.8 GB", :ram => "350 GB", :ebs_optimized_available => "false", :instance_store_volumes => "0") + + allow(ec2_connection).to receive(:flavors).and_return([@flavor1]) + end it 'invokes validate!' do @@ -35,28 +39,18 @@ knife_flavor_list.run end - before do - @flavor1 = double("flavor1", :name => "Micro Instance", :id => "t1.micro", :bits => "0", :cores => "2", :disk => "0", :ram => "613", :ebs_optimized_available => "false", :instance_store_volumes => "0") - @flavor2 = double("flavor2", :name => "Micro Instance", :id => "t2.micro", :bits => "64", :cores => "1", :disk => "0", :ram => "1024", :ebs_optimized_available => "false", :instance_store_volumes => "0") - @flavor3 = double("flavor3", :name => "Micro Instance", :id => "t2.small", :bits => "64", :cores => "1", :disk => "0", :ram => "2048", :ebs_optimized_available => "false", :instance_store_volumes => "0") - - allow(ec2_connection).to receive(:flavors).and_return([@flavor1, @flavor2, @flavor3]) - end - context '--format option' do context 'when format=summary' do before do + @output_s=["ID", "Name", "Architecture", "RAM", "Disk", "Cores", "c1.medium", "High-CPU Medium", "32-bit-bit", "350 GB", "1740.8 GB GB", "5"] knife_flavor_list.config[:format] = 'summary' allow(knife_flavor_list.ui).to receive(:warn) + allow(knife_flavor_list).to receive(:validate!) end it 'shows the output in summary format' do - output_column = ["ID", "Name", "Architecture", "RAM", "Disk", "Cores"] - output_column_count = output_column.length - allow(ec2_connection).to receive(:flavors).and_return([]) - allow(knife_flavor_list).to receive(:validate!) - expect(knife_flavor_list.ui).to receive(:list).with(output_column,:columns_across, output_column_count) + expect(knife_flavor_list.ui).to receive(:list).with(@output_s,:columns_across,6) knife_flavor_list.run end end @@ -75,8 +69,6 @@ knife_flavor_list.run end end - end - end end