linux_admin 0.19.0 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0909517c6cc080f6c8a9a82a7b1119e3a8c50452
4
- data.tar.gz: d2182ff8ce82b685d55e562968154010e486874c
3
+ metadata.gz: 45fe746b6f6401803b85ca5775c961a0fff87538
4
+ data.tar.gz: 748af8054c1540f1337541dbf02f9626a9d91ed8
5
5
  SHA512:
6
- metadata.gz: 7ff4ffc9e1c2650ed0c127b1a2338e5476e7caa211198b471288b17f3874aa6a1b84cacbcafdc165eed7ad971e07ee5d4b53270d2bb593ccdfac378cdbbbd029
7
- data.tar.gz: 9de9c8938028031bfe8f0793fe89f0e8e3dc6b329f4d6711842c43500ba41870f4bb5bf532e22206bd59f048a4879e92201c00c52ba5bc11f0c4e879fd3c49c3
6
+ metadata.gz: 8d31d976370bf3df926f66b0b075a2a3234b3f373faf4309eba1d1920163239e901297a9e846406e4c3e70429fcbb2c54feb01daef4cacc73e189468ad697ff0
7
+ data.tar.gz: e49ec88d3c04c0e2d56a75ec02d9814b9866e1c3144941160c390cd55182548959e3aaa8306ffbd07a6f51d1ed76a00ec5228ecce0dd052d265775e2989f4aef
@@ -51,11 +51,10 @@ module LinuxAdmin
51
51
 
52
52
  @network_conf[:mac] = parse_ip_output(ip_output, %r{link/ether}, 1)
53
53
 
54
- ip_route_res = Common.run!(Common.cmd("ip"), :params => ["route"])
55
- @network_conf[:gateway] = parse_ip_output(ip_route_res.output, /^default/, 2) if ip_route_res.success?
54
+ [4, 6].each do |version|
55
+ @network_conf["gateway#{version}".to_sym] = parse_ip_output(ip_route(version), /^default/, 2)
56
+ end
56
57
  true
57
- rescue AwesomeSpawn::CommandResultError => e
58
- raise NetworkInterfaceError.new(e.message, e.result)
59
58
  end
60
59
 
61
60
  # Retrieve the IPv4 address assigned to the interface
@@ -91,7 +90,7 @@ module LinuxAdmin
91
90
  #
92
91
  # @return [String] IPv4 netmask
93
92
  def netmask
94
- @network_conf[:mask]
93
+ @network_conf[:mask] ||= IPAddr.new('255.255.255.255').mask(prefix).to_s if prefix
95
94
  end
96
95
 
97
96
  # Retrieve the IPv6 sub-net mask assigned to the interface
@@ -99,10 +98,26 @@ module LinuxAdmin
99
98
  # @return [String] IPv6 netmask
100
99
  # @raise [ArgumentError] if the given scope is not `:global` or `:link`
101
100
  def netmask6(scope = :global)
102
- if scope == :global
103
- @network_conf[:mask6_global]
104
- elsif scope == :link
105
- @network_conf[:mask6_link]
101
+ if [:global, :link].include?(scope)
102
+ @network_conf["mask6_#{scope}".to_sym] ||= IPAddr.new('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff').mask(prefix6(scope)).to_s if prefix6(scope)
103
+ else
104
+ raise ArgumentError, "Unrecognized address scope #{scope}"
105
+ end
106
+ end
107
+
108
+ # Retrieve the IPv4 sub-net prefix length assigned to the interface
109
+ #
110
+ # @return [Numeric] IPv4 prefix length
111
+ def prefix
112
+ @network_conf[:prefix]
113
+ end
114
+
115
+ # Retrieve the IPv6 sub-net prefix length assigned to the interface
116
+ #
117
+ # @return [Numeric] IPv6 prefix length
118
+ def prefix6(scope = :global)
119
+ if [:global, :link].include?(scope)
120
+ @network_conf["prefix6_#{scope}".to_sym]
106
121
  else
107
122
  raise ArgumentError, "Unrecognized address scope #{scope}"
108
123
  end
@@ -112,7 +127,14 @@ module LinuxAdmin
112
127
  #
