Skip to content

Commit 7bf3b5b

Browse files
committed
Add default labels (lang and version) to all containers started
Closes #43
1 parent a61b013 commit 7bf3b5b

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

core/lib/testcontainers/docker_container.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ def initialize(image, name: nil, command: nil, entrypoint: nil, exposed_ports: n
5252
@volumes = add_volumes(volumes) if volumes
5353
@env = add_env(env) if env
5454
@filesystem_binds = add_filesystem_binds(filesystem_binds) if filesystem_binds
55-
@labels = add_labels(labels) if labels
55+
@labels = {}
56+
add_labels(default_labels)
57+
add_labels(labels) if labels
5658
@working_dir = working_dir
5759
@healthcheck = add_healthcheck(healthcheck) if healthcheck
5860
@wait_for = add_wait_for(wait_for)
@@ -1005,6 +1007,13 @@ def store_file(path, contents)
10051007

10061008
private
10071009

1010+
def default_labels
1011+
{
1012+
"org.testcontainers.lang" => "ruby",
1013+
"org.testcontainers.version" => Testcontainers::VERSION
1014+
}
1015+
end
1016+
10081017
def normalize_ports(ports)
10091018
return if ports.nil?
10101019
return ports if ports.is_a?(Hash)

core/test/docker_container_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def test_it_creates_and_returns_the_container_volumes
110110

111111
def test_it_returns_the_container_labels
112112
container = Testcontainers::DockerContainer.new("hello-world", labels: {"foo" => "bar"})
113-
assert_equal({"foo" => "bar"}, container.labels)
113+
assert_equal({"org.testcontainers.lang" => "ruby", "org.testcontainers.version" => "0.2.0", "foo" => "bar"}, container.labels)
114114
end
115115

116116
def test_it_returns_the_container_env
@@ -182,7 +182,7 @@ def test_it_adds_a_label
182182
container = Testcontainers::DockerContainer.new("hello-world")
183183
container.add_label("foo", "bar")
184184

185-
assert_equal({"foo" => "bar"}, container.labels)
185+
assert_equal({"org.testcontainers.lang" => "ruby", "org.testcontainers.version" => "0.2.0", "foo" => "bar"}, container.labels)
186186
end
187187

188188
def test_it_adds_a_wait_for_with_default
@@ -234,7 +234,7 @@ def test_it_can_be_mutated_with_the_with_method
234234
assert_equal({"80/tcp" => [{"HostPort" => "8080"}], "443/tcp" => [{"HostPort" => ""}]}, container.port_bindings)
235235
assert_equal({"/tmp" => {}, "/root" => {}}, container.volumes)
236236
assert_equal(["/tmp/docker:/tmp:rw", "/home/user:/root:rw"], container.filesystem_binds)
237-
assert_equal({"foo" => "bar"}, container.labels)
237+
assert_equal({"org.testcontainers.lang" => "ruby", "org.testcontainers.version" => "0.2.0", "foo" => "bar"}, container.labels)
238238
assert_equal(["PATH=/usr/bin"], container.env)
239239
assert_equal("/app", container.working_dir)
240240
assert_kind_of(Proc, container.wait_for)

0 commit comments

Comments
 (0)