Skip to content

Commit

Permalink
feat: add option to overwrite Docker read and open timeout values
Browse files Browse the repository at this point in the history
  • Loading branch information
yeikel committed Dec 13, 2024
1 parent d1608d6 commit 6e51aee
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
16 changes: 15 additions & 1 deletion docker/lib/dependabot/docker/update_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,27 @@ def docker_repo_name
"library/#{dependency.name}"
end

DEFAULT_OPEN_TIMEOUT_IN_SECONDS = 2
DEFAULT_READ_TIMEOUT_IN_SECONDS = 10

class << self
def read_timeout_in_seconds
ENV.fetch("DEPENDABOT_DOCKER_READ_TIMEOUT_IN_SECONDS", DEFAULT_READ_TIMEOUT_IN_SECONDS).to_i
end

def open_timeout_in_seconds
ENV.fetch("DEPENDABOT_DOCKER_OPEN_TIMEOUT_IN_SECONDS", DEFAULT_OPEN_TIMEOUT_IN_SECONDS).to_i
end
end

def docker_registry_client
@docker_registry_client ||=
DockerRegistry2::Registry.new(
"https://#{registry_hostname}",
user: registry_credentials&.fetch("username", nil),
password: registry_credentials&.fetch("password", nil),
read_timeout: 10,
read_timeout: :read_timeout_in_seconds,
open_timeout: :open_timeout_in_seconds,
http_options: { proxy: ENV.fetch("HTTPS_PROXY", nil) }
)
end
Expand Down
36 changes: 36 additions & 0 deletions docker/spec/dependabot/docker/update_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,42 @@ def stub_tag_with_no_digest(tag)
end
end

describe ".read_timeout_in_seconds" do
context "when DEPENDABOT_DOCKER_READ_TIMEOUT_IN_SECONDS is set" do
it "returns the provided value" do
override_value = 10
stub_const("ENV", ENV.to_hash.merge("DEPENDABOT_DOCKER_READ_TIMEOUT_IN_SECONDS" => override_value))

expect(described_class.send(:read_timeout_in_seconds)).to eq(override_value)
end
end

context "when ENV does not provide an override" do
it "falls back to a default value" do
expect(described_class.send(:read_timeout_in_seconds))
.to eq(described_class::DEFAULT_READ_TIMEOUT_IN_SECONDS)
end
end
end

describe ".open_timeout_in_seconds" do
context "when DEPENDABOT_DOCKER_OPEN_TIMEOUT_IN_SECONDS is set" do
it "returns the provided value" do
override_value = 10
stub_const("ENV", ENV.to_hash.merge("DEPENDABOT_DOCKER_OPEN_TIMEOUT_IN_SECONDS" => override_value))

expect(described_class.send(:open_timeout_in_seconds)).to eq(override_value)
end
end

context "when ENV does not provide an override" do
it "falls back to a default value" do
expect(described_class.send(:open_timeout_in_seconds))
.to eq(described_class::DEFAULT_OPEN_TIMEOUT_IN_SECONDS)
end
end
end

private

def stub_same_sha_for(*tags)
Expand Down

0 comments on commit 6e51aee

Please sign in to comment.