113
128
  # @return [String] IPv4 gateway address
114
129
  def gateway
115
- @network_conf[:gateway]
130
+ @network_conf[:gateway4]
131
+ end
132
+
133
+ # Retrieve the IPv6 default gateway associated with the interface
134
+ #
135
+ # @return [String] IPv6 gateway address
136
+ def gateway6
137
+ @network_conf[:gateway6]
116
138
  end
117
139
 
118
140
  # Brings up the network interface
@@ -152,6 +174,17 @@ module LinuxAdmin
152
174
  raise NetworkInterfaceError.new(e.message, e.result)
153
175
  end
154
176
 
177
+ # Runs the command `ip -[4/6] route` and returns the output
178
+ #
179
+ # @param version [Fixnum] Version of IP protocol (4 or 6)
180
+ # @return [String] The command output
181
+ # @raise [NetworkInterfaceError] if the command fails
182
+ def ip_route(version)
183
+ Common.run!(Common.cmd("ip"), :params => ["-#{version}", 'route']).output
184
+ rescue AwesomeSpawn::CommandResultError => e
185
+ raise NetworkInterfaceError.new(e.message, e.result)
186
+ end
187
+
155
188
  # Parses the IPv4 information from the output of `ip addr show <device>`
156
189
  #
157
190
  # @param ip_output [String] The command output
@@ -159,8 +192,9 @@ module LinuxAdmin
159
192
  cidr_ip = parse_ip_output(ip_output, /inet /, 1)
160
193
  return unless cidr_ip
161
194
 
162
- @network_conf[:address] = cidr_ip.split('/')[0]
163
- @network_conf[:mask] = IPAddr.new('255.255.255.255').mask(cidr_ip.split('/')[1]).to_s
195
+ parts = cidr_ip.split('/')
196
+ @network_conf[:address] = parts[0]
197
+ @network_conf[:prefix] = parts[1].to_i
164
198
  end
165
199
 
166
200
  # Parses the IPv6 information from the output of `ip addr show <device>`
@@ -168,13 +202,12 @@ module LinuxAdmin
168
202
  # @param ip_output [String] The command output
169
203
  # @param scope [Symbol] The IPv6 scope (either `:global` or `:local`)
170
204
  def parse_ip6(ip_output, scope)
