diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1d6814c..fd47c7d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -6,7 +6,7 @@ jobs: fail-fast: false matrix: os: [ ubuntu-latest, macos-latest ] - ruby: [ '2.7', '3.0', '3.1' ] + ruby: [ '3.2', '3.3', '3.4' ] runs-on: ${{ matrix.os }} diff --git a/berkeley_library-docker.gemspec b/berkeley_library-docker.gemspec index c9d67f8..8a9de87 100644 --- a/berkeley_library-docker.gemspec +++ b/berkeley_library-docker.gemspec @@ -25,7 +25,8 @@ Gem::Specification.new do |spec| spec.required_ruby_version = ">= #{ruby_version}" - spec.add_development_dependency 'rails', '~> 6' + spec.add_development_dependency 'fakefs' + spec.add_development_dependency 'rails', '~> 8' spec.add_development_dependency 'rake' spec.add_development_dependency 'rspec' spec.add_development_dependency 'rspec_junit_formatter' diff --git a/lib/berkeley_library/docker.rb b/lib/berkeley_library/docker.rb index c673670..40ea1d9 100644 --- a/lib/berkeley_library/docker.rb +++ b/lib/berkeley_library/docker.rb @@ -7,7 +7,7 @@ module BerkeleyLibrary module Docker class << self def running_in_container? - File.exist?('/.dockerenv') || init_cgroup_is_dockerish? + File.exist?('/.dockerenv') || init_cgroup_is_dockerish? || containerenv_exists? || container_in_env? end private @@ -19,6 +19,14 @@ def init_cgroup_is_dockerish? false end end + + def containerenv_exists? + File.exist?('/run/.containerenv') || File.exist?('/var/run/.containerenv') + end + + def container_in_env? + ENV.fetch('container', '') != '' + end end end end diff --git a/lib/berkeley_library/docker/module_info.rb b/lib/berkeley_library/docker/module_info.rb index 9875b0f..3536224 100644 --- a/lib/berkeley_library/docker/module_info.rb +++ b/lib/berkeley_library/docker/module_info.rb @@ -1,14 +1,16 @@ +# frozen_string_literal: true + module BerkeleyLibrary module Docker class ModuleInfo - NAME = 'berkeley_library-docker'.freeze - AUTHOR = 'Dan Schmidt'.freeze - AUTHOR_EMAIL = 'danschmidt5189@berkeley.edu'.freeze - SUMMARY = 'Utility functions for Dockerizing Ruby apps'.freeze - DESCRIPTION = 'Utility functions for making Ruby apps "just work" in Docker containers.'.freeze - LICENSE = 'MIT'.freeze - VERSION = '0.2.0'.freeze - HOMEPAGE = 'https://github.com/BerkeleyLibrary/docker'.freeze + NAME = 'berkeley_library-docker' + AUTHOR = 'Dan Schmidt' + AUTHOR_EMAIL = 'danschmidt5189@berkeley.edu' + SUMMARY = 'Utility functions for Dockerizing Ruby apps' + DESCRIPTION = 'Utility functions for making Ruby apps "just work" in Docker containers.' + LICENSE = 'MIT' + VERSION = '0.3.0.a1' + HOMEPAGE = 'https://github.com/BerkeleyLibrary/ruby-docker' private_class_method :new end diff --git a/spec/berkeley_library/docker_spec.rb b/spec/berkeley_library/docker_spec.rb index 5e3f68f..f53fbb7 100644 --- a/spec/berkeley_library/docker_spec.rb +++ b/spec/berkeley_library/docker_spec.rb @@ -1,10 +1,13 @@ require 'spec_helper' require 'rails' # require Rails first to mimic load order +require 'fakefs/spec_helpers' require 'berkeley_library/docker' module BerkeleyLibrary describe Docker do describe '#running_in_container?' do + include FakeFS::SpecHelpers + DOCKERISH_CGROUP = <<~EOL 11:cpuacct,cpu:/docker/12345 10:devices:/docker/12345 @@ -33,44 +36,47 @@ module BerkeleyLibrary 1:name=systemd:/ EOL + around(:example) do |example| + current_env = ENV.to_hash + begin + ENV.clear + example.run + ensure + ENV.replace(current_env) + end + end + it 'is true when /.dockerenv exists' do - mock_dockerenv + FileUtils.touch('/.dockerenv') expect(BerkeleyLibrary::Docker.running_in_container?).to be true end it 'is true when /proc/1/cgroup is docker-like' do - mock_dockerenv(false) - mock_init_cgroup + FileUtils.mkdir_p('/proc/1') + File.write('/proc/1/cgroup', DOCKERISH_CGROUP) expect(BerkeleyLibrary::Docker.running_in_container?).to be true end - it 'is false when /proc/1/cgroup is traditional' do - mock_dockerenv(false) - mock_init_cgroup(false) - expect(BerkeleyLibrary::Docker.running_in_container?).to be false + it 'is true when `container` is present in environment' do + ENV['container'] = 'lxc-libvirt' + expect(BerkeleyLibrary::Docker.running_in_container?).to be true end - it 'is false when /proc/1/cgroup does not exist' do - mock_dockerenv(false) - expect(File) - .to receive(:open).with('/proc/1/cgroup') - .and_raise(Errno::ENOENT) - expect(BerkeleyLibrary::Docker.running_in_container?).to be false + it 'is true when /run/.containerenv exists' do + FileUtils.mkdir('/run') + FileUtils.touch('/run/.containerenv') + expect(BerkeleyLibrary::Docker.running_in_container?).to be true end - private + it 'is false when /proc/1/cgroup is traditional' do + FileUtils.mkdir_p('/proc/1') + File.write('/proc/1/cgroup', TRADITIONAL_CGROUP) - def mock_dockerenv(exists = true) - expect(File) - .to receive(:exist?).with('/.dockerenv') - .and_return(exists) + expect(BerkeleyLibrary::Docker.running_in_container?).to be false end - def mock_init_cgroup(dockerish = true) - expect(File) - .to receive(:open).with('/proc/1/cgroup') - .and_return( - StringIO.new(dockerish ? DOCKERISH_CGROUP : TRADITIONAL_CGROUP)) + it 'is false when none of the conditions are true' do + expect(BerkeleyLibrary::Docker.running_in_container?).to be false end end end