donce 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -1
- data/donce.gemspec +1 -1
- data/lib/donce.rb +10 -10
- data/test/test_donce.rb +18 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 87e27e95378a5ad34a9ba6a7ef7c8578efb3d24990998beada04097c1a46d5c1
|
4
|
+
data.tar.gz: 0ea31ae8977024be7abdbc164a3250a8eb907511ea24b5427b2eb3f8960b2912
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df41062bb8aea186f7bafc2df0070d7abbf1238d2eea5270f49a62e1901a495b0d50afca4659b5314ea0ea0117a299241b8757369a07b5e384cd8595abf956f2
|
7
|
+
data.tar.gz: 2d6de2cde5d5df5dc3048bb7e9d68cc8b866ec76842f9ce7f9441a4b3b1f683a58e65428c558244c02ab2bcf1a6760606f1e63cea7a7c1a77f08778a18c59fd4
|
data/README.md
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
[](https://github.com/yegor256/donce/blob/master/LICENSE.txt)
|
13
13
|
|
14
14
|
This small Ruby library helps building temporary [Docker]
|
15
|
-
images,
|
15
|
+
images, runs Docker containers, and cleans up afterwards — it may be
|
16
16
|
convenient for automated tests (for example, with [Minitest]):
|
17
17
|
|
18
18
|
```ruby
|
@@ -29,6 +29,15 @@ class MyTest < Minitest::Test
|
|
29
29
|
end
|
30
30
|
```
|
31
31
|
|
32
|
+
It's possible to run Docker image in a background mode too:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
stdout = donce(image: 'ubuntu', command: 'sleep 9999') do |id|
|
36
|
+
# The "id" is the container id
|
37
|
+
# The "donce_host()" is the hostname of it
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
32
41
|
That's it.
|
33
42
|
|
34
43
|
## How to contribute
|
data/donce.gemspec
CHANGED
@@ -26,7 +26,7 @@ Gem::Specification.new do |s|
|
|
26
26
|
s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
|
27
27
|
s.required_ruby_version = '>=3.0'
|
28
28
|
s.name = 'donce'
|
29
|
-
s.version = '0.0.
|
29
|
+
s.version = '0.0.4'
|
30
30
|
s.license = 'MIT'
|
31
31
|
s.summary = 'Builds and starts temporary Docker containers'
|
32
32
|
s.description =
|
data/lib/donce.rb
CHANGED
@@ -89,16 +89,18 @@ module Kernel
|
|
89
89
|
if image
|
90
90
|
image
|
91
91
|
else
|
92
|
-
i = "donce-#{SecureRandom.hex(
|
92
|
+
i = "donce-#{SecureRandom.hex(6)}"
|
93
93
|
if dockerfile
|
94
|
-
Dir.mktmpdir do |
|
95
|
-
File.write(File.join(
|
96
|
-
qbash("#{docker} build #{Shellwords.escape(
|
94
|
+
Dir.mktmpdir do |tmp|
|
95
|
+
File.write(File.join(tmp, 'Dockerfile'), dockerfile)
|
96
|
+
qbash("#{docker} build #{Shellwords.escape(tmp)} -t #{i}", log:)
|
97
97
|
end
|
98
|
+
else
|
99
|
+
qbash("#{docker} build #{Shellwords.escape(home)} -t #{i}", log:)
|
98
100
|
end
|
99
101
|
i
|
100
102
|
end
|
101
|
-
container = "donce-#{SecureRandom.hex(
|
103
|
+
container = "donce-#{SecureRandom.hex(6)}"
|
102
104
|
begin
|
103
105
|
stdout = nil
|
104
106
|
code = 0
|
@@ -130,16 +132,14 @@ module Kernel
|
|
130
132
|
"Failed to run #{cmd} " \
|
131
133
|
"(exit code is ##{code}, stdout has #{stdout.split("\n").count} lines)"
|
132
134
|
end
|
133
|
-
if block_given?
|
134
|
-
r = yield container
|
135
|
-
return r
|
136
|
-
end
|
135
|
+
yield container if block_given?
|
137
136
|
ensure
|
138
|
-
qbash(
|
137
|
+
logs = qbash(
|
139
138
|
"#{docker} logs #{Shellwords.escape(container)}",
|
140
139
|
level: code.zero? ? Logger::DEBUG : Logger::ERROR,
|
141
140
|
log:
|
142
141
|
)
|
142
|
+
stdout = logs if block_given?
|
143
143
|
qbash("#{docker} rm -f #{Shellwords.escape(container)}", log:)
|
144
144
|
end
|
145
145
|
stdout
|
data/test/test_donce.rb
CHANGED
@@ -34,6 +34,19 @@ class TestDonce < Minitest::Test
|
|
34
34
|
assert_equal("hello\n", stdout)
|
35
35
|
end
|
36
36
|
|
37
|
+
def test_runs_existing_image
|
38
|
+
stdout = donce(image: 'ubuntu:24.04', command: 'echo hello', log: Loog::NULL)
|
39
|
+
assert_equal("hello\n", stdout)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_runs_from_home
|
43
|
+
Dir.mktmpdir do |home|
|
44
|
+
File.write(File.join(home, 'Dockerfile'), "FROM ubuntu\nCMD echo hello")
|
45
|
+
stdout = donce(home:, log: Loog::NULL)
|
46
|
+
assert_equal("hello\n", stdout)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
37
50
|
def test_runs_daemon
|
38
51
|
seen = false
|
39
52
|
donce(dockerfile: "FROM ubuntu\nCMD while true; do sleep 1; echo sleeping; done", log: Loog::NULL) do |id|
|
@@ -42,4 +55,9 @@ class TestDonce < Minitest::Test
|
|
42
55
|
end
|
43
56
|
assert(seen)
|
44
57
|
end
|
58
|
+
|
59
|
+
def test_returns_stdout_from_daemon
|
60
|
+
stdout = donce(dockerfile: "FROM ubuntu\nCMD echo hello", log: Loog::NULL) { |_| sleep 0.1 }
|
61
|
+
assert_equal("hello\n", stdout)
|
62
|
+
end
|
45
63
|
end
|