linux_admin 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,7 +14,7 @@ class LinuxAdmin
14
14
  params["--database"] = nil if options[:database]
15
15
  params["--unique-md-filenames"] = nil if options[:unique_file_names]
16
16
 
17
- run(cmd, :params => params)
17
+ run!(cmd, :params => params)
18
18
  end
19
19
 
20
20
  def self.download_packages(path, packages, options = {})
@@ -32,7 +32,7 @@ class LinuxAdmin
32
32
  params["-a"] = options[:arch] if options[:arch]
33
33
  params[nil] = packages
34
34
 
35
- run(cmd, :params => params)
35
+ run!(cmd, :params => params)
36
36
  end
37
37
 
38
38
  def self.repo_settings
@@ -43,7 +43,7 @@ class LinuxAdmin
43
43
  cmd = "yum check-update"
44
44
  params = {nil => packages} unless packages.blank?
45
45
 
46
- exitstatus = run(cmd, :params => params, :return_exitstatus => true)
46
+ exitstatus = run(cmd, :params => params).exit_status
47
47
  case exitstatus
48
48
  when 0; false
49
49
  when 100; true
@@ -55,7 +55,7 @@ class LinuxAdmin
55
55
  cmd = "yum -y update"
56
56
  params = {nil => packages} unless packages.blank?
57
57
 
58
- run(cmd, :params => params)
58
+ run!(cmd, :params => params)
59
59
  end
60
60
 
61
61
  def self.version_available(*packages)
@@ -64,7 +64,7 @@ class LinuxAdmin
64
64
  cmd = "repoquery --qf=\"%{name} %{version}\""
65
65
  params = {nil => packages}
66
66
 
67
- out = run(cmd, :params => params, :return_output => true)
67
+ out = run!(cmd, :params => params).output
68
68
 
69
69
  items = out.split("\n")
70
70
  items.each_with_object({}) do |i, versions|
data/spec/common_spec.rb CHANGED
@@ -30,85 +30,115 @@ describe LinuxAdmin::Common do
30
30
  context ".cmd" do
31
31
  it "looks up local command from id" do
32
32
  d = double(LinuxAdmin::Distro)
33
- d.class::COMMANDS = { :sh => '/bin/sh' }
33
+ d.class::COMMANDS = {:sh => '/bin/sh'}
34
34
  LinuxAdmin::Distro.should_receive(:local).and_return(d)
35
35
  subject.cmd(:sh).should == '/bin/sh'
36
36
  end
37
37
  end
38
38
 
39
- context ".run" do
39
+ shared_examples_for "run" do
40
40
  context "with params" do
41
+ before do
42
+ subject.stub(:exitstatus => 0)
43
+ end
44
+
41
45
  it "sanitizes crazy params" do
42
46
  subject.should_receive(:launch).once.with("true --user bob --pass P@\\$sw0\\^\\&\\ \\|\\<\\>/-\\+\\*d\\% --db --desc=Some\\ Description pkg1 some\\ pkg --pool 123 --pool 456", {})
43
- subject.run("true", :params => modified_params, :return_exitstatus => true)
47
+ subject.send(run_method, "true", :params => modified_params)
44
48
  end
45
49
 
46
50
  it "as empty hash" do
47
51
  subject.should_receive(:launch).once.with("true", {})
48
- subject.run("true", :params => {}, :return_exitstatus => true)
52
+ subject.send(run_method, "true", :params => {})
49
53
  end
50
54
 
51
55
  it "as nil" do
52
56
  subject.should_receive(:launch).once.with("true", {})
53
- subject.run("true", :params => nil, :return_exitstatus => true)
57
+ subject.send(run_method, "true", :params => nil)
54
58
  end
55
59
 
56
60
  it "won't modify caller params" do
57
61
  orig_params = params.dup
58
- subject.run("true", :params => params, :return_exitstatus => true)
62
+ subject.stub(:launch)
63
+ subject.send(run_method, "true", :params => params)
59
64
  expect(orig_params).to eq(params)
60
65
  end
66
+
67
+ it "supports spawn's chdir option" do
68
+ subject.should_receive(:launch).once.with("true", {:chdir => ".."})
69
+ subject.send(run_method, "true", :chdir => "..")
70
+ end
61
71
  end
