networkmanager-dbus 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Guardfile +5 -1
- data/README.md +20 -2
- data/Rakefile +13 -1
- data/TODO.md +3 -3
- data/examples/change_ip.rb +17 -0
- data/examples/connectivity.rb +1 -1
- data/lib/dbus_interface.rb +14 -5
- data/lib/dbus_interface/class.rb +28 -4
- data/lib/dbus_interface/connection.rb +58 -0
- data/lib/dbus_interface/object.rb +15 -7
- data/lib/network_manager.rb +3 -2
- data/lib/network_manager/dbus.rb +2 -0
- data/lib/network_manager/dbus/device.rb +9 -7
- data/lib/network_manager/dbus/root.rb +8 -4
- data/lib/network_manager/dbus/settings.rb +18 -5
- data/lib/network_manager/dbus/settings_connection.rb +86 -0
- data/lib/network_manager/ip4_config.rb +33 -0
- data/lib/network_manager/version.rb +1 -1
- data/lib/networkmanager-dbus.rb +1 -1
- data/networkmanager-dbus.gemspec +4 -0
- data/script/console +7 -0
- data/script/spec_server +1 -0
- data/spec/fixtures/active_connections.yml +10 -0
- data/spec/fixtures/devices.yml +18 -37
- data/spec/fixtures/dhcp4_configs.yml +17 -0
- data/spec/fixtures/ip4_configs.yml +7 -0
- data/spec/fixtures/settings.yml +4 -14
- data/spec/fixtures/settings_connections.yml +10 -0
- data/spec/lib/dbus_interface/class_spec.rb +31 -0
- data/spec/lib/dbus_interface/object_spec.rb +32 -0
- data/spec/{network_manager → lib/network_manager}/dbus/active_connection_spec.rb +6 -6
- data/spec/lib/network_manager/dbus/device_spec.rb +58 -0
- data/spec/lib/network_manager/dbus/dhcp4_config_spec.rb +19 -0
- data/spec/lib/network_manager/dbus/ethernet_device_spec.rb +14 -0
- data/spec/lib/network_manager/dbus/ip4_config_spec.rb +19 -0
- data/spec/lib/network_manager/dbus/root_spec.rb +38 -0
- data/spec/lib/network_manager/dbus/settings_connection_spec.rb +69 -0
- data/spec/lib/network_manager/dbus/settings_spec.rb +53 -0
- data/spec/lib/network_manager/ip4_config_spec.rb +27 -0
- data/spec/{network_manager_spec.rb → lib/network_manager_spec.rb} +2 -0
- data/spec/mocks/dbus/ethernet_device_mock.rb +5 -0
- data/spec/mocks/dbus/root_mock.rb +14 -0
- data/spec/mocks/dbus/settings_connection_mock.rb +21 -0
- data/spec/mocks/dbus/settings_mock.rb +9 -0
- data/spec/mocks/dbus_mock.rb +33 -0
- data/spec/mocks/init.rb +18 -0
- data/spec/mocks/system_bus_mock.rb +5 -0
- data/spec/mocks/system_bus_service_mock.rb +9 -0
- data/spec/spec_helper.rb +22 -3
- data/spec/support/fixture_helper.rb +21 -4
- metadata +96 -28
- data/lib/network_manager/ip4_helper.rb +0 -14
- data/spec/network_manager/dbus/device_spec.rb +0 -36
- data/spec/network_manager/dbus/dhcp4_config_spec.rb +0 -14
- data/spec/network_manager/dbus/ethernet_device_spec.rb +0 -21
- data/spec/network_manager/dbus/ip4_config_spec.rb +0 -14
- data/spec/network_manager/dbus/root_spec.rb +0 -22
- data/spec/network_manager/dbus/settings_connection_spec.rb +0 -13
- data/spec/network_manager/dbus/settings_spec.rb +0 -30
@@ -1,4 +1,90 @@
|
|
1
1
|
class NetworkManager::DBus::SettingsConnection
|
2
|
+
class SecretsCacheInvalidError < StandardError; end
|
3
|
+
|
4
|
+
HW_TYPE_ETHERNET = '802-3-ethernet'
|
5
|
+
HW_TYPE_WIRELESS = '802-11-wireless'
|
6
|
+
HW_TYPE_BLUETOOTH = 'bluetooth'
|
7
|
+
SW_TYPE_VPN = 'vpn'
|
8
|
+
SW_TYPE_BRIDGE = 'bridge'
|
9
|
+
|
2
10
|
include DBusInterface::Object
|
11
|
+
no_properties!
|
3
12
|
map_dbus :default_iface => 'org.freedesktop.NetworkManager.Settings.Connection'
|
13
|
+
|
14
|
+
def settings
|
15
|
+
call('GetSettings').first
|
16
|
+
end
|
17
|
+
|
18
|
+
def delete
|
19
|
+
call('Delete')
|
20
|
+
end
|
21
|
+
|
22
|
+
def secrets(name = '')
|
23
|
+
call('GetSecrets', name)
|
24
|
+
rescue DBus::Error => e
|
25
|
+
raise SecretsCacheInvalidError.new("Secrets cache invalid (#{e})")
|
26
|
+
end
|
27
|
+
|
28
|
+
def name
|
29
|
+
settings['connection']['id']
|
30
|
+
end
|
31
|
+
|
32
|
+
def uuid
|
33
|
+
settings['connection']['uuid']
|
34
|
+
end
|
35
|
+
|
36
|
+
def type
|
37
|
+
settings['connection']['type']
|
38
|
+
end
|
39
|
+
|
40
|
+
def update(hash)
|
41
|
+
new_settings = hash.dup
|
42
|
+
unless new_settings.has_key? 'connection'
|
43
|
+
new_settings.merge!(settings)
|
44
|
+
end
|
45
|
+
call('Update', new_settings)
|
46
|
+
end
|
47
|
+
|
48
|
+
def name=(new_id)
|
49
|
+
hash = settings
|
50
|
+
hash['connection']['id'] = new_id
|
51
|
+
hash.delete 'ipv4'
|
52
|
+
update(hash)
|
53
|
+
end
|
54
|
+
|
55
|
+
IPV4_METHOD_AUTO = 'auto'
|
56
|
+
IPV4_METHOD_MANUAL = 'manual'
|
57
|
+
|
58
|
+
def ip4=(ip4)
|
59
|
+
hash = settings
|
60
|
+
hash['ipv4'] = ip4
|
61
|
+
update(hash)
|
62
|
+
end
|
63
|
+
|
64
|
+
def ip4_auto!
|
65
|
+
self.ip4 = {'method' => IPV4_METHOD_AUTO}
|
66
|
+
end
|
67
|
+
|
68
|
+
def ip4_manual=(addresses)
|
69
|
+
# ensure wrapper array
|
70
|
+
addresses = [addresses] unless addresses.is_a? Array
|
71
|
+
|
72
|
+
self.ip4 = {
|
73
|
+
'method' => IPV4_METHOD_MANUAL,
|
74
|
+
'addresses' => ['aau', addresses.map{|a| a.to_nm_au}]
|
75
|
+
}
|
76
|
+
end
|
77
|
+
|
78
|
+
def ip4
|
79
|
+
if ipv4 = settings['ipv4']
|
80
|
+
if ipv4['method'] == IPV4_METHOD_MANUAL
|
81
|
+
address = ipv4['addresses'].first
|
82
|
+
NetworkManager::Ip4Config.from_nm_au *address
|
83
|
+
else
|
84
|
+
IPV4_METHOD_AUTO
|
85
|
+
end
|
86
|
+
else
|
87
|
+
nil
|
88
|
+
end
|
89
|
+
end
|
4
90
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class NetworkManager::Ip4Config
|
2
|
+
attr_accessor :address, :gateway
|
3
|
+
|
4
|
+
def self.from_dot_notation(address, subnet = '255.255.255.0', gateway = '0.0.0.0')
|
5
|
+
obj = self.new
|
6
|
+
obj.address = IPAddress::IPv4.new(address)
|
7
|
+
obj.address.netmask = subnet
|
8
|
+
obj.gateway = IPAddress::IPv4.new(gateway)
|
9
|
+
obj.gateway.netmask = subnet
|
10
|
+
obj
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.from_nm_au(address_u32, prefix = 0, gateway_u32 = 0)
|
14
|
+
obj = self.new
|
15
|
+
obj.address = revert_octects(IPAddress::IPv4.parse_u32(address_u32))
|
16
|
+
obj.address.prefix = prefix
|
17
|
+
obj.gateway = revert_octects(IPAddress::IPv4.parse_u32(gateway_u32))
|
18
|
+
obj.gateway.prefix = prefix
|
19
|
+
obj
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_nm_au
|
23
|
+
[revert_octects(address).to_i, address.prefix.to_i, revert_octects(gateway).to_i]
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.revert_octects(ipv4)
|
27
|
+
IPAddress::IPv4.new(ipv4.octets.reverse.join('.'))
|
28
|
+
end
|
29
|
+
|
30
|
+
def revert_octects(ipv4)
|
31
|
+
self.class.revert_octects(ipv4)
|
32
|
+
end
|
33
|
+
end
|
data/lib/networkmanager-dbus.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require "network_manager"
|
1
|
+
require File.join(File.dirname(__FILE__), "network_manager")
|
data/networkmanager-dbus.gemspec
CHANGED
@@ -20,11 +20,15 @@ Gem::Specification.new do |s|
|
|
20
20
|
|
21
21
|
# dependencies
|
22
22
|
s.add_development_dependency "rake"
|
23
|
+
s.add_development_dependency "pry"
|
23
24
|
s.add_development_dependency "rspec"
|
25
|
+
s.add_development_dependency "rr"
|
24
26
|
s.add_development_dependency "guard"
|
25
27
|
s.add_development_dependency "guard-shell"
|
28
|
+
s.add_development_dependency "growl-rspec"
|
26
29
|
s.add_development_dependency "growl"
|
27
30
|
s.add_development_dependency "json"
|
28
31
|
s.add_development_dependency "marilyn-rpc"
|
32
|
+
s.add_runtime_dependency 'ipaddress'
|
29
33
|
s.add_runtime_dependency "ruby-dbus"
|
30
34
|
end
|
data/script/console
ADDED
data/script/spec_server
CHANGED
@@ -0,0 +1,10 @@
|
|
1
|
+
"/org/freedesktop/NetworkManager/ActiveConnection/0":
|
2
|
+
properties:
|
3
|
+
"Connection": "/org/freedesktop/NetworkManager/Settings/0"
|
4
|
+
"SpecificObject": "/"
|
5
|
+
"Uuid": "f6ea104f-48aa-4925-b4c7-4e7ae561099c"
|
6
|
+
"Devices": ["/org/freedesktop/NetworkManager/Devices/0"]
|
7
|
+
"State": 2
|
8
|
+
"Default": no
|
9
|
+
"Default6": no
|
10
|
+
"Vpn": no
|
data/spec/fixtures/devices.yml
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
0:
|
1
|
+
"/org/freedesktop/NetworkManager/Device/0":
|
2
2
|
properties:
|
3
3
|
"Interface": "eth0"
|
4
4
|
"Ip4Address": 1698212032
|
@@ -22,39 +22,20 @@
|
|
22
22
|
"PermHwAddress": "08:00:27:FB:3B:10"
|
23
23
|
"Speed": 1000
|
24
24
|
"Carrier": yes
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
"dhcp_lease_time": "3600"
|
43
|
-
"ip_address": "192.168.56.101"
|
44
|
-
"subnet_mask": "255.255.255.0"
|
45
|
-
"0": "0"
|
46
|
-
"202": "202"
|
47
|
-
"lookup-hostname": "lookup-hostname"
|
48
|
-
"network_number": "192.168.56.0"
|
49
|
-
"dhcp_server_identifier": "192.168.56.100"
|
50
|
-
active_connection:
|
51
|
-
properties:
|
52
|
-
"Connection": "/org/freedesktop/NetworkManager/Settings/0"
|
53
|
-
"SpecificObject": "/"
|
54
|
-
"Uuid": "9bd14d22-6da6-45d0-b68a-596c3c3297c2"
|
55
|
-
"Devices": ["/org/freedesktop/NetworkManager/Devices/0"]
|
56
|
-
"State": 2
|
57
|
-
"Default": no
|
58
|
-
"Default6": no
|
59
|
-
"Vpn": no
|
60
|
-
|
25
|
+
"/org/freedesktop/NetworkManager/Device/1":
|
26
|
+
properties:
|
27
|
+
"Udi": "/sys/devices/pci0000:00/0000:00:03.0/net/eth1"
|
28
|
+
"Interface": "eth1"
|
29
|
+
"IpInterface": ""
|
30
|
+
"Driver": "e1000"
|
31
|
+
"Capabilities": 3
|
32
|
+
"Ip4Address": 0
|
33
|
+
"State": 30
|
34
|
+
"ActiveConnection": "/"
|
35
|
+
"Ip4Config": "/"
|
36
|
+
"Dhcp4Config": "/"
|
37
|
+
"Ip6Config": "/"
|
38
|
+
"Dhcp6Config": "/"
|
39
|
+
"Managed": yes
|
40
|
+
"FirmwareMissing": no
|
41
|
+
"DeviceType": 1
|
@@ -0,0 +1,17 @@
|
|
1
|
+
"/org/freedesktop/NetworkManager/DHCP4Config/0":
|
2
|
+
properties:
|
3
|
+
"Options":
|
4
|
+
"4163": "4163"
|
5
|
+
"1500": "1500"
|
6
|
+
"subnet_cidr": "24"
|
7
|
+
"broadcast_address": "192.168.56.255"
|
8
|
+
"dhcp_message_type": "5"
|
9
|
+
"eth0": "eth0"
|
10
|
+
"dhcp_lease_time": "3600"
|
11
|
+
"ip_address": "192.168.56.101"
|
12
|
+
"subnet_mask": "255.255.255.0"
|
13
|
+
"0": "0"
|
14
|
+
"202": "202"
|
15
|
+
"lookup-hostname": "lookup-hostname"
|
16
|
+
"network_number": "192.168.56.0"
|
17
|
+
"dhcp_server_identifier": "192.168.56.100"
|
data/spec/fixtures/settings.yml
CHANGED
@@ -1,14 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
-
|
6
|
-
settings:
|
7
|
-
"connection":
|
8
|
-
"id": "Wired connection 1"
|
9
|
-
"uuid": "6190ee1e-c5c5-41f5-8de3-500d229970f9"
|
10
|
-
"timestamp": "1320083832"
|
11
|
-
"type": "802-3-ethernet"
|
12
|
-
"802-3-ethernet":
|
13
|
-
"s390-options": {}
|
14
|
-
"mac-address": [8, 0, 39, 251, 59, 16]
|
1
|
+
"/org/freedesktop/NetworkManager/Settings":
|
2
|
+
properties:
|
3
|
+
Hostname: 'dev-vm'
|
4
|
+
CanModify: yes
|
@@ -0,0 +1,10 @@
|
|
1
|
+
"/org/freedesktop/NetworkManager/SettingsConnection/0":
|
2
|
+
settings:
|
3
|
+
"connection":
|
4
|
+
"id": "Wired connection 1"
|
5
|
+
"uuid": "6190ee1e-c5c5-41f5-8de3-500d229970f9"
|
6
|
+
"timestamp": "1320083832"
|
7
|
+
"type": "802-3-ethernet"
|
8
|
+
"802-3-ethernet":
|
9
|
+
"s390-options": {}
|
10
|
+
"mac-address": [8, 0, 39, 251, 59, 16]
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "DBusInterface::Class" do
|
4
|
+
|
5
|
+
it 'should map_dbus :default_iface' do
|
6
|
+
module AWorkingDummyInterface
|
7
|
+
extend ::DBusInterface::Class
|
8
|
+
map_dbus :default_iface => 'foo.bar', :object_path => 'foo/bar'
|
9
|
+
end
|
10
|
+
AWorkingDummyInterface.default_iface.should == 'foo.bar'
|
11
|
+
AWorkingDummyInterface.object_path.should == 'foo/bar'
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should raise if map_dbus lacks :default_iface' do
|
15
|
+
lambda do
|
16
|
+
module ABrokenDummyInterface
|
17
|
+
extend ::DBusInterface::Class
|
18
|
+
map_dbus :foo => 'bar'
|
19
|
+
end
|
20
|
+
end.should raise_error(ArgumentError, 'map_dbus must specify at least :default_iface')
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should raise if map_dbus lacks :default_iface' do
|
24
|
+
lambda do
|
25
|
+
module ABrokenDummyInterface
|
26
|
+
extend ::DBusInterface::Class
|
27
|
+
map_dbus 123
|
28
|
+
end
|
29
|
+
end.should raise_error(ArgumentError, 'map_dbus must receive a Hash instead of Fixnum')
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "DBusInterface::Object" do
|
4
|
+
|
5
|
+
it 'should use default object_path' do
|
6
|
+
class ADummyObject
|
7
|
+
include ::DBusInterface::Object
|
8
|
+
map_dbus :default_iface => 'foo.bar', :object_path => 'foo/bar'
|
9
|
+
end
|
10
|
+
ADummyObject.new.object_path.should == 'foo/bar'
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should use overwrite object_path' do
|
14
|
+
class ADummyObject
|
15
|
+
include ::DBusInterface::Object
|
16
|
+
map_dbus :default_iface => 'foo.bar', :object_path => 'foo/bar'
|
17
|
+
end
|
18
|
+
ADummyObject.new('boo/laa').object_path.should == 'boo/laa'
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should return nil if call is on empty object path' do
|
22
|
+
dbus_interface_mock
|
23
|
+
class ADummyObject
|
24
|
+
include ::DBusInterface::Object
|
25
|
+
map_dbus :default_iface => 'foo.bar', :object_path => 'foo/bar'
|
26
|
+
end
|
27
|
+
|
28
|
+
pending 'needs better mocking here'
|
29
|
+
ADummyObject.new('/').properties.should be_nil
|
30
|
+
ADummyObject.new(nil).properties.should be_nil
|
31
|
+
end
|
32
|
+
end
|
@@ -2,18 +2,18 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe "NetworkManager::DBus::ActiveConnection" do
|
4
4
|
before :each do
|
5
|
-
@
|
6
|
-
@
|
7
|
-
@config = @device['active_connection']
|
5
|
+
@data = fixture('active_connections.yml')
|
6
|
+
@object_paths = object_paths_from_fixture('active_connections.yml')
|
8
7
|
end
|
9
|
-
|
8
|
+
|
10
9
|
it 'should map interface "org.freedesktop.NetworkManager.Connection.Active"' do
|
11
10
|
NetworkManager::DBus::ActiveConnection.default_iface.should ==
|
12
11
|
'org.freedesktop.NetworkManager.Connection.Active'
|
13
12
|
end
|
14
13
|
|
15
14
|
it 'shoud list properties' do
|
16
|
-
|
17
|
-
|
15
|
+
network_manager_dbus_mock
|
16
|
+
con = NetworkManager::DBus::ActiveConnection.new @object_paths.first
|
17
|
+
con.properties.should == @data.first.last['properties']
|
18
18
|
end
|
19
19
|
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "NetworkManager::DBus::Device" do
|
4
|
+
before :each do
|
5
|
+
@devices = fixture('devices.yml')
|
6
|
+
@object_paths = object_paths_from_fixture('devices.yml')
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "NM_DEVICE_TYPE" do
|
10
|
+
before :each do
|
11
|
+
network_manager_dbus_mock
|
12
|
+
@nm_device_type = NetworkManager::DBus::Device::NM_DEVICE_TYPE__PROPERTY
|
13
|
+
@device = NetworkManager::DBus::Device.new @object_paths.first
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "NM_DEVICE_TYPE_ETHERNET" do
|
17
|
+
it "should detect ethernet devices with NM_DEVICE_TYPE_ETHERNET" do
|
18
|
+
@device.properties[@nm_device_type] =
|
19
|
+
NetworkManager::DBus::Device::NM_DEVICE_TYPE_ETHERNET
|
20
|
+
@device.ethernet?.should == true
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should not detect ethernet devices withour NM_DEVICE_TYPE_ETHERNET" do
|
24
|
+
@device.properties[@nm_device_type] = 1234567
|
25
|
+
@device.ethernet?.should == false
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should instantiate ethernet devices" do
|
29
|
+
@device.properties[@nm_device_type] =
|
30
|
+
NetworkManager::DBus::Device::NM_DEVICE_TYPE_ETHERNET
|
31
|
+
@device.ethernet.class.should == NetworkManager::DBus::EthernetDevice
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should not instantiate unknown device types" do
|
35
|
+
@device.properties[@nm_device_type] = 12345123
|
36
|
+
@device.ethernet.should == nil
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should provide ip4_address" do
|
42
|
+
network_manager_dbus_mock
|
43
|
+
device = NetworkManager::DBus::Device.new @object_paths.first
|
44
|
+
device.ip4_address.to_s.should == '192.168.56.101'
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should provide ip4_config' do
|
48
|
+
network_manager_dbus_mock
|
49
|
+
device = NetworkManager::DBus::Device.new @object_paths.first
|
50
|
+
device.ip4_config.class.should == NetworkManager::DBus::Ip4Config
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should provide active_connection' do
|
54
|
+
network_manager_dbus_mock
|
55
|
+
device = NetworkManager::DBus::Device.new @object_paths.first
|
56
|
+
device.active_connection.class.should == NetworkManager::DBus::ActiveConnection
|
57
|
+
end
|
58
|
+
end
|