Skip to content

Commit

Permalink
Refactor Netns
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Jun 10, 2016
1 parent 7b47ce8 commit e0f27e7
Show file tree
Hide file tree
Showing 17 changed files with 463 additions and 192 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ AllCops:
- spec/**/*.rb
- tasks/*.rake

Metrics/LineLength:
Exclude:
- features/step_definitions/*_steps.rb

Style/StringLiterals:
EnforcedStyle: single_quotes

Expand Down
14 changes: 6 additions & 8 deletions bin/phut
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ module Phut
Phut.pid_dir = options.fetch(:pid_dir)
Phut.log_dir = options.fetch(:log_dir)
Phut.socket_dir = options.fetch(:socket_dir)
Phut::Parser.new(stdout_logger).parse(args[0]).run
Phut::Parser.new(stdout_logger).parse(args[0])
end
end

Expand All @@ -97,7 +97,6 @@ module Phut
end

desc 'Stops a virtual network'
arg_name 'FILE'
command :stop do |c|
c.desc 'Location to put pid files'
c.flag [:P, :pid_dir], default_value: Phut.pid_dir
Expand All @@ -106,15 +105,14 @@ module Phut
c.desc 'Location to put socket files'
c.flag [:S, :socket_dir], default_value: Phut.socket_dir

c.action do |global_options, options, args|
stdout_logger = Logger.new($stderr).tap do |logger|
logger.formatter = proc { |_sev, _dtm, _name, msg| msg + "\n" }
logger.level = global_options[:verbose] ? Logger::DEBUG : Logger::INFO
end
c.action do |_global_options, options, _args|
Phut.pid_dir = options.fetch(:pid_dir)
Phut.log_dir = options.fetch(:log_dir)
Phut.socket_dir = options.fetch(:socket_dir)
Phut::Parser.new(stdout_logger).parse(args[0]).stop
Phut::Vswitch.destroy_all
Phut::Vhost.destroy_all
Phut::Netns.destroy_all
Phut::Link.destroy_all
end
end

Expand Down
79 changes: 79 additions & 0 deletions features/dsl_netns.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
Feature: The netns DSL directive.
@sudo
Scenario: phut run with "netns(name)"
Given a file named "phut.conf" with:
"""
netns('host1')
"""
When I do phut run "phut.conf"
Then a netns named "host1" should be started

@sudo
Scenario: ip option (no device)
Given a file named "phut.conf" with:
"""
netns('host1') {
ip '192.168.0.1'
}
"""
When I do phut run "phut.conf"
Then a netns named "host1" should be started
And the IP address of the netns "host1" should not be set

@sudo
Scenario: ip option
Given a file named "phut.conf" with:
"""
netns('host1') {
ip '192.168.0.1'
}
netns('host2') {
ip '192.168.0.2'
}
link 'host1', 'host2'
"""
When I do phut run "phut.conf"
Then the IP address of the netns "host1" should be "192.168.0.1"
And the IP address of the netns "host2" should be "192.168.0.2"

@sudo
Scenario: netmask option
Given a file named "phut.conf" with:
"""
netns('host1') {
ip '192.168.0.1'
netmask '255.255.255.0'
}
netns('host2') {
ip '192.168.0.2'
netmask '255.255.255.128'
}
link 'host1', 'host2'
"""
When I do phut run "phut.conf"
Then the netmask of the netns "host1" should be "255.255.255.0"
And the netmask of the netns "host2" should be "255.255.255.128"

@sudo
Scenario: route option
Given a file named "phut.conf" with:
"""
netns('host1') {
ip '192.168.0.1'
netmask '255.255.255.0'
route net: '0.0.0.0', gateway: '192.168.0.254'
}
netns('host2') {
ip '192.168.1.2'
netmask '255.255.255.0'
route net: '0.0.0.0', gateway: '192.168.1.254'
}
link 'host1', 'host2'
"""
When I do phut run "phut.conf"
Then the netns "host1" have the following route:
| net | gateway |
| 0.0.0.0 | 192.168.0.254 |
And the netns "host2" have the following route:
| net | gateway |
| 0.0.0.0 | 192.168.1.254 |
54 changes: 22 additions & 32 deletions features/step_definitions/phut_steps.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,4 @@
# frozen_string_literal: true
require 'phut'

# Temporary class
# FIXME: Delete me
class TearDownSyntax < Phut::Syntax
def vswitch(alias_name = nil, &block)
end

def vhost(name = nil, &block)
end

def link(name_a, name_b)
end
end

# Temporary class
# FIXME: Delete me
class TearDownParser
def parse(file)
Phut::Configuration.new do |config|
TearDownSyntax.new(config, @logger).instance_eval IO.read(file), file
config # .update_connections
end
end
end

When(/^I do phut run "(.*?)"$/) do |config_file|
@config_file = config_file
cd('.') do
Expand All @@ -42,24 +16,18 @@ def parse(file)
sleep time.to_i
end

# rubocop:disable LineLength
Then(/^a vswitch named "(.*?)" should be running$/) do |name|
expect(system("sudo ovs-vsctl br-exists #{Phut::Vswitch.prefix}#{name}")).to be_truthy
end
# rubocop:enable LineLength

# rubocop:disable LineLength
Then(/^a vswitch named "([^"]*)" \(controller port = (\d+)\) should be running$/) do |name, port_number|
step %(a vswitch named "#{name}" should be running)
expect(`sudo ovs-vsctl get-controller #{Phut::Vswitch.prefix}#{name}`.chomp).to eq "tcp:127.0.0.1:#{port_number}"
end
# rubocop:enable LineLength

# rubocop:disable LineLength
Then(/^a vswitch named "(.*?)" should not be running$/) do |name|
expect(system("sudo ovs-vsctl br-exists #{Phut::Vswitch.prefix}#{name}")).to be_falsey
end
# rubocop:enable LineLength

Then(/^a vhost named "(.*?)" launches$/) do |name|
step %(a file named "./tmp/pids/vhost.#{name}.pid" should exist)
Expand All @@ -68,3 +36,25 @@ def parse(file)
Then(/^a link is created between "(.*?)" and "(.*?)"$/) do |name_a, name_b|
expect(Phut::Link.find([name_a, name_b])).not_to be_nil
end

Then(/^a netns named "(.*?)" should be started$/) do |name|
expect(Phut::Netns.find_by!(name: name)).not_to be_nil
end

Then(/^the IP address of the netns "([^"]*)" should not be set$/) do |name|
expect(Phut::Netns.find_by!(name: name).ip_address).to be_nil
end

Then(/^the IP address of the netns "([^"]*)" should be "([^"]*)"$/) do |name, ip|
expect(Phut::Netns.find_by!(name: name).ip_address).to eq ip
end

Then(/^the netmask of the netns "([^"]*)" should be "([^"]*)"$/) do |name, netmask|
expect(Phut::Netns.find_by!(name: name).netmask).to eq netmask
end

Then(/^the netns "([^"]*)" have the following route:$/) do |name, table|
netns = Phut::Netns.find_by!(name: name)
expect(netns.route.net).to eq table.hashes.first['net']
expect(netns.route.gateway).to eq table.hashes.first['gateway']
end
1 change: 1 addition & 0 deletions features/support/env.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true
require 'aruba/cucumber'
require 'phut'

require 'coveralls'
Coveralls.wear_merged!
3 changes: 1 addition & 2 deletions features/support/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
Phut.pid_dir = @pid_dir
Phut.log_dir = @log_dir
Phut.socket_dir = @socket_dir
# FIXME: Delete me
TearDownParser.new.parse(@config_file).stop
Phut::Vswitch.destroy_all
Phut::Vhost.destroy_all
Phut::Netns.destroy_all
Phut::Link.destroy_all
end
end
Expand Down
39 changes: 19 additions & 20 deletions lib/phut/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
module Phut
# Parsed DSL data.
class Configuration
attr_reader :netns

def initialize
Vswitch.destroy_all
Vhost.destroy_all
Netns.all.clear
Link.destroy_all
@netns = []
yield self
update_connections
end

# rubocop:disable MethodLength
Expand All @@ -33,17 +33,6 @@ def fetch(key)
end
# rubocop:enable MethodLength

def update_connections
update_vswitch_ports
update_vhost_interfaces
update_netns_interfaces
self
end

def run
Netns.each(&:run)
end

def stop
[Vswitch, Vhost, Netns, Link].each do |klass|
klass.each(&:stop)
Expand All @@ -52,6 +41,12 @@ def stop

private

def update_connections
update_vswitch_ports
update_vhost_interfaces
update_netns_interfaces
end

def update_vswitch_ports
Link.each do |each|
maybe_connect_link_to_vswitch each
Expand All @@ -75,17 +70,21 @@ def update_vhost_interfaces
end

def update_netns_interfaces
Netns.each do |each|
each.network_device = find_network_device(each)
@netns.each do |each|
netns = Netns.create(name: each[:name],
ip_address: each[:ip], netmask: each[:netmask],
route: { net: each[:net],
gateway: each[:gateway] })
netns.device = find_network_device(each.name)
end
end

def find_network_device(vhost)
def find_network_device(name)
Link.each do |each|
device = each.device(vhost.name)
device = each.device(name)
return device if device
end
raise "No network device found for #{vhost}."
nil
end
end
end
Loading

0 comments on commit e0f27e7

Please sign in to comment.