62
72
 
63
73
  context "with real execution" do
64
74
  before do
65
- Kernel.should_receive(:spawn).and_call_original
75
+ Kernel.stub(:spawn).and_call_original
66
76
  end
67
77
 
68
78
  it "command ok exit ok" do
69
- expect(subject.run("true")).to be_true
79
+ expect(subject.send(run_method, "true")).to be_kind_of CommandResult
70
80
  end
71
81
 
72
82
  it "command ok exit bad" do
73
- expect { subject.run("false") }.to raise_error(CommandError)
83
+ if run_method == "run!"
84
+ error = nil
85
+
86
+ # raise_error with do/end block notation is broken in rspec-expectations 2.14.x
87
+ # and has been fixed in master but not yet released.
88
+ # See: https://github.com/rspec/rspec-expectations/commit/b0df827f4c12870aa4df2f20a817a8b01721a6af
89
+ expect {subject.send(run_method, "false")}.to raise_error {|e| error = e }
90
+ expect(error).to be_kind_of CommandResultError
91
+ expect(error.result).to be_kind_of CommandResult
92
+ else
93
+ expect {subject.send(run_method, "false")}.to_not raise_error
94
+ end
74
95
  end
75
96
 
76
97
  it "command bad" do
77
- expect { subject.run("XXXXX") }.to raise_error(Errno::ENOENT)
98
+ expect {subject.send(run_method, "XXXXX")}.to raise_error(Errno::ENOENT)
78
99
  end
79
100
 
80
- context "with :return_exitstatus => true" do
101
+ context "#exit_status" do
81
102
  it "command ok exit ok" do
82
- expect(subject.run("true", :return_exitstatus => true)).to eq(0)
103
+ expect(subject.send(run_method, "true").exit_status).to eq(0)
83
104
  end
84
105
 
85
106
  it "command ok exit bad" do
86
- expect(subject.run("false", :return_exitstatus => true)).to eq(1)
87
- end
88
-
89
- it "command bad" do
90
- expect(subject.run("XXXXX", :return_exitstatus => true)).to be_nil
107
+ expect(subject.send(run_method, "false").exit_status).to eq(1) if run_method == "run"
91
108
  end
92
109
  end
93
110
 
94
- context "with :return_output => true" do
111
+ context "#output" do
95
112
  it "command ok exit ok" do
96
- expect(subject.run("echo \"Hello World\"", :return_output => true)).to eq("Hello World\n")
113
+ expect(subject.send(run_method, "echo \"Hello World\"").output).to eq("Hello World\n")
97
114
  end
98
115
 
99
116
  it "command ok exit bad" do
100
- expect { subject.run("false", :return_output => true) }.to raise_error(CommandError)
117
+ expect(subject.send(run_method, "echo 'bad' && false").output).to eq("bad\n") if run_method == "run"
101
118
  end
119
+ end
102
120
 
103
- it "command bad" do
104
- expect { subject.run("XXXXX", :return_output => true) }.to raise_error(Errno::ENOENT)
121
+ context "#error" do
122
+ it "command ok exit ok" do
123
+ expect(subject.send(run_method, "echo \"Hello World\" >&2").error).to eq("Hello World\n")
124
+ end
125
+
126
+ it "command ok exit bad" do
127
+ expect(subject.send(run_method, "echo 'bad' >&2 && false").error).to eq("bad\n") if run_method == "run"
105
128
  end
106
129
  end
107
130
  end
131
+ end
132
+
133
+ context ".run" do
134
+ include_examples "run" do
135
+ let(:run_method) {"run"}
136
+ end
137
+ end
108
138
 
109
- it "supports spawn's chdir option" do
110
- subject.should_receive(:launch).once.with("true", {:chdir => ".."})
111
- subject.run("true", :chdir => "..", :return_exitstatus => true)
139
+ context ".run!" do
140
+ include_examples "run" do
141
+ let(:run_method) {"run!"}
112
142
  end
113
143
  end
114
144
  end
data/spec/disk_spec.rb CHANGED
@@ -15,11 +15,10 @@ describe LinuxAdmin::Disk do
15
15
  describe "#size" do
16
16
  it "uses fdisk" do