171
- mask_addr = IPAddr.new('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff')
172
205
  cidr_ip = parse_ip_output(ip_output, /inet6 .* scope #{scope}/, 1)
173
206
  return unless cidr_ip
174
207
 
175
208
  parts = cidr_ip.split('/')
176
209
  @network_conf["address6_#{scope}".to_sym] = parts[0]
177
- @network_conf["mask6_#{scope}".to_sym] = mask_addr.mask(parts[1]).to_s
210
+ @network_conf["prefix6_#{scope}".to_sym] = parts[1].to_i
178
211
  end
179
212
  end
180
213
  end
@@ -39,6 +39,18 @@ module LinuxAdmin
39
39
  @interface_config["IPADDR"] = address
40
40
  end
41
41
 
42
+ # Set the IPv6 address for this interface
43
+ #
44
+ # @param address [String] IPv6 address including the prefix length (i.e. '::1/127')
45
+ # @raise ArgumentError if the address is not formatted properly
46
+ def address6=(address)
47
+ validate_ip(address)
48
+ @interface_config['BOOTPROTO'] = 'static'
49
+ @interface_config['IPV6INIT'] = 'yes'
50
+ @interface_config['DHCPV6C'] = 'no'
51
+ @interface_config['IPV6ADDR'] = address
52
+ end
53
+
42
54
  # Set the IPv4 gateway address for this interface
43
55
  #
44
56
  # @param address [String]
@@ -48,6 +60,15 @@ module LinuxAdmin
48
60
  @interface_config["GATEWAY"] = address
49
61
  end
50
62
 
63
+ # Set the IPv6 gateway address for this interface
64
+ #
65
+ # @param address [String] IPv6 address optionally including the prefix length
66
+ # @raise ArgumentError if the address is not formatted properly
67
+ def gateway6=(address)
68
+ validate_ip(address)
69
+ @interface_config['IPV6_DEFAULTGW'] = address
70
+ end
71
+
51
72
  # Set the IPv4 sub-net mask for this interface
52
73
  #
53
74
  # @param mask [String]
@@ -74,7 +95,7 @@ module LinuxAdmin
74
95
  end
75
96
 
76
97
  # Set up the interface to use DHCP
77
- # Removes any previously set static networking information
98
+ # Removes any previously set static IPv4 networking information
78
99
  def enable_dhcp
79
100
  @interface_config["BOOTPROTO"] = "dhcp"
80
101
  @interface_config.delete("IPADDR")
@@ -86,6 +107,18 @@ module LinuxAdmin
86
107
  @interface_config.delete("DOMAIN")
87
108
  end
88
109
 
110
+ # Set up the interface to use DHCPv6
111
+ # Removes any previously set static IPv6 networking information
112
+ def enable_dhcp6
113
+ @interface_config['IPV6INIT'] = 'yes'
114
+ @interface_config['DHCPV6C'] = 'yes'
115
+ @interface_config.delete('IPV6ADDR')
116
+ @interface_config.delete('IPV6_DEFAULTGW')
117
+ @interface_config.delete("DNS1")
118
+ @interface_config.delete("DNS2")
119
+ @interface_config.delete("DOMAIN")
120
+ end
121
+
89
122
  # Applies the given static network configuration to the interface
90
123
  #
91
124
  # @param ip [String] IPv4 address
@@ -104,6 +137,23 @@ module LinuxAdmin
104
137
  save
105
138
  end
106
139
 
140
+ # Applies the given static IPv6 network configuration to the interface
141
+ #
142
+ # @param ip [String] IPv6 address
143
+ # @param prefix [Number] prefix length for IPv6 address
144
+ # @param gw [String] gateway address
145
+ # @param dns [Array<String>] list of dns servers
146
+ # @param search [Array<String>] list of search domains
147
+ # @return [Boolean] true on success, false otherwise
148
+ # @raise ArgumentError if an IP is not formatted properly or interface does not start
149
+ def apply_static6(ip, prefix, gw, dns, search = nil)
150
+ self.address6 = "#{ip}/#{prefix}"
151
+ self.gateway6 = gw
152
+ self.dns = dns
153
+ self.search_order = search if search
154
+ save
155
+ end
156
+
107
157
  # Writes the contents of @interface_config to @interface_file as `key`=`value` pairs
108
158
  # and resets the interface
109
159
  #
@@ -51,5 +51,9 @@ module LinuxAdmin
51
51
  def status
52
52
  Common.run(Common.cmd(:service), :params => [name, "status"]).output
53
53
  end
54
+
55
+ def show
56
+ raise NotImplementedError
57
+ end
54
58
  end
55
59
  end
@@ -1,38 +1,31 @@
1
1
  module LinuxAdmin
2
2
  class SystemdService < Service
3
3
  def running?
4
- Common.run(Common.cmd(:systemctl),
5
- :params => {nil => ["status", name]}).exit_status == 0
4
+ Common.run(command_path, :params => ["status", name]).success?
6
5
  end
7
6
 
8
7
  def enable
9
- Common.run!(Common.cmd(:systemctl),
10
- :params => {nil => ["enable", name]})
8
+ Common.run!(command_path, :params => ["enable", name])
11
9
  self
12
10
  end
13
11
 
14
12
  def disable
15
- Common.run!(Common.cmd(:systemctl),
16
- :params => {nil => ["disable", name]})
13
+ Common.run!(command_path, :params => ["disable", name])
17
14
  self
18
15
  end
19
16
 
20
17
  def start
21
- Common.run!(Common.cmd(:systemctl),
22
- :params => {nil => ["start", name]})
18
+ Common.run!(command_path, :params => ["start", name])
23
19
  self
24
20
  end
25
21
 
26
22
  def stop
27
- Common.run!(Common.cmd(:systemctl),
28
- :params => {nil => ["stop", name]})
23
+ Common.run!(command_path, :params => ["stop", name])
29
24
  self
30
25
  end
31
26
 
32
27
  def restart
33
- status =
34
- Common.run(Common.cmd(:systemctl),
35
- :params => {nil => ["restart", name]}).exit_status
28
+ status = Common.run(command_path, :params => ["restart", name]).exit_status
36
29
 
37
30
  # attempt to manually stop/start if restart fails
38
31
  if status != 0
@@ -44,12 +37,43 @@ module LinuxAdmin
44
37
  end
45
38
 
46
39
  def reload
47
- Common.run!(Common.cmd(:systemctl), :params => ["reload", name])
40
+ Common.run!(command_path, :params => ["reload", name])
48
41
  self
49
42
  end
50
43
 
51
44
  def status
52
- Common.run(Common.cmd(:systemctl), :params => ["status", name]).output
45
+ Common.run(command_path, :params => ["status", name]).output
46
+ end
47
+
48
+ def show
49
+ output = Common.run!(command_path, :params => ["show", name]).output
50
+ output.split("\n").each_with_object({}) do |line, h|
51
+ k, v = line.split("=", 2)
52
+ h[k] = cast_show_value(k, v)
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ def command_path
59
+ Common.cmd(:systemctl)
60
+ end
61
+
62
+ def cast_show_value(key, value)
63
+ return value.to_i if value =~ /^\d+$/
64
+
65
+ case key
66
+ when /^.*Timestamp$/
67
+ Time.parse(value)
68
+ when /Exec(Start|Stop)/
69
+ parse_exec_value(value)
70
+ else
71
+ value
72
+ end
73
+ end
74
+
75
+ def parse_exec_value(value)
76
+ value[1..-2].strip.split(" ; ").map { |s| s.split("=", 2) }.to_h
53
77
  end
54
78
  end
55
79
  end
@@ -1,3 +1,3 @@
1
1
  module LinuxAdmin
2
- VERSION = "0.19.0"
2
+ VERSION = "0.20.0".freeze
3
3
  end
@@ -40,14 +40,14 @@ EOF
40
40
  subject(:dhcp_interface) do
41
41
  allow(File).to receive(:exist?).and_return(true)
42
42
  stub_foreach_to_string(ifcfg_file_dhcp)
43
- allow(AwesomeSpawn).to receive(:run!).twice.and_return(result("", 0))
43
+ allow(AwesomeSpawn).to receive(:run!).exactly(4).times.and_return(result("", 0))
44
44
  described_class.new(device_name)
45
45
  end
46
46
 
47
47
  subject(:static_interface) do
48
48
  allow(File).to receive(:exist?).and_return(true)
49
49
  stub_foreach_to_string(ifcfg_file_static)
50
- allow(AwesomeSpawn).to receive(:run!).twice.and_return(result("", 0))
50
+ allow(AwesomeSpawn).to receive(:run!).exactly(4).times.and_return(result("", 0))
51
51
  described_class.new(device_name)
52
52
  end
53
53
 
@@ -100,6 +100,22 @@ EOF
100
100
  end
101
101
  end
102
102
 
103
+ describe '#address6=' do
104
+ it 'sets the ipv6 address' do
105
+ address = 'fe80::1/64'
106
+ dhcp_interface.address6 = address
107
+ conf = dhcp_interface.interface_config
108
+ expect(conf['IPV6ADDR']).to eq(address)
109
+ expect(conf['BOOTPROTO']).to eq('static')
110
+ expect(conf['IPV6INIT']).to eq('yes')
111
+ expect(conf['DHCPV6C']).to eq('no')
112
+ end
113
+
114
+ it 'raises error when given a bad address' do
115
+ expect { dhcp_interface.address6 = '1::1::1' }.to raise_error(ArgumentError)
116
+ end
117
+ end
118
+
103
119
  describe "#gateway=" do
104
120
  it "sets the gateway address" do
105
121
  address = "192.168.1.1"
@@ -112,6 +128,14 @@ EOF
112
128
  end
113
129
  end
114
130
 
131
+ describe '#gateway6=' do
132
+ it 'sets the default gateway for IPv6' do
133
+ address = 'fe80::1/64'
134
+ dhcp_interface.gateway6 = address
135
+ expect(dhcp_interface.interface_config['IPV6_DEFAULTGW']).to eq(address)
136
+ end
137
+ end
138
+
115
139
  describe "#netmask=" do
116
140
  it "sets the sub-net mask" do
117
141
  mask = "255.255.255.0"
@@ -185,6 +209,17 @@ EOF
185
209
  end
186
210
  end
187
211
 
212
+ describe '#enable_dhcp6' do
213
+ it 'sets the correct configuration' do
214
+ [static_interface, dhcp_interface].each do |interface|
215
+ interface.enable_dhcp6
216
+ conf = interface.interface_config
217
+ expect(conf).to include('IPV6INIT' => 'yes', 'DHCPV6C' => 'yes')
218
+ expect(conf.keys).not_to include('IPV6ADDR', 'IPV6_DEFAULTGW')
219
+ end
220
+ end
221
+ end
222
+
188
223
  describe "#apply_static" do
189
224
  it "sets the correct configuration" do
190
225
  expect(dhcp_interface).to receive(:save)
@@ -201,6 +236,16 @@ EOF
201
236
  end
202
237
  end
203
238
 
239
+ describe '#apply_static6' do
240
+ it 'sets the static IPv6 configuration' do
241
+ expect(dhcp_interface).to receive(:save)
242
+ dhcp_interface.apply_static6('d:e:a:d:b:e:e:f', 127, 'd:e:a:d::/64', ['d:e:a:d::'])
243
+ conf = dhcp_interface.interface_config
244
+ expect(conf).to include('BOOTPROTO' => 'static', 'IPV6INIT' => 'yes', 'DHCPV6C' => 'no',
245
+ 'IPV6ADDR' => 'd:e:a:d:b:e:e:f/127', 'IPV6_DEFAULTGW' => 'd:e:a:d::/64')
246
+ end
247
+ end
248
+
204
249
  describe "#save" do
205
250
  let(:iface_file) { Pathname.new("/etc/sysconfig/network-scripts/ifcfg-#{device_name}") }
206
251
 
@@ -65,7 +65,12 @@ describe LinuxAdmin::NetworkInterface do
65
65
 
66
66
  IP_ROUTE_ARGS = [
67
67
  LinuxAdmin::Common.cmd("ip"),
68
- :params => %w(route)
68
+ :params => ['-4', 'route']
69
+ ]
70
+
71
+ IP6_ROUTE_ARGS = [
72
+ LinuxAdmin::Common.cmd("ip"),
73
+ :params => ['-6', 'route']
69
74
  ]
70
75
 
71
76
  IFUP_ARGS = [
@@ -101,6 +106,16 @@ IP_OUT
101
106
  IP_ROUTE_OUT = <<-IP_OUT
102
107
  default via 192.168.1.1 dev eth0 proto static metric 100
103
108
  192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.9 metric 100
109
+ IP_OUT
110
+ IP6_ROUTE_OUT = <<-IP_OUT
111
+ default via d:e:a:d:b:e:e:f dev eth0 proto static metric 100
112
+ fc00:dead:beef:a::/64 dev virbr1 proto kernel metric 256 linkdown pref medium
113
+ fe80::/64 dev eth0 proto kernel scope link metric 100
114
+ IP_OUT
115
+
116
+ IP_NONE_ADDR_OUT = <<-IP_OUT.freeze
117
+ 2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr1 state DOWN group default qlen 1000
118
+ link/ether 52:54:00:ce:b4:f4 brd ff:ff:ff:ff:ff:ff
104
119
  IP_OUT
105
120
 
106
121
  subject(:subj) do
@@ -109,6 +124,7 @@ IP_OUT
109
124
 
110
125
  allow(AwesomeSpawn).to receive(:run!).with(*IP_SHOW_ARGS).and_return(result(IP_ADDR_OUT, 0))
111
126
  allow(AwesomeSpawn).to receive(:run!).with(*IP_ROUTE_ARGS).and_return(result(IP_ROUTE_OUT, 0))
127
+ allow(AwesomeSpawn).to receive(:run!).with(*IP6_ROUTE_ARGS).and_return(result(IP6_ROUTE_OUT, 0))
112
128
  described_class.new("eth0")
113
129
  end
114
130
 
@@ -118,6 +134,17 @@ IP_OUT
118
134
 
119
135
  allow(AwesomeSpawn).to receive(:run!).with(*IP_SHOW_ARGS).and_return(result(IP6_ADDR_OUT, 0))
120
136
  allow(AwesomeSpawn).to receive(:run!).with(*IP_ROUTE_ARGS).and_return(result(IP_ROUTE_OUT, 0))
137
+ allow(AwesomeSpawn).to receive(:run!).with(*IP6_ROUTE_ARGS).and_return(result(IP6_ROUTE_OUT, 0))
138
+ described_class.new("eth0")
139
+ end
140
+
141
+ subject(:subj_no_net) do
142
+ allow(LinuxAdmin::Distros).to receive(:local).and_return(LinuxAdmin::Distros.generic)
143
+ described_class.dist_class(true)
144
+
145
+ allow(AwesomeSpawn).to receive(:run!).with(*IP_SHOW_ARGS).and_return(result(IP_NONE_ADDR_OUT, 0))
146
+ allow(AwesomeSpawn).to receive(:run!).with(*IP_ROUTE_ARGS).and_return(result(IP_ROUTE_OUT, 0))
147
+ allow(AwesomeSpawn).to receive(:run!).with(*IP6_ROUTE_ARGS).and_return(result(IP6_ROUTE_OUT, 0))
121
148
  described_class.new("eth0")
122
149
  end
123
150
 
@@ -138,6 +165,7 @@ IP_OUT
138
165
  awesome_error = AwesomeSpawn::CommandResultError.new("", nil)
139
166
  allow(AwesomeSpawn).to receive(:run!).with(*IP_SHOW_ARGS).and_return(result(IP_ADDR_OUT, 0))
140
167
  allow(AwesomeSpawn).to receive(:run!).with(*IP_ROUTE_ARGS).and_raise(awesome_error)
168
+ allow(AwesomeSpawn).to receive(:run!).with(*IP6_ROUTE_ARGS).and_raise(awesome_error)
141
169
  expect { subj.reload }.to raise_error(LinuxAdmin::NetworkInterfaceError)
142
170
  end
143
171
 
@@ -145,6 +173,7 @@ IP_OUT
145
173
  subj6
146
174
  allow(AwesomeSpawn).to receive(:run!).with(*IP_SHOW_ARGS).and_return(result(IP6_ADDR_OUT, 0))
147
175
  allow(AwesomeSpawn).to receive(:run!).with(*IP_ROUTE_ARGS).and_return(result(IP_ROUTE_OUT, 0))
176
+ allow(AwesomeSpawn).to receive(:run!).with(*IP6_ROUTE_ARGS).and_return(result(IP6_ROUTE_OUT, 0))
148
177
  expect { subj.reload }.to_not raise_error
149
178
  end
150
179
  end
@@ -179,6 +208,10 @@ IP_OUT
179
208
  it "returns the correct netmask" do
180
209
  expect(subj.netmask).to eq("255.255.255.0")
181
210
  end
211
+
212
+ it 'does not blow-up, when no ip assigned' do
213
+ expect(subj_no_net.netmask).to eq(nil)
214
+ end
182
215
  end
183
216
 
184
217
  describe "#netmask6" do
@@ -195,12 +228,38 @@ IP_OUT
195
228
  end
196
229
  end
197
230
 
231
+ describe '#prefix' do
232
+ it 'returns the correct prefix' do
233
+ expect(subj.prefix).to eq(24)
234
+ end
235
+ end
236
+
237
+ describe '#prefix6' do
238
+ it 'returns the correct global prefix length' do
239
+ expect(subj.prefix6).to eq(96)
240
+ end
241
+
242
+ it 'returns the correct link local prefix length' do
243
+ expect(subj.prefix6(:link)).to eq(64)
244
+ end
245
+
246
+ it 'raises ArgumentError when given a bad scope' do
247
+ expect { subj.prefix6(:garbage) }.to raise_error(ArgumentError)
248
+ end
249
+ end
250
+
198
251
  describe "#gateway" do
199
252
  it "returns the correct gateway address" do
200
253
  expect(subj.gateway).to eq("192.168.1.1")
201
254
  end
202
255
  end
203
256
 
257
+ describe '#gateway6' do
258
+ it 'returns the correct default gateway for IPv6 routing' do
259
+ expect(subj.gateway6).to eq('d:e:a:d:b:e:e:f')
260
+ end
261
+ end
262
+
204
263
  describe "#start" do
205
264
  it "returns true on success" do
206
265
  expect(AwesomeSpawn).to receive(:run).with(*IFUP_ARGS).and_return(result("", 0))
@@ -1,4 +1,6 @@
1
1
  describe LinuxAdmin::SystemdService do
2
+ let(:command) { LinuxAdmin::Common.cmd(:systemctl) }
3
+
2
4
  before do
3
5
  @service = described_class.new 'foo'
4
6
  end
@@ -6,27 +8,24 @@ describe LinuxAdmin::SystemdService do
6
8
  describe "#running?" do
7
9
  it "checks service" do
8
10
  expect(LinuxAdmin::Common).to receive(:run)
9
- .with(LinuxAdmin::Common.cmd(:systemctl),
10
- :params => {nil => %w(status foo)}).and_return(double(:exit_status => 0))
11
+ .with(command, :params => %w(status foo)).and_return(double(:success? => true))
11
12
  @service.running?
12
13
  end
13
14
 
14
15
  it "returns true when service is running" do
15
- expect(LinuxAdmin::Common).to receive(:run).and_return(double(:exit_status => 0))
16
+ expect(LinuxAdmin::Common).to receive(:run).and_return(double(:success? => true))
16
17
  expect(@service).to be_running
17
18
  end
18
19
 
19
20
  it "returns false when service is not running" do
20
- expect(LinuxAdmin::Common).to receive(:run).and_return(double(:exit_status => 1))
21
+ expect(LinuxAdmin::Common).to receive(:run).and_return(double(:success? => false))
21
22
  expect(@service).not_to be_running
22
23
  end
23
24
  end
24
25
 
25
26
  describe "#enable" do
26
27
  it "enables service" do
27
- expect(LinuxAdmin::Common).to receive(:run!)
28
- .with(LinuxAdmin::Common.cmd(:systemctl),
29
- :params => {nil => %w(enable foo)})
28
+ expect(LinuxAdmin::Common).to receive(:run!) .with(command, :params => %w(enable foo))
30
29
  @service.enable
31
30
  end
32
31
 
@@ -38,9 +37,7 @@ describe LinuxAdmin::SystemdService do
38
37
 
39
38
  describe "#disable" do
40
39
  it "disables service" do
41
- expect(LinuxAdmin::Common).to receive(:run!)
42
- .with(LinuxAdmin::Common.cmd(:systemctl),
43
- :params => {nil => %w(disable foo)})
40
+ expect(LinuxAdmin::Common).to receive(:run!).with(command, :params => %w(disable foo))
44
41
  @service.disable
45
42
  end
46
43
 
@@ -52,9 +49,7 @@ describe LinuxAdmin::SystemdService do
52
49
 
53
50
  describe "#start" do
54
51
  it "starts service" do
55
- expect(LinuxAdmin::Common).to receive(:run!)
56
- .with(LinuxAdmin::Common.cmd(:systemctl),
57
- :params => {nil => %w(start foo)})
52
+ expect(LinuxAdmin::Common).to receive(:run!).with(command, :params => %w(start foo))
58
53
  @service.start
59
54
  end
60
55
 
@@ -66,9 +61,7 @@ describe LinuxAdmin::SystemdService do
66
61
 
67
62
  describe "#stop" do
68
63
  it "stops service" do
69
- expect(LinuxAdmin::Common).to receive(:run!)
70
- .with(LinuxAdmin::Common.cmd(:systemctl),
71
- :params => {nil => %w(stop foo)})
64
+ expect(LinuxAdmin::Common).to receive(:run!).with(command, :params => %w(stop foo))
72
65
  @service.stop
73
66
  end
74
67
 
@@ -80,9 +73,7 @@ describe LinuxAdmin::SystemdService do
80
73
 
81
74
  describe "#restart" do
82
75
  it "restarts service" do
83
- expect(LinuxAdmin::Common).to receive(:run)
84
- .with(LinuxAdmin::Common.cmd(:systemctl),
85
- :params => {nil => %w(restart foo)}).and_return(double(:exit_status => 0))
76
+ expect(LinuxAdmin::Common).to receive(:run).with(command, :params => %w(restart foo)).and_return(double(:exit_status => 0))
86
77
  @service.restart
87
78
  end
88
79
 
@@ -101,8 +92,7 @@ describe LinuxAdmin::SystemdService do
101
92
 
102
93
  describe "#reload" do
103
94
  it "reloads service" do
104
- expect(LinuxAdmin::Common).to receive(:run!)
105
- .with(LinuxAdmin::Common.cmd(:systemctl), :params => %w(reload foo))
95
+ expect(LinuxAdmin::Common).to receive(:run!).with(command, :params => %w(reload foo))
106
96
  expect(@service.reload).to eq(@service)
107
97
  end
108
98
  end
@@ -111,9 +101,33 @@ describe LinuxAdmin::SystemdService do
111
101
  it "returns the service status" do
112
102
  status = "service status here"
113
103
  expect(LinuxAdmin::Common).to receive(:run)
114
- .with(LinuxAdmin::Common.cmd(:systemctl),
115
- :params => %w(status foo)).and_return(double(:output => status))
104
+ .with(command, :params => %w(status foo)).and_return(double(:output => status))
116
105
  expect(@service.status).to eq(status)
117
106
  end
118
107
  end
108
+
109
+ describe "#show" do
110
+ it "returns a hash of runtime information" do
111
+ output = <<-EOS
112
+ MainPID=29189
113
+ ExecMainStartTimestamp=Wed 2017-02-08 13:49:57 EST
114
+ ExecStart={ path=/bin/sh ; argv[]=/bin/sh -c /bin/evmserver.sh start ; status=0/0 }
115
+ ExecStop={ path=/bin/sh ; argv[]=/bin/sh -c /bin/evmserver.sh stop ; status=0/0 }
116
+ ControlGroup=/system.slice/evmserverd.service
117
+ MemoryCurrent=2865373184
118
+ EOS
119
+
120
+ hash = {
121
+ "MainPID" => 29_189,
122
+ "ExecMainStartTimestamp" => Time.new(2017, 2, 8, 13, 49, 57, "-05:00"),
123
+ "ExecStart" => {"path" => "/bin/sh", "argv[]" => "/bin/sh -c /bin/evmserver.sh start", "status" => "0/0"},
124
+ "ExecStop" => {"path" => "/bin/sh", "argv[]" => "/bin/sh -c /bin/evmserver.sh stop", "status" => "0/0"},
125
+ "ControlGroup" => "/system.slice/evmserverd.service",
126
+ "MemoryCurrent" => 2_865_373_184
127
+ }
128
+ expect(LinuxAdmin::Common).to receive(:run!)
129
+ .with(command, :params => %w(show foo)).and_return(double(:output => output))
130
+ expect(@service.show).to eq(hash)
131
+ end
132
+ end
119
133
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linux_admin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.19.0
4
+ version: 0.20.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Dunne
@@ -20,7 +20,7 @@ authors:
20
20
  autorequire:
21
21
  bindir: bin
22
22
  cert_chain: []
23
- date: 2016-10-31 00:00:00.000000000 Z
23
+ date: 2017-03-24 00:00:00.000000000 Z
24
24
  dependencies:
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
@@ -140,14 +140,14 @@ dependencies:
140
140
  requirements:
141
141
  - - ">="
142
142
  - !ruby/object:Gem::Version
143
- version: '0'
143
+ version: 1.6.8
144
144
  type: :runtime
145
145
  prerelease: false
146
146
  version_requirements: !ruby/object:Gem::Requirement
147
147
  requirements:
148
148
  - - ">="
149
149
  - !ruby/object:Gem::Version
150
- version: '0'
150
+ version: 1.6.8
151
151
  - !ruby/object:Gem::Dependency
152
152
  name: openscap
153
153
  requirement: !ruby/object:Gem::Requirement