lxc 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/lxc/container.rb +13 -4
- data/lib/lxc/version.rb +1 -1
- data/spec/lxc/container_spec.rb +74 -0
- data/spec/lxc_spec.rb +9 -9
- metadata +5 -3
data/lib/lxc/container.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'timeout'
|
2
|
+
|
1
3
|
class LXC
|
2
4
|
|
3
5
|
# Container Error Class
|
@@ -55,7 +57,7 @@ class LXC
|
|
55
57
|
|
56
58
|
# RegEx pattern for extracting the container PID from the "lxc-info"
|
57
59
|
# command output.
|
58
|
-
REGEX_PID = /^pid:\s+([
|
60
|
+
REGEX_PID = /^pid:\s+([-\d]+)$/
|
59
61
|
|
60
62
|
# Returns the container name
|
61
63
|
#
|
@@ -75,6 +77,7 @@ class LXC
|
|
75
77
|
# Runs the "lxc-create" command.
|
76
78
|
#
|
77
79
|
# @param [Array] args Additional command-line arguments.
|
80
|
+
# @return [Symbol] The state of the container.
|
78
81
|
def create(*args)
|
79
82
|
self.exec("lxc-create", *args)
|
80
83
|
self.state
|
@@ -82,12 +85,15 @@ class LXC
|
|
82
85
|
|
83
86
|
# Destroy the container
|
84
87
|
#
|
85
|
-
#
|
88
|
+
# Runs the "lxc-destroy" command. If the container has not been stopped
|
89
|
+
# first then this will fail unless '-f' is passed as an argument. See
|
90
|
+
# the 'lxc-destroy' man page for more details.
|
86
91
|
#
|
87
92
|
# @param [Array] args Additional command-line arguments.
|
93
|
+
# @return [Symbol] The state of the container.
|
88
94
|
def destroy(*args)
|
89
|
-
self.stop
|
90
95
|
self.exec("lxc-destroy", *args)
|
96
|
+
self.state
|
91
97
|
end
|
92
98
|
|
93
99
|
# Start the container
|
@@ -188,6 +194,9 @@ class LXC
|
|
188
194
|
#
|
189
195
|
# Runs the "lxc-wait" command.
|
190
196
|
#
|
197
|
+
# The timeout only works when using remote control via SSH and will orphan
|
198
|
+
# the process ('lxc-wait') on the remote host.
|
199
|
+
#
|
191
200
|
# @param [Array] states An array of container states for which we will wait
|
192
201
|
# for the container to change state to.
|
193
202
|
# @param [Integer] timeout How long in seconds we will wait before the
|
@@ -200,7 +209,7 @@ class LXC
|
|
200
209
|
end.join('|')
|
201
210
|
|
202
211
|
begin
|
203
|
-
Timeout
|
212
|
+
Timeout.timeout(timeout) do
|
204
213
|
self.exec("lxc-wait", "-s", %('#{state_arg}'))
|
205
214
|
end
|
206
215
|
rescue Timeout::Error => e
|
data/lib/lxc/version.rb
CHANGED
@@ -0,0 +1,74 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe LXC::Container do
|
4
|
+
|
5
|
+
TEST_CONTAINER_NAME = "test-container"
|
6
|
+
|
7
|
+
subject {
|
8
|
+
|
9
|
+
ssh_connection = ::ZTK::SSH.new(
|
10
|
+
:host_name => "127.0.0.1",
|
11
|
+
:user => ENV['USER'],
|
12
|
+
:keys => "#{ENV['HOME']}/.ssh/id_rsa"
|
13
|
+
).ssh
|
14
|
+
|
15
|
+
@lxc = LXC.new
|
16
|
+
@lxc.use_ssh = ssh_connection
|
17
|
+
|
18
|
+
LXC::Container.new(@lxc, TEST_CONTAINER_NAME)
|
19
|
+
}
|
20
|
+
|
21
|
+
describe "class" do
|
22
|
+
|
23
|
+
it "should be an instance of LXC::Container" do
|
24
|
+
subject.should be_an_instance_of LXC::Container
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "attributes" do
|
28
|
+
|
29
|
+
describe "#name" do
|
30
|
+
|
31
|
+
it "should be readable and match what was passed to the initializer" do
|
32
|
+
subject.name.should == TEST_CONTAINER_NAME
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "methods" do
|
42
|
+
|
43
|
+
describe "#exists?" do
|
44
|
+
it "should return false for an un-created remote container" do
|
45
|
+
subject.exists?.should == false
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "#pid" do
|
50
|
+
it "should return -1 for an un-created remote container" do
|
51
|
+
subject.pid.should == -1
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "#state" do
|
56
|
+
it "should return stopped for an un-created remote container" do
|
57
|
+
subject.state.should == :stopped
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "#wait" do
|
62
|
+
it "should timeout while waiting for a non-existant remote container to start" do
|
63
|
+
subject.wait([:running], 1).should == false
|
64
|
+
%x(pkill -f lxc-wait)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should be successfully when waiting to stop a non-existant remote container" do
|
68
|
+
subject.wait([:stopped], 120).should == true
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
data/spec/lxc_spec.rb
CHANGED
@@ -30,7 +30,7 @@ describe LXC do
|
|
30
30
|
|
31
31
|
context "LXC Target Version #{lxc_version}" do
|
32
32
|
|
33
|
-
describe "ls" do
|
33
|
+
describe "#ls" do
|
34
34
|
|
35
35
|
context "with containers" do
|
36
36
|
|
@@ -58,7 +58,7 @@ describe LXC do
|
|
58
58
|
|
59
59
|
end
|
60
60
|
|
61
|
-
describe "exists?" do
|
61
|
+
describe "#exists?" do
|
62
62
|
|
63
63
|
context "with containers" do
|
64
64
|
|
@@ -90,7 +90,7 @@ describe LXC do
|
|
90
90
|
|
91
91
|
end
|
92
92
|
|
93
|
-
describe "ps" do
|
93
|
+
describe "#ps" do
|
94
94
|
it "should return an array of strings representing the lxc process list" do
|
95
95
|
subject.stub(:exec) { lxc_fixture(lxc_version, "lxc-ps.out") }
|
96
96
|
|
@@ -99,7 +99,7 @@ describe LXC do
|
|
99
99
|
end
|
100
100
|
end
|
101
101
|
|
102
|
-
describe "version" do
|
102
|
+
describe "#version" do
|
103
103
|
it "should return a string representation of the installed LXC version" do
|
104
104
|
subject.stub(:exec) { lxc_fixture(lxc_version, 'lxc-version.out') }
|
105
105
|
|
@@ -109,7 +109,7 @@ describe LXC do
|
|
109
109
|
end
|
110
110
|
end
|
111
111
|
|
112
|
-
describe "checkconfig" do
|
112
|
+
describe "#checkconfig" do
|
113
113
|
it "should return an array of strings representing the LXC configuration" do
|
114
114
|
subject.stub(:exec) { lxc_fixture(lxc_version, 'lxc-checkconfig.out') }
|
115
115
|
|
@@ -121,14 +121,14 @@ describe LXC do
|
|
121
121
|
end
|
122
122
|
end
|
123
123
|
|
124
|
-
describe "container" do
|
124
|
+
describe "#container" do
|
125
125
|
it "should return a container object for the requested container" do
|
126
126
|
result = subject.container("devop-test-1")
|
127
127
|
result.should be_an_instance_of(::LXC::Container)
|
128
128
|
end
|
129
129
|
end
|
130
130
|
|
131
|
-
describe "containers" do
|
131
|
+
describe "#containers" do
|
132
132
|
|
133
133
|
context "with containers" do
|
134
134
|
it "should return an array of container objects" do
|
@@ -150,14 +150,14 @@ describe LXC do
|
|
150
150
|
|
151
151
|
end
|
152
152
|
|
153
|
-
describe "inspect" do
|
153
|
+
describe "#inspect" do
|
154
154
|
it "should return an information string about our class instance" do
|
155
155
|
subject.inspect.should be_kind_of(String)
|
156
156
|
subject.inspect.length.should be > 0
|
157
157
|
end
|
158
158
|
end
|
159
159
|
|
160
|
-
describe "exec" do
|
160
|
+
describe "#exec" do
|
161
161
|
|
162
162
|
context "against local host" do
|
163
163
|
it "should exec the supplied LXC command" do
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: lxc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.5
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Zachary Patten
|
@@ -133,6 +133,7 @@ files:
|
|
133
133
|
- spec/fixtures/0.8.0-rc2/lxc-ls-wo-containers.out
|
134
134
|
- spec/fixtures/0.8.0-rc2/lxc-ps.out
|
135
135
|
- spec/fixtures/0.8.0-rc2/lxc-version.out
|
136
|
+
- spec/lxc/container_spec.rb
|
136
137
|
- spec/lxc_spec.rb
|
137
138
|
- spec/spec_helper.rb
|
138
139
|
homepage: https://github.com/zpatten/lxc
|
@@ -148,7 +149,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
148
149
|
requirements:
|
149
150
|
- - ">="
|
150
151
|
- !ruby/object:Gem::Version
|
151
|
-
hash:
|
152
|
+
hash: 479965379080138848
|
152
153
|
segments:
|
153
154
|
- 0
|
154
155
|
version: "0"
|
@@ -157,7 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
157
158
|
requirements:
|
158
159
|
- - ">="
|
159
160
|
- !ruby/object:Gem::Version
|
160
|
-
hash:
|
161
|
+
hash: 479965379080138848
|
161
162
|
segments:
|
162
163
|
- 0
|
163
164
|
version: "0"
|
@@ -179,6 +180,7 @@ test_files:
|
|
179
180
|
- spec/fixtures/0.8.0-rc2/lxc-ls-wo-containers.out
|
180
181
|
- spec/fixtures/0.8.0-rc2/lxc-ps.out
|
181
182
|
- spec/fixtures/0.8.0-rc2/lxc-version.out
|
183
|
+
- spec/lxc/container_spec.rb
|
182
184
|
- spec/lxc_spec.rb
|
183
185
|
- spec/spec_helper.rb
|
184
186
|
has_rdoc:
|