17
17
  disk = LinuxAdmin::Disk.new :path => '/dev/hda'
18
- disk.should_receive(:run).
18
+ disk.should_receive(:run!).
19
19
  with(disk.cmd(:fdisk),
20
- :return_output => true,
21
20
  :params => {"-l" => nil}).
22
- and_return("")
21
+ and_return(double(:output => ""))
23
22
  disk.size
24
23
  end
25
24
 
@@ -39,7 +38,7 @@ Disk identifier: 0x3ddb508b
39
38
  eos
40
39
 
41
40
  disk = LinuxAdmin::Disk.new :path => '/dev/hda'
42
- disk.stub(:run).and_return(fdisk)
41
+ disk.stub(:run!).and_return(double(:output => fdisk))
43
42
  disk.size.should == 500.1.gigabytes
44
43
  end
45
44
  end
@@ -49,9 +48,7 @@ eos
49
48
  disk = LinuxAdmin::Disk.new :path => '/dev/hda'
50
49
  disk.should_receive(:run).
51
50
  with(disk.cmd(:parted),
52
- :return_exitstatus => true,
53
- :return_output => true,
54
- :params => { nil => ['/dev/hda', 'print'] }).and_return ""
51
+ :params => { nil => ['/dev/hda', 'print'] }).and_return(double(:output => ""))
55
52
  disk.partitions
56
53
  end
57
54
 
@@ -76,7 +73,7 @@ Number Start End Size Type File system Flags
76
73
  3 162GB 163GB 1074MB logical linux-swap(v1)
77
74
  eos
78
75
  disk = LinuxAdmin::Disk.new
79
- disk.should_receive(:run).and_return(partitions)
76
+ disk.should_receive(:run).and_return(double(:output => partitions))
80
77
 
81
78
  disk.partitions[0].id.should == 1
82
79
  disk.partitions[0].disk.should == disk
@@ -112,32 +109,32 @@ eos
112
109
  end
113
110
 
114
111
  it "uses parted" do
115
- @disk.should_receive(:run).
112
+ @disk.should_receive(:run!).
116
113
  with(@disk.cmd(:parted),
117
114
  :params => { nil => ['/dev/hda', 'mkpart', 'primary', 1024, 2048] })
118
115
  @disk.create_partition 'primary', 1024
119
116
  end
120
117
 
121
118
  it "returns partition" do
122
- @disk.should_receive(:run) # stub out call to parted
119
+ @disk.should_receive(:run!) # stub out call to parted
123
120
  partition = @disk.create_partition 'primary', 1024
124
121
  partition.should be_an_instance_of(LinuxAdmin::Partition)
125
122
  end
126
123
 
127
124
  it "increments partition id" do
128
- @disk.should_receive(:run) # stub out call to parted
125
+ @disk.should_receive(:run!) # stub out call to parted
129
126
  partition = @disk.create_partition 'primary', 1024
130
127
  partition.id.should == 2
131
128
  end
132
129
 
133
130
  it "sets partition start to first unused sector on disk" do
134
- @disk.should_receive(:run) # stub out call to parted
131
+ @disk.should_receive(:run!) # stub out call to parted
135
132
  partition = @disk.create_partition 'primary', 1024
136
133
  partition.start_sector.should == 1024
137
134
  end
138
135
 
139
136
  it "stores new partition locally" do
140
- @disk.should_receive(:run) # stub out call to parted
137
+ @disk.should_receive(:run!) # stub out call to parted
141
138
  lambda {
142
139
  @disk.create_partition 'primary', 1024
143
140
  }.should change{@disk.partitions.size}.by(1)
@@ -147,15 +144,17 @@ eos
147
144
  describe "#clear!" do
148
145
  it "clears partitions" do
149
146
  disk = LinuxAdmin::Disk.new :path => '/dev/hda'
150
- disk.stub(:run).and_return("") # stub out call to cmds
147
+ disk.should_receive(:run).and_return(double(:output => "")) # stub out call to cmds
151
148
  disk.partitions << LinuxAdmin::Partition.new
149
+
150
+ disk.should_receive(:run!)
152
151
  disk.clear!
153
152
  disk.partitions.should be_empty
154
153
  end
155
154
 
