Skip to content

Commit

Permalink
Issue #51 - Unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiamarchi committed Aug 29, 2014
1 parent 2eeab12 commit d93ef9a
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def self.synopsis
def cmd(name, argv, env)
fail Errors::NoArgRequiredForCommand, cmd: name unless argv.size == 1
flavors = env[:openstack_client].nova.get_all_flavors(env)
display_item_list(flavors)
display_item_list(env, flavors)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ def cmd(name, argv, env)
floating_ip_pools.each do |floating_ip_pool|
rows << [floating_ip_pool['name']]
end
display_table(['Floating IP pools'], rows)
display_table(env, ['Floating IP pools'], rows)

rows = []
floating_ips.each do |floating_ip|
rows << [floating_ip['id'], floating_ip['ip'], floating_ip['pool'], floating_ip['instance_id']]
end
display_table(['Id', 'IP', 'Pool', 'Instance id'], rows)
display_table(env, ['Id', 'IP', 'Pool', 'Instance id'], rows)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def self.synopsis
def cmd(name, argv, env)
fail Errors::NoArgRequiredForCommand, cmd: name unless argv.size == 1
images = env[:openstack_client].nova.get_all_images(env)
display_item_list(images)
display_item_list(env, images)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def self.synopsis
def cmd(name, argv, env)
fail Errors::NoArgRequiredForCommand, cmd: name unless argv.size == 1
flavors = env[:openstack_client].neutron.get_private_networks(env)
display_item_list(flavors)
display_item_list(env, flavors)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions source/lib/vagrant-openstack-provider/command/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ module VagrantPlugins
module Openstack
module Command
module Utils
def display_item_list(items)
def display_item_list(env, items)
rows = []
items.each do |item|
rows << [item.id, item.name]
end
display_table(%w('Id' 'Name'), rows)
display_table(env, %w('Id' 'Name'), rows)
end

def display_table(headers, rows)
def display_table(env, headers, rows)
table = Terminal::Table.new headings: headers, rows: rows
@env.ui.info("\n#{table}\n")
env[:ui].info("\n#{table}\n")
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require 'vagrant-openstack-provider/spec_helper'

describe VagrantPlugins::Openstack::Command::FloatingIpList do
describe 'cmd' do

let(:config) do
double('config').tap do |config|
config.stub(:openstack_auth_url) { 'http://keystoneAuthV2' }
config.stub(:openstack_compute_url) { nil }
config.stub(:openstack_network_url) { nil }
config.stub(:tenant_name) { 'testTenant' }
config.stub(:username) { 'username' }
config.stub(:password) { 'password' }
end
end

let(:nova) do
double('nova').tap do |nova|
nova.stub(:get_floating_ip_pools) do
[
{
'name' => 'pool1'
},
{
'name' => 'pool2'
}
]
end
nova.stub(:get_floating_ips) do
[
{
'fixed_ip' => nil,
'id' => 1,
'instance_id' => nil,
'ip' => '10.10.10.1',
'pool' => 'pool1'
},
{
'fixed_ip' => nil,
'id' => 2,
'instance_id' => 'inst001',
'ip' => '10.10.10.2',
'pool' => 'pool2'
}
]
end
end
end

let(:env) do
Hash.new.tap do |env|
env[:ui] = double('ui')
env[:ui].stub(:info).with(anything)
env[:openstack_client] = double
env[:openstack_client].stub(:nova) { nova }
end
end

before :each do
@floating_ip_list_cmd = VagrantPlugins::Openstack::Command::FloatingIpList.new(nil, env)
end

it 'should get floating ip and floating ip pool from server' do
@floating_ip_list_cmd.cmd('floatingip-list', ['--'], env)
end
end
end

0 comments on commit d93ef9a

Please sign in to comment.