From 459e30efb651ece57f49453d18ff9ee6ee3326ac Mon Sep 17 00:00:00 2001 From: Calle Englund Date: Mon, 23 Jun 2025 13:50:19 +0200 Subject: [PATCH 1/3] Run gunicorn specs for all supported os --- spec/defines/gunicorn_spec.rb | 41 ++++++++++++++--------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/spec/defines/gunicorn_spec.rb b/spec/defines/gunicorn_spec.rb index 13284087..b9258a58 100644 --- a/spec/defines/gunicorn_spec.rb +++ b/spec/defines/gunicorn_spec.rb @@ -5,37 +5,28 @@ describe 'python::gunicorn', type: :define do let(:title) { 'test-app' } - context 'on Debian OS' do - let :facts do - { - id: 'root', - kernel: 'Linux', - lsbdistcodename: 'squeeze', - osfamily: 'Debian', - operatingsystem: 'Debian', - operatingsystemrelease: '6', - path: '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', - concat_basedir: '/dne' - } - end + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) { facts } - describe 'test-app with default parameter values' do - context 'configures test app with default parameter values' do - let(:params) { { dir: '/srv/testapp' } } + describe 'test-app with default parameter values' do + context 'configures test app with default parameter values' do + let(:params) { { dir: '/srv/testapp' } } - it { is_expected.to contain_file('/etc/gunicorn.d/test-app').with_mode('0644').with_content(%r{--log-level=error}) } - end + it { is_expected.to contain_file('/etc/gunicorn.d/test-app').with_mode('0644').with_content(%r{--log-level=error}) } + end - context 'test-app with custom log level' do - let(:params) { { dir: '/srv/testapp', log_level: 'info' } } + context 'test-app with custom log level' do + let(:params) { { dir: '/srv/testapp', log_level: 'info' } } - it { is_expected.to contain_file('/etc/gunicorn.d/test-app').with_mode('0644').with_content(%r{--log-level=info}) } - end + it { is_expected.to contain_file('/etc/gunicorn.d/test-app').with_mode('0644').with_content(%r{--log-level=info}) } + end - context 'test-app with custom gunicorn preload arguments' do - let(:params) { { dir: '/srv/testapp', args: ['--preload'] } } + context 'test-app with custom gunicorn preload arguments' do + let(:params) { { dir: '/srv/testapp', args: ['--preload'] } } - it { is_expected.to contain_file('/etc/gunicorn.d/test-app').with_mode('0644').with_content(%r{--preload}) } + it { is_expected.to contain_file('/etc/gunicorn.d/test-app').with_mode('0644').with_content(%r{--preload}) } + end end end end From 91d351ecc6ab04668d2e347b5d9b62d80a2d9799 Mon Sep 17 00:00:00 2001 From: Calle Englund Date: Mon, 23 Jun 2025 17:20:14 +0200 Subject: [PATCH 2/3] Expect current default for gunicorn workers Given that legacy facts were removed from facterdb 3, the current behavior relies on `NilClass#to_i` resolving to missing fact to 0. The added test case for custom `workers` parameter given cannot give false positives with the default formula always being an odd number. --- spec/defines/gunicorn_spec.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/spec/defines/gunicorn_spec.rb b/spec/defines/gunicorn_spec.rb index b9258a58..dc4d8bda 100644 --- a/spec/defines/gunicorn_spec.rb +++ b/spec/defines/gunicorn_spec.rb @@ -12,8 +12,13 @@ describe 'test-app with default parameter values' do context 'configures test app with default parameter values' do let(:params) { { dir: '/srv/testapp' } } + let(:expected_workers) do + # templates/gunicorn.erb + (facts[:processorcount].to_i * 2) + 1 + end it { is_expected.to contain_file('/etc/gunicorn.d/test-app').with_mode('0644').with_content(%r{--log-level=error}) } + it { is_expected.to contain_file('/etc/gunicorn.d/test-app').with_mode('0644').with_content(%r{--workers=#{expected_workers}}) } end context 'test-app with custom log level' do @@ -27,6 +32,12 @@ it { is_expected.to contain_file('/etc/gunicorn.d/test-app').with_mode('0644').with_content(%r{--preload}) } end + + context 'test-app with custom workers count' do + let(:params) { { dir: '/srv/testapp', workers: 42 } } + + it { is_expected.to contain_file('/etc/gunicorn.d/test-app').with_mode('0644').with_content(%r{--workers=#{params[:workers]}}) } + end end end end From 67c1a82e75f4de12d5e460aae21e4f271cffaea6 Mon Sep 17 00:00:00 2001 From: Calle Englund Date: Mon, 23 Jun 2025 17:59:28 +0200 Subject: [PATCH 3/3] Pick default gunicorn workers from non-legacy fact While the documentation for the core fact `processors.count` states that it is an _integer_ already, the previous behavior was to silently treat a missing fact as a processor count of zero, via `NilClass#to_i`. This does not happen in FacterDB entries for supported os, but the spec still mirrors the conversion done by the template. --- manifests/gunicorn.pp | 2 ++ spec/defines/gunicorn_spec.rb | 5 ++++- templates/gunicorn.erb | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/manifests/gunicorn.pp b/manifests/gunicorn.pp index 7670f394..5393ac33 100644 --- a/manifests/gunicorn.pp +++ b/manifests/gunicorn.pp @@ -55,6 +55,8 @@ String[1] $template = 'python/gunicorn.erb', Array $args = [], ) { + $processor_count = fact('processors.count') + if $manage_config_dir { file { $config_dir: ensure => directory, diff --git a/spec/defines/gunicorn_spec.rb b/spec/defines/gunicorn_spec.rb index dc4d8bda..de36c580 100644 --- a/spec/defines/gunicorn_spec.rb +++ b/spec/defines/gunicorn_spec.rb @@ -13,8 +13,11 @@ context 'configures test app with default parameter values' do let(:params) { { dir: '/srv/testapp' } } let(:expected_workers) do + # manifests/gunicorn.pp + processor_count = facts.dig(:processors, 'count') + # templates/gunicorn.erb - (facts[:processorcount].to_i * 2) + 1 + (processor_count.to_i * 2) + 1 end it { is_expected.to contain_file('/etc/gunicorn.d/test-app').with_mode('0644').with_content(%r{--log-level=error}) } diff --git a/templates/gunicorn.erb b/templates/gunicorn.erb index 07605ade..57dd4748 100644 --- a/templates/gunicorn.erb +++ b/templates/gunicorn.erb @@ -39,7 +39,7 @@ CONFIG = { <% if @workers -%> '--workers=<%= @workers %>', <% else -%> - '--workers=<%= @processorcount.to_i*2 + 1 %>', + '--workers=<%= @processor_count.to_i*2 + 1 %>', <% end -%> '--timeout=<%= @timeout %>', <% if @access_log_format -%>