156
155
  it "uses dd to clear partition table" do
157
156
  disk = LinuxAdmin::Disk.new :path => '/dev/hda'
158
- disk.should_receive(:run).
157
+ disk.should_receive(:run!).
159
158
  with(disk.cmd(:dd),
160
159
  :params => {'if=' => '/dev/zero', 'of=' => '/dev/hda',
161
160
  'bs=' => 512, 'count=' => 1})
@@ -164,7 +163,7 @@ eos
164
163
 
165
164
  it "returns self" do
166
165
  disk = LinuxAdmin::Disk.new :path => '/dev/hda'
167
- disk.stub(:run) # stub out call to dd
166
+ disk.stub(:run!) # stub out call to dd
168
167
  disk.clear!.should == disk
169
168
  end
170
169
  end
@@ -25,7 +25,7 @@ eos
25
25
  it "uses lvextend" do
26
26
  lv = described_class.new :name => 'lv'
27
27
  vg = LinuxAdmin::VolumeGroup.new :name => 'vg'
28
- lv.should_receive(:run).
28
+ lv.should_receive(:run!).
29
29
  with(vg.cmd(:lvextend),
30
30
  :params => ['lv', 'vg'])
31
31
  lv.extend_with(vg)
@@ -34,7 +34,7 @@ eos
34
34
  it "returns self" do
35
35
  lv = described_class.new :name => 'lv'
36
36
  vg = LinuxAdmin::VolumeGroup.new :name => 'vg'
37
- lv.stub(:run)
37
+ lv.stub(:run!)
38
38
  lv.extend_with(vg).should == lv
39
39
  end
40
40
  end
@@ -46,7 +46,7 @@ eos
46
46
 
47
47
  it "uses lvcreate" do
48
48
  described_class.instance_variable_set(:@lvs, [])
