Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint a random file #3874

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions nginx_stage/spec/generators/pun_config_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,63 +1,64 @@
# frozen_string_literal: true

require 'spec_helper'
require 'nginx_stage'

describe NginxStage::PunConfigGenerator do
let(:test_user) { "spec" }
let(:test_user) { 'spec' }
let(:test_user_gid) { 1000 }

before(:each) {
before do
etc_stub = {
:gid => test_user_gid,
:gid => test_user_gid,
:name => test_user
}

allow(Etc).to receive(:getpwnam).with(test_user).and_return(Struct.new(*etc_stub.keys).new(*etc_stub.values))
allow(Etc).to receive(:getgrgid).with(test_user_gid).and_return(Struct.new(*etc_stub.keys).new(*etc_stub.values))
allow_any_instance_of(NginxStage::User).to receive(:get_groups).and_return([test_user])
}
end

it 'has the correct options' do
expect(NginxStage::PunConfigGenerator.options.keys).to eq([:user, :skip_nginx, :app_init_url, :pre_hook_root_cmd])
expect(described_class.options.keys).to eq([:user, :skip_nginx, :app_init_url, :pre_hook_root_cmd])
end

it 'requires the user option' do
expect { described_class.new }.to raise_error(NginxStage::MissingOption, "missing option: user")
expect { described_class.new }.to raise_error(NginxStage::MissingOption, 'missing option: user')
end


describe 'pre_hook_root_cmd' do

let(:generator){
let(:generator) do
described_class.new({
:user => test_user,
:pre_hook_root_cmd => '/opt/pre_hook'
})
}
:user => test_user,
:pre_hook_root_cmd => '/opt/pre_hook'
})
end

let(:hook){
let(:hook) do
generator.class.hooks[:exec_pre_hook]
}
end

it 'invokes the right root pre hook' do
allow(Open3).to receive(:capture3).with('/opt/pre_hook', '--user', test_user)
generator.instance_eval(&hook)
end

it 'logs exceptions from underlying script' do
allow(Open3).to receive(:capture3).with('/opt/pre_hook', '--user', test_user).and_raise(StandardError.new "this is a test")
allow(Open3).to receive(:capture3).with('/opt/pre_hook', '--user',
test_user).and_raise(StandardError.new('this is a test'))
allow_any_instance_of(Syslog::Logger).to receive(:error).with("/opt/pre_hook threw exception 'this is a test' for spec")
generator.instance_eval(&hook)
end

it 'logs non-zero exits from underlying script' do
allow(Open3).to receive(:capture3)
.with('/opt/pre_hook', '--user', test_user)
.and_return(["", "this is the test stderr message", double(:success? => false, :exitstatus => 3)])
.and_return(['', 'this is the test stderr message', double(:success? => false, :exitstatus => 3)])

allow_any_instance_of(Syslog::Logger).to receive(:error)
.with("/opt/pre_hook exited with 3 for user spec. stderr was 'this is the test stderr message'")

generator.instance_eval(&hook)
end
end
end
end
Loading