49
- described_class.should_receive(:run).
49
+ described_class.should_receive(:run!).
50
50
  with(LinuxAdmin.cmd(:lvcreate),
51
51
  :params => { '-n' => 'lv',
52
52
  nil => 'vg',
@@ -55,16 +55,16 @@ eos
55
55
  end
56
56
 
57
57
  it "returns new logical volume" do
58
- LinuxAdmin::VolumeGroup.stub(:run => "")
59
- described_class.stub(:run => "")
58
+ LinuxAdmin::VolumeGroup.stub(:run! => double(:output => ""))
59
+ described_class.stub(:run! => double(:output => ""))
60
60
  lv = described_class.create 'lv', @vg, '256G'
61
61
  lv.should be_an_instance_of(described_class)
62
62
  lv.name.should == 'lv'
63
63
  end
64
64
 
65
65
  it "adds logical volume to local registry" do
66
- LinuxAdmin::VolumeGroup.stub(:run => "")
67
- described_class.stub(:run => "")
66
+ LinuxAdmin::VolumeGroup.stub(:run! => double(:output => ""))
67
+ described_class.stub(:run! => double(:output => ""))
68
68
  lv = described_class.create 'lv', @vg, '256G'
69
69
  described_class.scan.should include(lv)
70
70
  end
@@ -72,18 +72,17 @@ eos
72
72
 
73
73
  describe "#scan" do
74
74
  it "uses lvdisplay" do
75
- described_class.should_receive(:run).
75
+ described_class.should_receive(:run!).
76
76
  with(LinuxAdmin.cmd(:lvdisplay),
77
- :return_output => true,
78
77
  :params => { '-c' => nil}).
79
- and_return(@logical_volumes)
80
- LinuxAdmin::VolumeGroup.should_receive(:run).and_return(@groups) # stub out call to vgdisplay
78
+ and_return(double(:output => @logical_volumes))
79
+ LinuxAdmin::VolumeGroup.should_receive(:run!).and_return(double(:output => @groups)) # stub out call to vgdisplay
81
80
  described_class.scan
82
81
  end
83
82
 
84
83
  it "returns local logical volumes" do
85
- described_class.should_receive(:run).and_return(@logical_volumes)
86
- LinuxAdmin::VolumeGroup.should_receive(:run).and_return(@groups)
84
+ described_class.should_receive(:run!).and_return(double(:output => @logical_volumes))
85
+ LinuxAdmin::VolumeGroup.should_receive(:run!).and_return(double(:output => @groups))
87
86
  lvs = described_class.scan
88
87
 
89
88
  lvs[0].should be_an_instance_of(described_class)
@@ -96,8 +95,8 @@ eos
96
95
  end
97
96
 
98
97
  it "resolves volume group references" do
99
- described_class.should_receive(:run).and_return(@logical_volumes)
100
- LinuxAdmin::VolumeGroup.should_receive(:run).and_return(@groups)
98
+ described_class.should_receive(:run!).and_return(double(:output => @logical_volumes))
99
+ LinuxAdmin::VolumeGroup.should_receive(:run!).and_return(double(:output => @groups))
101
100
  lvs = described_class.scan
102
101
  lvs[0].volume_group.should be_an_instance_of(LinuxAdmin::VolumeGroup)
103
102
  lvs[0].volume_group.name.should == 'vg_foobar'
@@ -7,7 +7,7 @@ describe LinuxAdmin::Partition do
7
7
 
8
8
  # stub out calls that modify system
9
9
  FileUtils.stub(:mkdir)
10
- @partition.stub(:run)
10
+ @partition.stub(:run!)
11
11
  end
12
12
 
13
13
  describe "#path" do
@@ -18,14 +18,14 @@ describe LinuxAdmin::Partition do
18
18
 
19
19
  describe "#format_to" do
20
20
  it "uses mke2fs" do
21
- @partition.should_receive(:run).
21
+ @partition.should_receive(:run!).
22
22
  with(@partition.cmd(:mke2fs),
23
23
  :params => { '-t' => 'ext4', nil => '/dev/sda2'})
24
24
  @partition.format_to('ext4')
25
25
  end
26
26
 
27
27
  it "sets fs type" do
28
- @partition.should_receive(:run) # ignore actual formatting cmd
28
+ @partition.should_receive(:run!) # ignore actual formatting cmd
29
29
  @partition.format_to('ext4')
30
30
  @partition.fs_type.should == 'ext4'
31
31
  end
@@ -33,7 +33,7 @@ describe LinuxAdmin::Partition do
33
33
 
34
34
  describe "#mount" do
35
35
  it "sets mount point" do
36
- @partition.should_receive(:run) # ignore actual mount cmd
36
+ @partition.should_receive(:run!) # ignore actual mount cmd
37
37
  @partition.mount
38
38
  @partition.mount_point.should == '/mnt/sda2'
39
39
  end
@@ -42,13 +42,13 @@ describe LinuxAdmin::Partition do
42
42
  it "creates mountpoint" do
43
43
  File.should_receive(:directory?).with('/mnt/sda2').and_return(false)
44
44
  FileUtils.should_receive(:mkdir).with('/mnt/sda2')
45
- @partition.should_receive(:run) # ignore actual mount cmd
45
+ @partition.should_receive(:run!) # ignore actual mount cmd
46
46
  @partition.mount
47
47
  end
48
48
  end
49
49
 
50
50
  it "mounts partition" do
51
- @partition.should_receive(:run).
51
+ @partition.should_receive(:run!).
52
52
  with(@partition.cmd(:mount),
53
53
  :params => { nil => ['/dev/sda2', '/mnt/sda2']})
54
54
  @partition.mount
@@ -58,7 +58,7 @@ describe LinuxAdmin::Partition do
58
58
  describe "#umount" do
59
59
  it "unmounts partition" do
60
60
  @partition.mount_point = '/mnt/sda2'
61
- @partition.should_receive(:run).
61
+ @partition.should_receive(:run!).
62
62
  with(@partition.cmd(:umount),
63
63
  :params => { nil => ['/mnt/sda2']})
64
64
  @partition.umount
@@ -24,7 +24,7 @@ eos
24
24
  it "uses vgextend" do
25
25
  vg = LinuxAdmin::VolumeGroup.new :name => 'vg'
26
26
  pv = described_class.new :device_name => '/dev/hda'
27
- pv.should_receive(:run).
27
+ pv.should_receive(:run!).
28
28
  with(pv.cmd(:vgextend),
29
29
  :params => ['vg', '/dev/hda'])
30
30
  pv.attach_to(vg)
@@ -33,7 +33,7 @@ eos
33
33
  it "assigns volume group to physical volume" do
34
34
  vg = LinuxAdmin::VolumeGroup.new :name => 'vg'
35
35
  pv = described_class.new :device_name => '/dev/hda'
36
- pv.stub(:run)
36
+ pv.stub(:run!)
37
37
  pv.attach_to(vg)
38
38
  pv.volume_group.should == vg
39
39
  end
@@ -41,7 +41,7 @@ eos
41
41
  it "returns self" do
42
42
  vg = LinuxAdmin::VolumeGroup.new :name => 'vg'
43
43
  pv = described_class.new :device_name => '/dev/hda'
44
- pv.stub(:run)
44
+ pv.stub(:run!)
45
45
  pv.attach_to(vg).should == pv
46
46
  end
47
47
  end
@@ -56,23 +56,23 @@ eos
56
56
 
57
57
  it "uses pvcreate" do
58
58
  described_class.instance_variable_set(:@pvs, [])
59
- described_class.should_receive(:run).
59
+ described_class.should_receive(:run!).
60
60
  with(LinuxAdmin.cmd(:pvcreate),
61
61
  :params => { nil => '/dev/hda'})
62
62
  described_class.create disk
63
63
  end
64
64
 
65
65
  it "returns new physical volume" do
66
- LinuxAdmin::VolumeGroup.stub(:run => "")
67
- described_class.stub(:run => "")
66
+ LinuxAdmin::VolumeGroup.stub(:run! => double(:output => ""))
67
+ described_class.stub(:run! => double(:output => ""))
68
68
  pv = described_class.create disk
69
69
  pv.should be_an_instance_of(described_class)
70
70
  pv.device_name.should == '/dev/hda'
71
71
  end
72
72
 
73
73
  it "adds physical volume to local registry" do
74
- LinuxAdmin::VolumeGroup.stub(:run => "")
75
- described_class.stub(:run => "")
74
+ LinuxAdmin::VolumeGroup.stub(:run! => double(:output => ""))
75
+ described_class.stub(:run! => double(:output => ""))
76
76
  pv = described_class.create disk
77
77
  described_class.scan.should include(pv)
78
78
  end
@@ -80,18 +80,17 @@ eos
80
80
 
81
81
  describe "#scan" do
82
82
  it "uses pvdisplay" do
83
- described_class.should_receive(:run).
83
+ described_class.should_receive(:run!).
84
84
  with(LinuxAdmin.cmd(:pvdisplay),
85
- :return_output => true,
86
85
  :params => { '-c' => nil}).
87
- and_return(@physical_volumes)
88
- LinuxAdmin::VolumeGroup.should_receive(:run).and_return(@groups) # stub out call to vgdisplay
86
+ and_return(double(:output => @physical_volumes))
87
+ LinuxAdmin::VolumeGroup.should_receive(:run!).and_return(double(:output => @groups)) # stub out call to vgdisplay
89
88
  described_class.scan
90
89
  end
91
90
 
92
91
  it "returns local physical volumes" do
93
- described_class.should_receive(:run).and_return(@physical_volumes)
94
- LinuxAdmin::VolumeGroup.should_receive(:run).and_return(@groups)
92
+ described_class.should_receive(:run!).and_return(double(:output => @physical_volumes))
93
+ LinuxAdmin::VolumeGroup.should_receive(:run!).and_return(double(:output => @groups))
95
94
  pvs = described_class.scan
96
95
 
97
96
  pvs[0].should be_an_instance_of(described_class)
@@ -100,8 +99,8 @@ eos
100
99
  end
101
100
 
102
101
  it "resolves volume group references" do
103
- described_class.should_receive(:run).and_return(@physical_volumes)
104
- LinuxAdmin::VolumeGroup.should_receive(:run).and_return(@groups)
102
+ described_class.should_receive(:run!).and_return(double(:output => @physical_volumes))
103
+ LinuxAdmin::VolumeGroup.should_receive(:run!).and_return(double(:output => @groups))
105
104
  pvs = described_class.scan
106
105
  pvs[0].volume_group.should be_an_instance_of(LinuxAdmin::VolumeGroup)
107
106
  pvs[0].volume_group.name.should == 'vg_foobar'